How it works...

In any 3D rendering, depth is very important and hence we need to enable the depth testing feature in OpenGL by calling glEnable(GL_DEPTH_TEST). When we clear the buffer, we must also specify GL_DEPH_BUFFER_BIT so that the depth information is also being cleared, in order for the next image to be rendered correctly.

We use the MVP matrix in OpenGL so that the GPU knows how to render the 3D graphics correctly. In OpenGL 3 and later versions, OpenGL no longer handles this automatically through fixed functions. Programmers are given the freedom and flexibility to define their own matrices based on their use cases, then simply supply it to the GPU through a shader for rendering the final image. The model matrix contains the transformation data of the 3D object, namely the position, rotation, and scale of the object. The view matrix, on the other hand, is the camera or view information. Lastly, the projection matrix tells the GPU which projection method to use when projecting the 3D world onto the 2D screen.

In our example, we used the perspective projection method, which gives better perception of distance and depth. The opposite of perspective projection is orthographic projection, which makes everything look flat and parallel:

In this example, we used a timer to increase the rotation value by 50. Multiplying it with the deltaTime value. The deltaTime value varies, depending on your rendering frame rate. However, it makes the resulting animation speed consistent across different hardware that renders at a different frame rate.

Remember to call update() manually so that the screen gets refreshed, otherwise the cube will not be animated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset