1 | #ifndef UTIL_H |
1 | #ifndef UTIL_H |
2 | #define UTIL_H |
2 | #define UTIL_H |
3 | |
3 | |
4 | #include <SDL/SDL.h> |
|
|
5 | #include <SDL/SDL_image.h> |
|
|
6 | #include <cmath> |
4 | #include <cmath> |
7 | #include <cfloat> |
5 | #include <cfloat> |
8 | #include <vector> |
6 | #include <vector> |
9 | #include <string> |
7 | #include <string> |
10 | |
8 | |
11 | #include <Cg/cg.h> |
|
|
12 | #include <Cg/cgGL.h> |
|
|
13 | |
|
|
14 | #include <GL/gl.h> |
|
|
15 | #include <GL/glext.h> |
|
|
16 | #include "gldebug.h" |
9 | #include "opengl.h" |
|
|
10 | |
|
|
11 | #include <SDL/SDL_image.h> |
17 | |
12 | |
18 | using namespace std; |
13 | using namespace std; |
19 | |
14 | |
20 | extern CGcontext cgc; |
15 | extern CGcontext cgc; |
21 | extern CGprogram vsh; |
16 | extern CGprogram vsh; |
… | |
… | |
28 | #define OFFS_BITS 31 |
23 | #define OFFS_BITS 31 |
29 | #define SOFFS_MIN (soffs)-(1LL << (OFFS_BITS - 2)) |
24 | #define SOFFS_MIN (soffs)-(1LL << (OFFS_BITS - 2)) |
30 | #define SOFFS_MAX (soffs)+(1LL << (OFFS_BITS - 2)) |
25 | #define SOFFS_MAX (soffs)+(1LL << (OFFS_BITS - 2)) |
31 | #define MAXEXTENT (1ULL << (OFFS_BITS - 1)) |
26 | #define MAXEXTENT (1ULL << (OFFS_BITS - 1)) |
32 | |
27 | |
33 | struct sector { |
28 | struct sector |
|
|
29 | { |
34 | soffs x, y, z; |
30 | soffs x, y, z; |
35 | |
31 | |
36 | sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { }; |
32 | sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { }; |
37 | sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { }; |
33 | sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { }; |
38 | |
34 | |
… | |
… | |
77 | inline sector abs (const sector &s) |
73 | inline sector abs (const sector &s) |
78 | { |
74 | { |
79 | return sector (abs (s.x), abs (s.y), abs (s.z)); |
75 | return sector (abs (s.x), abs (s.y), abs (s.z)); |
80 | } |
76 | } |
81 | |
77 | |
82 | struct vec3 { |
78 | struct vec3 |
|
|
79 | { |
83 | GLfloat x, y, z; |
80 | GLfloat x, y, z; |
84 | vec3 () { }; |
81 | vec3 () { }; |
85 | vec3 (GLfloat s) : x(s), y(s), z(s) { }; |
82 | vec3 (GLfloat s) : x(s), y(s), z(s) { }; |
86 | vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
83 | vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
87 | vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { }; |
84 | vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { }; |
… | |
… | |
116 | inline const GLfloat abs (const vec3 &v) |
113 | inline const GLfloat abs (const vec3 &v) |
117 | { |
114 | { |
118 | return sqrtf (dot (v, v)); |
115 | return sqrtf (dot (v, v)); |
119 | } |
116 | } |
120 | |
117 | |
121 | struct matrix { |
118 | struct matrix |
|
|
119 | { |
122 | GLfloat data[4][4]; |
120 | GLfloat data[4][4]; |
123 | |
121 | |
124 | const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
122 | const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
125 | GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
123 | GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
126 | |
124 | |
… | |
… | |
143 | const vec3 operator *(const matrix &a, const vec3 &v); |
141 | const vec3 operator *(const matrix &a, const vec3 &v); |
144 | |
142 | |
145 | typedef vec3 point; |
143 | typedef vec3 point; |
146 | |
144 | |
147 | // a generic plane |
145 | // a generic plane |
148 | struct plane { |
146 | struct plane |
|
|
147 | { |
149 | vec3 n; |
148 | vec3 n; |
150 | GLfloat d; |
149 | GLfloat d; |
151 | |
150 | |
152 | GLfloat distance (const point &p) const |
151 | GLfloat distance (const point &p) const |
153 | { |
152 | { |
… | |
… | |
158 | plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); |
157 | plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); |
159 | }; |
158 | }; |
160 | |
159 | |
161 | void renormalize (sector &s, point &p); |
160 | void renormalize (sector &s, point &p); |
162 | |
161 | |
163 | struct colour { |
162 | struct colour |
|
|
163 | { |
164 | GLfloat r, g, b, a; |
164 | GLfloat r, g, b, a; |
165 | colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { }; |
165 | colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { }; |
166 | }; |
166 | }; |
167 | |
167 | |
168 | struct texc { |
168 | struct texc |
|
|
169 | { |
169 | GLfloat s, t; |
170 | GLfloat s, t; |
170 | texc () { }; |
171 | texc () { }; |
171 | texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
172 | texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
172 | }; |
173 | }; |
173 | |
174 | |
174 | struct box { |
175 | struct box |
|
|
176 | { |
175 | point a, b; |
177 | point a, b; |
176 | |
178 | |
177 | box() { }; |
179 | box() { }; |
178 | |
180 | |
179 | void reset () |
181 | void reset () |
… | |
… | |
184 | |
186 | |
185 | void add (const box &o); |
187 | void add (const box &o); |
186 | void add (const point &p); |
188 | void add (const point &p); |
187 | }; |
189 | }; |
188 | |
190 | |
189 | struct light { |
|
|
190 | point p; |
|
|
191 | colour c; |
|
|
192 | GLfloat intensity; |
|
|
193 | GLfloat radius; |
|
|
194 | }; |
|
|
195 | |
|
|
196 | struct material { |
|
|
197 | colour diffuse, specular, emission; |
|
|
198 | GLfloat shininess; |
|
|
199 | |
|
|
200 | material () |
|
|
201 | { |
|
|
202 | diffuse = colour (1, 0, 1, 1); |
|
|
203 | specular = colour (1, 0, 1, 1); |
|
|
204 | emission = colour (1, 0, 1, 1); |
|
|
205 | shininess = 1; |
|
|
206 | } |
|
|
207 | }; |
|
|
208 | |
|
|
209 | struct entity; |
191 | struct entity; |
210 | struct geometry; |
192 | struct geometry; |
211 | struct view; |
193 | struct view; |
212 | struct octant; |
194 | struct octant; |
213 | |
195 | |
214 | extern struct timer { |
196 | extern struct timer |
|
|
197 | { |
215 | static double now; |
198 | static double now; |
216 | static double diff; |
199 | static double diff; |
217 | |
200 | |
218 | static void frame (); |
201 | static void frame (); |
219 | timer (); |
202 | timer (); |
220 | } timer; |
203 | } timer; |
221 | |
204 | |
222 | /* |
205 | /* |
223 | #define MAX_EVENT_TYPES 10 |
206 | #define MAX_EVENT_TYPES 10 |
224 | enum event_type { TIMER_EV }; |
207 | enum event_type { TIMER_EV }; |
225 | struct event { |
208 | struct event |
|
|
209 | { |
226 | event_type type; |
210 | event_type type; |
227 | }; |
211 | }; |
228 | |
212 | |
229 | typedef callback1<void, event&> event_cb; |
213 | typedef callback1<void, event&> event_cb; |
230 | |
214 | |
231 | class skedjuhlar { |
215 | class skedjuhlar |
232 | |
216 | { |
233 | public: |
217 | public: |
234 | // only 10 types for now |
218 | // only 10 types for now |
235 | private: |
219 | private: |
236 | vector <list<event_cb> > event_lists; |
220 | vector <list<event_cb> > event_lists; |
237 | |
221 | |
… | |
… | |
262 | }; |
246 | }; |
263 | |
247 | |
264 | extern skedjuhlar main_scheduler; |
248 | extern skedjuhlar main_scheduler; |
265 | */ |
249 | */ |
266 | |
250 | |
267 | namespace gl { |
251 | namespace gl |
|
|
252 | { |
268 | void draw_box (const view &ctx, const sector &a, const sector &b); |
253 | void draw_box (const view &ctx, const sector &a, const sector &b); |
269 | |
254 | |
270 | extern int nesting; |
255 | extern int nesting; |
271 | void errchk (const char *name, const char *args, const char *file, int line); |
256 | void errchk (const char *name, const char *args, const char *file, int line); |
272 | } |
257 | } |
… | |
… | |
282 | value <<= 1; |
267 | value <<= 1; |
283 | } |
268 | } |
284 | return value; |
269 | return value; |
285 | } |
270 | } |
286 | |
271 | |
287 | struct TextureExecption { |
272 | struct TextureExecption |
|
|
273 | { |
288 | TextureExecption (string s) : msg(s) { } |
274 | TextureExecption (string s) : msg(s) { } |
289 | string msg; |
275 | string msg; |
290 | }; |
276 | }; |
291 | |
277 | |
292 | struct Texture { |
278 | struct Texture |
|
|
279 | { |
293 | SDL_Surface *image; |
280 | SDL_Surface *image; |
294 | GLuint texture; |
281 | GLuint texture; |
295 | GLfloat texcoord[4]; |
282 | GLfloat texcoord[4]; |
296 | |
283 | |
297 | public: |
284 | public: |