我遇到一个问题,OpenGL 场景的深度渲染不正确。我正在对 QOpenGLFramebufferObject 进行离屏渲染。如果我在 QGLWidget 中运行相同的代码,它会很好地呈现。这是代码:
// SETUP
SurfaceFormat format;
QWindow window;
window.setSurfaceType(QWindow::OpenGLSurface);
window.setFormat(format);
window.create();
QOpenGLContext context;
context.setFormat(format);
if (!context.create()) {
qFatal("Cannot create the requested OpenGL context!");
}
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QOpenGLFramebufferObject::Depth);
QSize drawRectSize(640, 480);
context.makeCurrent(&window);
QOpenGLFramebufferObject fbo(drawRectSize, fboFormat);
fbo.bind();
// OPENGL CODE
glViewport(0, 0, 640, 480);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_FLAT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixf(pose->getOpenGLModelView());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* calculate prjection parameters */
gluPerspective(fov, aspect, 0.01, 1.0f);
glColor3d(1, 0, 0);
glBegin(GL_TRIANGLES);
/* Draw triangles */
glEnd();
glFlush();
QImage result = fbo.toImage());
有什么想法我做错了吗?我检查了 GL_DEPTH_BITS,它似乎是 0。在此先感谢 :)
最佳答案
在您的代码中,您永远不会请求您的 FBO 的深度附件。你似乎所做的只是
QOpenGLFramebufferObjectFormat fboFormat;
QOpenGLFramebufferObject fbo(drawRectSize, fboFormat);
根据 Qt 文档,QOpenGLFramebufferObjectFormat
构造函数将执行以下操作:
By default the format specifies a non-multisample framebuffer object with no attachments, texture target GL_TEXTURE_2D, and internal format GL_RGBA8. On OpenGL/ES systems, the default internal format is GL_RGBA.
看看 Qt Documentation关于如何设置正确的格式。
关于qt - 使用 QOpenGLFramebufferObject 的 OpenGL 深度缓冲区不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22987753/