1 |
#include <algorithm> |
2 |
|
3 |
using namespace std; |
4 |
|
5 |
#include "util.h" |
6 |
#include "entity.h" |
7 |
#include "oct.h" |
8 |
#include "view.h" |
9 |
|
10 |
entity_base::entity_base () |
11 |
: parent(0) |
12 |
{ |
13 |
} |
14 |
|
15 |
entity_base::~entity_base () |
16 |
{ |
17 |
hide (); |
18 |
} |
19 |
|
20 |
void entity_base::update () |
21 |
{ |
22 |
if (parent) |
23 |
parent->update (); |
24 |
} |
25 |
|
26 |
void entity_base::hide () |
27 |
{ |
28 |
for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); ) |
29 |
(*i)->remove (this); |
30 |
|
31 |
o.clear (); |
32 |
} |
33 |
|
34 |
void entity_base::display (draw_context &ctx) |
35 |
{ |
36 |
if (ctx.may_draw (this)) |
37 |
draw (ctx); |
38 |
} |
39 |
|
40 |
///////////////////////////////////////////////////////////////////////////// |
41 |
|
42 |
void entity_filter::update () |
43 |
{ |
44 |
bbox = translate (e->bbox, e->orig, orig); |
45 |
|
46 |
entity_base::update (); |
47 |
} |
48 |
|
49 |
void entity_filter::show () |
50 |
{ |
51 |
entity_base::show (); |
52 |
|
53 |
e->show (); |
54 |
} |
55 |
|
56 |
void entity_filter::draw (draw_context &ctx) |
57 |
{ |
58 |
e->display (ctx); |
59 |
} |
60 |
|
61 |
entity_filter::~entity_filter () |
62 |
{ |
63 |
delete e; |
64 |
} |
65 |
|
66 |
///////////////////////////////////////////////////////////////////////////// |
67 |
|
68 |
void entity_container::update () |
69 |
{ |
70 |
bbox.reset (); |
71 |
|
72 |
for (iterator i = end (); i-- != begin (); ) |
73 |
bbox.add ((*i)->bbox); |
74 |
|
75 |
entity_base::update (); |
76 |
} |
77 |
|
78 |
void entity_container::show () |
79 |
{ |
80 |
entity_base::show (); |
81 |
|
82 |
for (iterator i = end (); i-- != begin (); ) |
83 |
(*i)->show (); |
84 |
} |
85 |
|
86 |
void entity_container::draw (draw_context &ctx) |
87 |
{ |
88 |
for (iterator i = end (); i-- != begin (); ) |
89 |
(*i)->display (ctx); |
90 |
} |
91 |
|
92 |
entity_container::~entity_container () |
93 |
{ |
94 |
hide (); |
95 |
|
96 |
for (iterator i = end (); i-- != begin (); ) |
97 |
delete *i; |
98 |
|
99 |
clear (); |
100 |
} |
101 |
|
102 |
///////////////////////////////////////////////////////////////////////////// |
103 |
|
104 |
entity_opengl::entity_opengl () |
105 |
{ |
106 |
list = glGenLists (1); |
107 |
} |
108 |
|
109 |
entity_opengl::~entity_opengl () |
110 |
{ |
111 |
glDeleteLists (list, 1); |
112 |
} |
113 |
|
114 |
template class entity_opengl1d<GL_POINTS>; |
115 |
template class entity_opengl1d<GL_LINES>; |
116 |
template class entity_opengl1d<GL_LINE_STRIP>; |
117 |
template class entity_opengl1d<GL_LINE_LOOP>; |
118 |
template class entity_opengl2d<GL_TRIANGLES>; |
119 |
template class entity_opengl2d<GL_TRIANGLE_STRIP>; |
120 |
template class entity_opengl2d<GL_TRIANGLE_FAN>; |
121 |
template class entity_opengl2d<GL_QUADS>; |
122 |
template class entity_opengl2d<GL_QUAD_STRIP>; |
123 |
template class entity_opengl2d<GL_POLYGON>; |
124 |
|
125 |
template<GLenum type> |
126 |
void entity_opengl1d<type>::update () |
127 |
{ |
128 |
bbox.reset (); |
129 |
|
130 |
for (vector<vertex1d>::iterator i = end (); i-- != begin (); ) |
131 |
bbox.add (i->p); |
132 |
|
133 |
entity_base::update (); |
134 |
} |
135 |
|
136 |
template<GLenum type> |
137 |
void entity_opengl1d<type>::draw (draw_context &ctx) |
138 |
{ |
139 |
glBegin (type); |
140 |
|
141 |
for (iterator i = begin (); i < end (); ++i) |
142 |
{ |
143 |
glColor3fv ((GLfloat *)&i->c); |
144 |
glVertex3fv ((GLfloat *)&i->p); |
145 |
} |
146 |
|
147 |
glEnd (); |
148 |
} |
149 |
|
150 |
template<GLenum type> |
151 |
void entity_opengl2d<type>::set (const vector<vertex2d> &v) |
152 |
{ |
153 |
bbox.reset (); |
154 |
|
155 |
for (vector<vertex2d>::const_iterator i = v.end (); i-- != v.begin (); ) |
156 |
bbox.add (i->p); |
157 |
|
158 |
update (); |
159 |
|
160 |
glNewList (list, GL_COMPILE); |
161 |
|
162 |
glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse); |
163 |
glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular); |
164 |
glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission); |
165 |
glMaterialf (GL_FRONT, GL_SHININESS, m.shininess); |
166 |
|
167 |
#if 0 |
168 |
glBegin (type); |
169 |
|
170 |
for (vector<vertex2d>::const_iterator i = v.begin (); i < v.end (); ++i) |
171 |
{ |
172 |
glTexCoord2fv ((GLfloat *)&i->t); |
173 |
glNormal3fv ((GLfloat *)&i->n); |
174 |
glVertex3fv ((GLfloat *)&i->p); |
175 |
} |
176 |
|
177 |
glEnd (); |
178 |
#else |
179 |
glEnableClientState (GL_VERTEX_ARRAY); |
180 |
glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->p); |
181 |
glEnableClientState (GL_NORMAL_ARRAY); |
182 |
glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->n); |
183 |
glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
184 |
glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->t); |
185 |
|
186 |
glDrawArrays (type, 0, v.size ()); |
187 |
#endif |
188 |
|
189 |
glEndList (); |
190 |
} |
191 |
|
192 |
template<GLenum type> |
193 |
void entity_opengl2d<type>::draw (draw_context &ctx) |
194 |
{ |
195 |
glPushMatrix (); |
196 |
const sector &corig = ctx.v.orig; |
197 |
glTranslatef (orig.x - corig.x, orig.y - corig.y, orig.z - corig.z); |
198 |
update_matrix (ctx);//D |
199 |
glCallList (list); |
200 |
glPopMatrix (); |
201 |
} |
202 |
|
203 |
///////////////////////////////////////////////////////////////////////////// |
204 |
|
205 |
entity_transform::entity_transform () |
206 |
{ |
207 |
m.identity (); |
208 |
} |
209 |
|
210 |
void entity_transform::update () |
211 |
{ |
212 |
box sub = translate (e->bbox, e->orig, orig); |
213 |
|
214 |
bbox.reset (); |
215 |
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z)); |
216 |
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z)); |
217 |
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z)); |
218 |
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z)); |
219 |
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z)); |
220 |
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z)); |
221 |
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z)); |
222 |
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z)); |
223 |
|
224 |
entity_base::update (); |
225 |
} |
226 |
|
227 |
void entity_transform::renormalize () |
228 |
{ |
229 |
#if 0 |
230 |
point trans(m(0,0), m(1,0), m(2,0)); |
231 |
::renormalize (orig, trans); |
232 |
m(0,0) = trans.x; m(1,0) = trans.y; m(2,0) = trans.z; |
233 |
#endif |
234 |
} |
235 |
|
236 |
void entity_transform::update (const gl_matrix &xfrm) |
237 |
{ |
238 |
m = m * xfrm; |
239 |
|
240 |
renormalize (); |
241 |
update (); |
242 |
} |
243 |
|
244 |
void entity_transform::set_matrix (const gl_matrix &xfrm) |
245 |
{ |
246 |
m = xfrm; |
247 |
|
248 |
renormalize (); |
249 |
update (); |
250 |
} |
251 |
|
252 |
void entity_transform::show () |
253 |
{ |
254 |
entity_base::show (); |
255 |
} |
256 |
|
257 |
void entity_transform::draw (draw_context &ctx) |
258 |
{ |
259 |
glPushMatrix (); |
260 |
glMultMatrixf ((GLfloat *)&m); |
261 |
#if 0 |
262 |
const sector &corig = ctx.v.orig; |
263 |
glTranslatef (orig.x - corig.x, orig.y - corig.y, orig.z - corig.z); |
264 |
#endif |
265 |
update_matrix (ctx);//D |
266 |
e->draw (ctx); |
267 |
glPopMatrix (); |
268 |
} |
269 |
|
270 |
void entity_anim::draw (draw_context &ctx) |
271 |
{ |
272 |
gl_matrix save_m = m; |
273 |
|
274 |
update (gl_matrix::rotation (vx * timer.now, vec3 (1, 0, 0)) |
275 |
* gl_matrix::rotation (vy * timer.now, vec3 (0, 1, 0)) |
276 |
* gl_matrix::rotation (vz * timer.now, vec3 (0, 0, 1))); |
277 |
|
278 |
entity_transform::draw (ctx); |
279 |
|
280 |
m = save_m; |
281 |
} |
282 |
|
283 |
|