fysxasteroids/engine/ViewBase.cc

326 lines
7.3 KiB
C++
Raw Normal View History

2010-04-05 23:38:59 +02:00
#include "ViewBase.h"
#include "ModelBase.h"
#include "ControllerBase.h"
#include "CameraBase.h"
#include "OverlayBase.h"
#include "SimpleConsoleOverlay.h"
#include "OGLFT.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "DrawingsGL.h"
using namespace std;
namespace Engine {
static ViewBase* ViewInstance = NULL;
void InitGL () {
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable (GL_CULL_FACE);
glDisable (GL_FOG);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
/*
* Inherited Module functions
*/
int ViewBase::OnInit (int argc, char* argv[]) {
LogMessage ("View Init");
mWindowHeight = VIEW_DEFAULT_HEIGHT;
mWindowWidth = VIEW_DEFAULT_WIDTH;
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if( SDL_SetVideoMode( mWindowWidth, mWindowHeight, 16, SDL_OPENGL | SDL_RESIZABLE ) == 0 ) {
LogError ("Video mode set failed: %s", SDL_GetError ());
exit (-1);
}
InitGL ();
Resize (mWindowWidth, mWindowHeight);
mConsoleFont = new OGLFT::Monochrome ("./data/fonts/console.ttf", 12);
if ( mConsoleFont == 0 || !mConsoleFont->isValid() ) {
LogError ("Could not load font %s!", "./data/fonts/console.ttf");
exit (-1);
}
OverlayBasePtr console_overlay(new SimpleConsoleOverlay);
2010-04-05 23:38:59 +02:00
AddOverlay (console_overlay);
mConsoleFont->setForegroundColor (1., 1., 1.);
mDrawAxis = false;
mDrawGrid = false;
mGridSizeX = 8;
mGridSizeZ = 8;
ViewInstance = this;
return 0;
}
void ViewBase::OnDestroy () {
if (mConsoleFont )
delete mConsoleFont;
mConsoleFont = NULL;
while (mOverlays.size() > 0)
mOverlays.pop_back();
2010-04-05 23:38:59 +02:00
ViewInstance = NULL;
LogDebug ("View Destroy");
}
void ViewBase::CalcWorldCoordinates (int screen_x, int screen_y, float world_y, float *pos_out) {
GLdouble modelMatrix[16], projMatrix[16];
GLint viewport[4];
GLdouble wx, wy, wz;
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
int realy = viewport[3] - screen_y - 1;
gluUnProject ((GLdouble) screen_x, (GLdouble) realy, 1.,
modelMatrix, projMatrix, viewport, &wx, &wy, &wz);
GLdouble t;
GLdouble d[3];
float eye[3];
mCamera->GetEye (&eye[0]);
d[0] = wx - eye[0];
d[1] = wy - eye[1];
d[2] = wz - eye[2];
assert (fabs (d[1]) >= 1.0e-3);
t = -eye[1]/d[1] + world_y;
pos_out[0] = eye[0] + t * d[0];
pos_out[1] = eye[1] + t * d[1];
pos_out[2] = eye[2] + t * d[2];
}
/*
* Module specific functions
*/
void ViewBase::UpdateCamera () {
EntityPhysicState* player_ent = GetEntityPhysicState (GetPlayerEntityId());
if (!player_ent) {
LogError ("Could not call Model::PositionCamera(): player entity not found!");
exit (-1);
}
vector3d entity_camera_distance (-2, 3, 0);
vector3d entity_position = player_ent->GetPosition();
player_ent->Globalize (entity_camera_distance);
mCamera->SetEye (
entity_camera_distance[0],
entity_camera_distance[1],
entity_camera_distance[2]
);
mCamera->SetPointOfIntrest (
entity_position[0],
entity_position[1],
entity_position[2]
);
mCamera->Update ();
}
void ViewBase::Draw () {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// update the frame rate counter
static Uint32 this_frame_ticks;
static Uint32 last_frame_ticks = 0;
static Uint32 last_fps_update = 0;
static int frame_counter = 0;
this_frame_ticks = SDL_GetTicks ();
last_fps_update += this_frame_ticks - last_frame_ticks;
last_frame_ticks = this_frame_ticks;
frame_counter++;
if (last_fps_update > 1000) {
mFrameRate = frame_counter;
last_fps_update = 0;
frame_counter = 0;
}
UpdateCamera ();
if (mDrawGrid)
DrawGrid ();
if (mDrawAxis)
DrawAxis ();
DrawWorld ();
std::vector<OverlayBasePtr>::iterator overlay_iter;
2010-04-05 23:38:59 +02:00
for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) {
(*overlay_iter)->Draw();
}
// and update the screen
SDL_GL_SwapBuffers ();
}
void ViewBase::DrawGLString (float x, float y, const char* str) {
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
mConsoleFont->draw (x, y, str);
}
void ViewBase::GetCamereEye (float *eye_out) {
assert (mCamera);
mCamera->GetEye (eye_out);
}
void ViewBase::DrawGrid () {
float xmin, xmax, xstep, zmin, zmax, zstep;
int i, count_x, count_z;
xmin = -mGridSizeX;
xmax = mGridSizeX;
zmin = -mGridSizeZ;
zmax = mGridSizeZ;
count_x = mGridSizeX * 2;
count_z = mGridSizeZ * 2;
xstep = 1.;
zstep = 1.;
glColor3f (1., 1., 1.);
glBegin (GL_LINES);
for (i = 0; i <= count_x; i++) {
glVertex3f (i * xstep + xmin, 0., zmin);
glVertex3f (i * xstep + xmin, 0., zmax);
}
for (i = 0; i <= count_z; i++) {
glVertex3f (xmin, 0, i * zstep + zmin);
glVertex3f (xmax, 0, i * zstep + zmin);
}
glEnd ();
}
void ViewBase::DrawWorld () {
}
void ViewBase::Resize (int width, int height) {
if (height == 0)
height = 1;
mWindowWidth = static_cast<unsigned int> (width);
mWindowHeight = static_cast<unsigned int> (height);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(mCamera->GetFOVY (), float (width) / float (height), 0.1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
LogDebug ("Resize to: %d x %d", mWindowWidth,mWindowHeight);
/** \warning
* This call has to be made for SDL 1.2 for 1.3 there seems to be a
* workaround, however since I do not yet run SDL 1.3 I hold on to this.
* See http://lists.libsdl.org/pipermail/sdl-libsdl.org/2008-November/067306.html
*/
if( SDL_SetVideoMode( mWindowWidth, mWindowHeight, 16, SDL_OPENGL | SDL_RESIZABLE ) == 0 ) {
LogError ("Video mode set failed: %s", SDL_GetError ());
exit (-1);
}
}
bool ViewBase::SendKeyDown (const SDL_keysym &keysym) {
std::vector<OverlayBasePtr>::iterator overlay_iter;
2010-04-05 23:38:59 +02:00
for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) {
if ( (*overlay_iter)->OnKeyDown (keysym))
return true;
}
return false;
}
bool ViewBase::SendKeyUp (const SDL_keysym &keysym) {
std::vector<OverlayBasePtr>::iterator overlay_iter;
2010-04-05 23:38:59 +02:00
for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) {
if ( (*overlay_iter)->OnKeyUp (keysym))
return true;
}
return false;
}
bool ViewBase::SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) {
std::vector<OverlayBasePtr>::iterator overlay_iter;
2010-04-05 23:38:59 +02:00
for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) {
if ( (*overlay_iter)->OnMouseButtonUp (button, xpos, ypos))
return true;
}
return false;
}
bool ViewBase::SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) {
std::vector<OverlayBasePtr>::iterator overlay_iter;
2010-04-05 23:38:59 +02:00
for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) {
if ( (*overlay_iter)->OnMouseButtonDown (button, xpos, ypos))
return true;
}
return false;
}
/*
* Global functions
*/
void DrawGLString (float x, float y, const char* str) {
if (!ViewInstance) {
LogError ("Cannot Draw GL String: View not yet initialized!");
return;
}
ViewInstance->DrawGLString (x, y, str);
}
unsigned int GetWindowWidth() {
return ViewInstance->GetWindowWidth ();
}
unsigned int GetWindowHeight() {
return ViewInstance->GetWindowHeight ();
}
int GetFrameRate () {
return ViewInstance->GetFrameRate ();
}
}