Hi All, I am new to Winform user Control Design. I wanna know how to make the user control with OpenGL context. I have tried below one, but it show nothing when I use the Control. I don't know what is the problem about it. Thanks your help in advance.
#pragma once
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <string>
#include <math.h>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
namespace OpenGLPanel {
public ref class OpenGLPanelControl : public System::Windows::Forms::UserControl
{
public:
float FarW,FarH, NearW,NearH;
float Fovy, Near, Far, Aspect;
float TransX, TransY, TransZ;
float RotX, RotY, RotZ,RotAng;
float ScaleX,ScaleY,ScaleZ;
String ^NearInfo, ^FarInfo, ^TransInfo, ^RotInfo, ^ScaleInfo;
public:
OpenGLPanelControl(void)
{
InitializeComponent();
}
void InitializeOpenGL()
{
CreateParams^ cp = gcnew CreateParams;
HWND GLhwnd = (HWND)this->Handle.ToPointer();
m_hDC = GetDC(GLhwnd);
if(m_hDC)
{
InitPixelFormat(m_hDC);
ReSizeGLScene(this->Width, this->Height);
InitGL();
DWORD dw = GetLastError();
}
}
GLint InitPixelFormat(HDC hdc)
{
static PIXELFORMATDESCRIPTOR pfd= {
sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 };
GLint iPixelFormat;
if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
{
MessageBox::Show("ChoosePixelFormat Failed");
return 0;
}
if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
{
MessageBox::Show("SetPixelFormat Failed");
return 0;
}
m_hglrc = wglGetCurrentContext();
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;
}
DWORD dw = GetLastError();
return 1;
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) {
if (height==0) {
height=1; }
Fovy = 45.0f;
Near = 0.1f;
Far = 100.0f;
Aspect = (float)width/height;
NearInfo = Aspect.ToString("F2");
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluPerspective(Fovy,Aspect,Near,Far);
NearH = (float)2*Near*tan(Near*3.1415926/360);
NearW = NearH*Aspect;
FarH = (float)2*Far*tan(Far*3.1415926/360);
FarW = FarH*Aspect;
NearInfo ="Near:" + Near.ToString() + ", Width--" + NearW.ToString() + " ; " + "Heigh--" + NearH.ToString();
FarInfo = "Far:"+ Far.ToString() + ", Width--" + FarW.ToString() + " ; " + "Heigh--" + FarH.ToString();
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
bool InitGL(GLvoid) {
TransX = 0;
TransY = 0;
TransZ = -6.0f;
RotX = 1;
RotY = 0;
RotZ = 0;
RotAng = 45.0f;
ScaleX = 1;
ScaleY = 1;
ScaleZ = 1;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); return TRUE; }
System::Void Render(System::Void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glTranslatef(TransX,TransY,TransZ); TransInfo = "glTranslatef(" + TransX.ToString()+ " , " + TransY.ToString() + " , " + TransZ.ToString()+ ");";
glRotatef(RotAng,RotX,RotY,RotZ); RotInfo = "Angle : " + RotAng.ToString()+ " , "
+ "Vector:" + "(" + RotX.ToString() + " , " + RotY.ToString() + " , " +RotZ.ToString() + ");";
glScalef(ScaleX,ScaleY,ScaleZ); ScaleInfo = "glScalef(" + ScaleX.ToString()+ " , " + ScaleY.ToString() + " , " + ScaleZ.ToString()+ ");";
glBegin(GL_LINES);
glColor4f(1.0f,0.0f,0.0f,1.0f);
glVertex3f( -2.0f, 0.0f, 0.0f);
glVertex3f( -1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glVertex3f( 2.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glEnd();
glPopMatrix();
glLoadIdentity();
}
System::Void SwapOpenGLBuffers(System::Void)
{
SwapBuffers(m_hDC) ;
}
protected:
~OpenGLPanelControl()
{
if (components)
{
delete components;
}
}
private:
HDC m_hDC;
HGLRC m_hglrc;
System::ComponentModel::Container^ components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->SuspendLayout();
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->Name = L"OpenGLPanelControl";
this->Size = System::Drawing::Size(332, 227);
this->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &OpenGLPanelControl::OpenGLPanelControl_MouseDown);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void OpenGLPanelControl_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
}
};
}