ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/entity.C
Revision: 1.51
Committed: Fri Oct 29 17:21:54 2004 UTC (22 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.50: +2 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <algorithm>
2 root 1.42 #include <cassert>
3 root 1.1
4     using namespace std;
5 root 1.42
6     #include "opengl.h"
7 root 1.1
8 root 1.28 #include "util.h"
9 root 1.1 #include "entity.h"
10 root 1.4 #include "oct.h"
11 root 1.13 #include "view.h"
12 root 1.1
13 root 1.21 /////////////////////////////////////////////////////////////////////////////
14    
15 root 1.38 geometry::~geometry ()
16 root 1.1 {
17 root 1.2 }
18    
19 root 1.38 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 root 1.21
30 root 1.38 geometry_opengl::geometry_opengl ()
31 root 1.24 {
32     list = glGenLists (1);
33     }
34    
35 root 1.38 geometry_opengl::~geometry_opengl ()
36 root 1.24 {
37     glDeleteLists (list, 1);
38     }
39    
40 root 1.2 template<GLenum type>
41 root 1.46 void geometry_opengl1d<type>::set (const vector<vertex_v3f> &v)
42 root 1.2 {
43 root 1.38 clear ();
44     insert (end (), v.begin (), v.end ());
45    
46 root 1.6 bbox.reset ();
47    
48 root 1.38 for (const_iterator i = end (); i-- != begin (); )
49 root 1.6 bbox.add (i->p);
50    
51 root 1.38 update ();
52 root 1.2 }
53    
54     template<GLenum type>
55 root 1.38 void geometry_opengl1d<type>::draw (view &ctx)
56 root 1.2 {
57     glBegin (type);
58    
59     for (iterator i = begin (); i < end (); ++i)
60 root 1.46 glVertex3fv ((GLfloat *)&i->p);
61 root 1.2
62     glEnd ();
63     }
64    
65     template<GLenum type>
66 root 1.46 void geometry_opengl2d<type>::set (const vector<vertex_t2f_n3f_v3f> &v)
67 root 1.2 {
68 root 1.24 bbox.reset ();
69    
70 root 1.46 for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.end (); i-- != v.begin (); )
71 root 1.24 bbox.add (i->p);
72 root 1.26
73 root 1.28 update ();
74 root 1.24
75     glNewList (list, GL_COMPILE);
76 root 1.14
77 root 1.20 #if 0
78 root 1.24 glBegin (type);
79    
80 root 1.46 for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.begin (); i < v.end (); ++i)
81 root 1.24 {
82     glTexCoord2fv ((GLfloat *)&i->t);
83     glNormal3fv ((GLfloat *)&i->n);
84     glVertex3fv ((GLfloat *)&i->p);
85     }
86 root 1.4
87 root 1.24 glEnd ();
88 root 1.19 #else
89 root 1.24 glEnableClientState (GL_VERTEX_ARRAY);
90     glEnableClientState (GL_NORMAL_ARRAY);
91     glEnableClientState (GL_TEXTURE_COORD_ARRAY);
92 root 1.46 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 root 1.19
96 root 1.24 glDrawArrays (type, 0, v.size ());
97 root 1.46
98     glDisableClientState (GL_VERTEX_ARRAY);
99     glDisableClientState (GL_NORMAL_ARRAY);
100     glDisableClientState (GL_TEXTURE_COORD_ARRAY);
101 root 1.19 #endif
102 root 1.22
103 root 1.24 glEndList ();
104     }
105 root 1.2
106 root 1.24 template<GLenum type>
107 root 1.38 void geometry_opengl2d<type>::draw (view &ctx)
108 root 1.24 {
109 root 1.51 m->enable (ctx);
110 root 1.17 glCallList (list);
111 root 1.51 m->disable (ctx);
112 root 1.22 }
113    
114 root 1.44 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 root 1.48 int n = min (100, max (15, (int)(ctx.pixfact * radius) / 10));
126 root 1.44
127     GLUquadric *quad = gluNewQuadric ();
128     gluQuadricTexture (quad, true);
129     gluSphere (quad, radius, n, n);
130     gluDeleteQuadric (quad);
131     }
132    
133 root 1.22 /////////////////////////////////////////////////////////////////////////////
134    
135 root 1.38 void geometry_transform::update ()
136 root 1.22 {
137 root 1.38 const box &sub = g->bbox;
138 root 1.27
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 root 1.22
149 root 1.38 geometry::update ();
150 root 1.28 }
151    
152 root 1.38 #if 0
153     void geometry_transform::renormalize ()
154 root 1.28 {
155 root 1.34 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 root 1.38 }
159 root 1.28 #endif
160    
161 root 1.38 void geometry_transform::update (const matrix &xfrm)
162 root 1.28 {
163     m = m * xfrm;
164    
165     update ();
166     }
167    
168 root 1.38 void geometry_transform::set_matrix (const matrix &xfrm)
169 root 1.28 {
170     m = xfrm;
171    
172     update ();
173 root 1.22 }
174    
175 root 1.38 void geometry_transform::draw (view &ctx)
176 root 1.22 {
177 root 1.33 glPushMatrix ();
178     glMultMatrixf ((GLfloat *)&m);
179 root 1.38 g->draw (ctx);
180 root 1.33 glPopMatrix ();
181 root 1.2 }
182    
183 root 1.38 void geometry_anim::draw (view &ctx)
184 root 1.23 {
185 root 1.30 matrix save_m = m;
186 root 1.23
187 root 1.30 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 root 1.28
191 root 1.38 geometry_transform::draw (ctx);
192 root 1.23
193     m = save_m;
194 root 1.38 }
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     entity::entity (geometry *g)
264 root 1.42 : geometry_filter(g)
265     , p(0,0,0)
266 root 1.38 {
267     update ();
268     }
269    
270     entity::~entity ()
271     {
272     hide ();
273     }
274    
275     void entity::move (const vec3 &v)
276     {
277     p = p + v;
278    
279 root 1.49 renormalize (orig, p);
280 root 1.38
281     update ();
282 root 1.42 }
283    
284     void entity::show ()
285     {
286     if (!o.size ())
287     world.add (this);
288 root 1.38 }
289    
290     void entity::hide ()
291     {
292     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
293     (*i)->remove (this);
294    
295     o.clear ();
296     }
297    
298     void entity::update ()
299     {
300     if (!g)
301     return;
302    
303     bbox = g->bbox;
304    
305     a.x = orig.x + (soffs)floorf (bbox.a.x + p.x);
306     a.y = orig.y + (soffs)floorf (bbox.a.y + p.y);
307     a.z = orig.z + (soffs)floorf (bbox.a.z + p.z);
308     b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x);
309     b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y);
310     b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z);
311    
312     if (o.size ())
313     {
314     hide ();
315     show ();
316     }
317     }
318    
319     void entity::draw (view &ctx)
320     {
321     sector diff = orig - ctx.orig;
322    
323     glPushMatrix ();
324     glTranslatef (diff.x + p.x, diff.y + p.y, diff.z + p.z);
325     g->draw (ctx);
326     glPopMatrix ();
327     }
328    
329 root 1.39 ///////////////////////////////////////////////////////////////////////////
330 root 1.40 //
331 root 1.39 static void nurbs_error (GLenum errorCode)
332     {
333     const GLubyte *estring;
334     estring = gluErrorString(errorCode);
335     fprintf (stderr, "Nurbs error: %s\n", estring);
336     }
337 root 1.40
338 root 1.39 void errorCallback(GLenum errorCode)
339     {
340     const GLubyte *estring;
341    
342     estring = gluErrorString(errorCode);
343     fprintf (stderr, "Tessellation Error: %s\n", estring);
344     exit (0);
345     }
346 root 1.41 void tcbBegin (GLenum prim)
347     {
348     glBegin (prim);
349     }
350     void tcbVertex (void *data)
351     {
352     glVertex3fv ((GLfloat *)data);
353     }
354     void tcbEnd ()
355     {
356     glEnd ();
357     }
358 root 1.39 void geometry_nurbs::set ()
359     {
360     // < XXX >: Testing CODE
361     int u, v;
362     for (u = 0; u < 4; u++) {
363     for (v = 0; v < 4; v++) {
364     ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
365     ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
366    
367     if ( (u == 1 || u == 2) && (v == 1 || v == 2))
368     ctlpoints[u][v][2] = 3.0;
369     else
370     ctlpoints[u][v][2] = -3.0;
371     }
372     }
373 root 1.41 tess = gluNewTess();
374 root 1.39 // </ XXX >
375 root 1.1
376 root 1.39 glEnable(GL_AUTO_NORMAL);
377     nurb = gluNewNurbsRenderer ();
378 root 1.41 gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
379     gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL);
380     gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
381     gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH);
382     gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0);
383 root 1.39 gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error);
384 root 1.41 gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin);
385     gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex);
386     gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd);
387 root 1.39 glDisable(GL_AUTO_NORMAL);
388 root 1.41
389 root 1.39 }
390 root 1.40
391 root 1.39 void geometry_nurbs::draw (view &ctx)
392     {
393     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
394    
395     GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
396     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
397     GLfloat mat_shininess[] = { 100.0 };
398    
399     glClearColor (0.0, 0.0, 0.0, 0.0);
400     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
401     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
402     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
403    
404    
405     glEnable(GL_AUTO_NORMAL);
406     gluBeginSurface (nurb);
407     gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3);
408 root 1.41 gluEndSurface (nurb);
409     /*
410     glEnable(GL_AUTO_NORMAL);
411     glPushMatrix();
412     GL_LG_DEBUG;
413     =======
414 root 1.39
415 root 1.40 gluEndSurface (nurb);
416 root 1.41 >>>>>>> 1.40
417 root 1.39 glDisable(GL_AUTO_NORMAL);
418 root 1.41 glPopMatrix();
419     glFlush();
420     */
421 root 1.39 }