0% found this document useful (0 votes)
25 views

Unit 5 Three Dimensional Transformations: Structure Page No

This document discusses three dimensional transformations. It begins by introducing 3D primitive transformations like translation, rotation, scaling, and their extensions from 2D. It describes how 3D transformations are used for rendering 3D scenes onto 2D displays by applying a sequence of transformations including camera positioning and projection. The document then provides examples of applying 3D rotation transformations around arbitrary axes and scaling a 3D object. It aims to explain how to implement common 3D geometric transformations.

Uploaded by

Muhammad Sahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Unit 5 Three Dimensional Transformations: Structure Page No

This document discusses three dimensional transformations. It begins by introducing 3D primitive transformations like translation, rotation, scaling, and their extensions from 2D. It describes how 3D transformations are used for rendering 3D scenes onto 2D displays by applying a sequence of transformations including camera positioning and projection. The document then provides examples of applying 3D rotation transformations around arbitrary axes and scaling a 3D object. It aims to explain how to implement common 3D geometric transformations.

Uploaded by

Muhammad Sahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Three Dimensional

UNIT 5 THREE DIMENSIONAL Transformations

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.

For displaying a 3D scene on a 2D display, one requires a sequence of


transformations. First you need to fix a position from where you would like to
view the scene. This is called camera or eye position. From that particular
orientation, you can then plan what to display and where to display. To model
the view angle and orientation, you need to apply a sequence of such 3D
primitive geometric transformations. Further, you require certain projection
transformation to display the scene on a 2D viewport. These geometric and
projective transformations are discussed in Chapters 11 and 12 of the book.

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.

5.2 3D PRIMITIVE AND COMPOSITE


TRANSFORMATIONS
In Unit 3, you have studied and implemented 2D geometric transformations for
object definitions in two dimensions. These transformations can be extended to 89
Computer Graphics 3D objects by including considerations for the z coordinate. In this unit, you
will study certain methods to implement such transformations. These methods
are discussed in Chapters 11 and 12 of the book. To know the details about
these methods you may start with reading the following:

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.

For further understanding of the general 3D transformation about an arbitrary


axis, let us consider the following examples.

Example 1: Rotate an object through an angle of 45 degrees with centre of the


object on (1, 2, 3) . The axis of rotation is given by the direction vector (1, 1, 1)
passing through (1, 0, 0) .

Solution: Refer to the book for the notations. Here


1 1
P = (1, 2, 3), V = ( 1, 1, 1), u = (1, 1, 1), u ′ = (0, 1, 1),
3 3
1 1 1
.
cos α = u ′ u
z
| u′ | | u | =
z 2
, u x | u ′ | | u z | sin α = u ′ × u z =
3
u x ⇒ sin α =
2

(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.

P′ = T(1, 0, 0) R x (−α) R y (−β)R z (45 )R y (β)R x (α) T( −1, 0, 0) P

Now you can calculate the coordinates of the changed position of P by


multiplying by the matrices in the above order.

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.

***

Example 2: An object has to be rotated about an axis passing through the


points (1, 0, 1), (1, 3, 1) . What will be the resulting rotation matrix?

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.

E3) Rotate a scene modelled in 3D by an angle of θ degrees such that the


axis of rotation is along the vector (1, 1, 0) and the origin remains
unchanged. Define the direction of positive rotation as clockwise when
one is looking towards the origin in the direction opposite to the vector
(i.e., in the direction (−1, − 1, 0) ). Assume a right handed coordinate
system. Write the sequence of transformations to implement the
rotation.

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.

You have studied and implemented OpenGL functions for 2D transformations.


As you know in OpenGL every entity is treated as a 3D entity. Actually the
transformation functions are also created with this philosophy only. Hence the
OpenGL functions that you have used for 2D transformations are also used for
3D geometric transformations. Following OpenGL routines can be used for
applying 3D primitive transformations.
• glScaled(sx,sy,sz): Scale by sx in x coordinate, by sy in
y-coordinate and by sz in z-coordinate direction.

• glTranslated(dx,dy,dz): Translate by dx in x-direction, by dy


in y-direction and by dz in z direction.

• glRotated(angle,vx,vy,vz): Rotate by 'angle' degrees


about the vector (vx, vy, vz). If the vector is not unit vector, the routine
automatically converts that to unit vector.
OpenGL transformation matrices are post-multiplied and you have to use
column major matrices. Note that post-multiplying with column-major
matrices produces the same result as pre-multiplying with row-major matrices.
You can use any notation, as long as it is clearly stated.
OpenGL matrices are 16-value arrays with base vectors laid out contiguously
in memory and the indices are numbered from 1 to 16. The translation
components occupy the 13th, 14th, and 15th elements of the array. To explain
you further the difference between row-major and column-major order we give
here following example. The matrix A given by

92
1 2 3 4 Three Dimensional
Transformations
2 3 4 1 
A=
3 4 1 2
 
0 0 0 1

In row-major storage A is accessed in linear memory in such a way that rows


are stored one after the other. This means A will be accessed as follows –

[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

glRotatef(45.0, 0.0, 1.0, 0.0); // R

glTranslatef(3.0, 4.0, 1.0); // T

the transformations occur in the reverse order


S * R * T * P = S * (R * (T * P)),
where P is the point on which you are applying the transformation.
But before using these transformations, you need to know about the concept of
viewing and related operations. So, we move to the next section where you will
learn about setting up a view direction and transforming your coordinate
system accordingly.

5.3 THREE-DIMENSIONAL VIEWING


Three dimensional objects are created using modelling coordinate system. The
modelled objects are then placed in locations specified in the scene with
respect to the world coordinate system. You can view the scene from any
orientation and this is what you call as the view of the scene. Viewing
coordinates are the coordinates generated with respect to the frame of reference
of the viewer, or you may call it the frame of reference created by the position
of camera. Viewing transformation is a composite transformation, which
transforms the scene from world coordinate system to viewing coordinate
system. To understand what viewing coordinate system and the viewing
transformation are, let us consider a simple example. Suppose you have a
vacant room and you want to organize its interior to make it an office. Chairs,
tables, shelves, etc. are created somewhere else (in some factory) using some
coordinate system. Let that be treated as a modelling coordinate system. Then
these items will be brought in the room (Transform to locations in the room).
Finally every object will be placed in appropriate position. This means the
modelled objects are now placed in the world coordinate system. Now you
93
Computer Graphics view it from various angles and then become satisfied with the arrangements.
When you are watching it from various angles, each time you change your
position, the relative positions of the objects in the room also change. Objects
closer to you become farther when you go on the other side of the room. This is
viewing coordinate system and your eyes make an automatic transformation of
objects from world coordinate system to viewing coordinate system.

How to transform from world coordinate system to viewing coordinate system?


To get an answer to this question read the following.

Read Secs. 12-1 to 12-2, Chapter 12, pages 452-458 of the book.

Let us now consider an example.

Example 4: Transform the scene in the world coordinate system to the


viewing coordinate system with viewpoint at (2, 2, 2) . The view plane normal
vector is ( −1, − 1, − 1) and the view up vector is ( 0, 1, 0) .

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

The resulting sequence of matrix operations is therefore given by

 −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

***

You may now try the following exercise.

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?

OpenGL Model View and related Matrix Operations

Since modelling is constructing the 3D model, modelling transformations are


concerned with positioning (translation), orientation (rotation) and scaling of
94
the objects in the virtual world and with their relative positions, orientations
and sizes (scale). Following functions facilitate you to perform the operations Three Dimensional
Transformations
related to modelling the scene.

• 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

 m11 m12 m13 m14 


m m 22 m 23 m 24 
 21
 m 31 m 32 m 33 m 34 
 
m 41 m 42 m 43 m 44 

 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).

• gluLookAt (eyeX, eyeY, eyeZ, atX, atY, atZ, upX,


upY, upZ);
 Used for positioning, pointing and orienting the virtual camera.
Here
• eyeX, eyeY, eyeZ give the location of the camera / eye.
• atX, atY, atZ specify the pointing direction of the camera.
95
Computer Graphics • upX, upY, upZ give the orientation.

 The default settings are


• (eyeX, eyeY, eyeZ) = (0, 0, 0). This is when you do not call
gluLookAt. Using gluLookAt, you can change the position
of viewer or camera.
• (atX, atY, atZ ) = (0, 0,-1). View is along negative z-axis.
• (upX, upY, upZ) = (0, 1, 0) means camera is oriented with
up vector along the y-axis.

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.

Projections can be classified into two types – parallel projections and


perspective projections. Depending on the type of application, one of the
projection types is used. While parallel projections are extensively used in
architectural drawing, perspective projections are used to bring reality to the
projected image, e.g., in games and virtual reality problems.

96
The taxonomy of projections is as shown in Fig. 2. Three Dimensional
Transformations

Projection

Parallel Perspective

Oblique Orthographic One point Two point Three point


perspective perspective perspective
Axonometric Multiview

Isometric

Fig. 2.

To know about projections, their types and applications in detail you may read
the following section of the book.

Read Secs. 12-3, Chapter 12, pages 458-466 of the book.

For a better understanding of different types of projections we are giving here


few examples.

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) .

Solution: For an orthographic projection the view direction has to be


perpendicular to the view plane and the square object will be projected as given
below in (i). In case of an oblique parallel projection with projection angle
parameters α = φ = 45  , a point ( x , y, z) will be projected to
( x + z / 2 , y + z / 2 , 0) . You can now easily compute the projection of each
vertex as per the rule. The transformed points given in the sequence are as
follows.

(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.

Let P ′ be the projection of P on the projection plane. The vectors OP and


OP ′ satisfy OP ′ = c OP . Thus

x ' = cx, y' = cy, z' = cz . (1)

Since P ′ falls on the projection plane, it must satisfy the equation


− x '+ y' − z' = d where d = −1 ⋅ 2 + 1 ⋅ 3 + (−1) ⋅ ( −1) = 2 (ref. Eqn.(10-6) page 329
of the book). Substituting the values of x ′, y ′, z ′ from Eqn. (1), we get
2
− cx + cy − cz = 2 ⇒ c =
−x+y−z
Hence the projection is given by
2x 2y 2z
x' = , y' = , z' = .
−x+y−z −x+y−z −x+y−z

***

You may now try the following exercises.

E5) Distinguish between parallel and perspective projection.

E6) Find out the matrix of projection for cavalier projection.

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.

5.5 VIEW VOLUMES AND GENERAL


PROJECTION TRANSFORMATIONS
You can start with reading the following.

Read Sec. 12-4, Chapter 12, pages 467-476 of the book.

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.

For orthographic parallel projection:

glOrtho(left, right, bottom, top, near, far);


glOrtho2D(left, right, bottom, top);

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 .

For perspective projection:

glFrustum(float l, float r, float b, float t, float n,


float f );

99
Computer Graphics The quantities are as specified in Fig. 5.

View Volume Frustum


(r , t , − n )
z = −f

z = −n (l, b, − n )

Fig. 5.

gluPerspective( float θ, float aspectRatio, float n, float


f );

Here θ is an angle (measured in degrees) specifying the vertical field of view


(θ is the solid angle between the top bounding plane and the bottom bounding
plane of the frustum). The parameter aspectRatio specifies the ratio of the
width of the frustum to the height of the frustum. A call to gluPerspective is
equivalent to calling glFrustum with

t = n tan(θ / 2); b = − n tan(θ / 2); r = t ⋅ aspectRatio; l = b ⋅ aspectRatio;

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.

//Program to draw 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 reShape(GLint newWidth, GLint newHeight)


{
glViewport(0,0,newWidth, newHeight);
w=newWidth;
h=newHeight;
}

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);
}

The output will be as shown in Fig.6.

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.

You may now try the following exercises.

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:

1) A three-dimensional translation shifts the object by a prescribed


102
parameter vector. For example, to show an object moving along a spiral
path C( t ) = (cos t , sin t , t ), (0 ≤ t ≤ 2π) , all you need to do is to keep Three Dimensional
Transformations
translating the object along the spiral by a parameter vector
(cos t , sin t , t ) , for t ∈ [0, 2π] .

2) Rotations about the coordinate axes are simple extensions of 2D rotation.


The rotation matrices R x , R y , R z about the coordinate axes x , y, z
respectively are given as follows:
1 0 0 0  cos θ 0 sin θ 0
0 cos θ − sin θ 0  0 1 0 0
Rx =  ;R y = 
0 sin θ cos θ 0 − sin θ 0 cos θ 0
   
0 0 0 1  0 0 0 1
cos θ − sin θ 0 0
 sin θ cos θ 0 0
Rz = 
 0 0 1 0
 
 0 0 0 1
3) For performing rotation about an axis parallel to one of the coordinate
axes (say z-axis), you first need to translate the axis (and hence the
object) so that it aligns with that coordinate axis (z-axis) and then perform
the rotation. Finally, translate the axis back to its original position.

4) Rotation about an arbitrary axis is a composition of several rotations and


translation operations. What you need to do is the following:
a) If the axis passes through the origin, perform the sequence of
rotations so that it aligns with one of the coordinate axes. Then
perform the required rotation of the object. Finally apply the
reverse sequence of inverses rotations so that the axis attains its
original orientation.
b) If the axis does not pass through the origin, translate the axis to
make it pass through the origin. Perform the operations as given in
(a) above and then translate the axis back to its original position.

5) Scaling, shear and reflection operations have natural extensions to 3D.

6) Viewing coordinates are the coordinates with reference to the eyes of the
viewer or the coordinates with reference to camera position and
orientation.

7) To transform from the world coordinate system to viewing coordinate


system you need to perform the following operations.
a) Translate the viewing coordinate origin to the world coordinate
origin (and hence the scene).
b) Rotate the axes so that your viewing coordinate system has the
same orientation as the world coordinate system.

While performing such an operation, you are actually performing a


change of basis transformation. This again results in another orientation
of three mutually perpendicular (linearly independent ) unit vectors.
103
Computer Graphics 8) Simple techniques exist which help you transform your scene from world
coordinate system to viewing coordinate system. For this you need to
define a look at vector and a look up vector. These are used in defining a
Cartesian coordinate system for your view/camera coordinates.

9) For transforming a 3D scene to a 2D display, you need to use a


transformation called projection. In computer graphics, a projection is a
transformation that maps a 3D point (x, y, z) to a 2D point ( x ′, y ′) on a
projection plane. Remember that ( x ′, y ′) does not mean that we are
simply omitting z-coordinate. Both x ′ and y ′ are functions of x, y and z,
in general.

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.

12) Parallel projection is orthographic if the projection vector is orthogonal to


the view plane. Otherwise, it is called oblique parallel projection.
Different parallel projections like isometric, cavalier and cabinet
projections have been in use extensively.

13) Perspective projection gives more realistic appearance and uses the same
principle as used in camera.

14) Perspective projection is not an affine transformation.

15) OpenGL functions for modelling and viewing transformations.


a) glMatrixMode(GL_MODELVIEW );
b) gluLookAt (eyeX, eyeY, eyeZ, atX, atY, atZ, upX, upY,
upZ);

16) OpenGL functions for parallel and perspective projections.


a) glFrustum(float l, float r, float b, float t, float n, float f
);

b) gluPerspective( float θ, float aspectRatio, float n, float f


);

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) .

E2) Translate by ( −2, 1, − 1) , scale by a factor of 2 in y-direction and then


translate by ( 2, − 1, 1) . The sequence will be as follows:

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

Exercises given on page 450 of the book.

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

cos α = u x , cos β = u y , cos γ = u z .

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

Note: The matrices in the transformation S2 are inverse rotation


transformations to matrices in S1. Remember that inverse rotation matrix
for rotation matrix R(θ) is nothing but R ( −θ) .

Exercise given on page 94 of the unit.

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.

Useful in civil, architectural Useful in virtual reality, gaming and


design, machine design, sectional other such applications.
view design, etc.
Less realistic. More realistic.

E6) Cavalier projection corresponds to tan α = 1, leading to the following


matrix.
1 0 cos φ 0
0 1 sin φ 0
M parallel =  
0 0 0 0
 
0 0 0 1

E7) Figs. 7 and 8 show the top view, front view and the side views of the
objects. Draw the object shape.

(a)

Top view Front view View from left

Fig. 7.

(b)

Top view Front view View from left

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

The image of projected cube will be as shown in Fig. 9.

(5/3, 5/3) (10/3, 5/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

(iii) Three point perspective image

Fig. 11.

E10) a) True. It is a type of oblique parallel projection when the tan α = 1 ,


where α is the angle between the two lines A and B described as
follows (see Figure 12-21 of the book on page 462).
A-line segment joining points – oblique parallel projected image and
the orthogonal parallel projected image of a point
B-line segment joining points – point to be projected and the
projected image of the point as per the oblique projection.
b) True. Parallel lines appear converging to a point.
c) False. The perspective projection contains z coordinate in the
denominator. Hence it cannot be an affine transformation.
d) False. A vanishing point for any set of lines that are parallel to one of
the principal coordinate axes of object is called a principal vanishing
point. So there can be 1, 2 or 3 principal vanishing points.

5.8 PRACTICAL EXERCISES


Session 7

1. Write a code to generate a composite matrix for general 3D rotation


matrix. Test your code and rotate continuously a cube about an axis.

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

You might also like