ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.40
Committed: Sat Oct 9 22:43:31 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.39: +7 -25 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <algorithm>
2 root 1.37 #include <GL/gl.h>
3 root 1.1
4     using namespace std;
5 root 1.39 static GLenum gl_error;
6 root 1.1
7 root 1.28 #include "util.h"
8 root 1.1 #include "entity.h"
9 root 1.4 #include "oct.h"
10 root 1.13 #include "view.h"
11 root 1.1
12 root 1.21 /////////////////////////////////////////////////////////////////////////////
13    
14 root 1.38 geometry::~geometry ()
15 root 1.1 {
16 root 1.2 }
17    
18 root 1.38 template class geometry_opengl1d<GL_POINTS>;
19     template class geometry_opengl1d<GL_LINES>;
20     template class geometry_opengl1d<GL_LINE_STRIP>;
21     template class geometry_opengl1d<GL_LINE_LOOP>;
22     template class geometry_opengl2d<GL_TRIANGLES>;
23     template class geometry_opengl2d<GL_TRIANGLE_STRIP>;
24     template class geometry_opengl2d<GL_TRIANGLE_FAN>;
25     template class geometry_opengl2d<GL_QUADS>;
26     template class geometry_opengl2d<GL_QUAD_STRIP>;
27     template class geometry_opengl2d<GL_POLYGON>;
28 root 1.21
29 root 1.38 geometry_opengl::geometry_opengl ()
30 root 1.24 {
31     list = glGenLists (1);
32     }
33    
34 root 1.38 geometry_opengl::~geometry_opengl ()
35 root 1.24 {
36     glDeleteLists (list, 1);
37     }
38    
39 root 1.2 template<GLenum type>
40 root 1.38 void geometry_opengl1d<type>::set (const vector<vertex1d> &v)
41 root 1.2 {
42 root 1.38 clear ();
43     insert (end (), v.begin (), v.end ());
44    
45 root 1.6 bbox.reset ();
46    
47 root 1.38 for (const_iterator i = end (); i-- != begin (); )
48 root 1.6 bbox.add (i->p);
49    
50 root 1.38 update ();
51 root 1.2 }
52    
53     template<GLenum type>
54 root 1.38 void geometry_opengl1d<type>::draw (view &ctx)
55 root 1.2 {
56     glBegin (type);
57    
58     for (iterator i = begin (); i < end (); ++i)
59     {
60 root 1.22 glColor3fv ((GLfloat *)&i->c);
61 root 1.2 glVertex3fv ((GLfloat *)&i->p);
62     }
63    
64     glEnd ();
65     }
66    
67     template<GLenum type>
68 root 1.38 void geometry_opengl2d<type>::set (const vector<vertex2d> &v)
69 root 1.2 {
70 root 1.24 bbox.reset ();
71    
72     for (vector<vertex2d>::const_iterator i = v.end (); i-- != v.begin (); )
73     bbox.add (i->p);
74 root 1.26
75 root 1.28 update ();
76 root 1.24
77     glNewList (list, GL_COMPILE);
78 root 1.14
79 root 1.24 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse);
80     glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular);
81     glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission);
82     glMaterialf (GL_FRONT, GL_SHININESS, m.shininess);
83 root 1.15
84 root 1.20 #if 0
85 root 1.24 glBegin (type);
86    
87 root 1.25 for (vector<vertex2d>::const_iterator i = v.begin (); i < v.end (); ++i)
88 root 1.24 {
89     glTexCoord2fv ((GLfloat *)&i->t);
90     glNormal3fv ((GLfloat *)&i->n);
91     glVertex3fv ((GLfloat *)&i->p);
92     }
93 root 1.4
94 root 1.24 glEnd ();
95 root 1.19 #else
96 root 1.24 glEnableClientState (GL_VERTEX_ARRAY);
97     glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->p);
98     glEnableClientState (GL_NORMAL_ARRAY);
99     glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->n);
100     glEnableClientState (GL_TEXTURE_COORD_ARRAY);
101     glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->t);
102 root 1.19
103 root 1.24 glDrawArrays (type, 0, v.size ());
104 root 1.19 #endif
105 root 1.22
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     : geometry_filter(g), p(0,0,0)
247     {
248     update ();
249     }
250    
251     entity::~entity ()
252     {
253     hide ();
254     }
255    
256     void entity::move (const vec3 &v)
257     {
258     p = p + v;
259    
260     //renormalize (orig, p);
261    
262     update ();
263     }
264    
265     void entity::hide ()
266     {
267     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
268     (*i)->remove (this);
269    
270     o.clear ();
271     }
272    
273     void entity::update ()
274     {
275     if (!g)
276     return;
277    
278     bbox = g->bbox;
279    
280     a.x = orig.x + (soffs)floorf (bbox.a.x + p.x);
281     a.y = orig.y + (soffs)floorf (bbox.a.y + p.y);
282     a.z = orig.z + (soffs)floorf (bbox.a.z + p.z);
283     b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x);
284     b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y);
285     b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z);
286    
287     if (o.size ())
288     {
289     hide ();
290     show ();
291     }
292     }
293    
294     void entity::draw (view &ctx)
295     {
296     sector diff = orig - ctx.orig;
297    
298     glPushMatrix ();
299     glTranslatef (diff.x + p.x, diff.y + p.y, diff.z + p.z);
300     g->draw (ctx);
301     glPopMatrix ();
302     }
303    
304     void entity::display (view &ctx)
305     {
306     if (ctx.may_draw (this))
307     draw (ctx);
308 root 1.23 }
309 root 1.1
310 root 1.39 ///////////////////////////////////////////////////////////////////////////
311 root 1.40 //
312 root 1.39 static void nurbs_error (GLenum errorCode)
313     {
314     const GLubyte *estring;
315     estring = gluErrorString(errorCode);
316     fprintf (stderr, "Nurbs error: %s\n", estring);
317     }
318 root 1.40
319 root 1.39 void errorCallback(GLenum errorCode)
320     {
321     const GLubyte *estring;
322    
323     estring = gluErrorString(errorCode);
324     fprintf (stderr, "Tessellation Error: %s\n", estring);
325     exit (0);
326     }
327 root 1.40
328 root 1.39 void geometry_nurbs::set ()
329     {
330     // < XXX >: Testing CODE
331     int u, v;
332     for (u = 0; u < 4; u++) {
333     for (v = 0; v < 4; v++) {
334     ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
335     ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
336    
337     if ( (u == 1 || u == 2) && (v == 1 || v == 2))
338     ctlpoints[u][v][2] = 3.0;
339     else
340     ctlpoints[u][v][2] = -3.0;
341     }
342     }
343     // </ XXX >
344 root 1.1
345 root 1.39 glEnable(GL_AUTO_NORMAL);
346     nurb = gluNewNurbsRenderer ();
347     gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 300.0);
348     gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_NURBS_TESSELLATOR);
349 root 1.40 // gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_PARAMETRIC_ERROR);
350     // gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_TRUE);
351 root 1.39 gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error);
352     glDisable(GL_AUTO_NORMAL);
353     }
354 root 1.40
355 root 1.39 void geometry_nurbs::draw (view &ctx)
356     {
357     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
358    
359     GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
360     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
361     GLfloat mat_shininess[] = { 100.0 };
362    
363     glClearColor (0.0, 0.0, 0.0, 0.0);
364     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
365     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
366     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
367    
368    
369     glEnable(GL_AUTO_NORMAL);
370     gluBeginSurface (nurb);
371     gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3);
372    
373 root 1.40 gluEndSurface (nurb);
374 root 1.39 glDisable(GL_AUTO_NORMAL);
375     }