00001 #include <windows.h>
00002 #include "shared.h"
00003 #include "polygon.h"
00004 #include "camera.h"
00005 #include "texture.h"
00006 #include "mmgr.h"
00007
00008 extern int numCameras;
00009 extern float multiplier;
00010 extern int fps;
00011
00012 void SetGLCamera(CAMERA* camera)
00013 {
00014 int temp;
00015 for(temp = 0; temp < numCameras; temp++)
00016 camera[temp].Reset();
00017 }
00018
00019 void SetGLWorld(POLYGON* polygon)
00020 {
00021
00022 polygon[0].Vertex[0].x = -1.0;
00023 polygon[0].Vertex[0].y = -1.0;
00024 polygon[0].Vertex[0].z = 1.0;
00025 polygon[0].Vertex[1].x = 1.0;
00026 polygon[0].Vertex[1].y = -1.0;
00027 polygon[0].Vertex[1].z = 1.0;
00028 polygon[0].Vertex[2].x = 1.0;
00029 polygon[0].Vertex[2].y = 1.0;
00030 polygon[0].Vertex[2].z = 1.0;
00031
00032 polygon[1].Vertex[0].x = -1.0;
00033 polygon[1].Vertex[0].y = -1.0;
00034 polygon[1].Vertex[0].z = 1.0;
00035 polygon[1].Vertex[1].x = 1.0;
00036 polygon[1].Vertex[1].y = 1.0;
00037 polygon[1].Vertex[1].z = 1.0;
00038 polygon[1].Vertex[2].x = -1.0;
00039 polygon[1].Vertex[2].y = 1.0;
00040 polygon[1].Vertex[2].z = 1.0;
00041
00042 polygon[2].Vertex[0].x = 1.0;
00043 polygon[2].Vertex[0].y = -1.0;
00044 polygon[2].Vertex[0].z = -1.0;
00045 polygon[2].Vertex[1].x = -1.0;
00046 polygon[2].Vertex[1].y = -1.0;
00047 polygon[2].Vertex[1].z = -1.0;
00048 polygon[2].Vertex[2].x = -1.0;
00049 polygon[2].Vertex[2].y = 1.0;
00050 polygon[2].Vertex[2].z = -1.0;
00051
00052 polygon[3].Vertex[0].x = 1.0;
00053 polygon[3].Vertex[0].y = -1.0;
00054 polygon[3].Vertex[0].z = -1.0;
00055 polygon[3].Vertex[1].x = -1.0;
00056 polygon[3].Vertex[1].y = 1.0;
00057 polygon[3].Vertex[1].z = -1.0;
00058 polygon[3].Vertex[2].x = 1.0;
00059 polygon[3].Vertex[2].y = 1.0;
00060 polygon[3].Vertex[2].z = -1.0;
00061
00062 polygon[4].Vertex[0].x = -1.0;
00063 polygon[4].Vertex[0].y = -1.0;
00064 polygon[4].Vertex[0].z = -1.0;
00065 polygon[4].Vertex[1].x = -1.0;
00066 polygon[4].Vertex[1].y = -1.0;
00067 polygon[4].Vertex[1].z = 1.0;
00068 polygon[4].Vertex[2].x = -1.0;
00069 polygon[4].Vertex[2].y = 1.0;
00070 polygon[4].Vertex[2].z = 1.0;
00071
00072 polygon[5].Vertex[0].x = -1.0;
00073 polygon[5].Vertex[0].y = -1.0;
00074 polygon[5].Vertex[0].z = -1.0;
00075 polygon[5].Vertex[1].x = -1.0;
00076 polygon[5].Vertex[1].y = 1.0;
00077 polygon[5].Vertex[1].z = 1.0;
00078 polygon[5].Vertex[2].x = -1.0;
00079 polygon[5].Vertex[2].y = 1.0;
00080 polygon[5].Vertex[2].z = -1.0;
00081
00082 polygon[6].Vertex[0].x = 1.0;
00083 polygon[6].Vertex[0].y = -1.0;
00084 polygon[6].Vertex[0].z = 1.0;
00085 polygon[6].Vertex[1].x = 1.0;
00086 polygon[6].Vertex[1].y = -1.0;
00087 polygon[6].Vertex[1].z = -1.0;
00088 polygon[6].Vertex[2].x = 1.0;
00089 polygon[6].Vertex[2].y = 1.0;
00090 polygon[6].Vertex[2].z = -1.0;
00091
00092 polygon[7].Vertex[0].x = 1.0;
00093 polygon[7].Vertex[0].y = -1.0;
00094 polygon[7].Vertex[0].z = 1.0;
00095 polygon[7].Vertex[1].x = 1.0;
00096 polygon[7].Vertex[1].y = 1.0;
00097 polygon[7].Vertex[1].z = -1.0;
00098 polygon[7].Vertex[2].x = 1.0;
00099 polygon[7].Vertex[2].y = 1.0;
00100 polygon[7].Vertex[2].z = 1.0;
00101
00102 polygon[8].Vertex[0].x = 1.0;
00103 polygon[8].Vertex[0].y = 1.0;
00104 polygon[8].Vertex[0].z = 1.0;
00105 polygon[8].Vertex[1].x = 1.0;
00106 polygon[8].Vertex[1].y = 1.0;
00107 polygon[8].Vertex[1].z = -1.0;
00108 polygon[8].Vertex[2].x = -1.0;
00109 polygon[8].Vertex[2].y = 1.0;
00110 polygon[8].Vertex[2].z = -1.0;
00111
00112 polygon[9].Vertex[0].x = 1.0;
00113 polygon[9].Vertex[0].y = 1.0;
00114 polygon[9].Vertex[0].z = 1.0;
00115 polygon[9].Vertex[1].x = -1.0;
00116 polygon[9].Vertex[1].y = 1.0;
00117 polygon[9].Vertex[1].z = -1.0;
00118 polygon[9].Vertex[2].x = -1.0;
00119 polygon[9].Vertex[2].y = 1.0;
00120 polygon[9].Vertex[2].z = 1.0;
00121
00122 polygon[10].Vertex[0].x = -1.0;
00123 polygon[10].Vertex[0].y = -1.0;
00124 polygon[10].Vertex[0].z = 1.0;
00125 polygon[10].Vertex[1].x = -1.0;
00126 polygon[10].Vertex[1].y = -1.0;
00127 polygon[10].Vertex[1].z = -1.0;
00128 polygon[10].Vertex[2].x = 1.0;
00129 polygon[10].Vertex[2].y = -1.0;
00130 polygon[10].Vertex[2].z = -1.0;
00131
00132 polygon[11].Vertex[0].x = -1.0;
00133 polygon[11].Vertex[0].y = -1.0;
00134 polygon[11].Vertex[0].z = 1.0;
00135 polygon[11].Vertex[1].x = 1.0;
00136 polygon[11].Vertex[1].y = -1.0;
00137 polygon[11].Vertex[1].z = -1.0;
00138 polygon[11].Vertex[2].x = 1.0;
00139 polygon[11].Vertex[2].y = -1.0;
00140 polygon[11].Vertex[2].z = 1.0;
00141
00142 polygon[0].SetNormal();
00143 polygon[1].SetNormal();
00144 polygon[2].SetNormal();
00145 polygon[3].SetNormal();
00146 polygon[4].SetNormal();
00147 polygon[5].SetNormal();
00148 polygon[6].SetNormal();
00149 polygon[7].SetNormal();
00150 polygon[8].SetNormal();
00151 polygon[9].SetNormal();
00152 polygon[10].SetNormal();
00153 polygon[11].SetNormal();
00154 }
00155
00156 void SetGLProperties()
00157 {
00158 glCullFace(GL_BACK);
00159 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00160 glClearDepth(1.0);
00161 glDepthFunc(GL_LESS);
00162 glEnable(GL_DEPTH_TEST);
00163 glShadeModel(GL_SMOOTH);
00164 glEnable(GL_NORMALIZE);
00165 glEnable(GL_CULL_FACE);
00166 }
00167
00168 void SetGLProjection(int Width, int Height)
00169 {
00170 if (Height==0)
00171 Height=1;
00172 glViewport(0, 0, Width, Height);
00173 glMatrixMode(GL_PROJECTION);
00174 glLoadIdentity();
00175 gluPerspective(45.0,(float)Width/(float)Height,1.0,200.0);
00176 }
00177
00178 void SetGLView(int Width, int Height)
00179 {
00180 SetGLProjection(Width, Height);
00181 glMatrixMode(GL_MODELVIEW);
00182 glLoadIdentity();
00183 }
00184
00185 void SetGLMaterial()
00186 {
00187 float mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
00188 float mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00189 float mat_specular[] = { 0.9, 0.9, 0.9, 1.0 };
00190 float mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
00191 float mat_shininess[] = { 80.0 };
00192
00193 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00194 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00195 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00196 glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00197 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
00198 }
00199
00200 void SetGLLighting()
00201 {
00202 float light_ambient[] = {0.2, 0.2, 0.2, 1.0};
00203 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00204 float light_position[] = {-3.0, 2.0, 1.0, 1.0};
00205 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00206 glEnable(GL_LIGHTING);
00207 glEnable(GL_LIGHT0);
00208 }
00209
00210 void SetGLTexture(TEXTURE* texture)
00211 {
00212 sprintf(texture[0].TexName, "%s", "tile.tga");
00213 texture[0].LoadTGA();
00214 }
00215
00216 void DrawGrid()
00217 {
00218 glDisable(GL_TEXTURE_2D);
00219 glDisable(GL_LIGHTING);
00220 glPushMatrix();
00221 glTranslatef(0,-2.0,0);
00222 float Line = -10;
00223 int Grid;
00224 glBegin(GL_LINES);
00225 for(Grid = 0; Grid <= 20; Grid += 1)
00226 {
00227 glColor3f(0.0f,1.0f,0.0f);
00228 glVertex3f(Line + Grid, 0, -10);
00229 glVertex3f(Line + Grid, 0, 10);
00230 glVertex3f(-10, 0, Line + Grid);
00231 glVertex3f(10, 0, Line + Grid);
00232 }
00233 glEnd();
00234 glPopMatrix();
00235 glEnable(GL_TEXTURE_2D);
00236 glEnable(GL_LIGHTING);
00237 }
00238
00239 void DrawCube(POLYGON* polygon, TEXTURE* texture)
00240 {
00241 float mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
00242 float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00243 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00244 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00245
00246 glBindTexture(GL_TEXTURE_2D, texture[0].TexID);
00247
00248 glPushMatrix();
00249 glTranslatef(0.0f,0.0f,-5.0f);
00250 glBegin(GL_TRIANGLES);
00251
00252 glNormal3fv(&polygon[0].Vertex[0].nx);
00253 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[0].x);
00254 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[1].x);
00255 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[0].Vertex[2].x);
00256
00257 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[1].Vertex[0].x);
00258 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[1].x);
00259 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[2].x);
00260
00261 glNormal3fv(&polygon[2].Vertex[0].nx);
00262 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[0].x);
00263 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[1].x);
00264 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[2].Vertex[2].x);
00265
00266 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[3].Vertex[0].x);
00267 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[1].x);
00268 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[2].x);
00269
00270 glNormal3fv(&polygon[4].Vertex[0].nx);
00271 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[0].x);
00272 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[1].x);
00273 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[4].Vertex[2].x);
00274
00275 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[5].Vertex[0].x);
00276 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[1].x);
00277 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[2].x);
00278
00279 glNormal3fv(&polygon[6].Vertex[0].nx);
00280 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[0].x);
00281 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[1].x);
00282 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[6].Vertex[2].x);
00283
00284 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[7].Vertex[0].x);
00285 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[1].x);
00286 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[2].x);
00287
00288 glNormal3fv(&polygon[8].Vertex[0].nx);
00289 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[0].x);
00290 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[1].x);
00291 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[8].Vertex[2].x);
00292
00293 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[9].Vertex[0].x);
00294 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[1].x);
00295 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[2].x);
00296
00297 glNormal3fv(&polygon[10].Vertex[0].nx);
00298 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[0].x);
00299 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[1].x);
00300 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[10].Vertex[2].x);
00301
00302 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[11].Vertex[0].x);
00303 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[1].x);
00304 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[2].x);
00305 glEnd();
00306 glPopMatrix();
00307 }
00308
00309 void DrawSphere()
00310 {
00311 float mat_ambient[] = { 0.8, 0.5, 0.1, 1.0 };
00312 float mat_diffuse[] = { 0.8, 0.5, 0.1, 1.0 };
00313 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00314 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00315
00316 glDisable(GL_TEXTURE_2D);
00317 glPushMatrix();
00318 glTranslatef(-3.0f,-1.0f,-8.0f);
00319 GLUquadricObj * sphere = gluNewQuadric();
00320 gluQuadricOrientation(sphere, GLU_OUTSIDE);
00321 gluSphere(sphere,1.0,50,50);
00322 glPopMatrix();
00323 glEnable(GL_TEXTURE_2D);
00324 }
00325
00326 void DrawCone()
00327 {
00328 float mat_ambient[] = { 0.1, 0.5, 1.0, 1.0 };
00329 float mat_diffuse[] = { 0.1, 0.5, 1.0, 1.0 };
00330 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00331 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00332
00333 glDisable(GL_TEXTURE_2D);
00334 glDisable(GL_CULL_FACE);
00335 glPushMatrix();
00336 glTranslatef(3.0f,-2.0f,-8.0f);
00337 glRotatef(-90,1,0,0);
00338 GLUquadricObj * cylinder = gluNewQuadric();
00339 gluQuadricOrientation(cylinder, GLU_OUTSIDE);
00340 gluCylinder(cylinder,1.0,0.0,2.0,20,20);
00341 glPopMatrix();
00342 glEnable(GL_CULL_FACE);
00343 glEnable(GL_TEXTURE_2D);
00344 }
00345
00346 float GetTimePassed(float& lasttime, int average, float* lastmultiplier)
00347 {
00348 float timeoffset;
00349 float currenttime;
00350
00351
00352
00353
00354 if (lasttime == 0)
00355 lasttime = (float)GetTickCount();
00356
00357 currenttime = (float)GetTickCount();
00358
00359 timeoffset = currenttime - lasttime;
00360
00361 if (timeoffset < 8.333333)
00362 {
00363 timeoffset = 8.333333;
00364 currenttime = lasttime + timeoffset;
00365 }
00366
00367 lasttime = currenttime;
00368
00369 multiplier = timeoffset / 1000;
00370 for (int loop = 0; loop < average - 1; loop++)
00371 {
00372 lastmultiplier[loop] = lastmultiplier[loop + 1];
00373 }
00374 lastmultiplier[average - 1] = multiplier;
00375 for (int loop = 0; loop < average - 1; loop++)
00376 multiplier += lastmultiplier[loop];
00377 multiplier /= (float)average;
00378 if (multiplier)
00379 fps = (int)(1.0 / multiplier);
00380 return multiplier;
00381 }
00382