ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.45
Committed: Mon Oct 11 15:26:34 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.44: +1 -1 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.38 void geometry_opengl1d<type>::set (const vector<vertex1d> &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     {
61 root 1.22 glColor3fv ((GLfloat *)&i->c);
62 root 1.2 glVertex3fv ((GLfloat *)&i->p);
63     }
64    
65     glEnd ();
66     }
67    
68     template<GLenum type>
69 root 1.38 void geometry_opengl2d<type>::set (const vector<vertex2d> &v)
70 root 1.2 {
71 root 1.24 bbox.reset ();
72    
73     for (vector<vertex2d>::const_iterator i = v.end (); i-- != v.begin (); )
74     bbox.add (i->p);
75 root 1.26
76 root 1.28 update ();
77 root 1.24
78     glNewList (list, GL_COMPILE);
79 root 1.14
80 root 1.43 m->begin ();
81 root 1.15
82 root 1.20 #if 0
83 root 1.24 glBegin (type);
84    
85 root 1.25 for (vector<vertex2d>::const_iterator i = v.begin (); i < v.end (); ++i)
86 root 1.24 {
87     glTexCoord2fv ((GLfloat *)&i->t);
88     glNormal3fv ((GLfloat *)&i->n);
89     glVertex3fv ((GLfloat *)&i->p);
90     }
91 root 1.4
92 root 1.24 glEnd ();
93 root 1.19 #else
94 root 1.24 glEnableClientState (GL_VERTEX_ARRAY);
95     glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->p);
96     glEnableClientState (GL_NORMAL_ARRAY);
97     glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->n);
98     glEnableClientState (GL_TEXTURE_COORD_ARRAY);
99     glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->t);
100 root 1.19
101 root 1.24 glDrawArrays (type, 0, v.size ());
102 root 1.19 #endif
103 root 1.22
104 root 1.43 m->end ();
105    
106 root 1.24 glEndList ();
107     }
108 root 1.2
109 root 1.24 template<GLenum type>
110 root 1.38 void geometry_opengl2d<type>::draw (view &ctx)
111 root 1.24 {
112 root 1.17 glCallList (list);
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     material *m = new simple_material;
127    
128 root 1.45 int n = max (8, (int)(ctx.pixfact * radius) / 10);
129 root 1.44
130     m->begin ();
131     GLUquadric *quad = gluNewQuadric ();
132     gluQuadricTexture (quad, true);
133     gluSphere (quad, radius, n, n);
134     gluDeleteQuadric (quad);
135     m->end ();
136    
137     delete m;
138     }
139    
140 root 1.22 /////////////////////////////////////////////////////////////////////////////
141    
142 root 1.38 void geometry_transform::update ()
143 root 1.22 {
144 root 1.38 const box &sub = g->bbox;
145 root 1.27
146     bbox.reset ();
147     bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z));
148     bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z));
149     bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z));
150     bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z));
151     bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z));
152     bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z));
153     bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z));
154     bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z));
155 root 1.22
156 root 1.38 geometry::update ();
157 root 1.28 }
158    
159 root 1.38 #if 0
160     void geometry_transform::renormalize ()
161 root 1.28 {
162 root 1.34 point trans(m(0,3), m(1,3), m(2,3));
163     ::renormalize (e->orig, trans);
164     m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z;
165 root 1.38 }
166 root 1.28 #endif
167    
168 root 1.38 void geometry_transform::update (const matrix &xfrm)
169 root 1.28 {
170     m = m * xfrm;
171    
172     update ();
173     }
174    
175 root 1.38 void geometry_transform::set_matrix (const matrix &xfrm)
176 root 1.28 {
177     m = xfrm;
178    
179     update ();
180 root 1.22 }
181    
182 root 1.38 void geometry_transform::draw (view &ctx)
183 root 1.22 {
184 root 1.33 glPushMatrix ();
185     glMultMatrixf ((GLfloat *)&m);
186 root 1.38 g->draw (ctx);
187 root 1.33 glPopMatrix ();
188 root 1.2 }
189    
190 root 1.38 void geometry_anim::draw (view &ctx)
191 root 1.23 {
192 root 1.30 matrix save_m = m;
193 root 1.23
194 root 1.30 update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0))
195     * matrix::rotation (vy * timer.now, vec3 (0, 1, 0))
196     * matrix::rotation (vz * timer.now, vec3 (0, 0, 1)));
197 root 1.28
198 root 1.38 geometry_transform::draw (ctx);
199 root 1.23
200     m = save_m;
201 root 1.38 }
202    
203     /////////////////////////////////////////////////////////////////////////////
204    
205     void geometry_filter::set (geometry *g)
206     {
207     this->g = g;
208    
209     if (g)
210     g->parent = this;
211    
212     update ();
213     }
214    
215     void geometry_filter::update ()
216     {
217     if (g)
218     {
219     bbox = g->bbox;
220     geometry::update ();
221     }
222     }
223    
224     void geometry_filter::draw (view &ctx)
225     {
226     g->draw (ctx);
227     }
228    
229     geometry_filter::~geometry_filter ()
230     {
231     delete g;
232     }
233    
234     /////////////////////////////////////////////////////////////////////////////
235    
236     void geometry_container::update ()
237     {
238     bbox.reset ();
239    
240     for (iterator i = end (); i-- != begin (); )
241     bbox.add ((*i)->bbox);
242    
243     geometry::update ();
244     }
245    
246     void geometry_container::add (geometry *g)
247     {
248     push_back (g);
249     g->parent = this;
250    
251     update ();
252     }
253    
254     void geometry_container::draw (view &ctx)
255     {
256     for (iterator i = end (); i-- != begin (); )
257     (*i)->draw (ctx);
258     }
259    
260     geometry_container::~geometry_container ()
261     {
262     for (iterator i = end (); i-- != begin (); )
263     delete *i;
264    
265     clear ();
266     }
267    
268     /////////////////////////////////////////////////////////////////////////////
269    
270     entity::entity (geometry *g)
271 root 1.42 : geometry_filter(g)
272     , p(0,0,0)
273 root 1.38 {
274     update ();
275     }
276    
277     entity::~entity ()
278     {
279     hide ();
280     }
281    
282     void entity::move (const vec3 &v)
283     {
284     p = p + v;
285    
286     //renormalize (orig, p);
287    
288     update ();
289 root 1.42 }
290    
291     void entity::show ()
292     {
293     if (!o.size ())
294     world.add (this);
295 root 1.38 }
296    
297     void entity::hide ()
298     {
299     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
300     (*i)->remove (this);
301    
302     o.clear ();
303     }
304    
305     void entity::update ()
306     {
307     if (!g)
308     return;
309    
310     bbox = g->bbox;
311    
312     a.x = orig.x + (soffs)floorf (bbox.a.x + p.x);
313     a.y = orig.y + (soffs)floorf (bbox.a.y + p.y);
314     a.z = orig.z + (soffs)floorf (bbox.a.z + p.z);
315     b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x);
316     b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y);
317     b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z);
318    
319     if (o.size ())
320     {
321     hide ();
322     show ();
323     }
324     }
325    
326     void entity::draw (view &ctx)
327     {
328     sector diff = orig - ctx.orig;
329    
330     glPushMatrix ();
331     glTranslatef (diff.x + p.x, diff.y + p.y, diff.z + p.z);
332     g->draw (ctx);
333     glPopMatrix ();
334     }
335    
336     void entity::display (view &ctx)
337     {
338     if (ctx.may_draw (this))
339     draw (ctx);
340 root 1.23 }
341 root 1.1
342 root 1.39 ///////////////////////////////////////////////////////////////////////////
343 root 1.40 //
344 root 1.39 static void nurbs_error (GLenum errorCode)
345     {
346     const GLubyte *estring;
347     estring = gluErrorString(errorCode);
348     fprintf (stderr, "Nurbs error: %s\n", estring);
349     }
350 root 1.40
351 root 1.39 void errorCallback(GLenum errorCode)
352     {
353     const GLubyte *estring;
354    
355     estring = gluErrorString(errorCode);
356     fprintf (stderr, "Tessellation Error: %s\n", estring);
357     exit (0);
358     }
359 root 1.41 void tcbBegin (GLenum prim)
360     {
361     glBegin (prim);
362     }
363     void tcbVertex (void *data)
364     {
365     glVertex3fv ((GLfloat *)data);
366     }
367     void tcbEnd ()
368     {
369     glEnd ();
370     }
371 root 1.39 void geometry_nurbs::set ()
372     {
373     // < XXX >: Testing CODE
374     int u, v;
375     for (u = 0; u < 4; u++) {
376     for (v = 0; v < 4; v++) {
377     ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
378     ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
379    
380     if ( (u == 1 || u == 2) && (v == 1 || v == 2))
381     ctlpoints[u][v][2] = 3.0;
382     else
383     ctlpoints[u][v][2] = -3.0;
384     }
385     }
386 root 1.41 tess = gluNewTess();
387 root 1.39 // </ XXX >
388 root 1.1
389 root 1.39 glEnable(GL_AUTO_NORMAL);
390     nurb = gluNewNurbsRenderer ();
391 root 1.41 gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
392     gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL);
393     gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
394     gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH);
395     gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0);
396 root 1.39 gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error);
397 root 1.41 gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin);
398     gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex);
399     gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd);
400 root 1.39 glDisable(GL_AUTO_NORMAL);
401 root 1.41
402 root 1.39 }
403 root 1.40
404 root 1.39 void geometry_nurbs::draw (view &ctx)
405     {
406     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
407    
408     GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
409     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
410     GLfloat mat_shininess[] = { 100.0 };
411    
412     glClearColor (0.0, 0.0, 0.0, 0.0);
413     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
414     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
415     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
416    
417    
418     glEnable(GL_AUTO_NORMAL);
419     gluBeginSurface (nurb);
420     gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3);
421 root 1.41 gluEndSurface (nurb);
422     /*
423     glEnable(GL_AUTO_NORMAL);
424     glPushMatrix();
425     GL_LG_DEBUG;
426     =======
427 root 1.39
428 root 1.40 gluEndSurface (nurb);
429 root 1.41 >>>>>>> 1.40
430 root 1.39 glDisable(GL_AUTO_NORMAL);
431 root 1.41 glPopMatrix();
432     glFlush();
433     */
434 root 1.39 }