Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C#/pinvoke] wglCreateContext fails with ERROR_INVALID_PIXEL_FORMAT

Started by Trillian Jan 14, 2008 at 3:09 PM 3 replies 5.5k views
Original Post
Trillian
Trillian
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?
Toolmaker
Toolmaker
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
 
Trillian
Trillian
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).
Trillian
Trillian
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!
Toolmaker
Toolmaker
There's also a WinAPI namespace in the OpenGL binding providing several of the WinAPI things, including the PixelFormat thing you're doing.

Toolmaker
 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.