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