#include #include using namespace std; #include "entity.h" const vec3 cross (const vec3 &a, const vec3 &b) { return vec3 ( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x ); } GLfloat dot (const vec3 &a, const vec3 &b) { return a.x * b.x + a.y * b.y + a.z * b.z; } void box::add (const box &o) { a.x = min (a.x, o.a.x); a.y = min (a.y, o.a.y); a.z = min (a.z, o.a.z); b.x = max (b.x, o.b.x); b.y = max (b.y, o.b.y); b.z = max (b.z, o.b.z); } 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 () { e->draw (); } entity_filter::~entity_filter () { delete e; } void entity::show (const sector &sec) { show (); } void entity::show () { e->show (this->sec); } void entity::draw () { glPushMatrix (); //TODO setup matrix etc. e->draw (); 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 () { for (iterator i = end (); i-- != begin (); ) (*i)->draw (); } 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 () { entity_base::update_bbox (); } template void entity_opengl2d::update_bbox () { entity_base::update_bbox (); } template void entity_opengl1d::draw () { glBegin (type); for (iterator i = begin (); i < end (); ++i) { glColor3fv ((GLfloat *)&i->c); glVertex3fv ((GLfloat *)&i->p); } glEnd (); } template void entity_opengl2d::draw () { glBegin (type); for (iterator i = begin (); i < end (); ++i) { glColor3fv ((GLfloat *)&i->c); glNormal3fv ((GLfloat *)&i->n); glTexCoord2fv ((GLfloat *)&i->t); 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 &c) { glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); double aspect = (double)w/h; double zNear = 5; 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 (); entity *e = new entity; e->sec.x = e->sec.y = e->sec.z = 0; entity_triangles *tri = new entity_triangles; tri->push_back (vertex2d (colour (1, 0, 0), point (-9, 0, -9), vec3 (0, 0, 1))); tri->push_back (vertex2d (colour (0, 1, 1), point ( 0, 9, -9), vec3 (0, 0, 1))); tri->push_back (vertex2d (colour (1, 0, 1), point ( 9, 0, -9), vec3 (0, 0, 1))); e->set (tri); e->show (); e->draw (); delete e; }