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