O'Reilly logo

Mastering SFML Game Development by Raimondas Pupius

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required