Modeling Transformations

Modeling Transformations

Arranging model elements in OpenGL is conceptually simple, but because there are several ways of looking at things, it can get a little confusing. The cleanest way of looking at things is to assume that the camera is fixed at the origin of a global coordinate system looking along the negative z-axis. Assume that there is a local coordinate system which can be manipulated using the three transformation operations (translation, scaling and rotation). All drawing operations are performed relative to the local coordinate system.

 

Transformation functions

Transformations are applied using transformation functions. the three basic transformation operations are

Scaling

Objects can be scaled along each of the axis using glScale*();

// scale 2 times along x-axis
glScalef(2.0f, 1.0f, 1.0f);


//shrink along z-axis by half
glScalef(1.0f, 1.0f, 0.5f);

//triple size of in all axis
glScalef(3.0f,3.0f,3.0f);

 

Rotation

Use glRotate*() to rotate objetcs about an axis or an arbitary vector;

// Rotate 45 degrees counterclockwise about the x-axis
glRoatef(45.0f,1.0f,0.0f,0.0f);

//Rotate 60 degrees clockwise about the vector (1,1,1)
glRotatef(-60.0f , 1.0f, 1.0f, 1.0f );

 

Translation

glTranslate*() will translate from one place to another;

// Move 2 units along the x-axis, no-move along the y-axis and back 3 units along the z-axis
glTranslatef(2.0f,0.0f,-3.0f);

Cumulative Transformations

This page discusses the effect of cumulative transformations.

 

Simple Transformation Demo

This code (windows .exe) (demonstrates a simple animation using transformation)

void Prog_Loop(){	
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // reset buffers
	glMatrixMode(GL_MODELVIEW); // reset modelview matrix for transformation
	glLoadIdentity();

glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); // Draw Wire frames only GLUquadricObj* obj=gluNewQuadric( ); // prepare to draw built-in spheres and cylinders // comment these lines out to stop animation
static float angle=0.0; angle+=0.2f; // increase angle of rotation glTranslatef(0.0f,0.0f,-15.0f); // move modeling coords back units. glRotatef(40, 1.0f, 0.0f, 0.0f); // rotate everything around x-axis, so we are looking down a little bit on the scene glColor3f(1.0,1.0,1.0); // set color to white gluSphere( obj, 2.0, 10,10); // draw a sphere glTranslatef(4.0f,0.0f,0.0f); // move coordinate system 4 units to the right glPushMatrix(); // "save the current state" glRotatef(angle, 0.0f, 1.0f, 0.0f); // rotate everything which follows around y-axis, // remember, the value of 'angle' is different each time the frame is drawn // this gives the APPEARANCE that the cylinder is rotating
gluCylinder( obj, 0.5, 0.5,3.0,10,10); // draw a cylinder glPopMatrix(); // UNDO any transformation since the last glPushMatrix glTranslatef(.0f,0.0f,-5.0f); // move coordinate system 5 units to the back glRotatef(90, 1.0f, 0.0f, 0.0f); // rotate everything 90 degrees about x-axis gluCylinder( obj, 0.5, 0.5,3.0,10,10); // draw a cylinder SwapBuffers(g_hdc); // bring back buffer foreground
}

ModelView Matrix