#include #include using namespace std; #include "opengl.h" #include "util.h" #include "entity.h" #include "oct.h" #include "view.h" ///////////////////////////////////////////////////////////////////////////// geometry::~geometry () { } template class geometry_opengl1d; template class geometry_opengl1d; template class geometry_opengl1d; template class geometry_opengl1d; template class geometry_opengl2d; template class geometry_opengl2d; template class geometry_opengl2d; template class geometry_opengl2d; template class geometry_opengl2d; template class geometry_opengl2d; geometry_opengl::geometry_opengl () { list = glGenLists (1); } geometry_opengl::~geometry_opengl () { glDeleteLists (list, 1); } template void geometry_opengl1d::set (const vector &v) { clear (); insert (end (), v.begin (), v.end ()); bbox.reset (); for (const_iterator i = end (); i-- != begin (); ) bbox.add (i->p); update (); } template void geometry_opengl1d::draw (view &ctx) { glBegin (type); for (iterator i = begin (); i < end (); ++i) { glColor3fv ((GLfloat *)&i->c); glVertex3fv ((GLfloat *)&i->p); } glEnd (); } template void geometry_opengl2d::set (const vector &v) { bbox.reset (); for (vector::const_iterator i = v.end (); i-- != v.begin (); ) bbox.add (i->p); update (); glNewList (list, GL_COMPILE); m->begin (); #if 0 glBegin (type); for (vector::const_iterator i = v.begin (); i < v.end (); ++i) { glTexCoord2fv ((GLfloat *)&i->t); glNormal3fv ((GLfloat *)&i->n); glVertex3fv ((GLfloat *)&i->p); } glEnd (); #else glEnableClientState (GL_VERTEX_ARRAY); glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->p); glEnableClientState (GL_NORMAL_ARRAY); glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->n); glEnableClientState (GL_TEXTURE_COORD_ARRAY); glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->t); glDrawArrays (type, 0, v.size ()); #endif m->end (); glEndList (); } template void geometry_opengl2d::draw (view &ctx) { glCallList (list); } void geometry_sphere::update () { bbox.reset (); bbox.add (-point (radius, radius, radius)); bbox.add ( point (radius, radius, radius)); geometry::update (); } void geometry_sphere::draw (view &ctx) { material *m = new simple_material; int n = max (8, (int)(ctx.pixfact * radius) / 10); m->begin (); GLUquadric *quad = gluNewQuadric (); gluQuadricTexture (quad, true); gluSphere (quad, radius, n, n); gluDeleteQuadric (quad); m->end (); delete m; } ///////////////////////////////////////////////////////////////////////////// void geometry_transform::update () { const box &sub = g->bbox; bbox.reset (); bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z)); bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z)); bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z)); bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z)); bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z)); bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z)); bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z)); bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z)); geometry::update (); } #if 0 void geometry_transform::renormalize () { point trans(m(0,3), m(1,3), m(2,3)); ::renormalize (e->orig, trans); m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z; } #endif void geometry_transform::update (const matrix &xfrm) { m = m * xfrm; update (); } void geometry_transform::set_matrix (const matrix &xfrm) { m = xfrm; update (); } void geometry_transform::draw (view &ctx) { glPushMatrix (); glMultMatrixf ((GLfloat *)&m); g->draw (ctx); glPopMatrix (); } void geometry_anim::draw (view &ctx) { matrix save_m = m; update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0)) * matrix::rotation (vy * timer.now, vec3 (0, 1, 0)) * matrix::rotation (vz * timer.now, vec3 (0, 0, 1))); geometry_transform::draw (ctx); m = save_m; } ///////////////////////////////////////////////////////////////////////////// void geometry_filter::set (geometry *g) { this->g = g; if (g) g->parent = this; update (); } void geometry_filter::update () { if (g) { bbox = g->bbox; geometry::update (); } } void geometry_filter::draw (view &ctx) { g->draw (ctx); } geometry_filter::~geometry_filter () { delete g; } ///////////////////////////////////////////////////////////////////////////// void geometry_container::update () { bbox.reset (); for (iterator i = end (); i-- != begin (); ) bbox.add ((*i)->bbox); geometry::update (); } void geometry_container::add (geometry *g) { push_back (g); g->parent = this; update (); } void geometry_container::draw (view &ctx) { for (iterator i = end (); i-- != begin (); ) (*i)->draw (ctx); } geometry_container::~geometry_container () { for (iterator i = end (); i-- != begin (); ) delete *i; clear (); } ///////////////////////////////////////////////////////////////////////////// entity::entity (geometry *g) : geometry_filter(g) , p(0,0,0) { update (); } entity::~entity () { hide (); } void entity::move (const vec3 &v) { p = p + v; //renormalize (orig, p); update (); } void entity::show () { if (!o.size ()) world.add (this); } void entity::hide () { for (vector::iterator i = o.end (); i-- != o.begin (); ) (*i)->remove (this); o.clear (); } void entity::update () { if (!g) return; bbox = g->bbox; a.x = orig.x + (soffs)floorf (bbox.a.x + p.x); a.y = orig.y + (soffs)floorf (bbox.a.y + p.y); a.z = orig.z + (soffs)floorf (bbox.a.z + p.z); b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x); b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y); b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z); if (o.size ()) { hide (); show (); } } void entity::draw (view &ctx) { sector diff = orig - ctx.orig; glPushMatrix (); glTranslatef (diff.x + p.x, diff.y + p.y, diff.z + p.z); g->draw (ctx); glPopMatrix (); } void entity::display (view &ctx) { if (ctx.may_draw (this)) draw (ctx); } /////////////////////////////////////////////////////////////////////////// // static void nurbs_error (GLenum errorCode) { const GLubyte *estring; estring = gluErrorString(errorCode); fprintf (stderr, "Nurbs error: %s\n", estring); } void errorCallback(GLenum errorCode) { const GLubyte *estring; estring = gluErrorString(errorCode); fprintf (stderr, "Tessellation Error: %s\n", estring); exit (0); } void tcbBegin (GLenum prim) { glBegin (prim); } void tcbVertex (void *data) { glVertex3fv ((GLfloat *)data); } void tcbEnd () { glEnd (); } void geometry_nurbs::set () { // < XXX >: Testing CODE int u, v; for (u = 0; u < 4; u++) { for (v = 0; v < 4; v++) { ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5); ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5); if ( (u == 1 || u == 2) && (v == 1 || v == 2)) ctlpoints[u][v][2] = 3.0; else ctlpoints[u][v][2] = -3.0; } } tess = gluNewTess(); // glEnable(GL_AUTO_NORMAL); nurb = gluNewNurbsRenderer (); gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_FALSE); gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_FILL); gluNurbsProperty (nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR); gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_OBJECT_PATH_LENGTH); gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 1.0); gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error); gluNurbsCallback (nurb, GLU_NURBS_BEGIN, (GLvoid(*)()) tcbBegin); gluNurbsCallback (nurb, GLU_NURBS_VERTEX,(GLvoid(*)()) tcbVertex); gluNurbsCallback (nurb, GLU_NURBS_END, tcbEnd); glDisable(GL_AUTO_NORMAL); } void geometry_nurbs::draw (view &ctx) { GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0}; GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 100.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glEnable(GL_AUTO_NORMAL); gluBeginSurface (nurb); gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3); gluEndSurface (nurb); /* glEnable(GL_AUTO_NORMAL); glPushMatrix(); GL_LG_DEBUG; ======= gluEndSurface (nurb); >>>>>>> 1.40 glDisable(GL_AUTO_NORMAL); glPopMatrix(); glFlush(); */ }