13

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();
}
Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 3
    Have you tried to disable depth testing via `glDisable(GL_DEPTH_TEST)`? The symptoms also indicate you not clearing your depth buffer. Clear both the color and depth buffer using `glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)`. – Steven Lu Mar 04 '12 at 03:37
  • Or set `glDepthFunc(GL_ALWAYS)` perhaps? – Brett Hale Mar 04 '12 at 17:31
  • 1
    @StevenLu: Disabling the depth test does [exactly what I thought it would](http://i.imgur.com/iabpz.png). I need the depth test so that it doesn't appear in front of my opaque objects. And yes, I'm clearing both buffers. – mpen Mar 04 '12 at 20:03
  • 1
    @BrettHale: `glDepthFunc(GL_ALWAYS)` has the same effect as Steven's suggestion. – mpen Mar 04 '12 at 20:06

1 Answers1

24

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

Tim
  • 35,413
  • 11
  • 95
  • 121
  • Bingo! That looks like it was the issue. Wouldn't have thought of that. – mpen Mar 04 '12 at 20:27
  • ... this took me 1 day. A external framework disabled DepthMask, so my clearing didn't work. thanks a lot. – kaiser Jun 02 '19 at 14:32