1 |
#ifndef UTIL_H |
2 |
#define UTIL_H |
3 |
|
4 |
#include <cmath> |
5 |
#include <vector> |
6 |
|
7 |
using namespace std; |
8 |
|
9 |
#include <GL/gl.h> |
10 |
|
11 |
typedef int soffs; // 32 bit |
12 |
typedef unsigned int uoffs; |
13 |
#define OFFS_BITS 31 |
14 |
#define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 1)) |
15 |
#define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 1)) |
16 |
#define MAXEXTENT ((1UL << OFFS_BITS) - 1) |
17 |
|
18 |
#define GLFLOAT_MAX 1e30 |
19 |
#define GLFLOAT_MIN -1e30 |
20 |
|
21 |
struct sector { |
22 |
soffs x, y, z; |
23 |
|
24 |
sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { }; |
25 |
|
26 |
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 |
}; |
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 |
inline const sector translate (const sector &p, const sector &src, const sector &dst) |
42 |
{ |
43 |
sector r; |
44 |
|
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 |
struct colour { |
53 |
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 |
}; |
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 |
|
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 |
|
84 |
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 |
void rotate (GLfloat degrees, const vec3 &axis); |
98 |
|
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 |
const vec3 operator *(const gl_matrix &a, const vec3 &v); |
105 |
|
106 |
struct texc { |
107 |
GLfloat s, t; |
108 |
texc () { }; |
109 |
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
110 |
}; |
111 |
|
112 |
struct box { |
113 |
sector a, b; |
114 |
|
115 |
box() { }; |
116 |
|
117 |
void reset () |
118 |
{ |
119 |
a = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN); |
120 |
b = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX); |
121 |
} |
122 |
|
123 |
void add (const box &o); |
124 |
void add (const sector &p); |
125 |
void add (const point &p); |
126 |
}; |
127 |
|
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 |
|
138 |
struct light { |
139 |
point p; |
140 |
colour c; |
141 |
GLfloat intensity; |
142 |
GLfloat radius; |
143 |
}; |
144 |
|
145 |
struct material { |
146 |
colour diffuse, specular, emission; |
147 |
GLfloat shininess; |
148 |
}; |
149 |
|
150 |
struct entity_base; |
151 |
struct draw_context; |
152 |
|
153 |
extern struct timer { |
154 |
static double now; |
155 |
static double diff; |
156 |
|
157 |
static void frame (); |
158 |
timer (); |
159 |
} timer; |
160 |
|
161 |
#endif |
162 |
|