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

Generated on Fri Dec 23 05:20:58 2005 for Polygon Selection by doxygen1.2.15