/* view.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 "client/client.h" #include "client/shipdrawer.h" #include "client/stardrawer.h" #include "render/render.h" #include "game/game.h" #include "sys/sys.h" #include "math/mathlib.h" using namespace render; namespace client { ShipDrawer *shipdrawer = 0; StarDrawer *stardrawer = 0; game::Ship *target = 0; // the view's target void View::init() { // draw scene if (!shipdrawer) { stardrawer = new StarDrawer(&game.star); shipdrawer = new ShipDrawer(&game.ship); target = &game.ship; } } void View::shutdown() { delete stardrawer; stardrawer = 0; delete shipdrawer; shipdrawer = 0; } void View::reset() { // Our shading model--Gouraud (smooth). gl::shademodel(GL_SMOOTH); // Culling gl::cullface( GL_BACK ); gl::frontface(GL_CCW ); } void View::draw_world(float elapsed) { gl::enable( GL_CULL_FACE ); // Depth buffer writing gl::depthmask(GL_TRUE); gl::enable(GL_DEPTH_TEST); // draw the world gl::push(); gl::translate(game.ship.location - target->location); gl::scale(0.2f, 0.2f, 0.2f); shipdrawer->draw(elapsed); gl::pop(); gl::push(); gl::translate(game.star.location - target->location); stardrawer->draw(elapsed); gl::pop(); gl::disable( GL_CULL_FACE ); } void View::draw_background(float elapsed) { using namespace gl; // enable Alpha blending gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl::enable(GL_BLEND); // galactic axis begin(Lines); color(0.9f, 0.5f, 0.0f); vertex(-2,0,0); color(1.0f, 1.0f, 0.0f); vertex(2,0,0); vertex(0,0,-0.5); vertex(0,0,0.5); vertex(0,1.0f,0); vertex(0,-1, 0); end(); int gridsize = 32; float s = 1.0f / gridsize; float y = -4.0f; float dx = target->location.x - floorf(target->location.x); float dz = target->location.z - floorf(target->location.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(); gl::disable(GL_BLEND); gl::disable(GL_DEPTH_TEST); } void View::draw(float elapsed) { // Clear the color and depth buffers. gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Change to the projection matrix and set our viewing volume. gl::matrixmode( GL_PROJECTION ); gl::loadidentity(); //glu::perspective( 64.0, video::ratio, 1.0, 1024.0 ); const float frustumsize = 0.5f; x = -frustumsize * video.ratio; width = video.ratio; y = -frustumsize; height = 1; //gl::frustum( -frustumsize * video.ratio, frustumsize * video.ratio, -frustumsize, frustumsize, 1.0f, 1024.0f); gl::frustum(x, x+width, y, y +height, 1.0f, 1024.0f); gl::matrixmode( GL_MODELVIEW ); gl::loadidentity(); gl::rotate(90.0f, 0, 1.0, 0); // Camera transformation camera.draw(elapsed); // draw the world draw_world(elapsed); // draw the semi-static background draw_background(elapsed); // switch to ortographic projection to draw the GUI gl::matrixmode( GL_PROJECTION ); gl::loadidentity(); glOrtho(0, video.width, video.height, 0, -1000.0f, 1000.0f); gl::matrixmode( GL_MODELVIEW ); gl::loadidentity(); // draw the console console.draw(); } } // namespace view