Home » Community » Forums » OpenGL » [C#/pinvoke] wglCreateContext fails with ERROR_INVALID_PIXEL_FORMAT
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 [C#/pinvoke] wglCreateContext fails with ERROR_INVALID_PIXEL_FORMAT
Post New Topic  Post Reply 
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?

 User Rating: 1207   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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


Toolmaker salutes mother Tiberia
[My website] - [My Dune II Remake Dev Blog]
/* -Earth is 98% full. Please delete anybody you can.*/


 User Rating: 1551   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1207   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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!

 User Rating: 1207   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

Toolmaker


Toolmaker salutes mother Tiberia
[My website] - [My Dune II Remake Dev Blog]
/* -Earth is 98% full. Please delete anybody you can.*/


 User Rating: 1551   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: