## Monday, March 18, 2013

### Learn how to make a Simple 3D Rotating Cube using OpenGL

In this tutorial we are going to learn about "spinning cube animation" or a '3D Rotating Cube". First we think how to animate? The answer - by allowing continuously display the picture with different frames shown, little by little and giving the appearance of motion.

First we declare the following -

#include<GL/glut.h>
#include<GL/gl.h>
static GLfloat rot;

The header are included to access the OpenGL libraries. Then there is variable for the angle of rotation of the cube. Next we call the display function which will have the matrix that holds the vertices as well the color values in them.

static GLfloat vert[]={
{ 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},
};

static GLfloat color[]={
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 1.0, 1.0, 0.0},
{1.0, 0.0, 1.0, 0.0},
{1.0, 1.0, 0.0, 0.0},
};

Rest display function will have the transformation functions and drawing of the cubes. Store the vertices of the cube by calling the glPushMatrix(). It pushes the matrix values on the stack (different stacks for glMatrixmode modes). Move all vertices by vector called on glTranslatef() and rotate by the declared angled calling on glRotatef() toward the axis coded.

glTranslatef(0.0, 0.0, -15.0);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -5.0);
glRotatef(rot*2, 1.0, 0.0, 0.0);

Next is to draw the cubes using the glBegin(GL_QUADS) by allocating the proper vertices and the colors that has been declared before at beginning of the display function. Then Pop the matrix that is on the stack with glPopMatrix();

void reshape_func(int width, int height)
{

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 10000.0);

glMatrixMode(GL_MODELVIEW);
}

As mentioned in the above there are many modes for  glMatrixmode one is GL_PROJECTION which is to set the projection like widening the camera lenses and GL_MODELVIEW is for the setting up the object that is to be drawn, like size and place in space. The glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. glLoadIdentity() replaces the current matrix with the identity matrix. glFrustum() describes a perspective matrix that produces a perspective projection.

void idle_func(void)
{
rot=0.1*(GLfloat)glutGet(GLUT_ELAPSED_TIME);
glutPostRedisplay(); //re-draw the screen glut
}

In the idle_func we chnage the angle by time by time by calling the glutGet which returns the amount of time (in milliseconds) from the time you call the () glutInit. The glutPostRedisplay() re-draw the screen. Below is the main function is self descriptive if you have code any sample program in OpenGL.

int main(int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("Sample 4");
glutDisplayFunc(display_func);
glutReshapeFunc(reshape_func);
glutIdleFunc(idle_func);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}

1. 2. 