4

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
Dirk
  • 2,094
  • 3
  • 25
  • 28
  • Can you add to your message the shaders that trigger those errors? (Assuming, of course, that just dropping `#version 130` from the shader's source doesn't make them work) – peppe Jun 30 '13 at 14:47
  • The shaders already contain `#version 130`, that's the reason it doesn't work – Dirk Jun 30 '13 at 15:10
  • Still, could you please add the source of the shaders you're using to your post? Also, could you please state which Qt version you're using, and dump the exact version of the OpenGL context you get? (via `glwidget->context()->format().majorVersion()` and `minorVersion`). – peppe Jun 30 '13 at 16:53
  • I have put my version, shaders and errors in my post – Dirk Jun 30 '13 at 17:57

3 Answers3

6

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.

fangda
  • 623
  • 1
  • 7
  • 8
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 );
Mortennobel
  • 3,383
  • 4
  • 29
  • 46
  • That solves those errors, but it gives me this error: `QGLShaderProgram: shader programs are not supported ` – Dirk Jun 30 '13 at 15:02
  • QGLShaderProgram is a small wrapper that basically makes things a little easier - but it is actually not needed. I have created a simple OpenGL 3.2 Qt example at Github you can use for inspiration: https://github.com/mortennobel/QtOpenGL3.2Core – Mortennobel Jun 30 '13 at 15:08
  • Qt 5.0 prefers OpenGL 2.0 (as you already know). If you want to use the Qt OpenGL abstraction layer, I suggest you install Qt 5.1 beta, which support modern OpenGL much better ( http://www.kdab.com/opengl-in-qt-5-1-part-1/ ) – Mortennobel Jun 30 '13 at 15:35
  • I did that, but now I get this: `QGLShader::compile(Vertex): ERROR: 0:1: '' : version '130' is not supported ERROR: 0:2: '' : #version required and missing. QGLShader::compile(Fragment): ERROR: 0:1: '' : version '130' is not supported ERROR: 0:2: '' : #version required and missing. ` – Dirk Jun 30 '13 at 16:24
  • "Qt 5.0 prefers OpenGL 2.0" is false (and nonsense. Qt doesn't "prefer" anything.). – peppe Jun 30 '13 at 16:57
  • @user1833511 are you sure that you are using QT 5.1.0? Try: `qDebug() << QT_VERSION_STR;` – Mortennobel Jul 01 '13 at 09:20
0

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

Community
  • 1
  • 1
xysun
  • 1,995
  • 18
  • 18