/* client/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 "render/render.h" #include "render/sphere.h" #include "render/box.h" #include "client/client.h" #include "client/camera.h" #include "client/draw.h" namespace client { render::Sphere sphere(math::Vector3f(0,0,0),1); render::Box cube(math::Vector3f(0.5f, 0.5f, 0.5f), math::Vector3f(-0.5f, -0.5f, -0.5f)); void draw_entity_sphere(core::Entity *entity) { render::gl::color(entity->color()); sphere.radius = entity->radius(); sphere.draw(); } void draw_entity_cube(core::Entity *entity) { cube.topcolor = entity->color(); cube.bottomcolor = entity->color(); cube.radius = entity->radius(); cube.draw(); } void draw_entity_diamond(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,0.0f,r/2); gl::vertex(0.0f,0.0f,-r/2); gl::vertex(0.0f,r,0.0f); gl::vertex(0.0f,-r, 0.0f); gl::end(); } // draw an entity of entity_type core::Entity::Default void draw_entity_default(core::Entity *entity) { render::gl::push(); render::gl::translate(entity->location()); switch(entity->shape()) { case core::Entity::Sphere: draw_entity_sphere(entity); break; case core::Entity::Diamond: draw_entity_diamond(entity); break; case core::Entity::Cube: default: draw_entity_cube(entity); break; } render::gl::pop(); } math::Vector3f v0(1.0f, -1.0f, -1.0f); math::Vector3f v1(1.0f, 1.0f, -1.0f); math::Vector3f v2(1.0f, 1.0f, 1.0f); math::Vector3f v3(1.0f, -1.0f, 1.0f); math::Vector3f v4(-1.0f, -1.0f, -1.0f); math::Vector3f v5(-1.0f, 1.0f, -1.0f); math::Vector3f v6(-1.0f, 1.0f, 1.0f); math::Vector3f v7(-1.0f, -1.0f, 1.0f); float angle = 0; void draw_ship(core::EntityControlable *entity) { using math::Vector3f; using math::Color; using namespace render; gl::push(); gl::translate(entity->location()); gl::scale(0.2f, 0.2f, 0.2f); gl::rotate(entity->direction(), 0.0f, 1.0f, 0.0f ); Vector3f tl(0.25, 0.125, 0.125); Vector3f br(-0.25, -0.125, -0.125); Box box(tl, br); box.draw(); tl = Vector3f(0, 0.07, 0.25); br = Vector3f(-0.5, -0.07, 0.125); Box engine1(tl, br); engine1.topcolor = Color(0.7, 0.7, 0.7); engine1.bottomcolor = engine1.topcolor * 0.5; engine1.draw(); tl = Vector3f(0, 0.07, -0.125); br = Vector3f(-0.5, -0.07, -0.25); Box engine2(tl, br); engine2.topcolor = engine1.topcolor; engine2.bottomcolor = engine1.bottomcolor; engine2.draw(); tl = Vector3f(0.4, 0.07, 0.07); br = Vector3f(0.25, -0.07, -0.07); Box cockpit(tl, br); cockpit.topcolor = engine1.topcolor; cockpit.bottomcolor = engine1.bottomcolor; cockpit.draw(); if(entity->thrust() > 0 ) { gl::color(1.0f,0 ,0 ); gl::begin(gl::Lines); gl::vertex(-0.5f, 0, 0.185); gl::vertex(-0.5f-0.25f*entity->thrust(), 0, 0.185); gl::vertex(-0.5f, 0, -0.185f); gl::vertex(-0.5f-0.25f*entity->thrust(), 0, -0.185f); gl::end(); } // shield rotation gl::rotate(angle, 0.0f, 1.0f, 0.0f ); // draw the shield gl::color(Color(0.0f, 1.0f ,0.0f , 0.5f)); gl::begin(gl::LineStrip); gl::vertex(v0); gl::vertex(v1); gl::vertex(v2); gl::vertex(v3); gl::vertex(v0); gl::end(); gl::begin(gl::LineStrip); gl::vertex(v4); gl::vertex(v5); gl::vertex(v6); gl::vertex(v7); gl::vertex(v4); gl::end(); gl::pop(); } void draw_spacegrid() { using namespace render::gl; translate(camera::target); int gridsize = 32; float s = 1.0f / gridsize; float y = -4.0f; float dx = camera::target.x - floorf(camera::target.x); float dz = camera::target.z - floorf(camera::target.z); color(0,0, 1.0f); begin(Lines); for (int i=-gridsize; i <= gridsize; i++) { color(0,0, 0, 0); vertex(i-dx, y, -gridsize-dz); color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s); vertex(i-dx, y, -dz); vertex(i-dx, y, -dz); color(0,0, 0, 0); vertex(i-dx, y, gridsize-dz); vertex(-gridsize-dx, y, i-dz); color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s); vertex(-dx, y, i-dz); vertex(-dx, y, i-dz); color(0,0, 0, 0); vertex(gridsize-dx, y, i-dz); } end(); } void draw_world(float seconds) { // used for animations angle += 180.0f * seconds; if( angle > 360.0f ) { angle -= 360.0f; } // draw entities using namespace render; std::map::iterator it; for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { switch ((*it).second->type()) { case core::Entity::Default: draw_entity_default((*it).second); break; case core::Entity::Controlable: draw_ship(static_cast ((*it).second)); break; default: break; } } // draw the background grid draw_spacegrid(); } }