ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.49
Committed: Thu Oct 21 15:46:38 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.48: +6 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #ifndef UTIL_H
2 #define UTIL_H
3
4 #include <cmath>
5 #include <cfloat>
6 #include <cstdlib>
7 #include <vector>
8 #include <string>
9
10 #include "opengl.h"
11
12 #include <SDL/SDL_image.h>
13
14 using namespace std;
15
16 extern CGcontext cgc;
17 extern CGprogram vsh;
18 extern CGprogram fsh;
19 extern CGparameter mv, mvp, lightpos;
20 extern CGprofile vsh_profile, fsh_profile;
21
22 typedef long long soffs; // 32 bit
23 typedef unsigned long long uoffs;
24 #define OFFS_BITS 63
25 #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
29 #define ABS(n) ((n) < 0 ? -(n) : (n))
30
31 struct sector
32 {
33 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 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 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 return sector (ABS (s.x), ABS (s.y), ABS (s.z));
84 }
85
86 struct vec3
87 {
88 GLfloat x, y, z;
89 vec3 () { };
90 vec3 (GLfloat s) : x(s), y(s), z(s) { };
91 vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
92 vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { };
93
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
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 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 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 // squared length
122 inline const GLfloat length2 (const vec3 &v)
123 {
124 return dot (v, v);
125 }
126
127 inline const GLfloat length (const vec3 &v)
128 {
129 return sqrtf (length2 (v));
130 }
131
132 typedef vec3 point;
133
134 // a generic plane
135 struct plane
136 {
137 vec3 n;
138 GLfloat d;
139
140 plane () { };
141 plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
142 };
143
144 inline GLfloat distance (const plane &a, const point &p)
145 {
146 return dot (a.n, p) + a.d;
147 }
148
149 struct sphere {
150 point p;
151 GLfloat r;
152
153 sphere () { };
154 sphere (const point &p, GLfloat r) : p(p), r(r) { };
155 };
156
157 inline bool overlap (const sphere &a, const sphere &b)
158 {
159 GLfloat d = a.r + b.r;
160
161 return length2 (a.p - b.p) <= d * d;
162 }
163
164 struct cone {
165 point p;
166 vec3 a; // axis
167 GLfloat f, fs1, fc2; // angle
168
169 cone () { };
170 cone (const point &p, const vec3 &a, GLfloat f)
171 : p(p), a(a), f(f)
172 {
173 fs1 = 1.F / sinf (f);
174 fc2 = cosf (f); fc2 *= fc2;
175 };
176 };
177
178 inline bool overlap (const cone &c, const sphere &s)
179 {
180 vec3 d = s.p - c.p + c.a * (s.r * c.fs1);
181 GLfloat dd = dot (c.a, d);
182 return dd > 0.F && dd * dd >= length2 (d) * c.fc2;
183 }
184
185 void renormalize (sector &s, point &p);
186
187 struct colour
188 {
189 GLubyte r, g, b, a;
190 colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.)
191 : r(GLubyte (r * 255.F + .5F))
192 , g(GLubyte (g * 255.F + .5F))
193 , b(GLubyte (b * 255.F + .5F))
194 , a(GLubyte (a * 255.F + .5F))
195 {
196 }
197 };
198
199 struct tex2
200 {
201 GLfloat s, t;
202 tex2 () { };
203 tex2 (GLfloat s, GLfloat t) : s(s), t(t) { };
204 };
205
206 struct box
207 {
208 point a, b;
209
210 box() { };
211
212 void reset ()
213 {
214 a = point ( FLT_MAX, FLT_MAX, FLT_MAX);
215 b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX);
216 }
217
218 void add (const box &o);
219 void add (const point &p);
220 };
221
222 struct entity;
223 struct geometry;
224 struct view;
225 struct octant;
226
227 extern struct timer
228 {
229 static double now;
230 static double diff;
231 static double fps;
232
233 static void frame ();
234 timer ();
235 } timer;
236
237 /*
238 #define MAX_EVENT_TYPES 10
239 enum event_type { TIMER_EV };
240 struct event
241 {
242 event_type type;
243 };
244
245 typedef callback1<void, event&> event_cb;
246
247 class skedjuhlar
248 {
249 public:
250 // only 10 types for now
251 private:
252 vector <list<event_cb> > event_lists;
253
254 public:
255 skedjuhlar () {
256 event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
257 }
258
259 void register_event_cb (const event_type &t, const event_cb &e) {
260 event_lists[t].push_back (e);
261 };
262 void send_event (event &e) {
263 list<event_cb> &l = event_lists[e.type];
264 for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
265 (*it)(e);
266 }
267 };
268 void check_events () {
269 while (!events.empty ()) {
270 event &e = events.pop_front ();
271 list<event_cb> &l = event_lists[e->name];
272 for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
273 (*it)(e);
274 }
275 delete e; // ugly slow? hai hai..... 183G
276 }
277 }
278 };
279
280 extern skedjuhlar main_scheduler;
281 */
282
283 namespace gl
284 {
285 #ifdef DEBUG
286 extern int nesting;
287 void errchk (const char *name, const char *args, const char *file, int line);
288 #endif
289
290 struct vertex_v3f
291 {
292 point p; // vertex
293
294 vertex_v3f () { };
295 vertex_v3f (point p) : p(p) { };
296
297 GLenum gl_format () const { return GL_V3F; }
298 };
299
300 struct vertex_t2f_n3f_v3f
301 {
302 tex2 t; // texture
303 vec3 n; // normal
304 point p; // vertex
305
306 vertex_t2f_n3f_v3f () { }
307 vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { }
308
309 GLenum gl_format () const { return GL_T2F_N3F_V3F; }
310 };
311
312 GLsizei format_stride (GLenum format);
313 GLsizei format_offset_p (GLenum format);
314 GLsizei format_offset_n (GLenum format);
315 GLsizei format_offset_t (GLenum format);
316
317 struct index_ushort
318 {
319 GLushort i;
320
321 index_ushort () { }
322 index_ushort (GLushort i) : i(i) { }
323
324 GLenum gl_format () const { return GL_UNSIGNED_SHORT; }
325 };
326
327 struct matrix
328 {
329 GLfloat data[4][4];
330
331 const GLfloat operator ()(int i, int j) const { return data[j][i]; };
332 GLfloat &operator ()(int i, int j) { return data[j][i]; };
333
334 operator GLfloat *() { return &data[0][0]; }
335
336 void diagonal (GLfloat v);
337 void clear () { diagonal (0.F); };
338 void identity () { diagonal (1.F); };
339
340 void print (); // ugly
341
342 static const matrix translation (const vec3 &v);
343 static const matrix rotation (GLfloat degrees, const vec3 &axis);
344
345 matrix () { };
346 matrix (GLfloat diag) { diagonal (diag); };
347 };
348
349 const matrix operator *(const matrix &a, const matrix &b);
350 const vec3 operator *(const matrix &a, const vec3 &v);
351
352 template<GLenum target>
353 struct vertex_buffer_object_base {
354 GLuint buffer;
355 GLenum format;
356
357 bool alloc ()
358 {
359 if (buffer)
360 return false;
361
362 glGenBuffersARB (1, &buffer);
363 return true;
364 }
365
366 void bind ()
367 {
368 glBindBufferARB (target, buffer);
369 }
370
371 template<class data>
372 void set (const data *d, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB)
373 {
374 alloc ();
375 format = d->gl_format ();
376 glBindBufferARB (target, buffer);
377 glBufferDataARB (target, count * sizeof (data), d, usage);
378 }
379
380 template<class data>
381 void set (const vector<data> &d, GLenum usage = GL_STATIC_DRAW_ARB)
382 {
383 set (&d[0], d.size (), usage);
384 }
385
386 vertex_buffer_object_base ()
387 : buffer(0)
388 {
389 }
390
391 ~vertex_buffer_object_base ()
392 {
393 if (buffer)
394 glDeleteBuffersARB (1, &buffer);
395 }
396
397 operator GLint ()
398 {
399 return buffer;
400 }
401 };
402
403 struct vertex_buffer_object : vertex_buffer_object_base<GL_ARRAY_BUFFER_ARB> {
404 void bind ()
405 {
406 vertex_buffer_object_base<GL_ARRAY_BUFFER_ARB>::bind ();
407 glInterleavedArrays (format, 0, 0);
408 }
409
410 void draw (GLenum mode, GLint first, GLsizei count)
411 {
412 bind ();
413 glDrawArrays (mode, first, count);
414 }
415 };
416
417 struct index_buffer_object : vertex_buffer_object_base<GL_ELEMENT_ARRAY_BUFFER_ARB> {
418 void draw (GLenum mode, GLint first, GLsizei count)
419 {
420 bind ();
421 glDrawElements (mode, count, format, (GLushort*)0 + first); // only SHORT supported :( // first //D//TODO
422 }
423 };
424
425 void draw_bbox (const sector &a, const sector &b);
426
427 }
428
429 GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord);
430
431 #endif
432