How to animate a 3d model (mesh) in OpenGL?

user2602528 picture user2602528 · Aug 22, 2013 · Viewed 25.2k times · Source

I want to animate a model (for example a human, walking) in OpenGL. I know there is stuff like skeleton-animation (with tricky math), but what about this....

  1. Create a model in Blender
  2. Create a skeleton for that model in Blender
  3. Now do a walking animation in Blender with that model and skeleton
  4. Take some "keyFrames" of that animation and export every "keyFrame" as a single model (for example as obj file)
  5. Make an OBJ file loader for OpenGL (to get vertex, texture, normal and face data)
  6. Use a VBO to draw that animated model in OpenGL (and get some tricky ideas how to change the current "keyFrame"/model in the VBO ... perhaps something with glMapBufferRange

Ok, I know this idea is only a little script, but is it worth looking into further? What is a good concept to change the "keyFrame"/models in the VBO?

I know that memory problem, but with small models (and not too much animations) it could be done, I think.

Answer

Justin Meiners picture Justin Meiners · Aug 22, 2013

The method you are referring to of animating between static keyframes was very popular in early 3D games (quake, etc) and is now often referred to as "blend shape" or "morph target" animation.

I would suggest implementing it slightly differently then you described. Instead of exporting a model for every possible frame of animation. Export models only at "keyframes" and interpolate the vertex positions. This will allow much smoother playback with significantly less memory usage.

There are various implementation options:

  • Create a dynamic/streaming VBO. Each frame find the previous and next keyframe model. Calculate the interpolated model between them and upload it to the VBO.

  • Create a static VBO containing the mesh data from all frames and an additional "next position" or "displacement" attribute at each vertex. Use the range options on glDrawArrays to select the current frame. Interpolate in the vertex shader between position and next position.

You can actually setup blender to export every frame of a scene as an OBJ. A custom tool could then compile these files into a nice animation format.

Read More: