Home arrow Multimedia arrow Page 4 - Animation in OpenGL for Game Programming using SDL

Animation: a Basic Example - Multimedia

Animation is the backbone of immersive experience in any game. This applies to both 2-D and 3-D games without exception. Hence, in order to create a seamless and immersive game environment, one must first understand the whys and wherefores of animation. One of the advantages that OpenGL has over other graphics and gaming toolkits is that in OpenGL, animation APIs are not low-level.

TABLE OF CONTENTS:
  1. Animation in OpenGL for Game Programming using SDL
  2. The Basics Continued: Coordinate Spaces
  3. Animation APIs in OpenGL
  4. Animation: a Basic Example
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 14
August 28, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The framework developed in the last part will become the testing ground for all the concepts in SDL and OpenGL. So let's use it for animating the triangle created to test the framework. This time only two functions of the framework will have to be used: Initialize() and Draw3D().

First Initialize():

bool Initialize(void) // Any Application & User Initialization Code Goes Here
{
   AppStatus.Visible= true;     // At The Beginning, Our App Is Visible
   AppStatus.MouseFocus= true;  // And Have Both Mouse
   AppStatus.KeyboardFocus = true;// And Input Focus

   // Start Of User Initialization. These are just examples
   angle = 0.0f; // Set The Starting Angle To Zero

     {
        printf("Cannot load graphic:%sn", SDL_GetError() );
        return false;
      }

      return true;        
        // Return TRUE (Initialization Successful)
}

Angle is a global variable of type float. The only functionality of the Initialize function is to set the initial value of the angle to 0.0f. The logic of animation is completely in the Draw3D() function. The following is the code tested in the last part:

void Draw3D(SDL_Surface *S)      // OpenGL drawing code here
{

   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     // clear screen and
     // depth buffer. Screen color has been cleared
     // at init

   glLoadIdentity();
     // reset the modelview matrix

   glBegin(GL_TRIANGLES);
       glVertex3f( 0.0f, 1.0f, 0.0f);
       glVertex3f(-1.0f,-1.0f, 0.0f);
       glVertex3f( 1.0f,-1.0f, 0.0f);
       glEnd();

   glFlush();
      // flush the gl rendering pipelines

   return;

}

To animate the triangle, the first step is to add the glRotate() before the glBegin() function with the parameters being angle as the first parameter, and 1.0f as the second parameter because the triangle has to be rotated around the y-axis:

void Draw3D(SDL_Surface *S)        // OpenGL drawing code here
{
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          
// clear screen and
           // depth buffer. Screen color has been cleared
           // at init

   glRotatef(angle,0.0f,1.0f,0.0f);
          // Rotate The Triangle On The Y axis
   glLoadIdentity();
           // reset the modelview matrix
  
glBegin(GL_TRIANGLES);
       glVertex3f( 0.0f, 1.0f, 0.0f);
       glVertex3f(-1.0f,-1.0f, 0.0f);
      
glVertex3f( 1.0f,-1.0f, 0.0f);
     glEnd();
   glFlush();
          // flush the gl rendering pipelines

   return;
}

To animate the triangle, the rotation should continue. That means the value of the angle has to be increased continuously. That is done after the glEnd() function.

void Draw3D(SDL_Surface *S)        // OpenGL drawing code here
{
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
           // clear screen and
          
// depth buffer. Screen color has been cleared
           // at init
  
glRotatef(angle,0.0f,1.0f,0.0f);// Rotate The Triangle On The Y axis
  
glLoadIdentity();
          
//  reset the modelview matrix
  
glBegin(GL_TRIANGLES);
       glVertex3f( 0.0f, 1.0f, 0.0f);
      
glVertex3f(-1.0f,-1.0f, 0.0f);
       glVertex3f( 1.0f,-1.0f, 0.0f);

     glEnd();

    angle+=1.0f;
   glFlush();
           // flush the gl rendering pipelines

   return;
}

What happens in essence is that Draw3D is called continuously until a quit message is posted. So the value of the angle is increased at the end of calling  the Draw3D() function. So next time when the glRotatef() is called the new angle value is received. And the new θ value then rotates the triangle created. That's all. The following image is what one gets.

The triangle is colored. The method to add color to the object as well as advanced coordinate mathematics including matrix and vector operations will be covered in the next part. So till next time…



 
 
>>> More Multimedia Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MULTIMEDIA ARTICLES

- Basic Lighting in OpenGL and SDL Game Progra...
- Working with Colors in OpenGL for Game Progr...
- Animation in OpenGL for Game Programming usi...
- Game Programming with SDL: Getting Started w...
- Using OpenGL with SDL for Game Programming
- Learning Sound for Game Programming using SDL
- Game Programming using SDL: Raw Graphics and...
- Game Programming using SDL: Getting Started
- Network Radio With Icecast
- Learning To SMILe

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: