ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.26
Committed: Wed Oct 6 17:55:40 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.25: +8 -0 lines
Log Message:
i

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