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 "mmgr.h"
00016 #include "resource.rh"
00017
00018
00019 static HGLRC hRC;
00020 static HDC hDC;
00021 HWND hWnd;
00022 RECT screen;
00023 PAINTSTRUCT ps;
00024
00025
00026 bool key[256];
00027 bool released_key[256];
00028
00029
00030 float pi = 3.141592;
00031 float radian = pi / 180;
00032
00033
00034 TEXTURE * texture = new TEXTURE[1];
00035
00036
00037 POLYGON * polygon = new POLYGON[12];
00038
00039 void InitGL(int Width, int Height)
00040 {
00041 SetGLProperties();
00042 SetGLMaterial();
00043 SetGLLighting();
00044 SetGLView(Width, Height);
00045 SetGLScene(polygon);
00046 SetGLTexture(texture);
00047 }
00048
00049 void ReSizeGLScene(int Width, int Height)
00050 {
00051 SetGLView(Width, Height);
00052 }
00053
00054 void DrawGLScene(void)
00055 {
00056 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00057 DrawGrid();
00058 DrawCube(polygon, texture);
00059 DrawSphere();
00060 DrawCone();
00061 }
00062
00063 LRESULT CALLBACK WndProc( HWND hWnd,
00064 UINT message,
00065 WPARAM wParam,
00066 LPARAM lParam )
00067 {
00068 GLuint PixelFormat;
00069 static PIXELFORMATDESCRIPTOR pfd=
00070 {
00071 sizeof(PIXELFORMATDESCRIPTOR),
00072 1,
00073 PFD_DRAW_TO_WINDOW |
00074 PFD_SUPPORT_OPENGL |
00075 PFD_DOUBLEBUFFER,
00076 PFD_TYPE_RGBA,
00077 16,
00078 0, 0, 0, 0, 0, 0,
00079 0,
00080 0,
00081 0,
00082 0, 0, 0, 0,
00083 16,
00084 0,
00085 0,
00086 PFD_MAIN_PLANE,
00087 0,
00088 0, 0, 0
00089 };
00090
00091
00092 switch (message)
00093 {
00094 case WM_CREATE:
00095 hDC = GetDC(hWnd);
00096
00097 PixelFormat = ChoosePixelFormat(hDC, &pfd);
00098
00099 if (!PixelFormat)
00100 {
00101 MessageBox(0,"Can't Find A Suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
00102 PostQuitMessage(0);
00103 break;
00104 }
00105
00106 if(!SetPixelFormat(hDC,PixelFormat,&pfd))
00107 {
00108 MessageBox(0,"Can't Set The PixelFormat.","Error",MB_OK|MB_ICONERROR);
00109 PostQuitMessage(0);
00110 break;
00111 }
00112
00113 hRC = wglCreateContext(hDC);
00114 if(!hRC)
00115 {
00116 MessageBox(0,"Can't Create A GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00117 PostQuitMessage(0);
00118 break;
00119 }
00120
00121 if(!wglMakeCurrent(hDC, hRC))
00122 {
00123 MessageBox(0,"Can't activate GLRC.","Error",MB_OK|MB_ICONERROR);
00124 PostQuitMessage(0);
00125 break;
00126 }
00127 InitGL(screen.right, screen.bottom);
00128 break;
00129
00130 case WM_SYSCOMMAND:
00131 {
00132 switch (wParam)
00133 {
00134 case SC_SCREENSAVE:
00135 case SC_MONITORPOWER:
00136 return 0;
00137 }
00138 break;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 case WM_DESTROY:
00152 case WM_CLOSE:
00153 delete[] texture;
00154 delete[] polygon;
00155
00156 ChangeDisplaySettings(NULL, 0);
00157 wglMakeCurrent(hDC,NULL);
00158 wglDeleteContext(hRC);
00159 ReleaseDC(hWnd,hDC);
00160 PostQuitMessage(0);
00161 break;
00162
00163 case WM_KEYDOWN:
00164 key[wParam] = TRUE;
00165 break;
00166
00167 case WM_KEYUP:
00168 key[wParam] = FALSE;
00169 break;
00170
00171 case WM_SIZE:
00172 ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
00173 break;
00174
00175 default:
00176 return (DefWindowProc(hWnd, message, wParam, lParam));
00177 }
00178 return (0);
00179 }
00180
00181 int WINAPI WinMain( HINSTANCE hInstance,
00182 HINSTANCE hPrevInstance,
00183 LPSTR lpCmdLine,
00184 int nCmdShow)
00185 {
00186 MSG msg;
00187 WNDCLASSEX wc;
00188
00189 GetWindowRect(GetDesktopWindow(), &screen);
00190 wc.cbSize = sizeof(WNDCLASSEX);
00191 wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_SAVEBITS;
00192 wc.lpfnWndProc = (WNDPROC) WndProc;
00193 wc.cbClsExtra = 0;
00194 wc.cbWndExtra = 0;
00195 wc.hInstance = hInstance;
00196 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00197 wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00198 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
00199 wc.hbrBackground = NULL;
00200 wc.lpszMenuName = NULL;
00201 wc.lpszClassName = "OpenGL WinClass";
00202
00203
00204 if(!RegisterClassEx(&wc))
00205 {
00206 MessageBox(0,"Failed To Register The Window Class.","Error",MB_OK|MB_ICONERROR);
00207 return FALSE;
00208 }
00209
00210 hWnd = CreateWindowEx(
00211 WS_EX_LEFT,
00212 "OpenGL WinClass",
00213 "OpenGL & Win32 Tutorial No.1",
00214
00215 WS_MAXIMIZE |
00216 WS_CLIPCHILDREN |
00217 WS_CLIPSIBLINGS |
00218 WS_POPUPWINDOW |
00219 WS_VISIBLE,
00220 0, 0,
00221 screen.right, screen.bottom,
00222 NULL,
00223 NULL,
00224 hInstance,
00225 NULL);
00226
00227 if(!hWnd)
00228 {
00229 MessageBox(0,"Window Creation Error.","Error",MB_OK|MB_ICONERROR);
00230 return FALSE;
00231 }
00232
00233 DEVMODE dmScreenSettings;
00234
00235 memset(&dmScreenSettings, 0, sizeof(DEVMODE));
00236 dmScreenSettings.dmSize = sizeof(DEVMODE);
00237 dmScreenSettings.dmPelsWidth = screen.right;
00238 dmScreenSettings.dmPelsHeight = screen.bottom;
00239 dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
00240 ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
00241
00242 ShowWindow(hWnd, SW_SHOW);
00243 UpdateWindow(hWnd);
00244 SetFocus(hWnd);
00245 wglMakeCurrent(hDC,hRC);
00246
00247 while (1)
00248 {
00249
00250 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
00251 {
00252 if (GetMessage(&msg, NULL, 0, 0))
00253 {
00254 TranslateMessage(&msg);
00255 DispatchMessage(&msg);
00256 }
00257 else
00258 {
00259 return TRUE;
00260 }
00261 }
00262
00263 DrawGLScene();
00264 glFlush();
00265 SwapBuffers(hDC);
00266
00267 if (key[VK_ESCAPE])
00268 SendMessage(hWnd,WM_CLOSE,0,0);
00269 }
00270 }