Stars 004 - rotating cube

Commit 7051262 implements the Model-View-Projection method to render a rotating cube.


OpenGL is essentially a very generic way to draw triangles. This is what “rasterization” means: it’s the process of taking a shape (in this case, a triangle defined by 3 points), and calculating each pixel it should take up. The cool thing about modern OpenGL, I think, is that there isn’t a direct way to say here are triangles; here are their colors; please draw them! Instead, you give the GPU some data and a shader program, and say please run this program once for each n bytes of the data. The shader program, which is run many times in parallel, then constructs vertices from this data. You, as the programmer, write this shader program and dictate how that data translates into vertices. Only then does the GPU take the triangles and draw them.

This lets you include arbitrarily complex information in each vertex: depth, color, or any other input to your shading code.


MVP is I think how everyone does 3D rendering. In the last post, I did have a 3D perspective, but it was accomplished in a different way:

When rendering the vertices in the shader program, I said the magnitude of any vertex (the distance away from the origin) should depend on its depth. (i.e. setting w to z) This did produce some sort of strange perspective but the second I tried to rotate the cube, it skewed in a very strange way.

That’s supposed to be a rotation around the y axis which is sort of visible but clearly very wrong.

MVP works differently:

Combine these with matrix multiplication and you’re left with one matrix that can be applied to each vertex on the GPU, to determine where on the screen it should fall. And that gives you a cube instead of a square! Want the cube to rotate? Multiply the model by a rotation matrix. This works for the camera too! What would have been a lot of confusing 3D calculations (at least for me), is now one 4-by-4 matrix that is multiplied rapidly in parallel.

This is the same video of the cube spinning.
(I’m pretty happy with it 👻)

And that’s all I know about cube-making! My next objectives are to render a more complicated 3D model by parsing a .obj file, and to add light-based shading.

Charles, etc