T2.4 [opt] Draw a cube and rotate

Add cube vertices

One cube has 8 vertices. Set the Z value of current 4 vertices to 1.0. Copy the existing 4 vertices and replace its third component with -1.0.

It will give us all the 8 vertices needed.

Colour assignment

The colour assignment as as follows

Set indices of triangles

in drawTriangles() change the number of vertices you are going to draw

enable depth test in main()

add glEnable(GL_DEPTH_TEST)

add GL_DEPTH_BUFFER_BIT in glClear()

Set the orthographic projection matrix

To show hidden surface correctly, we need to set up a basic orthographics projeciton matrix

Add projection matrix uniform in colour.vert and and use that to calcuate gl_Position

Connect the projection matrix to your glm orthographic matrix at the end of initTriangle()

Now you can see

Play it with scale, rotation and translation.

The following is scale by 0.5, rotate around Y axis by 45 degrees and then rotate around X axis by 30 degrees, as shown in the code

Last updated