/* render/draw.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "core/core.h" #include "core/model.h" #include "render/render.h" #include "render/draw.h" namespace render { size_t Stats::tris = 0; size_t Stats::quads = 0; void Stats::clear() { tris = 0; quads = 0; } math::Vector3f v0(1, -1, 1); math::Vector3f v1(1, 1, 1); math::Vector3f v2(-1, 1, 1); math::Vector3f v3(-1, -1, 1); math::Vector3f v4(1, -1, -1); math::Vector3f v5(1, 1, -1); math::Vector3f v6(-1, 1, -1); math::Vector3f v7(-1, -1, -1); const float drawdistance = 128.0f; const float drawfxdistance = 32.0f; math::Vector3f camera_target; float angle = 0; /* ----- Distance test functions ----------------------------------- */ inline bool test_draw_distance(core::Entity *entity) { return (entity->model() && (math::distancesquared(camera_target, entity->location()) <= (drawdistance*drawdistance*entity->model()->radius()))); } inline bool test_drawfx_distance(core::Entity *entity) { return (entity->model() && (math::distancesquared(camera_target, entity->location()) <= (drawfxdistance*drawfxdistance*entity->model()->radius()))); } /* ----- Default Entity shapes ------------------------------------- */ void draw_sphere(math::Color const & color, float radius) { //gl::push(); gl::scale(radius, radius, radius); gl::color(color); size_t index = 0; size_t count = (core::SPHERESEGMENTS+1)*2; // draw body for (int j=0; j < core::SPHERESEGMENTS-1; j++) { glDrawArrays(gl::QuadStrip, index, count); index += count; Stats::quads += count/2-1; } //gl::pop(); } void draw_entity_sphere(core::Entity *entity) { draw_sphere(entity->color(), entity->radius()); } void draw_entity_cube(core::Entity *entity) { float radius = entity->radius()/2; gl::scale(radius, radius, radius); gl::color(entity->color()); gl::begin(gl::Quads); // top gl::normal(0,0,1); gl::vertex(v0); gl::vertex(v1); gl::vertex(v2); gl::vertex(v3); // bottom gl::normal(0,0, -1); gl::vertex(v7); gl::vertex(v6); gl::vertex(v5); gl::vertex(v4); // sides gl::normal(1,0,0); gl::vertex(v1); gl::vertex(v0); gl::vertex(v4); gl::vertex(v5); gl::normal(-1,0,0); gl::vertex(v3); gl::vertex(v2); gl::vertex(v6); gl::vertex(v7); gl::normal(0,1,0); gl::vertex(v2); gl::vertex(v1); gl::vertex(v5); gl::vertex(v6); gl::normal(0,-1,0); gl::vertex(v0); gl::vertex(v3); gl::vertex(v7); gl::vertex(v4); gl::end(); } void draw_entity_axis(core::Entity *entity) { using namespace render; float r = entity->radius(); gl::begin(gl::Lines); gl::color(1.0f, 0.0f, 0.0f); gl::vertex(r,0.0f,0.0f); gl::color(entity->color()); gl::vertex(-r,0.0f,0.0f); gl::vertex(0.0f,r/2,0.0f); gl::vertex(0.0f,-r/2,0.0f); gl::vertex(0.0f,0.0f,r); gl::vertex(0.0f,0.0f,-r); gl::end(); } /* ----- Entity models --------------------------------------------- */ void draw_model_vertex(core::Entity *entity) { size_t count = entity->model()->vertex_structural(); if (test_drawfx_distance(entity)) count += entity->model()->vertex_detail(); // draw model vertices if (count) { size_t index = entity->model()->first_vertex(); glDrawArrays(gl::Triangles, index, count); Stats::tris += count/3; } } void draw_model_evertex(core::Entity *entity) { size_t count = entity->model()->evertex_structural(); if (test_drawfx_distance(entity)) count += entity->model()->evertex_detail(); // draw model evertices if (count) { size_t index = entity->model()->first_evertex(); render::gl::color(entity->color()); glDrawArrays(gl::Triangles, index, count); Stats::tris += count/3; } } void draw_model_lights(core::Entity *entity) { core::Model *model = entity->model(); if (model->model_light.size()) { glPointSize(10); gl::begin(gl::Points); for (std::list::iterator lit = model->model_light.begin(); lit != model->model_light.end(); lit++) { if (!(*lit)->strobe() || (( core::application()->time() - floorf(core::application()->time()) ) < 0.5f)) { math::Vector3f location = (*lit)->location(); gl::color((*lit)->color()); gl::vertex(location); } } gl::end(); glPointSize(1); } } void draw_model_engines(core::EntityControlable *entity) { core::Model *model = entity->model(); if (model->model_engine.size() && entity->thrust()) { gl::color(1.0f, 0.0f ,0.0f, 1.0f); gl::begin(gl::Lines); for (std::list::iterator eit = model->model_engine.begin(); eit != model->model_engine.end(); eit++) { math::Vector3f const & v = (*eit)->location(); gl::vertex(v); gl::vertex(v.x - 0.0625f*entity->thrust(), v.y, v.z); } gl::end(); } } void draw_model_radius(core::Entity *entity) { math::Color color = entity->color(); color.a = 0.25f; draw_sphere(color, entity->model()->radius()); } void draw_model_shield(core::EntityControlable *entity) { // shield rotation //gl::push(); gl::rotate(angle, 0.0f, 0.0f, 1.0f ); gl::scale(entity->model()->radius(), entity->model()->radius(), entity->model()->radius()); // draw the shield gl::color(math::Color(0.0f, 1.0f ,0.0f)); gl::begin(gl::LineLoop); // gl::normal(0, 0.5, 0.5); gl::vertex(v1); // gl::normal(0, -0.5, 0.5); gl::vertex(v0); // gl::normal(0, -0.5, -0.5); gl::vertex(v4); // gl::normal(0, 0.5, -0.5); gl::vertex(v5); gl::end(); gl::begin(gl::LineLoop); // gl::normal(0, -0.5, 0.5); gl::vertex(v3); // gl::normal(0, 0.5, 0.5); gl::vertex(v2); // gl::normal(0, 0.5, -0.5); gl::vertex(v6); // gl::normal(0, -0.5, -0.5); gl::vertex(v7); gl::end(); //gl::pop(); } /* ----- Render passes --------------------------------------------- */ /* Draw entities without model */ void draw_pass_default() { std::map::iterator it; for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { core::Entity *entity = (*it).second; // draw entities without model if (!entity->model()) { gl::push(); gl::translate(entity->location()); // FIXME //gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); gl::multmatrix(entity->axis()); if ((entity->flags() & core::Entity::Bright) == core::Entity::Bright) gl::disable(GL_LIGHTING); switch(entity->shape()) { case core::Entity::Sphere: draw_entity_sphere(entity); break; case core::Entity::Diamond: case core::Entity::Axis: draw_entity_axis(entity); break; case core::Entity::Cube: default: draw_entity_cube(entity); break; } if ((entity->flags() & core::Entity::Bright) == core::Entity::Bright) gl::enable(GL_LIGHTING); gl::pop(); } } } /* Draw model vertices*/ void draw_pass_model_vertex() { std::map::iterator it; for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { core::Entity *entity = (*it).second; // load entity models if necessary if (!entity->model() && entity->modelname().size()) { entity->entity_model = core::Model::get(entity->modelname()); if (!entity->model()) entity->entity_modelname.clear(); } if (test_draw_distance(entity)) { gl::push(); gl::translate(entity->location()); // FIXME //gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); gl::multmatrix(entity->axis()); draw_model_vertex(entity); gl::pop(); } } } /* Draw entites with model evertices */ void draw_pass_model_evertex() { std::map::iterator it; for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { core::Entity *entity = (*it).second; if (test_draw_distance(entity)) { gl::push(); gl::translate(entity->location()); // FIXME //gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); gl::multmatrix(entity->axis()); draw_model_evertex(entity); gl::pop(); } } } /* Draw model lights, engines */ void draw_pass_model_fx() { for (std::map::iterator it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { core::Entity *entity = (*it).second; if (test_drawfx_distance(entity)) { gl::push(); gl::translate(entity->location()); // FIXME //gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); gl::multmatrix(entity->axis()); draw_model_lights(entity); if (entity->type() == core::Entity::Controlable) { draw_model_engines((core::EntityControlable *)entity); draw_model_shield((core::EntityControlable *)entity); } gl::pop(); } } } void draw_pass_model_radius() { if (!(r_radius && r_radius->value())) return; for (std::map::iterator it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { core::Entity *entity = (*it).second; if (test_draw_distance(entity)) { gl::push(); gl::translate(entity->location()); draw_model_radius(entity); gl::pop(); } } } void draw_pass_spacegrid() { int gridsize = 32; float s = 1.0f / gridsize; float z = -4.0f; float dx = camera_target.x - floorf(camera_target.x); float dy = camera_target.y - floorf(camera_target.y); gl::push(); gl::translate(camera_target); gl::color(0,0, 1.0f); gl::normal(0, 0, 1.0f); gl::begin(gl::Lines); for (int i=-gridsize; i <= gridsize; i++) { gl::color(0,0, 0, 0); gl::vertex(i-dx, -gridsize-dy, z); gl::color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s); gl::vertex(i-dx, -dy, z ); gl::vertex(i-dx, -dy ,z ); gl::color(0,0, 0, 0); gl::vertex(i-dx, gridsize-dy, z); gl::vertex(-gridsize-dx, i-dy, z ); gl::color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s); gl::vertex(-dx, i-dy, z); gl::vertex(-dx, i-dy, z); gl::color(0,0, 0, 0); gl::vertex(gridsize-dx, i-dy, z); } gl::end(); gl::pop(); } void draw_local_axis() { if (!core::localcontrol()) return; gl::push(); gl::translate(core::localcontrol()->location()); gl::begin(gl::Lines); gl::color(0.5f, 0.0f,0.0f); gl::vertex(core::localcontrol()->axis()[0] * -1.0f); gl::color(1.0f, 0.0f, 0.0f); gl::vertex(core::localcontrol()->axis()[0]); gl::color(0.5f, 0.5f,0.0f); gl::vertex(core::localcontrol()->axis()[1] * -1.0f); gl::color(1.0f, 1.0f, 0.0f); gl::vertex(0.0f, 0.0f, 0.0f); gl::color(0.5f, 0.5f,0.0f); gl::vertex(core::localcontrol()->axis()[1]); gl::color(1.0f, 1.0f, 0.0f); gl::vertex(0.0f, 0.0f, 0.0f); gl::color(0.0f, 0.5f,0.0f); gl::vertex(core::localcontrol()->axis()[2] * -1.0f); gl::color(0.0f, 1.0f, 0.0f); gl::vertex(0.0f, 0.0f, 0.0f); gl::color(0.0f, 0.5f,0.0f); gl::vertex(core::localcontrol()->axis()[2]); gl::color(0.0f, 1.0f, 0.0f); gl::vertex(0.0f, 0.0f, 0.0f); gl::pop(); } /* ----- Main draw routine ----------------------------------------- */ void draw(math::Vector3f const &eye, math::Vector3f const &target, float seconds) { Stats::clear(); // used for animations angle += 180.0f * seconds; if( angle > 360.0f ) { angle -= 360.0f; } camera_target = target; gl::enable(GL_DEPTH_TEST); // enable depth buffer writing gl::enable(GL_CULL_FACE); // enable culling gl::enable(GL_COLOR_MATERIAL); // enable color tracking gl::enable(GL_LIGHTING); gl::disable(GL_BLEND); // disbable alpha blending for world polys gl::disable(GL_RESCALE_NORMAL); glVertexPointer(3, GL_FLOAT, 0, vertexarray->vertex()); glNormalPointer(GL_FLOAT, 0, vertexarray->normal()); glColorPointer(3, GL_FLOAT, 0, vertexarray->color()); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_COLOR_ARRAY); if (r_wireframe && r_wireframe->value()) { glPolygonMode(GL_FRONT, GL_LINE); } else { glPolygonMode(GL_FRONT, GL_FILL); } draw_pass_model_vertex(); // draw entities with model glDisableClientState(GL_COLOR_ARRAY); gl::enable(GL_RESCALE_NORMAL); // rescale normals by the transformation matrix scale factor draw_pass_default(); // draw entities without model gl::disable(GL_RESCALE_NORMAL); draw_pass_model_evertex(); // draw entities with model, vertices with entity color gl::disable(GL_LIGHTING); gl::enable(GL_BLEND); draw_pass_model_fx(); // draw entity lights and engines gl::enable(GL_LIGHTING); gl::enable(GL_RESCALE_NORMAL); draw_pass_model_radius(); //draw entity radius glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); gl::disable(GL_RESCALE_NORMAL); gl::disable(GL_LIGHTING); gl::disable(GL_COLOR_MATERIAL); // disable color tracking gl::disable(GL_CULL_FACE); // disable culling draw_local_axis(); draw_pass_spacegrid(); // draw the blue spacegrid gl::disable(GL_DEPTH_TEST); // disable depth buffer writing // GL_BLEND must be enabled for the GUI } }