SEMrush

Friday, 31 March 2017

Different Computer Graphics Operations on Cube

Read Me :-


To compile the program, type
"g++ cube.cpp -lGL -lGLU -lglut"

========================KEY BOARD CONTROLS============================

ESC         : Close the Program
Arrow keys : Rotate the Cube
Shift + Arrow keys : Translate the cube
Ctrl + Up arrow : Expand the cube in all the 3 directions
Ctrl + Down arrow : Shrink the cube in all the 3 directions
========================================================================

Program : -

#include <GL\glut.h>

int r_y = 0; //x-axis rotation angle
int r_x = 0; //y-axis rotation angle
float t_x = 0,t_y = 0, scale = 1;//x-axis translation value,x-axis translation value,scale factor


void drawcube()
{

glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
glBegin(GL_QUADS);

glColor3f(.3, 0, 1);

glVertex3i(1, 1, 1); //front
glVertex3i(1, -1, 1);
glVertex3i(-1, -1, 1);
glVertex3i(-1, 1, 1);

glColor3f(.2, 1, .8);

glVertex3i(1, 1, -1); //back
glVertex3i(1, -1, -1);
glVertex3i(-1, -1, -1);
glVertex3i(-1, 1, -1);

glColor3f(.7, .4, 1);

glVertex3i(1, 1, 1); //lside
glVertex3i(1, 1, -1);
glVertex3i(1, 0 - 1, -1);
glVertex3i(1, -1, 1);

glColor3f(1, .3, .8);

glVertex3i(-1, 1, 1); //rside
glVertex3i(-1, 1, -1);
glVertex3i(-1, 0 - 1, -1);
glVertex3i(-1, -1, 1);

glColor3f(.2, .5, .2);

glVertex3i(1, 1, 1); //top
glVertex3i(-1, 1, 1);
glVertex3i(-1, 1, -1);
glVertex3i(1, 1, -1);

glColor3f(.4, .4, .7);

glVertex3i(1, -1, 1); //bottom
glVertex3i(-1, -1, 1);
glVertex3i(-1, -1, -1);
glVertex3i(1, -1, -1);

glEnd();

//outline-----------------

glEnable(GL_LINE_SMOOTH);
glLineWidth(3.0);
glBegin(GL_LINES);
glColor3f(0,0,0);

glVertex3i(1, 1, 1); //front
glVertex3i(1, -1, 1);
glVertex3i(-1, -1, 1);
glVertex3i(-1, 1, 1);

glVertex3i(1, 1, -1); //back
glVertex3i(1, -1, -1);
glVertex3i(-1, -1, -1);
glVertex3i(-1, 1, -1);

glVertex3i(1, 1, 1); //lside
glVertex3i(1, 1, -1);
glVertex3i(1, 0 - 1, -1);
glVertex3i(1, -1, 1);

glVertex3i(-1, 1, 1); //rside
glVertex3i(-1, 1, -1);
glVertex3i(-1, 0 - 1, -1);
glVertex3i(-1, -1, 1);

glVertex3i(1, 1, 1); //top
glVertex3i(-1, 1, 1);
glVertex3i(-1, 1, -1);
glVertex3i(1, 1, -1);

glVertex3i(1, -1, 1); //bottom
glVertex3i(-1, -1, 1);
glVertex3i(-1, -1, -1);
glVertex3i(1, -1, -1);
glEnd();
glDisable(GL_LINE_SMOOTH);
glutPostRedisplay();

}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
glTranslatef(t_x, t_y, 0);
glScalef(scale, scale, scale);
glRotatef(r_x, 1.0, 0.0, 0.0);
glRotatef(r_y, 0.0, 1.0, 0.0);
drawcube();

glutSwapBuffers();

}

void specialKeys(int key, int x, int y)// for rotating cube
{

int modifiers = glutGetModifiers();
//  Right arrow - increase rotation by 5 degree
switch (key)
{
case GLUT_KEY_RIGHT:r_y += 5; break;
case GLUT_KEY_LEFT:r_y -= 5; break;
case GLUT_KEY_UP:r_x += 5; break;
case GLUT_KEY_DOWN:r_x -= 5; break;

}

//  Request display update
glutPostRedisplay();

}

void key_press(unsigned char key, int x, int y)//for translating and scale
{
switch (key)
{
case 'a':t_x -= .1; break;
case 'f':t_x += .1; break;
case 's':t_y += .1; break;
case 'd':t_y -= .1; break;
case '1':scale += .1; break;
case '2':scale -= .1; break;
}
glutPostRedisplay();
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(200, 20);
glutInitWindowSize(600, 600);
glutCreateWindow("Assignment PartA : CUBE");

glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutKeyboardFunc(key_press);
glMatrixMode(GL_PROJECTION);
glOrtho(-5, 5, -5, 5, -5, 5); //resize view cube size

glutMainLoop();
return 0;
}


No comments:

Post a Comment