Search code examples
c++openglglslopengl-4

Unable to Use the OpenGL Core Profile and Create and OpenGL 4.x Context


I am trying to develop simple OpenGL applications using the programmable pipeline, specifically using OpenGL 4.2+, but my programs seem to be stuck using OpenGL 3.0 and GLSL 1.30.

The output from glxinfo |grep "OpenGL" on my Ubuntu 18.04 machine is as follows:

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 520 (Skylake GT2) 
OpenGL core profile version string: 4.5 (Core Profile) Mesa 18.0.5
OpenGL core profile shading language version string: 4.50
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 18.0.5
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.2 Mesa 18.0.5
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
OpenGL ES profile extensions:

I've created a simple program which I believe should initialise an OpenGL 4.2 context:

#include <iostream>
#include <string>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

using namespace std;


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(600, 600);
    glutCreateWindow("OpenGL Test");
    glutInitContextVersion (4, 2);
    glutInitContextProfile (GLUT_CORE_PROFILE);

    if(glewInit() == GLEW_OK)
    {
        cout << "GLEW initialization successful! " << endl;
        cout << "Using GLEW version " << glewGetString(GLEW_VERSION) << 
endl;
    }
    else
    {
        cerr << "Unable to initialize GLEW  ...exiting." << endl;
        exit(EXIT_FAILURE);
    }

    cout << glGetString(GL_VERSION) << endl;
    cout << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl;
}

However, when I run this program the following output is printed to the console, showing the the OpenGL version used is 3.0 and the GLSL version used is 1.3.

GLEW initialization successful! 
Using GLEW version 2.0.0
3.0 Mesa 18.0.5    # OpenGL version
1.30               # GLSL Version

I am compiling and running my program as follows:

g++ OpenGLTest.cpp -o OpenGLTest -lglut -lGLU -lGL -lGLEW
./OpenGLTest

What I am doing wrong or what else could I try to fix this?


Solution

  • glutInitContextVersion and such sets parameters to be used for next context creation; all contexts that are already created will not be affected. That is true for many libraries, not just freeglut (e.g. SDL behaves the same way). As far as I can tell there is no dedicated function to create contexts in GLUT - context is created as part of window creation, so you need to set your parameters before calling glutCreateWindow.