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 |
root |
1.9 |
#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 |
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.9 |
void renormalize (sector &s, point &p); |
53 |
|
|
|
54 |
root |
1.1 |
struct colour { |
55 |
root |
1.2 |
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 |
root |
1.1 |
}; |
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 |
root |
1.6 |
|
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 |
root |
1.1 |
|
86 |
root |
1.5 |
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 |
root |
1.6 |
void rotate (GLfloat degrees, const vec3 &axis); |
100 |
root |
1.5 |
|
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 |
root |
1.6 |
const vec3 operator *(const gl_matrix &a, const vec3 &v); |
107 |
root |
1.5 |
|
108 |
root |
1.1 |
struct texc { |
109 |
|
|
GLfloat s, t; |
110 |
|
|
texc () { }; |
111 |
|
|
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
112 |
|
|
}; |
113 |
|
|
|
114 |
|
|
struct box { |
115 |
root |
1.8 |
sector a, b; |
116 |
|
|
|
117 |
|
|
box() { }; |
118 |
root |
1.1 |
|
119 |
|
|
void reset () |
120 |
|
|
{ |
121 |
root |
1.9 |
a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX); |
122 |
|
|
b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN); |
123 |
root |
1.1 |
} |
124 |
|
|
|
125 |
|
|
void add (const box &o); |
126 |
root |
1.8 |
void add (const sector &p); |
127 |
root |
1.1 |
void add (const point &p); |
128 |
|
|
}; |
129 |
root |
1.5 |
|
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 |
root |
1.1 |
|
140 |
|
|
struct light { |
141 |
|
|
point p; |
142 |
|
|
colour c; |
143 |
|
|
GLfloat intensity; |
144 |
|
|
GLfloat radius; |
145 |
root |
1.2 |
}; |
146 |
|
|
|
147 |
|
|
struct material { |
148 |
|
|
colour diffuse, specular, emission; |
149 |
|
|
GLfloat shininess; |
150 |
root |
1.1 |
}; |
151 |
|
|
|
152 |
root |
1.4 |
struct entity_base; |
153 |
|
|
struct draw_context; |
154 |
root |
1.7 |
|
155 |
|
|
extern struct timer { |
156 |
|
|
static double now; |
157 |
|
|
static double diff; |
158 |
|
|
|
159 |
|
|
static void frame (); |
160 |
|
|
timer (); |
161 |
|
|
} timer; |
162 |
root |
1.1 |
|
163 |
|
|
#endif |
164 |
|
|
|