I'm rotating the bones of a skeleton inside a mesh for a low poly 3D figure. On the vertex shader its applied like this.
glsl:
vec4 vert1 = (bone_matrix[index1]*vertex_in)*weight;
vec4 vert2 = (bone_matrix[index2]*vertex_in)*(1-weight);
gl_Position = vert1+vert2;
bone_matrix[index1]
is the matrix of one bone and bone_matrix[index2]
is the matrix of the other. weight
designates vertex_in
's membership to these bones. The problem is the closer the weight is to .5, the more the diameter of the elbow shrinks when a rotation is applied. I've tested it with around a 10,000 vertex cylinder shape (with a gradient of weights). The result looked like bending a garden hose.
I got my weighting method from these sources. Its actually the only way I could find:
http://www.opengl.org/wiki/Skeletal_Animation
http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html
http://blenderecia.orgfree.com/blender/skinning_proposal.pdf
The left is how the shape starts, the middle is how the above equation rotates it, and the right is my goal. The mid points are weighted 0.5
. It only gets worse the more bent it is, at 180 degrees it has zero diameter.
- I've tried assembling the matrix on the shader, so that I can apply the weights to the rotation instead of the resulting vertices. It looks perfect like the one in picture on the right, but it requires assembling the matrix for every single vertex (expensive)
- I've looked into quaternions, but glsl doesn't natively support them (correct me if I'm wrong) and they're confusing. Is that what I need to do?
- I've considered having three bones per joint, and add a "kneecap" between every bone. This wouldn't eliminate the problem but would mitigate it.
- I'm considering projecting the vertex its original distance from the axis after they're rotated. This would fail at 180 degrees but would be (relatively) cheap.
So considering the options, or other options that I may not have considered, How have others avoid this pinching effect?
EDIT: I've gotten SLERP to work using quaternions but I opted not to use it as GLSL does not natively support it. I couldn't get the geometric SLERP to work as described by Tom. I got NLERP working for the first 90 degrees, so I added an extra "bone" between each joint. So to bend the forearm 40 degrees I bend the elbow and the forearm by 20 degrees each. This eliminates the pinching effect at the expense of doubling the quantity of bones which is not an ideal solution.