Beruflich Dokumente
Kultur Dokumente
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;
GLint options;
// Translation
GLint modeoptions;
GLint subMenuMode;
// Rotation
GLint modeoptions2;
GLint subMenuMode2;
// Scaling
GLint modeoptions3;
GLint subMenuMode3;
// Select shape
GLint modeoptions4;
GLint subMenuMode4;
// cube
///////////////////////////////////////////////////////////////////////
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
void drawCube()
{
glBegin(GL_QUADS);
// face v0-v1-v2-v3
2 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
glNormal3f(0,0,1);
glColor3f(1,1,1);
glVertex3f(1,1,1);
glColor3f(1,1,0);
glVertex3f(-1,1,1);
glColor3f(1,0,0);
glVertex3f(-1,-1,1);
glColor3f(1,0,1);
glVertex3f(1,-1,1);
// face v0-v3-v4-v6
glNormal3f(1,0,0);
glColor3f(1,1,1);
glVertex3f(1,1,1);
glColor3f(1,0,1);
glVertex3f(1,-1,1);
glColor3f(0,0,1);
glVertex3f(1,-1,-1);
glColor3f(0,1,1);
glVertex3f(1,1,-1);
// face v0-v5-v6-v1
glNormal3f(0,1,0);
glColor3f(1,1,1);
glVertex3f(1,1,1);
glColor3f(0,1,1);
glVertex3f(1,1,-1);
glColor3f(0,1,0);
glVertex3f(-1,1,-1);
glColor3f(1,1,0);
glVertex3f(-1,1,1);
// face v1-v6-v7-v2
glNormal3f(-1,0,0);
glColor3f(1,1,0);
glVertex3f(-1,1,1);
glColor3f(0,1,0);
glVertex3f(-1,1,-1);
glColor3f(0,0,0);
glVertex3f(-1,-1,-1);
glColor3f(1,0,0);
glVertex3f(-1,-1,1);
// face v7-v4-v3-v2
glNormal3f(0,-1,0);
glColor3f(0,0,0);
glVertex3f(-1,-1,-1);
glColor3f(0,0,1);
glVertex3f(1,-1,-1);
glColor3f(1,0,1);
glVertex3f(1,-1,1);
glColor3f(1,0,0);
glVertex3f(-1,-1,1);
// face v4-v7-v6-v5
glNormal3f(0,0,-1);
glColor3f(0,0,1);
glVertex3f(1,-1,-1);
glColor3f(0,0,0);
glVertex3f(-1,-1,-1);
glColor3f(0,1,0);
3 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
glVertex3f(-1,1,-1);
glColor3f(0,1,1);
glVertex3f(1,1,-1);
glEnd();
}
// Triangle
void drawTriangle()
{
glBegin(GL_TRIANGLES);
// Side //
glNormal3f(0,0,1);
glColor3f(0,-1,1);
glVertex3f(0,3,-1);
glColor3f(-1,1,1);
glVertex3f(2.5,1,-1);
glColor3f(1,0,1);
glVertex3f(1.5,-1,1);
// Surface //
glNormal3f(0,0,1);
glColor3f(0,0,1);
glVertex3f(-1.5,-1,1);
glColor3f(-1,0,-1);
glVertex3f(1.5,-1,1);
glColor3f(1,1,1);
glVertex3f(2.5,1,-1);
// Front //
glNormal3f(0,0,1);
glColor3f(1,0,0);
glVertex3f(-1.5,-1,1);
glColor3f(1,0,1);
glVertex3f(1.5,-1,1);
glColor3f(0,-1,0);
glVertex3f(0,3,-1);
glEnd();
// Draw Axis
void plotAxisLine ()
{
glBegin (GL_LINES);
glVertex2i (0,320); // X axis
glVertex2i (800,320);
glVertex2i (400,0); // Y axis
glVertex2i (400,640);
glEnd();
4 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
}
// Add setup camera function
///////////////////////////////////////////////////////////////////////////
void setCamera(float posX, float posY, float posZ, float targetX, float
targetY, float targetZ)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(posX, posY, posZ, targetX, targetY, targetZ, 0, 1, 0); //
eye(x,y,z), focal(x,y,z), up(x,y,z)
}
void initialize(void)
{
glClearColor(0.0,000,0.0,0.0);
// Display function
void displayFunc(void)
{
glClear(GL_COLOR_BUFFER_BIT);
if (modeoptions3 == 1) {
scaleX = scale;
scaleY = 1;
scaleZ = 1;
}
else if (modeoptions3 == 2) {
scaleX = 1;
scaleY = scale;
scaleZ = 1;
}
else if (modeoptions3 == 3) {
5 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
scaleX = 1;
scaleY = 1;
scaleZ = scale;
}
else if (modeoptions3 == 4) {
scaleX = scale;
scaleY = scale;
scaleZ = scale;
}
glScaled(scaleX,scaleY,scaleZ);
//////////////////////////////////////////////////////////////
glPushMatrix(); // Rotation
if (modeoptions2 == 1) {
rotX = 1; // rotation at x-axis
rotY = 0;
rotZ = 0;
glRotated(degree, rotX, rotY, rotZ);// perform rotation
}
else if (modeoptions2 == 2) {
rotX = 0;
rotY = 1; // rotation at y-axis
rotZ = 0;
glRotated(degree, rotX, rotY, rotZ);// perform rotation
}
else if (modeoptions2 == 3) {
rotX = 0;
rotY = 0;
rotZ = 1; // rotation at z-axis
glRotated(degree, rotX, rotY, rotZ);// perform rotation
}
//////////////////////////////////////////////////////////////
glPushMatrix(); // Translation
if (modeoptions == 1) {
transY = 0; // translate at Y axis
}
else if (modeoptions == 2) {
transX = 0; // translate at X axis
}
else if (modeoptions == 3) {
transZ = 0; // translate at Z axis
6 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
if (modeoptions4 == 1)
{
drawCube(); //Draw cube
}
else if (modeoptions4 == 2)
{
drawTriangle();
}
//////////////////////////////////////////////////////////////
if (options == 4)
{
exit(1);
}
glFlush();
// Reshape function
void reshapeFunc(GLint newWidth,GLint newHeight){
windowWidth=newWidth;
windowHeight=newHeight;
// Keyboard function
void specialKey(int key, int x, int y)
{
switch(key)
{
7 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
// translation on y-axis
case GLUT_KEY_UP:
transY++;
break;
case GLUT_KEY_DOWN:
transY--;
break;
// translation on x-axis
case GLUT_KEY_LEFT:
transX--;
break;
case GLUT_KEY_RIGHT:
transX++;
break;
// translation on z-axis
case GLUT_KEY_F5:
transZ++;
break;
case GLUT_KEY_F6:
transZ--;
break;
// object rotation
case GLUT_KEY_F1:
degree=degree+20;
break;
// increase size
case GLUT_KEY_F3:
scale=scale+0.01;
break;
// decrease size
case GLUT_KEY_F4:
scale=scale-0.01;
break;
}
glutPostRedisplay();
}
switch(selectedOption)
{
case 4 : options = 4; // Exit
break;
8 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
glutPostRedisplay();
}
switch(selectedOption)
{
case 1 : modeoptions = 1;
break;
case 2 : modeoptions = 2;
break;
// submenu for z-axis
case 3 : modeoptions = 3;
break;
case 4 : modeoptions = 4;
break;
}
glutPostRedisplay();
switch(selectedOption)
{
case 1 : modeoptions2 = 1;
break;
case 2 : modeoptions2 = 2;
break;
// submenu for z-axis
case 3 : modeoptions2 = 3;
break;
// submenu for pivot point
case 4 : modeoptions2 = 4;
break;
}
glutPostRedisplay();
}
switch(selectedOption)
{
case 1 : modeoptions3 = 1;
break;
case 2 : modeoptions3 = 2;
break;
// submenu for z-axis
9 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation
case 3 : modeoptions3 = 3;
break;
case 4 : modeoptions3 = 4;
break;
}
glutPostRedisplay();
}
switch(selectedOption)
{
case 1 : modeoptions4 = 1;
break;
case 2 : modeoptions4 = 2;
break;
}
}
// Main function
void main (int argc, char** argv)
{
initialize ();
glutSpecialFunc(specialKey);
glutDisplayFunc (displayFunc);
glutReshapeFunc (reshapeFunc);
// Create menu
glutCreateMenu(option);
glutAddSubMenu("Translate", subMenuMode);
glutAddSubMenu("Rotation", subMenuMode2);
glutAddSubMenu("Scaling", subMenuMode3);
glutAddSubMenu("Select Shapes", subMenuMode4);
glutAddMenuEntry("Exit", 4);
glutMainLoop ();
}