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