Multimedia Page 4 - Animation in OpenGL for Game Programming using SDL |
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 // Start Of User Initialization. These are just examples { return true; 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); glLoadIdentity(); glBegin(GL_TRIANGLES); glFlush(); 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 glRotatef(angle,0.0f,1.0f,0.0f); 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 glEnd(); angle+=1.0f; 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…
blog comments powered by Disqus |
|
|
|
|
|
|
|