1 |
root |
1.3 |
/* |
2 |
|
|
* math support |
3 |
|
|
* most of the more complicated code is taken from mesa. |
4 |
|
|
*/ |
5 |
|
|
|
6 |
root |
1.2 |
#include <cstdio> // ugly |
7 |
root |
1.25 |
#include <cstring> |
8 |
root |
1.1 |
#include <cmath> |
9 |
|
|
|
10 |
root |
1.4 |
#include <sys/time.h> |
11 |
|
|
#include <time.h> |
12 |
root |
1.20 |
|
13 |
root |
1.15 |
#include <GL/gl.h> |
14 |
root |
1.25 |
#include <GL/glu.h> |
15 |
root |
1.20 |
#include <GL/glut.h> |
16 |
root |
1.2 |
|
17 |
root |
1.1 |
#include "util.h" |
18 |
root |
1.15 |
#include "entity.h" |
19 |
root |
1.1 |
|
20 |
root |
1.3 |
#define DEG2RAD (M_PI / 180.) |
21 |
|
|
|
22 |
root |
1.6 |
void renormalize (sector &s, point &p) |
23 |
|
|
{ |
24 |
|
|
float i; |
25 |
|
|
|
26 |
root |
1.11 |
p.x = modff (p.x, &i); s.x += (soffs)i; |
27 |
|
|
p.y = modff (p.y, &i); s.y += (soffs)i; |
28 |
|
|
p.z = modff (p.z, &i); s.z += (soffs)i; |
29 |
root |
1.6 |
} |
30 |
|
|
|
31 |
root |
1.7 |
///////////////////////////////////////////////////////////////////////////// |
32 |
|
|
|
33 |
root |
1.1 |
const vec3 normalize (const vec3 &v) |
34 |
|
|
{ |
35 |
root |
1.3 |
GLfloat s = abs (v); |
36 |
|
|
|
37 |
|
|
if (!s) |
38 |
|
|
return v; |
39 |
root |
1.1 |
|
40 |
root |
1.3 |
s = 1. / s; |
41 |
root |
1.1 |
return vec3 (v.x * s, v.y * s, v.z * s); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
const vec3 cross (const vec3 &a, const vec3 &b) |
45 |
|
|
{ |
46 |
|
|
return vec3 ( |
47 |
root |
1.3 |
a.y * b.z - a.z * b.y, |
48 |
|
|
a.z * b.x - a.x * b.z, |
49 |
|
|
a.x * b.y - a.y * b.x |
50 |
|
|
); |
51 |
root |
1.2 |
} |
52 |
|
|
|
53 |
root |
1.7 |
///////////////////////////////////////////////////////////////////////////// |
54 |
|
|
|
55 |
root |
1.12 |
void matrix::diagonal (GLfloat v) |
56 |
root |
1.2 |
{ |
57 |
|
|
for (int i = 4; i--; ) |
58 |
|
|
for (int j = 4; j--; ) |
59 |
|
|
data[i][j] = i == j ? v : 0.; |
60 |
|
|
} |
61 |
|
|
|
62 |
root |
1.12 |
const matrix operator *(const matrix &a, const matrix &b) |
63 |
root |
1.2 |
{ |
64 |
root |
1.12 |
matrix r; |
65 |
root |
1.2 |
|
66 |
root |
1.3 |
// taken from mesa |
67 |
|
|
for (int i = 0; i < 4; i++) |
68 |
|
|
{ |
69 |
|
|
const GLfloat ai0=a(i,0), ai1=a(i,1), ai2=a(i,2), ai3=a(i,3); |
70 |
|
|
|
71 |
|
|
r(i,0) = ai0 * b(0,0) + ai1 * b(1,0) + ai2 * b(2,0) + ai3 * b(3,0); |
72 |
|
|
r(i,1) = ai0 * b(0,1) + ai1 * b(1,1) + ai2 * b(2,1) + ai3 * b(3,1); |
73 |
|
|
r(i,2) = ai0 * b(0,2) + ai1 * b(1,2) + ai2 * b(2,2) + ai3 * b(3,2); |
74 |
|
|
r(i,3) = ai0 * b(0,3) + ai1 * b(1,3) + ai2 * b(2,3) + ai3 * b(3,3); |
75 |
|
|
} |
76 |
root |
1.2 |
|
77 |
root |
1.3 |
return r; |
78 |
|
|
} |
79 |
root |
1.2 |
|
80 |
root |
1.12 |
const matrix matrix::rotation (GLfloat angle, const vec3 &axis) |
81 |
root |
1.3 |
{ |
82 |
|
|
GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c; |
83 |
root |
1.2 |
|
84 |
root |
1.3 |
s = (GLfloat) sinf (angle * DEG2RAD); |
85 |
|
|
c = (GLfloat) cosf (angle * DEG2RAD); |
86 |
|
|
|
87 |
|
|
const GLfloat mag = abs (axis); |
88 |
|
|
|
89 |
|
|
if (mag <= 1.0e-4) |
90 |
root |
1.12 |
return matrix (1); |
91 |
root |
1.3 |
|
92 |
root |
1.12 |
matrix m; |
93 |
root |
1.3 |
const vec3 n = axis * (1. / mag); |
94 |
|
|
|
95 |
|
|
xx = n.x * n.x; |
96 |
|
|
yy = n.y * n.y; |
97 |
|
|
zz = n.z * n.z; |
98 |
|
|
xy = n.x * n.y; |
99 |
|
|
yz = n.y * n.z; |
100 |
|
|
zx = n.z * n.x; |
101 |
|
|
xs = n.x * s; |
102 |
|
|
ys = n.y * s; |
103 |
|
|
zs = n.z * s; |
104 |
|
|
one_c = 1.0F - c; |
105 |
|
|
|
106 |
|
|
m(0,0) = (one_c * xx) + c; |
107 |
|
|
m(0,1) = (one_c * xy) - zs; |
108 |
|
|
m(0,2) = (one_c * zx) + ys; |
109 |
|
|
m(0,3) = 0; |
110 |
|
|
|
111 |
|
|
m(1,0) = (one_c * xy) + zs; |
112 |
|
|
m(1,1) = (one_c * yy) + c; |
113 |
|
|
m(1,2) = (one_c * yz) - xs; |
114 |
|
|
m(1,3) = 0; |
115 |
|
|
|
116 |
|
|
m(2,0) = (one_c * zx) - ys; |
117 |
|
|
m(2,1) = (one_c * yz) + xs; |
118 |
|
|
m(2,2) = (one_c * zz) + c; |
119 |
|
|
m(2,3) = 0; |
120 |
|
|
|
121 |
|
|
m(3,0) = 0; |
122 |
|
|
m(3,1) = 0; |
123 |
|
|
m(3,2) = 0; |
124 |
|
|
m(3,3) = 1; |
125 |
|
|
|
126 |
root |
1.9 |
return m; |
127 |
root |
1.3 |
} |
128 |
|
|
|
129 |
root |
1.12 |
const vec3 operator *(const matrix &a, const vec3 &v) |
130 |
root |
1.3 |
{ |
131 |
|
|
return vec3 ( |
132 |
root |
1.9 |
a(0,0) * v.x + a(0,1) * v.y + a(0,2) * v.z + a(0,3), |
133 |
|
|
a(1,0) * v.x + a(1,1) * v.y + a(1,2) * v.z + a(1,3), |
134 |
|
|
a(2,0) * v.x + a(2,1) * v.y + a(2,2) * v.z + a(2,3) |
135 |
root |
1.3 |
); |
136 |
root |
1.2 |
} |
137 |
|
|
|
138 |
root |
1.12 |
void matrix::print () |
139 |
root |
1.2 |
{ |
140 |
|
|
printf ("\n"); |
141 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][0], data[1][0], data[2][0], data[3][0]); |
142 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][1], data[1][1], data[2][1], data[3][1]); |
143 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][2], data[1][2], data[2][2], data[3][2]); |
144 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][3], data[1][3], data[2][3], data[3][3]); |
145 |
|
|
} |
146 |
|
|
|
147 |
root |
1.12 |
const matrix matrix::translation (const vec3 &v) |
148 |
root |
1.2 |
{ |
149 |
root |
1.12 |
matrix m(1); |
150 |
root |
1.2 |
|
151 |
root |
1.9 |
m(0,3) = v.x; |
152 |
|
|
m(1,3) = v.y; |
153 |
|
|
m(2,3) = v.z; |
154 |
root |
1.2 |
|
155 |
root |
1.9 |
return m; |
156 |
root |
1.1 |
} |
157 |
|
|
|
158 |
root |
1.7 |
///////////////////////////////////////////////////////////////////////////// |
159 |
|
|
|
160 |
|
|
plane::plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d) |
161 |
root |
1.9 |
: n (vec3 (a,b,c)) |
162 |
root |
1.7 |
{ |
163 |
|
|
GLfloat s = 1. / abs (n); |
164 |
|
|
|
165 |
|
|
n = n * s; |
166 |
root |
1.9 |
this->d = d * s; |
167 |
root |
1.7 |
} |
168 |
|
|
|
169 |
|
|
///////////////////////////////////////////////////////////////////////////// |
170 |
|
|
|
171 |
root |
1.1 |
void box::add (const box &o) |
172 |
|
|
{ |
173 |
|
|
a.x = min (a.x, o.a.x); |
174 |
|
|
a.y = min (a.y, o.a.y); |
175 |
|
|
a.z = min (a.z, o.a.z); |
176 |
|
|
b.x = max (b.x, o.b.x); |
177 |
|
|
b.y = max (b.y, o.b.y); |
178 |
|
|
b.z = max (b.z, o.b.z); |
179 |
|
|
} |
180 |
|
|
|
181 |
root |
1.19 |
void box::add (const point &p) |
182 |
root |
1.1 |
{ |
183 |
|
|
a.x = min (a.x, p.x); |
184 |
|
|
a.y = min (a.y, p.y); |
185 |
|
|
a.z = min (a.z, p.z); |
186 |
|
|
b.x = max (b.x, p.x); |
187 |
|
|
b.y = max (b.y, p.y); |
188 |
|
|
b.z = max (b.z, p.z); |
189 |
root |
1.5 |
} |
190 |
|
|
|
191 |
root |
1.7 |
///////////////////////////////////////////////////////////////////////////// |
192 |
root |
1.4 |
|
193 |
|
|
struct timer timer; |
194 |
|
|
static double base; |
195 |
|
|
double timer::now = 0.; |
196 |
|
|
double timer::diff; |
197 |
root |
1.24 |
//double min_frame = 1. / 60.; |
198 |
|
|
double min_frame = 1. / 600.; |
199 |
root |
1.4 |
|
200 |
|
|
void timer::frame () |
201 |
|
|
{ |
202 |
|
|
struct timeval tv; |
203 |
root |
1.23 |
double next; |
204 |
root |
1.4 |
|
205 |
root |
1.23 |
for (;;) |
206 |
|
|
{ |
207 |
|
|
gettimeofday (&tv, 0); |
208 |
|
|
|
209 |
|
|
next = tv.tv_sec - base + tv.tv_usec / 1.e6; |
210 |
|
|
diff = next - now; |
211 |
|
|
|
212 |
|
|
if (diff >= min_frame) |
213 |
|
|
break; |
214 |
|
|
|
215 |
|
|
SDL_Delay ((unsigned int)((min_frame - diff) * 1000.)); |
216 |
|
|
} |
217 |
root |
1.4 |
|
218 |
|
|
now = next; |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
timer::timer () |
222 |
|
|
{ |
223 |
|
|
struct timeval tv; |
224 |
|
|
gettimeofday (&tv, 0); |
225 |
|
|
base = tv.tv_sec + tv.tv_usec / 1.e6; |
226 |
|
|
} |
227 |
|
|
|
228 |
root |
1.13 |
GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
229 |
|
|
{ |
230 |
|
|
GLuint texture; |
231 |
|
|
int w, h; |
232 |
|
|
SDL_Surface *image; |
233 |
|
|
SDL_Rect area; |
234 |
|
|
Uint32 saved_flags; |
235 |
|
|
Uint8 saved_alpha; |
236 |
|
|
|
237 |
|
|
/* Use the surface width and height expanded to powers of 2 */ |
238 |
root |
1.14 |
//w = power_of_two (surface->w); |
239 |
|
|
//h = power_of_two (surface->h); |
240 |
root |
1.13 |
w = power_of_two (surface->w); |
241 |
|
|
h = power_of_two (surface->h); |
242 |
|
|
texcoord[0] = 0.0f; /* Min X */ |
243 |
|
|
texcoord[1] = 0.0f; /* Min Y */ |
244 |
|
|
texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
245 |
|
|
texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
246 |
|
|
|
247 |
|
|
image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
248 |
|
|
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
249 |
|
|
0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
250 |
|
|
#else |
251 |
|
|
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
252 |
|
|
#endif |
253 |
|
|
); |
254 |
|
|
if (image == NULL) |
255 |
|
|
{ |
256 |
|
|
return 0; |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
/* Save the alpha blending attributes */ |
260 |
|
|
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
261 |
|
|
saved_alpha = surface->format->alpha; |
262 |
|
|
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
263 |
|
|
{ |
264 |
|
|
SDL_SetAlpha (surface, 0, 0); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
/* Copy the surface into the GL texture image */ |
268 |
|
|
area.x = 0; |
269 |
|
|
area.y = 0; |
270 |
|
|
area.w = surface->w; |
271 |
|
|
area.h = surface->h; |
272 |
|
|
SDL_BlitSurface (surface, &area, image, &area); |
273 |
|
|
|
274 |
|
|
/* Restore the alpha blending attributes */ |
275 |
|
|
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
276 |
|
|
{ |
277 |
|
|
SDL_SetAlpha (surface, saved_flags, saved_alpha); |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
/* Create an OpenGL texture for the image */ |
281 |
|
|
glGenTextures (1, &texture); |
282 |
|
|
glBindTexture (GL_TEXTURE_2D, texture); |
283 |
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
284 |
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
285 |
|
|
glTexImage2D (GL_TEXTURE_2D, |
286 |
|
|
0, |
287 |
|
|
GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
288 |
|
|
SDL_FreeSurface (image); /* No longer needed */ |
289 |
|
|
|
290 |
|
|
return texture; |
291 |
root |
1.20 |
} |
292 |
|
|
|
293 |
|
|
void render_text (GLint x, GLint y, const char *str) |
294 |
|
|
{ |
295 |
|
|
glRasterPos2i (x, y); |
296 |
|
|
|
297 |
|
|
while (!*str) |
298 |
|
|
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, *str++); |
299 |
root |
1.15 |
} |
300 |
root |
1.13 |
|
301 |
root |
1.21 |
namespace gl { |
302 |
|
|
|
303 |
root |
1.24 |
void draw_box (const view &ctx, const sector &a, const sector &b) |
304 |
|
|
{ |
305 |
|
|
int i; |
306 |
|
|
static GLint verts[] = { |
307 |
|
|
0x8000, 0x8004, 0x8006, 0x8002, // -x |
308 |
|
|
0x8001, 0x8003, 0x8007, 0x8005, // +x |
309 |
|
|
0x8000, 0x8001, 0x8005, 0x8004, // -y |
310 |
|
|
0x8007, 0x8003, 0x8002, 0x8006, // +y |
311 |
|
|
0x8000, 0x8002, 0x8003, 0x8001, // -z |
312 |
|
|
0x8004, 0x8005, 0x8007, 0x8006, // +z |
313 |
|
|
0 |
314 |
|
|
}; |
315 |
|
|
|
316 |
|
|
glBegin (GL_QUADS); |
317 |
|
|
|
318 |
|
|
for (GLint *v = verts; *v; v++) |
319 |
|
|
{ |
320 |
|
|
GLint mask = *v; |
321 |
|
|
|
322 |
|
|
glVertex3i (mask & 1 ? b.x : a.x, |
323 |
|
|
mask & 2 ? b.y : a.y, |
324 |
|
|
mask & 4 ? b.z : a.z); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
glEnd (); |
328 |
|
|
} |
329 |
|
|
|
330 |
root |
1.25 |
int nesting; |
331 |
|
|
|
332 |
root |
1.24 |
void errchk (const char *name, const char *args, const char *file, int line) |
333 |
|
|
{ |
334 |
root |
1.25 |
static int inbegin; |
335 |
|
|
|
336 |
|
|
if (name[2] == 'B' && !strcmp (name, "glBegin")) |
337 |
|
|
inbegin = 1; |
338 |
|
|
else if (name[2] == 'E' && !strcmp (name, "glEnd")) |
339 |
|
|
inbegin = 0; |
340 |
|
|
|
341 |
|
|
if (inbegin) |
342 |
|
|
return; |
343 |
|
|
|
344 |
root |
1.24 |
GLenum gl_derror = glGetError (); |
345 |
|
|
if (gl_derror != GL_NO_ERROR) |
346 |
root |
1.25 |
fprintf (stderr, "%s:%d [GLERROR %d,%s] %s(%s)\n", |
347 |
|
|
file, line, gl_derror, gluErrorString (gl_derror), name, args); |
348 |
root |
1.24 |
} |
349 |
root |
1.21 |
|
350 |
|
|
} |
351 |
|
|
|
352 |
root |
1.10 |
//skedjuhlar main_scheduler; |
353 |
root |
1.17 |
|