Face culling and depth buffer
One way of solving the draw order issues is by using a depth buffer. In the simplest terms, a depth buffer, also commonly known as the Z-buffer, is basically a texture managed by OpenGL in the background that contains depth information of each pixel. When a pixel is being rendered, its depth (Z value) is checked against that on the depth buffer. If a pixel being rendered has a lower Z value, the pixel is overwritten, as it is clearly on top.
Enabling the depth buffer only comes down to a single glEnable()
method call:
Game::Game() ... { ... glEnable(GL_DEPTH_TEST); ... }
Keep in mind that the depth buffer is a texture. It is imperative to make sure it gets allocated when the window is created, and it has enough data to work with. We can make sure of that by creating an sf::ContextSettings
structure and filling out its depthBits
data member before passing it to the SFML's window Create()
method:
void GL_Window::Create() { ... sf::ContextSettings...