无法使用 OpenGL 核心配置文件和创建 OpenGL 4.x 上下文

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

我正在尝试使用可编程管道开发简单的 OpenGL 应用程序,特别是使用 OpenGL 4.2+,但我的程序似乎无法使用 OpenGL 3.0 和 GLSL 1.30。

在我的 Ubuntu 18.04 机器上 glxinfo |grep "OpenGL" 的输出如下:

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:

我创建了一个简单的程序,我认为它应该初始化 OpenGL 4.2 上下文:

#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;
}

但是,当我运行这个程序时,以下输出被打印到控制台,显示使用的 OpenGL 版本是 3.0,使用的 GLSL 版本是 1.3。

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

我正在编译和运行我的程序如下:

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

我做错了什么或者我还能尝试解决这个问题吗?

glutInitContextVersion 等设置用于下一个上下文创建的参数;所有已经创建的上下文都不会受到影响。许多图书馆都是如此,而不仅仅是 freeglut(例如 SDL 的行为方式相同)。据我所知,在 GLUT 中没有创建上下文的专用函数 - 上下文是作为 window 创建的一部分创建的,因此您需要在调用 glutCreateWindow.

之前设置参数