Where translation moves a point by adding to it, scaling moves it by multiplication. When applied to an object centered at the origin, this transformation scales all points on the object, effectively making it larger (if the scale value is greater than 1) or smaller (if the scale value is less than 1), as shown in the figure.
You’ll need a new function, called scaling(x,y,z), that returns a 4x4 translation matrix. Add the following test to demonstrate how it’s used to scale a point.
| Scenario: A scaling matrix applied to a point |
| Given transform ← scaling(2, 3, 4) |
| And p ← point(-4, 6, 8) |
| Then transform * p = point(-8, 18, 32) |
Now, unlike translation, scaling applies to vectors as well, changing their length. Add the following test to show how vectors are affected by scaling.
| Scenario: A scaling matrix applied to a vector |
| Given transform ← scaling(2, 3, 4) |
| And v ← vector(-4, 6, 8) |
| Then transform * v = vector(-8, 18, 32) |
And as you might expect, multiplying a tuple by the inverse of a scaling matrix will scale the tuple in the opposite way (shrinking instead of growing, or vice versa). Add the following test to show that this is so.
| Scenario: Multiplying by the inverse of a scaling matrix |
| Given transform ← scaling(2, 3, 4) |
| And inv ← inverse(transform) |
| And v ← vector(-4, 6, 8) |
| Then inv * v = vector(-2, 2, 2) |
To construct a scaling matrix, take an identity matrix t and change the values at t00, t11, and t22 to be (respectively) the x, y, and z scaling values.
While we’re on the subject of scaling, let’s take a moment and discuss its near cousin: reflection. Reflection is a transformation that takes a point and reflects it—moving it to the other side of an axis. It can be useful when you have an object in your scene that you want to flip (or mirror) in some direction. Maybe the model is leaning the wrong way, facing the wrong direction. Maybe it’s a face that’s looking to the right when you want it looking to the left. Rather than breaking out a 3D modeler and editing the model, you can simply reflect the model across the appropriate axis.
Reflection is essentially the same thing as scaling by a negative value. Implement the following test, which shows how a point can be reflected across the x axis by scaling the x component by -1.
| Scenario: Reflection is scaling by a negative value |
| Given transform ← scaling(-1, 1, 1) |
| And p ← point(2, 3, 4) |
| Then transform * p = point(-2, 3, 4) |
Just like that, the point was moved from the positive side of the x axis, to the negative.
Make your tests pass, and then let’s move on to rotation.
3.12.161.77