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