Module 3 PT
Module 3 PT
Transformation
Module 3
Table Of Contents
3.1 Frames in OpenGL
3.2 Modelling a color cube
3.3 Affine Transformations
3.4 Translation, Rotation, Scaling
3.5 Transformation in Homogeneous coordinates
3.6 Concatenation of Transformations
Frames in OpenGL
• OpenGL is based on a pipeline model, the first part of which is a
sequence of operations on vertices.
• We can characterize these geometric operations by a sequence of
transformations or, equivalently, as a sequence of changes of frames
for the objects defined by a user program.
• In a typical application, there are six frames embedded in the
pipeline, although normally the programmer will not see more than a
few of them directly.
• In each of these frames, a vertex has different coordinates.
• The following is the order that the systems occur in the
pipeline
1. Object or model coordinates
2. World coordinates
3. Eye (or camera) coordinates
4. Clip coordinates
5. Normalized device coordinates
6. Window (or screen) coordinates
Geometric modeling
Coordinate Representation
Model Coordinate
• As an example, we could construct a bicycle by defining each of its parts
(wheels, frame, seat, handlebars, gears, chain, pedals) in a separate model-
coordinate frame.
Model Coordinate to World Coordinate
• Then, the component parts are fitted together in world coordinates.
• If both bicycle wheels are the same size, we need to describe only one
wheel in a local-coordinate frame (model-coordinate frame).
• Then the wheel description is fitted into the world-coordinate bicycle
description in two places.
Coordinate Representation
From Model to World
World coordinates
Coordinate Representation
World Coordinate to View Coordinate
• World-coordinate positions are first converted to viewing coordinates
corresponding to the view we want of a scene, based on the position and
orientation of a hypothetical camera.
• The transformation from model coordinates to world coordinates and from
world coordinates to eye coordinates.
• In OpenGL, these transformations are concatenated together into the
model-view transformation, which is specified by the model-view matrix.
Coordinate Representation
From World to View
View
• The image that is produced depends on what the camera
or viewer sees.
• Virtually all graphics systems use a frame whose origin is
the center of the camera’s lens and whose axes are
aligned with the sides of the camera.
• This frame is called the camera frame or eye frame.
Viewing coordinates
Coordinate Representation
From View to Clip
• Once objects are in eye coordinates, OpenGL must check whether
they lie within the view volume.
• If an object does not, it is clipped from the scene prior to
rasterization.
• We need to identify visible surfaces and eliminate picture parts
outside the bounds for the view we want to show on the display
device.
• OpenGL can carry out this process most efficiently if it first carries
out a projection transformation that brings all potentially visible
objects into a cube centered at the origin in clip coordinates.
Coordinate Representation
From View to Clip
Display:
1
Normalized Device Coordinates
0 1
Coordinate Representation
Normal to Screen Coordinates
• Finally, the picture is scan-converted into the refresh buffer of a raster
system for display.
• The coordinate systems for display devices are generally called device
coordinates, or screen coordinates in the case of a video monitor.
• Often, both normalised coordinates and screen coordinates are
specified in the left handed coordinate reference frame.
Coordinate Representation
• An initial modeling-coordinate position (xmc , ymc , zmc ) in this
illustration is transferred to world coordinates, then to viewing and
projection coordinates, then to left-handed normalized coordinates,
and finally to a device-coordinate position (xdc , ydc ) with the
sequence:
Coordinate Representation
Normal to Screen Coordinates
Display on screen:
0
1024 Device Coordinates
768
Interaction
3.2 Modelling A Colored Cube
glBegin(GL_POLYGON)
• Six times, each time followed by four vertices via glVertex and a glEnd, or
we could use
glBegin(GL_QUADS)
The data specifying the location of the vertices contain the geometry and can
be stored as a simple list or array, such as in vertices[8]—the vertex list.
3.2.4 The Color Cube
• We can use the vertex list to define a color cube.
• We assign the colors of the vertices of the color solid.
• We define a function quad to draw quadrilateral polygons specified by pointers into the
vertex list.
• We assign a color for the face using the index of the first vertex.
• Finally, the colorcube specifies the six faces, taking care to make them all outward-
facing as follows:
GLfoat vertices[8][3] = {{-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0},
{1.0,1.0,1.0},{-1.0,-1.0,-1.0}, {-1.0,1.0,-1.0},
{1.0,1.0,-1.0}, {1.0,-1.0,-1.0}};
𝑪𝟒𝟓 𝜷 = 𝟏 − 𝜷 𝑪𝟒 + 𝜷𝑪𝟓
3.2.6Vertex Arrays
• Although we used vertex lists to model our cube,
when we draw the cube we are making many calls to
OpenGL functions.
• If we assign a color to each vertex, we make 60
OpenGL calls: six faces, each of which needs a glBegin,
a glEnd, four calls to glColor, and four calls to glVertex.
• Each call involves overhead and data transfer.
• Vertex arrays provide a method for encapsulating the
information in our data structure such that we can
draw polyhedral objects with only a few function calls.
• They allow us to define a data structure using vertices
and pass this structure to the implementation.
• When the objects defined by these arrays need to be
drawn, we can ask OpenGL to traverse the structure
with just a few function calls.
GLfoat vertices[8][3] = {{-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0},
{1.0,1.0,1.0},{-1.0,-1.0,-1.0}, {-1.0,1.0,-1.0},
{1.0,1.0,-1.0}, {1.0,-1.0,-1.0}};
Now
x s x 0 x
P = S P y = 0 s y y
or .
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
cos − sin
Define the matrix R = , P = R P
sin cos
Scaling
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.
General Two-Dimensional Pivot Point Rotation
3.5 Transformation in 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
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.
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,
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...
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
which transforms
coordinate positions as
Shear Contd…
• An y-direction shear relative to the y axis is produced with the
transformation matrix
3.6 Concatenation Of Transformation
• 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:
• First, we calculate
M = CBA.
• Then, we use this matrix on each point
q = Mp.
3.6.1 Rotation about a Fixed Point
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
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,
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:
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