Find REAL3D on facebook

OpenGL Tutorial - 2

Render a animation in OpenGL? or how to animate an object in OpenGL? or real-time animation of an object in OpenGL etc?

In this tutorial you are going to learn

  • How can you animate an object in OpenGL?
  • What is Idle function "glutIdleFunc()" of OpenGL?

  • I am assuming that you have successfully compiled your first program using my OpenGLTest.cpp file or using my Visual Studio project. Now what you need to do is just copy the below code and replace it with display() function of the OpenGLTest.cpp file. You also need to declare one variable below the header files because I have mentioned earlier, declration of variables and functions are very important if you don't want to see any kind of error in MS VS.

    From here you can download the *.cpp file of this tutorial. The file contains a full OpenGL program in which a Cube and a teapot is rotating.


    File Name Type Size Release Date
    OpenGLTest_Animation *.cpp 5.55 KB 2012.06.19

    
    /********************************************************************
    	Created:	2012/06/19
    	Created:	19:6:2012   10:43
    	Filename: 	OpenGLTest_Animation.cpp
    	Author:		Furqan Ullah
        	Website:	www.real3d.pk
    	
    	Disclaimer:	This tutorial has been written for learning purposes only.
    *********************************************************************/
    
    float Rot_Cangle_animate = 0.0f; // declaration of first variable
    void ModelRotation(void); // declaration of a function
    
    /************************************************************************/
    /* This is the main OpenGL function. Whatever you want to draw in your OpenGL application, you will need to write in this function only. */
    /* This is a real-time activated function, this function operates like a loop function,  */
    /************************************************************************/
    void display(void)
    {
    	glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // you can apply background color by setting RBG values here.
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	
    	glPushMatrix();
    		glRotatef(15.0, 1.0, 0.0, 0.0);
    		glRotatef(30.0, 0.0, 1.0, 0.0);
    		glRotatef(0, 0.0, 0.0, 1.0);
    
    		glutIdleFunc(ModelRotation); // Idle function of OpenGL, idle function means it will never stop until the application is terminated or your stop by your self.
    		glRotatef(Rot_Cangle_animate, 1.0f, 0.0f, 0.0f); // apply rotation around X-axis
    		glRotatef(Rot_Cangle_animate, 0.0f, 0.0f, 1.0f); // rotation around Z-axis
    
    		glColor3f(1.0f, 1.0f, 1.0f); // apply color to object (Cube)
    		glutWireCube(30.0);
           		//glutWireTeapot(15.0); // if you want to render a teapot, just uncomment it.
           		//glutWireTorus(5.0, 20.0, 20.0, 20.0); // how about to render a torus, have fun 
    	glPopMatrix();
    
    	glutSwapBuffers();
    }
    
    /************************************************************************/
    /* This is how the model rotation works, we increment the angle value by 0.3 after each frame and  */
    /* when the value becomes more than 360 deg, we decrement it to zero.,  */
    /************************************************************************/
    void ModelRotation(void)
    {
    	Rot_Cangle_animate += 0.3f;
    	if (Rot_Cangle_animate > 360.0f)
    		Rot_Cangle_animate = Rot_Cangle_animate - 360.0f;
    
    	glutPostRedisplay(); // It is very important for any kind of animation in OpenGL. We must apply it to there when we want to refresh the scene.
    }
    
    

    OpenGLTest.cpp Output

    Output result of OpenGLTest_Animation.cpp file.



    You have learned your Second lesson just in 5 minutes. Isn't it easy?, yea I think so. Now spend some more time on rendering different objects by replacing the Cube and Teapot. GLUT provides you many pre-built models, you do not need to write any kind of code just use OpenGL builtin functions. Good Luck.



    Discussion: