ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.68
Committed: Tue Aug 9 23:58:43 2005 UTC (18 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.67: +115 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.54 #include <cassert>
2     #include <cstdlib>
3 root 1.1 #include <algorithm>
4    
5     using namespace std;
6 root 1.42
7     #include "opengl.h"
8 root 1.1
9 root 1.28 #include "util.h"
10 root 1.1 #include "entity.h"
11 root 1.4 #include "oct.h"
12 root 1.13 #include "view.h"
13 root 1.1
14 root 1.21 /////////////////////////////////////////////////////////////////////////////
15    
16 root 1.38 geometry::~geometry ()
17 root 1.1 {
18 root 1.2 }
19    
20 root 1.38 template class geometry_opengl1d<GL_POINTS>;
21     template class geometry_opengl1d<GL_LINES>;
22     template class geometry_opengl1d<GL_LINE_STRIP>;
23     template class geometry_opengl1d<GL_LINE_LOOP>;
24     template class geometry_opengl2d<GL_TRIANGLES>;
25     template class geometry_opengl2d<GL_TRIANGLE_STRIP>;
26     template class geometry_opengl2d<GL_TRIANGLE_FAN>;
27     template class geometry_opengl2d<GL_QUADS>;
28     template class geometry_opengl2d<GL_QUAD_STRIP>;
29     template class geometry_opengl2d<GL_POLYGON>;
30 root 1.21
31 root 1.38 geometry_opengl::geometry_opengl ()
32 root 1.24 {
33     list = glGenLists (1);
34     }
35    
36 root 1.38 geometry_opengl::~geometry_opengl ()
37 root 1.24 {
38     glDeleteLists (list, 1);
39     }
40    
41 root 1.2 template<GLenum type>
42 root 1.46 void geometry_opengl1d<type>::set (const vector<vertex_v3f> &v)
43 root 1.2 {
44 root 1.38 clear ();
45     insert (end (), v.begin (), v.end ());
46    
47 root 1.6 bbox.reset ();
48    
49 root 1.38 for (const_iterator i = end (); i-- != begin (); )
50 root 1.54 bbox.add (i->v);
51 root 1.6
52 root 1.38 update ();
53 root 1.2 }
54    
55     template<GLenum type>
56 root 1.38 void geometry_opengl1d<type>::draw (view &ctx)
57 root 1.2 {
58     glBegin (type);
59    
60     for (iterator i = begin (); i < end (); ++i)
61 root 1.54 glVertex3fv ((GLfloat *)&i->v);
62 root 1.2
63     glEnd ();
64     }
65    
66     template<GLenum type>
67 root 1.46 void geometry_opengl2d<type>::set (const vector<vertex_t2f_n3f_v3f> &v)
68 root 1.2 {
69 root 1.24 bbox.reset ();
70    
71 root 1.46 for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.end (); i-- != v.begin (); )
72 root 1.54 bbox.add (i->v);
73 root 1.26
74 root 1.28 update ();
75 root 1.24
76     glNewList (list, GL_COMPILE);
77 root 1.14
78 root 1.20 #if 0
79 root 1.24 glBegin (type);
80    
81 root 1.46 for (vector<vertex_t2f_n3f_v3f>::const_iterator i = v.begin (); i < v.end (); ++i)
82 root 1.24 {
83     glTexCoord2fv ((GLfloat *)&i->t);
84     glNormal3fv ((GLfloat *)&i->n);
85 root 1.54 glVertex3fv ((GLfloat *)&i->v);
86 root 1.24 }
87 root 1.4
88 root 1.24 glEnd ();
89 root 1.19 #else
90 root 1.65 glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0);
91    
92 root 1.24 glEnableClientState (GL_VERTEX_ARRAY);
93     glEnableClientState (GL_NORMAL_ARRAY);
94     glEnableClientState (GL_TEXTURE_COORD_ARRAY);
95 root 1.46 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.67 glVertexPointer (3, GL_FLOAT, sizeof (vertex_t2f_n3f_v3f), (void *)&v.begin ()->v);
98 root 1.19
99 root 1.24 glDrawArrays (type, 0, v.size ());
100 root 1.46
101     glDisableClientState (GL_VERTEX_ARRAY);
102     glDisableClientState (GL_NORMAL_ARRAY);
103     glDisableClientState (GL_TEXTURE_COORD_ARRAY);
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.51 m->enable (ctx);
113 root 1.17 glCallList (list);
114 root 1.51 m->disable (ctx);
115 root 1.22 }
116    
117 root 1.44 void geometry_sphere::update ()
118     {
119     bbox.reset ();
120     bbox.add (-point (radius, radius, radius));
121     bbox.add ( point (radius, radius, radius));
122    
123     geometry::update ();
124     }
125    
126     void geometry_sphere::draw (view &ctx)
127     {
128 root 1.48 int n = min (100, max (15, (int)(ctx.pixfact * radius) / 10));
129 root 1.44
130 root 1.61 m->enable (ctx);
131 root 1.44 GLUquadric *quad = gluNewQuadric ();
132     gluQuadricTexture (quad, true);
133     gluSphere (quad, radius, n, n);
134     gluDeleteQuadric (quad);
135 root 1.61 m->disable (ctx);
136 root 1.44 }
137    
138 root 1.22 /////////////////////////////////////////////////////////////////////////////
139    
140 root 1.63 void geometry_indexed_2d::draw (view &ctx)
141     {
142     m->enable (ctx);
143     vb.bind ();
144     ib.draw (type, 0, ib.count);
145     m->disable (ctx);
146     }
147    
148     /////////////////////////////////////////////////////////////////////////////
149    
150 root 1.38 void geometry_transform::update ()
151 root 1.22 {
152 root 1.38 const box &sub = g->bbox;
153 root 1.27
154     bbox.reset ();
155     bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z));
156     bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z));
157     bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z));
158     bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z));
159     bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z));
160     bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z));
161     bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z));
162     bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z));
163 root 1.22
164 root 1.38 geometry::update ();
165 root 1.28 }
166    
167 root 1.38 #if 0
168     void geometry_transform::renormalize ()
169 root 1.28 {
170 root 1.34 point trans(m(0,3), m(1,3), m(2,3));
171     ::renormalize (e->orig, trans);
172     m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z;
173 root 1.38 }
174 root 1.28 #endif
175    
176 root 1.38 void geometry_transform::update (const matrix &xfrm)
177 root 1.28 {
178     m = m * xfrm;
179    
180     update ();
181     }
182    
183 root 1.38 void geometry_transform::set_matrix (const matrix &xfrm)
184 root 1.28 {
185     m = xfrm;
186    
187     update ();
188 root 1.22 }
189    
190 root 1.38 void geometry_transform::draw (view &ctx)
191 root 1.22 {
192 root 1.33 glPushMatrix ();
193     glMultMatrixf ((GLfloat *)&m);
194 root 1.38 g->draw (ctx);
195 root 1.33 glPopMatrix ();
196 root 1.2 }
197    
198 root 1.38 void geometry_anim::draw (view &ctx)
199 root 1.23 {
200 root 1.30 matrix save_m = m;
201 root 1.23
202 root 1.30 update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0))
203     * matrix::rotation (vy * timer.now, vec3 (0, 1, 0))
204     * matrix::rotation (vz * timer.now, vec3 (0, 0, 1)));
205 root 1.28
206 root 1.38 geometry_transform::draw (ctx);
207 root 1.23
208     m = save_m;
209 root 1.38 }
210    
211     /////////////////////////////////////////////////////////////////////////////
212    
213     void geometry_filter::set (geometry *g)
214     {
215     this->g = g;
216    
217     if (g)
218     g->parent = this;
219    
220     update ();
221     }
222    
223     void geometry_filter::update ()
224     {
225     if (g)
226     {
227     bbox = g->bbox;
228     geometry::update ();
229     }
230     }
231    
232     void geometry_filter::draw (view &ctx)
233     {
234     g->draw (ctx);
235     }
236    
237     geometry_filter::~geometry_filter ()
238     {
239     delete g;
240     }
241    
242     /////////////////////////////////////////////////////////////////////////////
243    
244     void geometry_container::update ()
245     {
246     bbox.reset ();
247    
248     for (iterator i = end (); i-- != begin (); )
249     bbox.add ((*i)->bbox);
250    
251     geometry::update ();
252     }
253    
254     void geometry_container::add (geometry *g)
255     {
256     push_back (g);
257     g->parent = this;
258    
259     update ();
260     }
261    
262     void geometry_container::draw (view &ctx)
263     {
264     for (iterator i = end (); i-- != begin (); )
265     (*i)->draw (ctx);
266     }
267    
268     geometry_container::~geometry_container ()
269     {
270     for (iterator i = end (); i-- != begin (); )
271     delete *i;
272    
273     clear ();
274     }
275    
276 root 1.52 ///////////////////////////////////////////////////////////////////////////
277 root 1.38
278 root 1.39 static void nurbs_error (GLenum errorCode)
279     {
280     const GLubyte *estring;
281     estring = gluErrorString(errorCode);
282     fprintf (stderr, "Nurbs error: %s\n", estring);
283     }
284 root 1.40
285 root 1.39 void errorCallback(GLenum errorCode)
286     {
287     const GLubyte *estring;
288    
289     estring = gluErrorString(errorCode);
290     fprintf (stderr, "Tessellation Error: %s\n", estring);
291     exit (0);
292     }
293 root 1.54
294 root 1.41 void tcbBegin (GLenum prim)
295     {
296     glBegin (prim);
297     }
298 root 1.54
299 root 1.41 void tcbVertex (void *data)
300     {
301     glVertex3fv ((GLfloat *)data);
302     }
303 root 1.54
304 root 1.41 void tcbEnd ()
305     {
306     glEnd ();
307     }
308 root 1.54
309 root 1.39 void geometry_nurbs::set ()
310     {
311     // < XXX >: Testing CODE
312     int u, v;
313     for (u = 0; u < 4; u++) {
314     for (v = 0; v < 4; v++) {
315     ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
316     ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
317    
318     if ( (u == 1 || u == 2) && (v == 1 || v == 2))
319     ctlpoints[u][v][2] = 3.0;
320     else
321     ctlpoints[u][v][2] = -3.0;
322     }
323     }
324 root 1.41 tess = gluNewTess();
325 root 1.39 // </ XXX >
326 root 1.1
327 root 1.39 glEnable(GL_AUTO_NORMAL);
328     nurb = gluNewNurbsRenderer ();
329 root 1.41 gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
330     gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL);
331     gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
332     gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH);
333     gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0);
334 root 1.39 gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error);
335 root 1.41 gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin);
336     gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex);
337     gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd);
338 root 1.39 glDisable(GL_AUTO_NORMAL);
339 root 1.41
340 root 1.39 }
341 root 1.40
342 root 1.39 void geometry_nurbs::draw (view &ctx)
343     {
344     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
345    
346     GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
347     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
348     GLfloat mat_shininess[] = { 100.0 };
349    
350     glClearColor (0.0, 0.0, 0.0, 0.0);
351     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
352     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
353     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
354    
355    
356     glEnable(GL_AUTO_NORMAL);
357     gluBeginSurface (nurb);
358     gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3);
359 root 1.41 gluEndSurface (nurb);
360     /*
361     glEnable(GL_AUTO_NORMAL);
362     glPushMatrix();
363     GL_LG_DEBUG;
364     =======
365 root 1.39
366 root 1.40 gluEndSurface (nurb);
367 root 1.41 >>>>>>> 1.40
368 root 1.39 glDisable(GL_AUTO_NORMAL);
369 root 1.41 glPopMatrix();
370     glFlush();
371     */
372 root 1.39 }
373 root 1.52
374     /////////////////////////////////////////////////////////////////////////////
375    
376     entity::entity (geometry *g)
377     : geometry_filter(g)
378     , p(0,0,0)
379     {
380     update ();
381     }
382    
383     entity::~entity ()
384     {
385     hide ();
386     }
387    
388     void entity::move (const vec3 &v)
389     {
390     p = p + v;
391    
392     renormalize (orig, p);
393    
394     update ();
395     }
396    
397     void entity::show ()
398     {
399     if (!o.size ())
400     world.add (this);
401     }
402    
403     void entity::hide ()
404     {
405     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
406     (*i)->remove (this);
407    
408     o.clear ();
409     }
410    
411     void entity::update ()
412     {
413     if (!g)
414     return;
415    
416     bbox = g->bbox;
417    
418     a.x = orig.x + (soffs)floorf (bbox.a.x + p.x);
419     a.y = orig.y + (soffs)floorf (bbox.a.y + p.y);
420     a.z = orig.z + (soffs)floorf (bbox.a.z + p.z);
421     b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x);
422     b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y);
423     b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z);
424    
425     if (o.size ())
426     {
427     hide ();
428     show ();
429     }
430     }
431    
432     void entity::draw (view &ctx)
433     {
434 root 1.57 ctx.eorig = orig - ctx.orig;
435 root 1.52
436     glPushMatrix ();
437 root 1.57 glTranslatef (p.x + ctx.eorig.x, p.y + ctx.eorig.y, p.z + ctx.eorig.z);
438 root 1.52 g->draw (ctx);
439     glPopMatrix ();
440     }
441    
442     /////////////////////////////////////////////////////////////////////////////
443     //
444    
445     #define SIZE 33
446    
447     struct geometry_heightfield::node
448     {
449 root 1.54 vertex_t2f_n3f_v3f vd[SIZE][SIZE];
450 root 1.52 gl::vertex_buffer vb;
451 root 1.53 GLushort *ibp;
452 root 1.54 GLfloat hvar;
453 root 1.58 GLfloat x, y, z, e;
454 root 1.53
455 root 1.54 // 23
456     // 01
457     struct geometry_heightfield::node *sub[4];
458    
459 root 1.56 void update_normals ();
460     void draw (view &ctx);
461    
462 root 1.54 node ();
463 root 1.52 };
464 root 1.53
465 root 1.54 geometry_heightfield::node::node ()
466 root 1.53 {
467 root 1.54 sub[0] = sub[1] = sub[2] = sub[3] = 0;
468     //vb.set (vd);
469 root 1.53 }
470 root 1.52
471 root 1.54 static index_buffer ib;
472     static GLushort *ibp;
473     static int icnt;
474    
475     geometry_heightfield::geometry_heightfield (GLfloat sx, GLfloat sy)
476 root 1.55 : sx(sx), sy(sy)
477     , sm (max (sx, sy))
478 root 1.52 {
479 root 1.54 if (!ib)
480     {
481 root 1.58 ib.set<GLushort> (SIZE * SIZE * 6, GL_STATIC_DRAW);
482 root 1.54 ibp = (GLushort *)ib.map ();
483    
484     GLushort *p = ibp;
485    
486     for (int x = 0; x < SIZE - 1; x++)
487     for (int y = 0; y < SIZE - 1; y++)
488     {
489     unsigned short a = (y + 0) * SIZE + (x + 0);
490     unsigned short b = (y + 0) * SIZE + (x + 1);
491     unsigned short c = (y + 1) * SIZE + (x + 1);
492     unsigned short d = (y + 1) * SIZE + (x + 0);
493    
494     *p++ = c; *p++ = b; *p++ = a;
495     *p++ = d; *p++ = c; *p++ = a;
496     }
497    
498     icnt = p - ibp;
499    
500     ib.unmap ();
501     }
502    
503 root 1.52 update ();
504 root 1.54
505     tree = new node;
506     tree->hvar = 0.01F * sx;
507     tree->x = tree->y = 0.F;
508 root 1.56 tree->e = sm;
509 root 1.54
510     for (int x = 0; x < SIZE; x++)
511     for (int y = 0; y < SIZE; y++)
512     {
513     vertex_t2f_n3f_v3f &v = tree->vd[y][x];
514    
515     v.t = tex2 ((GLfloat)x / (SIZE - 1), (GLfloat)y / (SIZE - 1));
516     v.v = vec3 (sx * x / (SIZE - 1), tree->hvar * rand () / RAND_MAX, sy * y / (SIZE - 1));
517 root 1.58 v.n = vec3 (0, 1, 0);
518 root 1.54 }
519    
520 root 1.56 tree->update_normals ();
521    
522 root 1.54 tree->vb.set (&tree->vd[0][0], SIZE * SIZE);
523    
524     bbox.reset ();
525     bbox.add (vec3 (0, 0, 0));
526     bbox.add (vec3 (sx, tree->hvar, sy));
527 root 1.52 }
528    
529     void geometry_heightfield::update ()
530     {
531     geometry::update ();
532     }
533    
534 root 1.58 static int maxcnt, thiscnt;
535     static GLfloat mine;
536 root 1.57
537 root 1.52 void geometry_heightfield::draw (view &ctx)
538     {
539 root 1.62 testmat->enable (ctx);
540 root 1.57 maxcnt = 10;
541 root 1.58 thiscnt = 0;
542     mine = 1e38;
543 root 1.56 tree->draw (ctx);
544 root 1.62 testmat->disable (ctx);
545 root 1.59 //printf (" TC %d mine %f\n", thiscnt, mine);
546 root 1.56 }
547    
548     void geometry_heightfield::node::update_normals ()
549     {
550 root 1.58 GLfloat az = 0.F;
551    
552 root 1.56 for (int x = 0; x < SIZE; x++)
553     for (int y = 0; y < SIZE; y++)
554     {
555     vertex_t2f_n3f_v3f &v = vd[y][x];
556    
557     if (x && y)
558     v.n = normalize (cross (vd[y-1][x].v - v.v, vd[y][x-1].v - v.v));
559 root 1.58
560     az += v.v.y;
561 root 1.56 }
562 root 1.58
563     z = az / (SIZE * SIZE);
564 root 1.56 }
565    
566     void geometry_heightfield::node::draw (view &ctx)
567     {
568 root 1.58 vec3 center = vec3 (ctx.eorig) + vec3 (x + e * .5F, z, y + e * .5F);
569     GLfloat d = length (center);
570 root 1.57
571     if (!overlap (ctx.frustum.c, sphere (center, e * 2)))
572     return;
573    
574 root 1.58 if (d > e * 2.F)
575 root 1.55 {
576 root 1.58 if (mine > e) mine = e; thiscnt++;
577 root 1.56 vb.bind ();
578 root 1.55 ib.draw (GL_TRIANGLES, 0, icnt);
579 root 1.56 }
580     else
581     {
582     for (int i = 0; i < 4; i++)
583     {
584     if (!sub[i])
585     {
586 root 1.57 if (!--maxcnt)
587     {
588     vb.bind ();
589     ib.draw (GL_TRIANGLES, 0, icnt);
590     }
591    
592 root 1.56 node *n = sub[i] = new node;
593    
594 root 1.57 n->hvar = hvar * 0.5;
595 root 1.56 n->x = x + (i & 1 ? e * .5F : 0.F);
596     n->y = y + (i & 2 ? e * .5F : 0.F);
597     n->e = e * .5F;
598    
599     for (int x = 0; x < SIZE; x++)
600     for (int y = 0; y < SIZE; y++)
601     n->vd[y][x] = vd[(y >> 1) + (i & 2 ? SIZE / 2 : 0)][(x >> 1) + (i & 1 ? SIZE / 2 : 0)];
602    
603 root 1.58 for (int x = 1; x < SIZE; x += 2)
604     for (int y = 0; y < SIZE; y += 2)
605     {
606     n->vd[y][x].t = (n->vd[y][x-1].t + n->vd[y][x+1].t) * 0.5F;
607     n->vd[y][x].v = (n->vd[y][x-1].v + n->vd[y][x+1].v) * 0.5F;
608 root 1.60 if (y && y < SIZE - 1)
609     n->vd[y][x].v.y += n->hvar * (GLfloat)rand () / RAND_MAX - n->hvar * .5F;
610 root 1.58 }
611 root 1.57
612 root 1.58 for (int x = 0; x < SIZE; x++)
613     for (int y = 1; y < SIZE; y += 2)
614 root 1.56 {
615 root 1.58 n->vd[y][x].t = (n->vd[y-1][x].t + n->vd[y+1][x].t) * 0.5F;
616 root 1.57 n->vd[y][x].v = (n->vd[y-1][x].v + n->vd[y+1][x].v) * 0.5F;
617 root 1.60 if (x && x < SIZE - 1)
618     n->vd[y][x].v.y += n->hvar * (GLfloat)rand () / RAND_MAX - n->hvar * .5F;
619 root 1.56 }
620    
621     n->update_normals ();
622    
623     n->vb.set (&n->vd[0][0], SIZE * SIZE);
624     }
625    
626     sub[i]->draw (ctx);
627     }
628 root 1.55 }
629 root 1.52 }
630    
631 root 1.66 void
632     entity_moveable::perform_step (double t)
633     {
634     vec3 vel = t * v;
635     move (vel);
636     }
637 root 1.68
638     /////////////////////////////////////////////////////////////////////////////
639    
640     skybox::skybox (
641     const char *left,
642     const char *front,
643     const char *right,
644     const char *back,
645     const char *top,
646     const char *bottom
647     )
648     {
649     tex [0] = new texture (left , texture::CLAMP);
650     tex [1] = new texture (front , texture::CLAMP);
651     tex [2] = new texture (right , texture::CLAMP);
652     tex [3] = new texture (back , texture::CLAMP);
653     tex [4] = new texture (top , texture::CLAMP);
654     tex [5] = new texture (bottom, texture::CLAMP);
655     }
656    
657     skybox::~skybox ()
658     {
659     for (int i = 6; i--; )
660     delete tex [i];
661     }
662    
663     void
664     skybox::draw (view &ctx)
665     {
666    
667     float x = ctx.p.x;
668     float y = ctx.p.y;
669     float z = ctx.p.z;
670    
671     float width = 1000.;
672     float height = 1000.;
673     float length = 1000.;
674    
675     x -= width * .5F;
676     y -= height * .5F;
677     z -= length * .5F;
678    
679     static skybox_material sm;
680    
681     sm.enable (ctx);
682    
683     // Draw Left side
684     sm.tex->name = tex [0]->name;
685     sm.tex->enable ();
686     glBegin (GL_QUADS);
687     glTexCoord2f (1.0f, 0.0f); glVertex3f (x, y + height, z);
688     glTexCoord2f (0.0f, 0.0f); glVertex3f (x, y + height, z + length);
689     glTexCoord2f (0.0f, 1.0f); glVertex3f (x, y, z + length);
690     glTexCoord2f (1.0f, 1.0f); glVertex3f (x, y, z);
691     glEnd ();
692     sm.tex->disable ();
693    
694     // Draw Front side
695     sm.tex->name = tex [1]->name;
696     sm.tex->enable ();
697     glBegin (GL_QUADS);
698     glTexCoord2f (1.0f, 1.0f); glVertex3f (x + width, y, z);
699     glTexCoord2f (1.0f, 0.0f); glVertex3f (x + width, y + height, z);
700     glTexCoord2f (0.0f, 0.0f); glVertex3f (x, y + height, z);
701     glTexCoord2f (0.0f, 1.0f); glVertex3f (x, y, z);
702     glEnd ();
703     sm.tex->disable ();
704    
705     // Draw Right side
706     sm.tex->name = tex [2]->name;
707     sm.tex->enable ();
708     glBegin (GL_QUADS);
709     glTexCoord2f (0.0f, 1.0f); glVertex3f (x + width, y, z);
710     glTexCoord2f (1.0f, 1.0f); glVertex3f (x + width, y, z + length);
711     glTexCoord2f (1.0f, 0.0f); glVertex3f (x + width, y + height, z + length);
712     glTexCoord2f (0.0f, 0.0f); glVertex3f (x + width, y + height, z);
713     glEnd ();
714     sm.tex->disable ();
715    
716     // Draw Back side
717     sm.tex->name = tex [3]->name;
718     sm.tex->enable ();
719     glBegin (GL_QUADS);
720     glTexCoord2f (1.0f, 1.0f); glVertex3f (x, y, z + length);
721     glTexCoord2f (1.0f, 0.0f); glVertex3f (x, y + height, z + length);
722     glTexCoord2f (0.0f, 0.0f); glVertex3f (x + width, y + height, z + length);
723     glTexCoord2f (0.0f, 1.0f); glVertex3f (x + width, y, z + length);
724     glEnd ();
725     sm.tex->disable ();
726    
727     // Draw Up side
728     sm.tex->name = tex [4]->name;
729     sm.tex->enable ();
730     glBegin (GL_QUADS);
731     glTexCoord2f (1.0f, 0.0f); glVertex3f (x, y, z);
732     glTexCoord2f (0.0f, 0.0f); glVertex3f (x, y, z + length);
733     glTexCoord2f (0.0f, 1.0f); glVertex3f (x + width, y, z + length);
734     glTexCoord2f (1.0f, 1.0f); glVertex3f (x + width, y, z);
735     glEnd ();
736     sm.tex->disable ();
737    
738     // Draw Down side
739     sm.tex->name = tex [5]->name;
740     sm.tex->enable ();
741     glBegin (GL_QUADS);
742     glTexCoord2f (1.0f, 0.0f); glVertex3f (x + width, y + height, z);
743     glTexCoord2f (0.0f, 0.0f); glVertex3f (x + width, y + height, z + length);
744     glTexCoord2f (0.0f, 1.0f); glVertex3f (x, y + height, z + length);
745     glTexCoord2f (1.0f, 1.0f); glVertex3f (x, y + height, z);
746     glEnd ();
747     sm.tex->disable ();
748    
749     sm.disable (ctx);
750     }
751