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 (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.34: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #ifndef UTIL_H
2 #define UTIL_H
3
4 #include <SDL/SDL.h>
5 #include <SDL/SDL_image.h>
6 #include <cmath>
7 #include <cfloat>
8 #include <vector>
9 #include <string>
10
11 #include <Cg/cg.h>
12 #include <Cg/cgGL.h>
13
14 #include <GL/gl.h>
15 #include <GL/glext.h>
16 #include "gldebug.h"
17
18 using namespace std;
19
20 extern CGcontext cgc;
21 extern CGprogram vsh;
22 extern CGprogram fsh;
23 extern CGparameter mv, mvp, lightpos;
24 extern CGprofile vsh_profile, fsh_profile;
25
26 #include <GL/gl.h>
27
28 typedef int soffs; // 32 bit
29 typedef unsigned int uoffs;
30 #define OFFS_BITS 31
31 #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
35 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
84 struct vec3 {
85 GLfloat x, y, z;
86 vec3 () { };
87 vec3 (GLfloat s) : x(s), y(s), z(s) { };
88 vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
89 vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { };
90
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
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 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 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
123 struct matrix {
124 GLfloat data[4][4];
125
126 const GLfloat operator ()(int i, int j) const { return data[j][i]; };
127 GLfloat &operator ()(int i, int j) { return data[j][i]; };
128
129 operator GLfloat *() { return &data[0][0]; }
130
131 void diagonal (GLfloat v);
132 void clear () { diagonal (0.F); };
133 void identity () { diagonal (1.F); };
134
135 void print (); // ugly
136
137 static const matrix translation (const vec3 &v);
138 static const matrix rotation (GLfloat degrees, const vec3 &axis);
139
140 matrix () { };
141 matrix (GLfloat diag) { diagonal (diag); };
142 };
143
144 const matrix operator *(const matrix &a, const matrix &b);
145 const vec3 operator *(const matrix &a, const vec3 &v);
146
147 typedef vec3 point;
148
149 // a generic plane
150 struct plane {
151 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
170 struct texc {
171 GLfloat s, t;
172 texc () { };
173 texc (GLfloat s, GLfloat t) : s(s), t(t) { };
174 };
175
176 struct box {
177 point a, b;
178
179 box() { };
180
181 void reset ()
182 {
183 a = point ( FLT_MAX, FLT_MAX, FLT_MAX);
184 b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX);
185 }
186
187 void add (const box &o);
188 void add (const point &p);
189 };
190
191 struct light {
192 point p;
193 colour c;
194 GLfloat intensity;
195 GLfloat radius;
196 };
197
198 struct material {
199 colour diffuse, specular, emission;
200 GLfloat shininess;
201
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 };
210
211 struct entity;
212 struct geometry;
213 struct view;
214 struct octant;
215
216 extern struct timer {
217 static double now;
218 static double diff;
219
220 static void frame ();
221 timer ();
222 } timer;
223
224 /*
225 #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 */
268
269 namespace gl {
270 void draw_box (const view &ctx, const sector &a, const sector &b);
271
272 extern int nesting;
273 void errchk (const char *name, const char *args, const char *file, int line);
274 }
275
276 GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord);
277
278 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 TextureExecption (string s) : msg(s) { }
291 string msg;
292 };
293
294 struct Texture {
295 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
308 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 };
313
314 #endif
315