ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.44
Committed: Sun Oct 17 14:03:52 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.43: +2 -3 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 root 1.44 static double fps;
190 root 1.7
191     static void frame ();
192     timer ();
193     } timer;
194 root 1.11
195 root 1.13 /*
196 root 1.11 #define MAX_EVENT_TYPES 10
197     enum event_type { TIMER_EV };
198 root 1.37 struct event
199     {
200 root 1.11 event_type type;
201     };
202    
203     typedef callback1<void, event&> event_cb;
204    
205 root 1.37 class skedjuhlar
206     {
207 root 1.11 public:
208     // only 10 types for now
209     private:
210     vector <list<event_cb> > event_lists;
211    
212     public:
213     skedjuhlar () {
214     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
215     }
216    
217     void register_event_cb (const event_type &t, const event_cb &e) {
218     event_lists[t].push_back (e);
219     };
220     void send_event (event &e) {
221     list<event_cb> &l = event_lists[e.type];
222     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
223     (*it)(e);
224     }
225     };
226     void check_events () {
227     while (!events.empty ()) {
228     event &e = events.pop_front ();
229     list<event_cb> &l = event_lists[e->name];
230     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
231     (*it)(e);
232     }
233     delete e; // ugly slow? hai hai..... 183G
234     }
235     }
236     };
237    
238     extern skedjuhlar main_scheduler;
239 root 1.13 */
240 root 1.22
241 root 1.37 namespace gl
242     {
243 root 1.41 #ifdef DEBUG
244     extern int nesting;
245     void errchk (const char *name, const char *args, const char *file, int line);
246     #endif
247    
248 root 1.40 struct vertex_v3f
249     {
250     point p; // vertex
251    
252     vertex_v3f () { };
253     vertex_v3f (point p) : p(p) { };
254    
255     GLenum gl_format () const { return GL_V3F; }
256     };
257    
258     struct vertex_t2f_n3f_v3f
259     {
260     tex2 t; // texture
261     vec3 n; // normal
262     point p; // vertex
263    
264     vertex_t2f_n3f_v3f () { };
265     vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { };
266    
267     GLenum gl_format () const { return GL_T2F_N3F_V3F; }
268     };
269 root 1.34
270 root 1.41 struct matrix
271     {
272     GLfloat data[4][4];
273    
274     const GLfloat operator ()(int i, int j) const { return data[j][i]; };
275     GLfloat &operator ()(int i, int j) { return data[j][i]; };
276    
277     operator GLfloat *() { return &data[0][0]; }
278    
279     void diagonal (GLfloat v);
280     void clear () { diagonal (0.F); };
281     void identity () { diagonal (1.F); };
282    
283     void print (); // ugly
284    
285     static const matrix translation (const vec3 &v);
286     static const matrix rotation (GLfloat degrees, const vec3 &axis);
287    
288     matrix () { };
289     matrix (GLfloat diag) { diagonal (diag); };
290     };
291    
292     const matrix operator *(const matrix &a, const matrix &b);
293     const vec3 operator *(const matrix &a, const vec3 &v);
294 root 1.40
295     struct vertex_buffer_object {
296     GLuint buffer;
297     GLenum format;
298    
299     void alloc ()
300     {
301     if (!buffer)
302     glGenBuffersARB (1, &buffer);
303     }
304    
305     void bind (GLenum target = GL_ARRAY_BUFFER_ARB)
306     {
307     glBindBufferARB (target, buffer);
308     glInterleavedArrays (format, 0, 0);
309     }
310    
311     void draw (GLenum mode, GLint first, GLsizei count)
312     {
313     bind ();
314     glDrawArrays (mode, first, count);
315     }
316    
317     template<class vertex>
318     void set (const vertex *v, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB)
319     {
320     alloc ();
321     format = v->gl_format ();
322     glBindBufferARB (target, buffer);
323     glBufferDataARB (target, count * sizeof (vertex), v, usage);
324     }
325    
326     template<class vertex>
327     void set (const vector<vertex> &v, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB)
328     {
329     set (&v[0], v.size (), usage, target);
330     }
331    
332     vertex_buffer_object ()
333     : buffer(0)
334     {
335     }
336    
337     ~vertex_buffer_object ()
338     {
339     glDeleteBuffersARB (1, &buffer);
340     }
341    
342     operator GLint ()
343     {
344     return buffer;
345     }
346     };
347    
348 root 1.44 void draw_bbox (vertex_buffer_object &vb, const sector &a, const sector &b);
349 root 1.31 }
350    
351 root 1.40 GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord);
352 root 1.27
353 root 1.1 #endif
354