Drawing a Cube

Moving on from 2D to 3D in our drawing process, we arrive at the easiest geometric form one could draw with an extra dimension added: the humble cube. Because we are moving step by step in our learning process, we will be drawing a cube in the most inconvenient way possible: by creating the vertex array, triangle by triangle and passing it to the shader.

Our beautiful cube

Keeping the tone of the previous drawing examples we’ve presented in this series of tutorials, we will use a Cube class. It derives from the Model class and its primary functions are creation and drawing of the cube. The header and cpp file in their entirety are as follow:

In the header, we use the rotation vector to store the degrees by which the object is rotated on each axis. Rotation_speed is used to tweak the rotation speed on each axis.

In the cpp file, we bind the vertex buffer and fill it with all the triangles that a 2x2x2 cube is comprised of, as well as give each vertex its own unique color, depending on its position within the cube. In the Draw method, we modify the rotation array and pass it along with the view and projection matrices to the shader. I took the liberty of adding rotation on 2 axes and color each vertex in a different color so that we could see more of it, from all sides.

Regarding the values transmitted as uniform, these are values which will remain constant (or uniform) and can be accessed from any shader which makes up the program that these values are associated with. These values are usually constants (gravity, coefficients, etc.) and are extremely useful to operations within shaders.

In the shader, we calculate the rotation matrices around each axis, according to the angles received via the rotation attribute, after which we simply multiply all of our matrices to get the correct position of each vertex.

The next big change to the existing files is in the Scene_manager, where we calculate the view and projection matrices, to be passed on to the model.

Other less significant changes have been made to the Models_manager class (include the Cube class in the header, small changes in a few methods) and to the classes that Cube derives from, by adding a new Draw method with the view and projection matrices as parameters. All of these can be downloaded from our Git repository. Next time we will see how to improve just a little bit our engine.

I'm an engineer, currently employed at a financial software company. My interests include gaming, LPing and, of course, reviewing, but also game dev and graphics. Also, in the past I've dabbled in amateur photography, reviewing movies and writing short stories and blog posts. I am also a huge Song of Ice and Fire fan, but that's beside the point. Youtube Channel, Deviantart , Google + , Twitter