C++ - OpenGL - Checking the FreeGLUT, GLEW and OpenGL version

First thing to deal with OpenGL is to know which version you have on your operating system.

For this tutorial we will test it on Windows.

And because we are testing the OpenGL version, we will also check what is the FreeGLUT and GLEW version.

Explanation

To create code for OpenGL 3.3, you have to have a graphic card that handles OpenGL 3.3.

If you don't know which version you have, the code below will show you this.

For example, in the video, you could see that I have a 3.3 OpenGL version.

So I can create graphics with OpenGL 3.3 but I can't do this with OpenGL 4+.

We assume that for this tutorial you have already set up your environment, with for example Visual Studio.

The code below has been split into several functions in order to focus only on the libraries version.

Notice that you can close the application by pressing the ESC key.

The code

#include <GL\glew.h>        // the opengl library wrapped by extended features
#include <GL\freeglut.h>    // library cross-platform toolkit windows and managing input operations

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

/**
* analyse the version
*/
string makeMeString(GLint versionRaw) {
    stringstream ss;
    string str = "\0";

    ss << versionRaw;    // transfers versionRaw value into "ss"
    str = ss.str();        // sets the "str" string as the "ss" value
    return str;
}

/**
* Format the string as expected
*/
void formatMe(string *text) {
    string dot = ".";

    text->insert(1, dot); // transforms 30000 into 3.0000
    text->insert(4, dot); // transforms 3.0000 into 3.00.00
}

/**
* Message
*/
void consoleMessage() {
    const char *versionGL = "\0";
    GLint versionFreeGlutInt = 0;

    versionGL = (char *)(glGetString(GL_VERSION));
    versionFreeGlutInt = (glutGet(GLUT_VERSION));

    string versionFreeGlutString = makeMeString(versionFreeGlutInt);
    formatMe(&versionFreeGlutString);

    cout << endl;
    cout << "OpenGL version: " << versionGL << endl << endl;
    cout << "FreeGLUT version: " << versionFreeGlutString << endl << endl;

    cout << "GLEW version: " <<
        GLEW_VERSION << "." << GLEW_VERSION_MAJOR << "." <<
        GLEW_VERSION_MINOR << "." << GLEW_VERSION_MICRO << endl;
}

/**
* Manager error
*/
void managerError() {
    if (glewInit()) { // checks if glewInit() is activated
        cerr << "Unable to initialize GLEW." << endl;
        while (1); // let's use this infinite loop to check the error message before closing the window
        exit(EXIT_FAILURE);
    }
    // FreeConsole();
}

/**
* Manage display (to be implemented)
*/
void managerDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);                // clear the screen
    glutSwapBuffers();
}


/**
* Initialize FREEGLUT
*/
void initFreeGlut(int ac, char *av[]) {
    // A. init
    glutInit(&ac, av);                                // 1. inits glut with arguments from the shell
    glutInitDisplayString("");                        // 2a. sets display parameters with a string (obsolete)
    glutInitDisplayMode(GLUT_SINGLE);                // 2b. sets display parameters with defines
    glutInitWindowSize(600, 600);                    // 3. window size
    glutInitContextVersion(3, 3);                    // 4. sets the version 3.3 as current version (so some functions of 1.x and 2.x could not work properly)
    glutInitContextProfile(GLUT_CORE_PROFILE);        // 5. sets the version 3.3 for the profile core
    glutInitWindowPosition(500, 500);                // 6. distance from the top-left screen

                                                    // B. create window
    glutCreateWindow("BadproG - Hello world :D");    // 7. message displayed on top bar window

}

/**
* Manage keyboard
*/
void managerKeyboard(unsigned char key, int x, int y) {
    if (key == 27) { // 27 = ESC key
        exit(0);
    }
}

/**
* Main, what else?
*/
int main(int argc, char** argv) {
    initFreeGlut(argc, argv);    // inits freeglut
    managerError();                // manages errors
    consoleMessage();            // displays message on the console
    
    // C. register the display callback function
    glutDisplayFunc(managerDisplay);                        // 8. callback function
    glutKeyboardFunc(managerKeyboard);

    // D. main loop
    glutMainLoop();                                    // 9. infinite loop
    return 0;
}

Conclusion

A good way to avoid searching everywhere on your operating system, which OpenGL version you have.

Great job, once again you've made it. cool

Comments

Comment: 

YOUR WRONG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
The value of GLEW_VERSION is Not the Version Number
To get the version number you call glewGetString(GLEW_VERSION)
Same goes for any other enum.

Why do you think that version number is right? its 1 2 3 4
enum
{
1,
2,
3,
4,
}
You did not verify this.

Comment: 

Hello Wig,

Thank you for your comment, indeed, you are right. wink

I let the tutorial as it but everyone reading this message has to to use the glewGetString(GLEW_VERSION) to get it.

And if you want to have only the MAJOR number you have to use this like that: glewGetString(GLEW_VERSION_MAJOR).

Same for the MINOR and MICRO value.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.