/OpenGl/COpenGL.h

https://github.com/RJPalmer/OpenGLWinForm · C Header · 202 lines · 120 code · 64 blank · 18 comment · 11 complexity · 585bf9487a3f7ad8236baab37ce7049b MD5 · raw file

  1. #pragma once
  2. #include <windows.h>
  3. #include <gl\gl.h> // Header File For The OpenGL32 Library
  4. #include <gl\glu.h> // Header File For The GLu32 Library
  5. using namespace System::Windows::Forms;
  6. namespace OpenGLForm
  7. {
  8. public ref class COpenGL:
  9. public System::Windows::Forms::NativeWindow
  10. {
  11. public:
  12. COpenGL(System::Windows::Forms::Panel ^ parentPanel,
  13. GLsizei startX, GLsizei startY, GLsizei iWidth, GLsizei iHeight)
  14. {
  15. CreateParams^ cp = gcnew CreateParams;
  16. // Set the position on the form
  17. cp->X = startX - 40;
  18. cp->Y = startY - 40;
  19. cp->Height = iHeight;
  20. cp->Width = iWidth;
  21. // Specify the form as the parent.
  22. cp->Parent = parentPanel->Handle;
  23. // Create as a child of the specified parent
  24. // and make OpenGL compliant (no clipping)
  25. cp->Style = WS_CHILD | WS_VISIBLE |
  26. WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  27. /*
  28. FromHandle(this->Handle.ToPointer())
  29. */
  30. // Create the actual window
  31. this->CreateHandle(cp);
  32. m_hDC = GetDC((HWND)this->Handle.ToPointer());
  33. if(m_hDC)
  34. MySetPixelFormat(m_hDC);
  35. }
  36. static float theta = 0.00f;
  37. virtual System::Void Render(System::Void)
  38. {
  39. // Clear the color and depth buffers.
  40. glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
  41. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  42. glPushMatrix ();
  43. glRotatef (theta, 0.0f, 0.0f, 1.0f);
  44. glBegin (GL_TRIANGLES);
  45. glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (0.0f, 1.0f);
  46. glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (0.87f, -0.5f);
  47. glColor3f (0.0f, 0.0f, 1.0f); glVertex2f (-0.87f, -0.5f);
  48. glEnd ();
  49. glPopMatrix ();
  50. }
  51. /**
  52. *rotateShape() - Rotates a given shape
  53. *param theta - angle to rotate at
  54. */
  55. System::Void rotateShape(float theta)
  56. {
  57. this->theta = theta;
  58. }
  59. System::Void Redraw(System::Windows::Forms::Panel ^ parentPanel, GLsizei startX, GLsizei startY,
  60. GLsizei width, GLsizei height)
  61. {
  62. if (height==0) // Prevent A Divide By Zero By
  63. {
  64. height=1; // Making Height Equal One
  65. }
  66. glViewport(0,0,width,height); // Reset The Current Viewport
  67. //glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  68. glLoadIdentity(); // Reset The Projection Matrix
  69. // Calculate The Aspect Ratio Of The Window
  70. gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  71. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  72. glLoadIdentity();
  73. if(this)
  74. SetWindowPos((HWND)this->Handle.ToPointer(), NULL, startX - 40, startY -40,
  75. width, height, SWP_NOZORDER | SWP_NOACTIVATE);
  76. }
  77. System::Void SwapOpenGLBuffers(System::Void)
  78. {
  79. SwapBuffers(m_hDC) ;
  80. }
  81. private:
  82. HDC m_hDC;
  83. HGLRC m_hglrc;
  84. protected:
  85. ~COpenGL(System::Void)
  86. {
  87. this->DestroyHandle();
  88. }
  89. GLint MySetPixelFormat(HDC hdc)
  90. {
  91. PIXELFORMATDESCRIPTOR pfd = {
  92. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  93. 1, // version number
  94. PFD_DRAW_TO_WINDOW | // support window
  95. PFD_SUPPORT_OPENGL | // support OpenGL
  96. PFD_DOUBLEBUFFER, // double buffered
  97. PFD_TYPE_RGBA, // RGBA type
  98. 24, // 24-bit color depth
  99. 0, 0, 0, 0, 0, 0, // color bits ignored
  100. 0, // no alpha buffer
  101. 0, // shift bit ignored
  102. 0, // no accumulation buffer
  103. 0, 0, 0, 0, // accum bits ignored
  104. 32, // 32-bit z-buffer
  105. 0, // no stencil buffer
  106. 0, // no auxiliary buffer
  107. PFD_MAIN_PLANE, // main layer
  108. 0, // reserved
  109. 0, 0, 0 // layer masks ignored
  110. };
  111. GLint iPixelFormat;
  112. // get the device context's best, available pixel format match
  113. if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
  114. {
  115. MessageBox::Show("ChoosePixelFormat Failed");
  116. return 0;
  117. }
  118. // make that match the device context's current pixel format
  119. if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
  120. {
  121. MessageBox::Show("SetPixelFormat Failed");
  122. return 0;
  123. }
  124. if((m_hglrc = wglCreateContext(m_hDC)) == NULL)
  125. {
  126. MessageBox::Show("wglCreateContext Failed");
  127. return 0;
  128. }
  129. if((wglMakeCurrent(m_hDC, m_hglrc)) == NULL)
  130. {
  131. MessageBox::Show("wglMakeCurrent Failed");
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. };
  137. }