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 <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 "bspline.h"
00009 #include "glFont.h"
00010 #include "tll.h"
00011 #include "mmgr.h"
00012 
00013 extern int numCameras;
00014 extern int cameraMode;
00015 extern int currentCamera;
00016 extern int numLights;
00017 extern int currentLight;
00018 extern int fps;
00019 extern float multiplier;
00020 extern GLFONT myFont;
00021 extern char HaloTypeName[MAX_PATH];
00022 extern char FireTypeName[MAX_PATH];
00023 extern int numSplines;
00024 extern int lookAtPath;
00025 extern int currentSpline;
00026 extern SPLINE* spline;
00027 extern char SplineFileName[MAX_PATH];
00028 
00029 void AddSpline(int Number, LinkedList<SPLINE>& SplineList)
00030 {
00031     SPLINE* spline2 = new SPLINE;
00032     spline2->Active = 1;
00033     spline2->Repeat = 1;
00034     spline2->Degree = 3;
00035     spline2->NumControl = 7;
00036     spline2->NumPoints = 100;
00037     spline2->Control = new VECTOR[100];
00038     spline2->Output = new VECTOR[1000];
00039     spline2->StartTime = 5000;
00040     spline2->EndTime = 25000;
00041     spline2->CopyOfStartTime = spline2->StartTime;
00042     spline2->CopyOfEndTime = spline2->EndTime;
00043 
00044     spline2->Red = ((float)(rand()%226) + 30.0) / 255;
00045     spline2->Green = ((float)(rand()%226) + 30.0) / 255;
00046     spline2->Blue = ((float)(rand()%226) + 30.0) / 255;
00047 
00048     for (int loop = 0; loop < 100; loop++)
00049     {
00050         spline2->Control[loop].x = rand()%60 - 29;  spline2->Control[loop].y = rand()%60 - 29;  spline2->Control[loop].z = rand()%60 - 29;
00051     }
00052 
00053     spline2->linkPosition = Number;   //Set the link position of the spline
00054     SplineList.Insert(spline2);       //Insert spline in linked list
00055 }
00056 
00057 void DeleteSpline(int Number, LinkedList<SPLINE>& SplineList)
00058 {
00059     SPLINE* temp = SplineList.Get(Number);
00060     delete[] temp->Control;
00061     delete[] temp->Output;
00062     SplineList.Delete(Number);
00063     delete temp;
00064 }
00065 
00066 int LoadSplines(char* SplineFileName, LinkedList<SPLINE>& SplineList)
00067 {
00068     const int stringLength = 33;
00069     char tempString[stringLength];
00070     FILE* SplineFile;
00071     SplineFile = fopen(SplineFileName, "rt");
00072     if (!SplineFile)
00073     {
00074         MessageBox(NULL, "Spline File didn't open", "Error", MB_OK | MB_ICONERROR);
00075         return 0;
00076     }
00077     fseek(SplineFile, 0, SEEK_SET);
00078     int InitialNumSplines = numSplines;
00079     if (!fgets(tempString, stringLength, SplineFile))
00080     {
00081         MessageBox(NULL, "Error reading from the spline data file", "Error", MB_OK | MB_ICONERROR);
00082         numSplines = InitialNumSplines;
00083         return FALSE;
00084     }
00085     numSplines = atoi(tempString);
00086     if (InitialNumSplines > numSplines)
00087     {
00088         for (int loop = InitialNumSplines - 1; loop >= numSplines; loop--)
00089         {
00090             DeleteSpline(loop, SplineList);
00091         }
00092     }
00093     if (numSplines > InitialNumSplines)
00094     {
00095         for (int loop = InitialNumSplines; loop < numSplines; loop++)
00096         {
00097             AddSpline(loop, SplineList);
00098         }
00099     }
00100     for (int loop = 0; loop < numSplines; loop++)
00101     {
00102         spline = SplineList.Get(loop);
00103         // Active flag
00104         fgets(tempString, stringLength, SplineFile);
00105         spline->Active = atoi(tempString);
00106         // Repeat flag
00107         fgets(tempString, stringLength, SplineFile);
00108         spline->Repeat = atoi(tempString);
00109         // Degree
00110         fgets(tempString, stringLength, SplineFile);
00111         spline->Degree = atoi(tempString);
00112         // NumControl
00113         fgets(tempString, stringLength, SplineFile);
00114         spline->NumControl = atoi(tempString);
00115         // NumPoints
00116         fgets(tempString, stringLength, SplineFile);
00117         spline->NumPoints = atoi(tempString);
00118         // StartTime
00119         fgets(tempString, stringLength, SplineFile);
00120         spline->StartTime = atof(tempString);
00121         // EndTime
00122         fgets(tempString, stringLength, SplineFile);
00123         spline->EndTime = atof(tempString);
00124         // Red
00125         fgets(tempString, stringLength, SplineFile);
00126         spline->Red = atof(tempString);
00127         // Green
00128         fgets(tempString, stringLength, SplineFile);
00129         spline->Green = atof(tempString);
00130         // Blue
00131         fgets(tempString, stringLength, SplineFile);
00132         spline->Blue = atof(tempString);
00133 
00134         for (int loop2 = 0; loop2 <= spline->NumControl; loop2++)
00135         {
00136             fgets(tempString, stringLength, SplineFile);
00137             spline->Control[loop2].x = atof(tempString);
00138             fgets(tempString, stringLength, SplineFile);
00139             spline->Control[loop2].y = atof(tempString);
00140             fgets(tempString, stringLength, SplineFile);
00141             spline->Control[loop2].z = atof(tempString);
00142         }
00143         spline->CopyOfStartTime = spline->StartTime;
00144         spline->CopyOfEndTime = spline->EndTime;
00145     }
00146 
00147     currentSpline = numSplines - 1;
00148     if (lookAtPath >= numSplines)
00149         lookAtPath = numSplines - 1;
00150     if (fclose(SplineFile))
00151         MessageBox(NULL, "File didn't close", "Error", MB_OK | MB_ICONERROR);
00152     return 1;
00153 }
00154 
00155 void SetSplines(LinkedList<SPLINE>& SplineList)
00156 {
00157     // Seed random number generator
00158     srand((unsigned)time(NULL));
00159     
00160     //Create a new spline
00161     for (int loop = 0; loop < numSplines; loop++)
00162     {
00163         AddSpline(loop, SplineList);
00164     }
00165     //Load splines from data file
00166     LoadSplines(SplineFileName, SplineList);
00167 }
00168 
00169 void SetGLLighting(LIGHT* light)         // Loop through and reset all the lights
00170 {
00171     int temp;
00172     for (temp = 0; temp <= numLights; temp++)
00173     {
00174         light[temp].LightNumber = temp;
00175         light[temp].Reset();
00176     }
00177     glEnable(GL_LIGHTING);
00178 }
00179 
00180 void SetGLCamera(CAMERA* camera)          // Loop through and reset all the cameras
00181 {
00182     int temp;
00183     for(temp = 0; temp <= numCameras; temp++)
00184         camera[temp].Reset();
00185 }
00186 
00187 void SetGLWorld(POLYGON* polygon)            // Initialize the world data 
00188 {
00189 //Front
00190     polygon[0].Vertex[0].x = -1.0;
00191     polygon[0].Vertex[0].y = -1.0;
00192     polygon[0].Vertex[0].z = -4.0;
00193     polygon[0].Vertex[1].x = 1.0;
00194     polygon[0].Vertex[1].y = -1.0;
00195     polygon[0].Vertex[1].z = -4.0;
00196     polygon[0].Vertex[2].x = 1.0;
00197     polygon[0].Vertex[2].y = 1.0;
00198     polygon[0].Vertex[2].z = -4.0;
00199 
00200     polygon[1].Vertex[0].x = -1.0;
00201     polygon[1].Vertex[0].y = -1.0;
00202     polygon[1].Vertex[0].z = -4.0;
00203     polygon[1].Vertex[1].x = 1.0;
00204     polygon[1].Vertex[1].y = 1.0;
00205     polygon[1].Vertex[1].z = -4.0;
00206     polygon[1].Vertex[2].x = -1.0;
00207     polygon[1].Vertex[2].y = 1.0;
00208     polygon[1].Vertex[2].z = -4.0;
00209 //Back
00210     polygon[2].Vertex[0].x = 1.0;
00211     polygon[2].Vertex[0].y = -1.0;
00212     polygon[2].Vertex[0].z = -6.0;
00213     polygon[2].Vertex[1].x = -1.0;
00214     polygon[2].Vertex[1].y = -1.0;
00215     polygon[2].Vertex[1].z = -6.0;
00216     polygon[2].Vertex[2].x = -1.0;
00217     polygon[2].Vertex[2].y = 1.0;
00218     polygon[2].Vertex[2].z = -6.0;
00219 
00220     polygon[3].Vertex[0].x = 1.0;
00221     polygon[3].Vertex[0].y = -1.0;
00222     polygon[3].Vertex[0].z = -6.0;
00223     polygon[3].Vertex[1].x = -1.0;
00224     polygon[3].Vertex[1].y = 1.0;
00225     polygon[3].Vertex[1].z = -6.0;
00226     polygon[3].Vertex[2].x = 1.0;
00227     polygon[3].Vertex[2].y = 1.0;
00228     polygon[3].Vertex[2].z = -6.0;
00229 //Left
00230     polygon[4].Vertex[0].x = -1.0;
00231     polygon[4].Vertex[0].y = -1.0;
00232     polygon[4].Vertex[0].z = -6.0;
00233     polygon[4].Vertex[1].x = -1.0;
00234     polygon[4].Vertex[1].y = -1.0;
00235     polygon[4].Vertex[1].z = -4.0;
00236     polygon[4].Vertex[2].x = -1.0;
00237     polygon[4].Vertex[2].y = 1.0;
00238     polygon[4].Vertex[2].z = -4.0;
00239 
00240     polygon[5].Vertex[0].x = -1.0;
00241     polygon[5].Vertex[0].y = -1.0;
00242     polygon[5].Vertex[0].z = -6.0;
00243     polygon[5].Vertex[1].x = -1.0;
00244     polygon[5].Vertex[1].y = 1.0;
00245     polygon[5].Vertex[1].z = -4.0;
00246     polygon[5].Vertex[2].x = -1.0;
00247     polygon[5].Vertex[2].y = 1.0;
00248     polygon[5].Vertex[2].z = -6.0;
00249 //Right
00250     polygon[6].Vertex[0].x = 1.0;
00251     polygon[6].Vertex[0].y = -1.0;
00252     polygon[6].Vertex[0].z = -4.0;
00253     polygon[6].Vertex[1].x = 1.0;
00254     polygon[6].Vertex[1].y = -1.0;
00255     polygon[6].Vertex[1].z = -6.0;
00256     polygon[6].Vertex[2].x = 1.0;
00257     polygon[6].Vertex[2].y = 1.0;
00258     polygon[6].Vertex[2].z = -6.0;
00259 
00260     polygon[7].Vertex[0].x = 1.0;
00261     polygon[7].Vertex[0].y = -1.0;
00262     polygon[7].Vertex[0].z = -4.0;
00263     polygon[7].Vertex[1].x = 1.0;
00264     polygon[7].Vertex[1].y = 1.0;
00265     polygon[7].Vertex[1].z = -6.0;
00266     polygon[7].Vertex[2].x = 1.0;
00267     polygon[7].Vertex[2].y = 1.0;
00268     polygon[7].Vertex[2].z = -4.0;
00269 //Top
00270     polygon[8].Vertex[0].x = 1.0;
00271     polygon[8].Vertex[0].y = 1.0;
00272     polygon[8].Vertex[0].z = -4.0;
00273     polygon[8].Vertex[1].x = 1.0;
00274     polygon[8].Vertex[1].y = 1.0;
00275     polygon[8].Vertex[1].z = -6.0;
00276     polygon[8].Vertex[2].x = -1.0;
00277     polygon[8].Vertex[2].y = 1.0;
00278     polygon[8].Vertex[2].z = -6.0;
00279 
00280     polygon[9].Vertex[0].x = 1.0;
00281     polygon[9].Vertex[0].y = 1.0;
00282     polygon[9].Vertex[0].z = -4.0;
00283     polygon[9].Vertex[1].x = -1.0;
00284     polygon[9].Vertex[1].y = 1.0;
00285     polygon[9].Vertex[1].z = -6.0;
00286     polygon[9].Vertex[2].x = -1.0;
00287     polygon[9].Vertex[2].y = 1.0;
00288     polygon[9].Vertex[2].z = -4.0;
00289 //Bottom
00290     polygon[10].Vertex[0].x = -1.0;
00291     polygon[10].Vertex[0].y = -1.0;
00292     polygon[10].Vertex[0].z = -4.0;
00293     polygon[10].Vertex[1].x = -1.0;
00294     polygon[10].Vertex[1].y = -1.0;
00295     polygon[10].Vertex[1].z = -6.0;
00296     polygon[10].Vertex[2].x = 1.0;
00297     polygon[10].Vertex[2].y = -1.0;
00298     polygon[10].Vertex[2].z = -6.0;
00299 
00300     polygon[11].Vertex[0].x = -1.0;
00301     polygon[11].Vertex[0].y = -1.0;
00302     polygon[11].Vertex[0].z = -4.0;
00303     polygon[11].Vertex[1].x = 1.0;
00304     polygon[11].Vertex[1].y = -1.0;
00305     polygon[11].Vertex[1].z = -6.0;
00306     polygon[11].Vertex[2].x = 1.0;
00307     polygon[11].Vertex[2].y = -1.0;
00308     polygon[11].Vertex[2].z = -4.0;
00309 
00310     polygon[0].SetNormal();
00311     polygon[1].SetNormal();
00312     polygon[2].SetNormal();
00313     polygon[3].SetNormal();
00314     polygon[4].SetNormal();
00315     polygon[5].SetNormal();
00316     polygon[6].SetNormal();
00317     polygon[7].SetNormal();
00318     polygon[8].SetNormal();
00319     polygon[9].SetNormal();
00320     polygon[10].SetNormal();
00321     polygon[11].SetNormal();
00322 }
00323 
00324 void SetGLProperties()
00325 {
00326     glCullFace(GL_BACK);
00327     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00328     glClearDepth(1.0);
00329     glDepthFunc(GL_LESS);
00330     glEnable(GL_DEPTH_TEST);
00331     glShadeModel(GL_SMOOTH);
00332     glEnable(GL_NORMALIZE);
00333     glEnable(GL_CULL_FACE);
00334 }
00335 
00336 void SetGLProjection(int Width, int Height)
00337 {
00338     if (Height==0)
00339         Height=1;
00340     glViewport(0, 0, Width, Height);
00341     glMatrixMode(GL_PROJECTION);
00342     glLoadIdentity();
00343     gluPerspective(45.0,(float)Width/(float)Height,0.5,200.0);
00344 }
00345 
00346 void SetGLView(int Width, int Height)
00347 {
00348     SetGLProjection(Width, Height);
00349     glMatrixMode(GL_MODELVIEW);
00350     glLoadIdentity();
00351 }
00352 
00353 void SetGLMaterial()
00354 {
00355     float mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
00356     float mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00357     float mat_specular[] = { 0.9, 0.9, 0.9, 1.0 };
00358     float mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
00359     float mat_shininess[] = { 80.0 };
00360 
00361     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00362     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00363     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00364     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00365     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
00366 }
00367 
00368 int SetGLTexture(TEXTURE* texture)
00369 {
00370     glEnable(GL_TEXTURE_2D);
00371     sprintf(texture[0].TexName, "%s", "tile.tga");
00372     if (!texture[0].LoadTGA())
00373     {
00374         MessageBox(NULL,"Failed to load tile.tga","Error",MB_OK|MB_ICONERROR);
00375         return FALSE;
00376     }
00377     sprintf(texture[1].TexName, "%s", HaloTypeName);
00378     if (!texture[1].LoadTGA())
00379     {
00380         MessageBox(NULL,"Failed to load halo image","Error",MB_OK|MB_ICONERROR);
00381         return FALSE;
00382     }
00383     sprintf(texture[2].TexName, "%s", FireTypeName);
00384     if (!texture[2].LoadTGA())
00385     {
00386         MessageBox(NULL,"Failed to load fire image","Error",MB_OK|MB_ICONERROR);
00387         return FALSE;
00388     }
00389     return TRUE;
00390 }
00391 
00392 void DrawMyText()
00393 {
00394     glBlendFunc(GL_ONE,  GL_ONE_MINUS_SRC_ALPHA) ;
00395     glDisable(GL_LIGHTING);
00396     glColor3f(0.0, 1.0, 0.0);
00397     glEnable(GL_BLEND);
00398     glDisable(GL_DEPTH_TEST);
00399     glPushMatrix();
00400     glLoadIdentity();
00401     glFontBegin(&myFont);
00402     char text[256];
00403     char text2[256];
00404 
00405     glFontTextOut("Tutorial #10", -37, 27, -70);
00406 
00407     sprintf(text, "FPS = %d", fps);
00408     glFontTextOut(text, -52, 34, -100);
00409 
00410     sprintf(text, "%s", "Current spline = ");
00411     strcat(text, itoa(currentSpline + 1, text2, 10));
00412     glFontTextOut(text, -52, 20, -100);
00413     sprintf(text, "%s", "Target spline = ");
00414     strcat(text, itoa(lookAtPath + 1, text2, 10));
00415     glFontTextOut(text, -52, 18, -100);
00416     sprintf(text, "%s", "Camera mode = ");
00417     switch (cameraMode)
00418     {
00419     case 0:
00420     {
00421          strcat(text, "Free");
00422         }
00423     break;
00424 
00425     case 1:
00426     {
00427          strcat(text, "Look");
00428         }
00429     break;
00430     
00431     case 2:
00432     {
00433          strcat(text, "Follow");
00434         }
00435     break;
00436     } 
00437     glFontTextOut(text, -52, 16, -100);
00438     sprintf(text, "Press M to change camera mode");
00439     glFontTextOut(text, -52, 14, -100);
00440 
00441     glFontEnd();
00442     glPopMatrix();
00443     glEnable(GL_DEPTH_TEST);
00444     glDisable(GL_BLEND);
00445     glEnable(GL_LIGHTING);
00446 }
00447 
00448 void DrawGrid()
00449 {
00450     glDisable(GL_TEXTURE_2D);
00451     glDisable(GL_LIGHTING);
00452     glPushMatrix();
00453     glTranslatef(0,-2.0,0);
00454     float Line = -10;
00455     int Grid;
00456     glBegin(GL_LINES);
00457     for(Grid = 0; Grid <= 20; Grid += 1)
00458     {
00459         glColor3f(0.0f,1.0f,0.0f);
00460         glVertex3f(Line + Grid, 0, -10);
00461         glVertex3f(Line + Grid, 0, 10);
00462         glVertex3f(-10, 0, Line + Grid);
00463         glVertex3f(10, 0, Line + Grid);
00464     }
00465     glEnd();
00466     glPopMatrix();
00467     glEnable(GL_TEXTURE_2D);
00468     glEnable(GL_LIGHTING);
00469 }
00470 
00471 void DrawCube(POLYGON* polygon, TEXTURE* texture)
00472 {
00473         float mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
00474         float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00475         glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00476         glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00477 
00478         glBindTexture(GL_TEXTURE_2D, texture[0].TexID);
00479 
00480         glPushMatrix();
00481     glBegin(GL_TRIANGLES);
00482         // Front Face
00483             glNormal3fv(&polygon[0].Vertex[0].nx);
00484         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[0].x);
00485         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[1].x);
00486         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[0].Vertex[2].x);
00487 
00488         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[1].Vertex[0].x);
00489         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[1].x);
00490         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[2].x);
00491         // Back Face
00492             glNormal3fv(&polygon[2].Vertex[0].nx);
00493         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[0].x);
00494         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[1].x);
00495         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[2].Vertex[2].x);
00496 
00497         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[3].Vertex[0].x);
00498         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[1].x);
00499         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[2].x);
00500         // Top Face
00501             glNormal3fv(&polygon[4].Vertex[0].nx);
00502         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[0].x);
00503         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[1].x);
00504         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[4].Vertex[2].x);
00505 
00506         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[5].Vertex[0].x);
00507         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[1].x);
00508         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[2].x);
00509         // Bottom Face
00510             glNormal3fv(&polygon[6].Vertex[0].nx);
00511         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[0].x);
00512         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[1].x);
00513         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[6].Vertex[2].x);
00514 
00515         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[7].Vertex[0].x);
00516         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[1].x);
00517         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[2].x);
00518         // Right face
00519             glNormal3fv(&polygon[8].Vertex[0].nx);
00520         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[0].x);
00521         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[1].x);
00522         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[8].Vertex[2].x);
00523 
00524         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[9].Vertex[0].x);
00525         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[1].x);
00526         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[2].x);
00527         // Left Face
00528             glNormal3fv(&polygon[10].Vertex[0].nx);
00529         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[0].x);
00530         glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[1].x);
00531         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[10].Vertex[2].x);
00532 
00533         glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[11].Vertex[0].x);
00534         glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[1].x);
00535         glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[2].x);
00536     glEnd();
00537         glPopMatrix();
00538 }
00539 
00540 void DrawLightSphere(LIGHT* light)
00541 {
00542     float mat_ambient[] = { 1.0, 0.2, 0.1, 1.0 };
00543     float mat_diffuse[] = { 1.0, 0.2, 0.1, 1.0 };
00544     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00545     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00546     glDisable(GL_TEXTURE_2D);
00547     glPushMatrix();
00548     glTranslatef(light[currentLight].Position.x, light[currentLight].Position.y, light[currentLight].Position.z);
00549     GLUquadricObj * sphere = gluNewQuadric();
00550     gluQuadricOrientation(sphere, GLU_OUTSIDE);
00551     gluSphere(sphere,0.3,20,20);
00552     glPopMatrix();
00553     glEnable(GL_TEXTURE_2D);
00554 }
00555 
00556 void DrawGreenSphere()
00557 {
00558     float mat_ambient[] = { 0.2, 1.0, 0.1, 1.0 };
00559     float mat_diffuse[] = { 0.2, 1.0, 0.1, 1.0 };
00560     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00561     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00562     glDisable(GL_TEXTURE_2D);
00563     glPushMatrix();
00564     glTranslatef(-2.0, -2.0, -8.0);
00565     GLUquadricObj * sphere = gluNewQuadric();
00566     gluQuadricOrientation(sphere, GLU_OUTSIDE);
00567     gluSphere(sphere,0.3,20,20);
00568     glPopMatrix();
00569     glEnable(GL_TEXTURE_2D);
00570 }
00571 
00572 void DrawSphere()
00573 {
00574     float mat_ambient[] = { 0.8, 0.5, 0.1, 1.0 };
00575     float mat_diffuse[] = { 0.8, 0.5, 0.1, 1.0 };
00576     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00577     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00578 
00579     glDisable(GL_TEXTURE_2D);
00580     glPushMatrix();
00581     glTranslatef(-3.0f,-1.0f,-8.0f);
00582     GLUquadricObj * sphere = gluNewQuadric();
00583     gluQuadricOrientation(sphere, GLU_OUTSIDE);
00584     gluSphere(sphere,1.0,50,50);
00585     glPopMatrix();
00586     glEnable(GL_TEXTURE_2D);
00587 }
00588 
00589 void DrawCone()
00590 {
00591     float mat_ambient[] = { 0.1, 0.5, 1.0, 1.0 };
00592     float mat_diffuse[] = { 0.1, 0.5, 1.0, 1.0 };
00593     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00594     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00595 
00596     glDisable(GL_TEXTURE_2D);
00597     glDisable(GL_CULL_FACE);
00598     glPushMatrix();
00599     glTranslatef(3.0f,-2.0f,-8.0f);
00600     glRotatef(-90,1,0,0);
00601     GLUquadricObj * cylinder = gluNewQuadric();
00602     gluQuadricOrientation(cylinder, GLU_OUTSIDE);
00603     gluCylinder(cylinder,1.0,0.0,2.0,20,20);
00604     glPopMatrix();
00605     glEnable(GL_CULL_FACE);
00606     glEnable(GL_TEXTURE_2D);
00607 }
00608 
00609 float GetTimePassed(float& lasttime, int average, float* lastmultiplier)
00610 {
00611     float timeoffset;
00612     float currenttime;
00613 /*
00614 If the program has just started, set last tickcount to the current tickcount.
00615 This prevents the program from thinking that several hours have passed since the last frame.
00616 */
00617     if (lasttime == 0)
00618         lasttime = (float)GetTickCount();
00619         // Get the current time
00620     currenttime = (float)GetTickCount();
00621         // Calculate the offset
00622     timeoffset = currenttime - lasttime;
00623     // If timeoffset less than 1/120 of a second (in milliseconds) then set to minimum offset
00624     if (timeoffset < 8.333333)
00625     {
00626         timeoffset = 8.333333;
00627         currenttime = lasttime + timeoffset;
00628     }
00629     // Put the current time in the lasttime variable
00630         lasttime = currenttime;
00631     // return the time offset in seconds per frame
00632         multiplier = timeoffset / 1000;
00633     for (int loop = 0; loop < average - 1; loop++)
00634     {
00635         lastmultiplier[loop] = lastmultiplier[loop + 1];
00636     }
00637     lastmultiplier[average - 1] = multiplier;
00638     for (int loop = 0; loop < average - 1; loop++)
00639         multiplier += lastmultiplier[loop];
00640     multiplier /= (float)average; 
00641         if (multiplier)
00642         fps = (int)(1.0 / multiplier);
00643     return multiplier;
00644 }
00645 
00646 bool CheckClipPlanes(CAMERA Camera, VECTOR Vect)
00647 {
00648   float ProjectionMatrix[16];
00649   float ModelViewMatrix[16];
00650   float A, B, C, Distance;
00651   int Counter = 0;
00652   glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMatrix);
00653   glGetFloatv(GL_MODELVIEW_MATRIX, ModelViewMatrix);
00654 
00655   MultMatrix(ProjectionMatrix, ModelViewMatrix);
00656 
00657   //right clipping plane
00658   A = ProjectionMatrix[0] - ProjectionMatrix[3];
00659   B = ProjectionMatrix[4] - ProjectionMatrix[7];
00660   C = ProjectionMatrix[8] - ProjectionMatrix[11];
00661 
00662   Distance = -1 * (A * (-Camera.Position.x + Vect.x) + B * 
00663 
00664 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + 
00665 
00666 Vect.z));
00667   if (Distance > 0)
00668     Counter++;
00669 
00670   //left clipping plane
00671   A = ProjectionMatrix[0] + ProjectionMatrix[3];
00672   B = ProjectionMatrix[4] + ProjectionMatrix[7];
00673   C = ProjectionMatrix[8] + ProjectionMatrix[11];
00674 
00675   Distance = A * (-Camera.Position.x + Vect.x) + B * 
00676 
00677 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00678   if (Distance > 0)
00679     Counter++;
00680 
00681   //top clipping plane
00682   A = ProjectionMatrix[1] - ProjectionMatrix[3];
00683   B = ProjectionMatrix[5] - ProjectionMatrix[7];
00684   C = ProjectionMatrix[9] - ProjectionMatrix[11];
00685 
00686   Distance = -1 * (A * (-Camera.Position.x + Vect.x) + B * 
00687 
00688 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + 
00689 
00690 Vect.z));
00691   if (Distance > 0)
00692     Counter++;
00693 
00694   //bottom clipping plane
00695   A = ProjectionMatrix[1] + ProjectionMatrix[3];
00696   B = ProjectionMatrix[5] + ProjectionMatrix[7];
00697   C = ProjectionMatrix[9] + ProjectionMatrix[11];
00698 
00699   Distance = A * (-Camera.Position.x + Vect.x) + B * 
00700 
00701 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00702   if (Distance > 0)
00703     Counter++;
00704 
00705   // near clipping plane (might not be necessary when the near 
00706   // frustrum plane is close, but just in case)
00707   A = ProjectionMatrix[2] - ProjectionMatrix[3];
00708   B = ProjectionMatrix[6] - ProjectionMatrix[7];
00709   C = ProjectionMatrix[10] - ProjectionMatrix[11];
00710 
00711   Distance = A * (-Camera.Position.x + Vect.x) + B * 
00712 
00713 (-Camera.Position.y + Vect.y) + C * (-Camera.Position.z + Vect.z);
00714   if (Distance > 0)
00715     Counter++;
00716 
00717   // far clipping plane (the equation didn't work for the far plane, 
00718   // so I'll just use a distance test)
00719   VECTOR Vect2;
00720   Vect2.x = Vect.x - Camera.Position.x;
00721   Vect2.y = Vect.y - Camera.Position.y;
00722   Vect2.z = Vect.z - Camera.Position.z;
00723   if (MagnitudeVector(Vect2) < 200)
00724     Counter++;
00725 
00726   if (Counter == 6)
00727     return 1;
00728   else
00729     return 0;
00730 }
00731 
00732 //  A halo type billboard that always faces the camera
00733 void DrawHalo(TEXTURE* texture, LIGHT* light, CAMERA* camera)
00734 {
00735     float halfwidth = 2;
00736     float halfheight = 2;
00737 
00738     MATRIX mat;
00739     glGetFloatv(GL_MODELVIEW_MATRIX, mat.Element);
00740 
00741     VECTOR right;
00742     right.x = mat.Element[0];
00743     right.y = mat.Element[4];
00744     right.z = mat.Element[8];
00745     right.Normalize();
00746     right.x *= halfwidth;
00747     right.y *= halfwidth;
00748     right.z *= halfwidth;
00749 
00750     VECTOR up;
00751     up.x = mat.Element[1];
00752     up.y = mat.Element[5];
00753     up.z = mat.Element[9];
00754     up.Normalize();
00755     up.x *= halfheight;
00756     up.y *= halfheight;
00757     up.z *= halfheight;
00758 
00759 /*
00760    If you are using a quake style camera that doesn't roll
00761    then you can use the following commented code to make a conventional
00762    billboard remain vertical. If you are using a camera with 6DOF however,
00763    then this will not work.
00764 */
00765 
00766 /*
00767     up.x = 0.0;
00768     up.z = 0.0;
00769     right.y = 0.0;
00770 //*/
00771 
00772     float mat_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
00773     float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00774     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00775     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00776     glEnable(GL_BLEND);
00777     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00778     glEnable(GL_ALPHA_TEST);
00779     glAlphaFunc(GL_GREATER, 0);
00780     glDisable(GL_LIGHTING);
00781     glColor4f(1.0, 1.0, 1.0, 1.0);
00782     glBindTexture(GL_TEXTURE_2D, texture[1].TexID);
00783     glPopMatrix();
00784         glBegin(GL_QUADS);
00785             glTexCoord2f(0.0f, 0.0f); glVertex3f(light[currentLight].Position.x + (-right.x - up.x), light[currentLight].Position.y + (-right.y - up.y), light[currentLight].Position.z + (-right.z - up.z));
00786             glTexCoord2f(1.0f, 0.0f); glVertex3f(light[currentLight].Position.x + (right.x - up.x), light[currentLight].Position.y + (right.y - up.y), light[currentLight].Position.z + (right.z - up.z));
00787             glTexCoord2f(1.0f, 1.0f); glVertex3f(light[currentLight].Position.x + (right.x + up.x), light[currentLight].Position.y + (right.y + up.y), light[currentLight].Position.z + (right.z + up.z));
00788             glTexCoord2f(0.0f, 1.0f); glVertex3f(light[currentLight].Position.x + (up.x - right.x), light[currentLight].Position.y + (up.y - right.y), light[currentLight].Position.z + (up.z - right.z));
00789         glEnd();
00790     glPopMatrix();
00791     glEnable(GL_LIGHTING);
00792     glDisable(GL_ALPHA);
00793     glDisable(GL_BLEND);
00794 }
00795 
00796 //  A fire type billboard that rotates to face the camera but remains vertical
00797 void DrawFire(TEXTURE* texture, LIGHT* light, CAMERA* camera)
00798 {
00799     float halfwidth = 1.0;
00800     float halfheight = 2.0;
00801     VECTOR FirePosition(3.0, -2.0 + halfheight, -8.0);
00802 //  Get the vector from the billboard position to the camera
00803     VECTOR A;
00804     A.x = (camera[currentCamera].Position.x - FirePosition.x);
00805     A.y = (camera[currentCamera].Position.y - FirePosition.y);
00806     A.z = (camera[currentCamera].Position.z - FirePosition.z);
00807     A.Normalize();
00808 // Set a vector to the standard up vector
00809     VECTOR B;
00810     B.x = (0);
00811     B.y = (1);
00812     B.z = (0);
00813     B.Normalize();
00814 // Take the cross product of these vectors to find a vector perpendicular to the camera
00815     VECTOR C = CrossVector(A, B);
00816     C.Normalize();
00817 
00818 // Negate the perpendicular vector to make the billboard face the front
00819     VECTOR right;
00820     right.x = -C.x * halfwidth;
00821     right.y = -C.y * halfwidth;
00822     right.z = -C.z * halfwidth;
00823 
00824     VECTOR up;
00825     up.x = 0;
00826     up.y = 1 * halfheight;
00827     up.z = 0;
00828 
00829 // Continue as normal
00830     float mat_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
00831     float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00832     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00833     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00834     glEnable(GL_BLEND);
00835     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00836     glEnable(GL_ALPHA_TEST);
00837     glAlphaFunc(GL_GREATER, 0);
00838     glDisable(GL_LIGHTING);
00839     glColor4f(1.0, 1.0, 1.0, 1.0);
00840     glBindTexture(GL_TEXTURE_2D, texture[2].TexID);
00841     glPopMatrix();
00842         glBegin(GL_QUADS);
00843             glTexCoord2f(0.0f, 0.0f); glVertex3f(FirePosition.x + (-right.x - up.x), FirePosition.y + (-right.y - up.y), FirePosition.z + (-right.z - up.z));
00844             glTexCoord2f(1.0f, 0.0f); glVertex3f(FirePosition.x + (right.x - up.x), FirePosition.y + (right.y - up.y), FirePosition.z + (right.z - up.z));
00845             glTexCoord2f(1.0f, 1.0f); glVertex3f(FirePosition.x + (right.x + up.x), FirePosition.y + (right.y + up.y), FirePosition.z + (right.z + up.z));
00846             glTexCoord2f(0.0f, 1.0f); glVertex3f(FirePosition.x + (up.x - right.x), FirePosition.y + (up.y - right.y), FirePosition.z + (up.z - right.z));
00847         glEnd();
00848     glPopMatrix();
00849     glEnable(GL_LIGHTING);
00850     glDisable(GL_ALPHA);
00851     glDisable(GL_BLEND);
00852 }
00853 
00854 void DrawBillboards(TEXTURE* texture, LIGHT* light, CAMERA* camera)
00855 {
00856 // Draw depth sorted billboards
00857     VECTOR FirePosition(3.0, -2.0, -8.0);
00858 
00859     VECTOR VectorToCone;
00860     VectorToCone.x = FirePosition.x - camera[currentCamera].Position.x;
00861     VectorToCone.y = FirePosition.y - camera[currentCamera].Position.y;
00862     VectorToCone.z = FirePosition.z - camera[currentCamera].Position.z;
00863     float FireDistance = VectorToCone.GetMagnitude();
00864 
00865     VECTOR VectorToLight;
00866     VectorToLight.x = light[currentLight].Position.x - camera[currentCamera].Position.x;
00867     VectorToLight.y = light[currentLight].Position.y - camera[currentCamera].Position.y;
00868     VectorToLight.z = light[currentLight].Position.z - camera[currentCamera].Position.z;
00869     float LightDistance = VectorToLight.GetMagnitude();
00870 
00871     if(LightDistance < FireDistance)
00872     {
00873         DrawFire(texture, light, camera);
00874         DrawHalo(texture, light, camera);
00875     }
00876     else
00877     {
00878         DrawHalo(texture, light, camera);
00879         DrawFire(texture, light, camera);
00880     }
00881 }

Generated on Fri Dec 23 05:22:05 2005 for Splines by doxygen1.2.15