Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / OpenGL

OpenGL Version Check Windows Visual Studio No Dependencies

4.89/5 (9 votes)
3 Oct 2013CPOL1 min read 28.5K  
This fully copy pasteable program shows the version of OpenGL your Windows OS supports.

Introduction

This fully copy pasteable program shows the version of OpenGL your Windows OS supports. It is useful because you don't need to include any libraries or external dependencies other than the ones that Windows already contains. This tip is aimed for Visual Studio users. It creates an OpenGL rendering context, and displays the version number of your supported OpenGL version in a MessageBox for you.

Background

I wrote the same code snippet here, but I thought it would be good to put on CodeProject as well in case someone just quickly wanted to find out their OpenGL version:

Using the Code

Create a new project in Visual Studio, choose Win32 project, choose empty project. Add a new file, main.cpp, copy paste the following code into it and press F5 to run, you should see the version number your Windows OS supports in a message box popup.

C++
#include <windows.h>
#include <GL/GL.h>

#pragma comment (lib, "opengl32.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, 
             __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
    MSG msg          = {0};
    WNDCLASS wc      = {0}; 
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = L"oglversionchecksample";
    wc.style = CS_OWNDC;
    if( !RegisterClass(&wc) )
        return 1;
    CreateWindowW(wc.lpszClassName,L"openglversioncheck", 
            WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,640,480,0,0,hInstance,0); 

    while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
        DispatchMessage( &msg );

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_CREATE:
        {
        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
        };

        HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

        int  letWindowsChooseThisPixelFormat;
        letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd); 
        SetPixelFormat(ourWindowHandleToDeviceContext,letWindowsChooseThisPixelFormat, &pfd);

        HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
        wglMakeCurrent (ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

        MessageBoxA(0,(char*) glGetString(GL_VERSION), "OPENGL VERSION",0);

        wglDeleteContext(ourOpenGLRenderingContext);
        PostQuitMessage(0);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}

Points of Interest

It was interesting to note that just simply calling wgl commands was not enough, and the window had to be created at least in WM_CREATE until the version checkup would work.

History

  • 3.10.2013 + Fixed the OpenGL Wikipedia link missing one letter in the < A > tag
  • 2.10.2013 + First version submitted

License

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