简体   繁体   中英

Depth Buffer and Stencil Buffer Issues QML

I'm using QML, and wanted to run custom OpenGL code. I created a custom Widget in C++ (extending QQuickItem) and overrided the paint function().
When I run my application, the console prints

QSGContext::initialize: depth buffer support missing, expect rendering errors
QSGContext::initialize: stencil buffer support missing, expect rendering errors

And just as it predicted, I do get rendering errors. I'll use a spider model as an example. Here's what it should like

蜘蛛stl

And here's what I'm getting 在此输入图像描述

I also don't know exactly how to describe it, but basically the opposite face (which should be blocked by the front face) is showing through as I rotate it.

I've managed to get rid of the depth buffer error with this:

void MyGLWidget::handleWindowChanged(QQuickWindow *win)
{
    if (win) {
        connect(win, SIGNAL(beforeSynchronizing()), this, SLOT(sync()), Qt::DirectConnection);
        connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(cleanup()), Qt::DirectConnection);
        win->setClearBeforeRendering(false);
        QSurfaceFormat glFormat;
        glFormat.setVersion(3,2);
        glFormat.setProfile(QSurfaceFormat::CoreProfile);




        /*I'm showing everything for context, but this is the key line*/
        glFormat.setDepthBufferSize(1); 






        win->setFormat(glFormat);

    }
}

So now I'm only getting the stencil error, but that causes a different issue. One side is completely black, and doesn't show any of the lighting.

在此输入图像描述

Some other background info: I'm displaying a QQuickView. My OpenGLWidget is imported into QML and embedded like so:

MyGLWidget {
    id: glWidget
}

In the paint() of my renderer, I am calling glEnable(GL_DEPTH_TEST) and glEnable(GL_STENCIL_TEST) at the top, but that doesn't seem to do anything. Maybe I'm calling that in the wrong context? I don't know where else I would be able to call it, however.

As it turns out, all of my models (including the spider) had incorrect vertex normals, hence the shading issues. I should have checked them out in other rendering software.

glFormat.setDepthBufferSize(1) was the real fix

Try

glDepthMask(GL_TRUE); 
// your paintings here
glDepthMask(GL_FALSE);

See also Scene Graph and Rendering .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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