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