ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.54
Committed: Fri Oct 29 22:32:49 2004 UTC (21 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.53: +11 -10 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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 root 1.53 vec4 (const vec3 &v, GLfloat w = 1.F) : x(v.x), y(v.y), z(v.z), w(w) { };
143 root 1.51 };
144 root 1.10
145     // a generic plane
146 root 1.37 struct plane
147     {
148 root 1.10 vec3 n;
149     GLfloat d;
150    
151     plane () { };
152     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
153     };
154 root 1.45
155     inline GLfloat distance (const plane &a, const point &p)
156     {
157     return dot (a.n, p) + a.d;
158     }
159    
160     struct sphere {
161     point p;
162     GLfloat r;
163    
164     sphere () { };
165     sphere (const point &p, GLfloat r) : p(p), r(r) { };
166     };
167    
168     inline bool overlap (const sphere &a, const sphere &b)
169     {
170     GLfloat d = a.r + b.r;
171    
172     return length2 (a.p - b.p) <= d * d;
173     }
174    
175     struct cone {
176     point p;
177     vec3 a; // axis
178 root 1.47 GLfloat f, fs1, fc2; // angle
179 root 1.45
180     cone () { };
181     cone (const point &p, const vec3 &a, GLfloat f)
182     : p(p), a(a), f(f)
183     {
184 root 1.46 fs1 = 1.F / sinf (f);
185 root 1.47 fc2 = cosf (f); fc2 *= fc2;
186 root 1.45 };
187     };
188    
189     inline bool overlap (const cone &c, const sphere &s)
190     {
191 root 1.46 vec3 d = s.p - c.p + c.a * (s.r * c.fs1);
192 root 1.47 GLfloat dd = dot (c.a, d);
193     return dd > 0.F && dd * dd >= length2 (d) * c.fc2;
194 root 1.45 }
195 root 1.10
196     void renormalize (sector &s, point &p);
197    
198 root 1.37 struct colour
199     {
200 root 1.40 GLubyte r, g, b, a;
201     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.)
202     : r(GLubyte (r * 255.F + .5F))
203     , g(GLubyte (g * 255.F + .5F))
204     , b(GLubyte (b * 255.F + .5F))
205     , a(GLubyte (a * 255.F + .5F))
206     {
207     }
208 root 1.10 };
209 root 1.5
210 root 1.40 struct tex2
211 root 1.37 {
212 root 1.1 GLfloat s, t;
213 root 1.40 tex2 () { };
214     tex2 (GLfloat s, GLfloat t) : s(s), t(t) { };
215 root 1.1 };
216    
217 root 1.52 template<typename unused> class gltype_traits { };
218    
219     template<> struct gltype_traits<GLfloat > { static const GLenum gltype () { return GL_FLOAT; } };
220     template<> struct gltype_traits<GLdouble> { static const GLenum gltype () { return GL_DOUBLE; } };
221     template<> struct gltype_traits<GLint > { static const GLenum gltype () { return GL_INT; } };
222     template<> struct gltype_traits<GLuint > { static const GLenum gltype () { return GL_UNSIGNED_INT; } };
223     template<> struct gltype_traits<GLshort > { static const GLenum gltype () { return GL_SHORT; } };
224     template<> struct gltype_traits<GLushort> { static const GLenum gltype () { return GL_UNSIGNED_SHORT; } };
225     template<> struct gltype_traits<GLbyte > { static const GLenum gltype () { return GL_BYTE; } };
226     template<> struct gltype_traits<GLubyte > { static const GLenum gltype () { return GL_UNSIGNED_BYTE; } };
227    
228     template<typename vectype> class vector_traits { };
229    
230     template<>
231     struct vector_traits<vec2>
232     {
233     typedef GLfloat basetype;
234     static const int dimension () { return 2; }
235     };
236    
237     template<>
238     struct vector_traits<vec3>
239     {
240     typedef GLfloat basetype;
241     static const int dimension () { return 3; }
242     };
243    
244     template<>
245     struct vector_traits<vec4>
246     {
247     typedef GLfloat basetype;
248     static const int dimension () { return 4; }
249     };
250    
251     template<>
252     struct vector_traits<tex2>
253     {
254     typedef GLfloat basetype;
255     static const int dimension () { return 2; }
256     };
257    
258     template<>
259     struct vector_traits<colour>
260     {
261     typedef GLubyte basetype;
262     static const int dimension () { return 4; }
263     };
264    
265 root 1.37 struct box
266     {
267 root 1.27 point a, b;
268 root 1.8
269     box() { };
270 root 1.1
271     void reset ()
272     {
273 root 1.27 a = point ( FLT_MAX, FLT_MAX, FLT_MAX);
274     b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX);
275 root 1.1 }
276    
277     void add (const box &o);
278     void add (const point &p);
279     };
280 root 1.5
281 root 1.27 struct entity;
282     struct geometry;
283 root 1.20 struct view;
284 root 1.32 struct octant;
285 root 1.7
286 root 1.37 extern struct timer
287     {
288 root 1.7 static double now;
289     static double diff;
290 root 1.44 static double fps;
291 root 1.7
292     static void frame ();
293     timer ();
294     } timer;
295 root 1.11
296 root 1.13 /*
297 root 1.11 #define MAX_EVENT_TYPES 10
298     enum event_type { TIMER_EV };
299 root 1.37 struct event
300     {
301 root 1.11 event_type type;
302     };
303    
304     typedef callback1<void, event&> event_cb;
305    
306 root 1.37 class skedjuhlar
307     {
308 root 1.11 public:
309     // only 10 types for now
310     private:
311     vector <list<event_cb> > event_lists;
312    
313     public:
314     skedjuhlar () {
315     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
316     }
317    
318     void register_event_cb (const event_type &t, const event_cb &e) {
319     event_lists[t].push_back (e);
320     };
321     void send_event (event &e) {
322     list<event_cb> &l = event_lists[e.type];
323     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
324     (*it)(e);
325     }
326     };
327     void check_events () {
328     while (!events.empty ()) {
329     event &e = events.pop_front ();
330     list<event_cb> &l = event_lists[e->name];
331     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
332     (*it)(e);
333     }
334     delete e; // ugly slow? hai hai..... 183G
335     }
336     }
337     };
338    
339     extern skedjuhlar main_scheduler;
340 root 1.13 */
341 root 1.22
342 root 1.37 namespace gl
343     {
344 root 1.41 #ifdef DEBUG
345     void errchk (const char *name, const char *args, const char *file, int line);
346     #endif
347    
348 root 1.40 struct vertex_v3f
349     {
350     point p; // vertex
351    
352     vertex_v3f () { };
353     vertex_v3f (point p) : p(p) { };
354    
355     GLenum gl_format () const { return GL_V3F; }
356     };
357    
358     struct vertex_t2f_n3f_v3f
359     {
360     tex2 t; // texture
361     vec3 n; // normal
362     point p; // vertex
363    
364 root 1.48 vertex_t2f_n3f_v3f () { }
365     vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { }
366 root 1.40
367     GLenum gl_format () const { return GL_T2F_N3F_V3F; }
368     };
369 root 1.34
370 root 1.49 GLsizei format_stride (GLenum format);
371     GLsizei format_offset_p (GLenum format);
372     GLsizei format_offset_n (GLenum format);
373     GLsizei format_offset_t (GLenum format);
374    
375 root 1.48 struct index_ushort
376     {
377     GLushort i;
378    
379     index_ushort () { }
380     index_ushort (GLushort i) : i(i) { }
381    
382     GLenum gl_format () const { return GL_UNSIGNED_SHORT; }
383     };
384    
385 root 1.41 struct matrix
386     {
387     GLfloat data[4][4];
388    
389     const GLfloat operator ()(int i, int j) const { return data[j][i]; };
390     GLfloat &operator ()(int i, int j) { return data[j][i]; };
391    
392     operator GLfloat *() { return &data[0][0]; }
393    
394     void diagonal (GLfloat v);
395     void clear () { diagonal (0.F); };
396     void identity () { diagonal (1.F); };
397    
398     void print (); // ugly
399    
400 root 1.54 static matrix translation (const vec3 &v);
401     static matrix rotation (GLfloat degrees, const vec3 &axis);
402     static matrix scaling (GLfloat sx, GLfloat sy, GLfloat sz, GLfloat w = 1.F);
403 root 1.41
404     matrix () { };
405     matrix (GLfloat diag) { diagonal (diag); };
406     };
407    
408 root 1.54 matrix operator *(const matrix &a, const matrix &b);
409     vec3 operator *(const matrix &a, const vec3 &v);
410 root 1.40
411 root 1.48 template<GLenum target>
412 root 1.54 struct vertex_buffer_object {
413 root 1.40 GLuint buffer;
414     GLenum format;
415 root 1.52 GLsizei count;
416 root 1.40
417 root 1.48 bool alloc ()
418 root 1.40 {
419 root 1.48 if (buffer)
420     return false;
421    
422     glGenBuffersARB (1, &buffer);
423     return true;
424 root 1.40 }
425    
426 root 1.48 void bind ()
427 root 1.40 {
428     glBindBufferARB (target, buffer);
429     }
430    
431 root 1.48 template<class data>
432     void set (const data *d, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB)
433 root 1.40 {
434     alloc ();
435 root 1.48 format = d->gl_format ();
436 root 1.52 this->count = count;
437 root 1.40 glBindBufferARB (target, buffer);
438 root 1.48 glBufferDataARB (target, count * sizeof (data), d, usage);
439 root 1.40 }
440    
441 root 1.48 template<class data>
442     void set (const vector<data> &d, GLenum usage = GL_STATIC_DRAW_ARB)
443 root 1.40 {
444 root 1.48 set (&d[0], d.size (), usage);
445 root 1.40 }
446    
447 root 1.54 vertex_buffer_object ()
448 root 1.40 : buffer(0)
449     {
450     }
451    
452 root 1.54 ~vertex_buffer_object ()
453 root 1.40 {
454 root 1.48 if (buffer)
455     glDeleteBuffersARB (1, &buffer);
456 root 1.40 }
457    
458     operator GLint ()
459     {
460     return buffer;
461     }
462     };
463    
464 root 1.54 struct vertex_buffer : vertex_buffer_object<GL_ARRAY_BUFFER_ARB> {
465 root 1.48 void bind ()
466     {
467 root 1.54 vertex_buffer_object<GL_ARRAY_BUFFER_ARB>::bind ();
468 root 1.48 glInterleavedArrays (format, 0, 0);
469     }
470    
471     void draw (GLenum mode, GLint first, GLsizei count)
472     {
473     bind ();
474     glDrawArrays (mode, first, count);
475     }
476     };
477    
478 root 1.54 struct index_buffer : vertex_buffer_object<GL_ELEMENT_ARRAY_BUFFER_ARB> {
479 root 1.48 void draw (GLenum mode, GLint first, GLsizei count)
480     {
481     bind ();
482     glDrawElements (mode, count, format, (GLushort*)0 + first); // only SHORT supported :( // first //D//TODO
483     }
484     };
485    
486     void draw_bbox (const sector &a, const sector &b);
487 root 1.49
488 root 1.31 }
489    
490 root 1.40 GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord);
491 root 1.27
492 root 1.1 #endif
493