PIXELFORMATDESCTIPTOR problem

Hi everyone,

I’m relatively new to OpenGL programming. I successfully wrote an application with freeGLUT and GLEW displaying some triangles and some textures.
Now I wanted to create an application without freeGLUT.
The problem is that “ChoosePixelFormat()” always returns 0 instead of some matching number.
Here is my PIXELFORMATDESCRIPTOR:

 
PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),
		1, 
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		32, 
		0, 0, 0, 0, 0, 0, 
		0, 
		0, 
		0, 
		0, 0, 0, 0, 
		24, 
		0, 
		0, 
		PFD_MAIN_PLANE, 
		0, 
		0, 0, 0
	};

        int matchingPFD;
	matchingPFD = ChoosePixelFormat(hdc, &pfd);

I really have no idea what I am doing wrong here so I hope someone can help me out.
For your information I’m using Windows 8 and Visual Studio 2010.

Thanks and greetz
sikkiv

At least, as MSDN says, you can call GetLastError() after ChoosePixelFormat() to retrieve something to start with.

I know you can only call SetPixelFormat once for a window; this may be true for ChoosePixelFormat as well.

First of all, use PIXELFORMATDESCRIPTOR as a structure, and set parameters by name.
Second, unlike setting pixel format, choosing is not “malicious” operation and you can call it unlimited number of times without any convenience.
Third, what’s GetLastError saying?

Try the following form of your setting:

PIXELFORMATDESCRIPTOR pfd ;
   	memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
   	pfd.nSize  = sizeof(PIXELFORMATDESCRIPTOR);
   	pfd.nVersion   = 1; 
   	pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;   
   	pfd.iPixelType = PFD_TYPE_RGBA; 
   	pfd.cColorBits = 32;
   	pfd.cDepthBits = 24; 
   	pfd.iLayerType = PFD_MAIN_PLANE;

First thanks for your replies :slight_smile:

I set the parameters by name and called GetLastError, here is the error message:
“ChoosePixelFormat failed with error 6: the handle is invalid”
So it’s clear what I’ve done wrong, I didn’t retrieve the device context properly :whistle:
Now it’s working just fine.
I should get more comfortable with the Win32 API I think :smiley:

But thanks to all for the effort.
greetz sikkiv

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.