Advanced Topics

Depth Testing

Depth testing is a technique used to discard vertices that are behind other vertices to avoid unnecessary draw calls. There is no need to draw objects that are not visible. That would be a waste.

The idea is to store the z values of every pixel in a parallel buffer than the color buffer to keep only the z value that is the closest to the camera/view.

To enable this in OpenGL we first need to enable it:

glEnable(GL_DEPTH_TEST);

We should not forget to also clear the buffer when clearing the color buffer:

void Renderer_ClearBackground(float R, float G, float B, float Alpha) {
    glClearColor(R, G, B, Alpha);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

That is pretty much it. There are some scenarios where we will need to disable the depth testing or modify how it works but we will show later a case where this is useful.

Squared Wave SVG