1 |
/* |
2 |
* math support |
3 |
* most of the more complicated code is taken from mesa. |
4 |
*/ |
5 |
|
6 |
#include <cstdio> // ugly |
7 |
#include <cstring> |
8 |
#include <cmath> |
9 |
|
10 |
#include <sys/time.h> |
11 |
#include <time.h> |
12 |
|
13 |
#include "opengl.h" |
14 |
|
15 |
#include "util.h" |
16 |
#include "entity.h" |
17 |
|
18 |
#define DEG2RAD (M_PI / 180.) |
19 |
|
20 |
void renormalize (sector &s, point &p) |
21 |
{ |
22 |
float i; |
23 |
|
24 |
p.x = modff (p.x, &i); s.x += (soffs)i; |
25 |
p.y = modff (p.y, &i); s.y += (soffs)i; |
26 |
p.z = modff (p.z, &i); s.z += (soffs)i; |
27 |
} |
28 |
|
29 |
///////////////////////////////////////////////////////////////////////////// |
30 |
|
31 |
const vec3 normalize (const vec3 &v) |
32 |
{ |
33 |
GLfloat s = norm (v); |
34 |
|
35 |
if (!s) |
36 |
return v; |
37 |
|
38 |
s = 1. / s; |
39 |
return vec3 (v.x * s, v.y * s, v.z * s); |
40 |
} |
41 |
|
42 |
const vec3 cross (const vec3 &a, const vec3 &b) |
43 |
{ |
44 |
return vec3 ( |
45 |
a.y * b.z - a.z * b.y, |
46 |
a.z * b.x - a.x * b.z, |
47 |
a.x * b.y - a.y * b.x |
48 |
); |
49 |
} |
50 |
|
51 |
///////////////////////////////////////////////////////////////////////////// |
52 |
|
53 |
plane::plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d) |
54 |
: n (vec3 (a,b,c)) |
55 |
{ |
56 |
GLfloat s = 1. / norm (n); |
57 |
|
58 |
n = n * s; |
59 |
this->d = d * s; |
60 |
} |
61 |
|
62 |
///////////////////////////////////////////////////////////////////////////// |
63 |
|
64 |
void box::add (const box &o) |
65 |
{ |
66 |
a.x = min (a.x, o.a.x); |
67 |
a.y = min (a.y, o.a.y); |
68 |
a.z = min (a.z, o.a.z); |
69 |
b.x = max (b.x, o.b.x); |
70 |
b.y = max (b.y, o.b.y); |
71 |
b.z = max (b.z, o.b.z); |
72 |
} |
73 |
|
74 |
void box::add (const point &p) |
75 |
{ |
76 |
a.x = min (a.x, p.x); |
77 |
a.y = min (a.y, p.y); |
78 |
a.z = min (a.z, p.z); |
79 |
b.x = max (b.x, p.x); |
80 |
b.y = max (b.y, p.y); |
81 |
b.z = max (b.z, p.z); |
82 |
} |
83 |
|
84 |
///////////////////////////////////////////////////////////////////////////// |
85 |
|
86 |
struct timer timer; |
87 |
static double base; |
88 |
double timer::now = 0.; |
89 |
double timer::diff; |
90 |
//double min_frame = 1. / 60.; |
91 |
double min_frame = 1. / 1000.; |
92 |
|
93 |
void timer::frame () |
94 |
{ |
95 |
struct timeval tv; |
96 |
double next; |
97 |
|
98 |
for (;;) |
99 |
{ |
100 |
gettimeofday (&tv, 0); |
101 |
|
102 |
next = tv.tv_sec - base + tv.tv_usec / 1.e6; |
103 |
diff = next - now; |
104 |
|
105 |
if (diff >= min_frame) |
106 |
break; |
107 |
|
108 |
SDL_Delay ((unsigned int)((min_frame - diff) * 1000.)); |
109 |
} |
110 |
|
111 |
now = next; |
112 |
} |
113 |
|
114 |
timer::timer () |
115 |
{ |
116 |
struct timeval tv; |
117 |
gettimeofday (&tv, 0); |
118 |
base = tv.tv_sec + tv.tv_usec / 1.e6; |
119 |
} |
120 |
|
121 |
void render_text (GLint x, GLint y, const char *str) |
122 |
{ |
123 |
glRasterPos2i (x, y); |
124 |
|
125 |
#if 0 |
126 |
while (!*str) |
127 |
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, *str++); |
128 |
#endif |
129 |
} |
130 |
|
131 |
namespace gl { |
132 |
|
133 |
void matrix::diagonal (GLfloat v) |
134 |
{ |
135 |
for (int i = 4; i--; ) |
136 |
for (int j = 4; j--; ) |
137 |
data[i][j] = i == j ? v : 0.; |
138 |
} |
139 |
|
140 |
const matrix operator *(const matrix &a, const matrix &b) |
141 |
{ |
142 |
matrix r; |
143 |
|
144 |
// taken from mesa |
145 |
for (int i = 0; i < 4; i++) |
146 |
{ |
147 |
const GLfloat ai0=a(i,0), ai1=a(i,1), ai2=a(i,2), ai3=a(i,3); |
148 |
|
149 |
r(i,0) = ai0 * b(0,0) + ai1 * b(1,0) + ai2 * b(2,0) + ai3 * b(3,0); |
150 |
r(i,1) = ai0 * b(0,1) + ai1 * b(1,1) + ai2 * b(2,1) + ai3 * b(3,1); |
151 |
r(i,2) = ai0 * b(0,2) + ai1 * b(1,2) + ai2 * b(2,2) + ai3 * b(3,2); |
152 |
r(i,3) = ai0 * b(0,3) + ai1 * b(1,3) + ai2 * b(2,3) + ai3 * b(3,3); |
153 |
} |
154 |
|
155 |
return r; |
156 |
} |
157 |
|
158 |
const matrix matrix::rotation (GLfloat angle, const vec3 &axis) |
159 |
{ |
160 |
GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c; |
161 |
|
162 |
s = (GLfloat) sinf (angle * DEG2RAD); |
163 |
c = (GLfloat) cosf (angle * DEG2RAD); |
164 |
|
165 |
const GLfloat mag = norm (axis); |
166 |
|
167 |
if (mag <= 1.0e-4) |
168 |
return matrix (1); |
169 |
|
170 |
matrix m; |
171 |
const vec3 n = axis * (1. / mag); |
172 |
|
173 |
xx = n.x * n.x; |
174 |
yy = n.y * n.y; |
175 |
zz = n.z * n.z; |
176 |
xy = n.x * n.y; |
177 |
yz = n.y * n.z; |
178 |
zx = n.z * n.x; |
179 |
xs = n.x * s; |
180 |
ys = n.y * s; |
181 |
zs = n.z * s; |
182 |
one_c = 1.0F - c; |
183 |
|
184 |
m(0,0) = (one_c * xx) + c; |
185 |
m(0,1) = (one_c * xy) - zs; |
186 |
m(0,2) = (one_c * zx) + ys; |
187 |
m(0,3) = 0; |
188 |
|
189 |
m(1,0) = (one_c * xy) + zs; |
190 |
m(1,1) = (one_c * yy) + c; |
191 |
m(1,2) = (one_c * yz) - xs; |
192 |
m(1,3) = 0; |
193 |
|
194 |
m(2,0) = (one_c * zx) - ys; |
195 |
m(2,1) = (one_c * yz) + xs; |
196 |
m(2,2) = (one_c * zz) + c; |
197 |
m(2,3) = 0; |
198 |
|
199 |
m(3,0) = 0; |
200 |
m(3,1) = 0; |
201 |
m(3,2) = 0; |
202 |
m(3,3) = 1; |
203 |
|
204 |
return m; |
205 |
} |
206 |
|
207 |
const vec3 operator *(const matrix &a, const vec3 &v) |
208 |
{ |
209 |
return vec3 ( |
210 |
a(0,0) * v.x + a(0,1) * v.y + a(0,2) * v.z + a(0,3), |
211 |
a(1,0) * v.x + a(1,1) * v.y + a(1,2) * v.z + a(1,3), |
212 |
a(2,0) * v.x + a(2,1) * v.y + a(2,2) * v.z + a(2,3) |
213 |
); |
214 |
} |
215 |
|
216 |
void matrix::print () |
217 |
{ |
218 |
printf ("\n"); |
219 |
printf ("[ %f, %f, %f, %f ]\n", data[0][0], data[1][0], data[2][0], data[3][0]); |
220 |
printf ("[ %f, %f, %f, %f ]\n", data[0][1], data[1][1], data[2][1], data[3][1]); |
221 |
printf ("[ %f, %f, %f, %f ]\n", data[0][2], data[1][2], data[2][2], data[3][2]); |
222 |
printf ("[ %f, %f, %f, %f ]\n", data[0][3], data[1][3], data[2][3], data[3][3]); |
223 |
} |
224 |
|
225 |
const matrix matrix::translation (const vec3 &v) |
226 |
{ |
227 |
matrix m(1); |
228 |
|
229 |
m(0,3) = v.x; |
230 |
m(1,3) = v.y; |
231 |
m(2,3) = v.z; |
232 |
|
233 |
return m; |
234 |
} |
235 |
|
236 |
///////////////////////////////////////////////////////////////////////////// |
237 |
|
238 |
void gen_bbox (vertex_buffer_object &vb, const sector &a, const sector &b) |
239 |
{ |
240 |
static GLint verts[] = { |
241 |
0x8000, 0x8004, 0x8006, 0x8002, // -x |
242 |
0x8001, 0x8003, 0x8007, 0x8005, // +x |
243 |
0x8000, 0x8001, 0x8005, 0x8004, // -y |
244 |
0x8007, 0x8003, 0x8002, 0x8006, // +y |
245 |
0x8000, 0x8002, 0x8003, 0x8001, // -z |
246 |
0x8004, 0x8005, 0x8007, 0x8006, // +z |
247 |
0 |
248 |
}; |
249 |
|
250 |
point pa(a), pb(b); |
251 |
|
252 |
vector<vertex_v3f> vd; |
253 |
|
254 |
for (GLint *v = verts; *v; v++) |
255 |
{ |
256 |
GLint mask = *v; |
257 |
|
258 |
vd.push_back (point ( |
259 |
mask & 1 ? pb.x : pa.x, |
260 |
mask & 2 ? pb.y : pa.y, |
261 |
mask & 4 ? pb.z : pa.z |
262 |
)); |
263 |
} |
264 |
|
265 |
vb.set (vd); |
266 |
} |
267 |
|
268 |
int nesting; |
269 |
|
270 |
void errchk (const char *name, const char *args, const char *file, int line) |
271 |
{ |
272 |
static int inbegin; |
273 |
|
274 |
if (name[2] == 'B' && !strcmp (name, "glBegin")) |
275 |
inbegin = 1; |
276 |
else if (name[2] == 'E' && !strcmp (name, "glEnd")) |
277 |
inbegin = 0; |
278 |
|
279 |
if (inbegin) |
280 |
return; |
281 |
|
282 |
GLenum gl_derror = glGetError (); |
283 |
if (gl_derror != GL_NO_ERROR) |
284 |
{ |
285 |
fprintf (stderr, "%s:%d [GLERROR %d,%s] %s(%s)\n", |
286 |
file, line, gl_derror, gluErrorString (gl_derror), name, args); |
287 |
abort (); |
288 |
} |
289 |
} |
290 |
|
291 |
} |
292 |
|
293 |
//skedjuhlar main_scheduler; |
294 |
|