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