0

I have a QtOpenglWidget with OpenGL 3.3 running and attempting to do instance rendering, but my z-buffer will not work

Currently, I add the 3 test cubes in draw order of cube3, cube2, then cube1 and changing it shows it is just displaying the last cube drawn. I also know DEPTH_TEST is enabled as messing with glDepthFunc will just not show anything.

enter image description here

My init:

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

My Draw:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Vertex:

layout(location = 0) in highp vec3 position;
layout(location = 1) in highp mat4 modelToWorld;
uniform mat4 MVP;
out highp float DEPTH;

void main() {
    gl_Position = MVP * modelToWorld * vec4(position, 1.0);
    DEPTH = gl_Position.z / 20.0f;
}

Frag:

in highp float DEPTH;
out highp vec4 fColor;
void main() {
   fColor = vec4(DEPTH, DEPTH, DEPTH,1.0);
}

EDIT I am finding out this might be a QtWidget issue, The first thing being called in main is

QSurfaceFormat format;
format.setVersion(3, 3);
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
6
  • 2
    Do you even have a Z-Buffer? Just enabling the depth test is not enough - the depth test will do nothing if the currently bound framebuffer has no depth buffer. Commented Dec 28, 2017 at 21:23
  • 1
    @derhass sorry to sound dumb, but I was under impression z-buffer == depth test, also why would glDepthFunc(GL_GREATER) remove everything then? Commented Dec 28, 2017 at 21:29
  • 1
    @FrickeFresh: well, I just wanted to make you aware that you can't automatically assume that the depth buffer is there (I don't know if Qt has some default formats settings which would enforce that). It is correct that if no depth buffer exists, setting GL_GREATER should have no effect. Another possibility is that you just project everything to the same plane. You might think that you ruled that out by visualizing gl_Position.z, but actually, the z value the depth test will use is derived from gl_Position.z / gl_Position.w, and you might have constructed such a projection matrix. Commented Dec 28, 2017 at 21:42
  • Apart from gl_Position.z / gl_Position.w being constant, I could imagine the hypothetical case that you set a glDepthRange which maps everything to a single value, but that is really very hypothetical and would be more or less breaking the depth test on purpose, so I don't think that's the case. Commented Dec 28, 2017 at 21:46
  • @derhass I think you hit it with the gl_Position.z / gl_Position.w as doing that all the cubes are white regardless, I am gonna try fudging around with my MVP again Commented Dec 28, 2017 at 22:02

1 Answer 1

2

After HOURS of debugging and referencing examples, I found the issue!

It was this line

void MyWidget::resizeGL(int width, int height) {
    ...
    m_projection.perspective(45.0f, width / float(height), 0.0f, 1000.0f);
    ...
}

it should be

void MyWidget::resizeGL(int width, int height) {
    ...
    m_projection.perspective(45.0f, width / float(height), 0.1f, 1000.0f);
    ...
}

Apparently setting my projection with a nearPlane to 0 causes this. I actually don't know WHY this occurs so please enlighten me in comments if you know

Note: this is the QMatrix4x4 class

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know what QMatrix4x4 exactly does when specifying a near plane distance of 0.0, but a perspective projection with that property doesn't even exist in terms of how projective transformations are applied in a rendering pipeline - the resulting view volume would have to be infinite in NDC space, which completely contradicts the definition of the view volume to be a well-defined axis-aligned cuboid in NDC space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.