Computer Graphics
喒tfold University College
Welcome > Mathematics >3D-transform
../../gfx/prev.gif ../../gfx/home.gif ../../gfx/next.gif

Spatial transformations

[ Translation ][ Scaling ][ Rotation ][ Geometry ][ Compound operations ][ In OpenGL ][ Rows and columns ]

Purpose:

Understanding basic spatial transformations, and the relation between mathematics and geometric considerations.

This module mainly discusses the same subject as: 2D-transform, but has a coordinate system with three axes as a basis.

It is useful to agree of one way to draw the coordinate system in. In this material all reasoning in space is done in a right hand system.

right hand This means that if I put my right hand vertically down, like in karate, with my fingers along the positive x-axis, and bend the hand towards the y-axis, the thumb will point up along the positive z-axis.

We use homogeneous coordinates from the beginning. This means that the general transformation matrix is a 4x4 matrix, and that the general vector form is a column vector with four rows.

P2=M感1

|x2|   |m11 m12 m13 m14|   |x1|
|y2|   |m21 m22 m23 m24|   |y1|
|z2| = |m31 m32 m33 m34| * |z1|
|1 |   |m41 m42 m43 m44|   |1 |
	

Translation

|1 0 0 tx|
|0 1 0 ty|
|0 0 1 tz|
|0 0 0 1 |

A translation in space is described by tx, ty and tz. It is easy to see that this matrix realizes the equations:

	x2=x1+tx
	y2=y1+ty
	z2=z1+tz

Scaling

|sx 0 0 0|
|0 sy 0 0|
|0 0 sz 0|
|0 0  0 1|

Scaling in space is described by sx, sy and sz. We see that this matrix realizes the following equations:

	x2=x1新x
	y2=y1新y
	z2=z1新z
				

Rotation

Rotation is a bit more complicated. We define three different basic rotations, one around every axis.

axes

Around the Z-axis
|cos(v) -sin(v)  0   0|
|sin(v)  cos(v)  0   0|
|0         0     1   0|
|0         0     0   1|
Around the X-axis
|1    0        0    0|
|0  cos(v)  -sin(v) 0|
|0  sin(v)  cos(v)  0|
|0    0       0     1|
Around the Y-axis
|cos(v)  0  sin(v)  0|
|   0    1    0     0|
|-sin(v) 0  cos(v)  0|
|   0    0    0     1|

Geometry

The identity matrix

|1 0 0 0|
|0 1 0 0|
|0 0 1 0|
|0 0 0 1|

Again we can interpret it as:

  • Scale with 1 along all axes
  • Rotate with 0, around all axes
  • Translate with 0 in all axe directions

Mirroring

|1 0 0  0|
|0 1 0  0|
|0 0 -1 0|
|0 0 0  1|

We can mirror the different planes by using scaling factor -1 on the axis that is placed normally on the plane. Notice the matrix to the left. It mirrors around the xy-plane, and changes the coordinates from a right hand system to a left hand system.

Projection

|1 0 0 0|
|0 1 0 0|
|0 0 0 0|
|0 0 0 1|
So far we have only concentrated on moving points around in plane and space. Later we will worry about how we can depict space figures on a plane. It can be useful to notice that this can be done with a matrix operation. We can project a point orthogonical down into one of the main planes by using a matrix that scale the axis normally onto the plane with 0. The matrix to the left is a parallel projection down into the xy-plane.

Compound operations

The reasoning we used in the plane can be transferred to space. Again we use the fact that we can make compound transformation matrices that represent compound geometric transformations.

In the plane the situation is well arranged and it is easy to follow the operations graphically. In space this quickly gets complicated, especially when we rotate. A quite demanding exercise, which often is repeated in graphical literature, is rotating around an arbitrary axis. We will not go through the reasoning for this here. It is seldom we need to construct so complicated transformations in our head when we write code. Usually we get away with simpler solutions if we make some rational choices in the description of the objects we want to represent.

Let us use a simple example on rotation around an axis parallel to one of the main axes. We want to rotate the box on the figure 90 degrees around an axis that runs through P and is vertical on the xy-plane. The box has side edges of length 1. After the operation the point Q (2,4,4) should end up in Q2(1,3,4), and P(2,3,4) should remain in P2(2,3,4).

figur1

With our knowledge about transformations it should be a good strategy to:

  1. Move the point P in to the z-axis, the matrix T1
  2. Rotate around the z-axis, the matrix R
  3. Move the box back, the matrix T2

We remember from the chapter about 2D-transformations that we use the matrices in the opposite direction, and multiply from the left. We make the matrix M=T2愛愁1, and find Q'=M想 and P'=M感.

  |1 0 0 2| |0 -1 0 0| |1 0 0 -2| |0 -1 0 5|
M=|0 1 0 3|*|1  0 0 0|*|0 1 0 -3|=|1 0  0 1|
  |0 0 1 0| |0  0 1 0| |0 0 1  0| |0 0  1 0|
  |0 0 0 1| |0  0 0 1| |0 0 0  1| |0 0  0 1|


       |0 -1 0 5| |2| |1| 
Q2=M*Q=|1 0  0 1|*|4|=|3|
       |0 0  1 0| |4| |4|
       |0 0  0 1| |1| |1|


       |0 -1 0 5| |2| |2| 
P2=M*P=|1 0  0 1|*|3|=|3|
       |0 0  1 0| |4| |4|
       |0 0  0 1| |1| |1|

The figure or the coordinate system
To repeat in short: The reasoning above is based on the following:

We kept the idea about moving the figure. The order of the matrices is the opposite of the geometric logical order, of reasons that we discussed earlier for plane operations.

We can choose to reason in another way that goes better with the logic for the order of operations. It is the following:

  1. We move origin to the rotation axis, T2
  2. We rotate, R
  3. We move the origin back, T1

With this reasoning the order becomes: M=T2愛愁1 perhaps more logical.

In OpenGL

The functions in OpenGL:

  
  glLoadIdentity()
  glTanslatef(tx,ty,tz)
  glRotatef(v,x,y,z)
  glScalef(sx,sy,sz)
	

Notice that the f in the function names state that the parameters are floating numbers. There are integer number variants of the functions as well. Also notice that glRotate specify both rotation angle (in degrees) and rotation axis. A 30 degrees counterclockwise rotation around the z-axis looks like this:

  
  glRotatef(30.0f,0.0f,0.0f,1.0f)
	

Rows and columns

Again we can choose if we want to the operations like P2=M感, with P and P2 as column vectors or P2=P愚, with P and P2 as row vectors.

If we choose the last variant the respective base matrices become:

Translation
|1   0  0 0|
|0   1  0 0|
|0   0  1 0|
|tx ty tz 1|
Scaling
|sx 0 0  0|
|0 sy 0  0|
|0  0 sz 0|
|0  0 0  1|
Rotation around the z-axis
|cos  sin 0  0|
|-sin cos 0  0|
|0    0   1  0|
|0    0   0  1|
Rotation around the x-axis
|1  0    0   0|
|0 cos  sin  0|
|0 -sin cos  0|
|0  0    0   1|
Rotation around the y-axis
|cos  0  -sin 0|
|0    1  0    0|
|sin  0  cos  0|
|0    0   0   1|

A lot of graphical literature describes transformations in this way. You can also study transposition of matrices in the module: Algebra.

References and examples:

This material is described in all computer graphics teaching books, for example HearnBok chapter 5, FoleyBok chapter 5 or HillBok chapter 5.
See also general mathematics books about linear algebra.

Control and maintainance:

Revised June 2002, B鷨re Stenseth
Translated from Norwegian July 2004, Eirin 喒tvold Bl犘trud

Computer Graphics from 喒tfold College: http://www.ia.hiof.no/~borres/cgraph/

../../gfx/prev.gif ../../gfx/home.gif ../../gfx/next.gif

popup card
WXT : Apr 25, 2005
Valid XHTML