Feb
1
GM6 D3D from the Ground Up - Chapter 4
2007 @ 10:41 PMChapter 4 - Geometric Transformations
We've learned how to draw all kinds of stuff and start to compose a nice populated scene. But there is an integral part of scene elements that we have not discussed - positioning and orientation.
There is an important concept you must first understand though. Rather than actually moving the vertices, we will move the whole
coordinate system. Think of it like this - when you are stopped beside another car at a traffic light and the other car starts to move forward, you get the impression that your car is moving backwards. This is the same relation. It's easier to describe your object centered around the origin and then move the coordinate system.
Types of Transformations
Translations
A
translation is basically sliding the object. You specify an axis and it will slide along that axis for however many units you specify.
Rotations
A
rotation is simply rotating the object. You specify an axis and it will rotate around that axis using the angle you specify.
Scaling
Scaling is simply stretching or shrinking the object. The dimensions of the object is either increased or decreased by the amount you specify.
Understanding Transformations
Transformations occur between the time that you specify vertices and when they appear squished onto the 2D screen.
An important thing to remember about translations is that they are cumulative.
Matrices
Transformations are all handled in Direct3D by matrices internally. Again, learning matrices is beyond the scope of this tutorial and not completely necessary to use Direct3D. But it does give you a better understand of what is happening inside Direct3D.
Rotation and Scaling
In Direct3D, rotation and scaling are both done in respect to the origin of the world, not of the object's position. So to combat this, we must translate the object to the world origin and perform the scaling and rotation manipulations. Then we translate it back to its old position.
The Identity Transformation
Since all transformations are cumulative, we can't expect to transform more than one object without the previous objects' transformations affecting the new object. To fix this, we need to reset the origin. You can do this by using the function
d3d_transform_set_identity();.
Using Transformations
Let's build upon our cone solid object that was built in the last chapter.
...
colorChange = 0;
d3d_transform_set_identity();
d3d_transform_add_rotation_x(45);
d3d_transform_add_scaling(1.5,0,0.8);
d3d_primitive_begin(pr_trianglefan);
...
d3d_primitive_end(); //Last line
d3d_transform_set_identity();
The first transformation we set is the identity to reset the origin so that we can execute the transformations correctly. Next, the object is rotated 45 degrees around the x-axis with a call to
d3d_transform_add_rotation_x(angle);. Functions that also exist for this purpose include
d3d_transform_add_rotation_y(angle); and
d3d_transform_add_rotation_z(angle);. You could also use
d3d_transform_add_rotation_axis(xa,ya,za,angle);. xa, ya, and za form a vector identifying which axis to rotate it around. For example, if you wanted to rotate around the y-axis by 35 degrees, you would call
d3d_transform_add_rotation_axis(0,1,0,35);.
The next transformation that was done was a scaling. We used the function
d3d_transform_add_scaling(xs,ys,zs);. Again, xs, yz, and zs form a vector indicating how much to scale each axes' dimensions by. Values greater than 1 specify a stretch. Values less than 1 specify a shrink. For example, if we wanted to stretch the object on the x-axis by 150%, shrink the object on the y-axis by 85% and leave the object alone on the z-axis, we would call
d3d_transform_add_scaling(1.5,0.8,1);.
The last transformation, called at the end of the script, resets the origin. We do this so as to not affect any other things that may be drawn apart from this object.
The Transformation Stack
It is sometimes convenient to save the current cumulative transformation state, execute a new transformation, and then restore the old transformation state for further transformations. For example, if you were building a robot. You just translated the torso and orientated it. You save the transformation state. You then translate and rotate one leg, restore the previous transformation state, and add the other leg. If you didn't do this, you would have had to translate and rotate back to the torso via extraneous transformations. This has greater overhead than simply using our transformation stack.
The transformation stack works just like the stack data structure in other areas of programming. You can clear the whole stack's contents by calling
d3d_transform_stack_clear();. You can
push(add to) the stack by calling
d3d_transform_stack_push();. You can then
pop(remove from) the stack with
d3d_transform_stack_pop();.
We will see some examples later in the series that make efficient use of this powerful mechanism.
[Thanks to Yourself for notifying me of some errata]
<< Previous Chapter Next Chapter >>
Need to buy cheap runescape gold,runescape money,runescape gp,RS2 Item?We service cheap runescape gold,runescape money,RS2 Item at all server.Need buy runescape gold, runescape money, runescape gp? We sell runescape gold, Runescape money, runescape gp, 24/7 online support and fast delivery.
buy runescape money,runescape gp,rs money,buy runescape gold,rs money.