OpenGL: Toggling the DepthMask

glDepthMask(GL_TRUE/GL_FALSE);

Sometimes in blending, you will want to inter-mix opaque & translucent objects.

Here's the 2 obvious scenarios:
1. If the opaque object is in front of translucent object, we don't need to blend
2. If the translucent object is in front of the opaque object, we need to blend

There's a easy way to ensure the blending always works as expected. This is to render the opaque object FIRST with the writable depth buffer. And then render the translucent object with a read-only depth buffer. This can be done by using glDepthMask.

This way the renderer will automatically compare the translucent object's depth position with that of the read-only opaque object depth position. Blending is done only when needed, and buffer remains unchanged.

No comments: