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