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