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