Qt5 OpenGL GLSL version error

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

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

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 

My program is based on a QGLWidget

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

My computer:

  • MacBook pro retina '15, late 2012
  • Intel HD 4000
  • NVidia GeForce 650M

So, how can I make this work?

EDIT:

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

fragmentShader.frag:

#version 130  uniform vec4 color;  out vec4 fragColor;  void main(void) {     fragColor = color; } 

vertexShader.vert:

#version 130  uniform mat4 mvpMatrix;  in vec4 vertex;  void main(void) {     gl_Position = mvpMatrix * vertex; } 

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. 

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 

回答1:

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); 

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



回答2:

You should create an QGLFormat object and pass it to the QGLWidget as a constructor parameter. The QGLFormat object should be created as showed in the code below.

QGLFormat glFormat; glFormat.setVersion( 3, 2 ); glFormat.setProfile( QGLFormat::CoreProfile ); 


回答3:

I've got the same error on my macbook (early 2011), and this answer helps me. Basically you deprecate to version120.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!