- Multimedia Programming with Pure Data
- Bryan WC Chung
- 584字
- 2021-08-13 16:53:25
Applying geometric transformation
In this section, we start to move the graphical shapes in the 3D space. The operations are translation, rotation, and scale. Each operation comes with two versions. They are: translate
, translateXYZ
, rotate
, rotateXYZ
, scale
, and scaleXYZ
. In this book, we focus on the use of translateXYZ
, rotateXYZ
, and scaleXYZ
. Readers can use the help menu to check the usage of translate
, rotate
, and scale
.
Performing translation, rotation, and scaling
Create an empty patch and save it with name gem005.pd
in your folder. Put the gemwin
object, create
and destroy
messages, and the toggle box for rendering. In addition, put a gemhead
object with a cube
3D object.
Between the gemhead
and the cube
objects, insert a translateXYZ
object. The translateXYZ
operation moves the object along each of the three axes. The three number boxes control the magnitude of the movement:
Note that the cube changes position and renders with a perspective view:
For rotation, insert the rotateXYZ
object to replace the translateXYZ
object in the last example. It also requires three number boxes for the rotations in three axes. The unit is in degree:
Here is the result of the cube with the rotation in three axes:
For scaling, use the object scaleXYZ
. It changes the size (scale) of the cube in the X, Y, and Z dimension. The number 1
is the original size:
Here is the cube with different scaling factors in the three axes:
It is easy to understand the operations of translateXYZ
, rotateXYZ
, and scaleXYZ
in GEM. By changing the values in the number boxes, you control the amount of transformation in the particular coordinates axes.
Checking the margins of the window
By using the translateXYZ
object, we can verify the margins of the window, as shown in the last section about the coordinates system. Note that when the cube
is translated to the x value of -4
or 4
, half of it will be outside of the frame:
Here are the two cubes translated to the left-hand and right-hand margins of the GEM window:
You will observe that when we translate a cube towards the margins of the window, it looks distorted due to the perspective projection. In some game applications, we may want to maintain the 3D shape no matter where it travels across the screen. In this case, you might consider using the ortho
(orthogonal) projection object. All graphical shapes under the ortho
object will use parallel projection instead of perspective projection:
Here is the resulting image (shown in the next screenshot) where you will notice the difference between the orthogonal and perspective projection:
Order of transformations
Another issue we would like to cover is the order of transformation. We have covered only one transformation for each graphical shape. In fact, we can have a number of transformations for each shape. The following example illustrates how the order of transformation matters in some cases. The first one has a translation before a rotation along the y axis. The cube translates to the position (1, 1, 0) in the 3D space and then rotates around its own y axis:
The second example has the same rotation along y axis first and then a translation to the same position (1, 1, 0). The cube rotates along the y axis of the window with a displacement from the center by one unit along the x axis and one unit along the y axis. By clicking-and-dragging the number box for the rotateXYZ
object, the result is more obvious: