0% found this document useful (0 votes)
25 views80 pages

Geometric Transformations in Graphics

Chapter Five discusses geometric transformations in computer graphics, detailing the various types such as translation, scaling, rotation, shearing, and reflection. It explains how these transformations manipulate the size, position, and orientation of objects using mathematical operations and matrix representations. The chapter also introduces homogeneous coordinates for simplifying transformations in both 2D and 3D graphics.

Uploaded by

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

Geometric Transformations in Graphics

Chapter Five discusses geometric transformations in computer graphics, detailing the various types such as translation, scaling, rotation, shearing, and reflection. It explains how these transformations manipulate the size, position, and orientation of objects using mathematical operations and matrix representations. The chapter also introduces homogeneous coordinates for simplifying transformations in both 2D and 3D graphics.

Uploaded by

Adem Ketema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Chapter Five

Geometric Transformation in
Computer Graphics

10/15/2025 1
Contents

Transformation
Types of transformation
Matrix representation
Transformation in opengl
Resource
[Link]
cs/

10/15/2025 2
What is Transformation?
 Transformation means changing some graphics into something else by applying rules.

 A Cartographer can change the size of charts and topographical maps.

 So if graphics images are coded as numbers, the numbers can be stored in memory.

 These numbers are modified by mathematical operations called as Transformation.

 We are able to change the geometry of each object by changing its size, position or

orientation.

 These operations are called geometric transformations.

 Preimage Transform to Image.

10/15/2025 3
Count…
 Transformations play an important role in computer
graphics to reposition the graphics on the screen and
change their size or orientation.
 The purpose of using computers for drawing is
 To provide facility to user
 To view the object from different angles,
 Enlarging or reducing the scale or shape of object

10/15/2025 4
Cont..
 There are two complementary points of view for describing object

transformation.
 Geometric Transformation: The object itself is transformed relative

to the coordinate system or background. The mathematical statement


of this viewpoint is defined by geometric transformations applied to
each point of the object.
 Coordinate Transformation: The object is held stationary while the

coordinate system is transformed relative to the object. This effect is


attained through the application of coordinate transformations.

10/15/2025 5
Categories of Geometric Transformation
 Rigid Transformation: Does not change the size and shape of
preimage.
Translation
Rotation
Reflection
 Non Rigid Transformation: Change the size but not the shape
of the preimage.
Scaling

10/15/2025 6
1. Translation
 Moving an object in space without changing its
size, shape or orientation is called Translation.
 Here the object is positioned from one coordinate
location to another.
 Translation is a movement of objects without
deformation.
 For translating polygon, each vertex of the
polygon is converted to a new position.
 Objects drawn in new coordinates.
 To translate a point from coordinate position (x, y)
to another (x1 y1)
 we add algebraically the translation distances
Tx and Ty to original coordinate.
10/15/2025 7
• From the above figure, you can write that

• X’ = X + tx
• Y’ = Y + ty
• The pair (tx, ty) is called the translation
vector or shift vector.
10/15/2025 8
The translation pair (Δ X, Δ Y) is called as shift vector.

10/15/2025 9
10/15/2025 10
Examples
1. Translate the points(3,-4) under
translation by 5 units left and 3
units up
2. Translate the triangle points A(3,1),
B(7,-3) and C(1,6) under 6 units left
and 2 units up
3. Try the above two questions by
converting left into right and up to
bottom?

10/15/2025 11
2. Scaling
 Expanding or contracting an object without change its shape or
orientation.
 It is used to alter or change the size of objects.
 Scaling can be achieved by multiplying the original
coordinates of the object with the scaling factor to get the
desired result.

 There are two scaling factors, i.e. λx in x direction λy in y-

direction.

 If λxand λyare equal it is also called as Uniform Scaling.


10/15/2025 12
 If not equal then called as Differential/Non Uniform Scaling.
10/15/2025 13
10/15/2025 14
 If the picture to be enlarged to twice its original size then λx = λy =2.

 If scaling factors are one, then the size of the object will not be reduced or enlarged .

 If λxand λy are not equal then scaling will occur but it will elongate or distort the picture.

 If scaling factors are less than one, then the size of the object will be reduced.

 If scaling factors are higher than one, then the size of the object will be enlarged.

 If scaling factors with values less than one will move the object closer to coordinate origin,

 while a value higher than one will move coordinate position farther from origin.

 A scale value of zero collapses all object coordinates along that axis to zero.

10/15/2025 15
3. Rotation
 Rotation is a process of rotating an object about a fixed point without

change its size and shape


 It is a process of changing the angle of the object which can be clockwise

or anticlockwise, while we have to specify the angle of rotation and


rotation point.
 A rotation point is also called a pivot point.

 The positive value of the rotation angle rotates an object in an anti-

clockwise direction
 while the negative value of the rotation angle rotates an object in a

clockwise direction.
10/15/2025 16
 Initial co-ordinates of the object O = (Xold, Yold)
 Initial angle of the object O with respect to origin

 Rotation angle = θ
 New co-ordinates of the object O after rotation =
(Xnew, Ynew)
10/15/2025 17
10/15/2025 18
10/15/2025 19
 Problem Statement: Given a triangle with corner coordinates (0,

0), (1, 0) and (1, 1). Rotate the triangle by 90 degrees anti-
clockwise direction and find out the new coordinates.
 Solution:

We always rotate a polygon by rotating each of its vertexes with


the same rotation angle.
 Given,

 Old corner coordinates of the triangle = A (0, 0), B (1, 0), C (1, 1)

 Rotation angle = θ = 90º

10/15/2025 20
• For the Co-ordinate A (0, 0)
Let the new coordinates of corner A after rotation =
(Xnew, Ynew).
When we apply the rotation equation, we get-
Xnew = Xold x cosθ – Yold x sinθ
= 0 x cos90º – 0 x sin90º
=0+0
=0
Ynew = Xold x sinθ + Yold x cosθ
= 0 x sin90º + 0 x cos90º
=0+0
=0
• Thus, the new co-ordinates of corner A we get after
rotation is = (0, 0).
10/15/2025 21
• For the Coordinate B (1, 0)
Let the new coordinates of corner B after rotation
= (Xnew, Ynew).
Xnew = Xold x cosθ – Yold x sinθ
= 1 x cos90º – 0 x sin90º
=0–0
=0
Ynew = Xold x sinθ + Yold x cosθ
= 1 x sin90º + 0 x cos90º
=1+0
=1
Thus, the new co-ordinates of corner B that we
get after rotation are = (0, 1).
10/15/2025 22
• For the Coordinate C (1, 1)
Let the new coordinates of corner C after rotation =
(Xnew, Ynew).
Xnew = Xold x cosθ – Yold x sinθ
= 1 x cos90º – 1 x sin90º
=0–1
= -1
Ynew = Xold x sinθ + Yold x cosθ
= 1 x sin90º + 1 x cos90º
=1+0
=1
Thus, the new co-ordinates of corner C we get after
rotation is = (-1, 1).
Thus, the new co-ordinates of the triangle after
rotation
10/15/2025 are = A (0, 0), B (0, 1), C (-1, 1). 23
4. Shearing
 A transformation that slants the shape of an object is called the shear transformation.

 There are two shear transformations X-Shear and Y-Shear.

 One shifts X coordinates values and other shifts Y coordinate values.

 However; in both the cases only one coordinate changes its coordinates and other preserves

its values.

 Shearing is also termed as Skewing.

 X-Shear

 The X-Shear preserves the Y coordinate and changes are made to X coordinates,

 which causes the vertical lines to tilt right or left as shown in below figure.

 Shearing in X axis is achieved by using the following shearing equations-

 Xnew = Xold + Shx x Yold

 Ynew = Yold

10/15/2025 24
cont…

10/15/2025 25
Y-Shear

 The Y-Shear preserves the X coordinates


and changes the Y coordinates which
causes the horizontal lines to transform
into lines which slopes up or down.
 Shearing in Y axis is achieved by using the
following shearing equations-
 Xnew = Xold
 Ynew = Yold + Shy x Xold

10/15/2025 26
Examples
 Given a triangle with points (1, 1), (0, 0) and (1, 0). Apply
shear parameter 2 on X axis and 2 on Y axis and find out the
new coordinates of the object.

10/15/2025 27
5. Reflection

 Flipping an object across a line without changing its size or shape.

 The mirror image can be either about x-axis or y-axis.

 The object is rotated by 180°.

 Types of Reflection:

 Reflection about the x-axis

 Reflection about the y-axis

 Reflection about an axis perpendicular to xy plane and passing through the

origin

 Reflection about line y=x

 Reflection about line y=-x


10/15/2025 28
1. Reflection about x-axis: The object
can be reflected about x-axis with the help of
the following matrix

In this transformation value of x will remain same


whereas
10/15/2025 the value of y will become negative. 29
2. Reflection about y-axis: The object
can be reflected about y-axis with the
help of following transformation matrix.

Here the values of x will be reversed, whereas the value


of10/15/2025
y will remain the same. The object will lie another 30
side
3. Reflection about an axis
perpendicular to xy plane and
passing through origin:
In the matrix of this transformation is
given below

The value of x and y both will be reversed.


This is also called as half revolution about the
10/15/2025 31
4. Reflection about line y=x:
The object may be reflected about line y
= x with the help of following
transformation matrix

5. Reflection about line y=-x: use the above matrix


by replacing 1 with -1
10/15/2025 32
 Example: A triangle ABC is given. The
coordinates of A, B, C are given as
A (3, 4)
B (6, 4)
C (4 8)
 Find reflected position of triangle i.e., to
the x-axis.
a (3, 4) becomes a1 (3, -4)
b (6, 4) becomes b1 (6, -4)
c (4, 8) becomes c1 (4, -8)

10/15/2025 33
Homogenous Coordinates

 To shorten this process, we have to use 3×3


transformation matrix instead of 2×2
transformation matrix. To convert a 2×2 matrix to
3×3 matrix, we have to add an extra dummy
coordinate W.
 Add a 3rd coordinate to every 2D point

– (x, y, w) represents a point at location (x/w, y/w)

– (x, y, 0) represents a point at infinity

– (0, 0, 0) Is not allowed

10/15/2025 34
Count…

 In this system, we can represent all the transformation equations

in matrix multiplication.

 Any Cartesian point P(X, Y) can be converted to homogenous

coordinates by P’ (Xh, Yh, h).

 Importance of homogenous coordinates

 To have combined transformation

 To eliminated intermediate value calculation

 To save required time for transformation


10/15/2025 35
10/15/2025 36
3D Transformation
 Going to 3D, means adding one more
coordinate, the z direction
 All 3D vectors are now expressed as 4-element
columns in homogeneous coordinates
 All transformations become 4X4 matrixes
 Nothing else changes
Transformations can change the object’s
 Position(translation)
 Size(scaling)
 Orientation(rotation)
 Shape(shear)

10/15/2025 37
3D Translation

 Given a 3D object with coordinate points A(0, 3,


1), B(3, 3, 2), C(3, 0, 0), D(0, 0, 0). Apply the
translation with the distance 1 towards X axis, 1
towards Y axis and 2 towards Z axis and obtain
the new coordinates of the object.
 New coordinates of the object = A (1, 4, 3), B(4, 4,
4), C(4, 1, 2), D(1, 1, 2).
10/15/2025 38
3D Scaling
 Initial coordinates of the object O = (Xold, Yold,Zold)
 Scaling factor for X-axis = Sx
 Scaling factor for Y-axis = Sy
 Scaling factor for Z-axis = Sz
 New coordinates of the object O after scaling =
(Xnew, Ynew, Znew)
 This scaling is achieved by using the following
scaling equations-
 Xnew = Xold x Sx
 Ynew = Yold x Sy
 Znew = Zold x Sz

10/15/2025 39
Example Given a 3D object with coordinate
points A(0, 3, 3), B(3, 3, 6), C(3, 0, 1), D(0, 0, 0).
Apply the scaling parameter 2 towards X axis, 3
towards Y axis and 3 towards Z axis and obtain the
new coordinates of the object.

10/15/2025 40
3D Shearing
Shearing in X Axis-
1. Given a 3D triangle
 Xnew = Xold
with points (0, 0, 0),
 Ynew = Yold + Shy x Xold
(1, 1, 2) and (1, 1,
 Znew = Zold + Shz x Xold
3). Apply shear
Shearing in Y Axis-
 Xnew = Xold + Shx x Yold
parameter 2 on X
 Ynew = Yold
axis, 2 on Y axis
 Znew = Zold + Shz x Yold
and 3 on Z axis and
Shearing in Z Axis-
find out the new
 Xnew = Xold + Shx x Zold coordinates of the
 Ynew = Yold + Shy x Zold object.
 Znew = Zold

10/15/2025 41
3D Rotation

 Consider a point object O has to be rotated from one


angle to another in a 3D plane.
 Let-
 Initial coordinates of the object O = (Xold, Yold, Zold)
 Initial angle of the object O with respect to origin = Φ
 Rotation angle = θ
 New coordinates of the object O after rotation = (Xnew,
Ynew, Znew)
 In 3 dimensions, there are 3 possible types of rotation-
 X-axis Rotation
 Y-axis Rotation
 Z-axis Rotation

10/15/2025 42
Cont..
 For X-Axis Rotation-
 Xnew = Xold
Example Given a
 Ynew = Yold x cosθ – Zold x sinθ homogeneous point (1, 2, 3).
 Znew = Yold x sinθ + Zold x Apply rotation 90 degree
cosθ towards X, Y and Z axis and
 For Y-Axis Rotation- find out the new coordinate
 Xnew = Zold x sinθ + Xold x cosθ points.
 New coordinates after
 Ynew = Yold
rotation (1, -3, 2) in the X
 Znew = Zold x cosθ – Xold x sinθ direction
 For Z-Axis Rotation  Thus, New coordinates after
 Xnew = Xold x cosθ – Yold x sinθ rotation = (3, 2, -1) in the Y
direction
 Ynew = Xold x sinθ + Yold x cosθ
 New coordinates after
 Znew = Zold rotation = (-2, 1, 3). in the Z
direction
10/15/2025 43
Rotation in homogenies coordinate form

10/15/2025 44
Rotation and scaling about Arbitrary Axis

 When the object is rotated about an axis that is not parallel to any

one of co-ordinate axis, i.e., x, y, z.

 Then additional transformations are required.

 First of all, alignment is needed, and then the object is being back

to the original position.

Following steps are required for rotation

 Translate the object to the origin

 Rotate object so that axis of object coincide with any of

coordinate axis

 Translate the center of rotation back where its belong

 Note use the same step for scaling in arbitrary point


10/15/2025 45
1. Rotate by  around arbitrary point (a,b)

M T(a, b) R(θ ) T(-a,-b)

2. Scale by sx, sy around arbitrary point (a,b)

M T(a, b) S(sx, sy) T(-a,-b)

10/15/2025 46
Example1: Perform a counterclockwise of
450 rotation of Triangle points
A(2,3),B(5,5) C(4,3) about pivot point
(1,1). The new coordinate is

10/15/2025 47
Matrix composition
 To perform a sequence of transformation such as translation followed by

rotation and scaling, we need to follow a sequential process −


 Translate the coordinates

 Rotate the translated coordinates, and then

 Scale the rotated coordinates to complete the composite transformation.

Ex: find out the coordinates s figure bounded by the coordinates (1,1)(3,4),
(5,7),10,3 when rotated about a point (8,8) by 30 degree in clockwise
direction and scaled 2 unit in the x direction and 3 unit in the y direction

10/15/2025 48
10/15/2025 49
Geometric Transformation in openGL
There are two ways to specify a geometric
transformation:
 Pre-defined transformations: glTranslate,
glRotate and glScale.
 Arbitary transformations by direct specification
of matrices: glLoadMatrix, glMultMatrix
 Parameters
– Translation: translation amount in x, y, z axes
– Rotation: angle, orientation of the rotation axis that
passes through the origin
– Scaling: scaling factors for three coordinates

10/15/2025 50
Basic OpenGL Transformations
 glTranslate {f,d}(dx, dy, dz)
 Causes subsequently defined coordinate
positions to be translated by the vector
(dx,dy,dz), where dx, dy, dz are either
floating-point or double precision numbers.
 objects drawn after this call are translated
 For 2D applications set dz = 0
 glTranslatef(-0.4, -0.1, 0.0);
 glRectf(0.1,0.2,0.4,0.3);

10/15/2025 51
 glRotate {f,d}(angle, vx, vy, vz)
 Causes subsequently defined coordinate
positions to be rotated by angle degrees
about the axis (vx,vy,vz) through the origin.
The angle parameter specifies the angle of
rotation in degrees.
 glRotatef(90, 0.0, 0.0,1.0);
 glRectf(0.1,0.2,0.4,0.3);
 The effect of the above rotation shows the
rectangle rotate 90 degrees about the z-axis

10/15/2025 52
 glScale {f,d}(sx, sy, sz)
 Multiplies the current matrix by a matrix
that stretches, shrinks, or reflects an
object along the axes.
 Each x, y, and z coordinate of every
point in the object is multiplied by the
corresponding argument x, y, or z.
 With the local coordinate system
approach, the local coordinate axes are
stretched, shrunk, or reflected by the x,
y, and z factors, and the associated
object is transformed with them.
10/15/2025 53
 glScale*() is the only one of the three
modeling transformations that changes the
apparent size of an object
 Scaling with values greater than 1.0
stretches an object, and using values less
than 1.0 shrinks it.
 Scaling with a -1.0 value reflects an object
across an axis.
 The identity values for scaling are (1.0, 1.0,
1.0).
 Note: A scale value of zero collapses all
object coordinates along that axis to zero.
 glScalef(-0.5, 1.0, 1.0);
10/15/2025
glRectf(0.1,0.2,0.4,0.3); 54
Transformation order
 Order of transformation dependent
upon position of call in a code.
 Example:
glTranslatef (1.0,0.0,0.0);
glRotatef (45.0,0.0,1.0,0.0);
glScalef (2.0,2.0,2.0)
glBegin ()
glVertex2f (x,y);
glVertex2f(x,f);
glEnd
Transformation element to the object gets
scale first then Rotate Finally Translated.
10/15/2025 55
10/15/2025 56
• Transformations in OpenGl
1. Modeling transformation:-
The position and orientation of the camera
2. Viewing transformation:-
Arrange the object/models/avator in the world
3. Projection transformation:-
Select a camera lens(width, angle, normal or
telescopic) adjust the focus/zoom factor to
set the cameras field of view
4. Viewport transformation :-
Print the photo on a selected area of the
device or paper

10/15/2025 57
The Camera Analogy

 The transformation process used to produce the


desired scene for viewing is analogous to taking a
photograph with a camera. the steps with a
camera (or a computer) might be the following:
 Set up your tripod and point the camera at the
scene (viewing transformation). Arrange the
scene to be photographed into the desired
composition (modeling transformation).
 Choose a camera lens or adjust the zoom
(projection transformation).
 Determine how large you want the final
photograph to be—for example, you might want it
enlarged (viewport transformation).

10/15/2025 58
• Modeling Transformation
Refer to the transformation of models (i.e.,
thescenes, or objects)
Viewing Transformation
 Refer to the transformation on the camera
 Using glTranslate*() and glRotate*()
 Using gluLookAt()
 gluLookAt (eyeX, eyeY, eyeZ, centerX, centerY,
centerZ, upX, upY, upZ)
 eye = (eyeX, eyeY, eyeZ)T: Desired camera
position
 center = (centerX, centerY, centerZ)T: Where
camera is looking
 up = (upX, upY, upZ)T: Camera’s “up” vector
10/15/2025 59
Projection Transformation

 Refer to the transformation from scene to image


 Orthographic projection
 glOrtho (left, right, bottom, top, near, far)
 Perspective projection
 glFrustum (left, right, bottom, top, near, far)
 Notes on openGl transformations Before applying modeling
or viewing
 transformations, need to set
 glMatrixMode(GL_MODELVIEW)
 Before applying projection transformations, need to set
 glMatrixMode(GL_Projection)
 Replacement by either following commands
 glLoadIdentity();
 glLoadMatrix(M);
 Multiple transformations (either in modeling or viewing) are
applied in reverse order
10/15/2025 60
OpenGL Matrix Operations
 OpenGL maintains three sets of matrices for performing various
transformation operations
 Modelview matrix: used for transforming objects in the scene
and for changing the coordinates
 Projection matrix: handles parallel and perspective
projections
 Texture matrix: this is used in specifying how textures are
mapped onto objects
 OpenGL uses the ff. command to set the current matrix
mode
 glMatrixMode(mode);
 Where mode is GL_MODELVIEW, GL_PROJECTION, or GL_ TEXTURE
 To set the matrix mode to modelview :
 glMatrixMode(GL_MODELVIEW);
 To set the matrix mode to projection :
glMatrixMode(GL_PROJECTION);
10/15/2025 61
 Once you are in the model view
mode, a call to a transformation
routine generates a matrix that is
multiplied by the current matrix for
that mode
 Whatever object defined is multiplied
with the current matrix
 The contents of the current matrix
can also be manipulated explicitly

10/15/2025 62
OpenGL Matrix Stacks
• OpenGL has the following basic stack
operations
– glLoadIdentity(): Sets the current matrix to
the identity matrix
– glLoadMatrix*(M): Loads a given matrix over
the current matrix
– glMultMatrix*(M): Multiplies the current
matrix by a given matrix and replaces the
current matrix with this result.
– glPushMatrix(): Pushes a copy of the current
matrix on top the stack
– glPopMatrix(): Pops the current matrix off the
stack
10/15/2025 63
10/15/2025 64
Cont…
 It is the way in which almost all object
transformations are performed in OpenGL or the
basic steps:
(1) Push the matrix stack
(2) Apply (i.e., multiply) all the desired transformation
matrices with the current matrix
(3) Draw your object (the transformations will be
applied automatically), and
(4) Pop the matrix stack

10/15/2025 65
Example: Draw a rotated triangle
glBegin(GL_TRIANGLES)
– glPushMatrix(); // save the current matrix
– glRotatef(45, 0, 0, 1); // rotate by 45 degrees CCW
– glVertex2i(2, 2); //first vertex
– glVertex2i(50, 2); // second vertex
– glVertex2i(50, 50); //third vertex
– glPopMatrix(); // restore the old matrix
glEnd();
 glMatrixMode(GL_PROJECTION); // set projection matrix
 glLoadIdentity(); // initialize to identity
 gluOrtho2D(left, right, bottom top); // set the drawing area
 glMatrixMode(GL_MODELVIEW); // restore Model view
mode
10/15/2025 66
Opengl: Event Driven Programming
 Virtually all interactive graphics programs are
event driven.
 Unlike traditional programs that read from a
standard input file, a graphics program must be
prepared at any time for input from any number
of sources, including the mouse, or keyboard, or
other graphics devises such as trackballs and
joysticks.
 Program Respond to Events
 Events are handled by user defined callback
function.
 Call back must know context and event type
 (passed through variables)
10/15/2025 67
Cont…
Display event is invoked when the system
senses
That the contents of the window need to
be redisplayed, either because:
 The graphics window has completed its
initial creation
 An obscuring window has moved away,
thus revealing all or part of the graphics
window
 The program explicitly requests
redrawing, by calling glutpostredisplay().
10/15/2025 68
10/15/2025 69
Handling Events: Callback Functions
#include <GL/glut.h>
int main(int argc, char** argv){
glutInit(&argc,argv);
...
glutMouseFunc(function); // click mouse
glutKeyboardFunc(function); // keyboard g
glutSpecialFunc(function); // arrows, pgup
glutReshapeFunc(function); // window resize
glutDisplayFunc(function); // draw primitives ...
-------
glutMainLoop(); /* enter event loop */
return 0;
}
10/15/2025 70
Keyboard Event

 We can register callback functions to handle


keyboard inputs for normal and special keys,
respectively
 GLUT also has nice and simple functions that
provide the facility of Keyboard controlling
 glutKeyboardFunc: registers callback handler
for keyboard event.
 void glutKeyboardFunc (void (*func)
(unsigned char key, int x, int y) // key is the
char pressed, e.g., 'a' or 27 for ESC // (x, y) is
the mouse location in Windows' coordinates
when the key is pressed.
10/15/2025
glutKeyboardFunc(function); // keyboard 71
Example
/*void keyboard(unsigned char key, int x, int y)
{
if(key=='r')
{
glColor3f(1.0,0.0,0.0);
}
else if(key=='r')
glColor3f(0.0,1.0,0.0);
else if(key=='b')
glColor3f(0.0,0.0,1.0);
else if(key=='d')
glRotatef(90.0,0.0,0.0,1.0);
10/15/2025 72
10/15/2025 73
void keyboard(unsigned char key, int x, int y)
{
switch(key) {
case 'r':
glColor3f(1.0,0.0,0.0);
break;
case 'g':
glColor3f(0.0,1.0,0.0);
break;
case 'b':
glColor3f(0.0,0.0,1.0);
}
10/15/2025 74
Special keyboard events

 glutSpecialFunc: registers callback


handler for special key (such as arrow
keys and function keys)
 glutSpecialFunc(void (*f)(int key,
int x, int y))
{

}
 glutSpecialFunc(functionname)

10/15/2025 75
Key Definitions:
 GLUT_KEY_F1 thru GLUT_KEY_F12, G
LUT_KEY_LEFT, GLUT_KEY_RIGHT, GL
UT_KEY_UP, GLUT_KEY_DOWN, GLUT
_KEY_PAGE_UP, GLUT_KEY_PAGE_DO
WN, GLUT_KEY_HOME, GLUT_KEY_E
ND, and GLUT_KEY_INSERT
 The GLUT_KEY_* are predefined
constants in glut.h.

10/15/2025 76
Example
Void SpecialKey(int key, int x, int y)
{
switch(key) {
case GLUT_KEY_F1 :
case GLUT_KEY_F3 :
red = 1.0;
green = 0.0; red = 0.0;
blue = 0.0; break; green = 0.0;
case GLUT_KEY_F2 :
blue = 1.0;
red = 0.0;
break;
green = 1.0; }
blue = 0.0; break; }
10/15/2025 77
Mouse events
 Register with glutMouseFunc(void
(*f)(int button, int state, int x, int
y))
 button is
either GLUT_LEFT_BUTTON, GLUT_
MIDDLE_BUTTON,
or GLUT_RIGHT_BUTTON
 state is
either GLUT_UP or GLUT_DOWN
 X, y coordinates of the mouse
relatively to the upper left corner of
the client area of the window.
10/15/2025 78
Example
void mouse(int button, int state, int x, int y)
{
if(button==GLUT_LEFT_BUTTON)
{
WHAT Do you want to happen???
}
else if(button==GLUT_RIGHT_BUTTON)
WHAT Do you want to happen???
}
-------
glutMouseFunc(mouse);
10/15/2025 79
Cont…
 void myMouse(int b, int s, int x, int y) {
 switch (b) { // b indicates the button
 case GLUT_LEFT_BUTTON:
 if (s == GLUT_DOWN) // button pressed
 // ...
 else if (s == GLUT_UP) // button released
 // ...
 break;
 // ... // other button events

10/15/2025 80

You might also like