Jump to content
  • Advertisement
Sign in to follow this  
frogtag

Skeletal Animation

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is the one subject where no-one ever replies and there are very few tutorials out there that are any good ... so I BEG YOU any help no matter how pointless you may think it is, it may help me and maybe others! Using C++/OpenGL/VC++ 2008 I've set up hierarchy skeletal humanoid stick figure man, I've included the code if it'll help. How do I go about simple animation? I'm happy with the basics, just need to know about theory? Character.cpp
#include "character.h"

Character pCharacter(1.83f);

Character::Character(float new_height)
{
  height = new_height;
	head = height/8;
	face = head * 0.75f;
	names[0][0] = '\0';

	root	 = BoneAddChild(NULL,		0.0f,height/2,0.0f,			0.0f,0.0f,			0.0f,				0,"Center");
  parent = BoneAddChild(root,		0.0f,0.0f,0.0f,					1.5708f,0.0f,		head*0.5f,	0,"Trunk");
  child1 = BoneAddChild(parent,	0.0f,0.0f,0.0f,					0.0f,0.0f,			head*2.25f,	0,"Torso");
					 BoneAddChild(child1, 0.0f,0.0f,0.0f,					0.0f,0.0f,			head/2,			0,"Neck");
	child2 = BoneAddChild(child1, -head*0.25f,0.0f,0.0f,	-1.5708f,0.0f,	head,				0,"LChest");
  child3 = BoneAddChild(child2, 0.0f,0.0f,0.0f,					-1.5708f,0.0f,	head*1.5f,	0,"LShoulder");
  child4 = BoneAddChild(child3, 0.0f,0.0f,0.0f,					0.0f,0.0f,			head*1.25f,	0,"LElbow");
					 BoneAddChild(child4, 0.0f,0.0f,0.0f,					0.0f,0.0f,			head*0.75f,	0,"LWrist");
	child2 = BoneAddChild(child1, -head*0.25f,0.0f,0.0f,	1.5708f,0.0f,		head,				0,"RChest");
  child3 = BoneAddChild(child2, 0.0f,0.0f,0.0f,					1.5708f,0.0f,		head*1.5f,	0,"RShoulder");
  child4 = BoneAddChild(child3, 0.0f,0.0f,0.0f,					0.0f,0.0f,			head*1.25f,	0,"RElbow");
					 BoneAddChild(child4, 0.0f,0.0f,0.0f,					0.0f,0.0f,			head*0.75f,	0,"RWrist");
	parent = BoneAddChild(root,		0.0f,0.0f,0.0f,					0.0f,0.0f,			face/2,			0,"LHip");
  child1 = BoneAddChild(parent, 0.0f,0.0f,0.0f,					-1.5708f,0.0f,	head*2,			0,"LKnee");
					 BoneAddChild(child1, 0.0f,0.0f,0.0f,					0.0f,0.0f,			head*1.75f,	0,"LAnkle");
  parent = BoneAddChild(root,		0.0f,0.0f,0.0f,					0.0f,0.0f,			-face/2,		0,"RHip");
  child1 = BoneAddChild(parent, 0.0f,0.0f,0.0f,					-1.5708f,0.0f,	head*2,			0,"RKnee");
					 BoneAddChild(child1, 0.0f,0.0f,0.0f,					0.0f,0.0f,			head*1.75f, 0,"RAnkle");

	BoneListNames(root, names);
  currentName = names[nameIndex];
  animating = 0;
  frameNum = 0;
  BoneGenQuads(root);
  //meshLoadData("mesh.txt", &body, root);

}

Character::Bone * Character::BoneAddChild(Bone *root,float x,float y,float z,float xy,float xz,float l,uint8_t flags,char *name)
{
	Bone *temp;

	if(!root)
  {
    if(!(root = (Bone *)malloc(sizeof(Bone))))
			return NULL;
		root->parent = NULL;
  }
  else
  if(root->childCount < MAX_CHCOUNT)
  {
    if(!(temp = (Bone *)malloc(sizeof(Bone))))
      return NULL;
		temp->parent = root;
    root->child[root->childCount] = temp;
    root->childCount++;
    root = temp;
	}
  else
	return NULL;

  root->x = x;
	root->y = y;
	root->z = z;
	root->xy = xy;
	root->xz = xz;
  root->l = l;
	root->flags = flags;
  root->childCount = 0;

	if(name)
    strcpy_s(root->name, name);
  else
    strcpy_s(root->name, "Bone");

	for(int i = 0; i < MAX_CHCOUNT; i++)
		root->child = NULL;

	return root;
}

void Character::BoneDraw(Bone *root, int selected)
{
	int i;

	glPushMatrix();

	if(!strcmp(root->name, currentName))
		selected = 1;

	glTranslatef(root->x,root->y,root->z);
	glRotatef(RAD2DEG(root->xy),0.0f,0.0f,1.0f);

	glBegin(GL_LINES);

	if(selected)
		glColor3f(1.0f,0.0f,0.0f);
	else
		glColor3f(0.0f,1.0f,0.0f);

	glVertex2f(0.0f,0.0f);

	if(selected)
		glColor3f(1.0f,1.0f,0.0f);
	else
		glColor3f(0.0f,0.0f,1.0f);
	glVertex2f(root->l,0.0f);
	
	glEnd();

	glTranslatef(root->l,0.0f,0.0f);

	for (i = 0; i < root->childCount; i++)
		BoneDraw(root->child, selected);

	glPopMatrix();
}

void Character::BoneGenQuads(Bone *root)
{
	if (!root)
		return;

	int i;

	root->vertex[0].x = 0.0f;
	root->vertex[0].y = 0.05f;

	root->vertex[1].x = 0.0f;
	root->vertex[1].y = -0.05f;
	
	root->vertex[2].x = root->l;
	root->vertex[2].y = -0.05f;
	
	root->vertex[3].x = root->l;
	root->vertex[3].y = 0.05f;

	for (i = 0; i < 4; i++)
	{
		root->vertex.r = (rand() % 256) / 256.0f;
		root->vertex.g = (rand() % 256) / 256.0f;
		root->vertex.b = (rand() % 256) / 256.0f;
	}
	
	for (i = 0; i < root->childCount; i++)
		BoneGenQuads(root->child);
}

void Character::BoneListNames(Bone *root,char names[MAX_BONECOUNT][20])
{
  if(!root)
    return;

  int i;

  for(i = 0; (i < MAX_BONECOUNT) && (names[0] != '\0'); i++)
    if (!strcmp(names, root->name))
      break;

  if(i < MAX_BONECOUNT)
  {
    strcpy_s(names, root->name);
    if (i + 1 < MAX_BONECOUNT)
      names[i + 1][0] = '\0';
  }

  for(i = 0; i < root->childCount; i++)
    BoneListNames(root->child, names);
}

void Character::Man()
{
	BoneDraw(root, 0);
}

Character.h
#ifndef CHARACTER_H
#define CHARACTER_H

#define _USE_MATH_DEFINES
#include <cmath>
#include <stdint.h>

#include "window.h"

#define MAX_BONECOUNT			18
#define MAX_CHCOUNT				8
#define MAX_VXCOUNT				4
#define RAD2DEG(a) (((a) * 180.0f) / (float)M_PI)

class Character
{	
  public:	
		Character(float);

		void Man(void);

		typedef struct
    {
			float x,y,z,r,g,b;
    } Vertex;

    typedef struct _Bone
    {
			char name[20];
			
			float x,y,z,xy,xz,l;
			
			struct _Bone *child[MAX_CHCOUNT], *parent;

			uint8_t childCount,flags;

      Vertex vertex[MAX_VXCOUNT];
    } Bone;

		Bone *root, *parent, *child1, *child2, *child3, *child4;
		Bone *BoneAddChild(Bone*,float,float,float,float,float,float,uint8_t,char*);

		char *currentName;
		char names[MAX_BONECOUNT][20];

		float face,head,height;

		int frameNum,animating,nameIndex;

		void BoneDraw(Bone*,int);
		void BoneGenQuads(Bone*);
		void BoneListNames(Bone*,char names[MAX_BONECOUNT][20]);
};

extern Character pCharacter;

#endif

Share this post


Link to post
Share on other sites
Advertisement
There's a very good article here. It's first half covers the theory and the second half covers implementation using the D3DX animation API (this part you can ignore).

The article on transformation hierarchies is also worth a look.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!