0% found this document useful (0 votes)
82 views98 pages

2D and 3D Geometric Transformation

Uploaded by

Dhanuz Pc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views98 pages

2D and 3D Geometric Transformation

Uploaded by

Dhanuz Pc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

Geometric Transformations

OVERVIEW

• Two dimensional transformations


• Matrix representations
• Inverse transformations
• Three dimensional transformations
Geometric transformation functions
• Translation
• Rotation
• Scaling
• Reflection
• Shearing

• We first discuss the 2D transformations, then we will continue with 3D.


2D Translations

Point P defined as P ( x, y ),
translate to Point P( x, y) a distance d x parallel to x axis, d y parallel to y axis.
x = x + d x y  = y + d y
Define the column vec tors P’
 x  x  d x 
P =   , P =   , T =  
 y 
P
 y dy 
Now
P = P + T
2D Scaling from the origin.
Point P defined as P ( x, y ),
Perform a scale (stretch) to Point P( x, y) by a factor s x along the x axis,
and s y along the y axis.
x = s x . x, y = s y . y
Define the matrix P’
sx 0 P
S=
0 s y 
Now
 x   s x 0  x 
P = S  P or  y =  0 . 
   s y   y
2D Rotation about the origin.


P’(x’,y’)

P(x,y)
r

r
x
2D Rotation about the origin.

P’(x’,y’)

r
P(x,y) x = r. cos 
y = r. sin 
 y
r

x
x
2D Rotation about the origin.

x = r. cos( +  ) = r. cos  . cos  − r. sin  . sin 


y y = r. sin( +  ) = r. cos  . sin  + r. sin  . cos 

P’(x’,y’)

P(x,y)
r x = r. cos 
y = r. sin 
 y
r

x
x
2D Rotation about the origin.
x = r. cos( +  ) = r. cos  . cos  − r. sin  . sin 
y = r. sin( +  ) = r. cos  . sin  + r. sin  . cos 
Substituting for r :
x = r. cos 
y = r. sin 

Gives us :
x = x. cos  − y. sin 
y = x. sin  + y. cos 
2D Rotation about the origin.
x = x. cos  − y. sin 
y  = x. sin  + y. cos 

Rewriting in matrix form gives us :


 x  cos  − sin    x 
 y =  sin  cos  . y 
    

cos  − sin  
Define the matrix R =   , P = R  P
 sin  cos  
Transformations.
• Translation.
• P=T + P
• Scale
• P=S  P
• Rotation
• P=R  P
• We would like all transformations to be multiplications so we can
concatenate them  express points in homogenous coordinates.
Homogeneous coordinates
• Many graphics applications involve sequences of geometric
transformations.
• An animation might require an object to be translated and rotated at
each increment of the motion.
• In design and picture construction applications, we perform
translation, rotation, and scaling to fit the picture components into
their proper positions.
Homogeneous coordinates
• Each of the three basic two-dimensional transformations (translation, rotation,
and scaling) can be expressed in the general matrix form
P’ = M1 · P + M2

• With coordinate positions P and P’ represented as column vectors.


• Matrix M1 is a 2 × 2 array containing multiplicative factors, and M2 is a two-element
column matrix containing translational terms.
• A more efficient approach, however, is to combine the transformations so that
the final coordinate positions are obtained directly from the initial coordinates,
without calculating intermediate coordinate values.
• We can do this by reformulating the above equation to eliminate the matrix
addition operation.
Homogeneous coordinates
• Multiplicative and translational terms for a two-dimensional geometric
transformation can be combined into a single matrix if we expand the
representations to 3 × 3 matrices.
• A standard technique for accomplishing this is to expand each two-
dimensional coordinate-position representation (x, y) to a three-element
representation (Xh, Yh, h), called homogeneous coordinates, where the
homogeneous parameter h is a nonzero value such that
Homogeneous coordinates
• Add an extra coordinate, h, to a point.
• P(x,y,h).
• Two sets of homogeneous coordinates represent the same point if they
are a multiple of each other.
• (2,3,1) and (4,6,2) represent the same point.
• At least one component must be non-zero  (0,0,0) is not defined.
• If h 0 , divide by it to get Cartesian coordinates of point (x/h, y/h,1).
• If h=0, point is said to be at infinity.
Translations in homogenised coordinates
• Using a homogeneous-coordinate approach, we can represent the
equations for a two-dimensional translation of a coordinate
position using the following matrix multiplication:

• This translation operation can be written in the abbreviated form


Homogeneous form of scale

sx 0
S (sx , s y ) = 
s y 
Recall the (x,y) form of Scale :
0

sx 0 0
In homogeneous coordinates :
S ( s x , s y ) =  0 sy 0
 0 0 1
OR
Homogeneous form of rotation
Similarly, two-dimensional rotation transformation equations about the coordinate
origin can be expressed in the matrix form

 x  cos  − sin  0   x
 y =  sin  cos   
0 . y  
  
 1   0 0 1   1 
OR
Inverse Transformations
TRANSLATION
• For translation, we obtain the inverse matrix by negating the
translation distances.
• Thus, if we have two-dimensional translation distances tx and ty, the
inverse translation matrix is

• This produces a translation in the opposite direction, and the product


of a translation matrix and its inverse produces the identity matrix.
Inverse Transformations
ROTATION
• An inverse rotation is accomplished by replacing the rotation angle by
its negative.

• Negative values for rotation angles generate rotations in a clockwise


direction, so the identity matrix is produced when any rotation
matrix is multiplied by its inverse.
• We can calculate the inverse of any rotation matrix R by evaluating its
transpose .
Inverse Transformations
SCALING
• We form the inverse matrix for any scaling transformation by replacing the
scaling parameters with their reciprocals.
• For two-dimensional scaling with parameters sx and sy applied relative to
the coordinate origin, the inverse transformation matrix is

• The inverse matrix generates an opposite scaling transformation, so the


multiplication of any scaling matrix with its inverse produces the identity
matrix
Two-Dimensional Composite Transformations

• Using matrix representations, we can set up a sequence of


transformations as a composite transformation matrix by calculating
the product of the individual transformations.
• Forming products of transformation matrices is often referred to as a
concatenation, or composition, of matrices.
• Thus, if we want to apply two transformations to point position P, the
transformed location would be calculated as
P’=M2.M1.P
=M.P
Two-Dimensional Composite Transformations
Composite Two-Dimensional Translations
• If two successive translation vectors (t1x, t1y) and (t2x, t2y) are applied
to a two-dimensional coordinate position P, the final transformed
location P’ is calculated as

where P and P’ are represented as three-element, homogeneous-coordinate


column vectors
Two-Dimensional Composite Transformations
Composite Two-Dimensional Translations
• The composite transformation matrix for this sequence of translations
is
Two-Dimensional Composite Transformations
Composite Two-Dimensional Rotation
• Two successive rotations applied to a point P produce the transformed
position
Two-Dimensional Composite Transformations
Composite Two-Dimensional Rotation

• So that the final rotated coordinates of a point can be calculated with


the composite rotation matrix as
Two-Dimensional Composite Transformations
Composite Two-Dimensional Scaling
• Concatenating transformation matrices for two successive scaling
operations in two dimensions produces the following composite scaling
mat
Two-Dimensional Composite Transformations
Composite Two-Dimensional Scaling
• The resulting matrix in this case indicates that successive scaling
operations are multiplicative
2D Composite Transformations

• Combine transformations of different type


• Translate, Rotate, Translate
• Translate, Scale, Translate
• Use to rotate or scale an object w.r.t. a point that is not the origin
• Implement by multiplication of the corresponding homogeneous
matrices
General Two-Dimensional Pivot Point Rotation
Rotation around a pivot point
• Translate the object so that the pivot point moves to the origin.
Rotate about origin
• Translate the object so that the pivot point is back to its original
position
General Two –Dimensional Fixed
Point Scaling
Scaling with respect to a fixed point
• Translate to the origin
• Scale
• Translate back
General Two-Dimensional Scaling Directions
• Parameters sx and sy scale objects along the x and y directions.
• We can scale an object in other directions by rotating the object to align
the desired scaling directions with the coordinate axes before applying the
scaling transformation.
• Suppose we want to apply scaling factors with values specified by
parameters s1 and s2 in the directions shown in Figure.
• To accomplish the scaling without changing the orientation of the object, we first
perform a rotation so that the directions for s1 and s2 coincide with the x and y
axes, respectively.
• Then the scaling transformation S(s1, s2) is applied, followed by an opposite
rotation to return points to their original orientations.
General Two-Dimensional Scaling Directions
• The composite matrix resulting from the product of these three transformations is
General Two-Dimensional Scaling Directions
Example
Matrix Concatenation Properties
• Multiplication of matrices is associative.
• For any three matrices, M1, M2, and M3, the matrix product M3 · M2 · M1
can be performed by first multiplying M3 and M2 or by first multiplying M2
and M1:

• The composite matrix can be constructed either by multiplying from left to


right (pre-multiplying) or by multiplying from right to left (post-multiplying).
• Transformation products, on the other hand, may not be commutative. The
matrix product M2 · M1 is not equal to M1 · M2, in general.
Other Transformation

• Matrix composition is not commutative. So be careful


when applying a sequence of transformation.
Reflection
 A transformation that produces a mirror image of an object is called a reflection.
 For a two-dimensional reflection, this image is generated relative to an axis of reflection by
rotating the object 180◦ about the reflection axis.

sx = -1 sy = 1 original

sx = -1 sy = -1 sx = 1 sy = -1
Other Transformations
• Reflection

Reflection of an object with


Reflection in x and y axes Reflection in origin respect to the line y = x
Shear
• A transformation that distorts the shape of an object such that the
transformed shape appears as if the object were composed of internal
layers that had been caused to slide over each other is called a shear.
• Two common shearing transformations are those that shift coordinate x
values and those that shift y values.
• An x-direction shear relative to the x axis is produced with the
transformation matrix

which transforms
coordinate positions as
Shear Contd…

• An y-direction shear relative to the y axis is produced with the


transformation matrix
Raster Methods for Geometric Transformations
➢ Alternate method for performing certain two-dimensional transformations.
➢ Raster systems store picture information as color patterns in the frame buffer.
➢ Therefore, some simple object transformations can be carried out rapidly by manipulating an array of pixel values.
➢ Functions that manipulate rectangular pixel arrays are called raster operations and moving a block of pixel values
from one position to another is termed a block transfer, a bitblt, or a pixblt.
➢ All bit settings in the rectangular area shown are copied as a block into another part of the frame buffer.
➢ We can erase the pattern at the original location by assigning the background color to all pixels within that block
(assuming that the pattern to be erased does not overlap other objects in the scene).
P
max

P
min

P0
OpenGL Raster Transformations
• A translation of a rectangular array of pixel-color values from one buffer
area to another can be accomplished in OpenGL as the following copy
operation:
glCopyPixels (xmin, ymin, width, height, GL_COLOR);
• First four parameters in this function give the location and dimensions of
the pixel block.
• And the OpenGL symbolic constant GL COLOR specifies that it is color
values are to be copied.
• glReadPixels
• glDrawPixels
OpenGL Functions for Two-Dimensional Geometric
Transformations

• Basic OpenGL Geometric Transformation


• OpenGL Matrix Operations
Basic OpenGL Geometric Transformations
• In the core library of OpenGL, a separate function is available for each of the basic
geometric transformations.
• OpenGL is designed as a 3D graphics API, where all transformations are specified in
three dimensions.
• Internally all coordinate positions are represented as four-element column vectors
• All transformations are represented using 4X4 matrices.
• Performing 2D transformations within OpenGL is generally just a matter of using a
value for the transformations in the third(z) dimension that cause no change in that
dimension.
Basic OpenGL Geometric Transformations:
glTranslate*()
A 4 × 4 translation matrix is constructed with the following routine:
*-> f(float) or d(double)
Translate over [tx, ty, tz]:
glTranslate*(tx, ty, tz);
1.tx,ty and tz -> can be assigned with any real number values
2.For two-dimensional applications,we set tz=0.0;
3.Two-dimensional position is represented as a four-element
column matrix with the z-component equal to 0.0.
Example: glTranslatef(25.0,-10.0,0.0)
Basic OpenGL Geometric Transformations:
glRotate*()
• Similarly, a 4 × 4 rotation matrix is generated with
glRotate*(theta, vx, vy, vz);
v=(vx,vy,vz)-> Defines the orientation for a rotation axis
Theta-> Rotation angle in degrees,which the routine converts to
radians for the trignometric calculations.
V is the unit vector

• Rotation in 2D system is rotation about the z-axis, specified as a unit


vector with x and y components as zero, and a z component as 1.0
For example., glRotatef(90.0, 0.0, 0.0, 1.0);
Basic OpenGL Geometric Transformations:
glScale*()
Scale axes with factors sx, sy, sz:
glScale*(sx, sy, sz);
• sx,sy,sz-> can assign any real-number values
• Scaling in 2D system involves changes in the x and
y dimensions, and a z scaling factor of 1.0(which
causes no change in the z-coordinate positions)
Example: glScalef(2.0, 3.0, 1.0);

A zero value for any scaling parameter can cause a


processing error, because an inverse matrix cannot be
calculated
3D Transformations.
• Use homogeneous coordinates, just as in 2D case.
• Transformations are now 4x4 matrices, represented as a four-element
column vector.
• We will use a right-handed (world) coordinate system - ( z out of page ).
y

Video

Video Note:
Convenient to think of display as
Being left-handed !!
x ( z into the screen )

z (out of page)
3D Translation
• A position P=(x,y,z) in three-dimensional space is translated to a location P' =(x',y',z') by
adding translation distances tx, ty and tz to the Cartesian coordinates of P:
x'=x+tx, y'=y+ty , z'=z+tz ,
The formula illustrates three-dimensional translation.
• We can express the 3D translation operations in matrix form.

• But now the coordinate positions, P and P' are represented in homogeneous
coordinates with four- element column matrices , and the translation operator T is a
4X4 matrix.
Three-Dimensional Rotation
• We can rotate an object about any axis in space, but the easiest rotation axes
to handle are those that are parallel to the Cartesian-coordinate axes.
• Also, we can use combinations of coordinate-axis rotations to specify a
rotation about any other line in space.
• Lets us first consider the operations involved in coordinate-axis rotations, then
we discuss the calculations needed for other rotation axes.
• By convention, positive rotation angles produce counter-clockwise rotations
about a coordinate axis. cos  − sin  0 0
 sin  cos  0 0
• Need to specify which axis the rotation is about.
R z ( ) =  
• z-axis rotation is the same as the 2D case.  0 0 1 0
 
 0 0 0 1
Rotation
• Rotation around the coordinate axes
Rotation Contd..,

• The two- dimensional z-axis rotation equations are easily extended


to three dimensions, as follows:
Eqn1:

Parameter Ө specifies the rotation angle about the z axis, and


z-coordinate values are unchanged by this transformation.:
Rotation Contd..,
• In homogeneous-coordinate form, the three-dimensional z-axis rotation
equations are,

• Which we can write more compactly as,


Rotation Contd..,
• Transformation equations for rotations about the other two coordinate
axes can be obtained with cyclic permutation of the coordinate
parameters x,y, and z in eqn 1;
x->y->z->x
• Thus, to obtain the x-axis and y-axis rotation transformations, we cyclically
replace x with y, y with z, and z with x, as illustrated in the figure.
• The equation for three-dimensional x-axis rotation:
Rotation Contd..,
• In homogeneous-coordinate form, the three-dimensional x-axis rotation
equations are,

Which we can write more compactly as,


Rotation Contd..,
• The equation for three-dimensional y-axis rotation:

• In homogeneous-coordinate form, the three-dimensional y-axis rotation


equations are,

• Which we can write more compactly as,


Rotation around coordinate axes
Rotation around an axis parallel to x-axis
Rotation around an axis parallel to x-axis
• A rotation matrix for any axis that does not coincide with a coordinate
axis can be set up as a composite transformation involving combinations
of translations and the coordinate-axis rotations.
• We attain the desired rotation with the following transformation
sequence:
• Translate the object so that the rotation axis coincides with the parallel
coordinate axis.
• Perform the specified rotation about that axis.
• Translate the object so that the rotation axis is moved back to its original position
• A coordinate position P is transformed with the sequence as ,
Rotation about an arbitrary axis
• When an object is to be rotated about an axis that is not parallel to one of the
coordinate axes, we must perform some additional transformations.
• The below figure shows the five transformation steps for obtaining a composite matrix
for rotation about an arbitrary axis, with the rotation axis projected onto the z-axis
Rotation about an arbitrary axis
• Given the specifications for the rotation axis and the rotation angle, we
can accomplish the required rotation in 5 steps:
1. Translate the object so that the rotation axis passes through the coordinate
origin.
2. Rotate the object so that the axis of rotation coincides with one of the
coordinate axes.
3. Perform the specified rotation about the selected coordinate axis.
4. Apply inverse rotations to bring the rotation axis back to its original orientation.
5. Apply the inverse translation to bring the rotation axis back to its original spatial
position.
• We can transform the rotation axis onto any one of the three coordinate
axes. The z axis is often a convenient choice.
Rotation about an arbitrary axis

• A rotation axis can be defined with two coordinate positions or with one
coordinate point and direction cosines between the rotation axis and
two of the coordinate axes.
• We assume that the rotation axis is defined by two point, as illustrated
in the figure -initial position, and that the direction of rotation is to be
counter-clockwise when looking along the axis from P2 to P1.
Rotation about an arbitrary axis
• The components of the rotation –axis vector are then computed as:

• Where the components a, b, and c are the direction cosines for the
rotation axis:
Rotation about an arbitrary axis
• The first step in the rotation sequence is to set up the translation matrix that
repositions the rotation axis so that it passes through the coordinate origin.
• The translation matrix is:

• Next, we formulate the transformations that will put the rotation axis(i.e., u
vector) onto the z-axis.
Rotation about an arbitrary axis
FIRST STEP OF ROTATION:
Rotation about ‘x’ by ‘α’. Now we have to determine ‘α’
Project the unit vector u‫׳‬, along OP, into the YZ plane.
The y and z components, b and c are the direction cosines of the unit vector along the arbitrary
axis.
Rotation about an arbitrary axis. Contd..,
• It can be seen from the diagram, by using Right Angle triangle formula we get,

We also need angle ‘α’ to be calculated,

Hence we establish the transformation matrix for rotation around the x-axis by determining
the values for the sine and cosine of the rotation angle necessary to get OP (the vector u)
onto the ‘xz’ plane.
Rotation about an arbitrary axis. Contd..
• Now that we have determined the values for cosα and sineα in terms of the components
of vector ‘u’ (OP), we can set up the matrix elements for rotation of this vector about the
x-axis and into the xz plane.
Rotation about an arbitrary axis. Contd..,
After first step rotation:
Next step in the formulation of the transformation sequence is to determine the matrix that
will swing the unit vector in the xz plane counter-clockwise around the y-axis onto the positive z-axis.

The above figure shows the orientation of the unit vector in the xz plane, resulting from the
rotation about the x-axis.
This vector, labelled u'', has the value a for its x component, because rotation about x-axis leaves the x
component unchanged. Its z component is d (the magnitude of u'),because vector u' has been rotated
on to the z-axis. Also the y component of u'' is 0.
Rotation about an arbitrary axis. Contd..,
• Rotation by ‘β’ about y-axis.
• Now we have to determine ‘β’. Steps is similar to that done for ‘α’.
• Now we need to calculate cosine and sine for the angle ‘β’.
Rotation about an arbitrary axis. Contd..,
• Therefore, the transformation matrix for rotation of u’’ about the y axis is

• With transformation matrices of , and , we have aligned the rotation axis with
the positive z axis.
Rotation about an arbitrary axis. Contd..,
• To complete the required rotation about the given axis, we need to transform the
rotation axis back to its original position.
• This is done by applying the inverse of transformations.
• The transformation matrix for rotation about an arbitrary axis can then be expressed
as the composition of these seven individual transformations:
Scaling
• Change the coordinates of the object by scaling factors.

Scaling an object with this transformation will also move its position relative to the origin
- so move it to the origin, scale it, then move it back...
Scaling with respect to a Fixed Point
Scaling with respect to a Fixed Point
Reflection
• Reflection over planes, lines or points
Shear
• Deform the shape depending on another dimension
OpenGL 3D transformations
• Basic OpenGL Geometric Transformation
• OpenGL Matrix Operations
• OpenGL Matrix Stacks
OpenGL 3D transformations
• In the core library of OpenGL, a separate function is available for each of the basic
geometric transformations.
• OpenGL is designed as a 3D graphics API, where all transformations are specified in
three dimensions.
• Internally all coordinate positions are represented as four-element column vectors
• All transformations are represented using 4X4 matrices.
• Performing 2D transformations within OpenGL is generally just a matter of using a
value for the transformations in the third(z) dimension that cause no change in that
dimension.
OpenGL 3D transformations: glTranslate*()

Basic transformation functions: generate matrix and post-multiply this


with current matrix.
*-> f(float) or d(double)
Translate over [tx, ty, tz]:
glTranslate*(tx, ty, tz);
1.tx,ty and tz -> can be assigned with any real number values
2.For two-dimensional applications,we set tz=0.0;
3.Two-dimensional position is represented as a four-element column
matrix with the z-component equal to 0.0.
Example: glTranslatef(25.0,-10.0,0.0)
OpenGL 3D transformations: glRotate*()
• Rotate over theta degrees (!) around axis [vx, vy, vz]:
glRotate*(theta, vx, vy, vz);
v=(vx,vy,vz)-> Defines the orientation for a rotation axis
Theta-> Rotation angle in degrees,which the routine converts to
radians for the trignometric calculations.
V is the unit vector
• Rotation in 2D system is rotation about the z-axis, specified as a unit
vector with x and y components as zero, and a z component as 1.0
For example., glRotatef(90.0, 0.0, 0.0, 1.0);
OpenGL 3D transformations: glScale*()
Scale axes with factors sx, sy, sz:
glScale*(sx, sy, sz);
• sx,sy,sz-> can assign any real-number values
• Scaling in 2D system involves changes in the x and
y dimensions, and a z scaling factor of 1.0(which
causes no change in the z-coordinate positions)
Example: glScalef(2.0, 3.0, 1.0);

A zero value for any scaling parameter can cause a


processing error, because an inverse matrix cannot be
calculated
OpenGL 3D transformations: glMatrixMode()

OpenGL maintains two matrices:


• GL_PROJECTION
• GL_MODELVIEW
• GL_PROJECTION: The Matrix that is to be used for the
projection transformation. This transformation
determines how a scene is to be projected onto the
screen.
• GL_MODELVIEW: Used to store and combine the geometric
transformations .
OpenGL 3D transformations: glMatrixMode()

The default argument for the glMatrixMode function is


GL_MODELVIEW

Once we are in the MODELVIEW mode (or any other mode), a


call to a transformation routine generates a matrix that
is multiplied by the current matrix for that mode.
OpenGL 3D transformations:
glLoadIdentity() and glLoadMatrix*()

We can assign values to the elements of the current matrix, and there are two
functions in the OpenGL library for this purpose.
1. Initializing the matrix to I: Matrices are specified in
glLoadIdentity(); column-major order:
2. Alternatively, we can assign other values to m[0] m[4] m[8]m[12]
m[1] m[5] m[9] m[13]
the elements of the current matrix using M=
Replace the matrix with elements16 : m[2] m[6] m[10] m[14]
 
• glLoadMatrix*(elements16); m[3] m[7] m[11] m[15]

A Single-subscripted, 16-element array of floating-


point values is specified with parameter elements16 , and
a suffix code of either f or d is used to designate the
data type.
OpenGL 3D transformations:
glLoadIdentity() and glLoadMatrix*()

To illustrate the ordering of matrix, we


initialize the modelview matrix with the
following code:
Example: glMatrixMode (GL_MODELVIEW);
Glfloat elems[16];
Glint k;
for(k=0;k<16;k++)
elems[k]=float(k);
glLoadMatrixf(elems);
OpenGL 3D transformations: glMulMatrix*()
We can also concatenate a specified matrix with the current matrix as
follows:
• glMultMatrix*(otherElements16);
Again, the suffix code is either f or d.
The current matrix is postmultiplied by the matrix specified in
glMultMatrix, and this product replaces the current matrix.
Example:
glMatrixMode (GL_MODELVIEW);
glLoadIdentity() //Sets the Matrix to the Identity.
glMultMatrixf(M2); //Postmultiply identity with matrix M2.
glMultMatrixf(M1); // Postmultiply M2 with matrix M1.
Produces the following current modelview matrix:
M=M2.M1

The first transformation to be applied in this sequence is the last one


specified in the code
glMatrixMode (GL_MODELVIEW);
glColor3f (0.0, 0.0, 1.0);

glRecti (50, 100, 200, 150); // Display blue rectangle.


glLoadIdentity(); // Reset current matrix to identity.

glColor3f (1.0, 0.0, 0.0);


glTranslatef (-200.0, -50.0, 0.0); // Set translation parameters.
glRecti (50, 100, 200, 150);// Display red, translated rectangle.
glLoadIdentity ( );//Reset current matrix to identity.

glRotatef (90.0, 0.0, 0.0, 1.0); //Set 90-deg. rotation about z axis.
glRecti (50, 100, 200, 150); //Display red, rotated rectangle.
glLoadIdentity ( ); // Reset current matrix to identity.

glScalef (-0.5, 1.0, 1.0); // Set scale-reflection parameters.


glRecti (50, 100, 200, 150); // Display red, transformed rectangle.
OpenGL 3D Transformations :
glPushMatrix() and glPopMatrix()

With the following function, we copy the current matrix at the top of the
active stack and store that copy in the second stack position:
glPushMatrix():
• This gives us duplicate matrices at the top two positions of the
stack.

Push Matrix Normal Operation on stack


Second Stack Active Stack Second Stack Active Stack

Copy
Composite Matrix
of LI
Load
Idetity Load Identity Load Identity Load Identity
OpenGL 3D Transformations : glPopMatrix()

• The other stack function is


glPopMatrix():
• Which destroys the matrix at the top of the stack, and
the second matrix in the stack becomes the current
matrix.

Second Stack Active Stack

Load Identity

Load Identity Composite Matrix


Examples 200

150

100
Translated Original Position
Position
50

15010050 50 100 150 200


Scaled-Reflected
Position

Original Position
Original Position
50
50
Rotated Position

15010050 50100150200
15010050 50100150200
Example Program
glMatrixMode (GL_MODELVIEW);
glMatrixMode (GL_MODELVIEW); glColor3f (0.0, 0.0, 1.0);
glColor3f (0.0, 0.0, 1.0); glPushMatrix() //Make copy of Identity (top) matrix
glRecti (50, 100, 200, 150); // Display blue rectangle. glRecti (50, 100, 200, 150); // Display blue rectangle.
glLoadIdentity(); // Reset current matrix to identity.
glColor3f (1.0, 0.0, 0.0); glColor3f (1.0, 0.0, 0.0);
glTranslatef (-200.0, -50.0, 0.0); // Set translation parameters. glTranslatef (-200.0, -50.0, 0.0); // Set translation parameters.
glRecti (50, 100, 200, 150);// Display red, translated rectangle. glRecti (50, 100, 200, 150); // Display red, translated rectangle.
glPopMatrix(); //Throw away the Translation matrix
glLoadIdentity ( );//Reset current matrix to identity. glPushMatrix(); //Make copy of Identity (top) matrix
glRotatef (90.0, 0.0, 0.0, 1.0); //Set 90-deg. rotation about z
axis. glRotatef (90.0, 0.0, 0.0, 1.0); //Set 90-deg. rotation about z axis.
glRecti (50, 100, 200, 150); //Display red, rotated rectangle. glRecti (50, 100, 200, 150); //Display red, rotated rectangle.
glPopMatrix(); //Throw away the rotation matrix
glLoadIdentity ( ); // Reset current matrix to identity.
glScalef (-0.5, 1.0, 1.0); // Set scale-reflection parameters. glScalef (-0.5, 1.0, 1.0); // Set scale-reflection parameters.
glRecti (50, 100, 200, 150); // Display red, transformed glRecti (50, 100, 200, 150); // Display red, transformed rectangle.
rectangle.
Example 2
glPushMatrix(); //Create Matrix1 on the top
glTranslated(100,0,0); //Apply translation to matrix1
drawSphere(); //Draws a sphere with translation<100,0,0)
glPushMatrix(); //Clones matrix 1 to create matrix 2 and push it on the
top
glScaled(2,2,2); //Scales matrix2; doesn’t touch matrix 1
drawSphere(); //Draws a sphere with both translation and scale
glPopMatrix(); //Deletes matrix2 ;matrix1 becomes the top
drawSphere(); //same as the first sphere
glPopMatrix(); //deletes matrix1
Function Description
glTranslate* Specifies translation parameters.
glRotate* Specifies parameters for rotation about any axis
through the origin.
glScale* Specifies scaling parameters with respect to
coordinate origin.
glMatrixMode Specifies current matrix for geometric-viewing
transformations, projection transformations.
glLoadIdentity Sets current matrix to identity.
glLoadMatrix* (elems); Sets elements of current matrix.
glMultMatrix* (elems); Post multiplies the current matrix by the
specified matrix.
Copy the top matrix in the stack and store copy in the
glPushMatrix second stack position.
Erase top matrix in the stack and move second matrix
glPopMatrix to top of stack.
Thank You
Affine Transformations

• A coordinate transformation of the form


x’ = axx x + axy y + axz z + bx
y’ = ayx x + ayy y + ayz z + by
z’ = azx x + azy y + azz z + bz
is called an affine transformation.
• Each of the transformed coordinates x’ , y’ , and z’ is a linear function
of the original coordinates x, y, and z, and parameters aij and k are
constants determined by the transformation type.
Affine Transformations
• Affine transformations (in two dimensions, three dimensions, or
higher dimensions) have the general properties that parallel lines are
transformed into parallel lines, and finite points map to finite points.

• Translation, rotation, scaling, reflection, and shear are examples of


affine trans-formations.

You might also like