1 |
root |
1.1 |
#ifndef UTIL_H |
2 |
|
|
#define UTIL_H |
3 |
|
|
|
4 |
root |
1.7 |
#include <cmath> |
5 |
root |
1.1 |
#include <vector> |
6 |
|
|
|
7 |
|
|
using namespace std; |
8 |
|
|
|
9 |
root |
1.7 |
#include <GL/gl.h> |
10 |
|
|
|
11 |
root |
1.14 |
#include <sh/sh.hpp> |
12 |
|
|
|
13 |
|
|
using namespace SH; |
14 |
|
|
|
15 |
|
|
extern ShColor3f color; |
16 |
|
|
extern ShPoint3f lightPos; |
17 |
root |
1.15 |
extern ShMatrix4x4f mvp; |
18 |
root |
1.14 |
extern ShProgram vsh, fsh; |
19 |
|
|
|
20 |
root |
1.3 |
typedef int soffs; // 32 bit |
21 |
|
|
typedef unsigned int uoffs; |
22 |
root |
1.4 |
#define OFFS_BITS 31 |
23 |
root |
1.9 |
#define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 2)) |
24 |
|
|
#define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 2)) |
25 |
|
|
#define MAXEXTENT (1UL << (OFFS_BITS - 1)) |
26 |
root |
1.1 |
|
27 |
|
|
#define GLFLOAT_MAX 1e30 |
28 |
|
|
#define GLFLOAT_MIN -1e30 |
29 |
|
|
|
30 |
|
|
struct vec3 { |
31 |
|
|
GLfloat x, y, z; |
32 |
|
|
vec3 () { }; |
33 |
|
|
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
34 |
|
|
|
35 |
|
|
const vec3 operator -() const |
36 |
|
|
{ return vec3 (-x, -y, -z); } |
37 |
|
|
}; |
38 |
|
|
|
39 |
|
|
const vec3 normalize (const vec3 &v); |
40 |
|
|
const vec3 cross (const vec3 &a, const vec3 &b); |
41 |
root |
1.6 |
|
42 |
|
|
inline const vec3 operator *(const vec3 &a, GLfloat s) |
43 |
|
|
{ |
44 |
|
|
return vec3 (a.x * s, a.y * s, a.z * s); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
inline GLfloat dot (const vec3 &a, const vec3 &b) |
48 |
|
|
{ |
49 |
|
|
return a.x * b.x + a.y * b.y + a.z * b.z; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
inline const GLfloat abs (const vec3 &v) |
53 |
|
|
{ |
54 |
|
|
return sqrtf (dot (v, v)); |
55 |
|
|
} |
56 |
root |
1.1 |
|
57 |
root |
1.16 |
struct matrix { |
58 |
root |
1.5 |
GLfloat data[4][4]; |
59 |
|
|
|
60 |
root |
1.12 |
const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
61 |
|
|
GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
62 |
root |
1.5 |
|
63 |
|
|
void diagonal (GLfloat v); |
64 |
|
|
void clear () { diagonal (0.); }; |
65 |
|
|
void identity () { diagonal (1.); }; |
66 |
|
|
|
67 |
|
|
void print (); // ugly |
68 |
|
|
|
69 |
root |
1.16 |
static const matrix translation (const vec3 &v); |
70 |
|
|
static const matrix rotation (GLfloat degrees, const vec3 &axis); |
71 |
root |
1.5 |
|
72 |
root |
1.16 |
matrix () { }; |
73 |
|
|
matrix (GLfloat diag) { diagonal (diag); }; |
74 |
|
|
|
75 |
|
|
operator ShMatrix4x4f() const |
76 |
|
|
{ |
77 |
|
|
ShMatrix4x4f r; |
78 |
|
|
|
79 |
|
|
for (int i = 0; i < 4; i++) |
80 |
|
|
for (int j = 0; j < 4 ; j++) |
81 |
|
|
r[i][j] = data[j][i]; |
82 |
|
|
|
83 |
|
|
return r; |
84 |
|
|
} |
85 |
root |
1.5 |
}; |
86 |
|
|
|
87 |
root |
1.16 |
const matrix operator *(const matrix &a, const matrix &b); |
88 |
|
|
const vec3 operator *(const matrix &a, const vec3 &v); |
89 |
root |
1.10 |
|
90 |
|
|
typedef vec3 point; |
91 |
|
|
|
92 |
|
|
// a generic plane |
93 |
root |
1.12 |
struct plane { |
94 |
root |
1.10 |
vec3 n; |
95 |
|
|
GLfloat d; |
96 |
|
|
|
97 |
|
|
GLfloat distance (const point &p) const |
98 |
|
|
{ |
99 |
|
|
return dot (n, p) + d; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
plane () { }; |
103 |
|
|
plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); |
104 |
|
|
}; |
105 |
|
|
|
106 |
|
|
struct sector { |
107 |
|
|
soffs x, y, z; |
108 |
|
|
|
109 |
|
|
sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { }; |
110 |
|
|
|
111 |
|
|
void offset (int subindex, uoffs extent) |
112 |
|
|
{ |
113 |
|
|
if (subindex & 1) x += extent; |
114 |
|
|
if (subindex & 2) y += extent; |
115 |
|
|
if (subindex & 4) z += extent; |
116 |
|
|
} |
117 |
|
|
}; |
118 |
|
|
|
119 |
|
|
inline const sector translate (const sector &p, const sector &src, const sector &dst) |
120 |
|
|
{ |
121 |
|
|
sector r; |
122 |
|
|
|
123 |
|
|
r.x = p.x + (dst.x - src.x); |
124 |
|
|
r.y = p.y + (dst.y - src.y); |
125 |
|
|
r.z = p.z + (dst.z - src.z); |
126 |
|
|
|
127 |
|
|
return r; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
void renormalize (sector &s, point &p); |
131 |
|
|
|
132 |
|
|
struct colour { |
133 |
|
|
GLfloat r, g, b, a; |
134 |
|
|
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { }; |
135 |
|
|
}; |
136 |
root |
1.5 |
|
137 |
root |
1.1 |
struct texc { |
138 |
|
|
GLfloat s, t; |
139 |
|
|
texc () { }; |
140 |
|
|
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
141 |
|
|
}; |
142 |
|
|
|
143 |
|
|
struct box { |
144 |
root |
1.8 |
sector a, b; |
145 |
|
|
|
146 |
|
|
box() { }; |
147 |
root |
1.1 |
|
148 |
|
|
void reset () |
149 |
|
|
{ |
150 |
root |
1.9 |
a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX); |
151 |
|
|
b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN); |
152 |
root |
1.1 |
} |
153 |
|
|
|
154 |
|
|
void add (const box &o); |
155 |
root |
1.8 |
void add (const sector &p); |
156 |
root |
1.1 |
void add (const point &p); |
157 |
|
|
}; |
158 |
root |
1.5 |
|
159 |
|
|
inline const box translate (const box &b, const sector &src, const sector &dst) |
160 |
|
|
{ |
161 |
|
|
box r; |
162 |
|
|
|
163 |
|
|
r.a = translate (b.a, src, dst); |
164 |
|
|
r.b = translate (b.b, src, dst); |
165 |
|
|
|
166 |
|
|
return r; |
167 |
|
|
} |
168 |
root |
1.1 |
|
169 |
|
|
struct light { |
170 |
|
|
point p; |
171 |
|
|
colour c; |
172 |
|
|
GLfloat intensity; |
173 |
|
|
GLfloat radius; |
174 |
root |
1.2 |
}; |
175 |
|
|
|
176 |
|
|
struct material { |
177 |
|
|
colour diffuse, specular, emission; |
178 |
|
|
GLfloat shininess; |
179 |
root |
1.1 |
}; |
180 |
|
|
|
181 |
root |
1.4 |
struct entity_base; |
182 |
|
|
struct draw_context; |
183 |
root |
1.7 |
|
184 |
|
|
extern struct timer { |
185 |
|
|
static double now; |
186 |
|
|
static double diff; |
187 |
|
|
|
188 |
|
|
static void frame (); |
189 |
|
|
timer (); |
190 |
|
|
} timer; |
191 |
root |
1.11 |
|
192 |
root |
1.13 |
/* |
193 |
root |
1.11 |
#define MAX_EVENT_TYPES 10 |
194 |
|
|
enum event_type { TIMER_EV }; |
195 |
|
|
struct event { |
196 |
|
|
event_type type; |
197 |
|
|
}; |
198 |
|
|
|
199 |
|
|
typedef callback1<void, event&> event_cb; |
200 |
|
|
|
201 |
|
|
class skedjuhlar { |
202 |
|
|
|
203 |
|
|
public: |
204 |
|
|
// only 10 types for now |
205 |
|
|
private: |
206 |
|
|
vector <list<event_cb> > event_lists; |
207 |
|
|
|
208 |
|
|
public: |
209 |
|
|
skedjuhlar () { |
210 |
|
|
event_lists.resize (MAX_EVENT_TYPES, list<event_cb>()); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
void register_event_cb (const event_type &t, const event_cb &e) { |
214 |
|
|
event_lists[t].push_back (e); |
215 |
|
|
}; |
216 |
|
|
void send_event (event &e) { |
217 |
|
|
list<event_cb> &l = event_lists[e.type]; |
218 |
|
|
for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) { |
219 |
|
|
(*it)(e); |
220 |
|
|
} |
221 |
|
|
}; |
222 |
|
|
void check_events () { |
223 |
|
|
while (!events.empty ()) { |
224 |
|
|
event &e = events.pop_front (); |
225 |
|
|
list<event_cb> &l = event_lists[e->name]; |
226 |
|
|
for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) { |
227 |
|
|
(*it)(e); |
228 |
|
|
} |
229 |
|
|
delete e; // ugly slow? hai hai..... 183G |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
}; |
233 |
|
|
|
234 |
|
|
extern skedjuhlar main_scheduler; |
235 |
root |
1.13 |
*/ |
236 |
root |
1.1 |
|
237 |
|
|
#endif |
238 |
|
|
|