Multimedia
  Home arrow Multimedia arrow Page 4 - Animation in OpenGL for Game Programmi...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
OLM
 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: 4 stars4 stars4 stars4 stars4 stars / 6
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    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.

       · In this article I have discussed about using OpenGL API for animation using SDL for...
     

       

    MULTIMEDIA ARTICLES

    - 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway