OpenGL Depth Buffer不使用z值

问题描述 投票:0回答:1

我有一个QtOpenglWidget与OpenGL 3.3运行并尝试进行实例渲染,但我的z缓冲区将无法正常工作

目前,我按照cube3,cube2和cube1的绘制顺序添加3个测试立方体,并且更改它显示它只显示最后绘制的多维数据集。我也知道DEPTH_TEST启用,因为搞乱glDepthFunc将不会显示任何东西。

enter image description here

我的初学者:

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

我的画:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

顶点:

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

问:

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

编辑我发现这可能是一个QWidget问题,在main调用的第一件事是

QSurfaceFormat format;
format.setVersion(3, 3);
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
opengl opengl-3 depth-buffer qtopengl
1个回答
2
投票

经过HOURS的调试和参考示例,我发现了这个问题!

就是这条线

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

它应该是

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

显然用nearPlane将我的投影设置为0会导致这种情况。我实际上不知道为什么会这样,所以如果你知道,请在评论中启发我

注意:这是QMatrix4x4

© www.soinside.com 2019 - 2024. All rights reserved.