A couple of months ago I implemented 3D skeletal animation in OpenGL. Since I have never implemented such a thing before, it was fairly hard! Skeletal animation is one of those things where you work in total darkness with little possibility to debug anything. Until the code is finished you barely see any graphical result at all, most of the time the screen is black or displaying a heap of broken faces.
A video of the result of my work is displayed below:
My code is written in C++ and OpenGL. I used SFML for the OpenGL window context. Skeletal animations work by having a skeleton mapped to your 3D model. The bones in this skeleton are mapped to vertices in the model. The bones are said to influence the vertices. By moving a bone, the vertices move along. Animations are made by moving bones. There are several programming challenges involved in this. First, you need to take a finished model with skeleton and import them both. You then need to import the animations clips – a series of bone transformations – and calculate new positions for the bones. One of the biggest challenges is getting the bones transformed correctly. If one bone moves, bones that are said to be children of that bone, needs to move as well. This is calculated using hierarchical transformations.
When you have calculated the animation frames correctly you need to pass your model, along with the bone transformations and information about which vertices are influenced by which bones down to a shader on your GPU. This shader then does the actual transformation of the vertices. In my vertex shader the bones simply come as a huge list of transformations (4×4 matrices). These transformations are weighted, so that a vertex can be influenced by multiple bones.
For each frame, you can either calculate the transformations of the bones on the fly, or pre-calculate them. I started with calculating them on the fly and then switched to pre-calculated transformations (I calculated 60 per second, should be nice and smooth). The pre-calculation made my implementation several times faster, my animation played at a nice 700+ FPS.
The source code, both C++ and shaders, can be found here: http://files.zylinski.se/skeletal_animation/






