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
2
1, 1, 1
3
1, -1, 1
4
-1,-1, -1
5
-1, 1,-1
6
1, 1, -1
7
1, -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 faces
Counter clockwise
Rotation of back faces
Clockwise
/* map vertices to faces */
void colorcube()
{
polygon(0,3,2,1); // front face – counter clockwise
polygon(4,5,6,7); // back face – clockwise
polygon(2,3,7,6); // front face – counter clockwise
polygon(1,5,4,0); // back face – clockwise
polygon(1,2,6,5); // front face – counter clockwise
polygon(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 speed
if( 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; // x
if(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);
else
glOrtho(-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 can
perform background processing tasks or continuous
animation when window system events are not being
received. If enabled, the idle callback is continuously called
when events are not being received. The callback routine has
no parameters. The current window and current menu will
not be changed before the idle callback. Programs with
multiple windows and/or menus should explicitly set
the current window and/or current menu and not rely on its
current setting.

10CSL67 CG LAB PROGRAM 3

  • 1.
    PROGRAM 3 Program todraw a color cube and spin it using OpenGL transformation matrices.
  • 2.
    Rotation through Zaxis Rotation through X axis Rotation through Y axis
  • 3.
    Color cube 0 -1,-1, 1 1 -1,1, 1 2 1, 1, 1 3 1, -1, 1 4 -1,-1, -1 5 -1, 1,-1 6 1, 1, -1 7 1, -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} };
  • 4.
    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
  • 5.
    #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)*/
  • 6.
    /* draw apolygon 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(); }
  • 7.
    Rotation of frontfaces Counter clockwise Rotation of back faces Clockwise
  • 8.
    /* map verticesto faces */ void colorcube() { polygon(0,3,2,1); // front face – counter clockwise polygon(4,5,6,7); // back face – clockwise polygon(2,3,7,6); // front face – counter clockwise polygon(1,5,4,0); // back face – clockwise polygon(1,2,6,5); // front face – counter clockwise polygon(0,4,7,3); // back face – clockwise } 0 3 1 2 4 7 5 6
  • 9.
    /* 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(); }
  • 10.
    /* Idlecallback, spincube about selected axis */ void spinCube() { theta[axis] += 0.05; // Controls the speed if( 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; // x if(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 */
  • 11.
    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); else glOrtho(-2.0 *(GLfloat)w /(GLfloat) h, 2.0 *(GLfloat) w /(GLfloat) h,-2.0, 2.0,-10.0, 10.0); glMatrixMode(GL_MODELVIEW); }
  • 12.
    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(); }
  • 14.
    glutIdleFunc() sets the globalidle callback to be func so a GLUT program can perform background processing tasks or continuous animation when window system events are not being received. If enabled, the idle callback is continuously called when events are not being received. The callback routine has no parameters. The current window and current menu will not be changed before the idle callback. Programs with multiple windows and/or menus should explicitly set the current window and/or current menu and not rely on its current setting.