Click here to Skip to main content
15,880,608 members
Articles / Multimedia / OpenGL
Tip/Trick

The Role of WGL Functions of OpenGL Rendering onto Microsoft Windows

Rate me:
Please Sign up or sign in to vote.
1.73/5 (3 votes)
27 Feb 2021CPOL5 min read 8.6K   4  
This post sheds some light on all WGL functions(wiggle functions) in OpenGL extension for Microsoft Windows system
This post will share my discovery on all functions named as wglxxxx in header file. Surprisingly, I found the complete list of these WGL functions[3] in header file.

A Question

When we adventure into OpenGL land, as Microsoft developers, we will run into a natural question: how can OpenGL render on MFC & Win32 windows? Where does Win32/MFC message pump stay? These answers are scattered in rare posts on Internet and faded in the wind.

Furthermore, we can find out if we can have a simple framework to build our applications. [5] is one kind of attempts.

With the help of WGL functions(Wrangler library), through framebuffer, we can hangle Win32/MFC windows onto OpenGL layer. Here is my understanding of complete process flow.

Process Flow

Here is how MFC/Win32 Windows hangle onto OpenGL:

Image 1

Key Struct

If Win32 window needs to support OpenGL window, it must be set up to maintain right pixel format information in device context, then generate resource context to render OpenGL window. This is where PIXELFORMATDESCRIPTOR, the key data structure steps in.

As OpenGL standards evolve to version 4.1, Win32 window can support more OpenGL rendering with OpenGL Shading language.

Background

As we know, starting an OpenGL project in Visual Studio needs OpenGL core library and GLU library. These two libaries are in OpenGL32.lib and glu32.lib and distributed with current Visual Studio 2017 for example. We just need to include header files such as #include <GL/gl.h> and #include <GL/glu.h>, then we add open32.lib and glu32.lib in Linker setting of the project configurations. Easily, we get all OpenGL support from Microsoft.

When we need to use GLUT library, we have many options such as freeglut or glut from SGI. Now, we have GLFW library. If you browse through GLUT API index, you will see most of them (70%+)are GUI related functions including menu, keyboard and mouse manipulcations.

But, there is also library GLEW to support Windows management. Relatively, we only need a small number of functions, i.e., WGL, OpenGL extension for Microsoft Windows to support OpenGL rendering on Microsoft Windows.

When I tested many good examples from codeproject.com, I got a good understanding on these WGL functions from [1][2]. Surprisingly, I found the complete list of these WGL functions[3] in header file <wingdi.h>. So they are implemented in Win32 API to fully support these OpenGL rendering functions in Windows OS.

Code Snippet

From [2], we can see, without resorting to any external libraries, only using OpenGL features from Microsoft supplied libraries, we already obtained good deal of functions from Microsoft OpenGL support. This project is a Win32 program. The critical logic is in this function:

C++
//enable OpenGL
VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    // get the device context (DC)
    *hDC = GetDC( hWnd );

    // set the pixel format for the DC
    ZeroMemory( &pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat( *hDC, &pfd );
    SetPixelFormat( *hDC, iFormat, &pfd );

    // create and enable the render context (RC)
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );
}
C++
// Disable OpenGL
VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
{
    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( hRC );
    ReleaseDC( Structure, hDC );
} 

The major loop is already built in winMain message pump.

OpenGL project configuration in Visual Studio

As we start to test OpenGL program in Visual Studio, we need to configure it. Because it is a window application, we need to browse configuration Properties-> Linker->System -> Subsystem: set it to windows. also, since we use main() function as entry point of this application, we need to tell linker that main() function is the entry point. So in configuration Properties-> Linker -> Command Line -> additional options windows, put this option: /SUBSYSTEM:windows /ENTRY:mainCRTStartup .

Function List

To make the readers' life easier, I copied the list of functions from [1] for quick reference:

Function Name Description
wglCopyContext The wglCopyContext function copies selected groups of rendering states from one OpenGL rendering context to another.
wglCreateContext The wglCreateContext function creates a new OpenGL rendering context, which is suitable for drawing on the device referenced by hdc. The rendering context has the same pixel format as the device context.
wglCreateLayerContext The wglCreateLayerContext function creates a new OpenGL rendering context for drawing to a specified layer plane on a device context.
wglDeleteContext The wglDeleteContext function deletes a specified OpenGL rendering context.
wglDescribeLayerPlane The wglDescribeLayerPlane function obtains information about the layer planes of a given pixel format.
wglGetCurrentContext The wglGetCurrentContext function obtains a handle to the current OpenGL rendering context of the calling thread.
wglGetCurrentDC The wglGetCurrentDC function obtains a handle to the device context that is associated with the current OpenGL rendering context of the calling thread.
wglGetLayerPaletteEntries Retrieves the palette entries from a given color-index layer plane for a specified device context.
wglGetProcAddress The wglGetProcAddress function returns the address of an OpenGL extension function for use with the current OpenGL rendering context.
wglMakeCurrent The wglMakeCurrent function makes a specified OpenGL rendering context the calling thread's current rendering context.
wglRealizeLayerPalette The wglRealizeLayerPalette function maps palette entries from a given color-index layer plane into the physical palette or initializes the palette of an RGBA layer plane.
wglSetLayerPaletteEntries Sets the palette entries in a given color-index layer plane for a specified device context.
wglShareLists The wglShareLists function enables multiple OpenGL rendering contexts to share a single display-list space.
wglSwapLayerBuffers The wglSwapLayerBuffers function swaps the front and back buffers in the overlay, underlay, and main planes of the window referenced by a specified device context.
wglUseFontBitmapsA The wglUseFontBitmaps function creates a set of bitmap display lists for use in the current OpenGL rendering context.
wglUseFontBitmapsW The wglUseFontBitmaps function creates a set of bitmap display lists for use in the current OpenGL rendering context.
wglUseFontOutlinesA The wglUseFontOutlines function creates a set of display lists, one for each glyph of the currently selected outline font of a device context, for use with the current rendering context.
wglUseFontOutlinesW The wglUseFontOutlines function creates a set of display lists, one for each glyph of the currently selected outline font of a device context, for use with the current rendering context.

Conclusions

With existing support from Microsoft such as Visual Studio 2017 installation, we can use Win32 and MFC to build very useful applications already.

Furthermore, with GLFW, GLEW and GLM plus GLSL libraries, we have a complete eco-systems to implement very good C++ applications with MFC classes.

Any feedback or comments are welcome!

References

  1. wingdi.h header
  2. OpenGL Triangle Basics
  3. WGL: OpenGL Extension for Microsoft Windows NT and Windows 95
  4. PIXELFORMATDESCRIPTOR structure
  5. Simple OpenGL Framework

History

  • 20th February, 2021: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
turns good thoughts into actions...


Comments and Discussions

 
-- There are no messages in this forum --