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

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