Rendering 3D shapes
We learned how to draw simple 2D shapes onscreen in the previous section. However, to fully utilize the OpenGL API, we also need to learn how to use it to render 3D images. In a nutshell, 3D images are simply illusions that are created using 2D shapes, stacked in such a way that it makes them look like they’re 3D.
How to do it...
The main ingredient here is the depth value, which determines which shapes should appear in front of or behind the other shapes. The primitive shape that is positioned behind another surface (with a shallower depth than another shape) will not be rendered (or will be partially rendered). OpenGL provides a simple way to achieve this:
- Let’s continue our project from the previous 2D example. Enable depth testing by adding
glEnable(GL_DEPTH_TEST)
to theinitializeGL()
function inrenderwindow.cpp
:void RenderWindow::initializeGL() { openGLFunctions = openGLContext->functions(); ...