Sie sind auf Seite 1von 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
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 ()
{

// backup current model-view matrix


glPushMatrix(); // save current modelview matrix
glLoadIdentity(); // reset modelview matrix

// set to 2D orthogonal projection


//glMatrixMode(GL_PROJECTION); // switch to projection matrix
glPushMatrix(); // save current projection matrix
glLoadIdentity(); // reset projection matrix
//gluOrtho2D(0, 800, 0, 640); // set to orthogonal projection

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

// restore projection matrix


glPopMatrix(); // restore to previous projection
matrix

// restore modelview matrix


glMatrixMode(GL_MODELVIEW); // switch to modelview matrix
glPopMatrix(); // restore to previous modelview
matrix

}
// 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)
{

setCamera(0, 0, 10, 0, 0, 0);

glClearColor(0.0,000,0.0,0.0);

// Display function
void displayFunc(void)
{

glClear(GL_COLOR_BUFFER_BIT);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);// Wireframe


glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

plotAxisLine (); // plot axis line


/************************************************************/
glPushMatrix(); // Scaling

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
}

// rotation at pivot point


else if (modeoptions2 == 4) {
rotX = 0, rotY = 0, rotZ = 1;
glRotated(degree1, rotX, rotY, rotZ);
rotX = 0, rotY = 1, rotZ = 0;
glRotated(degree2, rotX, rotY, rotZ);
rotX = 1, rotY = 0, rotZ = 0;
glRotated(degree3, rotX, rotY, rotZ);
}

//////////////////////////////////////////////////////////////
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

glTranslated(transX, transY, transZ); // perform


translation

if (modeoptions4 == 1)
{
drawCube(); //Draw cube
}
else if (modeoptions4 == 2)
{
drawTriangle();
}

glPopMatrix(); // End translation

//////////////////////////////////////////////////////////////

glPopMatrix(); // End rotation


//////////////////////////////////////////////////////////////

glPopMatrix(); // End scaling


/************************************************************/

if (options == 4)
{
exit(1);
}

glFlush();

// Reshape function
void reshapeFunc(GLint newWidth,GLint newHeight){

windowWidth=newWidth;

windowHeight=newHeight;

// set viewport to be the entire window


glViewport(0, 0, (GLsizei)newWidth, (GLsizei)newHeight);

// set perspective viewing frustum


float aspectRatio = (float)newWidth / newHeight;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// FOV, AspectRatio, NearClip, FarClip


gluPerspective(60.0f, (float)(newWidth)/newHeight, 1.0f, 1000.0f);

// switch to modelview matrix in order to set scene


glMatrixMode(GL_MODELVIEW);

// 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;

// object rotation at pivot point


case GLUT_KEY_F2:
degree1=degree+80;
degree2=degree+30;
degree3=degree+60;
break;

// increase size
case GLUT_KEY_F3:
scale=scale+0.01;
break;

// decrease size
case GLUT_KEY_F4:
scale=scale-0.01;
break;
}
glutPostRedisplay();
}

// Menu function(Translation) into this program


void option (GLint selectedOption)
{
options = selectedOption;

switch(selectedOption)
{
case 4 : options = 4; // Exit
break;
8 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation

glutPostRedisplay();
}

// Submenu (translation) function


void mode (GLint selectedOption)
{
modeoptions = selectedOption;

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();

// Submenu (Rotation) function


void mode2(GLint selectedOption)
{
modeoptions2 = selectedOption;

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();
}

// Submenu (Scaling) function


void mode3(GLint selectedOption)
{
modeoptions3 = selectedOption;

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();
}

// Select shapes submenu


void mode4(GLint selectedOption)
{
modeoptions4 = selectedOption;

switch(selectedOption)
{
case 1 : modeoptions4 = 1;
break;

case 2 : modeoptions4 = 2;
break;
}
}

// Main function
void main (int argc, char** argv)
{

glutInit (&argc, argv);


glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (windowWidth,windowHeight);
glutCreateWindow ("Assignment 4 (Mouse, Menu and Keyboard)");

initialize ();

glutSpecialFunc(specialKey);

glutDisplayFunc (displayFunc);

glutReshapeFunc (reshapeFunc);

// Create sub menu translation


subMenuMode = glutCreateMenu(mode);
glutAddMenuEntry ("X-axis only", 1);
glutAddMenuEntry ("Y-axis only", 2);
// enable translation at z-axis
glutAddMenuEntry ("Z-axis only", 3);
glutAddMenuEntry ("All axes", 4);

// Submenu for rotation


subMenuMode2 = glutCreateMenu(mode2);
glutAddMenuEntry ("X-axis only", 1);
glutAddMenuEntry ("Y-axis only", 2);
// enable rotation on z-axis
glutAddMenuEntry ("Z-axis only", 3);
// enable rotation on pivot point
glutAddMenuEntry ("Rotate at pivot point", 4);

// Submenu for scaling


subMenuMode3 = glutCreateMenu(mode3);
glutAddMenuEntry ("X-axis only", 1);
10 SCV2513 Fundamentals of CG – Tutorial 4: 2D Transformation

glutAddMenuEntry ("Y-axis only", 2);


// enable scaling on z-axis
glutAddMenuEntry ("Z-axis only", 3);
glutAddMenuEntry ("All", 4);

// Submenu for selectShapes


subMenuMode4 = glutCreateMenu(mode4);
glutAddMenuEntry ("Cube", 1);
glutAddMenuEntry ("Triangle", 2);

// Create menu
glutCreateMenu(option);
glutAddSubMenu("Translate", subMenuMode);
glutAddSubMenu("Rotation", subMenuMode2);
glutAddSubMenu("Scaling", subMenuMode3);
glutAddSubMenu("Select Shapes", subMenuMode4);
glutAddMenuEntry("Exit", 4);

// Attach menu right button


glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop ();
}

Das könnte Ihnen auch gefallen