setting up opengl in .net

OK, so I am trying to set up opengl in a form created in .net. I have everything (all the includes and such) set up correctly. The form is created correctly and I call InitializeGL() at the end of the creation. The I call setupPixelFormat and set everything up for the window. When done right, the window should turn black to show that the opengl window was created successfully, but nothing is happening, can anyone help me to solve this problem, thanks. Here are my three functions as of right now.

I thought my problem might lie in the way I set up the hdc or hglrc, but I don’t know. Thanks again for anyone who can help.

cbtest::cbtest(int displayWidth, int displayheight)
{
displayWidth1 = displayWidth;
displayheight1 = displayheight;
//
// cbtest
//

		this->SuspendLayout();
	

		this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
		//this->BackColor = System::Drawing::SystemColors::ControlLightLight;
		this->ClientSize = System::Drawing::Size((displayWidth), (displayheight));

		this->ControlBox = false;
		this->MaximizeBox = false;
		this->Name = S"cbtest";
		this->Text = S"cbtest";
		this->ResumeLayout(false);
		InitializeGL();

}

void cbtest::InitializeGL()
{
hDC = ::GetDC(GetActiveWindow());
SetupPixelFormat();
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
// Lastly lets set up all the OpenGl info we will need (clear the screen to black
// and enable depth testing).
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
//smooth shading
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}

int cbtest::SetupPixelFormat()
{
// Just like in the Win32 OpenGL tutorials we have to set up the SetupPixelFormat().
// This is the same code from the Win32 tutorials.
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
32, // 32-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accumulation bits ignored
32, // 16-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};

int m_nPixelFormat = ChoosePixelFormat(hDC, &pfd);
if(m_nPixelFormat == 0) return -1;
return SetPixelFormat(hDC, m_nPixelFormat, &pfd);
}

Hi !

You need to call at least glViewport and glClear, and glFlush/SwapBuffers to get any kind of output in your window.

Mikael

that was my mistake, but I do call clear and swapbuffers in the paint function for the form, but I still get nothing.

I think this is the problem, because probably you get a wrong hdc:

Originally posted by dlswimmer:

void cbtest::InitializeGL()
{
hDC = ::GetDC(GetActiveWindow());

try:

HDC hdc = ::GetDC((HWND) this->get_Handle().ToPointer());

//or

Graphics* graphics = Graphics::FromHwnd(this->get_Handle());
System::IntPtr	graphics_hdc = graphics->GetHdc();

HDC hdc = (HDC) graphics_hdc.ToPointer();

I’m not sure which version is better but both work for me. The second is probably a little heavy weight and I’m also not sure if mixing of GDI and ogl works.

this->get_Handle() is a method of System.Windows.Forms.Control and returns the Handle Property (HWND) of a control as System::IntPtr.

thank you very much valoh, the HDC hdc = ::GetDC((HWND) this->get_Handle().ToPointer()); worked like a charm. Thanks again