ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.66
Committed: Thu Jan 6 02:15:17 2005 UTC (19 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.65: +6 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <cassert>
2 #include <cstdlib>
3 #include <algorithm>
4
5 using namespace std;
6
7 #include "opengl.h"
8
9 #include "util.h"
10 #include "entity.h"
11 #include "oct.h"
12 #include "view.h"
13
14 /////////////////////////////////////////////////////////////////////////////
15
16 geometry::~geometry ()
17 {
18 }
19
20 template class geometry_opengl1d<GL_POINTS>;
21 template class geometry_opengl1d<GL_LINES>;
22 template class geometry_opengl1d<GL_LINE_STRIP>;
23 template class geometry_opengl1d<GL_LINE_LOOP>;
24 template class geometry_opengl2d<GL_TRIANGLES>;
25 template class geometry_opengl2d<GL_TRIANGLE_STRIP>;
26 template class geometry_opengl2d<GL_TRIANGLE_FAN>;
27 template class geometry_opengl2d<GL_QUADS>;
28 template class geometry_opengl2d<GL_QUAD_STRIP>;
29 template class geometry_opengl2d<GL_POLYGON>;
30
31 geometry_opengl::geometry_opengl ()
32 {
33 list = glGenLists (1);
34 }
35
36 geometry_opengl::~geometry_opengl ()
37 {
38 glDeleteLists (list, 1);
39 }
40
41 template<GLenum type>
42 void geometry_opengl1d<type>::set (const vector<vertex_v3f> &v)
43 {
44 clear ();
45 insert (end (), v.begin (), v.end ());
46
47 bbox.reset ();
48
49 for (const_iterator i = end (); i-- != begin (); )
50 bbox.add (i->v);
51
52 update ();
53 }
54
55 template<GLenum type>
56 void geometry_opengl1d<type>::draw (view &ctx)
57 {
58 glBegin (type);
59
60 for (iterator i = begin (); i < end (); ++i)
61 glVertex3fv ((GLfloat *)&i->v);
62
63 glEnd ();
64 }
65
66 template<GLenum type>
67 void geometry_opengl2d<type>::set (const vector<vertex_t2f_n3f_v3f> &v)
68 {
69 bbox.reset ();
70
71 for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.end (); i-- != v.begin (); )
72 bbox.add (i->v);
73
74 update ();
75
76 glNewList (list, GL_COMPILE);
77
78 #if 0
79 glBegin (type);
80
81 for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.begin (); i < v.end (); ++i)
82 {
83 glTexCoord2fv ((GLfloat *)&i->t);
84 glNormal3fv ((GLfloat *)&i->n);
85 glVertex3fv ((GLfloat *)&i->v);
86 }
87
88 glEnd ();
89 #else
90 glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0);
91
92 glEnableClientState (GL_VERTEX_ARRAY);
93 glEnableClientState (GL_NORMAL_ARRAY);
94 glEnableClientState (GL_TEXTURE_COORD_ARRAY);
95 glVertexPointer (3, GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->v);
96 glNormalPointer (GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->n);
97 glTexCoordPointer (2, GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->t);
98
99 glDrawArrays (type, 0, v.size ());
100
101 glDisableClientState (GL_VERTEX_ARRAY);
102 glDisableClientState (GL_NORMAL_ARRAY);
103 glDisableClientState (GL_TEXTURE_COORD_ARRAY);
104 #endif
105
106 glEndList ();
107 }
108
109 template<GLenum type>
110 void geometry_opengl2d<type>::draw (view &ctx)
111 {
112 m->enable (ctx);
113 glCallList (list);
114 m->disable (ctx);
115 }
116
117 void geometry_sphere::update ()
118 {
119 bbox.reset ();
120 bbox.add (-point (radius, radius, radius));
121 bbox.add ( point (radius, radius, radius));
122
123 geometry::update ();
124 }
125
126 void geometry_sphere::draw (view &ctx)
127 {
128 int n = min (100, max (15, (int)(ctx.pixfact * radius) / 10));
129
130 m->enable (ctx);
131 GLUquadric *quad = gluNewQuadric ();
132 gluQuadricTexture (quad, true);
133 gluSphere (quad, radius, n, n);
134 gluDeleteQuadric (quad);
135 m->disable (ctx);
136 }
137
138 /////////////////////////////////////////////////////////////////////////////
139
140 void geometry_indexed_2d::draw (view &ctx)
141 {
142 m->enable (ctx);
143 vb.bind ();
144 ib.draw (type, 0, ib.count);
145 m->disable (ctx);
146 }
147
148 /////////////////////////////////////////////////////////////////////////////
149
150 void geometry_transform::update ()
151 {
152 const box &sub = g->bbox;
153
154 bbox.reset ();
155 bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z));
156 bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z));
157 bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z));
158 bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z));
159 bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z));
160 bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z));
161 bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z));
162 bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z));
163
164 geometry::update ();
165 }
166
167 #if 0
168 void geometry_transform::renormalize ()
169 {
170 point trans(m(0,3), m(1,3), m(2,3));
171 ::renormalize (e->orig, trans);
172 m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z;
173 }
174 #endif
175
176 void geometry_transform::update (const matrix &xfrm)
177 {
178 m = m * xfrm;
179
180 update ();
181 }
182
183 void geometry_transform::set_matrix (const matrix &xfrm)
184 {
185 m = xfrm;
186
187 update ();
188 }
189
190 void geometry_transform::draw (view &ctx)
191 {
192 glPushMatrix ();
193 glMultMatrixf ((GLfloat *)&m);
194 g->draw (ctx);
195 glPopMatrix ();
196 }
197
198 void geometry_anim::draw (view &ctx)
199 {
200 matrix save_m = m;
201
202 update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0))
203 * matrix::rotation (vy * timer.now, vec3 (0, 1, 0))
204 * matrix::rotation (vz * timer.now, vec3 (0, 0, 1)));
205
206 geometry_transform::draw (ctx);
207
208 m = save_m;
209 }
210
211 /////////////////////////////////////////////////////////////////////////////
212
213 void geometry_filter::set (geometry *g)
214 {
215 this->g = g;
216
217 if (g)
218 g->parent = this;
219
220 update ();
221 }
222
223 void geometry_filter::update ()
224 {
225 if (g)
226 {
227 bbox = g->bbox;
228 geometry::update ();
229 }
230 }
231
232 void geometry_filter::draw (view &ctx)
233 {
234 g->draw (ctx);
235 }
236
237 geometry_filter::~geometry_filter ()
238 {
239 delete g;
240 }
241
242 /////////////////////////////////////////////////////////////////////////////
243
244 void geometry_container::update ()
245 {
246 bbox.reset ();
247
248 for (iterator i = end (); i-- != begin (); )
249 bbox.add ((*i)->bbox);
250
251 geometry::update ();
252 }
253
254 void geometry_container::add (geometry *g)
255 {
256 push_back (g);
257 g->parent = this;
258
259 update ();
260 }
261
262 void geometry_container::draw (view &ctx)
263 {
264 for (iterator i = end (); i-- != begin (); )
265 (*i)->draw (ctx);
266 }
267
268 geometry_container::~geometry_container ()
269 {
270 for (iterator i = end (); i-- != begin (); )
271 delete *i;
272
273 clear ();
274 }
275
276 ///////////////////////////////////////////////////////////////////////////
277
278 static void nurbs_error (GLenum errorCode)
279 {
280 const GLubyte *estring;
281 estring = gluErrorString(errorCode);
282 fprintf (stderr, "Nurbs error: %s\n", estring);
283 }
284
285 void errorCallback(GLenum errorCode)
286 {
287 const GLubyte *estring;
288
289 estring = gluErrorString(errorCode);
290 fprintf (stderr, "Tessellation Error: %s\n", estring);
291 exit (0);
292 }
293
294 void tcbBegin (GLenum prim)
295 {
296 glBegin (prim);
297 }
298
299 void tcbVertex (void *data)
300 {
301 glVertex3fv ((GLfloat *)data);
302 }
303
304 void tcbEnd ()
305 {
306 glEnd ();
307 }
308
309 void geometry_nurbs::set ()
310 {
311 // < XXX >: Testing CODE
312 int u, v;
313 for (u = 0; u < 4; u++) {
314 for (v = 0; v < 4; v++) {
315 ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
316 ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
317
318 if ( (u == 1 || u == 2) && (v == 1 || v == 2))
319 ctlpoints[u][v][2] = 3.0;
320 else
321 ctlpoints[u][v][2] = -3.0;
322 }
323 }
324 tess = gluNewTess();
325 // </ XXX >
326
327 glEnable(GL_AUTO_NORMAL);
328 nurb = gluNewNurbsRenderer ();
329 gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
330 gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL);
331 gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
332 gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH);
333 gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0);
334 gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error);
335 gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin);
336 gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex);
337 gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd);
338 glDisable(GL_AUTO_NORMAL);
339
340 }
341
342 void geometry_nurbs::draw (view &ctx)
343 {
344 GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
345
346 GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
347 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
348 GLfloat mat_shininess[] = { 100.0 };
349
350 glClearColor (0.0, 0.0, 0.0, 0.0);
351 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
352 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
353 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
354
355
356 glEnable(GL_AUTO_NORMAL);
357 gluBeginSurface (nurb);
358 gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3);
359 gluEndSurface (nurb);
360 /*
361 glEnable(GL_AUTO_NORMAL);
362 glPushMatrix();
363 GL_LG_DEBUG;
364 =======
365
366 gluEndSurface (nurb);
367 >>>>>>> 1.40
368 glDisable(GL_AUTO_NORMAL);
369 glPopMatrix();
370 glFlush();
371 */
372 }
373
374 /////////////////////////////////////////////////////////////////////////////
375
376 entity::entity (geometry *g)
377 : geometry_filter(g)
378 , p(0,0,0)
379 {
380 update ();
381 }
382
383 entity::~entity ()
384 {
385 hide ();
386 }
387
388 void entity::move (const vec3 &v)
389 {
390 p = p + v;
391
392 renormalize (orig, p);
393
394 update ();
395 }
396
397 void entity::show ()
398 {
399 if (!o.size ())
400 world.add (this);
401 }
402
403 void entity::hide ()
404 {
405 for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
406 (*i)->remove (this);
407
408 o.clear ();
409 }
410
411 void entity::update ()
412 {
413 if (!g)
414 return;
415
416 bbox = g->bbox;
417
418 a.x = orig.x + (soffs)floorf (bbox.a.x + p.x);
419 a.y = orig.y + (soffs)floorf (bbox.a.y + p.y);
420 a.z = orig.z + (soffs)floorf (bbox.a.z + p.z);
421 b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x);
422 b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y);
423 b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z);
424
425 if (o.size ())
426 {
427 hide ();
428 show ();
429 }
430 }
431
432 void entity::draw (view &ctx)
433 {
434 ctx.eorig = orig - ctx.orig;
435
436 glPushMatrix ();
437 glTranslatef (p.x + ctx.eorig.x, p.y + ctx.eorig.y, p.z + ctx.eorig.z);
438 g->draw (ctx);
439 glPopMatrix ();
440 }
441
442 /////////////////////////////////////////////////////////////////////////////
443 //
444
445 #define SIZE 33
446
447 struct geometry_heightfield::node
448 {
449 vertex_t2f_n3f_v3f vd[SIZE][SIZE];
450 gl::vertex_buffer vb;
451 GLushort *ibp;
452 GLfloat hvar;
453 GLfloat x, y, z, e;
454
455 // 23
456 // 01
457 struct geometry_heightfield::node *sub[4];
458
459 void update_normals ();
460 void draw (view &ctx);
461
462 node ();
463 };
464
465 geometry_heightfield::node::node ()
466 {
467 sub[0] = sub[1] = sub[2] = sub[3] = 0;
468 //vb.set (vd);
469 }
470
471 static index_buffer ib;
472 static GLushort *ibp;
473 static int icnt;
474
475 geometry_heightfield::geometry_heightfield (GLfloat sx, GLfloat sy)
476 : sx(sx), sy(sy)
477 , sm (max (sx, sy))
478 {
479 if (!ib)
480 {
481 ib.set<GLushort> (SIZE * SIZE * 6, GL_STATIC_DRAW);
482 ibp = (GLushort *)ib.map ();
483
484 GLushort *p = ibp;
485
486 for (int x = 0; x < SIZE - 1; x++)
487 for (int y = 0; y < SIZE - 1; y++)
488 {
489 unsigned short a = (y + 0) * SIZE + (x + 0);
490 unsigned short b = (y + 0) * SIZE + (x + 1);
491 unsigned short c = (y + 1) * SIZE + (x + 1);
492 unsigned short d = (y + 1) * SIZE + (x + 0);
493
494 *p++ = c; *p++ = b; *p++ = a;
495 *p++ = d; *p++ = c; *p++ = a;
496 }
497
498 icnt = p - ibp;
499
500 ib.unmap ();
501 }
502
503 update ();
504
505 tree = new node;
506 tree->hvar = 0.01F * sx;
507 tree->x = tree->y = 0.F;
508 tree->e = sm;
509
510 for (int x = 0; x < SIZE; x++)
511 for (int y = 0; y < SIZE; y++)
512 {
513 vertex_t2f_n3f_v3f &v = tree->vd[y][x];
514
515 v.t = tex2 ((GLfloat)x / (SIZE - 1), (GLfloat)y / (SIZE - 1));
516 v.v = vec3 (sx * x / (SIZE - 1), tree->hvar * rand () / RAND_MAX, sy * y / (SIZE - 1));
517 v.n = vec3 (0, 1, 0);
518 }
519
520 tree->update_normals ();
521
522 tree->vb.set (&tree->vd[0][0], SIZE * SIZE);
523
524 bbox.reset ();
525 bbox.add (vec3 (0, 0, 0));
526 bbox.add (vec3 (sx, tree->hvar, sy));
527 }
528
529 void geometry_heightfield::update ()
530 {
531 geometry::update ();
532 }
533
534 static int maxcnt, thiscnt;
535 static GLfloat mine;
536
537 void geometry_heightfield::draw (view &ctx)
538 {
539 testmat->enable (ctx);
540 maxcnt = 10;
541 thiscnt = 0;
542 mine = 1e38;
543 tree->draw (ctx);
544 testmat->disable (ctx);
545 //printf (" TC %d mine %f\n", thiscnt, mine);
546 }
547
548 void geometry_heightfield::node::update_normals ()
549 {
550 GLfloat az = 0.F;
551
552 for (int x = 0; x < SIZE; x++)
553 for (int y = 0; y < SIZE; y++)
554 {
555 vertex_t2f_n3f_v3f &v = vd[y][x];
556
557 if (x && y)
558 v.n = normalize (cross (vd[y-1][x].v - v.v, vd[y][x-1].v - v.v));
559
560 az += v.v.y;
561 }
562
563 z = az / (SIZE * SIZE);
564 }
565
566 void geometry_heightfield::node::draw (view &ctx)
567 {
568 vec3 center = vec3 (ctx.eorig) + vec3 (x + e * .5F, z, y + e * .5F);
569 GLfloat d = length (center);
570
571 if (!overlap (ctx.frustum.c, sphere (center, e * 2)))
572 return;
573
574 if (d > e * 2.F)
575 {
576 if (mine > e) mine = e; thiscnt++;
577 vb.bind ();
578 ib.draw (GL_TRIANGLES, 0, icnt);
579 }
580 else
581 {
582 for (int i = 0; i < 4; i++)
583 {
584 if (!sub[i])
585 {
586 if (!--maxcnt)
587 {
588 vb.bind ();
589 ib.draw (GL_TRIANGLES, 0, icnt);
590 }
591
592 node *n = sub[i] = new node;
593
594 n->hvar = hvar * 0.5;
595 n->x = x + (i & 1 ? e * .5F : 0.F);
596 n->y = y + (i & 2 ? e * .5F : 0.F);
597 n->e = e * .5F;
598
599 for (int x = 0; x < SIZE; x++)
600 for (int y = 0; y < SIZE; y++)
601 n->vd[y][x] = vd[(y >> 1) + (i & 2 ? SIZE / 2 : 0)][(x >> 1) + (i & 1 ? SIZE / 2 : 0)];
602
603 for (int x = 1; x < SIZE; x += 2)
604 for (int y = 0; y < SIZE; y += 2)
605 {
606 n->vd[y][x].t = (n->vd[y][x-1].t + n->vd[y][x+1].t) * 0.5F;
607 n->vd[y][x].v = (n->vd[y][x-1].v + n->vd[y][x+1].v) * 0.5F;
608 if (y && y < SIZE - 1)
609 n->vd[y][x].v.y += n->hvar * (GLfloat)rand () / RAND_MAX - n->hvar * .5F;
610 }
611
612 for (int x = 0; x < SIZE; x++)
613 for (int y = 1; y < SIZE; y += 2)
614 {
615 n->vd[y][x].t = (n->vd[y-1][x].t + n->vd[y+1][x].t) * 0.5F;
616 n->vd[y][x].v = (n->vd[y-1][x].v + n->vd[y+1][x].v) * 0.5F;
617 if (x && x < SIZE - 1)
618 n->vd[y][x].v.y += n->hvar * (GLfloat)rand () / RAND_MAX - n->hvar * .5F;
619 }
620
621 n->update_normals ();
622
623 n->vb.set (&n->vd[0][0], SIZE * SIZE);
624 }
625
626 sub[i]->draw (ctx);
627 }
628 }
629 }
630
631 void
632 entity_moveable::perform_step (double t)
633 {
634 vec3 vel = t * v;
635 move (vel);
636 }