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 |
typedef int soffs; // 32 bit |
11 |
typedef unsigned int uoffs; |
12 |
#define OFFS_BITS 31 |
13 |
#define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 1)) |
14 |
#define MAXEXTENT ((1UL << OFFS_BITS) - 1) |
15 |
|
16 |
#define GLFLOAT_MAX 1e30 |
17 |
#define GLFLOAT_MIN -1e30 |
18 |
|
19 |
struct sector { |
20 |
soffs x, y, z; |
21 |
|
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 |
}; |
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 |
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 |
struct colour { |
49 |
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 |
}; |
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 |
|
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 |
|
80 |
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 |
void rotate (GLfloat degrees, const vec3 &axis); |
94 |
|
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 |
const vec3 operator *(const gl_matrix &a, const vec3 &v); |
101 |
|
102 |
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 |
|
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 |
|
131 |
struct light { |
132 |
point p; |
133 |
colour c; |
134 |
GLfloat intensity; |
135 |
GLfloat radius; |
136 |
}; |
137 |
|
138 |
struct material { |
139 |
colour diffuse, specular, emission; |
140 |
GLfloat shininess; |
141 |
}; |
142 |
|
143 |
struct entity_base; |
144 |
struct draw_context; |
145 |
|
146 |
#endif |
147 |
|