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