#ifndef UTIL_H #define UTIL_H #include #include #include #include #include #include #include #include using namespace std; extern CGcontext cgc; extern CGprogram vsh; extern CGprogram fsh; extern CGparameter mv, mvp, lightpos; extern CGprofile vsh_profile, fsh_profile; #include typedef int soffs; // 32 bit typedef unsigned int uoffs; #define OFFS_BITS 31 #define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 2)) #define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 2)) #define MAXEXTENT (1UL << (OFFS_BITS - 1)) #define GLFLOAT_MAX 1e30 #define GLFLOAT_MIN -1e30 struct vec3 { GLfloat x, y, z; vec3 () { }; vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; const vec3 operator -() const { return vec3 (-x, -y, -z); } }; const vec3 normalize (const vec3 &v); const vec3 cross (const vec3 &a, const vec3 &b); inline const vec3 operator *(const vec3 &a, GLfloat s) { return vec3 (a.x * s, a.y * s, a.z * s); } inline const vec3 operator +(const vec3 &a, const vec3 &b) { return vec3 (a.x + b.x, a.y + b.y, a.z + b.z); } inline const vec3 operator -(const vec3 &a, const vec3 &b) { return vec3 (a.x - b.x, a.y - b.y, a.z - b.z); } inline GLfloat dot (const vec3 &a, const vec3 &b) { return a.x * b.x + a.y * b.y + a.z * b.z; } inline const GLfloat abs (const vec3 &v) { return sqrtf (dot (v, v)); } struct matrix { GLfloat data[4][4]; const GLfloat operator ()(int i, int j) const { return data[j][i]; }; GLfloat &operator ()(int i, int j) { return data[j][i]; }; void diagonal (GLfloat v); void clear () { diagonal (0.); }; void identity () { diagonal (1.); }; void print (); // ugly static const matrix translation (const vec3 &v); static const matrix rotation (GLfloat degrees, const vec3 &axis); matrix () { }; matrix (GLfloat diag) { diagonal (diag); }; }; const matrix operator *(const matrix &a, const matrix &b); const vec3 operator *(const matrix &a, const vec3 &v); typedef vec3 point; // a generic plane struct plane { vec3 n; GLfloat d; GLfloat distance (const point &p) const { return dot (n, p) + d; } plane () { }; plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); }; struct sector { soffs x, y, z; sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { }; void offset (int subindex, uoffs extent) { if (subindex & 1) x += extent; if (subindex & 2) y += extent; if (subindex & 4) z += extent; } }; inline const sector operator +(const sector &a, const sector &b) { return sector (a.x + b.x, a.y + b.y, a.z + b.z); } inline const sector operator -(const sector &a, const sector &b) { return sector (a.x - b.x, a.y - b.y, a.z - b.z); } inline const sector translate (const sector &p, const sector &src, const sector &dst) { return p + (dst - src); } void renormalize (sector &s, point &p); struct colour { GLfloat r, g, b, a; colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { }; }; struct texc { GLfloat s, t; texc () { }; texc (GLfloat s, GLfloat t) : s(s), t(t) { }; }; struct box { point a, b; box() { }; void reset () { a = point ( FLT_MAX, FLT_MAX, FLT_MAX); b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX); } void add (const box &o); void add (const point &p); }; struct light { point p; colour c; GLfloat intensity; GLfloat radius; }; struct material { colour diffuse, specular, emission; GLfloat shininess; material () { diffuse = colour (1, 0, 1, 1); specular = colour (1, 0, 1, 1); emission = colour (1, 0, 1, 1); shininess = 1; } }; struct entity; struct geometry; struct view; extern struct timer { static double now; static double diff; static void frame (); timer (); } timer; /* #define MAX_EVENT_TYPES 10 enum event_type { TIMER_EV }; struct event { event_type type; }; typedef callback1 event_cb; class skedjuhlar { public: // only 10 types for now private: vector > event_lists; public: skedjuhlar () { event_lists.resize (MAX_EVENT_TYPES, list()); } void register_event_cb (const event_type &t, const event_cb &e) { event_lists[t].push_back (e); }; void send_event (event &e) { list &l = event_lists[e.type]; for (list::iterator it = l.begin (); it != l.end (); it++) { (*it)(e); } }; void check_events () { while (!events.empty ()) { event &e = events.pop_front (); list &l = event_lists[e->name]; for (list::iterator it = l.begin (); it !+ l.end (); it++) { (*it)(e); } delete e; // ugly slow? hai hai..... 183G } } }; extern skedjuhlar main_scheduler; */ GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord); inline int power_of_two (int input) { int value = 1; while (value < input) { value <<= 1; } return value; } struct TextureExecption { TextureExecption (string s) : msg(s) { } string msg; }; struct Texture { SDL_Surface *image; GLuint texture; GLfloat texcoord[4]; public: Texture(const char *f) { image = IMG_Load (f); if (!image) throw (TextureExecption ("Couldn't load " + (string) f + ": " + (string) IMG_GetError ())); texture = SDL_GL_LoadTexture (image, (GLfloat*)&texcoord); if (texture == 0) throw (TextureExecption ("Couldn't make GL Texture, failed to create new RGB SWSurface")); } }; #endif