Thread: C++ OpenGL bird animation

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    16

    C++ OpenGL bird animation

    Hey everyone,

    So right now I am trying to get a bird to basically flap around and be able to control it by using the keys awsd etc.

    I know how to traverse the object etc and import it, the only real problem I am having right now is trying to figure out how to articulate the object.

    By Articulate I mean make it so that I can get the bird to flap its wings as it flies rather than just be a static object zooming through the air.

    I believe it uses articulation but I am unable to find anything that relates to it.

    Just wondering if anyone could please point me in the right direction?

    Thanks!!!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, if a 2D game, use a set of images that represent the bird in each state ( wings up, wings middle, wings down, etc ) and keep changing them in a constant rate.
    Now, if a 3D game, then you'll have to use one of the 3D modelling tools that are out there and model and animate the bird as much as you like.
    If you don't know many things about modelling and animation i suggest that you search them on the internet. ( google them )
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    16
    It's 3D and the problem is I need to use DH notation to animate it, this basically steps through each vector and changes it by rotating it by the parents angle plus its own extra angle so that each outflung vector actually rotates by more than the parent if that makes sense

    But my problem is trying to figure out how to get the vectors split up etc so that i have the body etc etc if that makes sense :S

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Why getting into so mush fuss? The only vector you need is the one that will show the bird's direction and speed. You then find the angle between that vector and the origin vector, (0, 0, -1) in OpenGL, you compute the cross product vector of those two and then you rotate the bird around that vector, by using OpenGL commands.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    16
    That is fine for moving the bird but what I am trying to do is get his wings to rotate so that they look like they are flapping.

    In essence I need to lock the wings to the body and then rotate them around the body so that they go up and down no matter what the bird is doing.. ><

  6. #6
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Which modelling program are you using to create your models? A while ago I wrote some code which did something similar using milkshape models (although the model I used was of an ant which did different things such as walk). The different animation 'frames' are saved within the file, and in my program I opened the model, read the data out into a data structure and then for each frame in the animation did a clear-draw-wait cycle. My point is, all the information about the movement of the birds wings should be stored in the model file as a series of 3D 'pictures'. Your program should not calculate the movements of the wings themselves - although it could, it may be a bit more of a challenge than just drawing the model in your 3D coordinate system using glVertex3f() calls etc and then rendering with OpenGL. If you are lucky the file format specification of the models you create using your modelling program has been made public. In which case you could write a model loader (if you haven't already), and if you are even luckier, then someone else could have already written a model loader for you in C or C++

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    16
    Ohhhhhh

    So basically I browse through the object file, for each movement of the wing I set it as a count essentially, then go through the count to make it rotate down, then reverse through the count again to make it rotate up?

    I am not exactly sure if this is what they mean.. I will have to check because I have been going along with the assumption that this had to be manipulated all through opengl using a static object but that is a bit insane now that I think of it!!!

    BTW its using c++ and I can use any object type as long as I can import it into C++ opengl

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You need to use keyframes and skinning to get this working correctly. Keyframes represent the model in key poses and then you linearlily interpolate between these poses. Keyframes can be mixed in that you can have a keyframe of a skeleton waving and one of it walking and then combine the two to produce a walking/waving animation.

    Your bird needs to be represented as a skeleton of bones. You then use hierarchical transformations to orient each bone. Skin/Bone weights are used to determine how much of a bone's orientation affects vertices. This allows the mesh to skin smoothly as the underlying bones move around.

    A basic hierarchical transform for one claw of the bird might look like:

    ClawWorldMatrix = Root * UpperLeg * LowerLeg * Claw;

    For a human right hand it might look like:
    HandWorldMatrix = Root * Torso * UpperRightArm * LowerRightArm * RightWrist * RightHand;

    Google mesh skinning and 3D skeletal mesh animation for more information.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    16
    Hey Bubba

    That makes sense

    What do you think of the code I have?
    Code:
    #include <GL/glut.h>
    #include <GL/glu.h>
    #include <math.h>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class artObject{
    
      artObject** children;
      int numChildren;
      GLdouble linkLength;
      GLdouble theta;
      GLdouble twist;
      GLdouble displacement;
      GLdouble thetaMax;
      GLdouble thetaMin;
    
      GLdouble thetaInc;
      GLdouble direction; 
    
    public:
      artObject(ifstream &fin):thetaInc(0.02),direction(1.0){
        fin >> numChildren;
        fin >> linkLength >> theta >> twist >> displacement >> thetaMax >> thetaMin;
    
        if (numChildren>0){ 
          children = new artObject*[numChildren];
          for(int i=0; i<numChildren; i++)
    	children[i] = new artObject(fin);
        }
      };
    
      void traverse(){
        glPushMatrix();
        transform();
        draw();
        for (int i=0; i<numChildren; i++)
          children[i]->traverse();
        glPopMatrix();
      };
    
      void transform(){
        glRotatef(theta,0.0,0.0,1.0);
        glTranslatef(0,0,displacement);
        glTranslatef(linkLength,0.0,0.0);
        glRotatef(twist,1.0,0.0,0.0);
      };
    
      void draw(){
        glBegin(GL_LINES);
        glColor3f(1.0,0,0);
        glVertex3d(-5,0,0);
        glVertex3d(5,0,0);
        glColor3f(0,1.0,0);
        glVertex3d(0,-5,0);
        glVertex3d(0,5,0);
        glColor3f(0,0,1.0);
        glVertex3d(0,0,-5);
        glVertex3d(0,0,5);
        glEnd();
      };
    
      void update(){
        theta = theta + direction*thetaInc;
        if (theta<thetaMin || theta>thetaMax) direction *= -1;
        for (int i=0; i<numChildren; i++)
          children[i]->update();
      };
    
    };
    
    
    
    
    
    
    
    
    
    
    
    /* OpenGL stuff */
    
    GLdouble fov = 35.0;
    GLdouble aspect = 4/3.0;
    GLdouble dnear = 100.0, dfar = 200.0;
    GLint winWidth = 1200, winHeight=(GLint) winWidth/aspect;
    
    artObject *ao;
    
    void init()
    {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glShadeModel(GL_SMOOTH); 
      glEnable(GL_DEPTH_TEST);
      glClearColor(0.5,0.5,0.5,1.0);
    };
    
    void display(void)
    {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();
      glTranslated(0.0,0.0,-190.0);
      ao->traverse();
      glutSwapBuffers();
    }
    
    void reshape(int w, int h)
    {
      glViewport(0,0,w,h);
      aspect = ((GLdouble) w)/h;
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(fov,aspect,dnear,dfar);
      glMatrixMode(GL_MODELVIEW);
    }
    
    void update(void){
      ao->update();
      glutPostRedisplay();
    }
    
    int main(int argc, char **argv)
    {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize(winWidth, winHeight);
      glutCreateWindow("Articlated");
      glutIdleFunc(update);
      glutReshapeFunc(reshape);
      glutDisplayFunc(display);
      ifstream fin(argv[1]);
      ao = new artObject(fin);
      init();
      glutMainLoop();
      return 0;
    }

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't think that is close to what I was talking about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ animation through bitmap and OpenGL
    By egor in forum C++ Programming
    Replies: 15
    Last Post: 12-09-2009, 01:17 AM
  2. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. OpenGL: glutIdleFunc and animation
    By gazsux in forum Game Programming
    Replies: 1
    Last Post: 03-21-2003, 07:24 AM
  5. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM