| 1 |
root |
1.1 |
#ifndef UTIL_H |
| 2 |
|
|
#define UTIL_H |
| 3 |
|
|
|
| 4 |
root |
1.7 |
#include <cmath> |
| 5 |
root |
1.1 |
#include <vector> |
| 6 |
|
|
|
| 7 |
|
|
using namespace std; |
| 8 |
|
|
|
| 9 |
root |
1.7 |
#include <GL/gl.h> |
| 10 |
|
|
|
| 11 |
root |
1.3 |
typedef int soffs; // 32 bit |
| 12 |
|
|
typedef unsigned int uoffs; |
| 13 |
root |
1.4 |
#define OFFS_BITS 31 |
| 14 |
|
|
#define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 1)) |
| 15 |
root |
1.8 |
#define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 1)) |
| 16 |
root |
1.4 |
#define MAXEXTENT ((1UL << OFFS_BITS) - 1) |
| 17 |
root |
1.1 |
|
| 18 |
|
|
#define GLFLOAT_MAX 1e30 |
| 19 |
|
|
#define GLFLOAT_MIN -1e30 |
| 20 |
|
|
|
| 21 |
|
|
struct sector { |
| 22 |
|
|
soffs x, y, z; |
| 23 |
root |
1.3 |
|
| 24 |
root |
1.8 |
sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { }; |
| 25 |
|
|
|
| 26 |
root |
1.3 |
void offset (int subindex, uoffs extent) |
| 27 |
|
|
{ |
| 28 |
|
|
if (subindex & 1) x += extent; |
| 29 |
|
|
if (subindex & 2) y += extent; |
| 30 |
|
|
if (subindex & 4) z += extent; |
| 31 |
|
|
} |
| 32 |
root |
1.1 |
}; |
| 33 |
|
|
|
| 34 |
|
|
struct point { |
| 35 |
|
|
GLfloat x, y, z; |
| 36 |
|
|
|
| 37 |
|
|
point () { }; |
| 38 |
|
|
point (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
| 39 |
|
|
}; |
| 40 |
|
|
|
| 41 |
root |
1.8 |
inline const sector translate (const sector &p, const sector &src, const sector &dst) |
| 42 |
root |
1.5 |
{ |
| 43 |
root |
1.8 |
sector r; |
| 44 |
root |
1.5 |
|
| 45 |
|
|
r.x = p.x + (dst.x - src.x); |
| 46 |
|
|
r.y = p.y + (dst.y - src.y); |
| 47 |
|
|
r.z = p.z + (dst.z - src.z); |
| 48 |
|
|
|
| 49 |
|
|
return r; |
| 50 |
|
|
} |
| 51 |
|
|
|
| 52 |
root |
1.1 |
struct colour { |
| 53 |
root |
1.2 |
GLfloat r, g, b, a; |
| 54 |
|
|
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { }; |
| 55 |
root |
1.1 |
}; |
| 56 |
|
|
|
| 57 |
|
|
struct vec3 { |
| 58 |
|
|
GLfloat x, y, z; |
| 59 |
|
|
vec3 () { }; |
| 60 |
|
|
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
| 61 |
|
|
|
| 62 |
|
|
const vec3 operator -() const |
| 63 |
|
|
{ return vec3 (-x, -y, -z); } |
| 64 |
|
|
}; |
| 65 |
|
|
|
| 66 |
|
|
const vec3 normalize (const vec3 &v); |
| 67 |
|
|
const vec3 cross (const vec3 &a, const vec3 &b); |
| 68 |
root |
1.6 |
|
| 69 |
|
|
inline const vec3 operator *(const vec3 &a, GLfloat s) |
| 70 |
|
|
{ |
| 71 |
|
|
return vec3 (a.x * s, a.y * s, a.z * s); |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
inline GLfloat dot (const vec3 &a, const vec3 &b) |
| 75 |
|
|
{ |
| 76 |
|
|
return a.x * b.x + a.y * b.y + a.z * b.z; |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
inline const GLfloat abs (const vec3 &v) |
| 80 |
|
|
{ |
| 81 |
|
|
return sqrtf (dot (v, v)); |
| 82 |
|
|
} |
| 83 |
root |
1.1 |
|
| 84 |
root |
1.5 |
struct gl_matrix { |
| 85 |
|
|
GLfloat data[4][4]; |
| 86 |
|
|
|
| 87 |
|
|
const GLfloat operator ()(int i, int j) const { return data[i][j]; }; |
| 88 |
|
|
GLfloat &operator ()(int i, int j) { return data[i][j]; }; |
| 89 |
|
|
|
| 90 |
|
|
void diagonal (GLfloat v); |
| 91 |
|
|
void clear () { diagonal (0.); }; |
| 92 |
|
|
void identity () { diagonal (1.); }; |
| 93 |
|
|
|
| 94 |
|
|
void print (); // ugly |
| 95 |
|
|
|
| 96 |
|
|
void translate (const vec3 &v); |
| 97 |
root |
1.6 |
void rotate (GLfloat degrees, const vec3 &axis); |
| 98 |
root |
1.5 |
|
| 99 |
|
|
gl_matrix () { }; |
| 100 |
|
|
gl_matrix (GLfloat diag) { diagonal (diag); }; |
| 101 |
|
|
}; |
| 102 |
|
|
|
| 103 |
|
|
const gl_matrix operator *(const gl_matrix &a, const gl_matrix &b); |
| 104 |
root |
1.6 |
const vec3 operator *(const gl_matrix &a, const vec3 &v); |
| 105 |
root |
1.5 |
|
| 106 |
root |
1.1 |
struct texc { |
| 107 |
|
|
GLfloat s, t; |
| 108 |
|
|
texc () { }; |
| 109 |
|
|
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
| 110 |
|
|
}; |
| 111 |
|
|
|
| 112 |
|
|
struct box { |
| 113 |
root |
1.8 |
sector a, b; |
| 114 |
|
|
|
| 115 |
|
|
box() { }; |
| 116 |
root |
1.1 |
|
| 117 |
|
|
void reset () |
| 118 |
|
|
{ |
| 119 |
root |
1.8 |
a = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN); |
| 120 |
|
|
b = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX); |
| 121 |
root |
1.1 |
} |
| 122 |
|
|
|
| 123 |
|
|
void add (const box &o); |
| 124 |
root |
1.8 |
void add (const sector &p); |
| 125 |
root |
1.1 |
void add (const point &p); |
| 126 |
|
|
}; |
| 127 |
root |
1.5 |
|
| 128 |
|
|
inline const box translate (const box &b, const sector &src, const sector &dst) |
| 129 |
|
|
{ |
| 130 |
|
|
box r; |
| 131 |
|
|
|
| 132 |
|
|
r.a = translate (b.a, src, dst); |
| 133 |
|
|
r.b = translate (b.b, src, dst); |
| 134 |
|
|
|
| 135 |
|
|
return r; |
| 136 |
|
|
} |
| 137 |
root |
1.1 |
|
| 138 |
|
|
struct light { |
| 139 |
|
|
point p; |
| 140 |
|
|
colour c; |
| 141 |
|
|
GLfloat intensity; |
| 142 |
|
|
GLfloat radius; |
| 143 |
root |
1.2 |
}; |
| 144 |
|
|
|
| 145 |
|
|
struct material { |
| 146 |
|
|
colour diffuse, specular, emission; |
| 147 |
|
|
GLfloat shininess; |
| 148 |
root |
1.1 |
}; |
| 149 |
|
|
|
| 150 |
root |
1.4 |
struct entity_base; |
| 151 |
|
|
struct draw_context; |
| 152 |
root |
1.7 |
|
| 153 |
|
|
extern struct timer { |
| 154 |
|
|
static double now; |
| 155 |
|
|
static double diff; |
| 156 |
|
|
|
| 157 |
|
|
static void frame (); |
| 158 |
|
|
timer (); |
| 159 |
|
|
} timer; |
| 160 |
root |
1.1 |
|
| 161 |
|
|
#endif |
| 162 |
|
|
|