Unit 5 Three Dimensional Transformations: Structure Page No
Unit 5 Three Dimensional Transformations: Structure Page No
TRANSFORMATIONS
Structure Page No
5.1 Introduction 89
Objectives
5.2 3D Primitive and Composite Transformations 89
5.3 Three-Dimensional Viewing 93
5.4 Projections 96
5.5 View Volumes and General Projection Transformations 99
5.6 Summary 102
5.7 Solutions/Answers 104
5.8 Practical Exercises 109
5.1 INTRODUCTION
A 3D geometric transformation is used extensively in object modelling and
rendering. 2D transformations are naturally extended to 3D situations in most
cases. Except for rotation, where you need to specify the axis of rotation, you
actually can easily extend all other geometric transformations.
Objectives
After reading this unit you should be able to
• explain basic 3D transformations–translation, rotation, scaling, shear and
reflections–applied to objects in space;
• explain how to transform between two coordinate systems;
• explain how to apply basic geometric transformations on objects;
• explain the concepts used in rendering and projecting a 3D scene onto a
2D display;
• build model view matrix capturing the scene from camera position;
• implement projection transformations;
• distinguish between parallel and perspective projections.
Read Secs 11-1 to 11-7, Chapter 11, pages 428-449 of the book.
You must have noticed here that the translation is as simple as in 2D. Rotation
however requires a little more effort in 3D. Standard rotations about three
coordinate axes are simpler to perform. In order to apply rotation about an
arbitrary axis, you need to have a composite transformation while scaling,
shear and reflections are generalized to 3D in a natural way.
(Remember that the value of cos α can only provide information about the
magnitude of sin α if you use the trigonometric formula sin α = ± 1 − cos 2 α .
Hence you need to use the above computation to know exactly sin α .)
Therefore
1 0 0 0
0 1 / 2 − 1 / 2 0
R x (α ) =
0 1 / 2 1 / 2 0
0 0 0 1
Further,
1
u ′′ = (1, 0, 2 )
3
⋅
u ′′ u
z = 2 , sin β = u ′′ × u = − 1
cos β = z
′′
|u ||u | 3 3
z
Thus
2 / 3 0 − 1 / 3 0
0 1 0 0
R y (β) =
1/ 3 0 2 / 3 0
0 0 0 1
90
So, the object will be rotated with its centre P now on the position P ′ with P′ Three Dimensional
Transformations
given by the following concatenation of the matrices.
To explain you this sequence order, we begin with the rightmost matrix
transformation. Since the axis of rotation is specified with a direction vector
that passes through (1, 0, 0), you need to translate it by the vector ( −1, 0, 0) to
make the axis of rotation pass through the origin (T ( −1, 0, 0)) . Then to let the
rotation vector lie on the xz-plane, you have rotated the vector u about x-axis
through an angle α( R x (α)) . Again to align the vector with the z-axis, you
have rotated it about y axis with an angle β(R y (β)) . Finally, you have rotated
the object with centre at P about z-axis (which is also the axis of rotation now)
by an angle of 45°. Then apply the inverse transformation one by one in the
reverse order to bring the scene back in its original position and orientation.
***
Solution: The axis is parallel to y axis. All that you need to do is to translate
the axis so that it passes through the origin, apply rotation about y axis and then
translate the axis back to the original position. The sequence of matrix
transformations is given by
1 0 0 1 cos θ 0 sin θ 0 1 0 0 − 1
0 1 0 0 0 1 0 0 0 1 0 0
R=
0 0 1 1 − sin θ 0 cos θ 0 0 0 1 − 1
0 0 0 1 0 0 0 1 0 0 0 1
***
Example 3: Scale a sphere centered on the point (1, 2, 3) with radius 1, so that
the new sphere has the same centre with radius 2.
Solution: Translate the sphere so that its centre is at the origin. Scale
uniformly in all the coordinates by a factor of 2, and then translate the sphere
back to its original position. The transformation will be given by taking the
concatenation of following matrices.
1 0 0 1 2 0 0 0 1 0 0 − 1
0 1 0 2 0 2 0 0 0 1 0 − 2
R=
0 0 1 3 0 0 2 0 0 0 1 − 3
0 0 0 1 0 0 0 1 0 0 0 1
***
91
Computer Graphics You may now try the following exercises.
E1) Transform the vector (1, 1, 1) so that it aligns with the vector
(2, 0, − 1) .
E2) A scene is modelled in 3D. Write the sequence of matrix that will scale
the scene by a factor of 2 in y direction while leaving the position of
( 2, − 1, 1) unchanged.
You may also try the following exercises from the book.
Do the exercises no. 11-1, 11-4, 11-9, Chapter 11 on page 450 of the book.
92
1 2 3 4 Three Dimensional
Transformations
2 3 4 1
A=
3 4 1 2
0 0 0 1
[1234234134120001]
The same matrix viewed in column major order will be accessed as follows –
[1230234034104121]
Essentially this means, you have to be careful while applying OpenGL matrix
transformation. For example, if you call the following functions in the order
given below -
glScaled(1.0, 1.0, 1.0); // S
Read Secs. 12-1 to 12-2, Chapter 12, pages 452-458 of the book.
Solution: Refer to page 458 of the book. Here you have been given
N = ( −1, − 1, − 1) and V = (0, 1, 0) . Therefore you can construct rows of the
orthogonal matrix as follows.
−1
(0, 1, 0) × (1, 1, 1)
−1 3 1 −1
n= (1, 1, 1), u = = ( −1, 0, 1), v = n × u = (1, − 2, 1) .
3 −1 2 6
(0, 1, 0) × (1, 1, 1)
3
−1 1
2 0 0
2 1 0 0 − 2
−1 − 2 −1 0 1 0 − 2
0
6 3 6
−1 0 0 1 − 2
−1 −1
0
3 3 3 0 0 0 1
0 0 0 1
***
E4) Your camera is located at (2, 3, 3) in the world coordinate system. You
are looking at (0, 0, 0) . How will you transform the scene from the world
coordinate system to viewing coordinate system?
• glMatrixMode(GL_MODELVIEW );
Specifies that we require space for a 4×4 matrix to be used for
setting up the view of the model and in common with all the
derived matrices. The function glMatrixMode( ) is used for
setting up both viewing- modellling and projection. When you
wish to set up the modelling and viewing transformation, use the
symbolic constant GL_MODELVIEW.
GL_MODELVIEW matrix combines viewing matrix and
modelling matrix into one matrix.
The matrix mode is one of OpenGL states it remembers.
Basically it means that all subsequent operations will be
performed on whatever matrix you specify in glMatrixMode( )
until you change it to a different mode. So if you specify the
modelling and viewing matrix GL_MODELVIEW, it will
perform all subsequent matrix operations treating this as the
initial matrix. In case of GL_MODELVIEW the matrix specifies
the following:
x-axis vector
y-axis vector
z-axis vector
The x-axis, y-axis ad z-axis vectors are actually view left vector,
view up vector and view forward vectors.
• glLoadIdentity( );.
Initializes the current matrix as the identity matrix.
Hence in case of GL_MODELVIEW matrix, the view left
vector is set to (1, 0, 0), view up vector to (0, 1, 0) and view
forward vector is set to ( 0, 0, 1).
Now that you have studied how to transform from world coordinate system to
viewing coordinate system, you would like to know how to project the scene
from three dimensional world to two-dimensional display. Standard projection
transformations help you achieve this. In the next section, you will learn
about projection transformations.
5.4 PROJECTIONS
When all display devices are 2D, you need to devise methods that give a
realistic view of a 3D scene onto 2D display. With more and more devices
coming in the market with high definition resolution and many advanced
features, it becomes even more relevant to find out ways in which you can
effectively project your scene on a 2D device. A camera uses a transformation
method that captures a 3D scene in a 2D film or screen. The principle that a
camera uses is essentially the same as that used in a human eye system. Such
transformations that transform 3D scenes to 2D view plane are called
projections. More precisely a point P = ( x , y, z) in the 3D world is
transformed to a point P ′ = ( x ′, y ′, z ′) that lies on the projection plane or the
view plane as shown in Fig. 1.
y
P = ( x, y, z)
P ′ = ( x ′, y ′, z ′)
z Projection
Fig. 1.
96
The taxonomy of projections is as shown in Fig. 2. Three Dimensional
Transformations
Projection
Parallel Perspective
Isometric
Fig. 2.
To know about projections, their types and applications in detail you may read
the following section of the book.
Example 5: Imagine a square with vertices (0, 0, 1), (1, 0, 1), (1, 0, 2) and
(0, 0, 2) . Find out its image on the view plane aligned with the xy-plane under
following projections (i) Orthographic parallel projection (ii) Oblique parallel
projection with projection direction vector (1, 1, − 1) .
(i) orthographic projection – (0, 0), (1, 0), (1, 0), (0, 0) .
(ii) oblique parallel projection –
1 1 1 1
( , ), (1 + , ), (1 + 2 , 2 ), ( 2 , 2) .
2 2 2 2
Try drawing the result on the paper. Notice that while the shape of square gets
deformed with oblique parallel projection, in orthographic projection, it
reduces to a straight line segment.
***
Example 6: Let origin be the centre of projection. Find out the perspective
projection when the projection plane passes through the point P (2, 3, − 1) and
has normal vector ( −1, 1, − 1) .
97
Computer Graphics Solution: Refer Fig. 3 below.
y
P = ( x, y, z)
P ′ = ( x ′, y′, z ′)
O x
Projection plane
z
Fig. 3.
***
E7) Draw an approximate (i) front view (ii) top view and (iii) view from left,
of the objects shown in Fig. 4. ( Hint: Remember that the terms top
view, front view and view from left are normally used in case of parallel
projections only.)
(a) (b)
Fig. 4.
98
When you watch outside a window, you can view more if you are closer Three Dimensional
Transformations
to the window (wide angle view) as compared to the visible volume when
you are away from window. This is equivalent to saying that you are able
to capture more volume of the scene when you have a wide angle lens in
your camera. A view volume which we shall discuss in the next section is
the spatial extent that is visible through a window in the view plane.
Given the view window specification, you can set up a view volume
using the window boundary. The view volume is dependent on the
window shape and size and the projection specifications.
You have to remember that the view volume is the volume which sets up near
and far extents, top and bottom extents and left and right extents. These extents
are chosen so that you can eliminate those objects from further processing that
are not visible within the view volume. Any object with its defining boundaries
beyond these extents will be immediately removed from the display list. For
example, in a scene, there could be some very complex objects that are very far
and do not contribute much to the scene. In the absence of a far extent plane
such objects will remain in the display list and will consume lot of processing
time. With these view extents well defined in the beginning, performance of
display processing will be much improved.
View volume depends on the type of projection and the shape and size of the
window. OpenGL provides following functions for performing parallel and
perspective projections.
Here left, right define the x-direction extents, bottom, top define y-direction
extents and near, far define the z-direction extents of the view volume.
gluOrtho2D is exactly like glOrtho, but with near = −1 and far = 1. That is,
gluOrtho2D views points that have z-value between −1 and 1. Usually,
gluOrtho2D is used when drawing two-dimensional figures that lie in the
xy-plane, with z = 0 .
99
Computer Graphics The quantities are as specified in Fig. 5.
z = −n (l, b, − n )
Fig. 5.
Following simple program will give you an idea about how to use the forgoing
functions in a C program for displaying images of 3D objects. The program
displays a cube using orthographic projection.
#include <GL/glut.h>
GLint w=500,h=500;
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
// Setup the modelview matrix
glMatrixMode(GL_MODELVIEW);
// initialize your modelview matrix by identity
glLoadIdentity();
gluLookAt(0,0,0,5.0, 5.0,5.0,0.0,1.0,0.0);
// Setup the projection matrix
glMatrixMode(GL_PROJECTION);
//Initialize the projection matrix by identity
glLoadIdentity();
// Use orthogonal parallel projection
glOrtho(-5.0,5.0,-5.0,5.0,5.0,-5.0);
// Hide backfaces from the view point
glEnable(GL_CULL_FACE);
//Material variabels declaration
//Setting Specular reflection
GLfloat mat_specular[] = {0.0, 0.0, 1.0, 1.0};
//Material's shine factor
GLfloat mat_shininess[] = {50.0};
100
//Light source variables declaration Three Dimensional
//Position of light source 0 Transformations
GLfloat light_position0[] = { 5.0, 5.0, 5.0, 3.0 };
// Light color
GLfloat light_color[] ={1.0,1.0,1.0,1.0};
//Shading to be applied as flat shading
glShadeModel(GL_FLAT);
//Material Specular reflection settings
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
//Material shine-ness setting
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//Light 0 position setting
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
glEnable(GL_LIGHTING);//Power on
glEnable(GL_LIGHT0); //Light switch on
glEnable(GL_DEPTH_TEST);
glFlush();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
typedef GLint vertex[3];
typedef GLfloat color[3];
color c[8] =
{{1,0,0},{1,1,0},{0,1,0},{0,1,1},{0,0,1},{1,0,1},{1,0,0},{
1,0,0}};
vertex v[8] = {{0,0,0},{0,1,0},{1,0,0},{1,1,0},{0,0,1},
{0,1,1}, {1,0,1}, {1,1,1}};
glFrontFace(GL_CW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_INT, 0, v);
glColorPointer(3, GL_FLOAT, 0, c);
GLubyte vertexIndex[] = { 6,2,3,7, 7,3,1,5, 5,1,0,4,
4,0,2,6,2,0,1,3,7,5,4,6};
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,vertexIndex);
glutSwapBuffers();
}
void main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowPosition(50,50);
glutInitWindowSize(w,h);
glViewport(0,0,w,h);
glutCreateWindow("3D View");
101
Computer Graphics init();
glutDisplayFunc(display);
glutReshapeFunc(reShape);
glutMainLoop();
glDisable(GL_CULL_FACE);
}
Fig. 6.
A 3D program requires much more than these settings. For more information
on 3D graphics programming you may refer the coding resources and tutorials
available on the offical website www.opengl.org of OpenGL.
E8) You have a scene specified in the world coordinate system. The scene has
a cube with base square on the xz-plane having vertices (1, 0, 1),
( 2, 0, 1), (2, 0, 2) and (1, 0, 2) and top square having vertices (1, 1,1),
( 2,1,1), ( 2, 1, 2) and (1, 1, 2) . The look up vector is (0, 1, 0) and the
viewpoint is at (0, 0, 5) . Find out and draw image of the cube in a view
plane placed at the xy-plane of the world coordinate system. Also, find
out how many vanishing points will be there in the projected scene and
determine the vanishing point(s).
E9) Draw a projected cube with (i) 1-point perspective image (ii) 2-point
perspective image (iii) 3-point perspective image.
E10) State whether the following statements are true or false. Give
reasons/examples to justify your answer.
a) Cavalier projection is a parallel projection.
b) Angles are not preserved in perspective projection in general.
c) Perspective projection is an affine transformation.
d) There can be more than one vanishing point in a projected image but
only one principal vanishing point in a projected image.
We now end the unit by giving a summary of what we have covered in it.
5.6 SUMMARY
In this unit, we have covered the following:
6) Viewing coordinates are the coordinates with reference to the eyes of the
viewer or the coordinates with reference to camera position and
orientation.
10) There are two types of projections – parallel projection and perspective
projection.
11) In parallel projection, objects in scene are projected onto the 2D view
plane along rays parallel to a projection vector.
13) Perspective projection gives more realistic appearance and uses the same
principle as used in camera.
5.7 SOLUTIONS/ANSWERS
Exercises given on page 92 of the unit
E1) Hint: Apply the transformations to align (1, 1,1) with a coordinate axis,
say z-axis. Then apply transformation to reorient the z-axis unit vector so
that it aligns with (2, 0, − 1) .
104
1 0 0 − 2 1 0 0 0 1 0 0 2 Three Dimensional
Transformations
0 1 0 1 0 2 0 0 0 1 0 − 1
C=
0 0 1 − 1 0 0 1 0 0 0 1 1
0 0 0 1 0 0 0 1 0 0 0 1
E3) Axis of rotation is in the xy-plane. Rotate the axis so that it aligns with
one of the coordinate axes (say x-axis). The required rotation in this case
will be about z-axis by an angle of − 45 . Now apply rotation by the
specified angle − θ , since it is given that the clockwise rotation is
positive when one is looking towards the origin in the direction opposite
to the vector. Then apply the inverse rotation (rotation by 45 ) so that the
axis is back to its original position. Following sequence of
transformations will be applied.
1 1 1 1
− 0 0 1 0 0 0 2 0 0
2 2 2
1 1 0 cos θ − sin θ 0
C = 0 0 − 1 1
0 0
2 2 0 sin θ cos θ 0 2 2
0 0 1 0 0 0 1 0
0 0 0 1
0 0 0 1 0 0 0 1
11-1. The solution is similar to the solution of Exercise 5-6 on page 234 of the
book which is done in Unit 3. Use 4 × 4 matrix representation of 3D
transformations.
11-4. Please refer to Eqn. (11-35) on page 439 of the book. Since the matrix
aligns vectors u ′x , u ′y , u ′z with x , y and z-coordinate axes, it follows that
it is equivalent to the matrix product R y (β)R x (α) .
d 0 −a 0 1 0 0 0
0 1 0
0 0 c/d − b/d 0
R y (β)R x (α) =
a 0 d 0 0 b / d c / d 0
0 0 0 1 0 0 0 1
d − ab/d − ac/d 0
0 c/d − b/d 0
=
a b c 0
0 0 0 1
Observe that the third row vector is the vector about which rotation has to
be performed. Then the two other row vectors are perpendicular to this
vector and also to each other. Hence they form mutually orthogonal
system of vectors. When one of the vectors is exactly the same as the
rotation axis vector, then the matrix (11-35) must be the same as that
obtained by taking the product R y (β)R x (α) .
105
Computer Graphics 11-9. Since the direction angles are given as α, β, γ (refer to section A-2, page
625 of the book), the unit vector along which you need to scale would be
given by u = (u x , u y , u z ) where
Apply rotation to align the vector u with one of the coordinate axes, say
z-axis. So, you need to apply the transformation as given in the book.
Then scale the object with respect to z-axis and finally apply the inverse
rotations to bring things in their original orientation. Hence, the following
sequence of operations will be applied.
1 0 0 0 u2 + u2 0 − ux 0
0 y z
1 0 0 0 1 0 0
S1 =
0 0 s 0 u 0 u y + u 2z
2
0
x
0 0 0 1 0 0 0 1
1 0 0 0
uz − uy
0 0
u 2y + u 2z u 2y + u 2z
uy uz
0 0
u 2y + u 2z u 2y + u 2z
0 0 0 1
1 0 0 0
uz uy
0 0 u 2y + u 2z 0 ux 0
u 2y u 2z u 2y u 2z
+ + 0 1 0 0
S2 =
0
− uy uz
0 − u x 0 u y + u 2z
2
0
u 2y + u 2z u 2y + u 2z 0 0 0 1
0 0 0 1
S = S 2 ⋅ S1
E4) You are given a look at vector, which can be taken as the vector normal to
the view plane and in the opposite direction. You have to select a view up
vector yourself. Based on these vectors, you can proceed as in E1).
106
Exercises given on page 98 of the unit. Three Dimensional
Transformations
E5)
Parallel Projection Perspective projection
Coordinate position are Coordinate position are transformed
transformed to view plane along to view plane along lines that
parallel lines. The vector defining converge to a point called projection
the direction of lines is called reference point (PRP).
projection vector.
Object distance from the view Object's distance from the PRP and
plane is not significant. view plane is important. It plays a
role in determining size of the image.
Farther the object, smaller is the
image.
Parallel lines remain parallel. Parallel lines may not remain
parallel.
Ratios are preserved in general. Ratio are not preserved in general.
E7) Figs. 7 and 8 show the top view, front view and the side views of the
objects. Draw the object shape.
(a)
Fig. 7.
(b)
Fig. 8.
107
Computer Graphics Exercises given on page 102 of the unit
E8) Refer to the formula (12-16) of the book on page 465. Here
z prp = 5, z vp = 0, d p = 5 .
5x 5y
Therefore x p = , yp = .
5−z 5−z
Images of points are given as follows:
5 5 10 5
(1, 0, 1) → ( , 0), (2, 0, 1) → ( , 0), (2, 0, 2) → ( , 0), (1, 0, 2) → ( , 0)
4 2 3 3
5 5 5 5 10 5 5 5
(1, 1, 1) → ( , ), (2, 1, 1) → ( , ), (2, 1, 2) → ( , ), (1, 1, 2) → ( , )
4 4 2 4 3 3 3 3
(5/4, 5/4)
(5/2, 5/4)
(0, 0)
(5/4, 0) (5/3, 0) (5/2, 0) (10/3, 0)
Fig. 9.
There will be only one vanishing point since the view plane is on the
xy-plane, so all lines parallel to the xy-plane will remain parallel even
after the projection. The vanishing point in this projection is (0, 0) as
shown.
E9) Projected cube with (i) 1-point perspective image (ii) 2-point perspective
image (iii) 3-point perspective image are shown in Fig. 10 below and
Fig. 11 on the next page.
(i) One point perspective image (ii) Two point perspective image
Fig. 10.
108
Three Dimensional
Transformations
Fig. 11.
2. Write a code for (i) general parallel projection where the view vector is at
the choice of user and projection plane is x-y plane (ii) perspective
projection where the projection reference point is at the user’s choice and
the projection plane is again xy-plane.
***
109