Al's Programming Resource Homepage  Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

main.cpp

Go to the documentation of this file.
00001 // ~~~ OpenGL & Win32 ~~~
00002 // Tutorial No.7 - Dialogs, Text and FPS
00003 // Alan Baylis 2001
00004 
00005 #include <windows.h>
00006 #include "shared.h"
00007 #include "general.h"
00008 #include "vector.h"
00009 #include "vertex.h"
00010 #include "quat.h"
00011 #include "matrix.h"
00012 #include "texture.h"
00013 #include "locmath.h"
00014 #include "polygon.h"
00015 #include "object.h"
00016 #include "camera.h"
00017 #include "light.h"
00018 #include "collision.h"
00019 #include "winfuncs.h"
00020 #include "glfont.c"            // font routines
00021 #include "mmgr.h"
00022 #include "resource.rh"
00023 
00024 
00025 // Windows
00026 static HGLRC hRC;
00027 static HDC hDC;
00028 HWND hWnd;
00029 RECT screen;
00030 PAINTSTRUCT ps;
00031 
00032 // Math
00033 float pi = 3.141592;
00034 float radian = pi / 180;
00035 float epsilon = 0.05;
00036 
00037 // Input
00038 bool key[256];
00039 bool released_key[256];
00040 
00041 // Cameras
00042 int currentCamera = 0;
00043 int numCameras = 4;
00044 float step = 5.0;
00045 
00046 // Timing
00047 float lasttime;
00048 float multiplier;
00049 const int average = 20;
00050 float lastmultiplier[average];
00051 int fps;
00052 
00053 // Lights
00054 int GL_LIGHT[8] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};    // Represent GL lights in a conveniant array
00055 int currentLight = 0;
00056 int numLights = 0;
00057 
00058 // Cube polygons
00059 POLYGON * polygon = new POLYGON[12];
00060 
00061 // Texture
00062 TEXTURE * texture = new TEXTURE[1];
00063 
00064 // Camera
00065 CAMERA * camera = new CAMERA[numCameras + 1];
00066 CAMERA LastCam;
00067 
00068 // Lighting
00069 LIGHT * light = new LIGHT[numLights + 1];
00070 float lightColor[3] = {1.0, 1.0, 1.0};
00071 
00072 // Dialogs
00073 int DialogInUse = 0;
00074 int testint;               // variables used to test dialog input
00075 float testfloat;
00076 char teststring[64];
00077 
00078 // Font
00079 GLFONT myFont;              // the font to use
00080 
00081 void InitGL(int Width, int Height)
00082 {
00083     SetGLProperties();
00084     SetGLMaterial();
00085     SetGLView(Width, Height);
00086     SetGLLighting(light);
00087     SetGLWorld(polygon);
00088     SetGLTexture(texture);
00089     SetGLCamera(camera);
00090     glFontCreate(&myFont, "roman.glf", 2);    // create the font from the glf file
00091 }
00092 
00093 void ReSizeGLScene(int Width, int Height)
00094 {
00095     SetGLView(Width, Height);
00096 }
00097 
00098 void DrawGLScene(void)
00099 {
00100       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00101 
00102     LastCam = camera[currentCamera];
00103     camera[currentCamera].Update();
00104 
00105     // initialize collision detection structure
00106     CollisionPacket cp;
00107     cp.eRadius.x = 1.0;
00108     cp.eRadius.y = 1.0;
00109     cp.eRadius.z = 1.0;
00110     cp.sourcePoint.x = LastCam.Position.x;
00111     cp.sourcePoint.y = LastCam.Position.y;
00112     cp.sourcePoint.z = LastCam.Position.z;
00113     cp.velocity.x = camera[currentCamera].Position.x - LastCam.Position.x;
00114     cp.velocity.y = camera[currentCamera].Position.y - LastCam.Position.y;
00115     cp.velocity.z = camera[currentCamera].Position.z - LastCam.Position.z;
00116 
00117     CheckForCollision(polygon, &camera[currentCamera].Position, &cp);
00118 
00119     camera[currentCamera].Apply();
00120 
00121     int loop;
00122     for(loop = 0; loop <= numLights; loop++)
00123         light[loop].Apply();
00124 /*
00125     // if the light sphere position is in view draw a small green sphere
00126     if(CheckClipPlanes(camera[currentCamera], light[currentLight].Position))
00127     {
00128         glPushMatrix();
00129         glLoadIdentity();
00130         DrawGreenSphere();
00131         glPopMatrix();
00132     }
00133 */    
00134     DrawLightSphere(light);
00135     DrawGrid();
00136     DrawCube(polygon, texture);
00137     DrawSphere();
00138     DrawCone();
00139 
00140 // Draw text
00141         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA) ;
00142     glDisable(GL_LIGHTING);
00143     glColor3f(0.0, 1.0, 0.0);
00144     glEnable(GL_BLEND);
00145     glDisable(GL_DEPTH_TEST);
00146     glPushMatrix();
00147     glLoadIdentity();
00148     glFontBegin(&myFont);
00149     char text[256];
00150 
00151     glFontTextOut("Tutorial #7", -37, 27, -70);
00152 
00153     sprintf(text, "FPS = %d", fps);
00154     glFontTextOut(text, -52, 34, -100);
00155 
00156     sprintf(text, "Integer = %d", testint);
00157     glFontTextOut(text, -52, -30, -100);
00158 
00159     sprintf(text, "Float = %f", testfloat);
00160     glFontTextOut(text, -52, -32, -100);
00161 
00162     sprintf(text, "%s", "String = ");
00163     strcat(text, teststring);
00164     glFontTextOut(text, -52, -34, -100);
00165 
00166     glFontEnd();
00167     glPopMatrix();
00168     glEnable(GL_DEPTH_TEST);
00169         glDisable(GL_BLEND);
00170     glEnable(GL_LIGHTING);
00171 }
00172 
00173 LRESULT CALLBACK WndProc(    HWND    hWnd,
00174                             UINT    message,
00175                             WPARAM    wParam,
00176                             LPARAM    lParam)
00177 {
00178     GLuint    PixelFormat;
00179     static    PIXELFORMATDESCRIPTOR pfd=
00180     {
00181            sizeof(PIXELFORMATDESCRIPTOR),
00182         1,
00183         PFD_DRAW_TO_WINDOW |
00184         PFD_SUPPORT_OPENGL |
00185         PFD_DOUBLEBUFFER,
00186         PFD_TYPE_RGBA,
00187         16,
00188         0, 0, 0, 0, 0, 0,
00189         0,
00190         0,
00191         0,
00192         0, 0, 0, 0,
00193         16,
00194         0,
00195         0,
00196         PFD_MAIN_PLANE,
00197         0,
00198         0, 0, 0
00199     };
00200 
00201 
00202     switch (message)
00203     {
00204         case WM_CREATE:
00205             hDC = GetDC(hWnd);
00206 
00207             PixelFormat = ChoosePixelFormat(hDC, &pfd);
00208 
00209             if (!PixelFormat)
00210             {
00211                    MessageBox(NULL,"Can't find a suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
00212                 PostQuitMessage(0);
00213                 break;
00214             }
00215 
00216             if(!SetPixelFormat(hDC,PixelFormat,&pfd))
00217             {
00218                 MessageBox(NULL,"Can't set the PixelFormat.","Error",MB_OK|MB_ICONERROR);
00219                 PostQuitMessage(0);
00220                 break;
00221             }
00222 
00223             hRC = wglCreateContext(hDC);
00224             if(!hRC)
00225             {
00226                 MessageBox(NULL,"Can't create a GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00227                 PostQuitMessage(0);
00228                 break;
00229             }
00230 
00231             if(!wglMakeCurrent(hDC, hRC))
00232             {
00233                 MessageBox(NULL,"Can't activate the GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00234                 PostQuitMessage(0);
00235                 break;
00236             }
00237             InitGL(screen.right, screen.bottom);
00238         break;
00239 
00240         case WM_SYSCOMMAND:
00241         {
00242             switch (wParam)
00243             {
00244                 case SC_SCREENSAVE:
00245                 case SC_MONITORPOWER:
00246                     return 0;
00247             }
00248             break;
00249         }
00250 
00251 // required when a dialog window is moved or resized
00252         if (DialogInUse)
00253         {
00254             case WM_PAINT:
00255                 BeginPaint(hWnd,&ps);
00256                 DrawGLScene();
00257                 glFlush();
00258                 SwapBuffers(hDC);
00259                 EndPaint(hWnd,&ps);
00260             break;
00261         }
00262 
00263         case WM_DESTROY:
00264         case WM_CLOSE:
00265             glFontDestroy(&myFont); // free fonts allocated memory         
00266         
00267             delete[] texture;
00268             delete[] polygon;
00269             delete[] camera;
00270             delete[] light;
00271             ChangeDisplaySettings(NULL, 0);
00272 
00273             wglMakeCurrent(hDC,NULL);
00274             wglDeleteContext(hRC);
00275             ReleaseDC(hWnd,hDC);
00276 
00277             PostQuitMessage(0);
00278         break;
00279 
00280         case WM_KEYDOWN:
00281             key[wParam] = TRUE;
00282         break;
00283 
00284         case WM_KEYUP:
00285             key[wParam] = FALSE;
00286         break;
00287 
00288         case WM_SIZE:
00289             SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00290             ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
00291         break;
00292 
00293         case WM_MOUSEMOVE:
00294             camera[currentCamera].Delta_x = float(HIWORD(lParam) - screen.bottom * 0.5) * 10;
00295             camera[currentCamera].Delta_y = float(LOWORD(lParam) - screen.right * 0.5) * 10;
00296         break;
00297 
00298         case WM_LBUTTONDOWN:
00299             camera[currentCamera].Delta_z = -120.0;
00300         break;
00301 
00302         case WM_RBUTTONDOWN:
00303             camera[currentCamera].Delta_z = 120.0;
00304         break;
00305 
00306         case WM_LBUTTONUP:
00307             if (wParam != MK_RBUTTON)
00308                 camera[currentCamera].Delta_z = 0.0;
00309         break;
00310 
00311         case WM_RBUTTONUP:
00312             if (wParam != MK_LBUTTON)
00313                 camera[currentCamera].Delta_z = 0.0;
00314         break;
00315 
00316         default:
00317             return (DefWindowProc(hWnd, message, wParam, lParam));
00318     }
00319 return (0);
00320 }
00321 
00322 int WINAPI WinMain(    HINSTANCE    hInstance,
00323                     HINSTANCE,
00324                     LPSTR,
00325                     int)
00326 {
00327     MSG        msg;
00328     WNDCLASSEX    wc;
00329 
00330     GetWindowRect(GetDesktopWindow(), &screen);
00331     wc.cbSize = sizeof(WNDCLASSEX);
00332     wc.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_SAVEBITS;
00333     wc.lpfnWndProc        = (WNDPROC) WndProc;
00334     wc.cbClsExtra        = 0;
00335     wc.cbWndExtra        = 0;
00336     wc.hInstance        = hInstance;
00337     wc.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00338     wc.hIconSm          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00339     wc.hCursor            = LoadCursor(NULL, IDC_ARROW);
00340     wc.hbrBackground    = NULL;
00341     wc.lpszMenuName        = NULL;
00342     wc.lpszClassName    = "OpenGL WinClass";
00343 
00344 
00345     if(!RegisterClassEx(&wc))
00346     {
00347         MessageBox(NULL,"Failed To Register The Window Class.","Error",MB_OK|MB_ICONERROR);
00348         return FALSE;
00349     }
00350 
00351     hWnd = CreateWindowEx(
00352     WS_EX_LEFT,
00353     "OpenGL WinClass",
00354     "OpenGL & Win32 Tutorial No.5",
00355 
00356     WS_MAXIMIZE |
00357     WS_CLIPCHILDREN |
00358     WS_CLIPSIBLINGS |
00359     WS_POPUPWINDOW |
00360     WS_VISIBLE,
00361     0, 0,
00362     screen.right, screen.bottom,
00363     NULL,
00364     NULL,
00365     hInstance,
00366     NULL);
00367 
00368     if(!hWnd)
00369     {
00370         MessageBox(NULL,"Window Creation Error.","Error",MB_OK|MB_ICONERROR);
00371         return FALSE;
00372     }
00373 
00374     DEVMODE dmScreenSettings;
00375 
00376     memset(&dmScreenSettings, 0, sizeof(DEVMODE));
00377     dmScreenSettings.dmSize        = sizeof(DEVMODE);
00378     dmScreenSettings.dmPelsWidth    = screen.right;
00379     dmScreenSettings.dmPelsHeight    = screen.bottom;
00380     dmScreenSettings.dmFields    = DM_PELSWIDTH | DM_PELSHEIGHT;
00381     ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
00382 
00383     ShowWindow(hWnd, SW_HIDE);
00384     if(DialogBox(hInstance, "NAMEOFDLG", hWnd, (DLGPROC)NameOfProc) == IDOK) // calls the initialization dialog
00385     {
00386         ShowWindow(hWnd, SW_SHOW);
00387         UpdateWindow(hWnd);
00388         SetFocus(hWnd);
00389             wglMakeCurrent(hDC,hRC);
00390         SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00391         ShowCursor(0);
00392         }
00393     else
00394     {
00395         MessageBox(NULL,"Dialog Box Failed or You Cancelled It","Error",MB_OK|MB_ICONERROR);    
00396         delete[] texture;
00397         delete[] polygon;
00398         delete[] camera;
00399         delete[] light;
00400         PostQuitMessage(0);
00401     }
00402 
00403     while (1)
00404     {
00405         while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
00406         {
00407             if (GetMessage(&msg, NULL, 0, 0))
00408             {
00409                 TranslateMessage(&msg);
00410                 DispatchMessage(&msg);
00411             }
00412             else
00413             {
00414                 return TRUE;
00415             }
00416         }
00417 
00418         if(!DialogInUse)
00419         {
00420             SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00421             DrawGLScene();
00422             glFlush();
00423               SwapBuffers(hDC);
00424         }
00425 
00426         multiplier = GetTimePassed(lasttime, average, lastmultiplier);
00427         camera[currentCamera].Multiplier = multiplier;
00428         light[currentLight].Multiplier = multiplier;
00429 
00430         if (key['C'] && released_key['C'] == 0)
00431         {
00432             if (currentCamera < numCameras)
00433                 currentCamera++;
00434             else
00435                 currentCamera = 0;
00436             released_key['C'] = 1;
00437         }
00438 
00439         if (!key['C'])
00440             released_key['C'] = 0;
00441 
00442         if (key['L'] && released_key['L'] == 0)
00443         {
00444             if (currentLight < numLights)
00445                 currentLight++;
00446             else
00447                 currentLight = 0;
00448             released_key['L'] = 1;
00449         }
00450 
00451         if (!key['L'])
00452             released_key['L'] = 0;
00453 
00454         if (key['I'] && released_key['I'] == 0)
00455         {
00456             DialogInUse = 1;
00457             lightColor[0] = light[currentLight].Ambient[0];
00458             lightColor[1] = light[currentLight].Ambient[1];
00459             lightColor[2] = light[currentLight].Ambient[2];
00460             ShowCursor(1);
00461             GetLightColor();
00462             key['I'] = 0;
00463             ShowCursor(0);
00464             SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00465             light[currentLight].Ambient[0] = lightColor[0];
00466             light[currentLight].Ambient[1] = lightColor[1];
00467             light[currentLight].Ambient[2] = lightColor[2];
00468             light[currentLight].Update();
00469             DialogInUse = 0;
00470         }
00471 
00472         if (!key['I'])
00473             released_key['I'] = 0;
00474 
00475         if (key['O'] && released_key['O'] == 0)
00476         {
00477             DialogInUse = 2;
00478             lightColor[0] = light[currentLight].Diffuse[0];
00479             lightColor[1] = light[currentLight].Diffuse[1];
00480             lightColor[2] = light[currentLight].Diffuse[2];
00481             ShowCursor(1);
00482             GetLightColor();
00483             key['O'] = 0;
00484             ShowCursor(0);
00485             SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00486             light[currentLight].Diffuse[0] = lightColor[0];
00487             light[currentLight].Diffuse[1] = lightColor[1];
00488             light[currentLight].Diffuse[2] = lightColor[2];
00489             light[currentLight].Update();
00490             DialogInUse = 0;
00491         }
00492 
00493         if (!key['O'])
00494             released_key['O'] = 0;
00495 
00496         if (key['P'] && released_key['P'] == 0)
00497         {
00498             DialogInUse = 3;
00499             lightColor[0] = light[currentLight].Specular[0];
00500             lightColor[1] = light[currentLight].Specular[1];
00501             lightColor[2] = light[currentLight].Specular[2];
00502             ShowCursor(1);
00503             GetLightColor();
00504             key['P'] = 0;
00505             ShowCursor(0);
00506             SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00507             light[currentLight].Specular[0] = lightColor[0];
00508             light[currentLight].Specular[1] = lightColor[1];
00509             light[currentLight].Specular[2] = lightColor[2];
00510             light[currentLight].Update();
00511             DialogInUse = 0;
00512         }
00513 
00514         if (!key['P'])
00515             released_key['P'] = 0;
00516 
00517         if (key[49])
00518         {
00519             step = 1.0;
00520         }
00521 
00522         if (key[50])
00523         {
00524             step = 2.0;
00525         }
00526 
00527         if (key[51])
00528         {
00529             step = 3.0;
00530         }
00531 
00532         if (key[52])
00533         {
00534             step = 4.0;
00535         }
00536 
00537         if (key[53])
00538         {
00539             step = 5.0;
00540         }
00541 
00542         if (key[54])
00543         {
00544             step = 6.0;
00545         }
00546 
00547         if (key[55])
00548         {
00549             step = 7.0;
00550         }
00551 
00552         if (key[56])
00553         {
00554             step = 8.0;
00555         }
00556 
00557         if (key[57])
00558         {
00559             step = 9.0;
00560         }
00561 
00562         if (key[VK_NUMPAD6])
00563         {
00564             light[currentLight].Movement_x += step;
00565         }
00566 
00567         if (key[VK_NUMPAD4])
00568         {
00569             light[currentLight].Movement_x -= step;
00570         }
00571 
00572         if (key[VK_NUMPAD2])
00573         {
00574             light[currentLight].Movement_z += step;
00575         }
00576 
00577         if (key[VK_NUMPAD8])
00578         {
00579             light[currentLight].Movement_z -= step;
00580         }
00581 
00582         if (key[VK_NUMPAD7])
00583         {
00584             light[currentLight].Movement_y += step;
00585         }
00586 
00587         if (key[VK_NUMPAD9])
00588         {
00589             light[currentLight].Movement_y -= step;
00590         }
00591 
00592         if (key[VK_NUMPAD5] && released_key[VK_NUMPAD5] == 0)
00593         {
00594             if (light[currentLight].Positional == FALSE)
00595                 light[currentLight].Positional = TRUE;
00596             else
00597                 light[currentLight].Positional = FALSE;
00598             released_key[VK_NUMPAD5] = 1;
00599         }
00600 
00601         if (!key[VK_NUMPAD5])
00602             released_key[VK_NUMPAD5] = 0;
00603 
00604         if (key[VK_RIGHT])
00605         {
00606             camera[currentCamera].Movement_x += step;
00607         }
00608 
00609         if (key[VK_LEFT])
00610         {
00611             camera[currentCamera].Movement_x -= step;
00612         }
00613 
00614         if (key[VK_DOWN])
00615         {
00616             camera[currentCamera].Movement_z += step;
00617         }
00618 
00619         if (key[VK_UP])
00620         {
00621             camera[currentCamera].Movement_z -= step;
00622         }
00623 
00624         if (key[VK_PRIOR])
00625         {
00626             camera[currentCamera].Movement_y += step;
00627         }
00628 
00629         if (key[VK_NEXT])
00630         {
00631             camera[currentCamera].Movement_y -= step;
00632         }
00633 
00634         if (key[VK_SPACE])
00635         {
00636             camera[currentCamera].Reset();
00637         }
00638 
00639         if (key[VK_ESCAPE])
00640             SendMessage(hWnd,WM_CLOSE,0,0);
00641     }
00642 }
00643 
00644 
00645 
00646                                       
00647 
00648 

Generated on Fri Dec 23 05:17:02 2005 for Dialogs, Text & FPS by doxygen1.2.15