Jump to content
  • Advertisement
Sign in to follow this  
Saad Manzur

Bones and Skeletons

This topic is 2065 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

Can anyone give me some pointer on how to store bone structures and import animation in assimp, directx ? I cannot find any good information about it over the internet . I've got a term project on my hand on animation . Please give me some kind of example or anything..

Share this post

Link to post
Share on other sites

I'm not familiar with assimp, so I can't help you there. But with regard to storage, I found std::vector to work well, particularly for matrices, as std::vector guarantees contiguous memory for the data. Vectors work as well for the bone structures, animationset, animation and key frame structures. They deallocate themselves when they go out of scope.

Share this post

Link to post
Share on other sites

Suppose I stored the matrices. How do I get the position of the joints ? I want to draw the skeleton on the screen ? How to get the vertices of the joints ?

Share this post

Link to post
Share on other sites

Can anyone give me some pointer on how to store bone structures and import animation in assimp, directx ?

Beyond the assimp manual, see these for descriptions and code:

* <a href="http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html">Tutorial 38 - Skeletal Animation With Assimp</a>
* <a href="http://ephenationopengl.blogspot.com/2012/06/doing-animations-in-opengl.html">Doing animations in OpenGL (Ephenation OpenGL)</a>

Share this post

Link to post
Share on other sites

Hmm I haven't used assimp either and I don't know much of DirectX, but I can tell you how I did my experimental bone animation project in OpenGL.


I also searched the internet but I never came across something that clicked "dang, that's it!" - something that occasionally happens when I search for more usual stuff.

Still, with the help of the folks here in GameDev and very few obscure sites I found around (unfortunately I don't have them anymore, but I'll try to type what I got of useful from them) I managed to get a bone animation sample running.


First is, don't do the animation in your code/program. It's much much much slower. I've read (and then tested) about doing it in the shader and it indeed works much better.

Think of your data:

class Model
        float* vertexes;
        float* bonesWeights;
        Matrix4x4* bonesTransformations;

If you calculate each bone's transformation and modify the vertex data, you'll face two problems:

- It's slow because you'll be doing one vertex at a time and then updating the data.

- Because you're updating the vertex data, you're wasting memory because each model on screen will need to have it's own vertex buffer. If you don't change it's data, then you can have only a single vertex buffer and multiple models using it.


So the best course would be to do the vertex transformation at the shader level. From what I've read, they're already more suited for multi-threading and so the calculations go much faster.

Anyway, as for how to do it... this is for OpenGL so I'm not sure how similar it might be for DirectX, but I think you can get the concept and apply it if you know your stuff with DX Shaders.


Besides the usual Vector3 vertex_position, Vector2 vertex_UV and Vector3 vertex_normal in the shader, you'll need two more: Vector4 bones_weights and Vector4 bones_indexes.

Also you'll need an uniform Matrix4x4 called bone_animation[32] (or 64).


This bone_animation will hold your bones' data so you're limited by up to 32 bones per model. You can increase it if you want but it'll just be a waste of space if you're not really using it.

Another limitation (sorta of) is that each vertex can only be affected by UP TO FOUR bones. That's why you're using Vector4's for bones indexes and weights. You can use another variable if you want or even multiple, but then you would need to make some more additions to the code that might slow down a bit... still, it's up to you and depending on your needs.


Shader code logic:

void main()
    //the vertex basic data here
    vec3 vertexPosition = vertex_position; 

    //checking first bone, applying it's weight
    //I'm getting the Matrix4x4 containing the bone data, getting the index of the first bone and multiplying by it's weight.
    vertexPosition = vertexPosition * (bone_animation[bone_indexes.x] * bone_weight.x);
    //Do this for all 4 possible bones affecting this vertex
    vertexPosition = vertexPosition * (bone_animation[bone_indexes.y] * bone_weight.y);
    vertexPosition = vertexPosition * (bone_animation[bone_indexes.z] * bone_weight.z);
    vertexPosition = vertexPosition * (bone_animation[bone_indexes.w] * bone_weight.w);

    //By here this vertex has been fully skinned, multiply it as usual for your MVP matrix and you're done
    gl_Position = objectTransformationMatrix * vertex_position;

Oh by using this you also need to make sure of a few things, that the ROOT bone (index 0) is an identity matrix or that you pass some index for an identity matrix when there's no more bones affecting that vertex (so it just multiply by 1).


With this, for each model you have on screen you'll need to keep and track only their bones_animation and upload it to the card every frame instead of modifying their vertex data. That's why it's faster too.

This is called shader skinning and I think you can find the sources where I got this from on google, though at the time I googled them I found very few on it (that I could understand at least).


Now, this is the basic idea... you can get it a little bit faster if you change your data a bit. Instead of storing your bones transformations as matrices and uploading a float * 16 per animated model every frame, you can store only a vector3 and a vector4 and apply quaternion math to skin a vertex. You'll be uploading then float * 7 per model every frame then, and the shader will calculate everything else.

I couldn't which is faster on the shader (I haven't looked on debugging shaders yet), so it could be faster or slower... anyway, the idea is to use quaternion * vertex math to figure a vertex's rotated point and then translate it by the bone translation.


The shader code would be:

//uniform mat4 boneAnimation[32]; 
//mat4 containing all the bone transformations, unused for now

uniform vec3 boneTranslation[32]; //Only the translations of each bone
uniform vec4 boneRotation[32]; //And their rotations in quaternion form

//This is the function that will do the quaternion * vector3
void applySkin(in vec3 vertexPosition, in vec3 boneTranslation, in vec4 boneRotation, in float boneWeight, out vec3 updatedVertexPosition)
    vec3 cross;
    cross.x = (boneRotation.y * vertexPosition.z - vertexPosition.y * boneRotation.z) * 2;
    cross.y = (boneRotation.z * vertexPosition.x - vertexPosition.z * boneRotation.x) * 2;
    cross.z = (boneRotation.x * vertexPosition.y - vertexPosition.x * boneRotation.y) * 2;

    vec3 crossQuat;
    crossQuat.x = boneRotation.y * cross.z - cross.y * boneRotation.z;
    crossQuat.y = boneRotation.z * cross.x - cross.z * boneRotation.x;
    crossQuat.z = boneRotation.x * cross.y - cross.x * boneRotation.y;

    cross.x = cross.x * boneRotation.w;
    cross.y = cross.y * boneRotation.w;
    cross.z = cross.z * boneRotation.w;

    updatedVertexPosition = vertexPosition + ((cross + crossQuat + boneTranslation) * boneWeight);

void main()
    //same logic, multiply the vertex per all 4 possible bones affecting it
    //by the way if you want more than 4 here is where you'll have to adapt it and add another variable somewhere
    vec3 skinnedVertex;

    applySkin(vertex_position, boneTranslation[bone_indexes.x], boneRotation[bone_indexes.x], bone_weight.x, skinnedVertex);
    applySkin(vertex_position, boneTranslation[bone_indexes.y], boneRotation[bone_indexes.y], bone_weight.y, skinnedVertex);
    applySkin(vertex_position, boneTranslation[bone_indexes.z], boneRotation[bone_indexes.z], bone_weight.z, skinnedVertex);
    applySkin(vertex_position, boneTranslation[bone_indexes.w], boneRotation[bone_indexes.w], bone_weight.w, skinnedVertex);

    gl_Position = objectMVP * skinnedVertex;

This way you'll only need to worry about getting the data in the right format before, translations and rotations per bone and upload it every frame.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!