24

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

initial_ugly_good

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.

3
  • 3
    I like your edits. As I'm reading the question, I feel like I can follow it (modulo domain-specific jargon). Some may consider this question too broad, but in my opinion, the main problem is just that this topic has too few knowledgeable Stack Overflow readers. (In case anyone is wondering why I made this seemingly out-of-the-blue comment, it's because I found this question on meta.)
    – John Y
    Commented Aug 29, 2014 at 21:15
  • I appreciate all the interest and support I've gotten from meta. I intentionally kept the question broad after the edit because I'm interested in any alternate solution that doesn't have this problem and has been successfully employed elsewhere.
    – gunfulker
    Commented Aug 29, 2014 at 21:39
  • 7
    The community here is so great, we owe a lot to the members of this site.
    – JonH
    Commented Aug 29, 2014 at 22:19

3 Answers 3

9

Disclaimer : I'm not a lot of a 3D guy, so I'll just suggest you a mathematical approach that may help you.

First of all, let me put this little schema, this way we'll be sure we are all talking about the same thing :

enter image description here

The blue and green figures are the original bones, rotated completely with either bone_matrix[index1] or bone_matrix[index2]. The red dot is the center of rotation, the orange figure is what you want and the black one is what you have.

So, you figure being build as a weighted average of the blue and green ones, on this drawing we see (thanks to the gray lines), why it shrinks like that.

You need to somehow compensate for this shrinking, I would suggest scaling back the points from your center of rotation, we need a scaling of value 2 at the junction between the bones, and of value 1 at the extremities.

Let scale_matrix be a pre-computed matrix : a scaling of amplitude 2 centered at your center of rotation (red dot).

You end up with this shader :

vec4 vert1 = (bone_matrix[index1]*vertex_in)*weight;
vec4 vert2 = (bone_matrix[index2]*vertex_in)*(1-weight);
vec4 inter =  vert1+vert2;
vec4 scaled1 = inter*(1-2*min(weight, 1-weight));
vec4 scaled2 = (scale_matrix*inter)*(2*min(weight, 1-weight));
gl_Position =  scaled1+scaled2;

I'm afraid I can't test it right now (I don't know a lot about GLSL), but I think you'll be able to adapt it to your case if something doesn't fit.

6
  • I'll try it tonight. it looks promising.
    – gunfulker
    Commented Aug 30, 2014 at 15:15
  • I got it implemented ... clearly something is wrong. After dinner I'll double check I've faithfully implemented your suggestion and upload link a .gif of what it looked like before and what its doing now.
    – gunfulker
    Commented Aug 31, 2014 at 21:28
  • Ok so this is what it looked like originally: youtu.be/tPXU5WI4Bvk this is my attempt to use your suggestion: youtu.be/62i7MZaiHPw I figured it was too big so I took out the matrix scaling and got this:<br> youtu.be/HyuldtPdkdQ Note the dark parts represent the back of a triangle being visible (fragment shader effect). Also I confirmed (not shown in above videos) that it still completely pinches when its not scaled, which I guess is to be expected.
    – gunfulker
    Commented Sep 1, 2014 at 1:32
  • So either your methods are wrong or I'm multiplying my matrices wrong. I've got a skeleton class that recursively multiplies the matrices for the bones, I mimicked that so the the orientation matrices are multiplied to make sure its centering the scaling on the right coordinates. Lets say I got a a shoulder,elbow, and wrist matrix. The body is staying still so the matrices propagate out from the body. What would your scale_matrix for the wrist get multiplied by, and what order, before it gets sent to shader? I'm guessing shoulderelbowscale_matrix_original. Is that right?
    – gunfulker
    Commented Sep 1, 2014 at 1:39
  • In my mind, you first transform the whole member as if it were a single bone (thus weight of elbow and wrist are 0 or 1 constants depending on your implementation), then you transform both elbow and wrist relatively to shoulder as if they were a single member, etc... But I realize it might not be so easy with OpenGL shaders. I didn't think about this problem beforehand to be honest.
    – Levans
    Commented Sep 1, 2014 at 4:21
7
+50

The problem

The cause of what your seeing is illustrated by the drawing in Levans answer. However, to understand what's going on consider what's happening when you execute the code:

If the first point vert1 has coordinates (p, 0) the coordinates of vert2 will be (p cos(α), p sin(α)) where α is the angle between the two bones (this is always possible given an appropriate coordinate transform). Adding these together using the appropriate weights w and 1-w we get the following coordinates:

x = w p + (1-w) p cos(α)
y = (1-w) p sin(α)

The length of this vector is:

length^2 = x^2 + y^2
         = (w p + (1-w) p cos(α))^2 + (1-w)^2 p^2 sin(α)^2
         = p^2 [w^2 + 2 w (1-w) cos(α) + (1-w)^2 cos(α)^2 + (1-w)^2 sin(α)^2]
         = p^2 [w^2 + (1-w)^2 + 2 w (1-w) cos(α)]

As an example, when w = 1/2 this simplifies to:

length^2 = p^2 (1/2 + 1/2 cos(α)) = p^2 cos(α/2)^2

And length = p |cos(α/2)| whereas the length of the original vectors is p (see graph). The length of the new vector becomes smaller, this is the shrinking effect that you perceived. The reason for this is that we are actually interpolating the two vertices along a straight line. If we want the keep the same length p we actually need to interpolate along a circle around the center of the rotation. One possible approach is to renormalize the resulting vector, preserving the width at the joint.

This means we need to divide the resulting vertex coordinates by |cos(α/2)| (or the more general result for arbitrary weights). This has as a side effect of course, a divide by zero whenever the angle is exactly 180° (for the same reason the width at the joint is zero with your technique).

I'm no skeletal animation expert, but it seems to me the original solution as you described it, is an approximation to work with small bone angles (where the shrinking effect is minimal).

Alternative approaches

A different approach is to interpolate your rotations instead of your vertices. See for example the slerp wiki page and this paper.

SLERP

The slerp technique is similar to the technique I described above in the sense that it also preserves the width at the joint, however it interpolates directly along a circular path around the joint. The general formula is:

gl_Position = [sin((1-w)α)*vert1 + sin(wα)*vert2]/sin(α)

Given the points from above vert1 = (p, 0) and vert2 = (p cos(α), p sin(α)) applying the SLERP formula yields result = (x, y) with:

x = p [sin((1-w)α) + sin(wα) cos(α)]/sin(α)
y = p sin(wα) sin(α)/sin(α) = p sin(wα)

Calculating the cosine cos θ of the angle between vert1 and result yields:

cos(θ) = vert1*result/(|vert1| |result|) = vert1*result/p^2
       = p^2 [sin(wα) + sin((1-w)α) cos(α)]/sin(α)/p^2
       = [sin(α) cos((1-w)α) - cos(α) sin((1-w)α) + sin((1-w)α) cos(α)]/sin(α)
       = cos((1-w)α)

The angle between vert2 and result is:

cos(φ) = vert2*result/p^2
       = [sin(wα) cos(α) + sin((1-w)α) cos(α)^2 + sin((1-w)α) sin(α)^2]/sin(α)
       = [sin(wα) cos(α) + sin((1-w)α) cos(α)]/sin(α)
       = [sin(wα) cos(α) + sin(α) cos(wα) - cos(α) sin(wα)]/sin(α)
       = cos(wα)

This means that θ/φ = (1-w)/w which expresses the fact that SLERP interpolates with constant radial velocity. When working with 3D rotation matrices we can express the rotation transforming vert1 into vert2 as M = inverse(A)*B = transpose(A)*B so that we can express the rotation angle α as:

cos(α) = (tr(M) - 1)/2 = (tr(transpose(A)*B) - 1)/2
       = (A[0][0]*B[0][0] + A[0][1]*B[1][0] + A[0][2]*B[2][0] + 
          A[1][0]*B[0][1] + A[1][1]*B[1][1] + A[1][2]*B[2][1] + 
          A[2][0]*B[0][2] + A[2][1]*B[1][2] + A[2][2]*B[2][2] - 1)/2

Quaternion LERP

When working with quaternions a good approximation to the SLERP is to linearly interpolate the quaternions directly after which you renormalize the result. This gives an interpolation curve identical to the one in SLERP, however interpolation does not occur at constant radial velocity.

If you really want to avoid these problems altogether you can always split your meshes at the joint and rotate these separately.

14
  • What you described with |cos α/2| is what I mentioned in bullet point 4 in the question, I will try it. Splitting the mesh is not an option, but I've considered having an intermediate "kneecap" type bone to avoid approaching 180°. Before I try slerp, do I send the ending point to the vertex shader or do I multiply it by the matrix on the vertex shader?
    – gunfulker
    Commented Sep 1, 2014 at 18:27
  • You can keep the matrix multiplications in the shader, you only need to adjust the weights used to the ones mentioned in the wiki page. Do keep in mind that the slerp technique also has problems at 180°. Commented Sep 1, 2014 at 18:33
  • I can live with force-avoiding 170° and higher. Does it gradually get worse approaching 180° or is it some kind of divide-by-zero because it doesn't know which direction to go to "get from the north pole to the south pole"?
    – gunfulker
    Commented Sep 1, 2014 at 18:38
  • For the slerp, it's a divide by zero because the sine function is zero at 0° and 180°. For the renormalizing technique it's a divide by zero because the linear interpolation passes through the origin (you might indeed say it doesn't know how to get from the north to the south pole). To obtain the angle from the rotation matrix, keep in mind that cos α = (tr(M)-1)/2 where tr is the trace function and M is a 3D matrix. Commented Sep 1, 2014 at 18:42
  • 1
    @gunfulker - was your question answered - it is only fair to apply a bounty to Tom if that is the case. Please let me know since i opened the bounty.
    – JonH
    Commented Sep 4, 2014 at 20:15
5

Depending on your actual application, you might like this variant: you can add additional band between parts like so:

enter image description here

Weights are shown in green/teal. This requires a little bones trickery however, so when you bend to the right, you use right-side bones and set rotation center to the right, and when to the left - left-side bones and rotation center on the left.

2
  • Can't accept this one sorry, it suffers the same faults as the first one, when it bends greater then 90 degrees it starts looking obscenely pinched when viewed from the side. Furthermore it requires additional bone selection logic. It does give me ideas though, so thanks!
    – gunfulker
    Commented Sep 1, 2014 at 15:56
  • 1
    No worries. Maybe your idea will work out and you will post it here too :)
    – Kromster
    Commented Sep 1, 2014 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.