20

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.

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
user2602528
  • 243
  • 1
  • 3
  • 8

1 Answers1

15

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:

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • 3
    To be honest, implementing this keyframe tweening on a vertex level is probably a lot of more effort, than implementing a skeletal animation system. Skeletal animation wasn't done in the "old" days, because CPU's lacked the horsepower to perform all those additional vertex transformations. But today's GPUs crunch that additional matrix and interpolation for breakfast and it's a lot less code in GLSL, too. – datenwolf Aug 22 '13 at 21:36
  • @datenwolf I agree with you on the performance stuff, but I disagree about ease of implementing skeletal animation. Vertex tweening is `for i < vertCount { vbo->verts[i] = lerp(t, previous[i], next[i]) }` In comparison to slerping a hierarchy of quaternions, with relationships and lengths, (that part not to bad) but then you have to associate vertices with bone weights. Its just a lot more data to manage. – Justin Meiners Aug 22 '13 at 21:40
  • 3
    An animation with multiple keyframes can also be exported as a single OBJ file, this way you don't need to use a custom / proprietary animation format: https://stackoverflow.com/questions/757145/do-wavefront-obj-files-support-animation/13111722#13111722 – baptx Sep 08 '18 at 10:41
  • @baptx Good info. You certainly could use groups to store multiple frames of the mesh, but I don't know of a program that writes them that way or reads them that way. – Justin Meiners Sep 09 '18 at 01:39
  • @JustinMeiners I opened the OBJ file with Blender but all frames are displayed as one object. I don't know if Blender has a feature to view one group at a time but a Blender plugin could maybe be created and display the object frame by frame, like the custom viewer tool created to view Spyro animations from the OBJ file. If people want to use the OBJ file in their own game engine, it should not be too difficult to display the animation with a few lines of code. I heard Blender can also export an animation with a frame per OBJ file so a script could merge frames as single OBJ file with groups. – baptx Sep 17 '18 at 09:43
  • @baptx agreed, definitely possible to do that, I just didn't mention it because I never have seen it. Sounds like a good idea. – Justin Meiners Sep 17 '18 at 18:59