简体   繁体   English

使用QOpenGLFramebufferObject的OpenGL深度缓冲区不起作用

[英]OpenGL depth buffer using QOpenGLFramebufferObject not working

I have a problem where the depth of an OpenGL scene is not rendered correctly. 我有一个问题,其中OpenGL场景的深度无法正确渲染。 Im doing off-screen rendering into a QOpenGLFramebufferObject. 我在屏幕外渲染到QOpenGLFramebufferObject中。 If I run the same code in a QGLWidget it renders fine. 如果我在QGLWidget中运行相同的代码,它将很好。 Here is the code: 这是代码:

// 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());

Any ideas what im doing wrong? 任何想法我做错了什么? I checked GL_DEPTH_BITS and it seems to be 0. Thanks in advance :) 我检查了GL_DEPTH_BITS,它似乎是0。在此先感谢:)

In your code, you never request a depth attachment for your FBO. 在您的代码中,您永远不会为FBO 请求深度附件。 All you seem to do is 您似乎要做的就是

QOpenGLFramebufferObjectFormat fboFormat;
QOpenGLFramebufferObject fbo(drawRectSize, fboFormat);

According to the Qt docs, the QOpenGLFramebufferObjectFormat constructur will just do the following: 根据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. 默认情况下,格式指定无附件的非多样本帧缓冲对象,纹理目标GL_TEXTURE_2D和内部格式GL_RGBA8。 On OpenGL/ES systems, the default internal format is GL_RGBA. 在OpenGL / ES系统上,默认内部格式为GL_RGBA。

Have a look at the Qt Documentation on how to set the correct format. 查看有关如何设置正确格式的Qt文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM