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 - 2)) |
15 |
#define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 2)) |
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 |
void renormalize (sector &s, point &p); |
53 |
|
54 |
struct colour { |
55 |
GLfloat r, g, b, a; |
56 |
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { }; |
57 |
}; |
58 |
|
59 |
struct vec3 { |
60 |
GLfloat x, y, z; |
61 |
vec3 () { }; |
62 |
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
63 |
|
64 |
const vec3 operator -() const |
65 |
{ return vec3 (-x, -y, -z); } |
66 |
}; |
67 |
|
68 |
const vec3 normalize (const vec3 &v); |
69 |
const vec3 cross (const vec3 &a, const vec3 &b); |
70 |
|
71 |
inline const vec3 operator *(const vec3 &a, GLfloat s) |
72 |
{ |
73 |
return vec3 (a.x * s, a.y * s, a.z * s); |
74 |
} |
75 |
|
76 |
inline GLfloat dot (const vec3 &a, const vec3 &b) |
77 |
{ |
78 |
return a.x * b.x + a.y * b.y + a.z * b.z; |
79 |
} |
80 |
|
81 |
inline const GLfloat abs (const vec3 &v) |
82 |
{ |
83 |
return sqrtf (dot (v, v)); |
84 |
} |
85 |
|
86 |
struct gl_matrix { |
87 |
GLfloat data[4][4]; |
88 |
|
89 |
const GLfloat operator ()(int i, int j) const { return data[i][j]; }; |
90 |
GLfloat &operator ()(int i, int j) { return data[i][j]; }; |
91 |
|
92 |
void diagonal (GLfloat v); |
93 |
void clear () { diagonal (0.); }; |
94 |
void identity () { diagonal (1.); }; |
95 |
|
96 |
void print (); // ugly |
97 |
|
98 |
void translate (const vec3 &v); |
99 |
void rotate (GLfloat degrees, const vec3 &axis); |
100 |
|
101 |
gl_matrix () { }; |
102 |
gl_matrix (GLfloat diag) { diagonal (diag); }; |
103 |
}; |
104 |
|
105 |
const gl_matrix operator *(const gl_matrix &a, const gl_matrix &b); |
106 |
const vec3 operator *(const gl_matrix &a, const vec3 &v); |
107 |
|
108 |
struct texc { |
109 |
GLfloat s, t; |
110 |
texc () { }; |
111 |
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
112 |
}; |
113 |
|
114 |
struct box { |
115 |
sector a, b; |
116 |
|
117 |
box() { }; |
118 |
|
119 |
void reset () |
120 |
{ |
121 |
a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX); |
122 |
b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN); |
123 |
} |
124 |
|
125 |
void add (const box &o); |
126 |
void add (const sector &p); |
127 |
void add (const point &p); |
128 |
}; |
129 |
|
130 |
inline const box translate (const box &b, const sector &src, const sector &dst) |
131 |
{ |
132 |
box r; |
133 |
|
134 |
r.a = translate (b.a, src, dst); |
135 |
r.b = translate (b.b, src, dst); |
136 |
|
137 |
return r; |
138 |
} |
139 |
|
140 |
struct light { |
141 |
point p; |
142 |
colour c; |
143 |
GLfloat intensity; |
144 |
GLfloat radius; |
145 |
}; |
146 |
|
147 |
struct material { |
148 |
colour diffuse, specular, emission; |
149 |
GLfloat shininess; |
150 |
}; |
151 |
|
152 |
struct entity_base; |
153 |
struct draw_context; |
154 |
|
155 |
extern struct timer { |
156 |
static double now; |
157 |
static double diff; |
158 |
|
159 |
static void frame (); |
160 |
timer (); |
161 |
} timer; |
162 |
|
163 |
#endif |
164 |
|