1 |
#ifndef UTIL_H |
2 |
#define UTIL_H |
3 |
|
4 |
#include <cmath> |
5 |
#include <cfloat> |
6 |
#include <cstdlib> |
7 |
#include <vector> |
8 |
#include <string> |
9 |
|
10 |
#include "opengl.h" |
11 |
|
12 |
#include <SDL/SDL_image.h> |
13 |
|
14 |
using namespace std; |
15 |
|
16 |
extern CGcontext cgc; |
17 |
extern CGprogram vsh; |
18 |
extern CGprogram fsh; |
19 |
extern CGparameter mv, mvp, lightpos; |
20 |
extern CGprofile vsh_profile, fsh_profile; |
21 |
|
22 |
typedef long long soffs; // 32 bit |
23 |
typedef unsigned long long uoffs; |
24 |
#define OFFS_BITS 63 |
25 |
#define SOFFS_MIN (soffs)-(1LL << (OFFS_BITS - 2)) |
26 |
#define SOFFS_MAX (soffs)+(1LL << (OFFS_BITS - 2)) |
27 |
#define MAXEXTENT (1ULL << (OFFS_BITS - 1)) |
28 |
|
29 |
#define ABS(n) ((n) < 0 ? -(n) : (n)) |
30 |
|
31 |
struct sector |
32 |
{ |
33 |
soffs x, y, z; |
34 |
|
35 |
sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { }; |
36 |
sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { }; |
37 |
|
38 |
void offset (int subindex, uoffs extent) |
39 |
{ |
40 |
if (subindex & 1) x += extent; |
41 |
if (subindex & 2) y += extent; |
42 |
if (subindex & 4) z += extent; |
43 |
} |
44 |
}; |
45 |
|
46 |
inline sector operator +(const sector &a, const sector &b) |
47 |
{ |
48 |
return sector (a.x + b.x, a.y + b.y, a.z + b.z); |
49 |
} |
50 |
|
51 |
inline sector operator -(const sector &a, const sector &b) |
52 |
{ |
53 |
return sector (a.x - b.x, a.y - b.y, a.z - b.z); |
54 |
} |
55 |
|
56 |
inline sector operator /(const sector &a, soffs b) |
57 |
{ |
58 |
return sector (a.x / b, a.y / b, a.z / b); |
59 |
} |
60 |
|
61 |
inline sector operator >>(const sector &a, unsigned int b) |
62 |
{ |
63 |
return sector (a.x >> b, a.y >> b, a.z >> b); |
64 |
} |
65 |
|
66 |
inline bool operator <=(const sector &a, const sector &b) |
67 |
{ |
68 |
return a.x <= b.x && a.y <= b.y && a.z <= b.z; |
69 |
} |
70 |
|
71 |
inline soffs max (const sector &a) |
72 |
{ |
73 |
return max (a.x, max (a.y, a.z)); |
74 |
} |
75 |
|
76 |
inline sector translate (const sector &p, const sector &src, const sector &dst) |
77 |
{ |
78 |
return p + (dst - src); |
79 |
} |
80 |
|
81 |
inline sector abs (const sector &s) |
82 |
{ |
83 |
return sector (ABS (s.x), ABS (s.y), ABS (s.z)); |
84 |
} |
85 |
|
86 |
struct vec3 |
87 |
{ |
88 |
GLfloat x, y, z; |
89 |
vec3 () { }; |
90 |
vec3 (GLfloat s) : x(s), y(s), z(s) { }; |
91 |
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
92 |
vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { }; |
93 |
|
94 |
const vec3 operator -() const |
95 |
{ return vec3 (-x, -y, -z); } |
96 |
}; |
97 |
|
98 |
const vec3 normalize (const vec3 &v); |
99 |
const vec3 cross (const vec3 &a, const vec3 &b); |
100 |
|
101 |
inline const vec3 operator *(const vec3 &a, GLfloat s) |
102 |
{ |
103 |
return vec3 (a.x * s, a.y * s, a.z * s); |
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 const vec3 operator -(const vec3 &a, const vec3 &b) |
112 |
{ |
113 |
return vec3 (a.x - b.x, a.y - b.y, a.z - b.z); |
114 |
} |
115 |
|
116 |
inline GLfloat dot (const vec3 &a, const vec3 &b) |
117 |
{ |
118 |
return a.x * b.x + a.y * b.y + a.z * b.z; |
119 |
} |
120 |
|
121 |
inline const GLfloat norm (const vec3 &v) |
122 |
{ |
123 |
return sqrtf (dot (v, v)); |
124 |
} |
125 |
|
126 |
struct matrix |
127 |
{ |
128 |
GLfloat data[4][4]; |
129 |
|
130 |
const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
131 |
GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
132 |
|
133 |
operator GLfloat *() { return &data[0][0]; } |
134 |
|
135 |
void diagonal (GLfloat v); |
136 |
void clear () { diagonal (0.F); }; |
137 |
void identity () { diagonal (1.F); }; |
138 |
|
139 |
void print (); // ugly |
140 |
|
141 |
static const matrix translation (const vec3 &v); |
142 |
static const matrix rotation (GLfloat degrees, const vec3 &axis); |
143 |
|
144 |
matrix () { }; |
145 |
matrix (GLfloat diag) { diagonal (diag); }; |
146 |
}; |
147 |
|
148 |
const matrix operator *(const matrix &a, const matrix &b); |
149 |
const vec3 operator *(const matrix &a, const vec3 &v); |
150 |
|
151 |
typedef vec3 point; |
152 |
|
153 |
// a generic plane |
154 |
struct plane |
155 |
{ |
156 |
vec3 n; |
157 |
GLfloat d; |
158 |
|
159 |
GLfloat distance (const point &p) const |
160 |
{ |
161 |
return dot (n, p) + d; |
162 |
} |
163 |
|
164 |
plane () { }; |
165 |
plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); |
166 |
}; |
167 |
|
168 |
void renormalize (sector &s, point &p); |
169 |
|
170 |
struct colour |
171 |
{ |
172 |
GLubyte r, g, b, a; |
173 |
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) |
174 |
: r(GLubyte (r * 255.F + .5F)) |
175 |
, g(GLubyte (g * 255.F + .5F)) |
176 |
, b(GLubyte (b * 255.F + .5F)) |
177 |
, a(GLubyte (a * 255.F + .5F)) |
178 |
{ |
179 |
} |
180 |
}; |
181 |
|
182 |
struct tex2 |
183 |
{ |
184 |
GLfloat s, t; |
185 |
tex2 () { }; |
186 |
tex2 (GLfloat s, GLfloat t) : s(s), t(t) { }; |
187 |
}; |
188 |
|
189 |
struct box |
190 |
{ |
191 |
point a, b; |
192 |
|
193 |
box() { }; |
194 |
|
195 |
void reset () |
196 |
{ |
197 |
a = point ( FLT_MAX, FLT_MAX, FLT_MAX); |
198 |
b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX); |
199 |
} |
200 |
|
201 |
void add (const box &o); |
202 |
void add (const point &p); |
203 |
}; |
204 |
|
205 |
struct entity; |
206 |
struct geometry; |
207 |
struct view; |
208 |
struct octant; |
209 |
|
210 |
extern struct timer |
211 |
{ |
212 |
static double now; |
213 |
static double diff; |
214 |
|
215 |
static void frame (); |
216 |
timer (); |
217 |
} timer; |
218 |
|
219 |
/* |
220 |
#define MAX_EVENT_TYPES 10 |
221 |
enum event_type { TIMER_EV }; |
222 |
struct event |
223 |
{ |
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 |
namespace gl |
266 |
{ |
267 |
struct vertex_v3f |
268 |
{ |
269 |
point p; // vertex |
270 |
|
271 |
vertex_v3f () { }; |
272 |
vertex_v3f (point p) : p(p) { }; |
273 |
|
274 |
GLenum gl_format () const { return GL_V3F; } |
275 |
}; |
276 |
|
277 |
struct vertex_t2f_n3f_v3f |
278 |
{ |
279 |
tex2 t; // texture |
280 |
vec3 n; // normal |
281 |
point p; // vertex |
282 |
|
283 |
vertex_t2f_n3f_v3f () { }; |
284 |
vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { }; |
285 |
|
286 |
GLenum gl_format () const { return GL_T2F_N3F_V3F; } |
287 |
}; |
288 |
|
289 |
extern int nesting; |
290 |
void errchk (const char *name, const char *args, const char *file, int line); |
291 |
|
292 |
struct vertex_buffer_object { |
293 |
GLuint buffer; |
294 |
GLenum format; |
295 |
|
296 |
void alloc () |
297 |
{ |
298 |
if (!buffer) |
299 |
glGenBuffersARB (1, &buffer); |
300 |
} |
301 |
|
302 |
void bind (GLenum target = GL_ARRAY_BUFFER_ARB) |
303 |
{ |
304 |
glBindBufferARB (target, buffer); |
305 |
glInterleavedArrays (format, 0, 0); |
306 |
} |
307 |
|
308 |
void draw (GLenum mode, GLint first, GLsizei count) |
309 |
{ |
310 |
bind (); |
311 |
glDrawArrays (mode, first, count); |
312 |
} |
313 |
|
314 |
template<class vertex> |
315 |
void set (const vertex *v, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB) |
316 |
{ |
317 |
alloc (); |
318 |
format = v->gl_format (); |
319 |
glBindBufferARB (target, buffer); |
320 |
glBufferDataARB (target, count * sizeof (vertex), v, usage); |
321 |
} |
322 |
|
323 |
template<class vertex> |
324 |
void set (const vector<vertex> &v, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB) |
325 |
{ |
326 |
set (&v[0], v.size (), usage, target); |
327 |
} |
328 |
|
329 |
vertex_buffer_object () |
330 |
: buffer(0) |
331 |
{ |
332 |
} |
333 |
|
334 |
~vertex_buffer_object () |
335 |
{ |
336 |
glDeleteBuffersARB (1, &buffer); |
337 |
} |
338 |
|
339 |
operator GLint () |
340 |
{ |
341 |
return buffer; |
342 |
} |
343 |
}; |
344 |
|
345 |
const GLenum bbox_mode = GL_QUADS; |
346 |
const GLsizei bbox_count = 6*4; |
347 |
void gen_bbox (vertex_buffer_object &vb, const sector &a, const sector &b); |
348 |
} |
349 |
|
350 |
GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord); |
351 |
|
352 |
#endif |
353 |
|