Animation in OpenGL for Game Programming using SDL - Animation: a Basic Example (Page 4 of 4 )
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…
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |