Jump to content

View more

Image of the Day

The beast has a mighty searchlight of evil! #screenshotsaturday https://t.co/bzKepOojDL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

[C#/pinvoke] wglCreateContext fails with ERROR_INVALID_PIXEL_FORMAT

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
4 replies to this topic

#1 Trillian   Members   

410
Like
0Likes
Like

Posted 14 January 2008 - 09:09 AM

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?

#2 Toolmaker   Members   

967
Like
0Likes
Like

Posted 14 January 2008 - 09:19 AM

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

 


#3 Trillian   Members   

410
Like
0Likes
Like

Posted 14 January 2008 - 09:40 AM

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).

#4 Trillian   Members   

410
Like
0Likes
Like

Posted 14 January 2008 - 09:50 AM

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!

#5 Toolmaker   Members   

967
Like
0Likes
Like

Posted 14 January 2008 - 09:59 AM

There's also a WinAPI namespace in the OpenGL binding providing several of the WinAPI things, including the PixelFormat thing you're doing.

Toolmaker

 





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.