You are on page 1of 10

1 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation

OpenGL Tutorial 4 2D Transformation: Translation, Rotation and Scaling
/* Tamimi Tamby * SCV2513 Fundamental of Computer Graphics * GMM Department, FSKSM, UTM * 2009/2010 Session, Section 02 * Tutorial 4: 2D Transformations */ # include<GL/glut.h> // Global Declaration for Translation int transX = 0; int transY = 0; int transZ = 0; // Global Declaration for Rotation int rotX = 0; int rotY = 0; int rotZ = 0; int degree = 0; int degree1, degree2, degree3; // Global Declaration for Scaling float scale = 1; float scaleX = 1; float scaleY = 1; float scaleZ = 1; GLint options; // Translation GLint modeoptions; GLint subMenuMode; // Rotation GLint modeoptions2; GLint subMenuMode2; // Scaling GLint modeoptions3; GLint subMenuMode3; // Select shape GLint modeoptions4; GLint subMenuMode4; //Set window size GLsizei windowWidth = 800, windowHeight = 640; // cube /////////////////////////////////////////////////////////////////////// // v6----- v5 // /| /| // v1------v0| // | | | | // | |v7---|-|v4 // |/ |/ // v2------v3 void drawCube() { glBegin(GL_QUADS); // face v0-v1-v2-v3

1.-1. glColor3f(1.1. glVertex3f(-1.-1. glVertex3f(-1.0). glVertex3f(1. glColor3f(1.0).0).-1).1).-1.-1).1. glVertex3f(1.0.1). glColor3f(1. glVertex3f(1.-1.0.0.-1. glColor3f(1.-1. glColor3f(0. glColor3f(1. glVertex3f(-1.0.1).1. glColor3f(0.0).1.-1).0. glVertex3f(-1. glColor3f(1. // face v1-v6-v7-v2 glNormal3f(-1.1). glVertex3f(-1.1. glColor3f(0. // face v0-v5-v6-v1 glNormal3f(0. glVertex3f(1.1).0). glColor3f(0.0).0.1.1.-1).1.0.0).0).1).-1).-1).-1). glVertex3f(1.1).-1.-1. glVertex3f(-1. glVertex3f(1.0.0).0.1).0.1.0).-1).1).2 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation glNormal3f(0.1.1.1. glVertex3f(-1.1). // face v0-v3-v4-v6 glNormal3f(1.0.0.1.1). glColor3f(0.0. glColor3f(1. glColor3f(0. // face v7-v4-v3-v2 glNormal3f(0.1.-1. glColor3f(1. .-1. glColor3f(0.0.1).1).1). glVertex3f(-1. glColor3f(0.1).0).1).1.1.1). glColor3f(1.-1. glVertex3f(1. glColor3f(1. glVertex3f(1. glColor3f(0. glVertex3f(1.0).0). glColor3f(0.1. glVertex3f(-1.0.1).-1).-1.1).1). glVertex3f(-1.1).1. glVertex3f(1.1).1. // face v4-v7-v6-v5 glNormal3f(0. glColor3f(1.1).1.0).-1. glVertex3f(1. glVertex3f(-1.0). glColor3f(1.1).0.-1). glColor3f(0.0).-1).1.

640). glVertex3f(-1.5.0.1).-1. glColor3f(0.1).1.0). glEnd(). glVertex3f(0.1). glColor3f(1.1).1).5. glColor3f(0. glColor3f(-1.1. // Front // glNormal3f(0.-1).0. glVertex3f(1. // Side // glNormal3f(0.-1.0. glVertex3f(1. glVertex3f(0.0.3 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation glVertex3f(-1. glEnd().5.-1.1).0).-1).5. // save current modelview matrix glLoadIdentity(). glColor3f(0.-1. glColor3f(1. glColor3f(0. glPushMatrix(). glVertex3f(1.320).1).1).0. // X axis glVertex2i (800.-1.1. } // Triangle void drawTriangle() { glBegin(GL_TRIANGLES). } // Draw Axis void plotAxisLine () { // backup current model-view matrix glPushMatrix().1.1).5.1). .0). 640).3. glVertex3f(-1. glEnd().1). //gluOrtho2D(0.1. glColor3f(-1. glVertex3f(2.1.0. glColor3f(1. // Surface // glNormal3f(0. glVertex2i (0. glVertex2i (400.-1.320).-1). glLoadIdentity().0.-1).-1). 0.3.-1. 800.1). // switch to projection matrix // save current projection matrix // reset projection matrix // set to orthogonal projection glBegin (GL_LINES). glVertex3f(2. glColor3f(1.-1). // Y axis glVertex2i (400.-1).1).1). // reset modelview matrix // set to 2D orthogonal projection //glMatrixMode(GL_PROJECTION).0.1).1.5. glVertex3f(1.5.

0. matrix // switch to modelview matrix // restore to previous modelview } // Add setup camera function /////////////////////////////////////////////////////////////////////////// void setCamera(float posX.y. float targetX. scaleY = scale. float targetY. gluLookAt(posX. focal(x. // eye(x. float targetZ) { glMatrixMode(GL_MODELVIEW).0. scaleZ = 1. targetZ. up(x.z). } else if (modeoptions3 == 2) { scaleX = 1.0. 0). posY. targetX. glDisable(GL_CULL_FACE). float posZ. // Scaling if (modeoptions3 == 1) { scaleX = scale. 0. 0. GL_LINE). // restore to previous projection matrix // restore modelview matrix glMatrixMode(GL_MODELVIEW). } else if (modeoptions3 == 3) { . 0). glLoadIdentity().y.0. float posY.y.// Wireframe glDisable(GL_DEPTH_TEST). glPolygonMode(GL_FRONT_AND_BACK.0). plotAxisLine (). 0. targetY.4 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation // restore projection matrix glPopMatrix().000. 1. 0. glPopMatrix(). scaleY = 1. posZ. glClearColor(0.z) } void initialize(void) { setCamera(0. } // Display function void displayFunc(void) { glClear(GL_COLOR_BUFFER_BIT). scaleZ = 1. // plot axis line /************************************************************/ glPushMatrix().z). 10.

} else if (modeoptions3 == 4) { scaleX = scale. scaleZ = scale.// perform rotation } // rotation at pivot point else if (modeoptions2 == 4) { rotX = 0. rotZ). // translate at Y axis } else if (modeoptions == 2) { transX = 0. rotX. rotY. scaleY = 1. } glScaled(scaleX. rotX = 0. } = 1. rotX = 1. rotY = 1. rotY = 1. glRotated(degree. rotY = 0. rotZ = 1. rotY. rotZ = 0. rotY. = 0. rotX. rotX. rotY = 0. rotY = 0.// perform rotation } else if (modeoptions2 == 2) { rotX = 0. // translate at X axis } else if (modeoptions == 3) { transZ = 0. rotZ).scaleY. glRotated(degree. rotX. rotX. rotY. // Translation if (modeoptions == 1) { transY = 0. rotZ glRotated(degree3. rotZ glRotated(degree1. rotZ). rotY. rotZ). rotZ glRotated(degree2. scaleY = scale. // Rotation if (modeoptions2 == 1) { rotX = 1. = 0. rotX. rotZ).scaleZ). rotZ). ////////////////////////////////////////////////////////////// glPushMatrix(). // rotation at z-axis glRotated(degree. scaleZ = scale. // translate at Z axis .5 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation scaleX = 1.// perform rotation } else if (modeoptions2 == 3) { rotX = 0. ////////////////////////////////////////////////////////////// glPushMatrix(). // rotation at x-axis rotY = 0. rotY. // rotation at y-axis rotZ = 0.

} glFlush(). (GLsizei)newHeight). // End rotation ////////////////////////////////////////////////////////////// glPopMatrix(). 1000.6 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation } glTranslated(transX.0f. int x. // set perspective viewing frustum float aspectRatio = (float)newWidth / newHeight. // switch to modelview matrix in order to set scene glMatrixMode(GL_MODELVIEW). (GLsizei)newWidth. (float)(newWidth)/newHeight.GLint newHeight){ windowWidth=newWidth.0f. NearClip. transY. FarClip gluPerspective(60. // End scaling /************************************************************/ if (options == 4) { exit(1). // perform translation if (modeoptions4 == 1) { drawCube(). // set viewport to be the entire window glViewport(0.0f). // FOV. 1. windowHeight=newHeight. transZ). 0. int y) { switch(key) { . } // Reshape function void reshapeFunc(GLint newWidth. } glPopMatrix(). glLoadIdentity(). } // Keyboard function void specialKey(int key. glMatrixMode(GL_PROJECTION). //Draw cube } else if (modeoptions4 == 2) { drawTriangle(). AspectRatio. // End translation ////////////////////////////////////////////////////////////// glPopMatrix().

// object rotation at pivot point case GLUT_KEY_F2: degree1=degree+80.01. break. // translation on x-axis case GLUT_KEY_LEFT: transX--. break. // object rotation case GLUT_KEY_F1: degree=degree+20. degree2=degree+30. break. break. break. degree3=degree+60. case GLUT_KEY_F6: transZ--. case GLUT_KEY_DOWN: transY--. // Exit break. switch(selectedOption) { case 4 : options = 4. // increase size case GLUT_KEY_F3: scale=scale+0.7 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation // translation on y-axis case GLUT_KEY_UP: transY++. break. break. // translation on z-axis case GLUT_KEY_F5: transZ++. break. // decrease size case GLUT_KEY_F4: scale=scale-0. . } glutPostRedisplay(). break. break.01. } // Menu function(Translation) into this program void option (GLint selectedOption) { options = selectedOption. case GLUT_KEY_RIGHT: transX++.

break. } // Submenu (Scaling) function void mode3(GLint selectedOption) { modeoptions3 = selectedOption. } // Submenu (Rotation) function void mode2(GLint selectedOption) { modeoptions2 = selectedOption. 3. switch(selectedOption) { case 1 : modeoptions3 = 1. break. switch(selectedOption) { case 1 : modeoptions2 = 1. 2. break. // submenu for z-axis case 3 : modeoptions = break. // submenu for z-axis 1. break. case 4 : modeoptions = break. break. case 2 : modeoptions3 = 2. switch(selectedOption) { case 1 : modeoptions = break. } glutPostRedisplay().8 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation } glutPostRedisplay(). . break. case 2 : modeoptions2 = 2. } glutPostRedisplay(). 4. } // Submenu (translation) function void mode (GLint selectedOption) { modeoptions = selectedOption. // submenu for pivot point case 4 : modeoptions2 = 4. case 2 : modeoptions = break. // submenu for z-axis case 3 : modeoptions2 = 3.

9 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation case 3 : modeoptions3 = 3. 1). glutAddMenuEntry ("All axes". // Submenu for rotation subMenuMode2 = glutCreateMenu(mode2). char** argv) { glutInit (&argc. switch(selectedOption) { case 1 : modeoptions4 = 1. } // Select shapes submenu void mode4(GLint selectedOption) { modeoptions4 = selectedOption. // Submenu for scaling subMenuMode3 = glutCreateMenu(mode3). 1). 3). 3). glutAddMenuEntry ("Y-axis only". initialize (). glutAddMenuEntry ("X-axis only". glutAddMenuEntry ("Y-axis only". break. . glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB). 4). glutInitWindowSize (windowWidth. glutCreateWindow ("Assignment 4 (Mouse. glutAddMenuEntry ("X-axis only". case 4 : modeoptions3 = 4. glutInitWindowPosition (0. glutReshapeFunc (reshapeFunc). glutAddMenuEntry ("X-axis only". } glutPostRedisplay(). break. glutSpecialFunc(specialKey). Menu and Keyboard)"). glutDisplayFunc (displayFunc). } } // Main function void main (int argc. 4). 2). break. 1). argv). case 2 : modeoptions4 = 2.windowHeight). break. 2). // enable translation at z-axis glutAddMenuEntry ("Z-axis only". // Create sub menu translation subMenuMode = glutCreateMenu(mode). 0). // enable rotation on pivot point glutAddMenuEntry ("Rotate at pivot point". // enable rotation on z-axis glutAddMenuEntry ("Z-axis only".

1). glutMainLoop (). 2). subMenuMode4).10 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation glutAddMenuEntry ("Y-axis only". 4). // enable scaling on z-axis glutAddMenuEntry ("Z-axis only". 4). glutAddMenuEntry ("Triangle". } . glutAddSubMenu("Select Shapes". glutAddMenuEntry ("Cube". glutAddMenuEntry("Exit". subMenuMode2). glutAddSubMenu("Translate". // Create menu glutCreateMenu(option). glutAddSubMenu("Rotation". 2). subMenuMode). // Submenu for selectShapes subMenuMode4 = glutCreateMenu(mode4). 3). subMenuMode3). glutAddMenuEntry ("All". // Attach menu right button glutAttachMenu(GLUT_RIGHT_BUTTON). glutAddSubMenu("Scaling".