Chp3 - Transformation
Chp3 - Transformation
Transformations
Basic transformation
Old New
A(0,0) A(0,0)
B(5,0) B(7.5,0)
C(5,5) C(7.5,10)
Arbitrary Scaling Pivot
• Translate the object so that P will coincide with the origin(-px, -py)
• Rotate the object: R(q)
• Translate the object back: T(px,py)
• As a matrix multiplication
• p’ = T[px,py] * R[q] * T[-px, -py] * P
• OpenGL manages two 4 × 4 transformation matrices: the modelview
matrix (Model related operations: glBegin, glEnd,glTranslate, glRotate,
glScale, gluLookAt), and the projection
Matrix(glViewport,gluPerspective/glOrtho/glFrustum…).
• Whenever you specify geometry (using glVertex), the vertices are
transformed by the current modelview matrix and then the current
projection matrix.
• Hence, you don’t have to perform these transformations yourself. You
can modify the entries of these matrices at any time. OpenGL
provides several utilities for modifying these matrices.
• The modelview matrix is normally used to represent geometric
transformations of objects; the projection matrix is normally used to store
the camera transformation.
• To modify the current matrix, first specify which matrix is going to be
manipulated: use glMatrixMode(GL MODELVIEW) to modify the modelview
matrix.
• The modelview matrix can then be initialized to the identity with
glLoadIdentity().
• The matrix can be manipulated by directly filling its values, multiplying it by
an arbitrary matrix, or using the functions OpenGL provides to multiply the
matrix by specific transformation matrices (glRotate, glTranslate, and
glScale).
• OpenGL provides a stack to assist with hierarchical transformations.
There is one stack for the modelview matrix and one for the
projection matrix. OpenGL provides routines for pushing and popping
matrices on the stack.
Eg. The following example demonstrates using openGL transformation functions
#include<iostream.h> void display()
#include <GL/glut.h> //rotation of green rectangle specified by the angle
{ glClear(GL_COLOR_BUFFER_BIT);
#include<math.h> glPushMatrix();
//yellow triangle translated by (i,i)
float i=0.1; glLoadIdentity();
glPushMatrix();
float j=-0.1; glRotatef(angle, 0.0f, 0.0f, 1.0f);
glLoadIdentity();
float angle=30; glColor3f(0.0,1.0,0.0);
glTranslatef(i, i,0.0);
float scale =0.1; glBegin(GL_TRIANGLES);
glColor3f(0.8,0.8,0.0);
void update(int value) { glVertex2f(0,0.5);
glBegin(GL_TRIANGLES);
i += 0.2; glVertex2f(-0.5,0.0);
glVertex2f(0,0.5);
j-=0.2; glVertex2f(0.5,0.0);
glVertex2f(-0.5,0.0);
if(i>1) glEnd();
glVertex2f(0.5,0.0);
i=0; glPopMatrix();
glEnd();
if(j<-1) glPopMatrix();
j=0;
angle+=50;
if(angle>360)
angle-=360;
scale+=0.1;
if (scale>0.6)
scale=0.1;
glutPostRedisplay();
glutTimerFunc(1000, update, 0);}
//scaling of red triangle by the point scale
glPushMatrix();
glLoadIdentity(); //rotation ,translation and scaling of a triangle at once
glScalef(scale, scale, 0.0f);
glPushMatrix();
glColor3f(1.0,0.0,0.0);
glBegin(GL_TRIANGLES); glLoadIdentity();
glVertex2f(0,0.5); glRotatef(angle, 0.0f, 0.0f, 1.0f);
glVertex2f(-0.5,0.0); glTranslatef(i, i,0.0);
glVertex2f(0.5,0.0); void init()
glScalef(scale, scale, 0.0f);
glEnd(); {glClearColor(0.0, 0.0, 0.0, 0.0);}
glColor3f(1.0,1.0,1.0);
glPopMatrix(); int main(int argc, char** argv)
glBegin(GL_TRIANGLES);
{ glutInit(&argc,argv);
glVertex2f(0,0.5);
glutInitWindowSize(500, 500);
glVertex2f(-0.5,0.0);
glutCreateWindow("simple");
glVertex2f(0.5,0.0);
glutDisplayFunc(display);
glEnd();
init();
glPopMatrix();
glFlush();} glutTimerFunc(1000, update, 0);
glutMainLoop();}