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