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