1 |
#ifndef UTIL_H |
2 |
#define UTIL_H |
3 |
|
4 |
#include <SDL/SDL.h> |
5 |
#include <SDL/SDL_image.h> |
6 |
#include <cmath> |
7 |
#include <vector> |
8 |
#include <string> |
9 |
|
10 |
#include <Cg/cg.h> |
11 |
#include <Cg/cgGL.h> |
12 |
|
13 |
using namespace std; |
14 |
|
15 |
extern CGcontext cgc; |
16 |
extern CGprogram vsh; |
17 |
extern CGprogram fsh; |
18 |
extern CGparameter mv, mvp, lightpos; |
19 |
extern CGprofile vsh_profile, fsh_profile; |
20 |
|
21 |
#include <GL/gl.h> |
22 |
|
23 |
typedef int soffs; // 32 bit |
24 |
typedef unsigned int uoffs; |
25 |
#define OFFS_BITS 31 |
26 |
#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 |
|
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 |
|
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 |
|
60 |
struct matrix { |
61 |
GLfloat data[4][4]; |
62 |
|
63 |
const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
64 |
GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
65 |
|
66 |
void diagonal (GLfloat v); |
67 |
void clear () { diagonal (0.); }; |
68 |
void identity () { diagonal (1.); }; |
69 |
|
70 |
void print (); // ugly |
71 |
|
72 |
static const matrix translation (const vec3 &v); |
73 |
static const matrix rotation (GLfloat degrees, const vec3 &axis); |
74 |
|
75 |
matrix () { }; |
76 |
matrix (GLfloat diag) { diagonal (diag); }; |
77 |
}; |
78 |
|
79 |
const matrix operator *(const matrix &a, const matrix &b); |
80 |
const vec3 operator *(const matrix &a, const vec3 &v); |
81 |
|
82 |
typedef vec3 point; |
83 |
|
84 |
// a generic plane |
85 |
struct plane { |
86 |
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 operator +(const sector &a, const sector &b) |
112 |
{ |
113 |
return sector (a.x + b.x, a.y + b.y, a.z + b.z); |
114 |
} |
115 |
|
116 |
inline const sector operator -(const sector &a, const sector &b) |
117 |
{ |
118 |
return sector (a.x - b.x, a.y - b.y, a.z - b.z); |
119 |
} |
120 |
|
121 |
inline const sector translate (const sector &p, const sector &src, const sector &dst) |
122 |
{ |
123 |
sector r; |
124 |
|
125 |
r.x = p.x + (dst.x - src.x); |
126 |
r.y = p.y + (dst.y - src.y); |
127 |
r.z = p.z + (dst.z - src.z); |
128 |
|
129 |
return r; |
130 |
} |
131 |
|
132 |
void renormalize (sector &s, point &p); |
133 |
|
134 |
struct colour { |
135 |
GLfloat r, g, b, a; |
136 |
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { }; |
137 |
}; |
138 |
|
139 |
struct texc { |
140 |
GLfloat s, t; |
141 |
texc () { }; |
142 |
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
143 |
}; |
144 |
|
145 |
struct box { |
146 |
sector a, b; |
147 |
|
148 |
box() { }; |
149 |
|
150 |
void reset () |
151 |
{ |
152 |
a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX); |
153 |
b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN); |
154 |
} |
155 |
|
156 |
void add (const box &o); |
157 |
void add (const sector &p); |
158 |
void add (const point &p); |
159 |
}; |
160 |
|
161 |
inline const box translate (const box &b, const sector &src, const sector &dst) |
162 |
{ |
163 |
box r; |
164 |
|
165 |
r.a = translate (b.a, src, dst); |
166 |
r.b = translate (b.b, src, dst); |
167 |
|
168 |
return r; |
169 |
} |
170 |
|
171 |
struct light { |
172 |
point p; |
173 |
colour c; |
174 |
GLfloat intensity; |
175 |
GLfloat radius; |
176 |
}; |
177 |
|
178 |
struct material { |
179 |
colour diffuse, specular, emission; |
180 |
GLfloat shininess; |
181 |
}; |
182 |
|
183 |
struct entity_base; |
184 |
struct view; |
185 |
|
186 |
extern struct timer { |
187 |
static double now; |
188 |
static double diff; |
189 |
|
190 |
static void frame (); |
191 |
timer (); |
192 |
} timer; |
193 |
|
194 |
/* |
195 |
#define MAX_EVENT_TYPES 10 |
196 |
enum event_type { TIMER_EV }; |
197 |
struct event { |
198 |
event_type type; |
199 |
}; |
200 |
|
201 |
typedef callback1<void, event&> event_cb; |
202 |
|
203 |
class skedjuhlar { |
204 |
|
205 |
public: |
206 |
// only 10 types for now |
207 |
private: |
208 |
vector <list<event_cb> > event_lists; |
209 |
|
210 |
public: |
211 |
skedjuhlar () { |
212 |
event_lists.resize (MAX_EVENT_TYPES, list<event_cb>()); |
213 |
} |
214 |
|
215 |
void register_event_cb (const event_type &t, const event_cb &e) { |
216 |
event_lists[t].push_back (e); |
217 |
}; |
218 |
void send_event (event &e) { |
219 |
list<event_cb> &l = event_lists[e.type]; |
220 |
for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) { |
221 |
(*it)(e); |
222 |
} |
223 |
}; |
224 |
void check_events () { |
225 |
while (!events.empty ()) { |
226 |
event &e = events.pop_front (); |
227 |
list<event_cb> &l = event_lists[e->name]; |
228 |
for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) { |
229 |
(*it)(e); |
230 |
} |
231 |
delete e; // ugly slow? hai hai..... 183G |
232 |
} |
233 |
} |
234 |
}; |
235 |
|
236 |
extern skedjuhlar main_scheduler; |
237 |
*/ |
238 |
|
239 |
GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord); |
240 |
inline int power_of_two (int input) |
241 |
{ |
242 |
int value = 1; |
243 |
|
244 |
while (value < input) |
245 |
{ |
246 |
value <<= 1; |
247 |
} |
248 |
return value; |
249 |
} |
250 |
|
251 |
|
252 |
|
253 |
struct TextureExecption { |
254 |
TextureExecption(string s) : msg(s) {} |
255 |
string msg; |
256 |
}; |
257 |
struct Texture { |
258 |
SDL_Surface *image; |
259 |
GLuint texture; |
260 |
GLfloat texcoord[4]; |
261 |
|
262 |
public: |
263 |
Texture(const char *f) { |
264 |
image = IMG_Load(f); |
265 |
if(!image) |
266 |
throw (TextureExecption("Couldn't load " + (string) f + ": " + (string) IMG_GetError ())); |
267 |
texture = SDL_GL_LoadTexture (image, (GLfloat*)&texcoord); |
268 |
if (texture == 0) |
269 |
throw (TextureExecption("Couldn't make GL Texture, failed to create new RGB SWSurface")); |
270 |
} |
271 |
}; |
272 |
|
273 |
#endif |
274 |
|