Original Post
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 : 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?
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);