00001
00002
00003
00004
00005 #include <windows.h>
00006 #include "shared.h"
00007 #include "general.h"
00008 #include "listnode.h"
00009 #include "locmath.h"
00010 #include "vector.h"
00011 #include "vertex.h"
00012 #include "quat.h"
00013 #include "matrix.h"
00014 #include "texture.h"
00015 #include "polygon.h"
00016 #include "plane.h"
00017 #include "object.h"
00018 #include "camera.h"
00019 #include "light.h"
00020 #include "collision.h"
00021 #include "glfont.h"
00022 #include "bspline.h"
00023 #include "bsp.h"
00024 #include "portal.h"
00025 #include "tga.h"
00026 #include "lightmap.h"
00027 #include "winfuncs.h"
00028 #include "tll.h"
00029 #include "console.h"
00030 #include "log.h"
00031 #include "mmgr.h"
00032 #include "resource.rh"
00033
00034
00035 ConsoleWindow Console;
00036
00037
00038 static HGLRC hRC;
00039 static HDC hDC;
00040 HWND hWnd;
00041 HINSTANCE g_hInst;
00042 RECT screen;
00043 PAINTSTRUCT ps;
00044 char AppDirectory[MAX_PATH];
00045 float ApplicationStartTime;
00046
00047
00048 float pi = 3.141592;
00049 float radian = pi / 180;
00050 float epsilon = 0.05;
00051
00052
00053 bool key[256];
00054 bool released_key[256];
00055
00056
00057 int currentCamera = 0;
00058 int numCameras = 4;
00059 float step = 30.0;
00060
00061
00062 float lasttime;
00063 float multiplier;
00064 const int average = 20;
00065 float lastmultiplier[average];
00066 int fps;
00067
00068
00069 int GL_LIGHT[8] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};
00070 int currentLight = 0;
00071 int numLights = 0;
00072
00073
00074 PLANE frustum[6];
00075
00076
00077 int numVertices = 37;
00078 VERTEX* vertex = new VERTEX[numVertices];
00079
00080
00081 int numPolygons = 58;
00082 POLYGON* polygon = new POLYGON[numPolygons];
00083 POLYGON* bsppolygon = new POLYGON[numPolygons];
00084
00085
00086 TEXTURE* texture = new TEXTURE[4];
00087
00088
00089 CAMERA* camera = new CAMERA[numCameras + 1];
00090 CAMERA LastCam;
00091
00092
00093 LIGHT* light = new LIGHT[numLights + 1];
00094 float lightColor[3] = {1.0, 1.0, 1.0};
00095
00096
00097 HWND hWndStartDlg;
00098 int DialogInUse = 0;
00099 char szText[BUFFER_SIZE];
00100
00101
00102 GLFONT myFont;
00103
00104
00105 float Min_X, Min_Y, Min_Z, Max_X, Max_Y, Max_Z;
00106
00107
00108 int showportals = 1;
00109 int numlistleaves = 0;
00110 int numlistpartitions = 0;
00111 int numcurrentportals = 0;
00112
00113
00114 int numleaves = 0;
00115 int currentleaf = 0;
00116 int numpartitions = 0;
00117 bool BuiltBSP = false;
00118 BSP_node* root = new BSP_node;
00119
00120
00121 LinkedList<PORTAL> PortalList;
00122 int numportals = 0;
00123 PORTAL* portal;
00124
00125
00126 int numStaticLights = 9;
00127 StaticLight* staticlight = new StaticLight[numStaticLights];
00128
00129
00130 int numLightmaps = 0;
00131 Lightmap* lightmap = new Lightmap[numPolygons];
00132
00133
00134 int visible = 0;
00135 int numSplines = 0;
00136 int cameraMode = 2;
00137 int currentSpline = 0;
00138 int lookAtPath = 0;
00139 char SplineFileName[MAX_PATH];
00140 SPLINE* spline;
00141 LinkedList<SPLINE> SplineList;
00142
00143
00144 LinkedList<ListNode> LeafList;
00145 LinkedList<ListNode> PartitionList;
00146 ListNode* listnode;
00147
00148 int InitGL(int Width, int Height)
00149 {
00150 CLog::addLine("--- Program Start ---");
00151
00152 int ReturnValue;
00153 ApplicationStartTime = (float)GetTickCount();
00154
00155 SetGLProperties();
00156 SetGLMaterial();
00157 SetGLLighting(light);
00158 SetGLCamera(camera);
00159 cameraMode = 2;
00160 SetSplines(SplineList);
00161
00162 if (!SetGLTexture(texture))
00163 ReturnValue = 0;
00164 else
00165 ReturnValue = 1;
00166
00167
00168 glFontCreate(&myFont, "roman.glf", 600);
00169
00170 SetGLWorld(polygon, texture, vertex);
00171
00172
00173 Min_X = polygon[0].Vertex[0].x;
00174 Min_Y = polygon[0].Vertex[0].y;
00175 Min_Z = polygon[0].Vertex[0].z;
00176 Max_X = polygon[0].Vertex[0].x;
00177 Max_Y = polygon[0].Vertex[0].y;
00178 Max_Z = polygon[0].Vertex[0].z;
00179 for (int loop = 0; loop < numPolygons; loop++)
00180 {
00181 for (int i = 0; i < 3; i++)
00182 {
00183 if (polygon[loop].Vertex[i].x < Min_X )
00184 Min_X = polygon[loop].Vertex[i].x;
00185 if (polygon[loop].Vertex[i].y < Min_Y )
00186 Min_Y = polygon[loop].Vertex[i].y;
00187 if (polygon[loop].Vertex[i].z < Min_Z )
00188 Min_Z = polygon[loop].Vertex[i].z;
00189 if (polygon[loop].Vertex[i].x > Max_X )
00190 Max_X = polygon[loop].Vertex[i].x;
00191 if (polygon[loop].Vertex[i].y > Max_Y )
00192 Max_Y = polygon[loop].Vertex[i].y;
00193 if (polygon[loop].Vertex[i].z > Max_Z )
00194 Max_Z = polygon[loop].Vertex[i].z;
00195 }
00196 }
00197
00198
00199 for (int i = 0; i < numPolygons; i++)
00200 bsppolygon[i] = polygon[i];
00201
00202 root->nodeid = 0;
00203 root->leaf = 0;
00204 root->visible = 0;
00205 root->numpolys = numPolygons;
00206 root->nodepolylist = bsppolygon;
00207 root->nodelightmaplist = lightmap;
00208
00209 BuildBSP(root);
00210 BuiltBSP = true;
00211
00212 MakeNodeLists(root);
00213 MakePortalList();
00214
00215 AddPortalsToLeaves(root);
00216 FindTruePortals(root);
00217
00218
00219 SetStaticLights(staticlight);
00220
00221
00222 CreateBSPLightmaps(root);
00223 LoadBSPLightmaps(root);
00224
00225 return ReturnValue;
00226 }
00227
00228 void ReSizeGLScene(int Width, int Height)
00229 {
00230 SetGLView(Width, Height);
00231 }
00232
00233 void DrawGLScene(void)
00234 {
00235 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00236
00237 LastCam = camera[currentCamera];
00238 camera[currentCamera].Update();
00239
00240 CollisionPacket cp;
00241 cp.eRadius.x = 1.0;
00242 cp.eRadius.y = 1.0;
00243 cp.eRadius.z = 1.0;
00244 cp.sourcePoint.x = LastCam.Position.x;
00245 cp.sourcePoint.y = LastCam.Position.y;
00246 cp.sourcePoint.z = LastCam.Position.z;
00247 cp.velocity.x = camera[currentCamera].Position.x - LastCam.Position.x;
00248 cp.velocity.y = camera[currentCamera].Position.y - LastCam.Position.y;
00249 cp.velocity.z = camera[currentCamera].Position.z - LastCam.Position.z;
00250
00251 CheckForCollision(polygon, &camera[currentCamera].Position, &cp);
00252
00253 camera[currentCamera].Apply();
00254
00255 for(int loop = 0; loop <= numLights; loop++)
00256 light[loop].Apply();
00257
00258 currentleaf = FindCurrentLeaf(camera[currentCamera].Position, root);
00259
00260 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
00261 DrawWorld(root);
00262 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 DrawSplines(SplineList);
00315 DrawMyText();
00316 }
00317
00318 LRESULT CALLBACK WndProc( HWND hWnd,
00319 UINT message,
00320 WPARAM wParam,
00321 LPARAM lParam)
00322 {
00323 GLuint PixelFormat;
00324 static PIXELFORMATDESCRIPTOR pfd=
00325 {
00326 sizeof(PIXELFORMATDESCRIPTOR),
00327 1,
00328 PFD_DRAW_TO_WINDOW |
00329 PFD_SUPPORT_OPENGL |
00330 PFD_DOUBLEBUFFER,
00331 PFD_TYPE_RGBA,
00332 16,
00333 0, 0, 0, 0, 0, 0,
00334 0,
00335 0,
00336 0,
00337 0, 0, 0, 0,
00338 16,
00339 0,
00340 0,
00341 PFD_MAIN_PLANE,
00342 0,
00343 0, 0, 0
00344 };
00345
00346
00347 switch (message)
00348 {
00349 case WM_CREATE:
00350 hDC = GetDC(hWnd);
00351
00352 PixelFormat = ChoosePixelFormat(hDC, &pfd);
00353
00354 if (!PixelFormat)
00355 {
00356 MessageBox(NULL,"Can't find a suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
00357 PostQuitMessage(0);
00358 break;
00359 }
00360
00361 if(!SetPixelFormat(hDC,PixelFormat,&pfd))
00362 {
00363 MessageBox(NULL,"Can't set the PixelFormat.","Error",MB_OK|MB_ICONERROR);
00364 PostQuitMessage(0);
00365 break;
00366 }
00367
00368 hRC = wglCreateContext(hDC);
00369 if(!hRC)
00370 {
00371 MessageBox(NULL,"Can't create a GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00372 PostQuitMessage(0);
00373 break;
00374 }
00375
00376 if(!wglMakeCurrent(hDC, hRC))
00377 {
00378 MessageBox(NULL,"Can't activate the GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00379 PostQuitMessage(0);
00380 break;
00381 }
00382 break;
00383
00384 case WM_SYSCOMMAND:
00385 {
00386 switch (wParam)
00387 {
00388 case SC_SCREENSAVE:
00389 case SC_MONITORPOWER:
00390 return 0;
00391 }
00392 break;
00393 }
00394
00395 if (DialogInUse)
00396 {
00397 case WM_PAINT:
00398 BeginPaint(hWnd,&ps);
00399 DrawGLScene();
00400 glFlush();
00401 SwapBuffers(hDC);
00402 EndPaint(hWnd,&ps);
00403 break;
00404 }
00405
00406 case WM_CLOSE:
00407 delete[] lightmap;
00408 delete[] staticlight;
00409 delete[] texture;
00410 delete[] vertex;
00411 delete[] camera;
00412 delete[] light;
00413 delete[] polygon;
00414
00415 for (int i = numSplines - 1; i >= 0; i--)
00416 DeleteSpline(i, SplineList);
00417
00418 if (BuiltBSP)
00419 DeleteBSP(root);
00420 delete root;
00421
00422 for (int i = numportals; i > 0; i--)
00423 {
00424 portal = PortalList.Get(i);
00425 delete[] portal->Vertex;
00426 }
00427
00428 glFontDestroy(&myFont);
00429
00430 CLog::addLine("--- End of Program ---");
00431 Console.Close();
00432
00433 ChangeDisplaySettings(NULL, 0);
00434 wglMakeCurrent(hDC,NULL);
00435 wglDeleteContext(hRC);
00436 ReleaseDC(hWnd,hDC);
00437 DestroyWindow(hWnd);
00438 break;
00439
00440 case WM_DESTROY:
00441 PostQuitMessage(0);
00442 break;
00443
00444 case WM_KEYDOWN:
00445 key[wParam] = TRUE;
00446 break;
00447
00448 case WM_KEYUP:
00449 key[wParam] = FALSE;
00450 break;
00451
00452 case WM_SIZE:
00453 SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00454 ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
00455 break;
00456
00457 case WM_MOUSEMOVE:
00458 if (cameraMode == 0)
00459 {
00460 camera[currentCamera].Delta_x = float(HIWORD(lParam) - screen.bottom * 0.5) * 10;
00461 camera[currentCamera].Delta_y = float(LOWORD(lParam) - screen.right * 0.5) * 10;
00462 }
00463 break;
00464
00465 case WM_LBUTTONDOWN:
00466 camera[currentCamera].Delta_z = -120.0;
00467 break;
00468
00469 case WM_RBUTTONDOWN:
00470 camera[currentCamera].Delta_z = 120.0;
00471 break;
00472
00473 case WM_LBUTTONUP:
00474 if (wParam != MK_RBUTTON)
00475 camera[currentCamera].Delta_z = 0.0;
00476 break;
00477
00478 case WM_RBUTTONUP:
00479 if (wParam != MK_LBUTTON)
00480 camera[currentCamera].Delta_z = 0.0;
00481 break;
00482
00483 default:
00484 return (DefWindowProc(hWnd, message, wParam, lParam));
00485 }
00486 return (0);
00487 }
00488
00489 int WINAPI WinMain( HINSTANCE hInstance,
00490 HINSTANCE,
00491 LPSTR,
00492 int)
00493 {
00494 MSG msg;
00495 g_hInst = hInstance;
00496 GetWindowRect(GetDesktopWindow(), &screen);
00497
00498 WNDCLASSEX wc;
00499 wc.cbSize = sizeof(WNDCLASSEX);
00500 wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_SAVEBITS;
00501 wc.lpfnWndProc = (WNDPROC) WndProc;
00502 wc.cbClsExtra = 0;
00503 wc.cbWndExtra = 0;
00504 wc.hInstance = hInstance;
00505 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00506 wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00507 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
00508 wc.hbrBackground = NULL;
00509 wc.lpszMenuName = NULL;
00510 wc.lpszClassName = "OpenGL WinClass";
00511
00512
00513 if(!RegisterClassEx(&wc))
00514 {
00515 MessageBox(NULL,"Failed To Register The Window Class.","Error",MB_OK|MB_ICONERROR);
00516 return FALSE;
00517 }
00518
00519 hWnd = CreateWindowEx(
00520 WS_EX_LEFT,
00521 "OpenGL WinClass",
00522 "OpenGL & Win32 Tutorial No.13",
00523 WS_MAXIMIZE |
00524 WS_CLIPCHILDREN |
00525 WS_CLIPSIBLINGS |
00526 WS_POPUPWINDOW |
00527 WS_VISIBLE,
00528 0, 0,
00529 screen.right, screen.bottom,
00530 NULL,
00531 NULL,
00532 hInstance,
00533 NULL);
00534
00535 if(!hWnd)
00536 {
00537 MessageBox(NULL,"Window Creation Error.","Error",MB_OK|MB_ICONERROR);
00538 return FALSE;
00539 }
00540
00541 DEVMODE dmScreenSettings;
00542 memset(&dmScreenSettings, 0, sizeof(DEVMODE));
00543 dmScreenSettings.dmSize = sizeof(DEVMODE);
00544 dmScreenSettings.dmPelsWidth = screen.right;
00545 dmScreenSettings.dmPelsHeight = screen.bottom;
00546 dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
00547 ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
00548
00549 ShowWindow(hWnd, SW_HIDE);
00550
00551 GetCurrentDirectory(MAX_PATH, AppDirectory);
00552
00553 Console.Open();
00554
00555 if(DialogBox(hInstance, "STARTDLG", hWnd, (DLGPROC)StartProc) == IDOK)
00556 {
00557 if (!InitGL(screen.right, screen.bottom))
00558 {
00559 Console.Close();
00560 SendMessage(hWnd,WM_CLOSE,0,0);
00561 }
00562 else
00563 {
00564 ShowWindow(hWnd, SW_SHOW);
00565 UpdateWindow(hWnd);
00566 SetFocus(hWnd);
00567 wglMakeCurrent(hDC,hRC);
00568 SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00569 ShowCursor(0);
00570 }
00571 }
00572 else
00573 {
00574 delete[] lightmap;
00575 delete[] staticlight;
00576 delete[] texture;
00577 delete[] vertex;
00578 delete[] camera;
00579 delete[] light;
00580 delete[] polygon;
00581 delete[] bsppolygon;
00582 delete root;
00583 PostQuitMessage(0);
00584 }
00585
00586 while (1)
00587 {
00588 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
00589 {
00590 if (GetMessage(&msg, NULL, 0, 0))
00591 {
00592 TranslateMessage(&msg);
00593 DispatchMessage(&msg);
00594 }
00595 else
00596 {
00597 return TRUE;
00598 }
00599 }
00600
00601 if(!DialogInUse)
00602 {
00603 SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00604 DrawGLScene();
00605 glFlush();
00606 SwapBuffers(hDC);
00607 }
00608
00609 multiplier = GetTimePassed(lasttime, average, lastmultiplier);
00610 camera[currentCamera].Multiplier = multiplier;
00611 light[currentLight].Multiplier = multiplier;
00612
00613 if (key['C'] && released_key['C'] == 0)
00614 {
00615 if (currentCamera < numCameras)
00616 currentCamera++;
00617 else
00618 currentCamera = 0;
00619 released_key['C'] = 1;
00620 }
00621
00622 if (!key['C'])
00623 released_key['C'] = 0;
00624
00625 if (key['L'] && released_key['L'] == 0)
00626 {
00627 if (currentLight < numLights)
00628 currentLight++;
00629 else
00630 currentLight = 0;
00631 released_key['L'] = 1;
00632 }
00633
00634 if (!key['L'])
00635 released_key['L'] = 0;
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 if (key['P'] && released_key['P'] == 0)
00701 {
00702 if (showportals)
00703 showportals = 0;
00704 else
00705 showportals = 1;
00706 released_key['P'] = 1;
00707 }
00708
00709 if (!key['P'])
00710 released_key['P'] = 0;
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748 if (key['M'] && released_key['M'] == 0)
00749 {
00750
00751
00752
00753
00754
00755
00756 if (cameraMode == 2)
00757 cameraMode = 0;
00758 else
00759 cameraMode = 2;
00760 released_key['M'] = 1;
00761 }
00762
00763 if (!key['M'])
00764 released_key['M'] = 0;
00765
00766 if (key[49])
00767 {
00768 step = 10.0;
00769 }
00770
00771 if (key[50])
00772 {
00773 step = 20.0;
00774 }
00775
00776 if (key[51])
00777 {
00778 step = 30.0;
00779 }
00780
00781 if (key[52])
00782 {
00783 step = 40.0;
00784 }
00785
00786 if (key[53])
00787 {
00788 step = 50.0;
00789 }
00790
00791 if (key[54])
00792 {
00793 step = 60.0;
00794 }
00795
00796 if (key[55])
00797 {
00798 step = 70.0;
00799 }
00800
00801 if (key[56])
00802 {
00803 step = 80.0;
00804 }
00805
00806 if (key[57])
00807 {
00808 step = 90.0;
00809 }
00810
00811 if (key[VK_NUMPAD6])
00812 {
00813 light[currentLight].Movement_x += step;
00814 }
00815
00816 if (key[VK_NUMPAD4])
00817 {
00818 light[currentLight].Movement_x -= step;
00819 }
00820
00821 if (key[VK_NUMPAD2])
00822 {
00823 light[currentLight].Movement_z += step;
00824 }
00825
00826 if (key[VK_NUMPAD8])
00827 {
00828 light[currentLight].Movement_z -= step;
00829 }
00830
00831 if (key[VK_NUMPAD7])
00832 {
00833 light[currentLight].Movement_y += step;
00834 }
00835
00836 if (key[VK_NUMPAD9])
00837 {
00838 light[currentLight].Movement_y -= step;
00839 }
00840
00841 if (key[VK_NUMPAD5] && released_key[VK_NUMPAD5] == 0)
00842 {
00843 if (light[currentLight].Positional == FALSE)
00844 light[currentLight].Positional = TRUE;
00845 else
00846 light[currentLight].Positional = FALSE;
00847 released_key[VK_NUMPAD5] = 1;
00848 }
00849
00850 if (!key[VK_NUMPAD5])
00851 released_key[VK_NUMPAD5] = 0;
00852
00853 if (key[VK_RIGHT])
00854 {
00855 camera[currentCamera].Movement_x += step;
00856 }
00857
00858 if (key[VK_LEFT])
00859 {
00860 camera[currentCamera].Movement_x -= step;
00861 }
00862
00863 if (key[VK_DOWN])
00864 {
00865 camera[currentCamera].Movement_z += step;
00866 }
00867
00868 if (key[VK_UP])
00869 {
00870 camera[currentCamera].Movement_z -= step;
00871 }
00872
00873 if (key[VK_PRIOR])
00874 {
00875 camera[currentCamera].Movement_y += step;
00876 }
00877
00878 if (key[VK_NEXT])
00879 {
00880 camera[currentCamera].Movement_y -= step;
00881 }
00882
00883 if (key[VK_SPACE])
00884 {
00885 camera[currentCamera].Reset();
00886 }
00887
00888 if (key[VK_ESCAPE] || key['Q'])
00889 SendMessage(hWnd,WM_CLOSE,0,0);
00890 }
00891 }
00892
00893
00894
00895
00896
00897