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