Module 2
Module 2
Module 2
MODULE - II
• 2D and 3D graphics with OpenGL: 2D Geometric Transformations:
Basic 2D Geometric Transformations, matrix representations and
homogeneous coordinates, 2D Composite transformations, other 2D
transformations, raster methods for geometric transformations,
OpenGL raster transformations, OpenGL geometric transformations
functions
• 3D Geometric Transformations: Translation, rotation, scaling,
composite 3D transformations, other 3D transformations, OpenGL
geometric transformations functions
• Color models: color model, RGB color model, CMY color model
Transformations
• Transformation means changing some graphics into something else
by applying rules.
• We can have various types of transformations such as translation,
scaling up or down, rotation, shearing, etc.
• When a transformation takes place on a 2D plane, it is called 2D
transformation.
• Transformations play an important role in computer graphics
to reposition the graphics on the screen and change their
size or orientation.
Basic Two-Dimensional Geometric
Transformations
• Geometric transformations ?
• Operations that are applied to the geometric description of an object to change its
position, orientation, or size are called geometric transformations.
• The geometric-transformation functions that are available in all graphics packages
are
• 1. Translation,
• 2. Rotation, and
• 3. Scaling
• Other useful transformation
• routines that are sometimes included in a package are reflection and shearing operations.
2D Translation in Computer Graphics
•2D Translation is a process of moving an object from one position
to another in a two dimensional plane.
•Consider a point object O has to be moved from one position to
another in a 2D plane.
•Let-
• Initial coordinates of the object O = (Xold, Yold)
• New coordinates of the object O after translation = (Xnew, Ynew)
Translation vector or Shift vector = (Tx, Ty)
• Given a Translation vector (Tx, Ty)-
• Tx defines the distance the Xold coordinate has to be moved. Ty defines the distance the Yold
coordinate has to be moved.
In Matrix form, the above translation
equations may be represented as-
Given-
•Old coordinates of the square = A (0, 3), B(3, 3), C(3, 0),
D(0, 0)
•Translation vector = (Tx, Ty) = (1, 1)
Let the new coordinates of corner A = (Xnew, Ynew). Let the new coordinates of corner B = (Xnew, Ynew).
Applying the translation equations, we have- Applying the translation equations, we have-
•Xnew = Xold + Tx = 0 + 1 = 1 •Xnew = Xold + Tx = 3 + 1 = 4
•Ynew = Yold + Ty = 3 + 1 = 4 •Ynew = Yold + Ty = 3 + 1 = 4
•Given-
•Old center coordinates of C = (Xold, Yold) = (1, 4)
•Translation vector = (Tx, Ty) = (5, 1)
•Let the new center coordinates of C = (Xnew, Ynew). Applying
•Xnew = Xold + Tx = 1 + 5 = 6
•Ynew = Yold + Ty = 4 + 1 = 5
•In Matrix form, the above rotation equations may be represented as shown
above
2D Rotation about the origin.
P’(x’,y’)
P(x,y)
x = r.cosφ
r
y = r.sinφ
θ y
r
φ
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
′
x = r.cos(θ+φ) = r.cosφ.cosθ− r.sinφ.sinθ
′
y = r.sin(θ+φ) = r.cosφ.sinθ+ r.sinφ.cosθ
′ ′
Substituting for r x = x.cosθ− y.sinθ y
= x.sinθ+ y.cosθ
x = r.cosφ
y = r.sinφ Rewriting in matrix form gives us :
Gives us : ′
x ⎢ cosθ − sinθ⎥.⎢ x⎥
⎡ ⎤ = ⎡sinθ cosθ ⎤ ⎡y ⎤
′
x = x.cosθ− y.sinθ ⎢⎣ ⎥⎦
′ y′ ⎣ ⎦ ⎣ ⎦
y = x.sinθ+ y.cosθ
⎡cosθ − sinθ
Define the matrix R = ⎢ ⎤, ′
⎥⎦ P = R ⋅
⎣sinθ cosθ
P
• Given a line segment with starting point as (0, 0) and ending point as
(4, 4). Apply 30 degree rotation anticlockwise direction on the line
segment and find out the new coordinates of the line.
Solution-
Applying the rotation equations, we have-
We rotate a straight line by its end points with the same
angle. Then, we re-draw a line between the new end X
new
points. = Xold x cosθ – Yold x sinθ
= 4 x cos30º – 4 x sin30º
Given- = 4 x (√3 / 2) – 4 x (1 / 2)
• Old ending coordinates of the line = (Xold, Yold) = (4, 4) = 2√3 – 2
•Rotation angle = θ = 30º = 2(√3 – 1)
= 2(1.73 – 1)
= 1.46
Let new ending coordinates of the line after rotation
= (Xnew, Ynew).
Y
new
= Xold x sinθ + Yold x cosθ
= 4 x sin30º + 4 x cos30º
= 4 x (1 / 2) + 4 x (√3 / 2)
= 2 + 2√3
= 2(1 + √3)
= 2(1 + 1.73)
= 5.46
Thus, New ending coordinates of the line after rotation
= (1.46, 5. 46).
Assignment:
Given a triangle with corner coordinates (0, 0), (1, 0) and (1,
1). Rotate the triangle by 90 degree anticlockwise direction
and find out the new coordinates.
Rotation of a point about an arbitrary pivot position
class wcPt2D {
public: GLfloat x, y;
};
void rotatePolygon (wcPt2D * verts, GLint nVerts, wcPt2D pivPt,
GLdouble theta)
{
wcPt2D * vertsRot;
GLint k;
for (k = 0; k < nVerts; k++) {
vertsRot [k].x = pivPt.x + (verts [k].x - pivPt.x) * cos
(theta) - (verts [k].y - pivPt.y) * sin (theta);
vertsRot [k].y = pivPt.y + (verts [k].x - pivPt.x) * sin
(theta) + (verts [k].y - pivPt.y) * cos (theta);
}
glBegin {GL_POLYGON};
for (k = 0; k < nVerts; k++)
glVertex2f (vertsRot [k].x, vertsRot [k].y);
glEnd ( );
}
2D Scaling in Computer Graphics-
• In computer graphics, scaling is a process of modifying or altering the size
of objects.
• Scaling may be used to increase or reduce the size of object.
✔ The rotation transformation operator R(θ ) is the 3 × 3 matrix with rotation parameter θ.
Two-Dimensional Scaling Matrix
• A scaling transformation relative to the coordinate origin can
now be expressed as the matrix multiplication
Property 1:
The accumulated error over many steps can become quite large. The
object positions are reset when error accumulation becomes large.
Some animation applications automatically reset object positions at fixed
intervals (eg. 360 or 180 degrees)
Two-Dimensional Rigid-Body Transformation
• If a transformation matrix includes only translation and rotation
parameters, it is a rigid-body transformation matrix.
• Shear
Shearing deals with changing the shape and size of the
2D object along x-axis and y-axis
Problem:
Find the reflection of the triangle (1,1), (1,2),(3,1)
about the x axis
• Figure below illustrates the change in position of an object that
has been reflected about the line x = 0.
Problem:
Find the reflection of the triangle (1,1), (1,2),(3,1)
about the y axis
53
✔ We flip both the x and y coordinates of a point by reflecting
relative to an axis that is perpendicular to the xy plane and that
passes through the coordinate origin the matrix representation
for this reflection is
Problem:
Find the reflection of the triangle (1,1), (1,2),(3,1)
about this plane
• An example of reflection about the origin is shown in
Figure
• 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
57
Raster methods for geometric Transformations
Raster methods for geometric 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
• Few arithmetic operations are needed, so the pixel transformations are particularly efficient.
• 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.
✔ A 180◦ rotation is obtained by reversing the order of the elements in each row of the array, then reversing
the order of the rows.
✔ Figure below demonstrates the array manipulations that can be used to rotate a pixel block by 90◦ and by
180◦.
✔ For array rotations that are not multiples of 90◦, we need to do some extra
processing.
✔ The general procedure is illustrated in Figure below.
❖ To rotate the color values, we rearrange the rows and columns of the color array, as described in the
previous section. Then we put the rotated array back in the buffer with
glDrawPixels (width, height, GL_RGB, GL_UNSIGNED_BYTE, colorArray);
2. Rotation,
3. Scaling,
4. Composite 3D Transformations
•Three-Dimensional 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 following program segment illustrates construction of a
translation matrix, given an input set of translation parameters.
typedef GLfloat Matrix4x4 [4][4]; /* Construct the 4 x 4 identity matrix. */
void matrix4x4SetIdentity (Matrix4x4 matIdent4x4)
{
GLint row, col;
for (row = 0; row < 4; row++)
for (col = 0; col < 4 ; col++)
matIdent4x4 [row][col] = (row == col);
}
void translate3D (GLfloat tx, GLfloat ty, GLfloat tz)
{
Matrix4x4 matTransl3D; /* Initialize translation matrix to identity. */
matrix4x4SetIdentity (matTransl3D);
matTransl3D [0][3] = tx;
matTransl3D [1][3] = ty;
matTransl3D [2][3] = tz;
}
Three-Dimensional Rotation
• positive rotation angles produce counterclockwise rotations about a
coordinate axis when looking along the positive half of the axis toward
the origin.
Three-Dimensional Coordinate-Axis Rotations
Three-Dimensional Coordinate-Axis
Rotations ‘
‘
‘
‘
Along z axis ‘
‘
✔ Transformation equations for rotations about the other two coordinate axes can be obtained with a
cyclic permutation of the coordinate parameters x, y, and z
x → y→ z→ x
General Three-Dimensional Rotations
• To rotate an object about an axis that is parallel to one of
the coordinate axes, the following 3 steps are
performed:
1. Translate the object so that the rotation axis coincides with
the parallel coordinate axis.
2. Perform the specified rotation about that axis.
3. Translate the object so that the rotation axis is moved back to
its original position.
For a coordinate position P,
If an object is to be rotated about an axis that is not parallel to one of
the coordinate axes, we must perform the following 5 steps:
• Translate the object so that the rotation axis passes through the
coordinate origin.
• Rotate the object so that the axis of rotation coincides with one of the
coordinate axes.
• Perform the specified rotation about the selected coordinate axis.
• Apply inverse rotations to bring the rotation axis back to its original
orientation.
• Apply the inverse translation to bring the rotation axis back to its original
spatial position
Scaling
• The matrix expression for the three-dimensional scaling
transformation of a position P = (x, y, z) is given by
i.e.
x' = x · sx, y’ = y · sy, z’ = z · sz