Computer Graphics
UNIT - 3
TRANSFORMATIONS
Affine transformations,
Transformations in homogeneous coordinates,
Concatenation of transformations,
OpenGL transformation matrices.
Viewing: Classical and Computer views,
Viewing with a computer,
Positioning of camera,
Simple projections,
Projections in OpenGL,
Hidden surface removal,
Parallel-projection matrices,
Perspective-projection matrices
Dept. of CSE,NGIT 1
Computer Graphics
3.1 Affine transformations
An affine transformation is an important class of linear 2-D geometric transformations
which maps variables (e.g. pixel intensity values located at position in an input
image) into new variables (e.g. in an output image) by applying a linear
combination of translation, rotation, scaling and/or shearing (i.e. non-uniform scaling
in some directions) operations.
The general affine transformation is commonly written in homogeneous coordinates
as shown below:
By defining only the B matrix, this transformation can carry out pure
translation:
Pure rotation uses the A matrix and is defined as (for positive angles being
clockwise rotations):
Here, we are working in image coordinates, so the y axis goes downward. Rotation
formula can be defined for when the y axis goes upward.
Similarly, pure scaling is:
(Note that several different affine transformations are often combined to produce a resultant
transformation. The order in which the transformations occur is significant since a
translation followed by a rotation is not necessarily equivalent to the converse.)
Since the general affine transformation is defined by 6 constants, it is possible to define this
transformation by specifying the new output image locations of any three input
image coordinate pairs. (In practice, many more points are measured and a least
squares method is used to find the best fitting transform.)
Dept. of CSE,NGIT 2
Computer Graphics
Rotation, translation and scaling
Translation
void glTranslate{fd} (TYPE x, TYPE y, TYPE z);
Multiplies the current matrix by a matrix that moves (translates) an object by the given x,
y, and z values
Rotation
void glRotate{fd}(TYPE angle, TYPE x, TYPE y, TYPE z);
Multiplies the current matrix by a matrix that rotates an object in a
counterclockwise direction about the ray from the origin through the point (x, y,
z). The angle parameter specifies the angle of rotation in degrees.
Scaling
void glScale{fd} (TYPEx, TYPE y, TYPEz);
Multiplies the current matrix by a matrix that stretches, shrinks, or reflects
Dept. of CSE,NGIT 3
Computer Graphics
an object along the axes.
Equations :
Translation: Pf = T + P
xf = xo + dx
yf = yo + dy
Rotation: Pf = R · P
xf = xo * cos - yo *sin
yf = xo * sin + yo *cos
Scale: Pf = S · P
xf = sx * xo
yf = sy * yo
3.2 Transformations in homogeneous coordinates
More generally, we consider the <x, y, z> position vector to be merely a special case of
the four-component <x, y, z, w> form. This type of four-component position vector is
called a homogeneous position. When we express a vector position as an <x, y, z>
quantity, we assume that there is an implicit 1 for its w component.
Mathematically, the w value is the value by which you would divide the x,
y, and z components to obtain the conventional 3D (nonhomogeneous) position, as
shown in Equation 3-1.
Equation 3-1 Converting Between Nonhomogeneous and Homogeneous Positions
Expressing positions in this homogeneous form has many advantages.
Dept. of CSE,NGIT 4
Computer Graphics
For one, multiple transformations, including projective transformations required
for perspective 3D views, can be combined efficiently into a single 4x4 matrix.
Also, using homogeneous positions makes it unnecessary to perform expensive
intermediate divisions and to create special cases involving perspective views.
Homogeneous positions are also handy for representing directions and
curved
surfaces described by rational polynomials.
3.3 Concatenation of transformations
o Rotate a house about the origin
o Rotate the house about one of its corners
o translate so that a corner of the house is at the origin
o rotate the house about the origin
o translate so that the corner returns to its original position
All these operations could be carried out at once by multiplying the corresponding matrices
and obtaining one single matrix which would then be multiplied with the projection
matrix of the object to obtain the final result.
3.4 Classical Viewing
3 basic elements for viewing :
– One or more objects
– A viewer with a projection surface
– Projectors that go from the object(s) to the projection surface
Classical views are based on the relationship among these elements
Each object is assumed to constructed from flat principal faces
– Buildings, polyhedra, manufactured objects
– Front, top and side views.
Planar Geometric Projections
Projections : Standard projections project onto a plane
Dept. of CSE,NGIT 5
Computer Graphics
Projectors : Lines that either converge at a center of projection or, are parallel
(DOP)
Nonplanar projections are needed for applications such as map construction
Perspective and parallel projections :
Parallel viewing is a limiting case of perspective viewing
Perspective projection has a COP where all the projector lines converge.
Parallel projection has parallel projectors. Here the viewer is assumed to be present at
infinity.
So here we have a “Direction of projection(DOP)” instead of center of projection(COP).
Types Of Planar Geometric Projections :
Dept. of CSE,NGIT 6
Computer Graphics
Orthographic Projections :
Projectors are perpendicular to the projection plane.
Projection plane is kept parallel to one of the principal faces.
A viewer needs more than 2 views to visualize what an object looks like from
its multiview orthographic projection.
Advantages and Disadvantages :
Preserves both distances and angles
o Shapes preserved
o Can be used for measurements
Building plans
Manuals
Cannot see what object really looks like because many surfaces hidden from view
– Often we add the isometric
Axonometric Projections
Dept. of CSE,NGIT 7
Computer Graphics
Projectors are orthogonal to the projection plane , but projection plane can move
relative to object.
Classification by how many angles of a corner of a projected cube are the same
none: trimetric
two: dimetric
three: isometric
Advantages and Disadvantages :
Lines are scaled (foreshortened) but can find scaling factors
Lines preserved but angles are not
Projection of a circle in a plane not parallel to the projection plane is an ellipse
Can see three principal faces of a box-like object
Some optical illusions possible
Parallel lines appear to diverge
Does not look real because far objects are scaled the same as near objects
Used in CAD applications
Oblique Projections
Arbitrary relationship between projectors and projection plane
Perspective Viewing
Characterized by diminution of size i.e. when the objects move farther from the viewer
it appears smaller.
Major use is in architecture and animation.
3.5 Viewing with a Computer
There are three aspects of the viewing process, all of which are implemented in the
pipeline,
– Positioning the camera
Setting the model-view matrix
– Selecting a lens
Setting the projection matrix
– Clipping
Setting the view volume
The OpenGL Camera
In OpenGL, initially the object and camera frames are the same
– Default model-view matrix is an identity
The camera is located at origin and points in the negative z direction
OpenGL also specifies a default view volume that is a cube with sides of length
Dept. of CSE,NGIT 8
Computer Graphics
2 centered at the origin
– Default projection matrix is an identity
Positioning of the Camera.
If we want to visualize object with both positive and negative z values we can either
o Move the camera in the positive z direction
Translate the camera frame
o Move the objects in the negative z direction
Translate the world frame
Both of these views are equivalent and are determined by the model-view matrix
o Want a translation (glTranslatef(0.0,0.0,-d);)
o d>0
We can move the camera to any desired position by a sequence of rotations
and translations
Example: to position the camera to obtain a side view :
– Rotate the camera
– Move it away from origin
– Model-view matrix C = TR
Dept. of CSE,NGIT 9
Computer Graphics
E.g. 1 : Code to view an object present at the origin from a positive x axis
First the camera should be moved away from the object
0
Secondly the camera should be rotated about the y axis by 90 .
glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
glTranslatef(0.0, 0.0, -d);
glRotatef(90.0, 0.0, 1.0, 0.0);
Consider that we would like to get an isometric view of a cube centered at origin.
Consider the camera is placed somewhere along the positive z axis.
An isometric view could be obtained by :
o Rotate the cube about x-axis till we see the 2 faces symmetrically(45 degrees).
o Rotate cube about y axis to get the desired isometric view (-35.26 degrees)
The final model-view matrix is given by
o M = TRxRy
R = RxRy
Rx is of the form : 1 0 0 0
0 cosθ -sin θ 0
0 sin θ cosθ 0
0 0 0 1
Dept. of CSE,NGIT 10
Computer Graphics
[ NOTE : The default matrix for homogeneous coordinate, right handed, 3D system
is given by :
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Ry is of the form :
cosθ 0 sinθ 0
0 1 0 0
-sin θ 0 cos θ 0
0 0 0 1
Translation is a simple addition of the
matrices. glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
glTranslatef(0.0, 0.0, -d);
glRotatef(35.26, 0.0, 1.0, 0.0);
glRotatef(45.0, 0.0, 1.0, 0.0);
The openGL function to set camera orientation :
The Look At function : glLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)
Where : eyex, eyey, eyez – coordinates of the COP
atx, aty, atz - coordinates of the point at which the camera is
pointing at.
upx, upy, upz - The up direction
3.6 OpenGL Perspective
In case of orthographic projections, the default view volume is a parallelpiped. This
is because the projector lines are parallel and they do not converge at any
point(theoritically they converge at infinity)
In case of perspective projections, the projector lines converge at the COP. Hence the view
volume would be a frustum rather than a parellelpiped.
The frustum (part of a pyramid) as shown in the diagram has a near and a far plane.
The objects within this would be within the view volume and visible to the viewer.
OpenGL provides a function to define the
frustum.
glFrustum(left,right,bottom,top,near,far
)
Dept. of CSE,NGIT 11
Computer Graphics
Using Field of View
With glFrustum it is often difficult to get the desired view
gluPerpective(fov, aspect, near, far) often provides a better interface
Dept. of CSE,NGIT 12
Computer Graphics
where aspect = w/h
Fov = field of view (The area that the lens
would cover is determined by the angle shown in the diagram)
3.7 Hidden surface removal
A graphics system passes all the faces of a 3d object down the graphics pipeline to generate
the image. But the viewr might not be able to view all these phases. For e.g . all the 6 faces
of a cube might not be visible to a [Link] the graphics system must be careful as to
which surfaces it has to display.
Hidden surface – removal algortithms are those that remove the surfaces of the image
that should not be visible to the viewer.
2 types:
Object Space Algorithm : Orders the surfaces of the objects in such a way that
rendering them would provide the correct image.
Image Space Algorithm : Keeps track of the distance of the point rasterized from
the projection plane.
– The nearest point from the projection plane is what gets rendered.
– E.g z buffer algorithm.
Culling : For convex objects like sphere, the parts of the object which are away from
the viewer can be eliminated or culled before the rasterizer.
glEnable(GL_CULL);
Dept. of CSE,NGIT 13
Computer Graphics
Interactive Mesh Displays
A mesh is a set of polygons that share vertices and [Link] to calculate
topographical elevations.
Suppose that the heights are given as a function of y as :
y = f(x,z)
then by taking samples of x and z ,y can be calculated as follows :
yij = (xi,zj) yi+1,j
= xi+1,zj yi,j+1 =
xi,zj+1 yi+1,j+1 =
xi+1,zj+1
Normalization
Rather than derive a different projection matrix for each type of projection, we
can convert all projections to orthogonal projections with the default view volume
This strategy allows us to use standard transformations in the pipeline and makes
for efficient clipping
We stay in four-dimensional homogeneous coordinates through both the
modelview and projection transformations
o Both these transformations are nonsingular
o Default to identity matrices (orthogonal view)
Normalization lets us clip against simple cube regardless of type of projection
Dept. of CSE,NGIT 14
Computer Graphics
Delay final projection until end
o Important for hidden-surface removal to retain depth information as long
as possible
Orthogonal Normalization
glOrtho(left,right,bottom,top,near,far)
The diagram shows the normalization process
First the view volume specified by the glortho function is mapped to the canonical form
Canonical Form : default view volume centerd at the origin and having sides of length 2.
This involves 2 steps :
– Move center to origin
T(-(left+right)/2, -(bottom+top)/2,(near+far)/2))
– Scale to have sides of length 2
S(2/(left-right),2/(top-bottom),2/(near-far))
The resultant matrix is a product of the above 2 matrices i.e. P = ST =
2 right lef t
right 0 0 lef t
lef t right
2 top bottom
0 bottom 0 bottom
top to near
0 p
0 near2 f f far
ar near
0 0 0 ar 1
Dept. of CSE,NGIT 15
Computer Graphics
Dept. of CSE,NGIT 16