00001 /*************************************************************************** 00002 renderer.h - description 00003 ------------------- 00004 begin : Sun Jul 22 2001 00005 copyright : (C) 2001 by T Kontkanen 00006 email : tkontkanen@mail.com 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef RENDERER_H 00019 #define RENDERER_H 00020 00021 00025 #include "SDL/SDL.h" 00026 #include "GL/gl.h" 00027 #include "GL/glu.h" 00028 #include "GL/glpng.h" 00029 00030 #include <tgl.h> 00031 #include "allincludes.h" 00032 #include "string.h" 00033 #include "gamedefs.h" 00034 #include "vector.h" 00035 #include "texture.h" 00036 #include "color.h" 00037 00038 using namespace TGL; 00039 00040 enum RenderingModes { RENDER_TGL_OBJECT, RENDER_INTERFACE, RENDER_TERRAIN, RENDER_3D, RENDER_2D }; 00041 enum CameraModes { CAMERA_ORBIT, CAMERA_FREE }; 00042 00043 class BFont; 00044 00045 class Renderer { 00046 public: 00047 00049 Renderer( int w, int h, int depth, bool fullscr ); 00051 ~Renderer(); 00052 00053 //screen 00055 void setVideoMode( int w, int , int depth, bool fullscr ); 00057 int getScreenW() { return screen_w; } 00059 int getScreenH() { return screen_h; } 00061 int getScreenDepth() { return screen_depth; } 00063 bool getFullScreen() { return fullscreen; } 00065 void toggleFullScreen() { 00066 setFuncName( "Renderer::toggleFullScreen" ); 00067 if( SDL_WM_ToggleFullScreen( screen ) ) 00068 if( fullscreen ) fullscreen=false; //invert 00069 else fullscreen=true; 00070 else msg::msgOut( msg::MSG_FAILURE, "Unable to toggle fullscreen/windowed mode" ); 00071 } 00073 void toggleWireframe() { 00074 setFuncName( "Renderer::toggleWireframe" ); 00075 if( !wireframe ) { glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); wireframe=true; } 00076 else { glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); wireframe=false; } 00077 msgOut( MSG_DEBUG_3, "Wireframe mode toggled" ); 00078 } 00079 00080 00081 // rendering methods 00083 void swapBuffers() { SDL_GL_SwapBuffers(); checkError(); } 00085 void clearBuffer() { glClear( GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT ); } 00087 void renderSelectionBox( Vector p1, Vector p2 ); //render 2d box, p1 and p2 are opposing corners 00089 void renderBox( Vector p1, Vector p2, float r, float g, float b, float a, GLint textureid=0 ); 00091 void renderBox( Vector p1, Vector p2, Color col, GLint textureid=0 ) { renderBox( p1, p2, col.r(),col.g(),col.b(),col.a(), textureid ); } 00093 void renderBox( Vector p1, Vector p2, Vector textureoff1, Vector textureoff2, float r, float g, float b, float a, GLint textureid ); 00095 void renderBox( Vector p1, Vector p2, Vector textureoff1, Vector textureoff2, Color col, GLint textureid ) { renderBox( p1, p2, textureoff1, textureoff2,col.r(),col.g(),col.b(),col.a(), textureid ); } 00096 00098 void setRenderingMode( enum RenderingModes mode ); 00100 void renderMouseCursor( Vector p ); 00102 void renderPoint( Vector pos, Color col, float size=1 ); 00103 00104 //camera methods 00106 void initCamera(); 00108 void setCameraMode( enum CameraModes mode ); 00110 void setCameraLookAtDistance( float dist ); 00112 void adjustCameraLookAtDistance( float adjdist ); 00114 float getCameraLookAtDistance() const { return cameraDistance; } 00116 void setCameraPosition( float x, float y, float z, bool updateLookat=true ); 00118 void setCameraPosition( Vector pos, bool updateLookat=true ); 00120 void setCameraLookAt( Vector pos, bool updatePosition=true ); 00122 void setCameraAltitude( float z ); 00124 Vector getCameraPosition() { return cameraPosition; } 00126 Vector getCameraLookAt() { return lookAt; } 00128 void moveCameraForward( float forward ); 00130 void moveCameraXYZ( float x, float y, float z=0, bool updateLookat=true ); 00132 void moveCamera2DRight( float x ); 00134 void moveCamera2DUp( float y ); 00136 void setCameraAngle( float angle, float pitch ); 00138 void rotateCameraAngle( float angle, float pitch ); 00140 void applyCamera( float prevPosWeight=0.0 ); 00142 bool cameraMoved() { return camera_moved; } 00143 00144 00145 //other 00147 Vector screenToWorld( int x, int y, float z=-1 ); 00149 Vector screenToWorld( float x, float y, float z=-1 ) { return screenToWorld( int(x), int(y), z ); } 00151 Vector worldToScreen( Vector &objpos ); 00153 bool checkError( const char *str=NULL ); 00154 00155 //tgl objects 00157 tgl_object* loadTGLObject( const char *fname ); 00159 tgl_object* getTGLObject( const char *fname ); 00161 void freeTGLObject( const char *fname ); 00162 00163 //textures 00165 Texture *loadTexture( const char *texturefile, enum AlphaModes alpha=BLEND_ALPHA, bool mipmap=false, int minfilter=GL_NEAREST, int magfilter=GL_NEAREST, int wrapmode=GL_CLAMP ); 00167 GLint getTextureID( const char *texturefile, bool warn=true ); 00169 Texture *getTexture( const char *texturefile, bool warn=true ); 00170 00171 //text&fonts 00173 void setFont( BFont *font ) { currentfont=font; }; 00175 void renderText( int x, int y, const char *str ); 00176 void renderText( float x, float y, const char *str ) { renderText( int(x), int(y), str ); } 00178 float textLength( const char *str ); 00179 00180 private: 00182 void set3DModelView(); 00183 00184 00185 typedef void (*GL_ActiveTextureARB_Func)(GLenum); 00186 typedef void (*GL_MultiTexCoord2fARB_Func)(GLenum, GLfloat, GLfloat); 00187 GL_ActiveTextureARB_Func glActiveTextureARB_ptr; 00188 GL_MultiTexCoord2fARB_Func glMultiTexCoord2fARB_ptr; 00189 float fogColor[4]; 00190 enum RenderingModes currentRenderingMode; //current mode 00191 00192 // screen 00193 int screen_h, screen_w, screen_depth; 00194 bool fullscreen; 00195 SDL_Surface* screen; 00196 00197 //gl 00198 bool multitexturing, wireframe; 00199 00200 //camera 00201 bool camera_moved, camera_changed; 00202 float cameraAngle, cameraPitch, cameraDistance; 00203 Vector lookAt; // cam parameters 00204 Vector lookUp; 00205 Vector cameraPosition; 00206 Vector smoothLookAt, smoothLookUp, smoothCameraPosition; // camera parameters with smooth movement 00207 float maxViewDistance; 00208 enum CameraModes cameraMode; 00209 00210 //tgl objects 00211 tgl_object *objects[MAX_TGL_OBJECTS]; 00212 int object_clients[MAX_TGL_OBJECTS]; //determines how many units are using each model 00213 char object_file_names[MAX_TGL_OBJECTS][MAX_FILE_NAME_LEN]; 00214 00215 //textures 00216 Texture *textures[MAX_TEXTURES]; 00217 00218 //font 00219 BFont *currentfont; 00220 }; 00221 00222 00223 00224 00225 #endif