Multimedia
  Home arrow Multimedia arrow Page 4 - Animation in OpenGL for Game Programming using SDL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MULTIMEDIA

Animation in OpenGL for Game Programming using SDL
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2007-08-28


    Table of Contents:
  • Animation in OpenGL for Game Programming using SDL
  • The Basics Continued: Coordinate Spaces
  • Animation APIs in OpenGL
  • Animation: a Basic Example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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…



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

       

    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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT