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