#ifndef UTIL_H #define UTIL_H #include #include using namespace std; #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 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 translate (const sector &p, const sector &src, const sector &dst) { sector r; r.x = p.x + (dst.x - src.x); r.y = p.y + (dst.y - src.y); r.z = p.z + (dst.z - src.z); return r; } 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 { sector a, b; box() { }; void reset () { a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX); b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN); } void add (const box &o); void add (const sector &p); void add (const point &p); }; inline const box translate (const box &b, const sector &src, const sector &dst) { box r; r.a = translate (b.a, src, dst); r.b = translate (b.b, src, dst); return r; } struct light { point p; colour c; GLfloat intensity; GLfloat radius; }; struct material { colour diffuse, specular, emission; GLfloat shininess; }; struct entity_base; struct draw_context; 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; */ #endif