Help!!!wglCreateContext always failed at first call... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help!!!wglCreateContext always failed at first call...

kelvin79cnkelvin79cn Posts: 1Member
I am a beginner on OpenGL and I created a simple C# project for study.

The openGL functions are imported by PInvoke.

But I found that wglCreateContext always returns 0 at first time after I

choose and set the pixel format. But if I add a call to wglMakeCurrent(0,

0) before wglCreateContext, it will succeed. I rewite the same code in

C++, wglMakeCurrent never needed. Why?

The following is the codes :

bool InitializeOpenGL()
{
Graphics g = CreateGraphics();

//Get the device context associated with graphic.
IntPtr hdc = g.GetHdc();

//Without this line, wglCreateContext will fail.
gl.wglMakeCurrent(hdc, IntPtr.Zero);

PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR();
pfd.nSize = (short)System.Runtime.InteropServices.Marshal.SizeOf(pfd);
pfd.nVersion = 1;
pfd.dwFlags = gl.PFD_DRAW_TO_WINDOW | gl.PFD_DOUBLEBUFFER | gl.PFD_SUPPORT_OPENGL;

pfd.iPixelType = (byte)gl.PFD_TYPE_RGBA;
pfd.iLayerType = (byte)gl.PFD_MAIN_PLANE;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;

//choose the pixel format according to the pfd.
int pixelFormat = gl.ChoosePixelFormat(hdc, ref pfd);
//set the format just choosed.
gl.SetPixelFormat(hdc, pixelFormat, ref pfd);

// Create a gl rendering context.
m_hRc = gl.wglCreateContext(hdc);

gl.wglMakeCurrent(hdc, m_hRc);
//release the device context.
g.ReleaseHdc(hdc);
g.Dispose();

Sign In or Register to comment.