作为我在QT中使用QOpenGLWidget制作的3D网格查看器的一部分,我需要为用户提供单击模型中的节点的能力。为了仅限于选择可见节点,我尝试在我的选择算法中包含glReadPixels(GL_DEPTH_COMPONENT)。
我的问题是glReadPixels(深度)总是返回0.以下代码中的所有错误输出也返回0。 glReadPixels(红色)返回正确的值:
GLenum err = GL_NO_ERROR;
QTextStream(stdout) << "error before reading gl_red = " << err << endl;
GLfloat winX, winY, myred, mydepth;
winX = mousex;
winY = this->height() - mousey;
glReadPixels(winX,winY,1,1,GL_RED,GL_FLOAT, &myred);
QTextStream(stdout) << "GL RED = " << myred << endl;
err = glGetError();
QTextStream(stdout) << "error after reading gl_red = " << err << endl;
glReadPixels(winX,winY,1,1,GL_DEPTH_COMPONENT,GL_FLOAT, &mydepth);
QTextStream(stdout) << "GL_DEPTH_COMPONENT = " << mydepth << endl;
err = glGetError();
QTextStream(stdout) << "error after reading gl_depth = " << err << endl;
我的正常3D渲染工作正常,我的glEnable(GL_DEPTH_TEST)
函数中有initializeGL()
。目前我没有使用任何花哨的VBO或VAO。 FaceMeshQualityColor
和triVertices
都是数据类型QVector<QVector3D>
。我当前的面部渲染遵循以下进展:
shader = shaderVaryingColor;
shader->bind();
shader->setAttributeArray("color", FaceMeshQualityColor.constData());
shader->enableAttributeArray("color");
shader->setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
shader->setAttributeArray("vertex", triVertices.constData());
shader->enableAttributeArray("vertex");
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1,1);
glDrawArrays(GL_TRIANGLES, 0, triVertices.size());
glDisable(GL_POLYGON_OFFSET_FILL);
shader->disableAttributeArray("vertex");
shader->disableAttributeArray("color");
shader->release();
在我的主文件中,我明确地将我的OpenGL版本设置为具有glReadPixels(GL_DEPTH_COMPONENT)功能的东西(而不是OpenGL ES 2.0):
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat format;
format.setVersion(2, 1);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setDepthBufferSize(32);
QSurfaceFormat::setDefaultFormat(format);
MainWindow w;
w.showMaximized();
return a.exec();
}
我的glReadPixels(深度)问题是否与我对深度缓冲区的处理无关?
我是否需要激活&#39;在我调用glReadPixels之前能够从中读取的深度缓冲区?或者我是否需要让我的顶点着色器明确地将深度位置写入其他对象?
答案 0 :(得分:1)
QOpenGLWidget可用于基础FBO,如果启用了多次采样,则无法从该FBO读取深度分量。最简单的解决方案是将样本设置为零,因此您的代码将如下所示:
QSurfaceFormat format;
format.setVersion(2, 1);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setDepthBufferSize(32);
format.setSamples(0);
QSurfaceFormat::setDefaultFormat(format);
或者您可以使用多重采样,但是在没有多重采样的情况下需要额外的FBO,可以复制深度缓冲区。
class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
//
// OTHER WIDGET RELATED STUFF
//
QOpenGLFramebufferObject* mFBO = nullptr;
MyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// DRAW YOUR SCENE HERE!
//
QOpenGLContext *ctx = QOpenGLContext::currentContext();
// FBO must be re-created! is there a way to reset it?
if(mFBO) delete mFBO;
QOpenGLFramebufferObjectFormat format;
format.setSamples(0);
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
mFBO = new QOpenGLFramebufferObject(size(), format);
glBindFramebuffer(GL_READ_FRAMEBUFFER, ctx->defaultFramebufferObject());
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFBO->handle());
ctx->extraFunctions()->glBlitFramebuffer(0, 0, width(), height(), 0, 0, mFBO->width(), mFBO->height(), GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT, GL_NEAREST);
mFBO->bind(); // must rebind, otherwise it won't work!
float mouseDepth = 1.f;
glReadPixels(mouseX, mouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mouseDepth);
mFBO->release();
}
};