**Build a Simple 3D Pipeline in Tcl**

Pages: 1, **2**, 3

### Rotation About an Arbitrary Axis

This is the stuff lengthy, boring, game tutorial web pages are made of. I'll spare you and just point out where we're going to perform the linear algebra transformations to display the polygon on our Tcl game console.

*Figure 2. Arbitrary rotation matrix*

Figure 2. shows us our arbitrary rotation matrix. If you just want to rotate your polygon around the x axis, you adjust the alpha angle value, in radians. If you want to perform a rotation about the x, y, and z axes, then you must input the alpha, theta, and phi angle values in radians. The arbitrary rotation matrix is a linear algebra concatenation of all three transformation matrices into one universal matrix.

```
proc create_rotation_matrix_R { alpha theta
phi } {
# calculate the trig identities
set sin_alpha [expr {sin($alpha)}]
set cos_alpha [expr {cos($alpha)}]
set sin_theta [expr {sin($theta)}]
set cos_theta [expr {cos($theta)}]
set sin_phi [expr {sin($phi)}]
set cos_phi [expr {cos($phi)}]
# Eq 3.2 Rx, Real-Time Rendering, Moller & Haines
set Rx { \
{1 0 0 0} \
{0 0 0 0} \
{0 0 0 0} \
{0 0 0 1}\
}
# set the Rx matrix components
lset Rx 1 1 $cos_alpha
lset Rx 1 2 $sin_alpha
lset Rx 2 1 [expr {-1*$sin_alpha}]
lset Rx 2 2 $cos_alpha
# Eq 3.3 Rx, Real-Time Rendering, Moller & Haines
set Ry { \
{0 0 0 0} \
{0 1 0 0} \
{0 0 0 0} \
{0 0 0 1} \
}
lset Ry 0 0 $cos_theta
lset Ry 0 2 [expr {-1*$sin_theta}]
lset Ry 2 0 $sin_theta
lset Ry 2 2 $cos_theta
# Eq 3.4 Rx, Real-Time Rendering, Moller & Haines
set Rz { \
{0 0 0 0} \
{0 0 0 0} \
{0 0 1 0} \
{0 0 0 1} \
}
lset Rz 0 0 $cos_phi
lset Rz 0 1 $sin_phi
lset Rz 1 0 [expr {-1*$sin_phi}]
lset Rz 1 1 $cos_phi
return [matrix_multiply [matrix_multiply $Rx $Ry] $Rz]
}
```

You'll notice in the comments I mention the *Real-Time Rendering* text, by
Moller and Haines. This was my source of information for the linear algebra
equations. As a side note, this is a great book; the second edition is available for
over $60. I picked up my first edition off of eBay for $0.99.

The arbitrary rotation matrix procedure, `create_rotation_matrix_R,`

calls
the procedure `matrix_multiply`

. This is a straightforward matrix multiply that is
covered in all high-school algebra 2 and college linear algebra text books.