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