00001 #include <windows.h>
00002 #include "shared.h"
00003 #include "polygon.h"
00004 #include "camera.h"
00005 #include "texture.h"
00006 #include "light.h"
00007 #include "locmath.h"
00008 #include "mmgr.h"
00009
00010 extern int numCameras;
00011 extern int numLights;
00012 extern int currentLight;
00013 extern int fps;
00014 extern float multiplier;
00015
00016 void SetGLLighting(LIGHT* light)
00017 {
00018 int temp;
00019 for (temp = 0; temp <= numLights; temp++)
00020 {
00021 light[temp].LightNumber = temp;
00022 light[temp].Reset();
00023 }
00024 glEnable(GL_LIGHTING);
00025 }
00026
00027 void SetGLCamera(CAMERA* camera)
00028 {
00029 int temp;
00030 for(temp = 0; temp <= numCameras; temp++)
00031 camera[temp].Reset();
00032 }
00033
00034 void SetGLWorld(POLYGON* polygon)
00035 {
00036
00037 polygon[0].Vertex[0].x = -1.0;
00038 polygon[0].Vertex[0].y = -1.0;
00039 polygon[0].Vertex[0].z = -4.0;
00040 polygon[0].Vertex[1].x = 1.0;
00041 polygon[0].Vertex[1].y = -1.0;
00042 polygon[0].Vertex[1].z = -4.0;
00043 polygon[0].Vertex[2].x = 1.0;
00044 polygon[0].Vertex[2].y = 1.0;
00045 polygon[0].Vertex[2].z = -4.0;
00046
00047 polygon[1].Vertex[0].x = -1.0;
00048 polygon[1].Vertex[0].y = -1.0;
00049 polygon[1].Vertex[0].z = -4.0;
00050 polygon[1].Vertex[1].x = 1.0;
00051 polygon[1].Vertex[1].y = 1.0;
00052 polygon[1].Vertex[1].z = -4.0;
00053 polygon[1].Vertex[2].x = -1.0;
00054 polygon[1].Vertex[2].y = 1.0;
00055 polygon[1].Vertex[2].z = -4.0;
00056
00057 polygon[2].Vertex[0].x = 1.0;
00058 polygon[2].Vertex[0].y = -1.0;
00059 polygon[2].Vertex[0].z = -6.0;
00060 polygon[2].Vertex[1].x = -1.0;
00061 polygon[2].Vertex[1].y = -1.0;
00062 polygon[2].Vertex[1].z = -6.0;
00063 polygon[2].Vertex[2].x = -1.0;
00064 polygon[2].Vertex[2].y = 1.0;
00065 polygon[2].Vertex[2].z = -6.0;
00066
00067 polygon[3].Vertex[0].x = 1.0;
00068 polygon[3].Vertex[0].y = -1.0;
00069 polygon[3].Vertex[0].z = -6.0;
00070 polygon[3].Vertex[1].x = -1.0;
00071 polygon[3].Vertex[1].y = 1.0;
00072 polygon[3].Vertex[1].z = -6.0;
00073 polygon[3].Vertex[2].x = 1.0;
00074 polygon[3].Vertex[2].y = 1.0;
00075 polygon[3].Vertex[2].z = -6.0;
00076
00077 polygon[4].Vertex[0].x = -1.0;
00078 polygon[4].Vertex[0].y = -1.0;
00079 polygon[4].Vertex[0].z = -6.0;
00080 polygon[4].Vertex[1].x = -1.0;
00081 polygon[4].Vertex[1].y = -1.0;
00082 polygon[4].Vertex[1].z = -4.0;
00083 polygon[4].Vertex[2].x = -1.0;
00084 polygon[4].Vertex[2].y = 1.0;
00085 polygon[4].Vertex[2].z = -4.0;
00086
00087 polygon[5].Vertex[0].x = -1.0;
00088 polygon[5].Vertex[0].y = -1.0;
00089 polygon[5].Vertex[0].z = -6.0;
00090 polygon[5].Vertex[1].x = -1.0;
00091 polygon[5].Vertex[1].y = 1.0;
00092 polygon[5].Vertex[1].z = -4.0;
00093 polygon[5].Vertex[2].x = -1.0;
00094 polygon[5].Vertex[2].y = 1.0;
00095 polygon[5].Vertex[2].z = -6.0;
00096
00097 polygon[6].Vertex[0].x = 1.0;
00098 polygon[6].Vertex[0].y = -1.0;
00099 polygon[6].Vertex[0].z = -4.0;
00100 polygon[6].Vertex[1].x = 1.0;
00101 polygon[6].Vertex[1].y = -1.0;
00102 polygon[6].Vertex[1].z = -6.0;
00103 polygon[6].Vertex[2].x = 1.0;
00104 polygon[6].Vertex[2].y = 1.0;
00105 polygon[6].Vertex[2].z = -6.0;
00106
00107 polygon[7].Vertex[0].x = 1.0;
00108 polygon[7].Vertex[0].y = -1.0;
00109 polygon[7].Vertex[0].z = -4.0;
00110 polygon[7].Vertex[1].x = 1.0;
00111 polygon[7].Vertex[1].y = 1.0;
00112 polygon[7].Vertex[1].z = -6.0;
00113 polygon[7].Vertex[2].x = 1.0;
00114 polygon[7].Vertex[2].y = 1.0;
00115 polygon[7].Vertex[2].z = -4.0;
00116
00117 polygon[8].Vertex[0].x = 1.0;
00118 polygon[8].Vertex[0].y = 1.0;
00119 polygon[8].Vertex[0].z = -4.0;
00120 polygon[8].Vertex[1].x = 1.0;
00121 polygon[8].Vertex[1].y = 1.0;
00122 polygon[8].Vertex[1].z = -6.0;
00123 polygon[8].Vertex[2].x = -1.0;
00124 polygon[8].Vertex[2].y = 1.0;
00125 polygon[8].Vertex[2].z = -6.0;
00126
00127 polygon[9].Vertex[0].x = 1.0;
00128 polygon[9].Vertex[0].y = 1.0;
00129 polygon[9].Vertex[0].z = -4.0;
00130 polygon[9].Vertex[1].x = -1.0;
00131 polygon[9].Vertex[1].y = 1.0;
00132 polygon[9].Vertex[1].z = -6.0;
00133 polygon[9].Vertex[2].x = -1.0;
00134 polygon[9].Vertex[2].y = 1.0;
00135 polygon[9].Vertex[2].z = -4.0;
00136
00137 polygon[10].Vertex[0].x = -1.0;
00138 polygon[10].Vertex[0].y = -1.0;
00139 polygon[10].Vertex[0].z = -4.0;
00140 polygon[10].Vertex[1].x = -1.0;
00141 polygon[10].Vertex[1].y = -1.0;
00142 polygon[10].Vertex[1].z = -6.0;
00143 polygon[10].Vertex[2].x = 1.0;
00144 polygon[10].Vertex[2].y = -1.0;
00145 polygon[10].Vertex[2].z = -6.0;
00146
00147 polygon[11].Vertex[0].x = -1.0;
00148 polygon[11].Vertex[0].y = -1.0;
00149 polygon[11].Vertex[0].z = -4.0;
00150 polygon[11].Vertex[1].x = 1.0;
00151 polygon[11].Vertex[1].y = -1.0;
00152 polygon[11].Vertex[1].z = -6.0;
00153 polygon[11].Vertex[2].x = 1.0;
00154 polygon[11].Vertex[2].y = -1.0;
00155 polygon[11].Vertex[2].z = -4.0;
00156
00157 polygon[0].SetNormal();
00158 polygon[1].SetNormal();
00159 polygon[2].SetNormal();
00160 polygon[3].SetNormal();
00161 polygon[4].SetNormal();
00162 polygon[5].SetNormal();
00163 polygon[6].SetNormal();
00164 polygon[7].SetNormal();
00165 polygon[8].SetNormal();
00166 polygon[9].SetNormal();
00167 polygon[10].SetNormal();
00168 polygon[11].SetNormal();
00169 }
00170
00171 void SetGLProperties()
00172 {
00173 glCullFace(GL_BACK);
00174 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00175 glClearDepth(1.0);
00176 glDepthFunc(GL_LESS);
00177 glEnable(GL_DEPTH_TEST);
00178 glShadeModel(GL_SMOOTH);
00179 glEnable(GL_NORMALIZE);
00180 glEnable(GL_CULL_FACE);
00181 }
00182
00183 void SetGLProjection(int Width, int Height)
00184 {
00185 if (Height==0)
00186 Height=1;
00187 glViewport(0, 0, Width, Height);
00188 glMatrixMode(GL_PROJECTION);
00189 glLoadIdentity();
00190 gluPerspective(45.0,(float)Width/(float)Height,0.5,200.0);
00191 }
00192
00193 void SetGLView(int Width, int Height)
00194 {
00195 SetGLProjection(Width, Height);
00196 glMatrixMode(GL_MODELVIEW);
00197 glLoadIdentity();
00198 }
00199
00200 void SetGLMaterial()
00201 {
00202 float mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
00203 float mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00204 float mat_specular[] = { 0.9, 0.9, 0.9, 1.0 };
00205 float mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
00206 float mat_shininess[] = { 80.0 };
00207
00208 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00209 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00210 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00211 glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00212 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
00213 }
00214
00215 void SetGLTexture(TEXTURE* texture)
00216 {
00217 sprintf(texture[0].TexName, "%s", "tile.tga");
00218 texture[0].LoadTGA();
00219 }
00220
00221 void DrawGrid()
00222 {
00223 glDisable(GL_TEXTURE_2D);
00224 glDisable(GL_LIGHTING);
00225 glPushMatrix();
00226 glTranslatef(0,-2.0,0);
00227 float Line = -10;
00228 int Grid;
00229 glBegin(GL_LINES);
00230 for(Grid = 0; Grid <= 20; Grid += 1)
00231 {
00232 glColor3f(0.0f,1.0f,0.0f);
00233 glVertex3f(Line + Grid, 0, -10);
00234 glVertex3f(Line + Grid, 0, 10);
00235 glVertex3f(-10, 0, Line + Grid);
00236 glVertex3f(10, 0, Line + Grid);
00237 }
00238 glEnd();
00239 glPopMatrix();
00240 glEnable(GL_TEXTURE_2D);
00241 glEnable(GL_LIGHTING);
00242 }
00243
00244 void DrawCube(POLYGON* polygon, TEXTURE* texture)
00245 {
00246 float mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
00247 float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00248 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00249 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00250
00251 glBindTexture(GL_TEXTURE_2D, texture[0].TexID);
00252
00253 glPushMatrix();
00254 glBegin(GL_TRIANGLES);
00255
00256 glNormal3fv(&polygon[0].Vertex[0].nx);
00257 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[0].x);
00258 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[1].x);
00259 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[0].Vertex[2].x);
00260
00261 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[1].Vertex[0].x);
00262 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[1].x);
00263 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[2].x);
00264
00265 glNormal3fv(&polygon[2].Vertex[0].nx);
00266 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[0].x);
00267 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[1].x);
00268 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[2].Vertex[2].x);
00269
00270 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[3].Vertex[0].x);
00271 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[1].x);
00272 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[2].x);
00273
00274 glNormal3fv(&polygon[4].Vertex[0].nx);
00275 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[0].x);
00276 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[1].x);
00277 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[4].Vertex[2].x);
00278
00279 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[5].Vertex[0].x);
00280 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[1].x);
00281 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[2].x);
00282
00283 glNormal3fv(&polygon[6].Vertex[0].nx);
00284 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[0].x);
00285 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[1].x);
00286 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[6].Vertex[2].x);
00287
00288 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[7].Vertex[0].x);
00289 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[1].x);
00290 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[2].x);
00291
00292 glNormal3fv(&polygon[8].Vertex[0].nx);
00293 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[0].x);
00294 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[1].x);
00295 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[8].Vertex[2].x);
00296
00297 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[9].Vertex[0].x);
00298 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[1].x);
00299 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[2].x);
00300
00301 glNormal3fv(&polygon[10].Vertex[0].nx);
00302 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[0].x);
00303 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[1].x);
00304 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[10].Vertex[2].x);
00305
00306 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[11].Vertex[0].x);
00307 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[1].x);
00308 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[2].x);
00309 glEnd();
00310 glPopMatrix();
00311 }
00312
00313 void DrawLightSphere(LIGHT* light)
00314 {
00315 float mat_ambient[] = { 1.0, 0.2, 0.1, 1.0 };
00316 float mat_diffuse[] = { 1.0, 0.2, 0.1, 1.0 };
00317 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00318 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00319 glDisable(GL_TEXTURE_2D);
00320 glPushMatrix();
00321 glTranslatef(light[currentLight].Position.x, light[currentLight].Position.y, light[currentLight].Position.z);
00322 GLUquadricObj * sphere = gluNewQuadric();
00323 gluQuadricOrientation(sphere, GLU_OUTSIDE);
00324 gluSphere(sphere,0.3,20,20);
00325 glPopMatrix();
00326 glEnable(GL_TEXTURE_2D);
00327 }
00328
00329 void DrawGreenSphere()
00330 {
00331 float mat_ambient[] = { 0.2, 1.0, 0.1, 1.0 };
00332 float mat_diffuse[] = { 0.2, 1.0, 0.1, 1.0 };
00333 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00334 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00335 glDisable(GL_TEXTURE_2D);
00336 glPushMatrix();
00337 glTranslatef(-2.0, -2.0, -8.0);
00338 GLUquadricObj * sphere = gluNewQuadric();
00339 gluQuadricOrientation(sphere, GLU_OUTSIDE);
00340 gluSphere(sphere,0.3,20,20);
00341 glPopMatrix();
00342 glEnable(GL_TEXTURE_2D);
00343 }
00344
00345 void DrawSphere()
00346 {
00347 float mat_ambient[] = { 0.8, 0.5, 0.1, 1.0 };
00348 float mat_diffuse[] = { 0.8, 0.5, 0.1, 1.0 };
00349 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00350 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00351
00352 glDisable(GL_TEXTURE_2D);
00353 glPushMatrix();
00354 glTranslatef(-3.0f,-1.0f,-8.0f);
00355 GLUquadricObj * sphere = gluNewQuadric();
00356 gluQuadricOrientation(sphere, GLU_OUTSIDE);
00357 gluSphere(sphere,1.0,50,50);
00358 glPopMatrix();
00359 glEnable(GL_TEXTURE_2D);
00360 }
00361
00362 void DrawCone()
00363 {
00364 float mat_ambient[] = { 0.1, 0.5, 1.0, 1.0 };
00365 float mat_diffuse[] = { 0.1, 0.5, 1.0, 1.0 };
00366 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00367 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00368
00369 glDisable(GL_TEXTURE_2D);
00370 glDisable(GL_CULL_FACE);
00371 glPushMatrix();
00372 glTranslatef(3.0f,-2.0f,-8.0f);
00373 glRotatef(-90,1,0,0);
00374 GLUquadricObj * cylinder = gluNewQuadric();
00375 gluQuadricOrientation(cylinder, GLU_OUTSIDE);
00376 gluCylinder(cylinder,1.0,0.0,2.0,20,20);
00377 glPopMatrix();
00378 glEnable(GL_CULL_FACE);
00379 glEnable(GL_TEXTURE_2D);
00380 }
00381
00382 float GetTimePassed(float& lasttime, int average, float* lastmultiplier)
00383 {
00384 float timeoffset;
00385 float currenttime;
00386
00387
00388
00389
00390 if (lasttime == 0)
00391 lasttime = (float)GetTickCount();
00392
00393 currenttime = (float)GetTickCount();
00394
00395 timeoffset = currenttime - lasttime;
00396
00397 if (timeoffset < 8.333333)
00398 {
00399 timeoffset = 8.333333;
00400 currenttime = lasttime + timeoffset;
00401 }
00402
00403 lasttime = currenttime;
00404
00405 multiplier = timeoffset / 1000;
00406 for (int loop = 0; loop < average - 1; loop++)
00407 {
00408 lastmultiplier[loop] = lastmultiplier[loop + 1];
00409 }
00410 lastmultiplier[average - 1] = multiplier;
00411 for (int loop = 0; loop < average - 1; loop++)
00412 multiplier += lastmultiplier[loop];
00413 multiplier /= (float)average;
00414 if (multiplier)
00415 fps = (int)(1.0 / multiplier);
00416 return multiplier;
00417 }
00418
00419
00420 bool CheckClipPlanes(CAMERA Camera, VECTOR Vect)
00421 {
00422 float ProjectionMatrix[16];
00423 float ModelViewMatrix[16];
00424 float A, B, C, Distance;
00425 int Counter = 0;
00426 glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMatrix);
00427 glGetFloatv(GL_MODELVIEW_MATRIX, ModelViewMatrix);
00428
00429 MultMatrix(ProjectionMatrix, ModelViewMatrix);
00430
00431
00432 A = ProjectionMatrix[0] - ProjectionMatrix[3];
00433 B = ProjectionMatrix[4] - ProjectionMatrix[7];
00434 C = ProjectionMatrix[8] - ProjectionMatrix[11];
00435
00436 Distance = -1 * (A * (-Camera.Position.x + Vect.x) + B *
00437
00438 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z +
00439
00440 Vect.z));
00441 if (Distance > 0)
00442 Counter++;
00443
00444
00445 A = ProjectionMatrix[0] + ProjectionMatrix[3];
00446 B = ProjectionMatrix[4] + ProjectionMatrix[7];
00447 C = ProjectionMatrix[8] + ProjectionMatrix[11];
00448
00449 Distance = A * (-Camera.Position.x + Vect.x) + B *
00450
00451 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00452 if (Distance > 0)
00453 Counter++;
00454
00455
00456 A = ProjectionMatrix[1] - ProjectionMatrix[3];
00457 B = ProjectionMatrix[5] - ProjectionMatrix[7];
00458 C = ProjectionMatrix[9] - ProjectionMatrix[11];
00459
00460 Distance = -1 * (A * (-Camera.Position.x + Vect.x) + B *
00461
00462 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z +
00463
00464 Vect.z));
00465 if (Distance > 0)
00466 Counter++;
00467
00468
00469 A = ProjectionMatrix[1] + ProjectionMatrix[3];
00470 B = ProjectionMatrix[5] + ProjectionMatrix[7];
00471 C = ProjectionMatrix[9] + ProjectionMatrix[11];
00472
00473 Distance = A * (-Camera.Position.x + Vect.x) + B *
00474
00475 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00476 if (Distance > 0)
00477 Counter++;
00478
00479
00480
00481 A = ProjectionMatrix[2] - ProjectionMatrix[3];
00482 B = ProjectionMatrix[6] - ProjectionMatrix[7];
00483 C = ProjectionMatrix[10] - ProjectionMatrix[11];
00484
00485 Distance = A * (-Camera.Position.x + Vect.x) + B *
00486
00487 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00488 if (Distance > 0)
00489 Counter++;
00490
00491
00492
00493 VECTOR Vect2;
00494 Vect2.x = Vect.x - Camera.Position.x;
00495 Vect2.y = Vect.y - Camera.Position.y;
00496 Vect2.z = Vect.z - Camera.Position.z;
00497 if (MagnitudeVector(Vect2) < 200)
00498 Counter++;
00499
00500 if (Counter == 6)
00501 return 1;
00502 else
00503 return 0;
00504 }