#include #include using namespace std; #include "entity.h" #include "oct.h" void entity_base::update_bbox () { if (parent) parent->update_bbox (); } void entity_base::hide () { for (vector::iterator i = o.end (); i-- != o.begin (); ) (*i)->remove (this); o.clear (); } void entity_filter::update_bbox () { bbox = e->bbox; entity_base::update_bbox (); } void entity_filter::show (const sector &sec) { e->show (sec); } void entity_filter::draw (const draw_context &ctx) { e->draw (ctx); } entity_filter::~entity_filter () { delete e; } void entity::show (const sector &sec) { show (); } void entity::show () { e->show (this->sec); } void entity::draw (const draw_context &ctx) { glPushMatrix (); glTranslated (-sec.x, -sec.y, -sec.z); e->draw (ctx); glPopMatrix (); } void entity_container::update_bbox () { bbox.reset (); for (iterator i = end (); i-- != begin (); ) { (*i)->update_bbox (); bbox.add ((*i)->bbox); } } void entity_container::show (const sector &sec) { for (iterator i = end (); i-- != begin (); ) (*i)->show (sec); } void entity_container::draw (const draw_context &ctx) { for (iterator i = end (); i-- != begin (); ) (*i)->draw (ctx); } entity_container::~entity_container () { hide (); for (iterator i = end (); i-- != begin (); ) delete *i; clear (); } template entity_opengl1d; template entity_opengl1d; template entity_opengl1d; template entity_opengl1d; template entity_opengl2d; template entity_opengl2d; template entity_opengl2d; template entity_opengl2d; template entity_opengl2d; template entity_opengl2d; template void entity_opengl1d::update_bbox () { //for (vector::iterator i = end (); i-- != begin (); ) entity_base::update_bbox (); } template void entity_opengl2d::update_bbox () { entity_base::update_bbox (); } template void entity_opengl1d::draw (const draw_context &ctx) { glBegin (type); for (iterator i = begin (); i < end (); ++i) { if (ctx.mode == draw_context::LIGHTED) glColor3fv ((GLfloat *)&i->c); glVertex3fv ((GLfloat *)&i->p); } glEnd (); } template void entity_opengl2d::draw (const draw_context &ctx) { glBegin (type); if (ctx.mode == draw_context::LIGHTED) { glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular); glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission); glMaterialf (GL_FRONT, GL_SHININESS, m.shininess); } for (iterator i = begin (); i < end (); ++i) { if (ctx.mode == draw_context::LIGHTED) { glTexCoord2fv ((GLfloat *)&i->t); glNormal3fv ((GLfloat *)&i->n); } glVertex3fv ((GLfloat *)&i->p); } glEnd (); } template void entity_opengl1d::show (const sector &sec) { } template void entity_opengl2d::show (const sector &sec) { } void view::draw (const draw_context &ctx) { glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); double aspect = (double)w/h; double zNear = 0.0001; double zFar = 1000.; { GLdouble xmin, xmax, ymin, ymax; ymax = zNear * tan (fov * (M_PI / 360.0)); ymin = -ymax; xmin = ymin * aspect; xmax = ymax * aspect; glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); } vec3 rz = -d; vec3 rx = cross (u, rz); vec3 ry = cross (rz, rx); GLfloat m[4][4]; m[0][0] = rx.x; m[0][1] = rx.y; m[0][2] = rx.z; m[0][3] = 0; m[1][0] = ry.x; m[1][1] = ry.y; m[1][2] = ry.z; m[1][3] = 0; m[2][0] = rz.x; m[2][1] = rz.y; m[2][2] = rz.z; m[2][3] = 0; m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1; glMultMatrixf ((GLfloat *)m); glTranslatef (-p.x, -p.y, -p.z); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); world.draw (ctx); }