ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.63
Committed: Sat Nov 6 00:44:50 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.62: +10 -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.63 void geometry_indexed_2d::draw (view &ctx)
139     {
140     m->enable (ctx);
141     vb.bind ();
142     ib.draw (type, 0, ib.count);
143     m->disable (ctx);
144     }
145    
146     /////////////////////////////////////////////////////////////////////////////
147    
148 root 1.38 void geometry_transform::update ()
149 root 1.22 {
150 root 1.38 const box &sub = g->bbox;
151 root 1.27
152     bbox.reset ();
153     bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z));
154     bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z));
155     bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z));
156     bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z));
157     bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z));
158     bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z));
159     bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z));
160     bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z));
161 root 1.22
162 root 1.38 geometry::update ();
163 root 1.28 }
164    
165 root 1.38 #if 0
166     void geometry_transform::renormalize ()
167 root 1.28 {
168 root 1.34 point trans(m(0,3), m(1,3), m(2,3));
169     ::renormalize (e->orig, trans);
170     m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z;
171 root 1.38 }
172 root 1.28 #endif
173    
174 root 1.38 void geometry_transform::update (const matrix &xfrm)
175 root 1.28 {
176     m = m * xfrm;
177    
178     update ();
179     }
180    
181 root 1.38 void geometry_transform::set_matrix (const matrix &xfrm)
182 root 1.28 {
183     m = xfrm;
184    
185     update ();
186 root 1.22 }
187    
188 root 1.38 void geometry_transform::draw (view &ctx)
189 root 1.22 {
190 root 1.33 glPushMatrix ();
191     glMultMatrixf ((GLfloat *)&m);
192 root 1.38 g->draw (ctx);
193 root 1.33 glPopMatrix ();
194 root 1.2 }
195    
196 root 1.38 void geometry_anim::draw (view &ctx)
197 root 1.23 {
198 root 1.30 matrix save_m = m;
199 root 1.23
200 root 1.30 update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0))
201     * matrix::rotation (vy * timer.now, vec3 (0, 1, 0))
202     * matrix::rotation (vz * timer.now, vec3 (0, 0, 1)));
203 root 1.28
204 root 1.38 geometry_transform::draw (ctx);
205 root 1.23
206     m = save_m;
207 root 1.38 }
208    
209     /////////////////////////////////////////////////////////////////////////////
210    
211     void geometry_filter::set (geometry *g)
212     {
213     this->g = g;
214    
215     if (g)
216     g->parent = this;
217    
218     update ();
219     }
220    
221     void geometry_filter::update ()
222     {
223     if (g)
224     {
225     bbox = g->bbox;
226     geometry::update ();
227     }
228     }
229    
230     void geometry_filter::draw (view &ctx)
231     {
232     g->draw (ctx);
233     }
234    
235     geometry_filter::~geometry_filter ()
236     {
237     delete g;
238     }
239    
240     /////////////////////////////////////////////////////////////////////////////
241    
242     void geometry_container::update ()
243     {
244     bbox.reset ();
245    
246     for (iterator i = end (); i-- != begin (); )
247     bbox.add ((*i)->bbox);
248    
249     geometry::update ();
250     }
251    
252     void geometry_container::add (geometry *g)
253     {
254     push_back (g);
255     g->parent = this;
256    
257     update ();
258     }
259    
260     void geometry_container::draw (view &ctx)
261     {
262     for (iterator i = end (); i-- != begin (); )
263     (*i)->draw (ctx);
264     }
265    
266     geometry_container::~geometry_container ()
267     {
268     for (iterator i = end (); i-- != begin (); )
269     delete *i;
270    
271     clear ();
272     }
273    
274 root 1.52 ///////////////////////////////////////////////////////////////////////////
275 root 1.38
276 root 1.39 static void nurbs_error (GLenum errorCode)
277     {
278     const GLubyte *estring;
279     estring = gluErrorString(errorCode);
280     fprintf (stderr, "Nurbs error: %s\n", estring);
281     }
282 root 1.40
283 root 1.39 void errorCallback(GLenum errorCode)
284     {
285     const GLubyte *estring;
286    
287     estring = gluErrorString(errorCode);
288     fprintf (stderr, "Tessellation Error: %s\n", estring);
289     exit (0);
290     }
291 root 1.54
292 root 1.41 void tcbBegin (GLenum prim)
293     {
294     glBegin (prim);
295     }
296 root 1.54
297 root 1.41 void tcbVertex (void *data)
298     {
299     glVertex3fv ((GLfloat *)data);
300     }
301 root 1.54
302 root 1.41 void tcbEnd ()
303     {
304     glEnd ();
305     }
306 root 1.54
307 root 1.39 void geometry_nurbs::set ()
308     {
309     // < XXX >: Testing CODE
310     int u, v;
311     for (u = 0; u < 4; u++) {
312     for (v = 0; v < 4; v++) {
313     ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
314     ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
315    
316     if ( (u == 1 || u == 2) && (v == 1 || v == 2))
317     ctlpoints[u][v][2] = 3.0;
318     else
319     ctlpoints[u][v][2] = -3.0;
320     }
321     }
322 root 1.41 tess = gluNewTess();
323 root 1.39 // </ XXX >
324 root 1.1
325 root 1.39 glEnable(GL_AUTO_NORMAL);
326     nurb = gluNewNurbsRenderer ();
327 root 1.41 gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
328     gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL);
329     gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
330     gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH);
331     gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0);
332 root 1.39 gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error);
333 root 1.41 gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin);
334     gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex);
335     gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd);
336 root 1.39 glDisable(GL_AUTO_NORMAL);
337 root 1.41
338 root 1.39 }
339 root 1.40
340 root 1.39 void geometry_nurbs::draw (view &ctx)
341     {
342     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
343    
344     GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
345     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
346     GLfloat mat_shininess[] = { 100.0 };
347    
348     glClearColor (0.0, 0.0, 0.0, 0.0);
349     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
350     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
351     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
352    
353    
354     glEnable(GL_AUTO_NORMAL);
355     gluBeginSurface (nurb);
356     gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3);
357 root 1.41 gluEndSurface (nurb);
358     /*
359     glEnable(GL_AUTO_NORMAL);
360     glPushMatrix();
361     GL_LG_DEBUG;
362     =======
363 root 1.39
364 root 1.40 gluEndSurface (nurb);
365 root 1.41 >>>>>>> 1.40
366 root 1.39 glDisable(GL_AUTO_NORMAL);
367 root 1.41 glPopMatrix();
368     glFlush();
369     */
370 root 1.39 }
371 root 1.52
372     /////////////////////////////////////////////////////////////////////////////
373    
374     entity::entity (geometry *g)
375     : geometry_filter(g)
376     , p(0,0,0)
377     {
378     update ();
379     }
380    
381     entity::~entity ()
382     {
383     hide ();
384     }
385    
386     void entity::move (const vec3 &v)
387     {
388     p = p + v;
389    
390     renormalize (orig, p);
391    
392     update ();
393     }
394    
395     void entity::show ()
396     {
397     if (!o.size ())
398     world.add (this);
399     }
400    
401     void entity::hide ()
402     {
403     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
404     (*i)->remove (this);
405    
406     o.clear ();
407     }
408    
409     void entity::update ()
410     {
411     if (!g)
412     return;
413    
414     bbox = g->bbox;
415    
416     a.x = orig.x + (soffs)floorf (bbox.a.x + p.x);
417     a.y = orig.y + (soffs)floorf (bbox.a.y + p.y);
418     a.z = orig.z + (soffs)floorf (bbox.a.z + p.z);
419     b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x);
420     b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y);
421     b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z);
422    
423     if (o.size ())
424     {
425     hide ();
426     show ();
427     }
428     }
429    
430     void entity::draw (view &ctx)
431     {
432 root 1.57 ctx.eorig = orig - ctx.orig;
433 root 1.52
434     glPushMatrix ();
435 root 1.57 glTranslatef (p.x + ctx.eorig.x, p.y + ctx.eorig.y, p.z + ctx.eorig.z);
436 root 1.52 g->draw (ctx);
437     glPopMatrix ();
438     }
439    
440     /////////////////////////////////////////////////////////////////////////////
441     //
442    
443     #define SIZE 33
444    
445     struct geometry_heightfield::node
446     {
447 root 1.54 vertex_t2f_n3f_v3f vd[SIZE][SIZE];
448 root 1.52 gl::vertex_buffer vb;
449 root 1.53 GLushort *ibp;
450 root 1.54 GLfloat hvar;
451 root 1.58 GLfloat x, y, z, e;
452 root 1.53
453 root 1.54 // 23
454     // 01
455     struct geometry_heightfield::node *sub[4];
456    
457 root 1.56 void update_normals ();
458     void draw (view &ctx);
459    
460 root 1.54 node ();
461 root 1.52 };
462 root 1.53
463 root 1.54 geometry_heightfield::node::node ()
464 root 1.53 {
465 root 1.54 sub[0] = sub[1] = sub[2] = sub[3] = 0;
466     //vb.set (vd);
467 root 1.53 }
468 root 1.52
469 root 1.54 static index_buffer ib;
470     static GLushort *ibp;
471     static int icnt;
472    
473     geometry_heightfield::geometry_heightfield (GLfloat sx, GLfloat sy)
474 root 1.55 : sx(sx), sy(sy)
475     , sm (max (sx, sy))
476 root 1.52 {
477 root 1.54 if (!ib)
478     {
479 root 1.58 ib.set<GLushort> (SIZE * SIZE * 6, GL_STATIC_DRAW);
480 root 1.54 ibp = (GLushort *)ib.map ();
481    
482     GLushort *p = ibp;
483    
484     for (int x = 0; x < SIZE - 1; x++)
485     for (int y = 0; y < SIZE - 1; y++)
486     {
487     unsigned short a = (y + 0) * SIZE + (x + 0);
488     unsigned short b = (y + 0) * SIZE + (x + 1);
489     unsigned short c = (y + 1) * SIZE + (x + 1);
490     unsigned short d = (y + 1) * SIZE + (x + 0);
491    
492     *p++ = c; *p++ = b; *p++ = a;
493     *p++ = d; *p++ = c; *p++ = a;
494     }
495    
496     icnt = p - ibp;
497    
498     ib.unmap ();
499     }
500    
501 root 1.52 update ();
502 root 1.54
503     tree = new node;
504     tree->hvar = 0.01F * sx;
505     tree->x = tree->y = 0.F;
506 root 1.56 tree->e = sm;
507 root 1.54
508     for (int x = 0; x < SIZE; x++)
509     for (int y = 0; y < SIZE; y++)
510     {
511     vertex_t2f_n3f_v3f &v = tree->vd[y][x];
512    
513     v.t = tex2 ((GLfloat)x / (SIZE - 1), (GLfloat)y / (SIZE - 1));
514     v.v = vec3 (sx * x / (SIZE - 1), tree->hvar * rand () / RAND_MAX, sy * y / (SIZE - 1));
515 root 1.58 v.n = vec3 (0, 1, 0);
516 root 1.54 }
517    
518 root 1.56 tree->update_normals ();
519    
520 root 1.54 tree->vb.set (&tree->vd[0][0], SIZE * SIZE);
521    
522     bbox.reset ();
523     bbox.add (vec3 (0, 0, 0));
524     bbox.add (vec3 (sx, tree->hvar, sy));
525 root 1.52 }
526    
527     void geometry_heightfield::update ()
528     {
529     geometry::update ();
530     }
531    
532 root 1.58 static int maxcnt, thiscnt;
533     static GLfloat mine;
534 root 1.57
535 root 1.52 void geometry_heightfield::draw (view &ctx)
536     {
537 root 1.62 testmat->enable (ctx);
538 root 1.57 maxcnt = 10;
539 root 1.58 thiscnt = 0;
540     mine = 1e38;
541 root 1.56 tree->draw (ctx);
542 root 1.62 testmat->disable (ctx);
543 root 1.59 //printf (" TC %d mine %f\n", thiscnt, mine);
544 root 1.56 }
545    
546     void geometry_heightfield::node::update_normals ()
547     {
548 root 1.58 GLfloat az = 0.F;
549    
550 root 1.56 for (int x = 0; x < SIZE; x++)
551     for (int y = 0; y < SIZE; y++)
552     {
553     vertex_t2f_n3f_v3f &v = vd[y][x];
554    
555     if (x && y)
556     v.n = normalize (cross (vd[y-1][x].v - v.v, vd[y][x-1].v - v.v));
557 root 1.58
558     az += v.v.y;
559 root 1.56 }
560 root 1.58
561     z = az / (SIZE * SIZE);
562 root 1.56 }
563    
564     void geometry_heightfield::node::draw (view &ctx)
565     {
566 root 1.58 vec3 center = vec3 (ctx.eorig) + vec3 (x + e * .5F, z, y + e * .5F);
567     GLfloat d = length (center);
568 root 1.57
569     if (!overlap (ctx.frustum.c, sphere (center, e * 2)))
570     return;
571    
572 root 1.58 if (d > e * 2.F)
573 root 1.55 {
574 root 1.58 if (mine > e) mine = e; thiscnt++;
575 root 1.56 vb.bind ();
576 root 1.55 ib.draw (GL_TRIANGLES, 0, icnt);
577 root 1.56 }
578     else
579     {
580     for (int i = 0; i < 4; i++)
581     {
582     if (!sub[i])
583     {
584 root 1.57 if (!--maxcnt)
585     {
586     vb.bind ();
587     ib.draw (GL_TRIANGLES, 0, icnt);
588     }
589    
590 root 1.56 node *n = sub[i] = new node;
591    
592 root 1.57 n->hvar = hvar * 0.5;
593 root 1.56 n->x = x + (i & 1 ? e * .5F : 0.F);
594     n->y = y + (i & 2 ? e * .5F : 0.F);
595     n->e = e * .5F;
596    
597     for (int x = 0; x < SIZE; x++)
598     for (int y = 0; y < SIZE; y++)
599     n->vd[y][x] = vd[(y >> 1) + (i & 2 ? SIZE / 2 : 0)][(x >> 1) + (i & 1 ? SIZE / 2 : 0)];
600    
601 root 1.58 for (int x = 1; x < SIZE; x += 2)
602     for (int y = 0; y < SIZE; y += 2)
603     {
604     n->vd[y][x].t = (n->vd[y][x-1].t + n->vd[y][x+1].t) * 0.5F;
605     n->vd[y][x].v = (n->vd[y][x-1].v + n->vd[y][x+1].v) * 0.5F;
606 root 1.60 if (y && y < SIZE - 1)
607     n->vd[y][x].v.y += n->hvar * (GLfloat)rand () / RAND_MAX - n->hvar * .5F;
608 root 1.58 }
609 root 1.57
610 root 1.58 for (int x = 0; x < SIZE; x++)
611     for (int y = 1; y < SIZE; y += 2)
612 root 1.56 {
613 root 1.58 n->vd[y][x].t = (n->vd[y-1][x].t + n->vd[y+1][x].t) * 0.5F;
614 root 1.57 n->vd[y][x].v = (n->vd[y-1][x].v + n->vd[y+1][x].v) * 0.5F;
615 root 1.60 if (x && x < SIZE - 1)
616     n->vd[y][x].v.y += n->hvar * (GLfloat)rand () / RAND_MAX - n->hvar * .5F;
617 root 1.56 }
618    
619     n->update_normals ();
620    
621     n->vb.set (&n->vd[0][0], SIZE * SIZE);
622     }
623    
624     sub[i]->draw (ctx);
625     }
626 root 1.55 }
627 root 1.52 }
628