Xavier
Xavier

Reputation: 9049

opengl rotations for a human

I currently can rotate around a pivot point by first translating to the pivot point then performing the rotation and finally translating back to the origin. I do that easily enough for the shoulder in my example. However I cannot figure out how to also add in a rotation around the elbow for the forearm.

I've tried the following for the forearm rotation around the elbow:

  1. translate to shoulder, rotate, translate to origin, translate to forearm, rotate, translate to origin
  2. translate to shoulder, rotate, translate to forearm, rotate, translate to shoulder, translate to origin

Neither work for me. Any suggestions? I'm really stuck on this one.

Upvotes: 2

Views: 628

Answers (2)

Ryan
Ryan

Reputation: 4677

I ran into a similar problem when I was doing some skeletal animation. It's very helpful to use recursion for this. Also, structure your bones hierarchically (e.g. shoulder is a parent of forearm which is a parent of hand, etc.). By doing that you can write your code as follows:

void drawBone(Bone *root) {  
    if (!root) return;  
    glPushMatrix();  
    glTranslatef(root->x,root->y,0);  
    glRotatef(root->a,0,0,1);  
    // insert code to actually draw bone here  
    int i;  
    glTranslatef(root->l,0,0);  
    for (i=0; i<root->childCount; i++)   
        drawBone(root->child[i]);  
    glPopMatrix();  
}  

My bone struct looked like this:

typedef struct _Bone {  
    float x,y,a,l;    // position, angle, length of bone  
    int childCound;   // number of children for this bone  
    struct _Bone *child[MAX_CHILD_COUNT], *parent;  
} Bone;  

This website is a great resource for skeletal animation.

Upvotes: 3

tgmath
tgmath

Reputation: 13541

The second approach should work. I don't really know your model for the human body. Some similar task was a first assignment in a computer vision course I took.

A helpful thing is to use a scene-graph to build parts that share a common local coordinate system. Then you can traverse it and rotate correctly. It is helpful not to translate but to use glPushMatrix() and glPopMatrix()

In this way you can think in a local coordinate system and affect all other elements.

Upvotes: 0

Related Questions