[Solved] Qt5 OpenGL reports wrong glVersion
-
First of all, yes, QSurfaceFormat defaults to requesting a 2.x context as this is the minimum required for Qt Quick 2. If you want an OpenGL 4.3 context you need to ask for it via QSurfaceFormat::setVersion() as you found.
Second, there is no need to cast the result form QOpenGLContext::versionFunctions() if you use the templated version of it. e.g.
@
m_funcs = context->versionFunctions<QOpenGLFunctions_4_3_Core>();
if (!m_funcs)
// something went wrong. fallback or bail out
@As to why you see nothing when using OpenGL 4.3 I suspect that is because that example is written against older style OpenGL as evidenced by the fact it worked with OpenGL 2.x profile. To test this you could ask for an OpenGL 4.3 Compatibility profile context so that the stuff which is deprecated and removed in the core profile is still available. See QSurfaceFormat::setProfile().
Alternatively you would need to port the rest of the example to use only Core profile features. That is remove all of the fixed function pipeline calls and built in shader uniforms. Also you would need to tweak the shaders to use in/out rather than attribute/varying.
Good luck.
-
Ohhhh god I love you.
Compatibility profile worked. However, opengl.org is down from my side. I can't check what's supported in 3.1 core and what has been thrown away.
On the shaders, I clearly specify the version 420, so if it compile, am I good to assume there is no problem there ? I use gl_Position still, but I don't recall having an alternative.
On the code, the only functions used with QOpenGLFunctions are :
glClear(), glViewport(), glDrawArrays().
The other calls are made from the program itself :
link(), bind(), enableAttributeArray(), setAttributeBuffer(), disableAttributeArray(), release().
And finally, I use QOpenGLBuffers for my meshes info :
create(), setUsagePattern(), bind(), allocate().That's it. I know glViewport and glDrawArrays are not deprecated. Is glClear my issue ?
Also, the context does a swapBuffers(), is it ok ? -
I check the glError after each call and an error pops up for each call to QOpenGLShaderProgram::setAttributeBuffer :
"m_program->setAttributeBuffer("posAttr", GL_FLOAT, 0, 2);"
"m_program->setAttributeBuffer("colAttr", GL_FLOAT, 0, 3);"
This error doesn't show up for compatibility profile. Safe to assume this is my culprit ?
Any idea ? -
The problems will be in the shaders themselves.
The vertex shader has code like this for the input vertex attribute variables:
@
attribute vec2 posAttr;
attribute vec3 colAttr;
@The attribute keyword is gone in the Core profile. Instead yuo have to use "in":
@
in vec2 posAttr;
in vec3 colAttr;
@And in the vertex shader instead of using "varying" for the output you use "out". You still need to write to gl_Position as the input to the rasterizer though.
Then in the fragment shader for the input variables you no longer use "varying" but rather "in".
For the fragment shader output the old built-in variable gl_FragColor is gone. Instead you must define your own output variable for the fragment shader and write to that. Something like this...
@
out vec4 fragColor;
...
void main()
{
...
fragColor = vec4(...);
}
@Hope this helps.
-
My shaders are arleady set to version 420
@#version 420
in vec2 posAttr;
in vec3 colAttr;uniform mat4 matrix;
out vec4 vCol;
void main() {
vCol = vec4(colAttr, 1.0);
gl_Position = matrix * vec4(posAttr, 0.0, 1.0);
}@@#version 420
in vec4 vCol;
out vec4 fCol;
void main() {
fCol = vCol;
}@Is there an issue ?
-
You tell me ;) Do they compile and link? Can you obtain the locations of the attributes?
Are you creating and binidng a vertex array object anywhere? Core profile requires that you bind a vertex array object. This can be as simple as creating a QOpenGLVertexArrayObject instance and in your init function doing:
@
m_vao.create();
m_vao.bind();
@Of course that is the minimal way to do it. You can have multiple VAOs in a program.
-
Direct hit :D.
My shaders were compiling, no problem on this side (otherwise I wouldn't be able to see anything with the 2.1 profile anyway).
The problem was the VAO. As soon as I created/binded one in my init then bind it the rendering, "pouf" it worked.Thanks for the help. That was some kick ass troubleshooting you've done here :D.
Cheers.
PS : Maybe update the sample in the Qt Wiki. I don't think there is any example that works for 5.2 out there.
-
Great! Glad you got it working.
I plan to add some examples to Qt for 5.4 showing some of the newer OpenGL features and the helpers in Qt.
BTW, in case you or anybody else is interested, KDAB offers training courses in "Modern OpenGL and Qt":http://www.kdab.com/training/courses/modern-opengl-with-qt/
Happy hacking!
-
And if I may, the courses are worth both the time and money !
-
Hehe, thanks Samuel. Also good to get feedback :)