#include #include #include #include "glbase.h" #include "engine.h" #include "generic.h" #ifdef HAVE_CONFIG_H #include "config.h" #endif mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : mogltk::base(w, h, flags, 0), twoD(0), fovy(60.0) { SDL_Surface * surface; mogltk::engine::setup(); if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError()); } if (!(surface = SDL_SetVideoMode(w, h, 0, flags | SDL_OPENGL | SDL_HWSURFACE))) { throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError()); } mogltk::engine::glbase_o = this; mogltk::engine::base_o = this; ratio = (GLdouble) surface->w / surface->h; printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio); printm(M_INFO, "\n"); printm(M_INFO, "OpenGL infos\n"); printm(M_INFO, "------------\n"); printm(M_INFO, String("Vendor : ") + (char *) glGetString(GL_VENDOR) + "\n"); printm(M_INFO, String("Renderer : ") + (char *) glGetString(GL_RENDERER) + "\n"); printm(M_INFO, String("Version : ") + (char *) glGetString(GL_VERSION) + "\n"); printm(M_INFO, String("Extensions: ") + (char *) glGetString(GL_EXTENSIONS) + "\n"); glViewport(0, 0, surface->w, surface->h); glCullFace(GL_BACK); glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glClearColor(0, 0, 0, 0); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(fovy, ratio, 1.0, 1024.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SDL_GL_SwapBuffers(); SDL_ShowCursor(SDL_DISABLE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); setsurface(surface); mogltk::engine::postsetup(); } mogltk::glbase::~glbase() { } void mogltk::glbase::Enter2DMode(void) { if (twoD) return; glPushAttrib(GL_ENABLE_BIT); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0.0, GetWidth(), GetHeight(), 0.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); twoD = 1; } void mogltk::glbase::Leave2DMode(void) { if (!twoD) return; glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glPopAttrib(); twoD = 0; } void mogltk::glbase::Flip() { mogltk::engine::pollevents(); SDL_GL_SwapBuffers(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } bool mogltk::glbase::is2D() { return twoD; } void mogltk::glbase::glVertex(GLshort x, GLshort y, GLshort z, GLshort w) { #ifdef DEBUG printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (shorts)\n", x, y, z, w); #endif glVertex4i(x, y, z, w); } void mogltk::glbase::glVertex(GLint x, GLint y, GLint z, GLint w) { #ifdef DEBUG printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (ints)\n", x, y, z, w); #endif glVertex4i(x, y, z, w); } void mogltk::glbase::glVertex(GLfloat x, GLfloat y, GLfloat z, GLfloat w) { #ifdef DEBUG printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (floats)\n", x, y, z, w); #endif glVertex4f(x, y, z, w); } void mogltk::glbase::glVertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) { #ifdef DEBUG printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (doubles)\n", x, y, z, w); #endif glVertex4d(x, y, z, w); } void mogltk::glbase::changeviewport(int x, int y, unsigned int w, unsigned int h) { if ((w == 0) && (h == 0)) { w = GetWidth() - x; h = GetHeight() - y; } ratio = (GLdouble) w / h; glViewport(x, y, w, h); if (!engine::base_o->is2D()) gluPerspective(fovy, ratio, 1.0, 1024.0); } void mogltk::glbase::changefovy(GLdouble nfoyv) { fovy = nfoyv; if (!engine::base_o->is2D()) gluPerspective(fovy, ratio, 1.0, 1024.0); }