A *rotation* transformation is the result of the orientation of
the initial space being different from the orientation of the destination space. The
basis vectors of the space do not change orientation relative to one another, but
relative to the destination coordinate system, they are pointed in different directions
than they were in their own coordinate system.

A rotation looks like this:

Rotations are usually considered the most complex of the basic transformations, primarily because of the math involved in computing the transformation matrix. Generally, rotations are looked at as an operation, such as rotating around a particular basis vector or some such. The prior part of the tutorial laid down some of the groundwork that will make this much simpler.

First, let's look back at our equation for determining what the position of a coordinate is relative to certain coordinate space:

$$X\left[\begin{array}{c}{A}_{x}\\ {A}_{y}\\ {A}_{z}\end{array}\right]+Y\left[\begin{array}{c}{B}_{x}\\ {B}_{y}\\ {B}_{z}\end{array}\right]+Z\left[\begin{array}{c}{C}_{x}\\ {C}_{y}\\ {C}_{z}\end{array}\right]+\left[\begin{array}{c}{O}_{x}\\ {O}_{y}\\ {O}_{z}\end{array}\right]$$

Does not this look a bit familiar? No? Maybe this look at vector-matrix multiplication will jog your memory:

$$\left[\begin{array}{cccc}\mathrm{m11}& \mathrm{m21}& \mathrm{m31}& \mathrm{m41}\\ \mathrm{m12}& \mathrm{m22}& \mathrm{m32}& \mathrm{m42}\\ \mathrm{m13}& \mathrm{m23}& \mathrm{m33}& \mathrm{m43}\\ \mathrm{m14}& \mathrm{m24}& \mathrm{m34}& \mathrm{m44}\end{array}\right]\left[\begin{array}{c}x\\ y\\ z\\ w\end{array}\right]=\left[\begin{array}{ccccccc}x*\mathrm{m11}& +& y*\mathrm{m21}& +& z*\mathrm{m31}& +& w*\mathrm{m41}\\ x*\mathrm{m12}& +& y*\mathrm{m22}& +& z*\mathrm{m32}& +& w*\mathrm{m42}\\ x*\mathrm{m13}& +& y*\mathrm{m23}& +& z*\mathrm{m33}& +& w*\mathrm{m43}\\ x*\mathrm{m14}& +& y*\mathrm{m24}& +& z*\mathrm{m34}& +& w*\mathrm{m44}\end{array}\right]$$

Still nothing? Perhaps an alternate look would help:

**Equation 6.5. Vectorized Matrix Multiplication**

$$\left[\begin{array}{cccc}\mathrm{m11}& \mathrm{m21}& \mathrm{m31}& \mathrm{m41}\\ \mathrm{m12}& \mathrm{m22}& \mathrm{m32}& \mathrm{m42}\\ \mathrm{m13}& \mathrm{m23}& \mathrm{m33}& \mathrm{m43}\\ \mathrm{m14}& \mathrm{m24}& \mathrm{m34}& \mathrm{m44}\end{array}\right]\left[\begin{array}{c}x\\ y\\ z\\ w\end{array}\right]=x\left[\begin{array}{c}\mathrm{m11}\\ \mathrm{m12}\\ \mathrm{m13}\\ \mathrm{m14}\end{array}\right]+y\left[\begin{array}{c}\mathrm{m21}\\ \mathrm{m22}\\ \mathrm{m23}\\ \mathrm{m24}\end{array}\right]+z\left[\begin{array}{c}\mathrm{m31}\\ \mathrm{m32}\\ \mathrm{m33}\\ \mathrm{m34}\end{array}\right]+w\left[\begin{array}{c}\mathrm{m41}\\ \mathrm{m42}\\ \mathrm{m43}\\ \mathrm{m44}\end{array}\right]$$

Does it look familiar *now*?

What this tells us is that the columns of our transformation matrices are, and have
*always* been, nothing more than the axes of a coordinate system.
Except for the fourth column; because the input position has a 1 in the W, it acts as an
offset.

Transformation from one space to another ultimately means this: taking the basis
vectors and origin point from the original coordinate system and re-expressing them
relative to the destination coordinate system. The transformation matrix from one space
to another contains the basis vectors and origin of the original coordinate system, but
the *values* of those basis vectors and origin are relative to the
destination coordinate system.

Earlier, we said that numerical coordinates of a space must be expressed relative to another space. A matrix is a numerical representation of a coordinate system, and its values are expressed in the destination coordinate system. Therefore, a transformation matrix takes values in one coordinate system and transforms them into another. It does this by taking the basis vectors and origin of the input coordinate system and represents them relative to the output space. To put it another way, the transformation from space A to space B is what space A looks like from an observer in space B.

A rotation matrix is just a transform that expresses the basis vectors of the input space in a different orientation. The length of the basis vectors will be the same, and the origin will not change. Also, the angle between the basis vectors will not change. All that changes is the relative direction of all of the basis vectors.

Therefore, a rotation matrix is not really a “rotation” matrix; it is an
*orientation* matrix. It defines the orientation of one space
relative to another space. Remember this, and you will avoid many pitfalls when you
start dealing with more complex transformations.

For any two spaces, the orientation transformation between them can be expressed as rotating the source space by some angle around a particular axis (specified in the initial space). This is true for any change of orientation.

A common rotation question is to therefore compute a rotation around an arbitrary axis. Or to put it more correctly, to determine the orientation of a space if it is rotated around an arbitrary axis. The axis of rotation is expressed in terms of the initial space. In 2D, there is only one axis that can be rotated around and still remain within that 2D plane: the Z-axis.

In 3D, there are many possible axes of rotation. It does not have to be one of the initial space's basis axes; it can be any arbitrary direction. Of course, the problem is made much simpler if one rotates only around the primary axes.

Deriving these matrix equations is beyond the scope of this tutorial; so instead, we will simply provide them. To perform rotations along the primary axes, use the following matrices:

**Equation 6.6. Axial Rotation Matrices**

$$\begin{array}{l}\text{Rotation Angle}=\theta \\ \begin{array}{cc}\text{X Rotation}& \left[\begin{array}{cccc}1& 0& 0& 0\\ 0& \mathrm{cos}\left(\theta \right)& -\mathrm{sin}\left(\theta \right)& 0\\ 0& \mathrm{sin}\left(\theta \right)& \mathrm{cos}\left(\theta \right)& 0\\ 0& 0& 0& 1\end{array}\right]\\ \text{Y Rotation}& \left[\begin{array}{cccc}\mathrm{cos}\left(\theta \right)& 0& \mathrm{sin}\left(\theta \right)& 0\\ 0& 1& 0& 0\\ -\mathrm{sin}\left(\theta \right)& 0& \mathrm{cos}\left(\theta \right)& 0\\ 0& 0& 0& 1\end{array}\right]\\ \text{Z Rotation}& \left[\begin{array}{cccc}\mathrm{cos}\left(\theta \right)& -\mathrm{sin}\left(\theta \right)& 0& 0\\ \mathrm{sin}\left(\theta \right)& \mathrm{cos}\left(\theta \right)& 0& 0\\ 0& 0& 1& 0\\ 0& 0& 0& 1\end{array}\right]\end{array}\end{array}$$

When using the standard C/C++ library `sin`

and
`cos`

functions, the angles must be in radians.

As useful as these are, the more generic equation for rotation by an angle about an arbitrary axis is as follows.

**Equation 6.7. Angle/Axis Rotation Matrix**

$$\begin{array}{c}\begin{array}{ll}\text{Axis}=\left(x,y,z\right)& \text{Angle}=\theta \\ C=\mathrm{cos}\left(\theta \right)& S=\mathrm{sin}\left(\theta \right)\\ \mathrm{iC}=1-\mathrm{cos}\left(\theta \right)\end{array}\\ \left[\begin{array}{cccc}{x}^{2}+\left(1-{x}^{2}\right)C& \mathrm{iC}xy-zS& \mathrm{iC}xz+yS& 0\\ \mathrm{iC}xy+zS& {y}^{2}+\left(1-{y}^{2}\right)C& \mathrm{iC}yz-xS& 0\\ \mathrm{iC}xz-yS& \mathrm{iC}yz+xS& {z}^{2}+\left(1-{z}^{2}\right)C& 0\\ 0& 0& 0& 1\end{array}\right]\end{array}$$

All of these matrices are such that, from the point of view of an observer looking down the axis of rotation (the positive direction of the axis is pointed into the eye of the observer), the object rotates counter-clockwise with positive angles.

The Rotations tutorial shows off each of these rotation matrix functions. Similar to how the others work, there are multiple instances rendered based on functions.

The function that builds the transformation matrix looks like this:

**Example 6.4. Rotation Transformation Building**

glm::mat4 ConstructMatrix(float fElapsedTime) { const glm::mat3 &rotMatrix = CalcRotation(fElapsedTime); glm::mat4 theMat(rotMatrix); theMat[3] = glm::vec4(offset, 1.0f); return theMat; }

The constructor of glm::mat4 that takes a glm::mat3 generates a 4x4 matrix with the 3x3 matrix in the top-left corner, and all other positions 0 except the bottom-left corner, which is set to 1. As with much of GLM, this works in GLSL as well.