QOpenGLWindow renders in order of draw calls instead of depth(z coordinate).
-
Hello Everyone,
I am starting a project Design for logic based animation, and this was how far I got until the OpenGL render order continues to favor the order of draw calls instead of z position.Here is my source code is open source for auditing and suggestions although currently still considered proprietary.
https://github.com/DahliaEnterprise/DahliaAnimation/blob/main/source/openglwindows.cppWhen I swap the order of triangle and triangle two, the depth of the triangles are changed. The triangles have different z positions.
One solution found online said they had the exact problem and they needed to create a QOpenGLContext in order for depth to register; However I attempted to use their coding solution and it make the render blank(no two triangles AND clear color was not setting the background).
So I’m asking for a solution or how to properly create and attach a qopenglcontext that actually allows rendering.triangle_two_ogl_vao_quad.bind(); glDrawArrays(GL_TRIANGLES, 0, 3); triangle_two_ogl_vao_quad.release(); triangle_ogl_vao_quad.bind(); glDrawArrays(GL_TRIANGLES, 0, 3); triangle_ogl_vao_quad.release();
-
The problem is not with context. Well, not entirely at least.
Although your vertex data has a x,y,z components you only take the first two in your shader and ignore the third (z):
"layout(location = 0) in vec2 position;\n"
Your shader then always writes a 0.0 depth value:"gl_Position = vec4(position, 0.0, 1.0);\n"
.
You also have a depth func set to lessglDepthFunc(GL_LESS);
, so since everything has the same value of 0.0 whatever was drawn first will stay there, because the next 0 is no less than the previous one.You need to take the whole vec3 position and write the proper depth information to
gl_Position
.But yes, for all of that to work your context needs to have a valid depth buffer format in the first place. The example shows how to set it up for QOpenGLWIdget using
setFormat
. It's the same for QOpenGLWindow. -
@Chris-Kawa wow I can’t believe I missed that.
I’m sure that is the issue, I will try this solution right away.