OpenGL C/C++


SUBMITTED BY: Guest

DATE: April 30, 2013, 1:25 a.m.

FORMAT: C++

SIZE: 4.1 kB

HITS: 1166

  1. #include <windows.h>
  2. #include <gl/gl.h>
  3. // Function Declarations
  4. LRESULT CALLBACK
  5. WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  6. VOID EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
  7. VOID DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
  8. // WinMain
  9. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  10. LPSTR lpCmdLine, int iCmdShow)
  11. {
  12. WNDCLASS wc;
  13. HWND hWnd;
  14. HDC hDC;
  15. HGLRC hRC;
  16. MSG msg;
  17. BOOL bQuit = FALSE;
  18. float theta = 0.0f;
  19. // register window class
  20. wc.style = CS_OWNDC;
  21. wc.lpfnWndProc = WndProc;
  22. wc.cbClsExtra = 0;
  23. wc.cbWndExtra = 0;
  24. wc.hInstance = hInstance;
  25. wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  26. wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  27. wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
  28. wc.lpszMenuName = NULL;
  29. wc.lpszClassName = "GLSample";
  30. RegisterClass( &wc );
  31. // create main window
  32. hWnd = CreateWindow(
  33. "GLSample", "OpenGL Sample",
  34. WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  35. 0, 0, 256, 256,
  36. NULL, NULL, hInstance, NULL);
  37. // enable OpenGL for the window
  38. EnableOpenGL( hWnd, &hDC, &hRC );
  39. // program main loop
  40. while (!bQuit)
  41. {
  42. // check for messages
  43. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  44. {
  45. // handle or dispatch messages
  46. if (msg.message == WM_QUIT)
  47. {
  48. bQuit = TRUE;
  49. }
  50. else
  51. {
  52. TranslateMessage(&msg);
  53. DispatchMessage(&msg);
  54. }
  55. }
  56. else
  57. {
  58. // OpenGL animation code goes here
  59. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  60. glClear(GL_COLOR_BUFFER_BIT);
  61. glPushMatrix();
  62. glRotatef(theta, 0.0f, 0.0f, 1.0f);
  63. glBegin(GL_TRIANGLES);
  64. glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
  65. glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
  66. glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
  67. glEnd();
  68. glPopMatrix();
  69. SwapBuffers( hDC );
  70. theta += 1.0f;
  71. }
  72. }
  73. // shutdown OpenGL
  74. DisableOpenGL( hWnd, hDC, hRC );
  75. // destroy the window explicitly
  76. DestroyWindow( hWnd );
  77. return msg.wParam;
  78. }
  79. // Window Procedure
  80. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  81. {
  82. switch (message)
  83. {
  84. case WM_CREATE:
  85. return 0;
  86. case WM_CLOSE:
  87. PostQuitMessage( 0 );
  88. return 0;
  89. case WM_DESTROY:
  90. return 0;
  91. case WM_KEYDOWN:
  92. switch (wParam)
  93. {
  94. case VK_ESCAPE:
  95. PostQuitMessage( 0 );
  96. return 0;
  97. }
  98. return 0;
  99. default:
  100. return DefWindowProc(hWnd, message, wParam, lParam);
  101. }
  102. }
  103. // Enable OpenGL
  104. VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
  105. {
  106. PIXELFORMATDESCRIPTOR pfd;
  107. int iFormat;
  108. // get the device context (DC)
  109. *hDC = GetDC( hWnd );
  110. // set the pixel format for the DC
  111. ZeroMemory( &pfd, sizeof( pfd ) );
  112. pfd.nSize = sizeof( pfd );
  113. pfd.nVersion = 1;
  114. pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  115. PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  116. pfd.iPixelType = PFD_TYPE_RGBA;
  117. pfd.cColorBits = 24;
  118. pfd.cDepthBits = 16;
  119. pfd.iLayerType = PFD_MAIN_PLANE;
  120. iFormat = ChoosePixelFormat( *hDC, &pfd );
  121. SetPixelFormat( *hDC, iFormat, &pfd );
  122. // create and enable the render context (RC)
  123. *hRC = wglCreateContext( *hDC );
  124. wglMakeCurrent( *hDC, *hRC );
  125. }
  126. // Disable OpenGL
  127. VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
  128. {
  129. wglMakeCurrent( NULL, NULL );
  130. wglDeleteContext( hRC );
  131. ReleaseDC( hWnd, hDC );
  132. }

comments powered by Disqus