ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.53
Committed: Fri Oct 29 23:19:08 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.52: +10 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 #if 0
78 glBegin (type);
79
80 for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.begin (); i < v.end (); ++i)
81 {
82 glTexCoord2fv ((GLfloat *)&i->t);
83 glNormal3fv ((GLfloat *)&i->n);
84 glVertex3fv ((GLfloat *)&i->p);
85 }
86
87 glEnd ();
88 #else
89 glEnableClientState (GL_VERTEX_ARRAY);
90 glEnableClientState (GL_NORMAL_ARRAY);
91 glEnableClientState (GL_TEXTURE_COORD_ARRAY);
92 glVertexPointer (3, GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->p);
93 glNormalPointer (GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->n);
94 glTexCoordPointer (2, GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->t);
95
96 glDrawArrays (type, 0, v.size ());
97
98 glDisableClientState (GL_VERTEX_ARRAY);
99 glDisableClientState (GL_NORMAL_ARRAY);
100 glDisableClientState (GL_TEXTURE_COORD_ARRAY);
101 #endif
102
103 glEndList ();
104 }
105
106 template<GLenum type>
107 void geometry_opengl2d<type>::draw (view &ctx)
108 {
109 m->enable (ctx);
110 glCallList (list);
111 m->disable (ctx);
112 }
113
114 void geometry_sphere::update ()
115 {
116 bbox.reset ();
117 bbox.add (-point (radius, radius, radius));
118 bbox.add ( point (radius, radius, radius));
119
120 geometry::update ();
121 }
122
123 void geometry_sphere::draw (view &ctx)
124 {
125 int n = min (100, max (15, (int)(ctx.pixfact * radius) / 10));
126
127 GLUquadric *quad = gluNewQuadric ();
128 gluQuadricTexture (quad, true);
129 gluSphere (quad, radius, n, n);
130 gluDeleteQuadric (quad);
131 }
132
133 /////////////////////////////////////////////////////////////////////////////
134
135 void geometry_transform::update ()
136 {
137 const box &sub = g->bbox;
138
139 bbox.reset ();
140 bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z));
141 bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z));
142 bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z));
143 bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z));
144 bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z));
145 bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z));
146 bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z));
147 bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z));
148
149 geometry::update ();
150 }
151
152 #if 0
153 void geometry_transform::renormalize ()
154 {
155 point trans(m(0,3), m(1,3), m(2,3));
156 ::renormalize (e->orig, trans);
157 m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z;
158 }
159 #endif
160
161 void geometry_transform::update (const matrix &xfrm)
162 {
163 m = m * xfrm;
164
165 update ();
166 }
167
168 void geometry_transform::set_matrix (const matrix &xfrm)
169 {
170 m = xfrm;
171
172 update ();
173 }
174
175 void geometry_transform::draw (view &ctx)
176 {
177 glPushMatrix ();
178 glMultMatrixf ((GLfloat *)&m);
179 g->draw (ctx);
180 glPopMatrix ();
181 }
182
183 void geometry_anim::draw (view &ctx)
184 {
185 matrix save_m = m;
186
187 update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0))
188 * matrix::rotation (vy * timer.now, vec3 (0, 1, 0))
189 * matrix::rotation (vz * timer.now, vec3 (0, 0, 1)));
190
191 geometry_transform::draw (ctx);
192
193 m = save_m;
194 }
195
196 /////////////////////////////////////////////////////////////////////////////
197
198 void geometry_filter::set (geometry *g)
199 {
200 this->g = g;
201
202 if (g)
203 g->parent = this;
204
205 update ();
206 }
207
208 void geometry_filter::update ()
209 {
210 if (g)
211 {
212 bbox = g->bbox;
213 geometry::update ();
214 }
215 }
216
217 void geometry_filter::draw (view &ctx)
218 {
219 g->draw (ctx);
220 }
221
222 geometry_filter::~geometry_filter ()
223 {
224 delete g;
225 }
226
227 /////////////////////////////////////////////////////////////////////////////
228
229 void geometry_container::update ()
230 {
231 bbox.reset ();
232
233 for (iterator i = end (); i-- != begin (); )
234 bbox.add ((*i)->bbox);
235
236 geometry::update ();
237 }
238
239 void geometry_container::add (geometry *g)
240 {
241 push_back (g);
242 g->parent = this;
243
244 update ();
245 }
246
247 void geometry_container::draw (view &ctx)
248 {
249 for (iterator i = end (); i-- != begin (); )
250 (*i)->draw (ctx);
251 }
252
253 geometry_container::~geometry_container ()
254 {
255 for (iterator i = end (); i-- != begin (); )
256 delete *i;
257
258 clear ();
259 }
260
261 ///////////////////////////////////////////////////////////////////////////
262
263 static void nurbs_error (GLenum errorCode)
264 {
265 const GLubyte *estring;
266 estring = gluErrorString(errorCode);
267 fprintf (stderr, "Nurbs error: %s\n", estring);
268 }
269
270 void errorCallback(GLenum errorCode)
271 {
272 const GLubyte *estring;
273
274 estring = gluErrorString(errorCode);
275 fprintf (stderr, "Tessellation Error: %s\n", estring);
276 exit (0);
277 }
278 void tcbBegin (GLenum prim)
279 {
280 glBegin (prim);
281 }
282 void tcbVertex (void *data)
283 {
284 glVertex3fv ((GLfloat *)data);
285 }
286 void tcbEnd ()
287 {
288 glEnd ();
289 }
290 void geometry_nurbs::set ()
291 {
292 // < XXX >: Testing CODE
293 int u, v;
294 for (u = 0; u < 4; u++) {
295 for (v = 0; v < 4; v++) {
296 ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
297 ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
298
299 if ( (u == 1 || u == 2) && (v == 1 || v == 2))
300 ctlpoints[u][v][2] = 3.0;
301 else
302 ctlpoints[u][v][2] = -3.0;
303 }
304 }
305 tess = gluNewTess();
306 // </ XXX >
307
308 glEnable(GL_AUTO_NORMAL);
309 nurb = gluNewNurbsRenderer ();
310 gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
311 gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL);
312 gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
313 gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH);
314 gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0);
315 gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error);
316 gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin);
317 gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex);
318 gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd);
319 glDisable(GL_AUTO_NORMAL);
320
321 }
322
323 void geometry_nurbs::draw (view &ctx)
324 {
325 GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
326
327 GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
328 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
329 GLfloat mat_shininess[] = { 100.0 };
330
331 glClearColor (0.0, 0.0, 0.0, 0.0);
332 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
333 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
334 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
335
336
337 glEnable(GL_AUTO_NORMAL);
338 gluBeginSurface (nurb);
339 gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3);
340 gluEndSurface (nurb);
341 /*
342 glEnable(GL_AUTO_NORMAL);
343 glPushMatrix();
344 GL_LG_DEBUG;
345 =======
346
347 gluEndSurface (nurb);
348 >>>>>>> 1.40
349 glDisable(GL_AUTO_NORMAL);
350 glPopMatrix();
351 glFlush();
352 */
353 }
354
355 /////////////////////////////////////////////////////////////////////////////
356
357 entity::entity (geometry *g)
358 : geometry_filter(g)
359 , p(0,0,0)
360 {
361 update ();
362 }
363
364 entity::~entity ()
365 {
366 hide ();
367 }
368
369 void entity::move (const vec3 &v)
370 {
371 p = p + v;
372
373 renormalize (orig, p);
374
375 update ();
376 }
377
378 void entity::show ()
379 {
380 if (!o.size ())
381 world.add (this);
382 }
383
384 void entity::hide ()
385 {
386 for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
387 (*i)->remove (this);
388
389 o.clear ();
390 }
391
392 void entity::update ()
393 {
394 if (!g)
395 return;
396
397 bbox = g->bbox;
398
399 a.x = orig.x + (soffs)floorf (bbox.a.x + p.x);
400 a.y = orig.y + (soffs)floorf (bbox.a.y + p.y);
401 a.z = orig.z + (soffs)floorf (bbox.a.z + p.z);
402 b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x);
403 b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y);
404 b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z);
405
406 if (o.size ())
407 {
408 hide ();
409 show ();
410 }
411 }
412
413 void entity::draw (view &ctx)
414 {
415 sector diff = orig - ctx.orig;
416
417 glPushMatrix ();
418 glTranslatef (diff.x + p.x, diff.y + p.y, diff.z + p.z);
419 g->draw (ctx);
420 glPopMatrix ();
421 }
422
423 /////////////////////////////////////////////////////////////////////////////
424 //
425
426 #define SIZE 33
427
428 struct geometry_heightfield::node
429 {
430 gl::vertex_buffer vb;
431 gl::index_buffer ib;
432 GLushort *ibp;
433
434 node (vertex_t2f_n3f_v3f v[SIZE * SIZE]);
435 };
436
437 geometry_heightfield::node::node (vertex_t2f_n3f_v3f v[SIZE * SIZE])
438 {
439 vb.set (v, SIZE * SIZE);
440 ib.set (SIZE * SIZE * 4);
441 ibp = (GLushort *)ib.map ();
442 }
443
444 geometry_heightfield::geometry_heightfield ()
445 {
446 update ();
447 }
448
449 void geometry_heightfield::update ()
450 {
451 bbox.a = sector (0, 0, 0);
452 bbox.b = sector (1, 1, 1);
453
454 geometry::update ();
455 }
456
457 void geometry_heightfield::draw (view &ctx)
458 {
459 glBegin (GL_QUADS);
460 glVertex3f (0.F, 0.F, 0.F);
461 glVertex3f (0.F, 0.F, 1.F);
462 glVertex3f (1.F, 0.F, 1.F);
463 glVertex3f (1.F, 0.F, 0.F);
464 glEnd ();
465 }
466