Since it is very common to render 3D objects in practice, this time we are looking at the basics of rendering 3D objects with illumination effects.

Transformation matrices

As it is shown below, there are three matrices that determine how 3D objects are presented on the screen. In order to represnet all three-dimensional transformations with a single matrix, homogeneous coordinates are used. Usually model matrices and view matrices are linear transformations (that is, combinations of translation, rotation and scale), and projection matrices are either orthograpic or perspective.

Transformation matrices (source: learnopengl)

Since transformation matrices are often used, we can write a common library for transformations (which is named as gl_lib/ in the source folder).


The figure above shows that the coordinates are transformed from world space to view space based on the camera, which is determined by how the user is trying to view the scene. I choose to implement a simple first-person-shooter-like camera. In this case, there are two parameters controlling the view direction, namely pitch and yaw. These two parameters corresponding to the two degrees of freedom introduced by the mouse.

Euler angles (source: learnopengl)

Under this setting, the front (viewing) direction is given by

Since it is conventional to set the default front direction to be towards the negated axis, the initial settings would be . We know that by pressing “a” or “d” in an FPS game, the character would move left or right. In order to do that, we also need to compute the right direction. Because the roll angle does not change, we can always assume that the global up direction is given by the unit vector. Therefore, the right direction is given by

where denotes cross product. It is also obvious that the up direction is given by

The implementation the camera can be seen in gl_lib/


Shading refers to depicting depth perception in 3D models or illustrations by varying levels of darkness. One of the shading models that are widely used is Phong shading. Different shading methods can change the style of output image drastically, other shading models include anisotropic shading, toon shading and so on.

Phong shading

In this section, angle brackets are used to denote dot product; round dots are used to denote scalar-scalar/scalr-matrix product; round circles are used to denote Hadamard product.

Phong shading

In Phong shading, the final result is the combination of three components: ambient, diffuse and specular. There are several parameters in this model1:

  • : the light color
  • : the object color
  • : ambient strength
  • : specular strength
  • : surface normal vector
  • : light direction vector

    Although the light shines onto objects to make them visible, we always assume that the light direction vector is pointing out from the object surface for the simplicity of calculation. Therefore, if the light source is point light, this vector is given by the normalized difference between the light position and the fragment position. If the light source is directional light, this vector is given by the inverted direction of the light.

  • : view direction vector

    This is given by the normalized difference between the eye position and the fragment position.

  • : reflection direction vector

    Similar to the convention of light direction vectors, the reflection direction vectors are also point out from the object surface. By the law of reflection, it can be seen that

  • : specular hightlight effect parameter

    The choice of different values results in distinct specular highlight effects. Usually is an integer that is greater than 0.

The ambient color is given by

The diffuse color is given by

The specular color is given by

Finally, the fragment color is given by

Anisotropic shading

A widely used anisotropic model is the Heidrich–Seidel2 anisotropic distribution. Reusing all the notations in Phong shading, Heidrich–Seidel also introduces the following parameters:

  • : thread direction
  • : thread direction based on surface normal

    It is given by

The diffuse color is given by

The specular color is given by

The fragment color can be computed in a similar manner.

The scene under anisotropic shading

Putting everything together

The program of this tutorial can be found in the GitHub repository.

Simple introduction:

  • Use “wasd” and the mouse to move around the scene
  • Press ESC to exit
  • Press “o” to toggle anisotropic shading
  • Press “p” to take a screenshot


  1. The colors are represented as three dimensional RGB vectors where each component takes on values from 0 to 1. All the directional vectors are normalized. The strength parameters are usually within the range

  2. Heidrich, Wolfgang and Hans-Peter Seidel. “Efficient Rendering of Anisotropic Surfaces Using Computer Graphics Hardware.” (1998).