1 |
root |
1.1 |
#ifndef UTIL_H |
2 |
|
|
#define UTIL_H |
3 |
|
|
|
4 |
root |
1.7 |
#include <cmath> |
5 |
root |
1.27 |
#include <cfloat> |
6 |
root |
1.1 |
#include <vector> |
7 |
root |
1.22 |
#include <string> |
8 |
root |
1.1 |
|
9 |
root |
1.37 |
#include "opengl.h" |
10 |
root |
1.18 |
|
11 |
root |
1.37 |
#include <SDL/SDL_image.h> |
12 |
root |
1.30 |
|
13 |
root |
1.1 |
using namespace std; |
14 |
root |
1.18 |
|
15 |
|
|
extern CGcontext cgc; |
16 |
|
|
extern CGprogram vsh; |
17 |
|
|
extern CGprogram fsh; |
18 |
root |
1.19 |
extern CGparameter mv, mvp, lightpos; |
19 |
root |
1.21 |
extern CGprofile vsh_profile, fsh_profile; |
20 |
root |
1.1 |
|
21 |
root |
1.36 |
typedef long long soffs; // 32 bit |
22 |
|
|
typedef unsigned long long uoffs; |
23 |
root |
1.4 |
#define OFFS_BITS 31 |
24 |
root |
1.36 |
#define SOFFS_MIN (soffs)-(1LL << (OFFS_BITS - 2)) |
25 |
|
|
#define SOFFS_MAX (soffs)+(1LL << (OFFS_BITS - 2)) |
26 |
|
|
#define MAXEXTENT (1ULL << (OFFS_BITS - 1)) |
27 |
root |
1.1 |
|
28 |
root |
1.37 |
struct sector |
29 |
|
|
{ |
30 |
root |
1.32 |
soffs x, y, z; |
31 |
|
|
|
32 |
|
|
sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { }; |
33 |
|
|
sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { }; |
34 |
|
|
|
35 |
|
|
void offset (int subindex, uoffs extent) |
36 |
|
|
{ |
37 |
|
|
if (subindex & 1) x += extent; |
38 |
|
|
if (subindex & 2) y += extent; |
39 |
|
|
if (subindex & 4) z += extent; |
40 |
|
|
} |
41 |
|
|
}; |
42 |
|
|
|
43 |
|
|
inline sector operator +(const sector &a, const sector &b) |
44 |
|
|
{ |
45 |
|
|
return sector (a.x + b.x, a.y + b.y, a.z + b.z); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
inline sector operator -(const sector &a, const sector &b) |
49 |
|
|
{ |
50 |
|
|
return sector (a.x - b.x, a.y - b.y, a.z - b.z); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
inline sector operator /(const sector &a, soffs b) |
54 |
|
|
{ |
55 |
|
|
return sector (a.x / b, a.y / b, a.z / b); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
inline bool operator <=(const sector &a, const sector &b) |
59 |
|
|
{ |
60 |
|
|
return a.x <= b.x && a.y <= b.y && a.z <= b.z; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
inline soffs max (const sector &a) |
64 |
|
|
{ |
65 |
|
|
return max (a.x, max (a.y, a.z)); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
inline sector translate (const sector &p, const sector &src, const sector &dst) |
69 |
|
|
{ |
70 |
|
|
return p + (dst - src); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
inline sector abs (const sector &s) |
74 |
|
|
{ |
75 |
|
|
return sector (abs (s.x), abs (s.y), abs (s.z)); |
76 |
|
|
} |
77 |
root |
1.1 |
|
78 |
root |
1.37 |
struct vec3 |
79 |
|
|
{ |
80 |
root |
1.1 |
GLfloat x, y, z; |
81 |
|
|
vec3 () { }; |
82 |
root |
1.32 |
vec3 (GLfloat s) : x(s), y(s), z(s) { }; |
83 |
root |
1.1 |
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
84 |
root |
1.32 |
vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { }; |
85 |
root |
1.1 |
|
86 |
|
|
const vec3 operator -() const |
87 |
|
|
{ return vec3 (-x, -y, -z); } |
88 |
|
|
}; |
89 |
|
|
|
90 |
|
|
const vec3 normalize (const vec3 &v); |
91 |
|
|
const vec3 cross (const vec3 &a, const vec3 &b); |
92 |
root |
1.6 |
|
93 |
|
|
inline const vec3 operator *(const vec3 &a, GLfloat s) |
94 |
|
|
{ |
95 |
|
|
return vec3 (a.x * s, a.y * s, a.z * s); |
96 |
|
|
} |
97 |
|
|
|
98 |
root |
1.27 |
inline const vec3 operator +(const vec3 &a, const vec3 &b) |
99 |
|
|
{ |
100 |
|
|
return vec3 (a.x + b.x, a.y + b.y, a.z + b.z); |
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 |
root |
1.6 |
inline GLfloat dot (const vec3 &a, const vec3 &b) |
109 |
|
|
{ |
110 |
|
|
return a.x * b.x + a.y * b.y + a.z * b.z; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
inline const GLfloat abs (const vec3 &v) |
114 |
|
|
{ |
115 |
|
|
return sqrtf (dot (v, v)); |
116 |
|
|
} |
117 |
root |
1.1 |
|
118 |
root |
1.37 |
struct matrix |
119 |
|
|
{ |
120 |
root |
1.5 |
GLfloat data[4][4]; |
121 |
|
|
|
122 |
root |
1.12 |
const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
123 |
|
|
GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
124 |
root |
1.5 |
|
125 |
root |
1.33 |
operator GLfloat *() { return &data[0][0]; } |
126 |
|
|
|
127 |
root |
1.5 |
void diagonal (GLfloat v); |
128 |
root |
1.32 |
void clear () { diagonal (0.F); }; |
129 |
|
|
void identity () { diagonal (1.F); }; |
130 |
root |
1.5 |
|
131 |
|
|
void print (); // ugly |
132 |
|
|
|
133 |
root |
1.16 |
static const matrix translation (const vec3 &v); |
134 |
|
|
static const matrix rotation (GLfloat degrees, const vec3 &axis); |
135 |
root |
1.5 |
|
136 |
root |
1.16 |
matrix () { }; |
137 |
|
|
matrix (GLfloat diag) { diagonal (diag); }; |
138 |
root |
1.5 |
}; |
139 |
|
|
|
140 |
root |
1.16 |
const matrix operator *(const matrix &a, const matrix &b); |
141 |
|
|
const vec3 operator *(const matrix &a, const vec3 &v); |
142 |
root |
1.10 |
|
143 |
|
|
typedef vec3 point; |
144 |
|
|
|
145 |
|
|
// a generic plane |
146 |
root |
1.37 |
struct plane |
147 |
|
|
{ |
148 |
root |
1.10 |
vec3 n; |
149 |
|
|
GLfloat d; |
150 |
|
|
|
151 |
|
|
GLfloat distance (const point &p) const |
152 |
|
|
{ |
153 |
|
|
return dot (n, p) + d; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
plane () { }; |
157 |
|
|
plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); |
158 |
|
|
}; |
159 |
|
|
|
160 |
|
|
void renormalize (sector &s, point &p); |
161 |
|
|
|
162 |
root |
1.37 |
struct colour |
163 |
|
|
{ |
164 |
root |
1.10 |
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 |
root |
1.5 |
|
168 |
root |
1.37 |
struct texc |
169 |
|
|
{ |
170 |
root |
1.1 |
GLfloat s, t; |
171 |
|
|
texc () { }; |
172 |
|
|
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
173 |
|
|
}; |
174 |
|
|
|
175 |
root |
1.37 |
struct box |
176 |
|
|
{ |
177 |
root |
1.27 |
point a, b; |
178 |
root |
1.8 |
|
179 |
|
|
box() { }; |
180 |
root |
1.1 |
|
181 |
|
|
void reset () |
182 |
|
|
{ |
183 |
root |
1.27 |
a = point ( FLT_MAX, FLT_MAX, FLT_MAX); |
184 |
|
|
b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX); |
185 |
root |
1.1 |
} |
186 |
|
|
|
187 |
|
|
void add (const box &o); |
188 |
|
|
void add (const point &p); |
189 |
|
|
}; |
190 |
root |
1.5 |
|
191 |
root |
1.27 |
struct entity; |
192 |
|
|
struct geometry; |
193 |
root |
1.20 |
struct view; |
194 |
root |
1.32 |
struct octant; |
195 |
root |
1.7 |
|
196 |
root |
1.37 |
extern struct timer |
197 |
|
|
{ |
198 |
root |
1.7 |
static double now; |
199 |
|
|
static double diff; |
200 |
|
|
|
201 |
|
|
static void frame (); |
202 |
|
|
timer (); |
203 |
|
|
} timer; |
204 |
root |
1.11 |
|
205 |
root |
1.13 |
/* |
206 |
root |
1.11 |
#define MAX_EVENT_TYPES 10 |
207 |
|
|
enum event_type { TIMER_EV }; |
208 |
root |
1.37 |
struct event |
209 |
|
|
{ |
210 |
root |
1.11 |
event_type type; |
211 |
|
|
}; |
212 |
|
|
|
213 |
|
|
typedef callback1<void, event&> event_cb; |
214 |
|
|
|
215 |
root |
1.37 |
class skedjuhlar |
216 |
|
|
{ |
217 |
root |
1.11 |
public: |
218 |
|
|
// only 10 types for now |
219 |
|
|
private: |
220 |
|
|
vector <list<event_cb> > event_lists; |
221 |
|
|
|
222 |
|
|
public: |
223 |
|
|
skedjuhlar () { |
224 |
|
|
event_lists.resize (MAX_EVENT_TYPES, list<event_cb>()); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
void register_event_cb (const event_type &t, const event_cb &e) { |
228 |
|
|
event_lists[t].push_back (e); |
229 |
|
|
}; |
230 |
|
|
void send_event (event &e) { |
231 |
|
|
list<event_cb> &l = event_lists[e.type]; |
232 |
|
|
for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) { |
233 |
|
|
(*it)(e); |
234 |
|
|
} |
235 |
|
|
}; |
236 |
|
|
void check_events () { |
237 |
|
|
while (!events.empty ()) { |
238 |
|
|
event &e = events.pop_front (); |
239 |
|
|
list<event_cb> &l = event_lists[e->name]; |
240 |
|
|
for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) { |
241 |
|
|
(*it)(e); |
242 |
|
|
} |
243 |
|
|
delete e; // ugly slow? hai hai..... 183G |
244 |
|
|
} |
245 |
|
|
} |
246 |
|
|
}; |
247 |
|
|
|
248 |
|
|
extern skedjuhlar main_scheduler; |
249 |
root |
1.13 |
*/ |
250 |
root |
1.22 |
|
251 |
root |
1.37 |
namespace gl |
252 |
|
|
{ |
253 |
root |
1.31 |
void draw_box (const view &ctx, const sector &a, const sector &b); |
254 |
root |
1.34 |
|
255 |
root |
1.35 |
extern int nesting; |
256 |
root |
1.34 |
void errchk (const char *name, const char *args, const char *file, int line); |
257 |
root |
1.31 |
} |
258 |
|
|
|
259 |
root |
1.22 |
GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord); |
260 |
root |
1.27 |
|
261 |
root |
1.1 |
#endif |
262 |
|
|
|