0% found this document useful (0 votes)
28 views3 pages

OpenGL Cube Rotation Tutorial

The document describes drawing and spinning a color cube using OpenGL in C++. It defines vertex and color arrays for an 8-vertex cube. A polygon function draws the cube faces by specifying vertices and colors. A colorCube function calls polygon to draw the cube. The display function redraws the cube with rotations around the X, Y, or Z axis depending on mouse input. It spins the cube by incrementing the rotation angle over time.

Uploaded by

Pramoda S
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)
28 views3 pages

OpenGL Cube Rotation Tutorial

The document describes drawing and spinning a color cube using OpenGL in C++. It defines vertex and color arrays for an 8-vertex cube. A polygon function draws the cube faces by specifying vertices and colors. A colorCube function calls polygon to draw the cube. The display function redraws the cube with rotations around the X, Y, or Z axis depending on mouse input. It spins the cube by incrementing the rotation angle over time.

Uploaded by

Pramoda S
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

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);
}
void 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 specify the new
window size in pixels.
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}

Output

You might also like