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