Qt5 OpenGL GLSL 版本错误

2021-12-19 00:00:00 opengl macos qt c++ glsl

我开始在 Qt 和着色器中使用 OpenGL(我有 OpenGL 经验,但还没有使用着色器)

I'm starting out on using OpenGL with Qt, and with Shaders (I have OpenGL experience, but not with shaders yet)

我正在学习本教程:http://releases.qt-project.org/learning/developerguides/qtopengltutorial/OpenGLTutorial.pdf(Qt5 OpenGL 官方教程).

I'm following this tutorial: http://releases.qt-project.org/learning/developerguides/qtopengltutorial/OpenGLTutorial.pdf (the official Qt5 OpenGL tutorial).

问题是,当我尝试运行我的程序时,出现黑屏并显示以下错误消息:

The problem is, that when I try to run my program, I get a black screen and the following error messages:

QGLShader::compile(Vertex): ERROR: 0:1: '' :  version '130' is not supported

QGLShader::compile(Fragment): ERROR: 0:1: '' :  version '130' is not supported

我的程序基于 QGLWidget

My program is based on a QGLWidget

通过在互联网上的一些浏览,我发现我需要使用 OpenGL 3.2 上下文,但 Qt 喜欢使用 OpenGL 2.x

With some browsing on the interwebs I found out that I need to use an OpenGL 3.2 context, but that Qt likes to use OpenGL 2.x

我的电脑:

  • MacBook Pro Retina '15,2012 年末
  • 英特尔 HD 4000
  • NVIDIA GeForce 650M

那么,我怎样才能做到这一点?

So, how can I make this work?

我的版本是 3.2(通过 QGLFormat 设置),没有指定格式它使用 2.0

My version is 3.2 (set through QGLFormat), without a specified format it uses 2.0

fragmentShader.frag:

fragmentShader.frag:

#version 130

uniform vec4 color;

out vec4 fragColor;

void main(void)
{
    fragColor = color;
}

vertexShader.vert:

vertexShader.vert:

#version 130

uniform mat4 mvpMatrix;

in vec4 vertex;

void main(void)
{
    gl_Position = mvpMatrix * vertex;
}

错误(格式,OpenGL 3.2):

Errors (with format, OpenGL 3.2):

QGLShaderProgram: shader programs are not supported 
QGLShaderProgram::uniformLocation( mvpMatrix ): shader program is not linked 
The program has unexpectedly finished.

错误(无格式,OpenGL 2.0):

Errors (without format, OpenGL 2.0):

QGLShader::compile(Vertex): ERROR: 0:1: '' :  version '130' is not supported

QGLShader::compile(Fragment): ERROR: 0:1: '' :  version '130' is not supported

推荐答案

较新的 QOpenGLWidget 不支持任何带有 QGLFormat 的构造函数.相反,在您的 main.cpp 中,为所有 QOpenGLWidgetQOpenGLContext 指定默认的 QSurfaceFormat 如下:

Newer QOpenGLWidget doesn't support any constructor with QGLFormat. Instead, in your main.cpp, specify the default QSurfaceFormat for all QOpenGLWidget and QOpenGLContext as following:

// main.cpp
QSurfaceFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(glFormat);

现在你应该可以在你的着色器中使用类似 #version 330 core 的东西了.

Now you should be able to use something like #version 330 core in your shader.

相关文章