ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/entity.C
Revision: 1.61
Committed: Thu Nov 4 04:46:58 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.60: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

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