You are on page 1of 6

COMPUTER GRAPHICS

LAB # 06
TRANSFORMATIONS

OBJECTIVE: MODELING TRANSFORMATIONS IN OPENGL


• Translation
• Scaling
• Rotation

TRANSFORMATION
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.

TRANSLATION
Translation refers to the shifting of a point to some other place, whose distance with regard
to the present point is known.
A translation moves an object to a different position on the screen. You can translate a point in 2D
by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to get the new coordinate
(X’, Y’).

Translation is done via glTranslate function:

voidgltranslatef (GLfloatx, GLfloaty, GLfloatz)

1
COMPUTER GRAPHICS

SCALING
To change the size of an object, scaling transformation is used. In the scaling process, you either
expand or compress the dimensions of the object. Scaling can be achieved by multiplying the
original coordinates of the object with the scaling factor to get the desired result.

ROTATION

2
COMPUTER GRAPHICS

In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following
figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate
with distance r from the origin.

Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you will get
a new point P’ (X’, Y’).

Rotation is done via glRotatef function:

voidglRotatef(GLfloatangle, GLfloatx, GLfloaty, GLfloatz)


// where angle specifies the rotation in degree, (x, y, z) forms the axis of
rotation.

EXECUTE THE FOLLOWING CODE AND OBSERVE THE RESULTS


#include<GL/glut.h>

void display() { glClear(GL_COLOR_BUFFER_BIT);


glTranslatef(0.2f, -0.3f, 0.0f); // Translate right and down
// glScalef(0.5,1.0,0.5);
// glRotatef(180.0f, 0.0f, 0.0f, 1.0f); // Rotate 180 degree

glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle


glColor3f(1.0f, 0.0f, 0.0f); // Red glVertex2f(-0.3f, -0.2f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f( 0.3f, -0.2f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue

3
COMPUTER GRAPHICS

glVertex2f( 0.0f, 0.3f); glEnd();


glFlush(); }
int main(intargc, char** argv)
{ glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(80, 80);
glutInitWindowSize(600, 500);
glutCreateWindow("A Simple Triangle");
glutDisplayFunc(display);
glutMainLoop();
}

4
COMPUTER GRAPHICS

LAB TASKS

TASK # 1
Write Opengl code that shows the following output:

TASK # 2
Re-write the house-building program implemented in the previous lab in a way that:

• On pressing ‘r’ the house should be moved on right side of the original, keeping original
at its place.
• On pressing ‘l’ the house should be moved on left side of the original, keeping original at
its place.

5
COMPUTER GRAPHICS

You might also like