Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

general.cpp

Go to the documentation of this file.
00001 #include "general.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 "glfont.h"
00009 
00010 extern int numCameras;
00011 extern int currentCamera;
00012 extern int numLights;
00013 extern int currentLight;
00014 extern LIGHT* light;
00015 extern int fps;
00016 extern float multiplier;
00017 extern GLFONT myFont;
00018 
00019 void SetGLLighting()         
00020 {
00021     int temp;
00022     for (temp = 0; temp <= numLights; temp++)
00023     {
00024         light[temp].LightNumber = temp;
00025         light[temp].Reset();
00026     }
00027     glEnable(GL_LIGHTING);
00028 }
00029 
00030 void SetGLCamera(CAMERA* camera)          
00031 {
00032     int temp;
00033     for(temp = 0; temp <= numCameras; temp++)
00034         camera[temp].Reset();
00035 }
00036 
00037 void SetGLWorld(POLYGON* polygon)            
00038 {
00039 //Front
00040     polygon[0].Vertex[0].x = -1.0;
00041     polygon[0].Vertex[0].y = -1.0;
00042     polygon[0].Vertex[0].z = -4.0;
00043     polygon[0].Vertex[1].x = 1.0;
00044     polygon[0].Vertex[1].y = -1.0;
00045     polygon[0].Vertex[1].z = -4.0;
00046     polygon[0].Vertex[2].x = 1.0;
00047     polygon[0].Vertex[2].y = 1.0;
00048     polygon[0].Vertex[2].z = -4.0;
00049 
00050     polygon[1].Vertex[0].x = -1.0;
00051     polygon[1].Vertex[0].y = -1.0;
00052     polygon[1].Vertex[0].z = -4.0;
00053     polygon[1].Vertex[1].x = 1.0;
00054     polygon[1].Vertex[1].y = 1.0;
00055     polygon[1].Vertex[1].z = -4.0;
00056     polygon[1].Vertex[2].x = -1.0;
00057     polygon[1].Vertex[2].y = 1.0;
00058     polygon[1].Vertex[2].z = -4.0;
00059 //Back
00060     polygon[2].Vertex[0].x = 1.0;
00061     polygon[2].Vertex[0].y = -1.0;
00062     polygon[2].Vertex[0].z = -6.0;
00063     polygon[2].Vertex[1].x = -1.0;
00064     polygon[2].Vertex[1].y = -1.0;
00065     polygon[2].Vertex[1].z = -6.0;
00066     polygon[2].Vertex[2].x = -1.0;
00067     polygon[2].Vertex[2].y = 1.0;
00068     polygon[2].Vertex[2].z = -6.0;
00069 
00070     polygon[3].Vertex[0].x = 1.0;
00071     polygon[3].Vertex[0].y = -1.0;
00072     polygon[3].Vertex[0].z = -6.0;
00073     polygon[3].Vertex[1].x = -1.0;
00074     polygon[3].Vertex[1].y = 1.0;
00075     polygon[3].Vertex[1].z = -6.0;
00076     polygon[3].Vertex[2].x = 1.0;
00077     polygon[3].Vertex[2].y = 1.0;
00078     polygon[3].Vertex[2].z = -6.0;
00079 //Left
00080     polygon[4].Vertex[0].x = -1.0;
00081     polygon[4].Vertex[0].y = -1.0;
00082     polygon[4].Vertex[0].z = -6.0;
00083     polygon[4].Vertex[1].x = -1.0;
00084     polygon[4].Vertex[1].y = -1.0;
00085     polygon[4].Vertex[1].z = -4.0;
00086     polygon[4].Vertex[2].x = -1.0;
00087     polygon[4].Vertex[2].y = 1.0;
00088     polygon[4].Vertex[2].z = -4.0;
00089 
00090     polygon[5].Vertex[0].x = -1.0;
00091     polygon[5].Vertex[0].y = -1.0;
00092     polygon[5].Vertex[0].z = -6.0;
00093     polygon[5].Vertex[1].x = -1.0;
00094     polygon[5].Vertex[1].y = 1.0;
00095     polygon[5].Vertex[1].z = -4.0;
00096     polygon[5].Vertex[2].x = -1.0;
00097     polygon[5].Vertex[2].y = 1.0;
00098     polygon[5].Vertex[2].z = -6.0;
00099 //Right
00100     polygon[6].Vertex[0].x = 1.0;
00101     polygon[6].Vertex[0].y = -1.0;
00102     polygon[6].Vertex[0].z = -4.0;
00103     polygon[6].Vertex[1].x = 1.0;
00104     polygon[6].Vertex[1].y = -1.0;
00105     polygon[6].Vertex[1].z = -6.0;
00106     polygon[6].Vertex[2].x = 1.0;
00107     polygon[6].Vertex[2].y = 1.0;
00108     polygon[6].Vertex[2].z = -6.0;
00109 
00110     polygon[7].Vertex[0].x = 1.0;
00111     polygon[7].Vertex[0].y = -1.0;
00112     polygon[7].Vertex[0].z = -4.0;
00113     polygon[7].Vertex[1].x = 1.0;
00114     polygon[7].Vertex[1].y = 1.0;
00115     polygon[7].Vertex[1].z = -6.0;
00116     polygon[7].Vertex[2].x = 1.0;
00117     polygon[7].Vertex[2].y = 1.0;
00118     polygon[7].Vertex[2].z = -4.0;
00119 //Top
00120     polygon[8].Vertex[0].x = 1.0;
00121     polygon[8].Vertex[0].y = 1.0;
00122     polygon[8].Vertex[0].z = -4.0;
00123     polygon[8].Vertex[1].x = 1.0;
00124     polygon[8].Vertex[1].y = 1.0;
00125     polygon[8].Vertex[1].z = -6.0;
00126     polygon[8].Vertex[2].x = -1.0;
00127     polygon[8].Vertex[2].y = 1.0;
00128     polygon[8].Vertex[2].z = -6.0;
00129 
00130     polygon[9].Vertex[0].x = 1.0;
00131     polygon[9].Vertex[0].y = 1.0;
00132     polygon[9].Vertex[0].z = -4.0;
00133     polygon[9].Vertex[1].x = -1.0;
00134     polygon[9].Vertex[1].y = 1.0;
00135     polygon[9].Vertex[1].z = -6.0;
00136     polygon[9].Vertex[2].x = -1.0;
00137     polygon[9].Vertex[2].y = 1.0;
00138     polygon[9].Vertex[2].z = -4.0;
00139 //Bottom
00140     polygon[10].Vertex[0].x = -1.0;
00141     polygon[10].Vertex[0].y = -1.0;
00142     polygon[10].Vertex[0].z = -4.0;
00143     polygon[10].Vertex[1].x = -1.0;
00144     polygon[10].Vertex[1].y = -1.0;
00145     polygon[10].Vertex[1].z = -6.0;
00146     polygon[10].Vertex[2].x = 1.0;
00147     polygon[10].Vertex[2].y = -1.0;
00148     polygon[10].Vertex[2].z = -6.0;
00149 
00150     polygon[11].Vertex[0].x = -1.0;
00151     polygon[11].Vertex[0].y = -1.0;
00152     polygon[11].Vertex[0].z = -4.0;
00153     polygon[11].Vertex[1].x = 1.0;
00154     polygon[11].Vertex[1].y = -1.0;
00155     polygon[11].Vertex[1].z = -6.0;
00156     polygon[11].Vertex[2].x = 1.0;
00157     polygon[11].Vertex[2].y = -1.0;
00158     polygon[11].Vertex[2].z = -4.0;
00159 
00160     polygon[0].SetNormal();
00161     polygon[1].SetNormal();
00162     polygon[2].SetNormal();
00163     polygon[3].SetNormal();
00164     polygon[4].SetNormal();
00165     polygon[5].SetNormal();
00166     polygon[6].SetNormal();
00167     polygon[7].SetNormal();
00168     polygon[8].SetNormal();
00169     polygon[9].SetNormal();
00170     polygon[10].SetNormal();
00171     polygon[11].SetNormal();
00172 }
00173 
00174 void SetGLProperties()
00175 {
00176     glCullFace(GL_BACK);
00177     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00178     glClearDepth(1.0);
00179     glDepthFunc(GL_LESS);
00180     glEnable(GL_DEPTH_TEST);
00181     glShadeModel(GL_SMOOTH);
00182     glEnable(GL_NORMALIZE);
00183     glEnable(GL_CULL_FACE);
00184 }
00185 
00186 void SetGLProjection(int Width, int Height)
00187 {
00188     if (Height==0)
00189         Height=1;
00190     glViewport(0, 0, Width, Height);
00191     glMatrixMode(GL_PROJECTION);
00192     glLoadIdentity();
00193     gluPerspective(45.0,(float)Width/(float)Height,0.5,200.0);
00194 }
00195 
00196 void SetGLView(int Width, int Height)
00197 {
00198     SetGLProjection(Width, Height);
00199     glMatrixMode(GL_MODELVIEW);
00200     glLoadIdentity();
00201 }
00202 
00203 void SetGLMaterial()
00204 {
00205     float mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
00206     float mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00207     float mat_specular[] = { 0.9, 0.9, 0.9, 1.0 };
00208     float mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
00209     float mat_shininess[] = { 80.0 };
00210 
00211     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00212     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00213     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00214     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00215     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
00216 }
00217 
00218 void SetGLTexture(TEXTURE* texture)
00219 {
00220     sprintf(texture[0].TexName, "%s", "tile.tga");
00221     texture[0].LoadTGA();
00222 
00223     sprintf(texture[1].TexName, "%s", "north.tga");
00224     texture[1].LoadTGA();
00225 
00226     sprintf(texture[2].TexName, "%s", "east.tga");
00227     texture[2].LoadTGA();
00228 
00229     sprintf(texture[3].TexName, "%s", "south.tga");
00230     texture[3].LoadTGA();
00231 
00232     sprintf(texture[4].TexName, "%s", "west.tga");
00233     texture[4].LoadTGA();
00234 
00235     sprintf(texture[5].TexName, "%s", "sky.tga");
00236     texture[5].LoadTGA();
00237 
00238     sprintf(texture[6].TexName, "%s", "glare.tga");
00239     texture[6].LoadTGA();
00240 
00241     sprintf(texture[7].TexName, "%s", "terrain.tga");
00242     texture[7].LoadTGA();
00243 }
00244 
00245 void DrawMyText()
00246 {
00247     glBlendFunc(GL_ONE,  GL_ONE_MINUS_SRC_ALPHA) ;
00248     glDisable(GL_LIGHTING);
00249     glColor3f(0.0, 1.0, 0.0);
00250     glEnable(GL_BLEND);
00251     glDisable(GL_DEPTH_TEST);
00252     glPushMatrix();
00253     glLoadIdentity();
00254     glFontBegin(&myFont);
00255     char text[256];
00256 
00257     glFontTextOut("Tutorial #18  (Skybox & Lensflare)", -52, 40, -100);
00258 
00259     sprintf(text, "FPS = %d", fps);
00260     glFontTextOut(text, -52, 38, -100);
00261 
00262     sprintf(text, "%s", "Press R to change the lensflare");
00263     glFontTextOut(text, -52, 34, -100);
00264 
00265     glFontEnd();
00266     glPopMatrix();
00267     glEnable(GL_DEPTH_TEST);
00268     glDisable(GL_BLEND);
00269     glEnable(GL_LIGHTING);
00270 }
00271 
00272 void DrawSkybox(CAMERA* camera, TEXTURE* texture)
00273 {
00274     VECTOR facenormal;
00275     VECTOR SkyboxVertex[8];
00276 
00277     // make the skybox relative to the camera position
00278     SkyboxVertex[0].x = camera[currentCamera].Position.x - 50.0;
00279     SkyboxVertex[0].y = camera[currentCamera].Position.y - 10.0;
00280     SkyboxVertex[0].z = camera[currentCamera].Position.z + 50.0;
00281 
00282     SkyboxVertex[1].x = camera[currentCamera].Position.x - 50.0;
00283     SkyboxVertex[1].y = camera[currentCamera].Position.y - 10.0;
00284     SkyboxVertex[1].z = camera[currentCamera].Position.z - 50.0;
00285 
00286     SkyboxVertex[2].x = camera[currentCamera].Position.x + 50.0;
00287     SkyboxVertex[2].y = camera[currentCamera].Position.y - 10.0;
00288     SkyboxVertex[2].z = camera[currentCamera].Position.z - 50.0;
00289 
00290     SkyboxVertex[3].x = camera[currentCamera].Position.x + 50.0;
00291     SkyboxVertex[3].y = camera[currentCamera].Position.y - 10.0;
00292     SkyboxVertex[3].z = camera[currentCamera].Position.z + 50.0;
00293 
00294     SkyboxVertex[4].x = camera[currentCamera].Position.x - 50.0;
00295     SkyboxVertex[4].y = camera[currentCamera].Position.y + 20.0;
00296     SkyboxVertex[4].z = camera[currentCamera].Position.z + 50.0;
00297 
00298     SkyboxVertex[5].x = camera[currentCamera].Position.x - 50.0;
00299     SkyboxVertex[5].y = camera[currentCamera].Position.y + 20.0;
00300     SkyboxVertex[5].z = camera[currentCamera].Position.z - 50.0;
00301 
00302     SkyboxVertex[6].x = camera[currentCamera].Position.x + 50.0;
00303     SkyboxVertex[6].y = camera[currentCamera].Position.y + 20.0;
00304     SkyboxVertex[6].z = camera[currentCamera].Position.z - 50.0;
00305 
00306     SkyboxVertex[7].x = camera[currentCamera].Position.x + 50.0;
00307     SkyboxVertex[7].y = camera[currentCamera].Position.y + 20.0;
00308     SkyboxVertex[7].z = camera[currentCamera].Position.z + 50.0;
00309 
00310     glDisable(GL_CULL_FACE);
00311     glDisable(GL_LIGHTING);
00312     glPushMatrix();
00313     glColor3f(1.0f, 1.0f, 1.0f);
00314 
00315     // Left Face
00316     glBindTexture(GL_TEXTURE_2D, texture[1].TexID);
00317     glBegin(GL_QUADS);
00318         facenormal = GetNormal(SkyboxVertex[6], SkyboxVertex[0], SkyboxVertex[3]);
00319         glNormal3fv(&facenormal.x);
00320         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&SkyboxVertex[0].x);
00321         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&SkyboxVertex[1].x);
00322         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&SkyboxVertex[5].x);
00323         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&SkyboxVertex[4].x);
00324     glEnd();
00325 
00326     // Back Face
00327     glBindTexture(GL_TEXTURE_2D, texture[2].TexID);
00328     glBegin(GL_QUADS);
00329         facenormal = GetNormal(SkyboxVertex[4], SkyboxVertex[5], SkyboxVertex[7]);
00330         glNormal3fv(&facenormal.x);
00331         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&SkyboxVertex[1].x);
00332         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&SkyboxVertex[2].x);
00333         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&SkyboxVertex[6].x);
00334         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&SkyboxVertex[5].x);
00335     glEnd();
00336 
00337     // Right Face
00338     glBindTexture(GL_TEXTURE_2D, texture[3].TexID);
00339     glBegin(GL_QUADS);
00340         facenormal = GetNormal(SkyboxVertex[1], SkyboxVertex[7], SkyboxVertex[5]);
00341         glNormal3fv(&facenormal.x);
00342         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&SkyboxVertex[2].x);
00343         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&SkyboxVertex[3].x);
00344         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&SkyboxVertex[7].x);
00345         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&SkyboxVertex[6].x);
00346     glEnd();
00347 
00348     // Front Face
00349     glBindTexture(GL_TEXTURE_2D, texture[4].TexID);
00350     glBegin(GL_QUADS);
00351         glColor3f(1.0f,1.0f,1.0f);
00352         facenormal = GetNormal(SkyboxVertex[0], SkyboxVertex[1], SkyboxVertex[2]);
00353         glNormal3fv(&facenormal.x);
00354         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&SkyboxVertex[3].x);
00355         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&SkyboxVertex[0].x);
00356         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&SkyboxVertex[4].x);
00357         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&SkyboxVertex[7].x);
00358     glEnd();
00359 
00360     // Top Face
00361     glBindTexture(GL_TEXTURE_2D, texture[5].TexID);
00362     glBegin(GL_QUADS);
00363         facenormal = GetNormal(SkyboxVertex[3], SkyboxVertex[2], SkyboxVertex[5]);
00364         glNormal3fv(&facenormal.x);
00365         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&SkyboxVertex[7].x);
00366         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&SkyboxVertex[4].x);
00367         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&SkyboxVertex[5].x);
00368         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&SkyboxVertex[6].x);
00369     glEnd();
00370 
00371     glPopMatrix();
00372     glEnable(GL_CULL_FACE);
00373     glEnable(GL_LIGHTING);
00374 }
00375 
00376 void DrawTerrain(TEXTURE* texture)
00377 {
00378     float mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
00379     float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00380     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00381     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00382 
00383     glBindTexture(GL_TEXTURE_2D, texture[7].TexID);
00384 
00385     glDisable(GL_CULL_FACE);
00386     glPushMatrix();
00387     glBegin(GL_QUADS);
00388         glNormal3f(0.0, 1.0, 0.0);
00389         glTexCoord2f( 0.0f, 0.0f );
00390         glVertex3f(-10.0, -2.0, 10.0);
00391         glTexCoord2f( 1.0f, 0.0f );
00392         glVertex3f(10.0, -2.0, 10.0);
00393         glTexCoord2f( 1.0f, 1.0f );
00394         glVertex3f(10.0, -2.0, -10.0);
00395         glTexCoord2f( 0.0f, 1.0f );
00396         glVertex3f(-10, -2.0, -10.0);
00397     glEnd();
00398     glPopMatrix();
00399     glEnable(GL_CULL_FACE);
00400 }
00401 
00402 void DrawGrid()
00403 {
00404     glDisable(GL_TEXTURE_2D);
00405     glDisable(GL_LIGHTING);
00406     glPushMatrix();
00407     glTranslatef(0,-2.0,0);
00408     float Line = -10;
00409     int Grid;
00410     glBegin(GL_LINES);
00411     for(Grid = 0; Grid <= 20; Grid += 1)
00412     {
00413         glColor3f(0.0f,1.0f,0.0f);
00414         glVertex3f(Line + Grid, 0, -10);
00415         glVertex3f(Line + Grid, 0, 10);
00416         glVertex3f(-10, 0, Line + Grid);
00417         glVertex3f(10, 0, Line + Grid);
00418     }
00419     glEnd();
00420     glPopMatrix();
00421     glEnable(GL_TEXTURE_2D);
00422     glEnable(GL_LIGHTING);
00423 }
00424 
00425 void DrawCube(POLYGON* polygon, TEXTURE* texture)
00426 {
00427     float mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
00428     float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00429     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00430     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00431 
00432     glBindTexture(GL_TEXTURE_2D, texture[0].TexID);
00433 
00434     glPushMatrix();
00435     glBegin(GL_TRIANGLES);
00436         // Front Face
00437             glNormal3fv(&polygon[0].Vertex[0].nx);
00438         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[0].x);
00439         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[1].x);
00440         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[0].Vertex[2].x);
00441 
00442         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[1].Vertex[0].x);
00443         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[1].x);
00444         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[2].x);
00445         // Back Face
00446             glNormal3fv(&polygon[2].Vertex[0].nx);
00447         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[0].x);
00448         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[1].x);
00449         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[2].Vertex[2].x);
00450 
00451         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[3].Vertex[0].x);
00452         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[1].x);
00453         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[2].x);
00454         // Top Face
00455             glNormal3fv(&polygon[4].Vertex[0].nx);
00456         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[0].x);
00457         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[1].x);
00458         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[4].Vertex[2].x);
00459 
00460         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[5].Vertex[0].x);
00461         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[1].x);
00462         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[2].x);
00463         // Bottom Face
00464             glNormal3fv(&polygon[6].Vertex[0].nx);
00465         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[0].x);
00466         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[1].x);
00467         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[6].Vertex[2].x);
00468 
00469         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[7].Vertex[0].x);
00470         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[1].x);
00471         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[2].x);
00472         // Right face
00473             glNormal3fv(&polygon[8].Vertex[0].nx);
00474         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[0].x);
00475         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[1].x);
00476         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[8].Vertex[2].x);
00477 
00478         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[9].Vertex[0].x);
00479         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[1].x);
00480         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[2].x);
00481         // Left Face
00482             glNormal3fv(&polygon[10].Vertex[0].nx);
00483         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[0].x);
00484         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[1].x);
00485         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[10].Vertex[2].x);
00486 
00487         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[11].Vertex[0].x);
00488         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[1].x);
00489         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[2].x);
00490     glEnd();
00491     glPopMatrix();
00492 }
00493 
00494 void DrawLightSphere()
00495 {
00496     float mat_ambient[] = { 0.8, 0.5, 0.1, 1.0 };
00497     float mat_diffuse[] = { 0.8, 0.5, 0.1, 1.0 };
00498     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00499     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00500     glDisable(GL_TEXTURE_2D);
00501     glPushMatrix();
00502     glTranslatef(light[currentLight].Position.x, light[currentLight].Position.y, light[currentLight].Position.z);
00503     GLUquadricObj * sphere = gluNewQuadric();
00504     gluQuadricOrientation(sphere, GLU_OUTSIDE);
00505     gluSphere(sphere,3.0,20,20);
00506     glPopMatrix();
00507     glEnable(GL_TEXTURE_2D);
00508 }
00509 
00510 void DrawGreenSphere()
00511 {
00512     float mat_ambient[] = { 0.2, 1.0, 0.1, 1.0 };
00513     float mat_diffuse[] = { 0.2, 1.0, 0.1, 1.0 };
00514     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00515     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00516     glDisable(GL_TEXTURE_2D);
00517     glPushMatrix();
00518     glTranslatef(-2.0, -2.0, -8.0);
00519     GLUquadricObj * sphere = gluNewQuadric();
00520     gluQuadricOrientation(sphere, GLU_OUTSIDE);
00521     gluSphere(sphere,0.3,20,20);
00522     glPopMatrix();
00523     glEnable(GL_TEXTURE_2D);
00524 }
00525 
00526 void DrawSphere()
00527 {
00528     float mat_ambient[] = { 0.8, 0.5, 0.1, 1.0 };
00529     float mat_diffuse[] = { 0.8, 0.5, 0.1, 1.0 };
00530     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00531     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00532 
00533     glDisable(GL_TEXTURE_2D);
00534     glPushMatrix();
00535     glTranslatef(-3.0f,-1.0f,-8.0f);
00536     GLUquadricObj * sphere = gluNewQuadric();
00537     gluQuadricOrientation(sphere, GLU_OUTSIDE);
00538     gluSphere(sphere,1.0,50,50);
00539     glPopMatrix();
00540     glEnable(GL_TEXTURE_2D);
00541 }
00542 
00543 void DrawCone()
00544 {
00545     float mat_ambient[] = { 0.1, 0.5, 1.0, 1.0 };
00546     float mat_diffuse[] = { 0.1, 0.5, 1.0, 1.0 };
00547     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00548     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00549 
00550     glDisable(GL_TEXTURE_2D);
00551     glDisable(GL_CULL_FACE);
00552     glPushMatrix();
00553     glTranslatef(3.0f,-2.0f,-8.0f);
00554     glRotatef(-90,1,0,0);
00555     GLUquadricObj * cylinder = gluNewQuadric();
00556     gluQuadricOrientation(cylinder, GLU_OUTSIDE);
00557     gluCylinder(cylinder,1.0,0.0,2.0,20,20);
00558     glPopMatrix();
00559     glEnable(GL_CULL_FACE);
00560     glEnable(GL_TEXTURE_2D);
00561 }
00562 
00563 float GetTimePassed(float& lasttime, int average, float* lastmultiplier)
00564 {
00565     float timeoffset;
00566     float currenttime;
00567 /*
00568 If the program has just started, set last tickcount to the current tickcount.
00569 This prevents the program from thinking that several hours have passed since the last frame.
00570 */
00571     if (lasttime == 0)
00572         lasttime = (float)GetTickCount();
00573         // Get the current time
00574     currenttime = (float)GetTickCount();
00575         // Calculate the offset
00576     timeoffset = currenttime - lasttime;
00577     // If timeoffset less than 1/120 of a second (in milliseconds) then set to minimum offset
00578     if (timeoffset < 8.333333)
00579     {
00580         timeoffset = 8.333333;
00581         currenttime = lasttime + timeoffset;
00582     }
00583     // Put the current time in the lasttime variable
00584         lasttime = currenttime;
00585     // return the time offset in seconds per frame
00586         multiplier = timeoffset / 1000;
00587     for (int loop = 0; loop < average - 1; loop++)
00588     {
00589         lastmultiplier[loop] = lastmultiplier[loop + 1];
00590     }
00591     lastmultiplier[average - 1] = multiplier;
00592     for (int loop = 0; loop < average - 1; loop++)
00593         multiplier += lastmultiplier[loop];
00594     multiplier /= (float)average; 
00595         if (multiplier)
00596         fps = (int)(1.0 / multiplier);
00597     return multiplier;
00598 }
00599 
00600 bool CheckClipPlanes(CAMERA Camera, VECTOR Vect)
00601 {
00602   float ProjectionMatrix[16];
00603   float ModelViewMatrix[16];
00604   float A, B, C, Distance;
00605   int Counter = 0;
00606   glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMatrix);
00607   glGetFloatv(GL_MODELVIEW_MATRIX, ModelViewMatrix);
00608 
00609   MultMatrix(ProjectionMatrix, ModelViewMatrix);
00610 
00611   //right clipping plane
00612   A = ProjectionMatrix[0] - ProjectionMatrix[3];
00613   B = ProjectionMatrix[4] - ProjectionMatrix[7];
00614   C = ProjectionMatrix[8] - ProjectionMatrix[11];
00615 
00616   Distance = -1 * (A * (-Camera.Position.x + Vect.x) + B * (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z));
00617   if (Distance > 0)
00618     Counter++;
00619 
00620   //left clipping plane
00621   A = ProjectionMatrix[0] + ProjectionMatrix[3];
00622   B = ProjectionMatrix[4] + ProjectionMatrix[7];
00623   C = ProjectionMatrix[8] + ProjectionMatrix[11];
00624 
00625   Distance = A * (-Camera.Position.x + Vect.x) + B * (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00626   if (Distance > 0)
00627     Counter++;
00628 
00629   //top clipping plane
00630   A = ProjectionMatrix[1] - ProjectionMatrix[3];
00631   B = ProjectionMatrix[5] - ProjectionMatrix[7];
00632   C = ProjectionMatrix[9] - ProjectionMatrix[11];
00633 
00634   Distance = -1 * (A * (-Camera.Position.x + Vect.x) + B * (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z));
00635   if (Distance > 0)
00636     Counter++;
00637 
00638   //bottom clipping plane
00639   A = ProjectionMatrix[1] + ProjectionMatrix[3];
00640   B = ProjectionMatrix[5] + ProjectionMatrix[7];
00641   C = ProjectionMatrix[9] + ProjectionMatrix[11];
00642 
00643   Distance = A * (-Camera.Position.x + Vect.x) + B * (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00644   if (Distance > 0)
00645     Counter++;
00646 
00647   // near clipping plane (might not be necessary when the near frustrum plane is close, but just in case)
00648   A = ProjectionMatrix[2] - ProjectionMatrix[3];
00649   B = ProjectionMatrix[6] - ProjectionMatrix[7];
00650   C = ProjectionMatrix[10] - ProjectionMatrix[11];
00651 
00652   Distance = A * (-Camera.Position.x + Vect.x) + B * (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00653   if (Distance > 0)
00654     Counter++;
00655 
00656   // far clipping plane (the equation didn't work for the far plane, so I'll just use a distance test)
00657   VECTOR Vect2;
00658   Vect2.x = Vect.x - Camera.Position.x;
00659   Vect2.y = Vect.y - Camera.Position.y;
00660   Vect2.z = Vect.z - Camera.Position.z;
00661   if (MagnitudeVector(Vect2) < 200)
00662     Counter++;
00663 
00664   if (Counter == 6)
00665     return 1;
00666   else
00667     return 0;
00668 }

Generated on Fri Dec 23 05:21:20 2005 for Skybox by doxygen1.2.15