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