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 -


#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);
glLoadIdentity();
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;
}

Download Full Source code

COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve 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 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 Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation 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
http://www.openglprojects.in/2013/03/learn-simple-3d-cubes-rotation-opengl.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2013/03/learn-simple-3d-cubes-rotation-opengl.html
true
2020194800836807725
UTF-8
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