Archive Pages Design$type=blogging

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.

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 -

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);
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 10000.0);


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)
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);
glutCreateWindow("Sample 4");
return 0;

Download Full Source code



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
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.
OpenGL Projects
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago