HWND (window handler)
DC (device context)
1
Many of the Windows-specific initialization functions have the "wgl" prefix affixed to them.
Pixel Format
1
2
3
4
5
Each window in MS Windows has a Device Context (DC) associated with it.
This object can store something called a Pixel Format.
This(Pixel Format) is a generic structure that describes the properties of the default framebuffer that the OpenGL context you want to create should have.
Setting up the pixel format is non-intuitive. The way you create a pixel format is that you fill out a struct that describes the features you want. Then you give that struct to a function that will return a number that represents the closest match that it can find in the list of supported pixel formats. You then set this number to be the pixel format of the DC.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
32, // Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, // Number of bits for the depthbuffer
8, // Number of bits for the stencilbuffer
0, // Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
Create the Context
1
2
3
Once you have set pixel format in the DC, creating the context is easy.
You call wglCreateContext.
This function takes the DC as a parameter and returns a handle to the the OpenGL context (of type HGLRC, for handle to GL Rendering Context).
1
2
3
4
5
6
7
8
9
10
Before you can use OpenGL, the context you created must be made current.
This is done with the wglMakeCurrent function.
This takes a DC and the HGLRC context.
This is done with the wglMakeCurrent function. This takes a DC and the HGLRC context.
If there is already a current context, then this function will cause the old context to be replaced with the new.
If you pass NULL for the context, then the old one is removed and OpenGL functions will fail (or crash) as though you had never made a context current.
The current context is thread-specific;
each thread can have a different context current, and it's dangerous to have the same context current in multiple threads.
Delete the Context
1
2
3
The first step is always to make sure that the context you want to delete is not current. Call wglMakeCurrent with NULL for the context.
Now that the context is not current, you can call wglDeleteContext on it.
1
2
3
Unless you are making a very simple application, you should not use the above simple context creation steps.
There are a number of WGL extensions that give you greater power and flexibility in creating contexts.
But to get access to those extensions, you have to make context creation a bit more complex.
Create a False Context
1
2
3
4
5
6
7
The key problem is this: the function you use to get WGL extensions is, itself, an OpenGL extension.
Thus like any OpenGL function, it requires an OpenGL context to call it.
tself, an OpenGL extension. Thus like any OpenGL function, it requires an OpenGL context to call it.
So in order to get the functions we need to create a context, we have to... create a context.
Fortunately, this context does not need to be our final context.
All we need to do is create a dummy context to get function pointers, then use those functions directly.
1
Warning: Unfortunately, Windows does not allow the user to change the pixel format of a window. You get to set it exactly once. Therefore, if you want to use a different pixel format from the one your fake context used (for sRGB or multisample framebuffers, or just different bit-depths of buffers), you must destroy the window entirely and recreate it after we are finished with the dummy context.