3

Before creating an OpenGL context on Windows, we need to call SetPixelFormat for the Window's device context. Its function prototype is as follows:

BOOL WINAPI SetPixelFormat(
     HDC hdc,
     int iPixelFormat,
     const PIXELFORMATDESCRIPTOR *ppfd);

When creating a fixed function context, we get a supported pixel format index by calling ChoosePixelFormat with our desired pixel format, so the value that really matters is the value passed as the iPixelFormat argument. And when creating a modern GL context, we still need to call SetPixelFormat according to the docs

"Once you have a pixel format number, you can set it just like any pixel format with SetPixelFormat."

This is even though the PIXELFORMATDESCRIPTOR structure is not really relevant. In this case, I've just been passing NULL.

Does it matter what I pass as the third parameter to SetPixelFormat? If so, when?

Nasser Al-Shawwa
  • 3,573
  • 17
  • 27

1 Answers1

4

Well yes and no. The PFD struct is mainly used for ChoosePixelFormat, unless you're using wglChoosePixelFormatARB of course. As far as I recall SetPixelFormat relies on iPixelFormat and passing the PFD struct is just a nice "extra" or meta data in other words.

The SetPixelFormat documentation is a bit cryptic but does actually mention this:

Pointer to a PIXELFORMATDESCRIPTOR structure that contains the logical pixel format specification. The system's metafile component uses this structure to record the logical pixel format specification. The structure has no other effect upon the behavior of the SetPixelFormat function.

So first you setup a PFD and define what your pixel format requires. Then calling ChoosePixelFormat finds a matching pixel format and returns the index. Calling SetPixelFormat then applies this pixel format using the index to the device context. The PFD in relation to SetPixelFormat is just meta data.

All in all I would recommend passing it anyways, just to be on the safe side.

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • That's what I do for Fixed Function OpenGL context creation, where I first copy the values of the chosen index using `DescribePixelFormat`. But what about the case for modern OpenGL context creation, where the pixel format is chosen using `wglChoosePixelFormatARB`, and the PFD structure is not even used? Any idea? – Nasser Al-Shawwa Mar 21 '17 at 12:16
  • Take a look at my edit, it reflects this. – vallentin Mar 21 '17 at 12:19