0% found this document useful (0 votes)
59 views

Cube Rotation

The document describes a program that draws and spins a color cube using OpenGL in C++. It defines arrays to store vertex and color values for the cube. A polygon function draws each face of the cube using the values. A colorCube function calls polygon to draw the entire cube. The display function redraws the cube with rotations on the X, Y, and Z axes. SpinCube is called continuously to incrementally rotate the cube on one axis. Mouse buttons select the rotation axis. The program initializes OpenGL and GLUT functions to set up the window, drawing, and spinning behavior.

Uploaded by

Hittu Prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Cube Rotation

The document describes a program that draws and spins a color cube using OpenGL in C++. It defines arrays to store vertex and color values for the cube. A polygon function draws each face of the cube using the values. A colorCube function calls polygon to draw the entire cube. The display function redraws the cube with rotations on the X, Y, and Z axes. SpinCube is called continuously to incrementally rotate the cube on one axis. Mouse buttons select the rotation axis. The program initializes OpenGL and GLUT functions to set up the window, drawing, and spinning behavior.

Uploaded by

Hittu Prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

3. Draw a color cube and spin it using OpenGL transformation matrices.

#include<stdlib.h>
#include<GL/glut.h>
GLfloat vertices[8][3] = { { -1,-1,1 },{ -1,1,1 },{ 1,1,1 },
{ 1,-1,1 },{ -1,-1,-1 },{ -1,1,-1 },{ 1,1,-1 },{ 1,-1,-1 } };//2D array to store the
vertices values
GLfloat colors[8][3] = { { 0,0,0 },{ 1,0,0 },{ 1,1,0 },
{ 0,1,0 },{ 0,0,1 },{ 1,0,1 },{ 1,1,1 },{ 0,1,1 } };
//2D array to store the colour values
void polygon(int a, int b, int c, int d)
{
glBegin(GL_POLYGON);//Draw a polygon with list of vertices
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();
}
void colorCube(void)
{
polygon(3, 2, 1, 0);
polygon(7, 6, 5, 4);
polygon(2, 3, 7, 6);
polygon(4, 5, 1, 0);
polygon(2, 6, 5, 1);
polygon(0, 4, 7, 3);

}
static GLfloat th[] = { 0,0,0 };
static GLint axis = 1; //Default y axis Rotation
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//Clear Frame Buffer and depth
Buffer
glLoadIdentity();
glRotatef(th[0], 1, 0, 0);//Rotate Angle on X axis, glRotatef(angle,x,y,z);
glRotatef(th[1], 0, 1, 0);
glRotatef(th[2], 0, 0, 1);
colorCube();
glFlush();
glutSwapBuffers();//Swapping of Front and Back buffer

}
void spinCube()
{
th[axis] = th[axis] + 0.1;//Controls the speed of rotation
if (th[axis]>360)
th[axis] = th[axis] - 360;
glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
axis = 0;//x axis
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
axis = 1;//y axis
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;//z axis
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2, 2, -2, 2, -10, 10);//Specify the distances to the nearer and far depth
clipping planes.
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("SPINCUBE");
glutInitWindowSize(700, 700);
glutReshapeFunc(myReshape);//The width and height parameters of the callback
glutDisplayFunc(display); specify the new window size in pixels.
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
Output

You might also like