1 |
#ifndef ENTITY_H |
2 |
#define ENTITY_H |
3 |
|
4 |
#include <vector> |
5 |
|
6 |
#include <iostream> |
7 |
#include "opengl.h" |
8 |
|
9 |
#include "util.h" |
10 |
#include "oct.h" |
11 |
#include "view.h" |
12 |
#include "material.h" |
13 |
|
14 |
using namespace std; |
15 |
using namespace gl; |
16 |
|
17 |
struct geometry |
18 |
{ |
19 |
geometry *parent; |
20 |
box bbox; |
21 |
|
22 |
virtual void update () |
23 |
{ |
24 |
if (parent) |
25 |
parent->update (); |
26 |
} |
27 |
|
28 |
virtual void draw (view &ctx) = 0; |
29 |
geometry (geometry *parent = 0) : parent(parent) { }; |
30 |
virtual ~geometry (); |
31 |
}; |
32 |
|
33 |
struct geometry_opengl : geometry |
34 |
{ |
35 |
GLuint list; // TODO: dynamic caching |
36 |
|
37 |
geometry_opengl (); |
38 |
~geometry_opengl (); |
39 |
}; |
40 |
|
41 |
template<GLenum type> |
42 |
struct geometry_opengl1d : geometry_opengl, vector<vertex_v3f> |
43 |
{ |
44 |
void set (const vector<vertex_v3f> &v); |
45 |
void draw (view &ctx); |
46 |
}; |
47 |
|
48 |
template<GLenum type> |
49 |
struct geometry_opengl2d : geometry_opengl |
50 |
{ |
51 |
material *m; |
52 |
|
53 |
geometry_opengl2d () : m(0) { }; |
54 |
|
55 |
void set (const vector<vertex_t2f_n3f_v3f> &v); |
56 |
void draw (view &ctx); |
57 |
}; |
58 |
|
59 |
typedef geometry_opengl1d<GL_POINTS> geometry_points; |
60 |
typedef geometry_opengl1d<GL_LINES> geometry_lines; |
61 |
typedef geometry_opengl1d<GL_LINE_STRIP> geometry_line_strip; |
62 |
typedef geometry_opengl1d<GL_LINE_LOOP> geometry_line_loop; |
63 |
typedef geometry_opengl2d<GL_TRIANGLES> geometry_triangles; |
64 |
typedef geometry_opengl2d<GL_TRIANGLE_STRIP> geometry_triangle_strip; |
65 |
typedef geometry_opengl2d<GL_TRIANGLE_FAN> geometry_triangle_fan; |
66 |
typedef geometry_opengl2d<GL_QUADS> geometry_quads; |
67 |
typedef geometry_opengl2d<GL_QUAD_STRIP> geometry_quad_strip; |
68 |
typedef geometry_opengl2d<GL_POLYGON> geometry_polygon; |
69 |
|
70 |
struct geometry_nurbs : geometry, vector<vertex_t2f_n3f_v3f> |
71 |
{ |
72 |
GLUnurbsObj *nurb; |
73 |
GLUtesselator *tess; |
74 |
GLfloat ctlpoints[4][4][3]; |
75 |
|
76 |
geometry_nurbs() : tess(0), nurb(0) { } |
77 |
virtual void draw (view &ctx); |
78 |
void set (); |
79 |
}; |
80 |
|
81 |
struct geometry_sphere : geometry |
82 |
{ |
83 |
material *m; |
84 |
GLfloat radius; |
85 |
|
86 |
void update (); |
87 |
void draw (view &ctx); |
88 |
geometry_sphere (material *m, GLfloat radius) : m(m), radius(radius) { update (); }; |
89 |
}; |
90 |
|
91 |
struct geometry_indexed_2d : geometry |
92 |
{ |
93 |
material *m; |
94 |
|
95 |
GLenum type; |
96 |
|
97 |
vertex_buffer vb; |
98 |
index_buffer ib; |
99 |
|
100 |
void draw (view &ctx); |
101 |
|
102 |
template <typename vertex, typename index> |
103 |
geometry_indexed_2d (material *m, GLenum type, const vector<vertex> &v, const vector<index> &i) |
104 |
: m(m), type(type) |
105 |
{ |
106 |
vb.set (v); |
107 |
ib.set (i); |
108 |
|
109 |
bbox.reset (); |
110 |
for (typename vector<vertex>::const_iterator i = v.begin () ; i != v.end (); ++i) |
111 |
bbox.add (i->v); |
112 |
} |
113 |
|
114 |
}; |
115 |
|
116 |
///////////////////////////////////////////////////////////////////////////// |
117 |
|
118 |
struct geometry_filter : geometry |
119 |
{ |
120 |
protected: |
121 |
geometry *g; |
122 |
public: |
123 |
|
124 |
void set (geometry *g); |
125 |
|
126 |
geometry *content () { return g; }; |
127 |
|
128 |
void update (); |
129 |
void show (); |
130 |
void draw (view &ctx); |
131 |
geometry_filter (geometry *g = 0) { set (g); } |
132 |
~geometry_filter (); |
133 |
}; |
134 |
|
135 |
struct geometry_transform : geometry_filter |
136 |
{ |
137 |
protected: |
138 |
matrix m; |
139 |
public: |
140 |
void update (); |
141 |
|
142 |
void show (); |
143 |
void draw (view &ctx); |
144 |
void set_matrix (const matrix &xfrm); |
145 |
void update (const matrix &xfrm); |
146 |
|
147 |
geometry_transform (geometry *g) : geometry_filter(g), m(1) { }; |
148 |
geometry_transform () : geometry_filter(0), m(1) { }; |
149 |
}; |
150 |
|
151 |
struct geometry_anim : geometry_transform |
152 |
{ |
153 |
GLfloat vx, vy, vz; |
154 |
void draw (view &ctx); |
155 |
}; |
156 |
|
157 |
struct geometry_container : geometry, protected vector<geometry *> |
158 |
{ |
159 |
void update (); |
160 |
|
161 |
void add (geometry *g); |
162 |
void draw (view &ctx); |
163 |
~geometry_container (); |
164 |
}; |
165 |
|
166 |
///////////////////////////////////////////////////////////////////////////// |
167 |
|
168 |
struct entity_visibility : visibility_base { |
169 |
entity *e; |
170 |
double next; // time of next check |
171 |
int occ_res; |
172 |
|
173 |
void clear () |
174 |
{ |
175 |
next = 0.; |
176 |
occ_res = -1; |
177 |
} |
178 |
|
179 |
entity_visibility (entity &e) |
180 |
: e(&e) |
181 |
{ |
182 |
clear (); |
183 |
} |
184 |
}; |
185 |
|
186 |
struct entity : geometry_filter, visible |
187 |
{ |
188 |
sector orig; |
189 |
point p; |
190 |
sector a, b; // bounding box corners |
191 |
|
192 |
vector<octant *> o; |
193 |
|
194 |
void update (); |
195 |
void draw (view &ctx); |
196 |
|
197 |
void move (const vec3 &v); |
198 |
|
199 |
virtual void show (); |
200 |
virtual void hide (); |
201 |
|
202 |
visibility_base *new_visibility (); |
203 |
void clear_visibility (visibility_base *vs); |
204 |
|
205 |
entity (geometry *g = 0); |
206 |
~entity (); |
207 |
}; |
208 |
|
209 |
struct entity_moveable : entity |
210 |
{ |
211 |
vec3 v; |
212 |
entity_moveable (geometry *g = 0) : entity (g) { } |
213 |
|
214 |
void perform_step (double t); |
215 |
}; |
216 |
|
217 |
struct entity_light : entity |
218 |
{ |
219 |
light *lview; |
220 |
}; |
221 |
|
222 |
///////////////////////////////////////////////////////////////////////////// |
223 |
// not the final API(!) |
224 |
|
225 |
struct skybox |
226 |
{ |
227 |
texture *tex[6]; |
228 |
|
229 |
skybox ( |
230 |
const char *left, |
231 |
const char *front, |
232 |
const char *right, |
233 |
const char *back, |
234 |
const char *top, |
235 |
const char *bottom |
236 |
); |
237 |
|
238 |
~skybox (); |
239 |
|
240 |
void draw (view &ctx); |
241 |
}; |
242 |
|
243 |
///////////////////////////////////////////////////////////////////////////// |
244 |
// |
245 |
// VERY EXPERIMENTAL HEIGHTMAP |
246 |
|
247 |
struct geometry_heightfield : geometry |
248 |
{ |
249 |
struct node; |
250 |
|
251 |
node *tree; |
252 |
|
253 |
GLfloat sx, sy, sm; |
254 |
|
255 |
void update (); |
256 |
void draw (view &ctx); |
257 |
geometry_heightfield (GLfloat sx, GLfloat sy); |
258 |
}; |
259 |
|
260 |
#endif |
261 |
|
262 |
|
263 |
|