mpen
mpen

Reputation: 283173

Why does disabling writing to the depth buffer cause almost nothing to be drawn?

Following SigTerm's suggestion, I render all my opaque polygons first, and then I disable the depth buffer (glDepthMask(GL_FALSE)) before I render my translucent polygons.

As soon as I disable the depth buffer though, it stops drawing anything to the screen. I can only see a glimpse of the world as I move the camera around (only little pieces show up for a second, and then disappear when I stop moving).

If I leave the depth buffer enabled the whole time, everything renders fine, it's just not as nice as I'd like (translucent objects obscure other translucent objects).

How do I get it to render properly?

My main render loop looks like this:

protected void RenderTerrain()
{
    GL.Enable(EnableCap.DepthTest);
    GL.Enable(EnableCap.CullFace);

    _bfShader.Use();
    _bfProjUniform.Mat4(_viewMat * _bfProjMat);
    _bfTex.Bind();

    GL.DepthMask(true);
    GL.Disable(EnableCap.Blend);
    _blendUniform.Set1(false);
    _bfVao.Bind();
    GL.DrawElementsInstancedBaseVertex(BeginMode.TriangleStrip, SharedData.FaceIndices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero, _bfInstBuf.Length, 0);

    GL.DepthMask(false);
    GL.Enable(EnableCap.Blend);
    _blendUniform.Set1(true);
    _transVao.Bind();
    GL.DrawElementsInstancedBaseVertex(BeginMode.TriangleStrip, SharedData.FaceIndices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero, _bfTransBuf.Length, 0);
}

The other half of the render loop:

protected override void OnRenderFrame(FrameEventArgs e)
{
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    lock(_renderQueue)
    {
        while(_renderQueue.Count > 0)
        {
            _renderQueue.Dequeue().Invoke();
        }
    }
    RenderTerrain();
    RenderHUD();
    SwapBuffers();
}

Upvotes: 13

Views: 9926

Answers (1)

Tim
Tim

Reputation: 35943

Can you reenable the glDepthMask before calling glClear(), or at the end of your render loop?

Preventing writes to the depth buffer will prevent it from being correctly cleared.

http://www.opengl.org/wiki/FAQ#Masking

Upvotes: 24

Related Questions