Main Page | Class List | File List | Class Members | File Members

render.cpp

Go to the documentation of this file.
00001 00002 00003 #include "render.h" 00004 #include "utilities.h" 00005 #include "world.h" 00006 00007 #ifdef WIN32 00008 #include "Windows.h" 00009 #endif 00010 00011 #include <GL/gl.h> 00012 #include <GL/glu.h> 00013 #include <GL/glut.h> 00014 00015 00016 00017 vector2i g_windowDimensions; 00018 uInt g_terrainDisplayListId; 00019 00020 00021 void_func_void g_pRender; 00022 void_func_void g_pMainLoop; 00023 00024 00025 void __draw() 00026 { 00027 glClear(GL_COLOR_BUFFER_BIT); 00028 g_pRender(); 00029 00030 glutSwapBuffers(); 00031 } 00032 00033 void __idle() 00034 { 00035 g_pMainLoop(); 00036 glutPostRedisplay(); 00037 } 00038 00039 00040 void __reshape(int x, int y) 00041 { 00042 if(x>0) 00043 { 00044 g_windowDimensions.x = x; 00045 } else 00046 { 00047 g_windowDimensions.x = 0; 00048 } 00049 00050 if(y>0) 00051 { 00052 g_windowDimensions.y = y; 00053 } else 00054 { 00055 g_windowDimensions.y = 0; 00056 } 00057 00058 glViewport(0, 0, g_windowDimensions.x, g_windowDimensions.y); 00059 } 00060 00061 00062 void setMainLoopCallback(void_func_void pIdle) 00063 { 00064 g_pMainLoop = pIdle; 00065 } 00066 00067 00068 void setRenderCallback(void_func_void pRender) 00069 { 00070 g_pRender = pRender; 00071 } 00072 00073 00074 void beginMainLoop() 00075 { 00076 if(g_pMainLoop && g_pRender) glutMainLoop(); 00077 } 00078 00079 00080 void getWindowDimensions( vector2i& dim ) 00081 { 00082 dim.x = g_windowDimensions.x; 00083 dim.y = g_windowDimensions.y; 00084 } 00085 00086 00087 void drawRectangle( const vector2f* pPos, 00088 const vector2f* pDim, 00089 const float* pLineWidth, 00090 const colour4f* pLineColour, 00091 const colour4f* pFillColour) 00092 { 00093 static uInt outlineDisplayListId = 0; 00094 static uInt fillDisplayListId = 0; 00095 00096 if(pPos) 00097 { 00098 glPushMatrix(); 00099 glLoadIdentity(); 00100 glTranslatef(pPos->x, pPos->y, 0.0f); 00101 if(pDim) glScalef(pDim->x, pDim->y, 1.0f); 00102 } 00103 00104 if(pLineColour) 00105 { 00106 glColor3fv(pLineColour->m_data); 00107 if(pLineWidth) glLineWidth(*pLineWidth); 00108 00109 00110 if(outlineDisplayListId) 00111 { 00112 glCallList(outlineDisplayListId); 00113 } else 00114 { 00115 outlineDisplayListId = glGenLists(1); 00116 glNewList(outlineDisplayListId, GL_COMPILE_AND_EXECUTE); 00117 00118 glBegin(GL_LINE_LOOP); 00119 00120 glVertex3f(0.0f,0.0f,0.0f); 00121 glVertex3f(1.0f,0.0f,0.0f); 00122 glVertex3f(1.0f,1.0f,0.0f); 00123 glVertex3f(0.0f,1.0f,0.0f); 00124 00125 glEnd(); 00126 00127 glEndList(); 00128 } 00129 } 00130 00131 if(pFillColour) 00132 { 00133 glColor3fv(pFillColour->m_data); 00134 00135 00136 if(fillDisplayListId) 00137 { 00138 glCallList(fillDisplayListId); 00139 } else 00140 { 00141 fillDisplayListId = glGenLists(1); 00142 glNewList(fillDisplayListId, GL_COMPILE_AND_EXECUTE); 00143 00144 glBegin(GL_POLYGON); 00145 00146 glVertex3f(0.0f,0.0f,0.0f); 00147 glVertex3f(1.0f,0.0f,0.0f); 00148 glVertex3f(1.0f,1.0f,0.0f); 00149 glVertex3f(0.0f,1.0f,0.0f); 00150 00151 glEnd(); 00152 00153 glEndList(); 00154 } 00155 } 00156 00157 if(pPos) glPopMatrix(); 00158 } 00159 00160 00161 void drawGrid( const vector2i& subdivs, 00162 const vector2f* pPos, 00163 const vector2f* pDim, 00164 const float* pLineWidth, 00165 const colour4f* pLineColour, 00166 const colour4f* pFillColour) 00167 { 00168 00169 00170 if(pPos) 00171 { 00172 glLoadIdentity(); 00173 glPushMatrix(); 00174 glTranslatef(pPos->x, pPos->y, 0.0f); 00175 if(pDim) glScalef(pDim->x, pDim->y, 1.0f); 00176 } 00177 00178 00179 if(pLineWidth) glLineWidth(*pLineWidth); 00180 00181 drawRectangle(NULL, pDim, NULL, pLineColour, pFillColour); 00182 00183 glColor3fv(pLineColour->m_data); 00184 glBegin(GL_LINES); 00185 00186 for(int i=0;i<=subdivs.x;i++) 00187 { 00188 glVertex2f((float)i/subdivs.x, 0); 00189 glVertex2f((float)i/subdivs.x, 1); 00190 } 00191 00192 for(int i=0;i<=subdivs.y;i++) 00193 { 00194 glVertex2f(0, (float)i/subdivs.y); 00195 glVertex2f(1, (float)i/subdivs.y); 00196 } 00197 00198 glEnd(); 00199 00200 00201 if(pPos) glPopMatrix(); 00202 } 00203 00204 00205 void initRenderingContext(int argc, char* argv[]) 00206 { 00207 glutInit(&argc, argv); 00208 00209 glutInitWindowSize(800,800); 00210 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); 00211 glutCreateWindow("experiment03"); 00212 //glutFullScreen(); 00213 00214 glDisable(GL_LIGHTING); 00215 glDisable(GL_DEPTH_TEST); 00216 glEnable(GL_BLEND); 00217 00218 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00219 00220 glutReshapeFunc(__reshape); 00221 glutDisplayFunc(__draw); 00222 glutIdleFunc(__idle); 00223 00224 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 00225 00226 glMatrixMode(GL_PROJECTION); 00227 glLoadIdentity(); 00228 glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); 00229 00230 glMatrixMode(GL_MODELVIEW); 00231 glLoadIdentity(); 00232 } 00233 00234 00235 00236 00237 00238 00239

Generated on Sun Mar 6 22:11:24 2005 for experiment03 by doxygen 1.3.7