1 |
root |
1.1 |
#ifndef UTIL_H |
2 |
|
|
#define UTIL_H |
3 |
|
|
|
4 |
root |
1.7 |
#include <cmath> |
5 |
root |
1.27 |
#include <cfloat> |
6 |
root |
1.39 |
#include <cstdlib> |
7 |
root |
1.1 |
#include <vector> |
8 |
root |
1.22 |
#include <string> |
9 |
root |
1.1 |
|
10 |
root |
1.37 |
#include "opengl.h" |
11 |
root |
1.18 |
|
12 |
root |
1.37 |
#include <SDL/SDL_image.h> |
13 |
root |
1.30 |
|
14 |
root |
1.1 |
using namespace std; |
15 |
root |
1.18 |
|
16 |
root |
1.36 |
typedef long long soffs; // 32 bit |
17 |
|
|
typedef unsigned long long uoffs; |
18 |
root |
1.39 |
#define OFFS_BITS 63 |
19 |
root |
1.36 |
#define SOFFS_MIN (soffs)-(1LL << (OFFS_BITS - 2)) |
20 |
|
|
#define SOFFS_MAX (soffs)+(1LL << (OFFS_BITS - 2)) |
21 |
|
|
#define MAXEXTENT (1ULL << (OFFS_BITS - 1)) |
22 |
root |
1.1 |
|
23 |
root |
1.39 |
#define ABS(n) ((n) < 0 ? -(n) : (n)) |
24 |
|
|
|
25 |
root |
1.37 |
struct sector |
26 |
|
|
{ |
27 |
root |
1.32 |
soffs x, y, z; |
28 |
|
|
|
29 |
|
|
sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { }; |
30 |
|
|
sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { }; |
31 |
|
|
|
32 |
|
|
void offset (int subindex, uoffs extent) |
33 |
|
|
{ |
34 |
|
|
if (subindex & 1) x += extent; |
35 |
|
|
if (subindex & 2) y += extent; |
36 |
|
|
if (subindex & 4) z += extent; |
37 |
|
|
} |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
inline sector operator +(const sector &a, const sector &b) |
41 |
|
|
{ |
42 |
|
|
return sector (a.x + b.x, a.y + b.y, a.z + b.z); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
inline sector operator -(const sector &a, const sector &b) |
46 |
|
|
{ |
47 |
|
|
return sector (a.x - b.x, a.y - b.y, a.z - b.z); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
inline sector operator /(const sector &a, soffs b) |
51 |
|
|
{ |
52 |
|
|
return sector (a.x / b, a.y / b, a.z / b); |
53 |
|
|
} |
54 |
|
|
|
55 |
root |
1.40 |
inline sector operator >>(const sector &a, unsigned int b) |
56 |
|
|
{ |
57 |
|
|
return sector (a.x >> b, a.y >> b, a.z >> b); |
58 |
|
|
} |
59 |
|
|
|
60 |
root |
1.32 |
inline bool operator <=(const sector &a, const sector &b) |
61 |
|
|
{ |
62 |
|
|
return a.x <= b.x && a.y <= b.y && a.z <= b.z; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
inline soffs max (const sector &a) |
66 |
|
|
{ |
67 |
|
|
return max (a.x, max (a.y, a.z)); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
inline sector translate (const sector &p, const sector &src, const sector &dst) |
71 |
|
|
{ |
72 |
|
|
return p + (dst - src); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
inline sector abs (const sector &s) |
76 |
|
|
{ |
77 |
root |
1.39 |
return sector (ABS (s.x), ABS (s.y), ABS (s.z)); |
78 |
root |
1.32 |
} |
79 |
root |
1.1 |
|
80 |
root |
1.51 |
struct vec2 |
81 |
|
|
{ |
82 |
|
|
GLfloat x, y; |
83 |
|
|
vec2 () { }; |
84 |
|
|
vec2 (GLfloat s) : x(s), y(s) { }; |
85 |
|
|
vec2 (GLfloat x, GLfloat y) : x(x), y(y) { }; |
86 |
|
|
}; |
87 |
|
|
|
88 |
root |
1.37 |
struct vec3 |
89 |
|
|
{ |
90 |
root |
1.1 |
GLfloat x, y, z; |
91 |
|
|
vec3 () { }; |
92 |
root |
1.32 |
vec3 (GLfloat s) : x(s), y(s), z(s) { }; |
93 |
root |
1.1 |
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
94 |
root |
1.32 |
vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { }; |
95 |
root |
1.1 |
|
96 |
|
|
const vec3 operator -() const |
97 |
|
|
{ return vec3 (-x, -y, -z); } |
98 |
|
|
}; |
99 |
|
|
|
100 |
|
|
const vec3 normalize (const vec3 &v); |
101 |
|
|
const vec3 cross (const vec3 &a, const vec3 &b); |
102 |
root |
1.6 |
|
103 |
|
|
inline const vec3 operator *(const vec3 &a, GLfloat s) |
104 |
|
|
{ |
105 |
|
|
return vec3 (a.x * s, a.y * s, a.z * s); |
106 |
|
|
} |
107 |
|
|
|
108 |
root |
1.27 |
inline const vec3 operator +(const vec3 &a, const vec3 &b) |
109 |
|
|
{ |
110 |
|
|
return vec3 (a.x + b.x, a.y + b.y, a.z + b.z); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
inline const vec3 operator -(const vec3 &a, const vec3 &b) |
114 |
|
|
{ |
115 |
|
|
return vec3 (a.x - b.x, a.y - b.y, a.z - b.z); |
116 |
|
|
} |
117 |
|
|
|
118 |
root |
1.6 |
inline GLfloat dot (const vec3 &a, const vec3 &b) |
119 |
|
|
{ |
120 |
|
|
return a.x * b.x + a.y * b.y + a.z * b.z; |
121 |
|
|
} |
122 |
|
|
|
123 |
root |
1.45 |
// squared length |
124 |
|
|
inline const GLfloat length2 (const vec3 &v) |
125 |
root |
1.6 |
{ |
126 |
root |
1.45 |
return dot (v, v); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
inline const GLfloat length (const vec3 &v) |
130 |
|
|
{ |
131 |
|
|
return sqrtf (length2 (v)); |
132 |
root |
1.6 |
} |
133 |
root |
1.1 |
|
134 |
root |
1.10 |
typedef vec3 point; |
135 |
root |
1.51 |
|
136 |
|
|
struct vec4 |
137 |
|
|
{ |
138 |
|
|
GLfloat x, y, z, w; |
139 |
|
|
vec4 () { }; |
140 |
|
|
vec4 (GLfloat s) : x(s), y(s), z(s), w(s) { }; |
141 |
|
|
vec4 (GLfloat x, GLfloat y, GLfloat z, GLfloat w) : x(x), y(y), z(z), w(w) { }; |
142 |
|
|
}; |
143 |
root |
1.10 |
|
144 |
|
|
// a generic plane |
145 |
root |
1.37 |
struct plane |
146 |
|
|
{ |
147 |
root |
1.10 |
vec3 n; |
148 |
|
|
GLfloat d; |
149 |
|
|
|
150 |
|
|
plane () { }; |
151 |
|
|
plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); |
152 |
|
|
}; |
153 |
root |
1.45 |
|
154 |
|
|
inline GLfloat distance (const plane &a, const point &p) |
155 |
|
|
{ |
156 |
|
|
return dot (a.n, p) + a.d; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
struct sphere { |
160 |
|
|
point p; |
161 |
|
|
GLfloat r; |
162 |
|
|
|
163 |
|
|
sphere () { }; |
164 |
|
|
sphere (const point &p, GLfloat r) : p(p), r(r) { }; |
165 |
|
|
}; |
166 |
|
|
|
167 |
|
|
inline bool overlap (const sphere &a, const sphere &b) |
168 |
|
|
{ |
169 |
|
|
GLfloat d = a.r + b.r; |
170 |
|
|
|
171 |
|
|
return length2 (a.p - b.p) <= d * d; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
struct cone { |
175 |
|
|
point p; |
176 |
|
|
vec3 a; // axis |
177 |
root |
1.47 |
GLfloat f, fs1, fc2; // angle |
178 |
root |
1.45 |
|
179 |
|
|
cone () { }; |
180 |
|
|
cone (const point &p, const vec3 &a, GLfloat f) |
181 |
|
|
: p(p), a(a), f(f) |
182 |
|
|
{ |
183 |
root |
1.46 |
fs1 = 1.F / sinf (f); |
184 |
root |
1.47 |
fc2 = cosf (f); fc2 *= fc2; |
185 |
root |
1.45 |
}; |
186 |
|
|
}; |
187 |
|
|
|
188 |
|
|
inline bool overlap (const cone &c, const sphere &s) |
189 |
|
|
{ |
190 |
root |
1.46 |
vec3 d = s.p - c.p + c.a * (s.r * c.fs1); |
191 |
root |
1.47 |
GLfloat dd = dot (c.a, d); |
192 |
|
|
return dd > 0.F && dd * dd >= length2 (d) * c.fc2; |
193 |
root |
1.45 |
} |
194 |
root |
1.10 |
|
195 |
|
|
void renormalize (sector &s, point &p); |
196 |
|
|
|
197 |
root |
1.37 |
struct colour |
198 |
|
|
{ |
199 |
root |
1.40 |
GLubyte r, g, b, a; |
200 |
|
|
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) |
201 |
|
|
: r(GLubyte (r * 255.F + .5F)) |
202 |
|
|
, g(GLubyte (g * 255.F + .5F)) |
203 |
|
|
, b(GLubyte (b * 255.F + .5F)) |
204 |
|
|
, a(GLubyte (a * 255.F + .5F)) |
205 |
|
|
{ |
206 |
|
|
} |
207 |
root |
1.10 |
}; |
208 |
root |
1.5 |
|
209 |
root |
1.40 |
struct tex2 |
210 |
root |
1.37 |
{ |
211 |
root |
1.1 |
GLfloat s, t; |
212 |
root |
1.40 |
tex2 () { }; |
213 |
|
|
tex2 (GLfloat s, GLfloat t) : s(s), t(t) { }; |
214 |
root |
1.1 |
}; |
215 |
|
|
|
216 |
root |
1.52 |
template<typename unused> class gltype_traits { }; |
217 |
|
|
|
218 |
|
|
template<> struct gltype_traits<GLfloat > { static const GLenum gltype () { return GL_FLOAT; } }; |
219 |
|
|
template<> struct gltype_traits<GLdouble> { static const GLenum gltype () { return GL_DOUBLE; } }; |
220 |
|
|
template<> struct gltype_traits<GLint > { static const GLenum gltype () { return GL_INT; } }; |
221 |
|
|
template<> struct gltype_traits<GLuint > { static const GLenum gltype () { return GL_UNSIGNED_INT; } }; |
222 |
|
|
template<> struct gltype_traits<GLshort > { static const GLenum gltype () { return GL_SHORT; } }; |
223 |
|
|
template<> struct gltype_traits<GLushort> { static const GLenum gltype () { return GL_UNSIGNED_SHORT; } }; |
224 |
|
|
template<> struct gltype_traits<GLbyte > { static const GLenum gltype () { return GL_BYTE; } }; |
225 |
|
|
template<> struct gltype_traits<GLubyte > { static const GLenum gltype () { return GL_UNSIGNED_BYTE; } }; |
226 |
|
|
|
227 |
|
|
template<typename vectype> class vector_traits { }; |
228 |
|
|
|
229 |
|
|
template<> |
230 |
|
|
struct vector_traits<vec2> |
231 |
|
|
{ |
232 |
|
|
typedef GLfloat basetype; |
233 |
|
|
static const int dimension () { return 2; } |
234 |
|
|
}; |
235 |
|
|
|
236 |
|
|
template<> |
237 |
|
|
struct vector_traits<vec3> |
238 |
|
|
{ |
239 |
|
|
typedef GLfloat basetype; |
240 |
|
|
static const int dimension () { return 3; } |
241 |
|
|
}; |
242 |
|
|
|
243 |
|
|
template<> |
244 |
|
|
struct vector_traits<vec4> |
245 |
|
|
{ |
246 |
|
|
typedef GLfloat basetype; |
247 |
|
|
static const int dimension () { return 4; } |
248 |
|
|
}; |
249 |
|
|
|
250 |
|
|
template<> |
251 |
|
|
struct vector_traits<tex2> |
252 |
|
|
{ |
253 |
|
|
typedef GLfloat basetype; |
254 |
|
|
static const int dimension () { return 2; } |
255 |
|
|
}; |
256 |
|
|
|
257 |
|
|
template<> |
258 |
|
|
struct vector_traits<colour> |
259 |
|
|
{ |
260 |
|
|
typedef GLubyte basetype; |
261 |
|
|
static const int dimension () { return 4; } |
262 |
|
|
}; |
263 |
|
|
|
264 |
root |
1.37 |
struct box |
265 |
|
|
{ |
266 |
root |
1.27 |
point a, b; |
267 |
root |
1.8 |
|
268 |
|
|
box() { }; |
269 |
root |
1.1 |
|
270 |
|
|
void reset () |
271 |
|
|
{ |
272 |
root |
1.27 |
a = point ( FLT_MAX, FLT_MAX, FLT_MAX); |
273 |
|
|
b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX); |
274 |
root |
1.1 |
} |
275 |
|
|
|
276 |
|
|
void add (const box &o); |
277 |
|
|
void add (const point &p); |
278 |
|
|
}; |
279 |
root |
1.5 |
|
280 |
root |
1.27 |
struct entity; |
281 |
|
|
struct geometry; |
282 |
root |
1.20 |
struct view; |
283 |
root |
1.32 |
struct octant; |
284 |
root |
1.7 |
|
285 |
root |
1.37 |
extern struct timer |
286 |
|
|
{ |
287 |
root |
1.7 |
static double now; |
288 |
|
|
static double diff; |
289 |
root |
1.44 |
static double fps; |
290 |
root |
1.7 |
|
291 |
|
|
static void frame (); |
292 |
|
|
timer (); |
293 |
|
|
} timer; |
294 |
root |
1.11 |
|
295 |
root |
1.13 |
/* |
296 |
root |
1.11 |
#define MAX_EVENT_TYPES 10 |
297 |
|
|
enum event_type { TIMER_EV }; |
298 |
root |
1.37 |
struct event |
299 |
|
|
{ |
300 |
root |
1.11 |
event_type type; |
301 |
|
|
}; |
302 |
|
|
|
303 |
|
|
typedef callback1<void, event&> event_cb; |
304 |
|
|
|
305 |
root |
1.37 |
class skedjuhlar |
306 |
|
|
{ |
307 |
root |
1.11 |
public: |
308 |
|
|
// only 10 types for now |
309 |
|
|
private: |
310 |
|
|
vector <list<event_cb> > event_lists; |
311 |
|
|
|
312 |
|
|
public: |
313 |
|
|
skedjuhlar () { |
314 |
|
|
event_lists.resize (MAX_EVENT_TYPES, list<event_cb>()); |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
void register_event_cb (const event_type &t, const event_cb &e) { |
318 |
|
|
event_lists[t].push_back (e); |
319 |
|
|
}; |
320 |
|
|
void send_event (event &e) { |
321 |
|
|
list<event_cb> &l = event_lists[e.type]; |
322 |
|
|
for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) { |
323 |
|
|
(*it)(e); |
324 |
|
|
} |
325 |
|
|
}; |
326 |
|
|
void check_events () { |
327 |
|
|
while (!events.empty ()) { |
328 |
|
|
event &e = events.pop_front (); |
329 |
|
|
list<event_cb> &l = event_lists[e->name]; |
330 |
|
|
for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) { |
331 |
|
|
(*it)(e); |
332 |
|
|
} |
333 |
|
|
delete e; // ugly slow? hai hai..... 183G |
334 |
|
|
} |
335 |
|
|
} |
336 |
|
|
}; |
337 |
|
|
|
338 |
|
|
extern skedjuhlar main_scheduler; |
339 |
root |
1.13 |
*/ |
340 |
root |
1.22 |
|
341 |
root |
1.37 |
namespace gl |
342 |
|
|
{ |
343 |
root |
1.41 |
#ifdef DEBUG |
344 |
|
|
void errchk (const char *name, const char *args, const char *file, int line); |
345 |
|
|
#endif |
346 |
|
|
|
347 |
root |
1.40 |
struct vertex_v3f |
348 |
|
|
{ |
349 |
|
|
point p; // vertex |
350 |
|
|
|
351 |
|
|
vertex_v3f () { }; |
352 |
|
|
vertex_v3f (point p) : p(p) { }; |
353 |
|
|
|
354 |
|
|
GLenum gl_format () const { return GL_V3F; } |
355 |
|
|
}; |
356 |
|
|
|
357 |
|
|
struct vertex_t2f_n3f_v3f |
358 |
|
|
{ |
359 |
|
|
tex2 t; // texture |
360 |
|
|
vec3 n; // normal |
361 |
|
|
point p; // vertex |
362 |
|
|
|
363 |
root |
1.48 |
vertex_t2f_n3f_v3f () { } |
364 |
|
|
vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { } |
365 |
root |
1.40 |
|
366 |
|
|
GLenum gl_format () const { return GL_T2F_N3F_V3F; } |
367 |
|
|
}; |
368 |
root |
1.34 |
|
369 |
root |
1.49 |
GLsizei format_stride (GLenum format); |
370 |
|
|
GLsizei format_offset_p (GLenum format); |
371 |
|
|
GLsizei format_offset_n (GLenum format); |
372 |
|
|
GLsizei format_offset_t (GLenum format); |
373 |
|
|
|
374 |
root |
1.48 |
struct index_ushort |
375 |
|
|
{ |
376 |
|
|
GLushort i; |
377 |
|
|
|
378 |
|
|
index_ushort () { } |
379 |
|
|
index_ushort (GLushort i) : i(i) { } |
380 |
|
|
|
381 |
|
|
GLenum gl_format () const { return GL_UNSIGNED_SHORT; } |
382 |
|
|
}; |
383 |
|
|
|
384 |
root |
1.41 |
struct matrix |
385 |
|
|
{ |
386 |
|
|
GLfloat data[4][4]; |
387 |
|
|
|
388 |
|
|
const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
389 |
|
|
GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
390 |
|
|
|
391 |
|
|
operator GLfloat *() { return &data[0][0]; } |
392 |
|
|
|
393 |
|
|
void diagonal (GLfloat v); |
394 |
|
|
void clear () { diagonal (0.F); }; |
395 |
|
|
void identity () { diagonal (1.F); }; |
396 |
|
|
|
397 |
|
|
void print (); // ugly |
398 |
|
|
|
399 |
|
|
static const matrix translation (const vec3 &v); |
400 |
|
|
static const matrix rotation (GLfloat degrees, const vec3 &axis); |
401 |
|
|
|
402 |
|
|
matrix () { }; |
403 |
|
|
matrix (GLfloat diag) { diagonal (diag); }; |
404 |
|
|
}; |
405 |
|
|
|
406 |
|
|
const matrix operator *(const matrix &a, const matrix &b); |
407 |
|
|
const vec3 operator *(const matrix &a, const vec3 &v); |
408 |
root |
1.40 |
|
409 |
root |
1.48 |
template<GLenum target> |
410 |
|
|
struct vertex_buffer_object_base { |
411 |
root |
1.40 |
GLuint buffer; |
412 |
|
|
GLenum format; |
413 |
root |
1.52 |
GLsizei count; |
414 |
root |
1.40 |
|
415 |
root |
1.48 |
bool alloc () |
416 |
root |
1.40 |
{ |
417 |
root |
1.48 |
if (buffer) |
418 |
|
|
return false; |
419 |
|
|
|
420 |
|
|
glGenBuffersARB (1, &buffer); |
421 |
|
|
return true; |
422 |
root |
1.40 |
} |
423 |
|
|
|
424 |
root |
1.48 |
void bind () |
425 |
root |
1.40 |
{ |
426 |
|
|
glBindBufferARB (target, buffer); |
427 |
|
|
} |
428 |
|
|
|
429 |
root |
1.48 |
template<class data> |
430 |
|
|
void set (const data *d, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB) |
431 |
root |
1.40 |
{ |
432 |
|
|
alloc (); |
433 |
root |
1.48 |
format = d->gl_format (); |
434 |
root |
1.52 |
this->count = count; |
435 |
root |
1.40 |
glBindBufferARB (target, buffer); |
436 |
root |
1.48 |
glBufferDataARB (target, count * sizeof (data), d, usage); |
437 |
root |
1.40 |
} |
438 |
|
|
|
439 |
root |
1.48 |
template<class data> |
440 |
|
|
void set (const vector<data> &d, GLenum usage = GL_STATIC_DRAW_ARB) |
441 |
root |
1.40 |
{ |
442 |
root |
1.48 |
set (&d[0], d.size (), usage); |
443 |
root |
1.40 |
} |
444 |
|
|
|
445 |
root |
1.48 |
vertex_buffer_object_base () |
446 |
root |
1.40 |
: buffer(0) |
447 |
|
|
{ |
448 |
|
|
} |
449 |
|
|
|
450 |
root |
1.48 |
~vertex_buffer_object_base () |
451 |
root |
1.40 |
{ |
452 |
root |
1.48 |
if (buffer) |
453 |
|
|
glDeleteBuffersARB (1, &buffer); |
454 |
root |
1.40 |
} |
455 |
|
|
|
456 |
|
|
operator GLint () |
457 |
|
|
{ |
458 |
|
|
return buffer; |
459 |
|
|
} |
460 |
|
|
}; |
461 |
|
|
|
462 |
root |
1.48 |
struct vertex_buffer_object : vertex_buffer_object_base<GL_ARRAY_BUFFER_ARB> { |
463 |
|
|
void bind () |
464 |
|
|
{ |
465 |
|
|
vertex_buffer_object_base<GL_ARRAY_BUFFER_ARB>::bind (); |
466 |
|
|
glInterleavedArrays (format, 0, 0); |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
void draw (GLenum mode, GLint first, GLsizei count) |
470 |
|
|
{ |
471 |
|
|
bind (); |
472 |
|
|
glDrawArrays (mode, first, count); |
473 |
|
|
} |
474 |
|
|
}; |
475 |
|
|
|
476 |
|
|
struct index_buffer_object : vertex_buffer_object_base<GL_ELEMENT_ARRAY_BUFFER_ARB> { |
477 |
|
|
void draw (GLenum mode, GLint first, GLsizei count) |
478 |
|
|
{ |
479 |
|
|
bind (); |
480 |
|
|
glDrawElements (mode, count, format, (GLushort*)0 + first); // only SHORT supported :( // first //D//TODO |
481 |
|
|
} |
482 |
|
|
}; |
483 |
|
|
|
484 |
|
|
void draw_bbox (const sector &a, const sector &b); |
485 |
root |
1.49 |
|
486 |
root |
1.31 |
} |
487 |
|
|
|
488 |
root |
1.40 |
GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord); |
489 |
root |
1.27 |
|
490 |
root |
1.1 |
#endif |
491 |
|
|
|