0% found this document useful (0 votes)
381 views40 pages

21cs63 CG Vtu

Uploaded by

chandans
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)
381 views40 pages

21cs63 CG Vtu

Uploaded by

chandans
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/ 40

2D and 3D graphics with

OpenGL
Thaseen Bhashith
Dept of CSE
JNNCE
2D Geometric Transformations

Basic Two-Dimensional Geometric Transformations

1. Translation
2. Rotation
3. Scaling
Translation
• Translation on a single coordinate point by adding offsets to its
coordinates to generate a new coordinate position.
• Consider point( x, y ), to translate it to new point with offset (tx, ty)
Rotation

• Consider the point( x, y ),


• The original coordinates of the point in polar coordinates are
x = r cos φ,
y = r sin φ

• The transformed coordinates are


expressed in terms of angles θ and φ as

x1 = r cos(φ + θ) = r cos φ cos θ − r sin φ sin θ


y1= r sin(φ + θ) = r cos φ sin θ + r sin φ cos θ
x1 = x cos θ − y sin θ
y1 = x sin θ + y cos θ
The rotation matrix can be

P 1= =
Scaling
• To alter the size of an object, apply a scaling transformation.
• A simple two dimensional scaling operation is performed by multiplying
object positions (x, y) by scaling factors sx and sy to produce the
transformed coordinates (x1 , y1 ):
x1 = x · sx, y1= y · sy
Rotation of a point about an arbitrary pivot position

• Translations, rotations are rigid-


body transformations that
move objects without
Rotating a point from position (x, y) to deformation.
position (x1 , y1 ) through an angle θ
about rotation point (xr , yr) • Every point on an object is
rotated through the same
angle.
Scaling of a point about an arbitrary pivot position
• It is possible to control the location of a scaled object by choosing a position, called
the fixed point, that is to remain unchanged after the scaling transformation.
• Coordinates for the fixed point, (xf, yf ), are often chosen at some object position,
such as its centroid.
• For a coordinate position (x, y), the scaled coordinates (x1,y1 ) are then calculated
from the relationships:
Matrix Representations and Homogeneous Coordinates

• The three basic two-dimensional transformations: translation, rotation,


and scaling can be expressed in the general matrix form
P1= M1 · P + M2
• With coordinate positions P and P1 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. For translation, M1
is the identity matrix.
• For rotation or scaling, M2 contains the translational terms associated with
the pivot point or scaling fixed point
Homogeneous Coordinates
• Multiplicative and translational terms for a two-dimensional geometric
transformation can be combined into a single.
• Use the third column of a transformation matrix for the translation terms,
and all transformation equations can be expressed as matrix multiplications.
• Also there is a need to expand the matrix representation for a two-
dimensional coordinate position to a three-element column matrix.
• 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
Two-Dimensional Translation Matrix

Two-Dimensional Scaling
Matrix

Two-Dimensional Rotation Matrix


Two-Dimensional Composite Transformations
• Using matrix representations, 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 referred to as a
concatenation, or composition, of matrices.
• Because a coordinate position is represented with a homogeneous
column matrix, do pre multiply the column matrix by the matrices
representing any transformation sequence.
• Also, because many positions in a scene are typically transformed by
the same sequence, it is more efficient to first multiply the
transformation matrices to form a single composite matrix.
• Thus, if there is a need to apply two transformations to point position
P, the transformed location would be calculated as
Composite Two-Dimensional Translations

Composite Two-Dimensional Rotation


Composite Two-Dimensional Scalings
General Two-Dimensional Pivot-Point Rotation
• To generate a two-dimensional rotation about any other pivot point (xr, yr) by
perform the following sequence of translate-rotate-translate operations:
1. Translate the object so that the pivot-point position is moved to the coordinate
origin.
2. Rotate the object about the coordinate origin.
3. Translate the object so that the pivot point is returned to its original position
General Two-Dimensional Fixed-Point Scaling

• To illustrate a transformation sequence to produce a two-dimensional


scaling with respect to a selected fixed position (xf , yf ), when there is a
function that can scale relative to the coordinate origin only. This sequence
is:
1. Translate the object so that the fixed point coincides with the coordinate
origin
2. Scale the object with respect to the coordinate origin.
3. Use the inverse of the translation in step (1) to return the object to its
original position.
General Two-Dimensional Fixed-Point Scaling

A transformation
sequence for scaling
an object with respect
to a specified fixed
position using the
scaling matrix S(sx , sy )
General Two-Dimensional Composite Transformations and Computational
Efficiency
• The elements of the composite transformation matrix are
Other Two-Dimensional Transformations

• Other Two-Dimensional Transformations are:

1. Reflection and

2. Shear
Reflection

• A transformation that produces a mirror image of an object is called a


reflection.
• For a two-dimensional reflection, the image is generated relative to an
axis of reflection by rotating the object 180◦ about the reflection axis
• Reflection about the line y = 0 (the x axis) is accomplished with the
transformation matrix
• A reflection about the line x = 0 (the y axis) flips x coordinates while
keeping y coordinates the same. The matrix for this transformation is:
Reflection about the origin
The reflection axis as the diagonal line y = x,
the reflection matrix is:
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 y values


A unit square is converted to a parallelogram using the
x-direction shear matrix with shx= 2.
x-direction shears relative to other reference lines with
shear parameter value of 1 2 relative to the line yref = −1.
The matrix is:

A unit square (a) is transformed to a shifted


parallelogram (b) with sh x = 0.5 and yref = −1
A y-direction shear relative to the line x = xref is
generated with the transformation matrix is:

A unit square (a) is turned into a shifted


parallelogram (b) with parameter values sh y =
0.5 and xref = −1 in the y-direction
Raster Methods for Geometric Transformations

• Raster systems suggest an 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.
• Few arithmetic operations are used, so the pixel transformations
becomes 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.
• Routines for performing some raster operations are available in a
graphics package.
Translating an object from screen position (a) to the destination
position shown in (b) by moving a rectangular block of pixel values.
Coordinate positions Pmin and Pmax specify the limits of the
rectangular block to be moved, and P0 is the destination reference
position.
• Rotations in 90-degree increments are accomplished easily by
rearranging the elements of a pixel array.
• When a two-dimensional object or pattern is rotated by 90◦
counterclockwise by reversing the pixel values in each row of the
array, then interchanging rows and columns.
• 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.
• The array manipulations that can be used to rotate a pixel block by
90◦ and by 180◦ is:
Rotating an array of pixel values. The original array is shown in
(a), the positions of the array elements after a 90◦
counterclockwise rotation are shown in (b), and the positions of
the array elements after a 180◦ rotation are shown in (c).
• Similar methods is used to scale a block of pixels.
• Pixel areas in the original block are scaled, using specified values
for sx and sy, and then mapped onto a set of destination pixels.
• The color of each destination pixel is then assigned according to its
area of overlap with the scaled pixel areas

Mapping destination pixel areas onto a scaled array of pixel values. Scaling
factors sx = sy = 0.5 are applied relative to fixed point (x f , y f ).
OpenGL Raster Transformations
• The OpenGL functions for performing raster operations are available.
• A translation of a rectangular array of pixel-color values from one buffer area to
another can be accomplished in OpenGL using:
glCopyPixels (xmin, ymin, width, height, GL_COLOR);-→color values to be copied
• To rotate a block of pixel-color values in 90-degree increments by first saving
the block in an array, then rearranging the elements of the array and placing it
back in the refresh buffer. A block of RGB color values in a buffer can be saved
in an array with the function
glReadPixels (xmin, ymin, width, height, GL_RGB, GL_UNSIGNED_BYTE, colorArray);
OpenGL Raster Transformations
• To rotate the color values, rearrange the rows and columns of the color array, then
the rotated array is put back in the buffer with
glDrawPixels (width, height, GL_RGB, GL_UNSIGNED_BYTE, colorArray);
• The lower-left corner of this array is placed at the current raster position.
• Select the source buffer containing the original block of pixel values with
glReadBuffer(), and designate a destination buffer with glDrawBuffer().
OpenGL Functions for Two-Dimensional Geometric Transformations

• A 4× 4 translation matrix is constructed with the following routine:


glTranslate* (tx, ty, tz);
• Translation parameters tx, ty, and tz can be assigned any real-number
values
• Similarly, a 4 × 4 rotation matrix is generated with
glRotate* (theta, vx, vy, vz);
• To obtain a 4 × 4 scaling matrix with respect to the coordinate origin with
the following routine:
glScale* (sx, sy, sz);
• The glMatrixMode routine is used to set the projection mode, which
designates the matrix that is to be used for the projection transformation.
• This transformation determines how a scene is to be projected onto the screen.
• The same routine is used to set up a matrix for the geometric transformations.
• glMatrixMode (GL_MODELVIEW);
• glLoadIdentity ( );
• glLoadMatrix* (elements16);
• glMultMatrix* (otherElements16);

You might also like