1 |
#include <algorithm> |
2 |
#include <cassert> |
3 |
|
4 |
using namespace std; |
5 |
|
6 |
#include "opengl.h" |
7 |
|
8 |
#include "util.h" |
9 |
#include "entity.h" |
10 |
#include "oct.h" |
11 |
#include "view.h" |
12 |
|
13 |
///////////////////////////////////////////////////////////////////////////// |
14 |
|
15 |
geometry::~geometry () |
16 |
{ |
17 |
} |
18 |
|
19 |
template class geometry_opengl1d<GL_POINTS>; |
20 |
template class geometry_opengl1d<GL_LINES>; |
21 |
template class geometry_opengl1d<GL_LINE_STRIP>; |
22 |
template class geometry_opengl1d<GL_LINE_LOOP>; |
23 |
template class geometry_opengl2d<GL_TRIANGLES>; |
24 |
template class geometry_opengl2d<GL_TRIANGLE_STRIP>; |
25 |
template class geometry_opengl2d<GL_TRIANGLE_FAN>; |
26 |
template class geometry_opengl2d<GL_QUADS>; |
27 |
template class geometry_opengl2d<GL_QUAD_STRIP>; |
28 |
template class geometry_opengl2d<GL_POLYGON>; |
29 |
|
30 |
geometry_opengl::geometry_opengl () |
31 |
{ |
32 |
list = glGenLists (1); |
33 |
} |
34 |
|
35 |
geometry_opengl::~geometry_opengl () |
36 |
{ |
37 |
glDeleteLists (list, 1); |
38 |
} |
39 |
|
40 |
template<GLenum type> |
41 |
void geometry_opengl1d<type>::set (const vector<vertex_v3f> &v) |
42 |
{ |
43 |
clear (); |
44 |
insert (end (), v.begin (), v.end ()); |
45 |
|
46 |
bbox.reset (); |
47 |
|
48 |
for (const_iterator i = end (); i-- != begin (); ) |
49 |
bbox.add (i->p); |
50 |
|
51 |
update (); |
52 |
} |
53 |
|
54 |
template<GLenum type> |
55 |
void geometry_opengl1d<type>::draw (view &ctx) |
56 |
{ |
57 |
glBegin (type); |
58 |
|
59 |
for (iterator i = begin (); i < end (); ++i) |
60 |
glVertex3fv ((GLfloat *)&i->p); |
61 |
|
62 |
glEnd (); |
63 |
} |
64 |
|
65 |
template<GLenum type> |
66 |
void geometry_opengl2d<type>::set (const vector<vertex_t2f_n3f_v3f> &v) |
67 |
{ |
68 |
bbox.reset (); |
69 |
|
70 |
for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.end (); i-- != v.begin (); ) |
71 |
bbox.add (i->p); |
72 |
|
73 |
update (); |
74 |
|
75 |
glNewList (list, GL_COMPILE); |
76 |
|
77 |
m->begin (); |
78 |
|
79 |
#if 0 |
80 |
glBegin (type); |
81 |
|
82 |
for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.begin (); i < v.end (); ++i) |
83 |
{ |
84 |
glTexCoord2fv ((GLfloat *)&i->t); |
85 |
glNormal3fv ((GLfloat *)&i->n); |
86 |
glVertex3fv ((GLfloat *)&i->p); |
87 |
} |
88 |
|
89 |
glEnd (); |
90 |
#else |
91 |
glEnableClientState (GL_VERTEX_ARRAY); |
92 |
glEnableClientState (GL_NORMAL_ARRAY); |
93 |
glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
94 |
glVertexPointer (3, GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->p); |
95 |
glNormalPointer (GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->n); |
96 |
glTexCoordPointer (2, GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->t); |
97 |
|
98 |
glDrawArrays (type, 0, v.size ()); |
99 |
|
100 |
glDisableClientState (GL_VERTEX_ARRAY); |
101 |
glDisableClientState (GL_NORMAL_ARRAY); |
102 |
glDisableClientState (GL_TEXTURE_COORD_ARRAY); |
103 |
#endif |
104 |
|
105 |
m->end (); |
106 |
|
107 |
glEndList (); |
108 |
} |
109 |
|
110 |
template<GLenum type> |
111 |
void geometry_opengl2d<type>::draw (view &ctx) |
112 |
{ |
113 |
glCallList (list); |
114 |
} |
115 |
|
116 |
void geometry_sphere::update () |
117 |
{ |
118 |
bbox.reset (); |
119 |
bbox.add (-point (radius, radius, radius)); |
120 |
bbox.add ( point (radius, radius, radius)); |
121 |
|
122 |
geometry::update (); |
123 |
} |
124 |
|
125 |
void geometry_sphere::draw (view &ctx) |
126 |
{ |
127 |
material *m = new simple_material; |
128 |
|
129 |
int n = max (8, (int)(ctx.pixfact * radius) / 10); |
130 |
|
131 |
m->begin (); |
132 |
GLUquadric *quad = gluNewQuadric (); |
133 |
gluQuadricTexture (quad, true); |
134 |
gluSphere (quad, radius, n, n); |
135 |
gluDeleteQuadric (quad); |
136 |
m->end (); |
137 |
|
138 |
delete m; |
139 |
} |
140 |
|
141 |
///////////////////////////////////////////////////////////////////////////// |
142 |
|
143 |
void geometry_transform::update () |
144 |
{ |
145 |
const box &sub = g->bbox; |
146 |
|
147 |
bbox.reset (); |
148 |
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z)); |
149 |
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z)); |
150 |
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z)); |
151 |
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z)); |
152 |
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z)); |
153 |
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z)); |
154 |
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z)); |
155 |
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z)); |
156 |
|
157 |
geometry::update (); |
158 |
} |
159 |
|
160 |
#if 0 |
161 |
void geometry_transform::renormalize () |
162 |
{ |
163 |
point trans(m(0,3), m(1,3), m(2,3)); |
164 |
::renormalize (e->orig, trans); |
165 |
m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z; |
166 |
} |
167 |
#endif |
168 |
|
169 |
void geometry_transform::update (const matrix &xfrm) |
170 |
{ |
171 |
m = m * xfrm; |
172 |
|
173 |
update (); |
174 |
} |
175 |
|
176 |
void geometry_transform::set_matrix (const matrix &xfrm) |
177 |
{ |
178 |
m = xfrm; |
179 |
|
180 |
update (); |
181 |
} |
182 |
|
183 |
void geometry_transform::draw (view &ctx) |
184 |
{ |
185 |
glPushMatrix (); |
186 |
glMultMatrixf ((GLfloat *)&m); |
187 |
g->draw (ctx); |
188 |
glPopMatrix (); |
189 |
} |
190 |
|
191 |
void geometry_anim::draw (view &ctx) |
192 |
{ |
193 |
matrix save_m = m; |
194 |
|
195 |
update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0)) |
196 |
* matrix::rotation (vy * timer.now, vec3 (0, 1, 0)) |
197 |
* matrix::rotation (vz * timer.now, vec3 (0, 0, 1))); |
198 |
|
199 |
geometry_transform::draw (ctx); |
200 |
|
201 |
m = save_m; |
202 |
} |
203 |
|
204 |
///////////////////////////////////////////////////////////////////////////// |
205 |
|
206 |
void geometry_filter::set (geometry *g) |
207 |
{ |
208 |
this->g = g; |
209 |
|
210 |
if (g) |
211 |
g->parent = this; |
212 |
|
213 |
update (); |
214 |
} |
215 |
|
216 |
void geometry_filter::update () |
217 |
{ |
218 |
if (g) |
219 |
{ |
220 |
bbox = g->bbox; |
221 |
geometry::update (); |
222 |
} |
223 |
} |
224 |
|
225 |
void geometry_filter::draw (view &ctx) |
226 |
{ |
227 |
g->draw (ctx); |
228 |
} |
229 |
|
230 |
geometry_filter::~geometry_filter () |
231 |
{ |
232 |
delete g; |
233 |
} |
234 |
|
235 |
///////////////////////////////////////////////////////////////////////////// |
236 |
|
237 |
void geometry_container::update () |
238 |
{ |
239 |
bbox.reset (); |
240 |
|
241 |
for (iterator i = end (); i-- != begin (); ) |
242 |
bbox.add ((*i)->bbox); |
243 |
|
244 |
geometry::update (); |
245 |
} |
246 |
|
247 |
void geometry_container::add (geometry *g) |
248 |
{ |
249 |
push_back (g); |
250 |
g->parent = this; |
251 |
|
252 |
update (); |
253 |
} |
254 |
|
255 |
void geometry_container::draw (view &ctx) |
256 |
{ |
257 |
for (iterator i = end (); i-- != begin (); ) |
258 |
(*i)->draw (ctx); |
259 |
} |
260 |
|
261 |
geometry_container::~geometry_container () |
262 |
{ |
263 |
for (iterator i = end (); i-- != begin (); ) |
264 |
delete *i; |
265 |
|
266 |
clear (); |
267 |
} |
268 |
|
269 |
///////////////////////////////////////////////////////////////////////////// |
270 |
|
271 |
entity::entity (geometry *g) |
272 |
: geometry_filter(g) |
273 |
, p(0,0,0) |
274 |
{ |
275 |
update (); |
276 |
} |
277 |
|
278 |
entity::~entity () |
279 |
{ |
280 |
hide (); |
281 |
} |
282 |
|
283 |
void entity::move (const vec3 &v) |
284 |
{ |
285 |
p = p + v; |
286 |
|
287 |
//renormalize (orig, p); |
288 |
|
289 |
update (); |
290 |
} |
291 |
|
292 |
void entity::show () |
293 |
{ |
294 |
if (!o.size ()) |
295 |
world.add (this); |
296 |
} |
297 |
|
298 |
void entity::hide () |
299 |
{ |
300 |
for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); ) |
301 |
(*i)->remove (this); |
302 |
|
303 |
o.clear (); |
304 |
} |
305 |
|
306 |
void entity::update () |
307 |
{ |
308 |
if (!g) |
309 |
return; |
310 |
|
311 |
bbox = g->bbox; |
312 |
|
313 |
a.x = orig.x + (soffs)floorf (bbox.a.x + p.x); |
314 |
a.y = orig.y + (soffs)floorf (bbox.a.y + p.y); |
315 |
a.z = orig.z + (soffs)floorf (bbox.a.z + p.z); |
316 |
b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x); |
317 |
b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y); |
318 |
b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z); |
319 |
|
320 |
if (o.size ()) |
321 |
{ |
322 |
hide (); |
323 |
show (); |
324 |
} |
325 |
} |
326 |
|
327 |
void entity::draw (view &ctx) |
328 |
{ |
329 |
sector diff = orig - ctx.orig; |
330 |
|
331 |
glPushMatrix (); |
332 |
glTranslatef (diff.x + p.x, diff.y + p.y, diff.z + p.z); |
333 |
g->draw (ctx); |
334 |
glPopMatrix (); |
335 |
} |
336 |
|
337 |
/////////////////////////////////////////////////////////////////////////// |
338 |
// |
339 |
static void nurbs_error (GLenum errorCode) |
340 |
{ |
341 |
const GLubyte *estring; |
342 |
estring = gluErrorString(errorCode); |
343 |
fprintf (stderr, "Nurbs error: %s\n", estring); |
344 |
} |
345 |
|
346 |
void errorCallback(GLenum errorCode) |
347 |
{ |
348 |
const GLubyte *estring; |
349 |
|
350 |
estring = gluErrorString(errorCode); |
351 |
fprintf (stderr, "Tessellation Error: %s\n", estring); |
352 |
exit (0); |
353 |
} |
354 |
void tcbBegin (GLenum prim) |
355 |
{ |
356 |
glBegin (prim); |
357 |
} |
358 |
void tcbVertex (void *data) |
359 |
{ |
360 |
glVertex3fv ((GLfloat *)data); |
361 |
} |
362 |
void tcbEnd () |
363 |
{ |
364 |
glEnd (); |
365 |
} |
366 |
void geometry_nurbs::set () |
367 |
{ |
368 |
// < XXX >: Testing CODE |
369 |
int u, v; |
370 |
for (u = 0; u < 4; u++) { |
371 |
for (v = 0; v < 4; v++) { |
372 |
ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5); |
373 |
ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5); |
374 |
|
375 |
if ( (u == 1 || u == 2) && (v == 1 || v == 2)) |
376 |
ctlpoints[u][v][2] = 3.0; |
377 |
else |
378 |
ctlpoints[u][v][2] = -3.0; |
379 |
} |
380 |
} |
381 |
tess = gluNewTess(); |
382 |
// </ XXX > |
383 |
|
384 |
glEnable(GL_AUTO_NORMAL); |
385 |
nurb = gluNewNurbsRenderer (); |
386 |
gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE); |
387 |
gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL); |
388 |
gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR); |
389 |
gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH); |
390 |
gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0); |
391 |
gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error); |
392 |
gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin); |
393 |
gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex); |
394 |
gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd); |
395 |
glDisable(GL_AUTO_NORMAL); |
396 |
|
397 |
} |
398 |
|
399 |
void geometry_nurbs::draw (view &ctx) |
400 |
{ |
401 |
GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0}; |
402 |
|
403 |
GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 }; |
404 |
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; |
405 |
GLfloat mat_shininess[] = { 100.0 }; |
406 |
|
407 |
glClearColor (0.0, 0.0, 0.0, 0.0); |
408 |
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); |
409 |
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); |
410 |
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); |
411 |
|
412 |
|
413 |
glEnable(GL_AUTO_NORMAL); |
414 |
gluBeginSurface (nurb); |
415 |
gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3); |
416 |
gluEndSurface (nurb); |
417 |
/* |
418 |
glEnable(GL_AUTO_NORMAL); |
419 |
glPushMatrix(); |
420 |
GL_LG_DEBUG; |
421 |
======= |
422 |
|
423 |
gluEndSurface (nurb); |
424 |
>>>>>>> 1.40 |
425 |
glDisable(GL_AUTO_NORMAL); |
426 |
glPopMatrix(); |
427 |
glFlush(); |
428 |
*/ |
429 |
} |