ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.35
Committed: Sat Oct 9 23:12:07 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.34: +1 -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.22 #include <SDL/SDL.h>
5     #include <SDL/SDL_image.h>
6 root 1.7 #include <cmath>
7 root 1.27 #include <cfloat>
8 root 1.1 #include <vector>
9 root 1.22 #include <string>
10 root 1.1
11 root 1.18 #include <Cg/cg.h>
12     #include <Cg/cgGL.h>
13    
14 root 1.34 #include <GL/gl.h>
15     #include <GL/glext.h>
16     #include "gldebug.h"
17 root 1.30
18 root 1.1 using namespace std;
19 root 1.18
20     extern CGcontext cgc;
21     extern CGprogram vsh;
22     extern CGprogram fsh;
23 root 1.19 extern CGparameter mv, mvp, lightpos;
24 root 1.21 extern CGprofile vsh_profile, fsh_profile;
25 root 1.1
26 root 1.7 #include <GL/gl.h>
27    
28 root 1.3 typedef int soffs; // 32 bit
29     typedef unsigned int uoffs;
30 root 1.4 #define OFFS_BITS 31
31 root 1.9 #define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 2))
32     #define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 2))
33     #define MAXEXTENT (1UL << (OFFS_BITS - 1))
34 root 1.1
35 root 1.32 struct sector {
36     soffs x, y, z;
37    
38     sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { };
39     sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { };
40    
41     void offset (int subindex, uoffs extent)
42     {
43     if (subindex & 1) x += extent;
44     if (subindex & 2) y += extent;
45     if (subindex & 4) z += extent;
46     }
47     };
48    
49     inline sector operator +(const sector &a, const sector &b)
50     {
51     return sector (a.x + b.x, a.y + b.y, a.z + b.z);
52     }
53    
54     inline sector operator -(const sector &a, const sector &b)
55     {
56     return sector (a.x - b.x, a.y - b.y, a.z - b.z);
57     }
58    
59     inline sector operator /(const sector &a, soffs b)
60     {
61     return sector (a.x / b, a.y / b, a.z / b);
62     }
63    
64     inline bool operator <=(const sector &a, const sector &b)
65     {
66     return a.x <= b.x && a.y <= b.y && a.z <= b.z;
67     }
68    
69     inline soffs max (const sector &a)
70     {
71     return max (a.x, max (a.y, a.z));
72     }
73    
74     inline sector translate (const sector &p, const sector &src, const sector &dst)
75     {
76     return p + (dst - src);
77     }
78    
79     inline sector abs (const sector &s)
80     {
81     return sector (abs (s.x), abs (s.y), abs (s.z));
82     }
83 root 1.1
84     struct vec3 {
85     GLfloat x, y, z;
86     vec3 () { };
87 root 1.32 vec3 (GLfloat s) : x(s), y(s), z(s) { };
88 root 1.1 vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
89 root 1.32 vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { };
90 root 1.1
91     const vec3 operator -() const
92     { return vec3 (-x, -y, -z); }
93     };
94    
95     const vec3 normalize (const vec3 &v);
96     const vec3 cross (const vec3 &a, const vec3 &b);
97 root 1.6
98     inline const vec3 operator *(const vec3 &a, GLfloat s)
99     {
100     return vec3 (a.x * s, a.y * s, a.z * s);
101     }
102    
103 root 1.27 inline const vec3 operator +(const vec3 &a, const vec3 &b)
104     {
105     return vec3 (a.x + b.x, a.y + b.y, a.z + b.z);
106     }
107    
108     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 root 1.6 inline GLfloat dot (const vec3 &a, const vec3 &b)
114     {
115     return a.x * b.x + a.y * b.y + a.z * b.z;
116     }
117    
118     inline const GLfloat abs (const vec3 &v)
119     {
120     return sqrtf (dot (v, v));
121     }
122 root 1.1
123 root 1.16 struct matrix {
124 root 1.5 GLfloat data[4][4];
125    
126 root 1.12 const GLfloat operator ()(int i, int j) const { return data[j][i]; };
127     GLfloat &operator ()(int i, int j) { return data[j][i]; };
128 root 1.5
129 root 1.33 operator GLfloat *() { return &data[0][0]; }
130    
131 root 1.5 void diagonal (GLfloat v);
132 root 1.32 void clear () { diagonal (0.F); };
133     void identity () { diagonal (1.F); };
134 root 1.5
135     void print (); // ugly
136    
137 root 1.16 static const matrix translation (const vec3 &v);
138     static const matrix rotation (GLfloat degrees, const vec3 &axis);
139 root 1.5
140 root 1.16 matrix () { };
141     matrix (GLfloat diag) { diagonal (diag); };
142 root 1.5 };
143    
144 root 1.16 const matrix operator *(const matrix &a, const matrix &b);
145     const vec3 operator *(const matrix &a, const vec3 &v);
146 root 1.10
147     typedef vec3 point;
148    
149     // a generic plane
150 root 1.12 struct plane {
151 root 1.10 vec3 n;
152     GLfloat d;
153    
154     GLfloat distance (const point &p) const
155     {
156     return dot (n, p) + d;
157     }
158    
159     plane () { };
160     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
161     };
162    
163     void renormalize (sector &s, point &p);
164    
165     struct colour {
166     GLfloat r, g, b, a;
167     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { };
168     };
169 root 1.5
170 root 1.1 struct texc {
171     GLfloat s, t;
172     texc () { };
173     texc (GLfloat s, GLfloat t) : s(s), t(t) { };
174     };
175    
176     struct box {
177 root 1.27 point a, b;
178 root 1.8
179     box() { };
180 root 1.1
181     void reset ()
182     {
183 root 1.27 a = point ( FLT_MAX, FLT_MAX, FLT_MAX);
184     b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX);
185 root 1.1 }
186    
187     void add (const box &o);
188     void add (const point &p);
189     };
190 root 1.5
191 root 1.1 struct light {
192     point p;
193     colour c;
194     GLfloat intensity;
195     GLfloat radius;
196 root 1.2 };
197    
198     struct material {
199     colour diffuse, specular, emission;
200     GLfloat shininess;
201 root 1.26
202     material ()
203     {
204     diffuse = colour (1, 0, 1, 1);
205     specular = colour (1, 0, 1, 1);
206     emission = colour (1, 0, 1, 1);
207     shininess = 1;
208     }
209 root 1.1 };
210    
211 root 1.27 struct entity;
212     struct geometry;
213 root 1.20 struct view;
214 root 1.32 struct octant;
215 root 1.7
216     extern struct timer {
217     static double now;
218     static double diff;
219    
220     static void frame ();
221     timer ();
222     } timer;
223 root 1.11
224 root 1.13 /*
225 root 1.11 #define MAX_EVENT_TYPES 10
226     enum event_type { TIMER_EV };
227     struct event {
228     event_type type;
229     };
230    
231     typedef callback1<void, event&> event_cb;
232    
233     class skedjuhlar {
234    
235     public:
236     // only 10 types for now
237     private:
238     vector <list<event_cb> > event_lists;
239    
240     public:
241     skedjuhlar () {
242     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
243     }
244    
245     void register_event_cb (const event_type &t, const event_cb &e) {
246     event_lists[t].push_back (e);
247     };
248     void send_event (event &e) {
249     list<event_cb> &l = event_lists[e.type];
250     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
251     (*it)(e);
252     }
253     };
254     void check_events () {
255     while (!events.empty ()) {
256     event &e = events.pop_front ();
257     list<event_cb> &l = event_lists[e->name];
258     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
259     (*it)(e);
260     }
261     delete e; // ugly slow? hai hai..... 183G
262     }
263     }
264     };
265    
266     extern skedjuhlar main_scheduler;
267 root 1.13 */
268 root 1.22
269 root 1.31 namespace gl {
270     void draw_box (const view &ctx, const sector &a, const sector &b);
271 root 1.34
272 root 1.35 extern int nesting;
273 root 1.34 void errchk (const char *name, const char *args, const char *file, int line);
274 root 1.31 }
275    
276 root 1.22 GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord);
277 root 1.27
278 root 1.22 inline int power_of_two (int input)
279     {
280     int value = 1;
281    
282     while (value < input)
283     {
284     value <<= 1;
285     }
286     return value;
287     }
288    
289     struct TextureExecption {
290 root 1.27 TextureExecption (string s) : msg(s) { }
291     string msg;
292 root 1.22 };
293 root 1.27
294 root 1.22 struct Texture {
295 root 1.27 SDL_Surface *image;
296     GLuint texture;
297     GLfloat texcoord[4];
298    
299     public:
300    
301     Texture(const char *f)
302     {
303     image = IMG_Load (f);
304    
305     if (!image)
306     throw (TextureExecption ("Couldn't load " + (string) f + ": " + (string) IMG_GetError ()));
307 root 1.22
308 root 1.27 texture = SDL_GL_LoadTexture (image, (GLfloat*)&texcoord);
309     if (texture == 0)
310     throw (TextureExecption ("Couldn't make GL Texture, failed to create new RGB SWSurface"));
311     }
312 root 1.22 };
313 root 1.1
314     #endif
315