00001
00002
00003
00004
00005 #include <windows.h>
00006 #include "shared.h"
00007 #include "general.h"
00008 #include "vector.h"
00009 #include "vertex.h"
00010 #include "quat.h"
00011 #include "matrix.h"
00012 #include "texture.h"
00013 #include "locmath.h"
00014 #include "polygon.h"
00015 #include "object.h"
00016 #include "camera.h"
00017 #include "mmgr.h"
00018 #include "resource.rh"
00019
00020
00021 static HGLRC hRC;
00022 static HDC hDC;
00023 HWND hWnd;
00024 RECT screen;
00025 PAINTSTRUCT ps;
00026
00027
00028 bool key[256];
00029 bool released_key[256];
00030
00031
00032 float pi = 3.141592;
00033 float radian = pi / 180;
00034
00035
00036 int currentCamera = 0;
00037 int numCameras = 4;
00038 float step = 5.0;
00039
00040
00041 float lasttime;
00042 float multiplier;
00043 const int average = 20;
00044 float lastmultiplier[average];
00045 int fps;
00046
00047
00048 POLYGON * polygon = new POLYGON[12];
00049
00050
00051 TEXTURE * texture = new TEXTURE[1];
00052
00053
00054 CAMERA * camera = new CAMERA[numCameras + 1];
00055
00056 void InitGL(int Width, int Height)
00057 {
00058 SetGLProperties();
00059 SetGLMaterial();
00060 SetGLLighting();
00061 SetGLView(Width, Height);
00062 SetGLWorld(polygon);
00063 SetGLTexture(texture);
00064 SetGLCamera(camera);
00065 }
00066
00067 void ReSizeGLScene(int Width, int Height)
00068 {
00069 SetGLView(Width, Height);
00070 }
00071
00072 void DrawGLScene(void)
00073 {
00074 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00075
00076 camera[currentCamera].Update();
00077
00078 camera[currentCamera].Apply();
00079
00080 SetGLLighting();
00081
00082 DrawGrid();
00083 DrawCube(polygon, texture);
00084 DrawSphere();
00085 DrawCone();
00086 }
00087
00088 LRESULT CALLBACK WndProc( HWND hWnd,
00089 UINT message,
00090 WPARAM wParam,
00091 LPARAM lParam)
00092 {
00093 GLuint PixelFormat;
00094 static PIXELFORMATDESCRIPTOR pfd=
00095 {
00096 sizeof(PIXELFORMATDESCRIPTOR),
00097 1,
00098 PFD_DRAW_TO_WINDOW |
00099 PFD_SUPPORT_OPENGL |
00100 PFD_DOUBLEBUFFER,
00101 PFD_TYPE_RGBA,
00102 16,
00103 0, 0, 0, 0, 0, 0,
00104 0,
00105 0,
00106 0,
00107 0, 0, 0, 0,
00108 16,
00109 0,
00110 0,
00111 PFD_MAIN_PLANE,
00112 0,
00113 0, 0, 0
00114 };
00115
00116
00117 switch (message)
00118 {
00119 case WM_CREATE:
00120 hDC = GetDC(hWnd);
00121
00122 PixelFormat = ChoosePixelFormat(hDC, &pfd);
00123
00124 if (!PixelFormat)
00125 {
00126 MessageBox(NULL,"Can't find a suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
00127 PostQuitMessage(0);
00128 break;
00129 }
00130
00131 if(!SetPixelFormat(hDC,PixelFormat,&pfd))
00132 {
00133 MessageBox(NULL,"Can't set the PixelFormat.","Error",MB_OK|MB_ICONERROR);
00134 PostQuitMessage(0);
00135 break;
00136 }
00137
00138 hRC = wglCreateContext(hDC);
00139 if(!hRC)
00140 {
00141 MessageBox(NULL,"Can't create a GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00142 PostQuitMessage(0);
00143 break;
00144 }
00145
00146 if(!wglMakeCurrent(hDC, hRC))
00147 {
00148 MessageBox(NULL,"Can't activate the GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00149 PostQuitMessage(0);
00150 break;
00151 }
00152 InitGL(screen.right, screen.bottom);
00153 break;
00154
00155 case WM_SYSCOMMAND:
00156 {
00157 switch (wParam)
00158 {
00159 case SC_SCREENSAVE:
00160 case SC_MONITORPOWER:
00161 return 0;
00162 }
00163 break;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 case WM_DESTROY:
00177 case WM_CLOSE:
00178 delete[] texture;
00179 delete[] polygon;
00180 delete[] camera;
00181 ChangeDisplaySettings(NULL, 0);
00182
00183 wglMakeCurrent(hDC,NULL);
00184 wglDeleteContext(hRC);
00185 ReleaseDC(hWnd,hDC);
00186
00187 PostQuitMessage(0);
00188 break;
00189
00190 case WM_KEYDOWN:
00191 key[wParam] = TRUE;
00192 break;
00193
00194 case WM_KEYUP:
00195 key[wParam] = FALSE;
00196 break;
00197
00198 case WM_SIZE:
00199 SetCursorPos((int)(screen.right * 0.5), (int)(screen.bottom * 0.5));
00200 ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
00201 break;
00202
00203 case WM_MOUSEMOVE:
00204
00205 camera[currentCamera].Delta_x = float(HIWORD(lParam) - screen.bottom * 0.5) * 10;
00206 camera[currentCamera].Delta_y = float(LOWORD(lParam) - screen.right * 0.5) * 10;
00207 break;
00208
00209 case WM_LBUTTONDOWN:
00210 camera[currentCamera].Delta_z = -120.0;
00211 break;
00212
00213 case WM_RBUTTONDOWN:
00214 camera[currentCamera].Delta_z = 120.0;
00215 break;
00216
00217 case WM_LBUTTONUP:
00218 if (wParam != MK_RBUTTON)
00219 camera[currentCamera].Delta_z = 0.0;
00220 break;
00221
00222 case WM_RBUTTONUP:
00223 if (wParam != MK_LBUTTON)
00224 camera[currentCamera].Delta_z = 0.0;
00225 break;
00226
00227 default:
00228 return (DefWindowProc(hWnd, message, wParam, lParam));
00229 }
00230 return (0);
00231 }
00232
00233 int WINAPI WinMain( HINSTANCE hInstance,
00234 HINSTANCE,
00235 LPSTR,
00236 int)
00237 {
00238 MSG msg;
00239 WNDCLASSEX wc;
00240
00241 GetWindowRect(GetDesktopWindow(), &screen);
00242 wc.cbSize = sizeof(WNDCLASSEX);
00243 wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_SAVEBITS;
00244 wc.lpfnWndProc = (WNDPROC) WndProc;
00245 wc.cbClsExtra = 0;
00246 wc.cbWndExtra = 0;
00247 wc.hInstance = hInstance;
00248 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00249 wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00250 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
00251 wc.hbrBackground = NULL;
00252 wc.lpszMenuName = NULL;
00253 wc.lpszClassName = "OpenGL WinClass";
00254
00255
00256 if(!RegisterClassEx(&wc))
00257 {
00258 MessageBox(NULL,"Failed To Register The Window Class.","Error",MB_OK|MB_ICONERROR);
00259 return FALSE;
00260 }
00261
00262 hWnd = CreateWindowEx(
00263 WS_EX_LEFT,
00264 "OpenGL WinClass",
00265 "OpenGL & Win32 Tutorial No.3",
00266
00267 WS_MAXIMIZE |
00268 WS_CLIPCHILDREN |
00269 WS_CLIPSIBLINGS |
00270 WS_POPUPWINDOW |
00271 WS_VISIBLE,
00272 0, 0,
00273 screen.right, screen.bottom,
00274 NULL,
00275 NULL,
00276 hInstance,
00277 NULL);
00278
00279 if(!hWnd)
00280 {
00281 MessageBox(NULL,"Window Creation Error.","Error",MB_OK|MB_ICONERROR);
00282 return FALSE;
00283 }
00284
00285 DEVMODE dmScreenSettings;
00286
00287 memset(&dmScreenSettings, 0, sizeof(DEVMODE));
00288 dmScreenSettings.dmSize = sizeof(DEVMODE);
00289 dmScreenSettings.dmPelsWidth = screen.right;
00290 dmScreenSettings.dmPelsHeight = screen.bottom;
00291 dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
00292 ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
00293
00294 ShowWindow(hWnd, SW_SHOW);
00295 UpdateWindow(hWnd);
00296 SetFocus(hWnd);
00297 wglMakeCurrent(hDC,hRC);
00298 ShowCursor(0);
00299
00300 while (1)
00301 {
00302 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
00303 {
00304 if (GetMessage(&msg, NULL, 0, 0))
00305 {
00306 TranslateMessage(&msg);
00307 DispatchMessage(&msg);
00308 }
00309 else
00310 {
00311 return TRUE;
00312 }
00313 }
00314
00315 SetCursorPos((int)(screen.right * 0.5),(int)(screen.bottom * 0.5));
00316 DrawGLScene();
00317 glFlush();
00318 SwapBuffers(hDC);
00319
00320 camera[currentCamera].Multiplier = GetTimePassed(lasttime, average, lastmultiplier);
00321
00322 if (key['C'] && released_key['C'] == 0)
00323 {
00324 if (currentCamera < numCameras)
00325 currentCamera++;
00326 else
00327 currentCamera = 0;
00328 released_key['C'] = 1;
00329 }
00330
00331 if (!key['C'])
00332 released_key['C'] = 0;
00333
00334 if (key[49])
00335 {
00336 step = 1.0;
00337 }
00338
00339 if (key[50])
00340 {
00341 step = 2.0;
00342 }
00343
00344 if (key[51])
00345 {
00346 step = 3.0;
00347 }
00348
00349 if (key[52])
00350 {
00351 step = 4.0;
00352 }
00353
00354 if (key[53])
00355 {
00356 step = 5.0;
00357 }
00358
00359 if (key[54])
00360 {
00361 step = 6.0;
00362 }
00363
00364 if (key[55])
00365 {
00366 step = 7.0;
00367 }
00368
00369 if (key[56])
00370 {
00371 step = 8.0;
00372 }
00373
00374 if (key[57])
00375 {
00376 step = 9.0;
00377 }
00378
00379 if (key[VK_RIGHT])
00380 {
00381 camera[currentCamera].Movement_x += step;
00382 }
00383
00384 if (key[VK_LEFT])
00385 {
00386 camera[currentCamera].Movement_x -= step;
00387 }
00388
00389 if (key[VK_DOWN])
00390 {
00391 camera[currentCamera].Movement_z += step;
00392 }
00393
00394 if (key[VK_UP])
00395 {
00396 camera[currentCamera].Movement_z -= step;
00397 }
00398
00399 if (key[VK_PRIOR])
00400 {
00401 camera[currentCamera].Movement_y += step;
00402 }
00403
00404 if (key[VK_NEXT])
00405 {
00406 camera[currentCamera].Movement_y -= step;
00407 }
00408
00409 if (key[VK_SPACE])
00410 {
00411 camera[currentCamera].Reset();
00412 }
00413
00414 if (key[VK_ESCAPE])
00415 SendMessage(hWnd,WM_CLOSE,0,0);
00416 }
00417 }
00418
00419
00420
00421
00422
00423