14
PROGRAM 3 Program to draw a color cube and spin it using OpenGL transformation matrices.

10CSL67 CG LAB PROGRAM 3

Embed Size (px)

Citation preview

PROGRAM 3

Program to draw a color cube and spin it

using OpenGL transformation matrices.

Rotation through Z axis Rotation through X axis Rotation through Y axis

Color cube

0-1,-1, 1

1-1, 1, 1

21, 1, 1

31, -1, 1

4-1,-1, -1

5-1, 1,-1

61, 1, -1

71, -1, -1

GLfloat vertices[8][3] = { {-1.0,-1.0,1.0},{-1.0,1.0,1.0}, {1.0,1.0,1.0}, {1.0,-1.0,1.0},

{-1.0,-1.0,-1.0}, {-1.0,1.0,-1.0}, {1.0,1.0,-1.0}, {1.0,-1.0,-1.0}

};

GLfloat colors[8][3] = { {0.0,0.0,1.0}, {0.0,1.0,1.0}, {1.0,1.0,1.0}, {1.0,0.0,1.0},

{0.0,0.0,0.0},{0.0,1.0,0.0}, {1.0,1.0,0.0}, {1.0,0.0,0.0}

};

Index Vertex

values

Color

values

Color

name

0 -1, -1, 1 0, 0, 1 Blue

1 -1, 1, 1 0, 1, 1 Cyan

2 1, 1, 1 1, 1, 1 White

3 1, -1, 1 1, 0, 1 Magenta

4 -1, -1, -1 0, 0, 0 Black

5 -1, 1, -1 0, 1, 0 Green

6 1, 1, -1 1, 1, 0 Yellow

7 1, -1, -1 1, 0, 0 Red

GLfloat vertices[8][3] = { {-1.0,-1.0,1.0},{-1.0,1.0,1.0}, {1.0,1.0,1.0}, {1.0,-1.0,1.0},

{-1.0,-1.0,-1.0}, {-1.0,1.0,-1.0}, {1.0,1.0,-1.0}, {1.0,-1.0,-1.0}

};

GLfloat colors[8][3] = { {0.0,0.0,1.0}, {0.0,1.0,1.0}, {1.0,1.0,1.0}, {1.0,0.0,1.0},

{0.0,0.0,0.0},{0.0,1.0,0.0}, {1.0,1.0,0.0}, {1.0,0.0,0.0}

};

0 3

1 2

4 7

5 6

#include<GL/glut.h>

GLfloat vertices[8][3] = { {-1.0,-1.0,1.0},{-1.0,1.0,1.0}, {1.0,1.0,1.0}, {1.0,-1.0,1.0}, {-1.0,-1.0,-1.0}, {-1.0,1.0,-1.0}, {1.0,1.0,-1.0}, {1.0,-1.0,-1.0}

};

GLfloat colors[8][3] = {{0.0,0.0,1.0}, {0.0,1.0,1.0}, {1.0,1.0,1.0}, {1.0,0.0,1.0}, {0.0,0.0,0.0},{0.0,1.0,0.0}, {1.0,1.0,0.0}, {1.0,0.0,0.0}

};

GLfloat theta[] = {0.0,0.0,0.0};GLint axis = 2; /* default z-axis rotation – 0(x axis), 1(y axis), 2(z axis)*/

/* draw a polygon via list of vertices */void polygon(int a, int b, int c , int d){

glBegin(GL_POLYGON);glColor3fv(colors[a]);glVertex3fv(vertices[a]);glColor3fv(colors[b]);glVertex3fv(vertices[b]);glColor3fv(colors[c]);glVertex3fv(vertices[c]);glColor3fv(colors[d]);glVertex3fv(vertices[d]);

glEnd();}

Rotation of front facesCounter clockwise

Rotation of back facesClockwise

/* map vertices to faces */ void colorcube(){

polygon(0,3,2,1); // front face – counter clockwisepolygon(4,5,6,7); // back face – clockwise

polygon(2,3,7,6); // front face – counter clockwisepolygon(1,5,4,0); // back face – clockwise

polygon(1,2,6,5); // front face – counter clockwisepolygon(0,4,7,3); // back face – clockwise

}

0 3

1 2

4 7

5 6

/* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glRotatef(theta[0], 1.0, 0.0, 0.0);glRotatef(theta[1], 0.0, 1.0, 0.0);glRotatef(theta[2], 0.0, 0.0, 1.0);

colorcube();glFlush();glutSwapBuffers();

}

/* Idlecallback, spin cube about selected axis */void spinCube(){

theta[axis] += 0.05; // Controls the speedif( theta[axis] > 360.0 ) theta[axis] -= 360.0;glutPostRedisplay();

}/* mouse callback, selects an axis about which to rotate */ void mouse(int btn, int state, int x, int y){if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; // xif(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;}/* state = GLUT_DOWN or GLUT_UP

(x,y) = window coordinates of the mouse at the time of the click*/

void myReshape(int w, int h) {

glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w <= h)

glOrtho(-2.0, 2.0, -2.0 *(GLfloat) h/(GLfloat)w,2.0*(GLfloat) h /(GLfloat) w,-10.0, 10.0);

elseglOrtho(-2.0 *(GLfloat)w /(GLfloat) h, 2.0 *(GLfloat) w /(GLfloat) h,-2.0, 2.0,-10.0, 10.0);

glMatrixMode(GL_MODELVIEW);}

void main(int argc, char **argv){

glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

/* need both double buffering and z buffer */glutInitWindowSize(500, 500);glutCreateWindow("Rotating a Color Cube");glutReshapeFunc(myReshape);glutDisplayFunc(display);glutIdleFunc(spinCube);glutMouseFunc(mouse);glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */glutMainLoop();

}

glutIdleFunc()sets the global idle callback to be func so a GLUT program canperform background processing tasks or continuousanimation when window system events are not beingreceived. If enabled, the idle callback is continuously calledwhen events are not being received. The callback routine hasno parameters. The current window and current menu willnot be changed before the idle callback. Programs withmultiple windows and/or menus should explicitly setthe current window and/or current menu and not rely on itscurrent setting.