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