ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/util.h
Revision: 1.41
Committed: Sun Oct 17 05:23:38 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.40: +29 -27 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.37 struct vec3
87     {
88 root 1.1 GLfloat x, y, z;
89     vec3 () { };
90 root 1.32 vec3 (GLfloat s) : x(s), y(s), z(s) { };
91 root 1.1 vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
92 root 1.32 vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { };
93 root 1.1
94     const vec3 operator -() const
95     { return vec3 (-x, -y, -z); }
96     };
97    
98     const vec3 normalize (const vec3 &v);
99     const vec3 cross (const vec3 &a, const vec3 &b);
100 root 1.6
101     inline const vec3 operator *(const vec3 &a, GLfloat s)
102     {
103     return vec3 (a.x * s, a.y * s, a.z * s);
104     }
105    
106 root 1.27 inline const vec3 operator +(const vec3 &a, const vec3 &b)
107     {
108     return vec3 (a.x + b.x, a.y + b.y, a.z + b.z);
109     }
110    
111     inline const vec3 operator -(const vec3 &a, const vec3 &b)
112     {
113     return vec3 (a.x - b.x, a.y - b.y, a.z - b.z);
114     }
115    
116 root 1.6 inline GLfloat dot (const vec3 &a, const vec3 &b)
117     {
118     return a.x * b.x + a.y * b.y + a.z * b.z;
119     }
120    
121 root 1.39 inline const GLfloat norm (const vec3 &v)
122 root 1.6 {
123     return sqrtf (dot (v, v));
124     }
125 root 1.1
126 root 1.10 typedef vec3 point;
127    
128     // a generic plane
129 root 1.37 struct plane
130     {
131 root 1.10 vec3 n;
132     GLfloat d;
133    
134     GLfloat distance (const point &p) const
135     {
136     return dot (n, p) + d;
137     }
138    
139     plane () { };
140     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
141     };
142    
143     void renormalize (sector &s, point &p);
144    
145 root 1.37 struct colour
146     {
147 root 1.40 GLubyte r, g, b, a;
148     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.)
149     : r(GLubyte (r * 255.F + .5F))
150     , g(GLubyte (g * 255.F + .5F))
151     , b(GLubyte (b * 255.F + .5F))
152     , a(GLubyte (a * 255.F + .5F))
153     {
154     }
155 root 1.10 };
156 root 1.5
157 root 1.40 struct tex2
158 root 1.37 {
159 root 1.1 GLfloat s, t;
160 root 1.40 tex2 () { };
161     tex2 (GLfloat s, GLfloat t) : s(s), t(t) { };
162 root 1.1 };
163    
164 root 1.37 struct box
165     {
166 root 1.27 point a, b;
167 root 1.8
168     box() { };
169 root 1.1
170     void reset ()
171     {
172 root 1.27 a = point ( FLT_MAX, FLT_MAX, FLT_MAX);
173     b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX);
174 root 1.1 }
175    
176     void add (const box &o);
177     void add (const point &p);
178     };
179 root 1.5
180 root 1.27 struct entity;
181     struct geometry;
182 root 1.20 struct view;
183 root 1.32 struct octant;
184 root 1.7
185 root 1.37 extern struct timer
186     {
187 root 1.7 static double now;
188     static double diff;
189    
190     static void frame ();
191     timer ();
192     } timer;
193 root 1.11
194 root 1.13 /*
195 root 1.11 #define MAX_EVENT_TYPES 10
196     enum event_type { TIMER_EV };
197 root 1.37 struct event
198     {
199 root 1.11 event_type type;
200     };
201    
202     typedef callback1<void, event&> event_cb;
203    
204 root 1.37 class skedjuhlar
205     {
206 root 1.11 public:
207     // only 10 types for now
208     private:
209     vector <list<event_cb> > event_lists;
210    
211     public:
212     skedjuhlar () {
213     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
214     }
215    
216     void register_event_cb (const event_type &t, const event_cb &e) {
217     event_lists[t].push_back (e);
218     };
219     void send_event (event &e) {
220     list<event_cb> &l = event_lists[e.type];
221     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
222     (*it)(e);
223     }
224     };
225     void check_events () {
226     while (!events.empty ()) {
227     event &e = events.pop_front ();
228     list<event_cb> &l = event_lists[e->name];
229     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
230     (*it)(e);
231     }
232     delete e; // ugly slow? hai hai..... 183G
233     }
234     }
235     };
236    
237     extern skedjuhlar main_scheduler;
238 root 1.13 */
239 root 1.22
240 root 1.37 namespace gl
241     {
242 root 1.41 #ifdef DEBUG
243     extern int nesting;
244     void errchk (const char *name, const char *args, const char *file, int line);
245     #endif
246    
247 root 1.40 struct vertex_v3f
248     {
249     point p; // vertex
250    
251     vertex_v3f () { };
252     vertex_v3f (point p) : p(p) { };
253    
254     GLenum gl_format () const { return GL_V3F; }
255     };
256    
257     struct vertex_t2f_n3f_v3f
258     {
259     tex2 t; // texture
260     vec3 n; // normal
261     point p; // vertex
262    
263     vertex_t2f_n3f_v3f () { };
264     vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { };
265    
266     GLenum gl_format () const { return GL_T2F_N3F_V3F; }
267     };
268 root 1.34
269 root 1.41 struct matrix
270     {
271     GLfloat data[4][4];
272    
273     const GLfloat operator ()(int i, int j) const { return data[j][i]; };
274     GLfloat &operator ()(int i, int j) { return data[j][i]; };
275    
276     operator GLfloat *() { return &data[0][0]; }
277    
278     void diagonal (GLfloat v);
279     void clear () { diagonal (0.F); };
280     void identity () { diagonal (1.F); };
281    
282     void print (); // ugly
283    
284     static const matrix translation (const vec3 &v);
285     static const matrix rotation (GLfloat degrees, const vec3 &axis);
286    
287     matrix () { };
288     matrix (GLfloat diag) { diagonal (diag); };
289     };
290    
291     const matrix operator *(const matrix &a, const matrix &b);
292     const vec3 operator *(const matrix &a, const vec3 &v);
293 root 1.40
294     struct vertex_buffer_object {
295     GLuint buffer;
296     GLenum format;
297    
298     void alloc ()
299     {
300     if (!buffer)
301     glGenBuffersARB (1, &buffer);
302     }
303    
304     void bind (GLenum target = GL_ARRAY_BUFFER_ARB)
305     {
306     glBindBufferARB (target, buffer);
307     glInterleavedArrays (format, 0, 0);
308     }
309    
310     void draw (GLenum mode, GLint first, GLsizei count)
311     {
312     bind ();
313     glDrawArrays (mode, first, count);
314     }
315    
316     template<class vertex>
317     void set (const vertex *v, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB)
318     {
319     alloc ();
320     format = v->gl_format ();
321     glBindBufferARB (target, buffer);
322     glBufferDataARB (target, count * sizeof (vertex), v, usage);
323     }
324    
325     template<class vertex>
326     void set (const vector<vertex> &v, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB)
327     {
328     set (&v[0], v.size (), usage, target);
329     }
330    
331     vertex_buffer_object ()
332     : buffer(0)
333     {
334     }
335    
336     ~vertex_buffer_object ()
337     {
338     glDeleteBuffersARB (1, &buffer);
339     }
340    
341     operator GLint ()
342     {
343     return buffer;
344     }
345     };
346    
347     const GLenum bbox_mode = GL_QUADS;
348     const GLsizei bbox_count = 6*4;
349     void gen_bbox (vertex_buffer_object &vb, const sector &a, const sector &b);
350 root 1.31 }
351    
352 root 1.40 GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord);
353 root 1.27
354 root 1.1 #endif
355