Sei sulla pagina 1di 3

#pragma once

#include <windows.h>
#include <GL/gl.h>
using namespace System::Windows::Forms;
namespace OpenGLForm
{
public ref class COpenGL: public System::Windows::Forms::NativeWindow
{
public:
COpenGL(System::Windows::Forms::Form ^ parentForm, GLsizei iWidt
h, GLsizei iHeight)
{
CreateParams^ cp = gcnew CreateParams;
// Set the position on the form
cp->X = 100;
cp->Y = 100;
cp->Height = iWidth;
cp->Width = iHeight;
// Specify the form as the parent.
cp->Parent = parentForm->Handle;
// Create as a child of the specified parent and make Op
enGL compliant (no clipping)
cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS
_CLIPCHILDREN;
// Create the actual window
this->CreateHandle(cp);
m_hDC = GetDC((HWND)this->Handle.ToPointer());
if(m_hDC)
MySetPixelFormat(m_hDC);
}
virtual System::Void Render(System::Void)
{
// Clear the color and depth buffers.
glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
System::Void SwapOpenGLBuffers(System::Void)
{
SwapBuffers(m_hDC);
}
private:
HDC m_hDC;
HGLRC m_hglrc;
protected:
~COpenGL(System::Void)
{
this->DestroyHandle();
}
GLint MySetPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of thi
s pfd
1, // version num
ber
PFD_DRAW_TO_WINDOW | // support win
dow
PFD_SUPPORT_OPENGL | // support Ope
nGL
PFD_DOUBLEBUFFER, // double buff
ered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit colo
r depth
0, 0, 0, 0, 0, 0, // color bits
ignored
0, // no alpha bu
ffer
0, // shift bit i
gnored
0, // no accumula
tion buffer
0, 0, 0, 0, // accum bits
ignored
32, // 32-bit z-bu
ffer
0, // no stencil
buffer
0, // no auxiliar
y buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks
ignored
};
GLint iPixelFormat;

// get the device context's best, available pixel format
match
if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
{
MessageBox::Show("ChoosePixelFormat Failed");
return 0;
}

// make that match the device context's current pixel fo
rmat
if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
{
MessageBox::Show("SetPixelFormat Failed");
return 0;
}
if((m_hglrc = wglCreateContext(m_hDC)) == NULL)
{
MessageBox::Show("wglCreateContext Failed");
return 0;
}
if((wglMakeCurrent(m_hDC, m_hglrc)) == NULL)
{
MessageBox::Show("wglMakeCurrent Failed");
return 0;
}
return 1;
}
};
}

Potrebbero piacerti anche