ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/util.h
Revision: 1.56
Committed: Sat Oct 30 15:59:20 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.55: +51 -43 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 vectype> class vector_traits { };
218    
219     template<>
220     struct vector_traits<vec2>
221     {
222     typedef GLfloat basetype;
223     static const int dimension () { return 2; }
224     };
225    
226     template<>
227     struct vector_traits<vec3>
228     {
229     typedef GLfloat basetype;
230     static const int dimension () { return 3; }
231     };
232    
233     template<>
234     struct vector_traits<vec4>
235     {
236     typedef GLfloat basetype;
237     static const int dimension () { return 4; }
238     };
239    
240     template<>
241     struct vector_traits<tex2>
242     {
243     typedef GLfloat basetype;
244     static const int dimension () { return 2; }
245     };
246    
247     template<>
248     struct vector_traits<colour>
249     {
250     typedef GLubyte basetype;
251     static const int dimension () { return 4; }
252     };
253    
254 root 1.37 struct box
255     {
256 root 1.27 point a, b;
257 root 1.8
258     box() { };
259 root 1.1
260     void reset ()
261     {
262 root 1.27 a = point ( FLT_MAX, FLT_MAX, FLT_MAX);
263     b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX);
264 root 1.1 }
265    
266     void add (const box &o);
267     void add (const point &p);
268     };
269 root 1.5
270 root 1.27 struct entity;
271     struct geometry;
272 root 1.20 struct view;
273 root 1.32 struct octant;
274 root 1.7
275 root 1.37 extern struct timer
276     {
277 root 1.7 static double now;
278     static double diff;
279 root 1.44 static double fps;
280 root 1.7
281     static void frame ();
282     timer ();
283     } timer;
284 root 1.11
285 root 1.13 /*
286 root 1.11 #define MAX_EVENT_TYPES 10
287     enum event_type { TIMER_EV };
288 root 1.37 struct event
289     {
290 root 1.11 event_type type;
291     };
292    
293     typedef callback1<void, event&> event_cb;
294    
295 root 1.37 class skedjuhlar
296     {
297 root 1.11 public:
298     // only 10 types for now
299     private:
300     vector <list<event_cb> > event_lists;
301    
302     public:
303     skedjuhlar () {
304     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
305     }
306    
307     void register_event_cb (const event_type &t, const event_cb &e) {
308     event_lists[t].push_back (e);
309     };
310     void send_event (event &e) {
311     list<event_cb> &l = event_lists[e.type];
312     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
313     (*it)(e);
314     }
315     };
316     void check_events () {
317     while (!events.empty ()) {
318     event &e = events.pop_front ();
319     list<event_cb> &l = event_lists[e->name];
320     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
321     (*it)(e);
322     }
323     delete e; // ugly slow? hai hai..... 183G
324     }
325     }
326     };
327    
328     extern skedjuhlar main_scheduler;
329 root 1.13 */
330 root 1.22
331 root 1.37 namespace gl
332     {
333 root 1.41 #ifdef DEBUG
334     void errchk (const char *name, const char *args, const char *file, int line);
335     #endif
336    
337 root 1.40 struct vertex_v3f
338     {
339 root 1.56 point v; // vertex
340 root 1.40
341     vertex_v3f () { };
342 root 1.56 vertex_v3f (point v) : v(v) { };
343 root 1.40 };
344    
345     struct vertex_t2f_n3f_v3f
346     {
347     tex2 t; // texture
348     vec3 n; // normal
349 root 1.56 point v; // vertex
350 root 1.40
351 root 1.48 vertex_t2f_n3f_v3f () { }
352 root 1.56 vertex_t2f_n3f_v3f (point v, vec3 n, tex2 t = tex2()) : v(v), n(n), t(t) { }
353     };
354 root 1.40
355 root 1.56 template<typename unused> class type_traits { };
356    
357     template<> struct type_traits<GLfloat > { static const GLenum glenum () { return GL_FLOAT; } };
358     template<> struct type_traits<GLdouble> { static const GLenum glenum () { return GL_DOUBLE; } };
359     template<> struct type_traits<GLint > { static const GLenum glenum () { return GL_INT; } };
360     template<> struct type_traits<GLuint > { static const GLenum glenum () { return GL_UNSIGNED_INT; } };
361     template<> struct type_traits<GLshort > { static const GLenum glenum () { return GL_SHORT; } };
362     template<> struct type_traits<GLushort> { static const GLenum glenum () { return GL_UNSIGNED_SHORT; } };
363     template<> struct type_traits<GLbyte > { static const GLenum glenum () { return GL_BYTE; } };
364     template<> struct type_traits<GLubyte > { static const GLenum glenum () { return GL_UNSIGNED_BYTE; } };
365     template<> struct type_traits<vertex_v3f > { static const GLenum glenum () { return GL_V3F; } };
366     template<> struct type_traits<vertex_t2f_n3f_v3f> { static const GLenum glenum () { return GL_T2F_N3F_V3F; } };
367    
368     template<GLenum unused> class enum_traits { };
369    
370     template<> struct enum_traits<GL_FLOAT > { typedef GLfloat type; };
371     template<> struct enum_traits<GL_DOUBLE > { typedef GLdouble type; };
372     template<> struct enum_traits<GL_INT > { typedef GLint type; };
373     template<> struct enum_traits<GL_UNSIGNED_INT > { typedef GLuint type; };
374     template<> struct enum_traits<GL_SHORT > { typedef GLshort type; };
375     template<> struct enum_traits<GL_UNSIGNED_SHORT> { typedef GLushort type; };
376     template<> struct enum_traits<GL_BYTE > { typedef GLbyte type; };
377     template<> struct enum_traits<GL_UNSIGNED_BYTE > { typedef GLubyte type; };
378     template<> struct enum_traits<GL_V3F > { typedef vertex_v3f type; };
379     template<> struct enum_traits<GL_T2F_N3F_V3F > { typedef vertex_t2f_n3f_v3f type; };
380 root 1.34
381 root 1.56 // deprecated:
382 root 1.49 GLsizei format_stride (GLenum format);
383     GLsizei format_offset_p (GLenum format);
384     GLsizei format_offset_n (GLenum format);
385     GLsizei format_offset_t (GLenum format);
386    
387 root 1.41 struct matrix
388     {
389     GLfloat data[4][4];
390    
391     const GLfloat operator ()(int i, int j) const { return data[j][i]; };
392     GLfloat &operator ()(int i, int j) { return data[j][i]; };
393    
394     operator GLfloat *() { return &data[0][0]; }
395    
396     void diagonal (GLfloat v);
397     void clear () { diagonal (0.F); };
398     void identity () { diagonal (1.F); };
399    
400     void print (); // ugly
401    
402 root 1.54 static matrix translation (const vec3 &v);
403     static matrix rotation (GLfloat degrees, const vec3 &axis);
404     static matrix scaling (GLfloat sx, GLfloat sy, GLfloat sz, GLfloat w = 1.F);
405 root 1.41
406     matrix () { };
407     matrix (GLfloat diag) { diagonal (diag); };
408     };
409    
410 root 1.54 matrix operator *(const matrix &a, const matrix &b);
411     vec3 operator *(const matrix &a, const vec3 &v);
412 root 1.40
413 root 1.48 template<GLenum target>
414 root 1.56 struct vertex_buffer_object
415     {
416 root 1.40 GLuint buffer;
417     GLenum format;
418 root 1.52 GLsizei count;
419 root 1.56 GLsizei size;
420 root 1.40
421 root 1.48 bool alloc ()
422 root 1.40 {
423 root 1.48 if (buffer)
424     return false;
425    
426     glGenBuffersARB (1, &buffer);
427     return true;
428 root 1.40 }
429    
430 root 1.48 void bind ()
431 root 1.40 {
432     glBindBufferARB (target, buffer);
433     }
434    
435 root 1.56 template<class basetype>
436     void set (const basetype *d, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB)
437 root 1.40 {
438     alloc ();
439 root 1.56 format = type_traits<basetype>::glenum ();
440 root 1.52 this->count = count;
441 root 1.56 size = sizeof (basetype);
442 root 1.55 vertex_buffer_object::bind ();
443 root 1.56 glBufferDataARB (target, count * this->size, d, usage);
444 root 1.40 }
445    
446 root 1.48 template<class data>
447     void set (const vector<data> &d, GLenum usage = GL_STATIC_DRAW_ARB)
448 root 1.40 {
449 root 1.48 set (&d[0], d.size (), usage);
450 root 1.40 }
451    
452 root 1.56 template<typename basetype>
453     void set (GLsizei count, GLenum usage = GL_DYNAMIC_DRAW_ARB)
454 root 1.55 {
455     alloc ();
456 root 1.56 format = type_traits<basetype>::glenum ();
457     this->count = count;
458     size = sizeof (basetype);
459 root 1.55 vertex_buffer_object::bind ();
460 root 1.56 glBufferDataARB (target, this->count * size, 0, usage);
461 root 1.55 }
462    
463     void *map (GLenum access = GL_WRITE_ONLY)
464     {
465     vertex_buffer_object::bind ();
466     return glMapBufferARB (target, access);
467     }
468    
469     void unmap ()
470     {
471     vertex_buffer_object::bind ();
472     glUnmapBuffer (target);
473     }
474    
475 root 1.54 vertex_buffer_object ()
476 root 1.40 : buffer(0)
477     {
478     }
479    
480 root 1.54 ~vertex_buffer_object ()
481 root 1.40 {
482 root 1.48 if (buffer)
483     glDeleteBuffersARB (1, &buffer);
484 root 1.40 }
485    
486     operator GLint ()
487     {
488     return buffer;
489     }
490     };
491    
492 root 1.56 struct vertex_buffer : vertex_buffer_object<GL_ARRAY_BUFFER_ARB>
493     {
494 root 1.48 void bind ()
495     {
496 root 1.54 vertex_buffer_object<GL_ARRAY_BUFFER_ARB>::bind ();
497 root 1.56 glInterleavedArrays (format, 0, 0);
498 root 1.48 }
499    
500     void draw (GLenum mode, GLint first, GLsizei count)
501     {
502     bind ();
503     glDrawArrays (mode, first, count);
504     }
505     };
506    
507 root 1.56 struct index_buffer : vertex_buffer_object<GL_ELEMENT_ARRAY_BUFFER_ARB>
508     {
509 root 1.48 void draw (GLenum mode, GLint first, GLsizei count)
510     {
511     bind ();
512 root 1.56 glDrawElements (mode, count, format, (GLvoid *)((char *)0 + first * size)); // only SHORT supported :( // first //D//TODO
513 root 1.48 }
514     };
515    
516     void draw_bbox (const sector &a, const sector &b);
517 root 1.49
518 root 1.31 }
519    
520 root 1.40 GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord);
521 root 1.27
522 root 1.1 #endif
523