## Archive Pages Design\$type=blogging

Tutorial to code "spinning cube animation" or a '3D Rotating Cube" in OpenGL C++ Computer graphics with full sourcecode.

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[][4]={
{ 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[][4]={
{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;
}

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm BÃ©zier curves Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero Doll DSAV Escapa Fighter Jet First Come First Serve Flag Flag Hoisting flag of usa Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Martyr’s Monument Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Programming OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Queuqe Random Flowing lines Ray Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Shaheed Minar Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Space Invaders Sphere Spot Light Swing Stack Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Trippy Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: Learn how to make a Simple 3D Rotating Cube using OpenGL
Learn how to make a Simple 3D Rotating Cube using OpenGL
Tutorial to code "spinning cube animation" or a '3D Rotating Cube" in OpenGL C++ Computer graphics with full sourcecode.
http://4.bp.blogspot.com/-KqUoskplAWg/UUaSPT7Lu8I/AAAAAAAAE1U/Cfp5M56eCjw/s1600/3d+spinning+cube+tutorial.jpg
http://4.bp.blogspot.com/-KqUoskplAWg/UUaSPT7Lu8I/AAAAAAAAE1U/Cfp5M56eCjw/s72-c/3d+spinning+cube+tutorial.jpg
OpenGL Projects
https://www.openglprojects.in/2013/03/learn-simple-3d-cubes-rotation-opengl.html
https://www.openglprojects.in/
https://www.openglprojects.in/
https://www.openglprojects.in/2013/03/learn-simple-3d-cubes-rotation-opengl.html
true
2020194800836807725
UTF-8