Guest User

Untitled

a guest
Jul 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.20 KB | None | 0 0
  1.  
  2. /*
  3. * This Code Was Created By Jeff Molofee 2000
  4. * A HUGE Thanks To Fredric Echols For Cleaning Up
  5. * And Optimizing The Base Code, Making It More Flexible!
  6. * If You've Found This Code Useful, Please Let Me Know.
  7. * Visit My Site At nehe.gamedev.net
  8. */
  9.  
  10. #include <windows.h> // Header File For Windows
  11. #include <gl\gl.h> // Header File For The OpenGL32 Library
  12. #include <gl\glu.h> // Header File For The GLu32 Library
  13.  
  14. HDC hDC=NULL; // Private GDI Device Context
  15. HGLRC hRC=NULL; // Permanent Rendering Context
  16. HWND hWnd=NULL; // Holds Our Window Handle
  17. HINSTANCE hInstance; // Holds The Instance Of The Application
  18.  
  19. bool keys[256]; // Array Used For The Keyboard Routine
  20. bool active=TRUE; // Window Active Flag Set To TRUE By Default
  21. bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
  22.  
  23. float rot = 0.5f;
  24. float xrot = 0.0f;
  25. float yrot = 1.0f;
  26. float zrot = 0.0f;
  27.  
  28. GLfloat rtri; // Angle For The Triangle ( NEW )
  29. GLfloat rquad; // Angle For The Quad ( NEW )
  30.  
  31. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
  32.  
  33. GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
  34. {
  35. if (height==0) // Prevent A Divide By Zero By
  36. {
  37. height=1; // Making Height Equal One
  38. }
  39.  
  40. glViewport(0,0,width,height); // Reset The Current Viewport
  41.  
  42. glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  43. glLoadIdentity(); // Reset The Projection Matrix
  44.  
  45. // Calculate The Aspect Ratio Of The Window
  46. gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  47.  
  48. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  49. glLoadIdentity(); // Reset The Modelview Matrix
  50. }
  51.  
  52. int InitGL(GLvoid) // All Setup For OpenGL Goes Here
  53. {
  54. glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  55. glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
  56. glClearDepth(1.0f); // Depth Buffer Setup
  57. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  58. glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  59. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
  60. return TRUE; // Initialization Went OK
  61. }
  62.  
  63. int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
  64. {
  65. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
  66. glLoadIdentity(); // Reset The Current Modelview Matrix
  67. glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
  68. glRotatef(rtri,xrot,yrot,zrot);
  69. glBegin(GL_TRIANGLES); // Start Drawing A Triangle
  70. glColor3f(1.0f,0.0f,0.0f); // Red
  71. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
  72. glColor3f(0.0f,1.0f,0.0f); // Green
  73. glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
  74. glColor3f(0.0f,0.0f,1.0f); // Blue
  75. glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
  76. glColor3f(1.0f,0.0f,0.0f); // Red
  77. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
  78. glColor3f(0.0f,0.0f,1.0f); // Blue
  79. glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
  80. glColor3f(0.0f,1.0f,0.0f); // Green
  81. glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
  82. glColor3f(1.0f,0.0f,0.0f); // Red
  83. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
  84. glColor3f(0.0f,1.0f,0.0f); // Green
  85. glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
  86. glColor3f(0.0f,0.0f,1.0f); // Blue
  87. glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
  88. glColor3f(1.0f,0.0f,0.0f); // Red
  89. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
  90. glColor3f(0.0f,0.0f,1.0f); // Blue
  91. glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
  92. glColor3f(0.0f,1.0f,0.0f); // Green
  93. glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
  94. glEnd(); // Done Drawing The Pyramid
  95.  
  96. glLoadIdentity(); // Reset The Current Modelview Matrix
  97. glTranslatef(1.5f,0.0f,-7.0f); // Move Right 1.5 Units And Into The Screen 7.0
  98. glRotatef(rtri,yrot,xrot,zrot);
  99. glBegin(GL_QUADS); // Draw A Quad
  100. glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
  101. glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
  102. glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
  103. glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
  104. glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
  105. glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
  106. glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
  107. glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
  108. glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
  109. glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
  110. glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
  111. glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
  112. glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
  113. glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
  114. glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
  115. glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
  116. glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
  117. glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
  118. glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
  119. glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
  120. glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
  121. glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
  122. glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
  123. glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
  124. glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
  125. glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
  126. glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
  127. glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
  128. glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
  129. glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
  130. glEnd(); // Done Drawing The Quad
  131.  
  132. rtri+=rot; // Increase The Rotation Variable For The Triangle ( NEW )
  133. rquad-=rot; // Decrease The Rotation Variable For The Quad ( NEW )
  134. return TRUE; // Keep Going
  135. }
  136.  
  137. GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
  138. {
  139. if (fullscreen) // Are We In Fullscreen Mode?
  140. {
  141. ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
  142. ShowCursor(TRUE); // Show Mouse Pointer
  143. }
  144.  
  145. if (hRC) // Do We Have A Rendering Context?
  146. {
  147. if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
  148. {
  149. MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  150. }
  151.  
  152. if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
  153. {
  154. MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  155. }
  156. hRC=NULL; // Set RC To NULL
  157. }
  158.  
  159. if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
  160. {
  161. MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  162. hDC=NULL; // Set DC To NULL
  163. }
  164.  
  165. if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
  166. {
  167. MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  168. hWnd=NULL; // Set hWnd To NULL
  169. }
  170.  
  171. if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
  172. {
  173. MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  174. hInstance=NULL; // Set hInstance To NULL
  175. }
  176. }
  177.  
  178. /* This Code Creates Our OpenGL Window. Parameters Are: *
  179. * title - Title To Appear At The Top Of The Window *
  180. * width - Width Of The GL Window Or Fullscreen Mode *
  181. * height - Height Of The GL Window Or Fullscreen Mode *
  182. * bits - Number Of Bits To Use For Color (8/16/24/32) *
  183. * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
  184.  
  185. BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
  186. {
  187. GLuint PixelFormat; // Holds The Results After Searching For A Match
  188. WNDCLASS wc; // Windows Class Structure
  189. DWORD dwExStyle; // Window Extended Style
  190. DWORD dwStyle; // Window Style
  191. RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
  192. WindowRect.left=(long)0; // Set Left Value To 0
  193. WindowRect.right=(long)width; // Set Right Value To Requested Width
  194. WindowRect.top=(long)0; // Set Top Value To 0
  195. WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
  196.  
  197. fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
  198.  
  199. hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
  200. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
  201. wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
  202. wc.cbClsExtra = 0; // No Extra Window Data
  203. wc.cbWndExtra = 0; // No Extra Window Data
  204. wc.hInstance = hInstance; // Set The Instance
  205. wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
  206. wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
  207. wc.hbrBackground = NULL; // No Background Required For GL
  208. wc.lpszMenuName = NULL; // We Don't Want A Menu
  209. wc.lpszClassName = "OpenGL"; // Set The Class Name
  210.  
  211. if (!RegisterClass(&wc)) // Attempt To Register The Window Class
  212. {
  213. MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  214. return FALSE; // Return FALSE
  215. }
  216.  
  217. if (fullscreen) // Attempt Fullscreen Mode?
  218. {
  219. DEVMODE dmScreenSettings; // Device Mode
  220. memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
  221. dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
  222. dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
  223. dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
  224. dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
  225. dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  226.  
  227. // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
  228. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  229. {
  230. // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
  231. if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
  232. {
  233. fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
  234. }
  235. else
  236. {
  237. // Pop Up A Message Box Letting User Know The Program Is Closing.
  238. MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
  239. return FALSE; // Return FALSE
  240. }
  241. }
  242. }
  243.  
  244. if (fullscreen) // Are We Still In Fullscreen Mode?
  245. {
  246. dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
  247. dwStyle=WS_POPUP; // Windows Style
  248. ShowCursor(FALSE); // Hide Mouse Pointer
  249. }
  250. else
  251. {
  252. dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
  253. dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
  254. }
  255.  
  256. AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
  257.  
  258. // Create The Window
  259. if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
  260. "OpenGL", // Class Name
  261. title, // Window Title
  262. dwStyle | // Defined Window Style
  263. WS_CLIPSIBLINGS | // Required Window Style
  264. WS_CLIPCHILDREN, // Required Window Style
  265. 0, 0, // Window Position
  266. WindowRect.right-WindowRect.left, // Calculate Window Width
  267. WindowRect.bottom-WindowRect.top, // Calculate Window Height
  268. NULL, // No Parent Window
  269. NULL, // No Menu
  270. hInstance, // Instance
  271. NULL))) // Dont Pass Anything To WM_CREATE
  272. {
  273. KillGLWindow(); // Reset The Display
  274. MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  275. return FALSE; // Return FALSE
  276. }
  277.  
  278. static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
  279. {
  280. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  281. 1, // Version Number
  282. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  283. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  284. PFD_DOUBLEBUFFER, // Must Support Double Buffering
  285. PFD_TYPE_RGBA, // Request An RGBA Format
  286. bits, // Select Our Color Depth
  287. 0, 0, 0, 0, 0, 0, // Color Bits Ignored
  288. 0, // No Alpha Buffer
  289. 0, // Shift Bit Ignored
  290. 0, // No Accumulation Buffer
  291. 0, 0, 0, 0, // Accumulation Bits Ignored
  292. 16, // 16Bit Z-Buffer (Depth Buffer)
  293. 0, // No Stencil Buffer
  294. 0, // No Auxiliary Buffer
  295. PFD_MAIN_PLANE, // Main Drawing Layer
  296. 0, // Reserved
  297. 0, 0, 0 // Layer Masks Ignored
  298. };
  299.  
  300. if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
  301. {
  302. KillGLWindow(); // Reset The Display
  303. MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  304. return FALSE; // Return FALSE
  305. }
  306.  
  307. if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
  308. {
  309. KillGLWindow(); // Reset The Display
  310. MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  311. return FALSE; // Return FALSE
  312. }
  313.  
  314. if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
  315. {
  316. KillGLWindow(); // Reset The Display
  317. MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  318. return FALSE; // Return FALSE
  319. }
  320.  
  321. if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
  322. {
  323. KillGLWindow(); // Reset The Display
  324. MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  325. return FALSE; // Return FALSE
  326. }
  327.  
  328. if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
  329. {
  330. KillGLWindow(); // Reset The Display
  331. MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  332. return FALSE; // Return FALSE
  333. }
  334.  
  335. ShowWindow(hWnd,SW_SHOW); // Show The Window
  336. SetForegroundWindow(hWnd); // Slightly Higher Priority
  337. SetFocus(hWnd); // Sets Keyboard Focus To The Window
  338. ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
  339.  
  340. if (!InitGL()) // Initialize Our Newly Created GL Window
  341. {
  342. KillGLWindow(); // Reset The Display
  343. MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  344. return FALSE; // Return FALSE
  345. }
  346.  
  347. return TRUE; // Success
  348. }
  349.  
  350. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
  351. UINT uMsg, // Message For This Window
  352. WPARAM wParam, // Additional Message Information
  353. LPARAM lParam) // Additional Message Information
  354. {
  355. switch (uMsg) // Check For Windows Messages
  356. {
  357. case WM_ACTIVATE: // Watch For Window Activate Message
  358. {
  359. // LoWord Can Be WA_INACTIVE, WA_ACTIVE, WA_CLICKACTIVE,
  360. // The High-Order Word Specifies The Minimized State Of The Window Being Activated Or Deactivated.
  361. // A NonZero Value Indicates The Window Is Minimized.
  362. if ((LOWORD(wParam) != WA_INACTIVE) && !((BOOL)HIWORD(wParam)))
  363. active=TRUE; // Program Is Active
  364. else
  365. active=FALSE; // Program Is No Longer Active
  366.  
  367. return 0; // Return To The Message Loop
  368. }
  369.  
  370. case WM_SYSCOMMAND: // Intercept System Commands
  371. {
  372. switch (wParam) // Check System Calls
  373. {
  374. case SC_SCREENSAVE: // Screensaver Trying To Start?
  375. case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
  376. return 0; // Prevent From Happening
  377. }
  378. break; // Exit
  379. }
  380.  
  381. case WM_CLOSE: // Did We Receive A Close Message?
  382. {
  383. PostQuitMessage(0); // Send A Quit Message
  384. return 0; // Jump Back
  385. }
  386.  
  387. case WM_KEYDOWN: // Is A Key Being Held Down?
  388. {
  389. keys[wParam] = TRUE; // If So, Mark It As TRUE
  390. return 0; // Jump Back
  391. }
  392.  
  393. case WM_KEYUP: // Has A Key Been Released?
  394. {
  395. keys[wParam] = FALSE; // If So, Mark It As FALSE
  396. return 0; // Jump Back
  397. }
  398.  
  399. case WM_SIZE: // Resize The OpenGL Window
  400. {
  401. ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
  402. return 0; // Jump Back
  403. }
  404. }
  405.  
  406. // Pass All Unhandled Messages To DefWindowProc
  407. return DefWindowProc(hWnd,uMsg,wParam,lParam);
  408. }
  409.  
  410. int WINAPI WinMain( HINSTANCE hInstance, // Instance
  411. HINSTANCE hPrevInstance, // Previous Instance
  412. LPSTR lpCmdLine, // Command Line Parameters
  413. int nCmdShow) // Window Show State
  414. {
  415. MSG msg; // Windows Message Structure
  416. BOOL done=FALSE; // Bool Variable To Exit Loop
  417.  
  418. // Ask The User Which Screen Mode They Prefer
  419. if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
  420. {
  421. fullscreen=FALSE; // Windowed Mode
  422. }
  423.  
  424. // Create Our OpenGL Window
  425. if (!CreateGLWindow("3D Object Test",640,480,16,fullscreen))
  426. {
  427. return 0; // Quit If Window Was Not Created
  428. }
  429.  
  430. while(!done) // Loop That Runs While done=FALSE
  431. {
  432. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
  433. {
  434. if (msg.message==WM_QUIT) // Have We Received A Quit Message?
  435. {
  436. done=TRUE; // If So done=TRUE
  437. }
  438. else // If Not, Deal With Window Messages
  439. {
  440. TranslateMessage(&msg); // Translate The Message
  441. DispatchMessage(&msg); // Dispatch The Message
  442. }
  443. }
  444. else // If There Are No Messages
  445. {
  446. // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
  447. if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received?
  448. {
  449. done=TRUE; // ESC or DrawGLScene Signalled A Quit
  450. }
  451. else // Not Time To Quit, Update Screen
  452. {
  453. SwapBuffers(hDC); // Swap Buffers (Double Buffering)
  454. }
  455.  
  456. if (keys[VK_UP])
  457. {
  458. rot += 0.1f; // on cursor up increase rotation by 0.1
  459. }
  460.  
  461. if (keys[VK_DOWN])
  462. {
  463. rot -= 0.1f; // on cursor down decrease rotation by 0.1
  464. }
  465.  
  466. if (keys[VK_NUMPAD1])
  467. {
  468. xrot = 1.0f;
  469. }
  470.  
  471. if (keys[VK_NUMPAD3])
  472. {
  473. xrot = 0.0f;
  474. }
  475.  
  476. if (keys[VK_NUMPAD4])
  477. {
  478. yrot = 1.0f;
  479. }
  480.  
  481. if (keys[VK_NUMPAD6])
  482. {
  483. yrot = 0.0f;
  484. }
  485.  
  486. if (keys[VK_NUMPAD7])
  487. {
  488. zrot = 1.0f;
  489. }
  490.  
  491. if (keys[VK_NUMPAD9])
  492. {
  493. zrot = 0.0f;
  494. }
  495.  
  496. if (keys[VK_F1]) // Is F1 Being Pressed?
  497. {
  498. keys[VK_F1]=FALSE; // If So Make Key FALSE
  499. KillGLWindow(); // Kill Our Current Window
  500. fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
  501. // Recreate Our OpenGL Window
  502. if (!CreateGLWindow("3D Object Test",640,480,16,fullscreen))
  503. {
  504. return 0; // Quit If Window Was Not Created
  505. }
  506. }
  507. }
  508. }
  509.  
  510. // Shutdown
  511. KillGLWindow(); // Kill The Window
  512. return (msg.wParam); // Exit The Program
  513. }
Add Comment
Please, Sign In to add comment