Jump to content
  • Advertisement
Sign in to follow this  
Trillian

OpenGL [C#/pinvoke] wglCreateContext fails with ERROR_INVALID_PIXEL_FORMAT

This topic is 3906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I'm trying to build a C# opengl application but I'm having a bit of trouble here. As you know, microsoft has no managed api for opengl so I decided to use my C++ opengl setup routine and adapt it to C# using pinvoke. However, the code now fails and Marshal.GetLastWin32Error() returns error 2000 : ERROR_INVALID_PIXEL_FORMAT. Here's the relevant code :
WinAPI.PIXELFORMATDESCRIPTOR pixelFormatDesc = new WinAPI.PIXELFORMATDESCRIPTOR();
pixelFormatDesc.nSize = (ushort)Marshal.SizeOf(pixelFormatDesc.GetType());
pixelFormatDesc.nVersion = 1;
pixelFormatDesc.dwFlags = WinAPI.PFD_DRAW_TO_WINDOW | WinAPI.PFD_SUPPORT_OPENGL | WinAPI.PFD_DOUBLEBUFFER;
pixelFormatDesc.iPixelType = WinAPI.PFD_TYPE_RGBA;
pixelFormatDesc.cColorBits = (byte)24; // also tryed with 32
pixelFormatDesc.cDepthBits = (byte)24; // also tryed with 0 and 16
pixelFormatDesc.iLayerType = WinAPI.PFD_MAIN_PLANE;

int pixelFormatID = WinAPI.ChoosePixelFormat(deviceContextHandle, ref pixelFormatDesc);
if(pixelFormatID == 0)
{
    WinAPI.DestroyWindow(windowHandle);
    throw new Exception("WinAPI.ChoosePixelFormat failed");
}

if(!WinAPI.SetPixelFormat(deviceContextHandle, pixelFormatID, ref pixelFormatDesc))
{
    WinAPI.DestroyWindow(windowHandle);
    throw new Exception("WinAPI.SetPixelFormat failed");
}

// The following call fails
glRenderContextHandle = WGL.wglCreateContext(deviceContextHandle);
if (glRenderContextHandle == IntPtr.Zero)
{
    int lastError = Marshal.GetLastWin32Error();
    WinAPI.DestroyWindow(windowHandle);
    throw new Exception("WGL.wglCreateContext failed");
}

//WGL.wglCreateContext is :
//[DllImport("opengl32.dll", SetLastError=true)]
//public static extern IntPtr wglCreateContext(IntPtr hdc);



The debugger confirmed that my PIXELFORMATDESCRIPTOR structure was filled with precisely the same data as it was with my C++ application, which works. Googling have found out that bogus ATI drivers might cause this but I have a GeForce 8600M. Any clues?

Share this post


Link to post
Share on other sites
Advertisement
Try the OpenGL framework for .NET called Tao. It are the .NET bindings for OpenGL, ODE, Lua, DevIL and a few other often uses libraries.

It saves you a lot of work.

Toolmaker

Share this post


Link to post
Share on other sites
Well, Tao has opengl bindings (it imports the gl* functions) but nothing related to a windows forms opengl control or a simple opengl window (which is what I'm trying to do after an unsuccessful attempt at doing an openg control based on SharpGL).

I see there's also Tao.Glfw, but I'm not fond of glut-like things, and my window class is so close to work (if I get rid of this error then there's only wglMakeCurrent and my window's ready to rumble).

Share this post


Link to post
Share on other sites
Oh! A little bit more research found out that a SimpleOpenGLControl is available in Tao.Platform.Windows.dll. I'd have expected such control to be in Tao.OpenGl.dll.

Now I'm going to go ahead and try it! Thanks!

Share this post


Link to post
Share on other sites
There's also a WinAPI namespace in the OpenGL binding providing several of the WinAPI things, including the PixelFormat thing you're doing.

Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!