Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This time I've decided to ask a question related to my skinning problem. Two earlier ones were not really related to my problem and I've deleted them.

Ok, first of all, I'm using OpenGL 3, 3ds max, and Delphi to create a skeletal animation demo. I'm using the IGame interface to export meshes from 3ds max to XML - I use the sample included in the SDK.

Now, I'm going to describe as precisely as I can what I do.

Passing bone weights to the shader

I'll skip parsing geometry, because it works okay. Now, if a mesh is skinned (using the Physique modifier), the resulting XML contains information about bone influences for each vertex, like so:

<Skin VertexID="2" Type="Rigid">
    <Bone BoneID="22" Weight="1,000000"></Bone>
</Skin>
...
<Skin VertexID="14" Type="Blended">
    <Bone BoneID="22" Weight="0,986472"></Bone>
    <Bone BoneID="14" Weight="0,013015"></Bone>
    <Bone BoneID="23" Weight="0,000513"></Bone>
</Skin>
...
<Skin VertexID="31" Type="Blended">
    <Bone BoneID="22" Weight="0,948354"></Bone>
    <Bone BoneID="23" Weight="0,030462"></Bone>
    <Bone BoneID="23" Weight="0,011265"></Bone>
    <Bone BoneID="14" Weight="0,009909"></Bone>
    <Bone BoneID="23" Weight="0,000011"></Bone>
</Skin>

My shader assumes that there should be always 4 bones, which influence a single vertex, and as you can see, sometimes there is sulprus/insufficient number of them, so I decided to:

  1. Sum up weights of the same bone - take a look at vertex 31 - there are five entries, but after summing them up, we're left with three.
  2. Pad the remaining number of weights with zero.

Right now, I have a list of bone IDs/vertex weight pairs, which I pass to the shader like so:

const
    MAX_BONES_PER_VERTEX = 4;
type
    TVertexWeight = record
        BoneID: Integer;
        Weight: Single; // standard float
    end;
...
var
  L: TList<TVertexWeight>;
...
glGenBuffers(1, @FBonesVBO);
glBindBuffer(GL_ARRAY_BUFFER, FBonesVBO);
glBufferData(GL_ARRAY_BUFFER, L.ListSize, L.ListPointer, GL_STATIC_DRAW);

glEnableVertexAttribArray(FBoneIDsAttr);
glVertexAttribIPointer(FBoneIDsAttr, MAX_BONES_PER_VERTEX, GL_INT,
    SizeOf(TVertexWeight), PInteger(0));

glEnableVertexAttribArray(FWeightsAttr);
glVertexAttribPointer(FWeightsAttr, MAX_BONES_PER_VERTEX, GL_FLOAT,
    False, SizeOf(TVertexWeight), PInteger(4));

I think this part is working fine, because when I hardcode an identity matrix in the shader, the model is rendered in its bind pose.

Computing bone matrices

For now, I just want to display the model in a pose which it would be in, let's say, frame 49: enter image description here

Here's the code I use:

procedure CalculateBones(const SequenceName: String; const FrameID: Integer;
    const ABone: TAnimatedMeshBone; const ParentTransform: TBrainMatrix);
var
    Samples: TBrainList<TAnimatedMeshAnimationSample>;
    BoneSample: TAnimatedMeshAnimationSample;
    I: Integer;
    BoneTransform, GlobalTransform: TBrainMatrix;
    Children: TBonesList;
begin
    Samples := TBrainList<TAnimatedMeshAnimationSample>.Create();
    Children := TBonesList.Create();
    try
      FAnims[SequenceName].SamplesByFrame(FrameID, Samples);
      BoneSample := nil;
      for I := 0 to Samples.Count -1 do
        if (Samples[I].Bone.ID = ABone.ID) then
        begin
          BoneSample := Samples[I];
          break;
        end;

      BoneTransform := ABone.OffsetMatrix;
      if (BoneSample <> nil) then
        BoneTransform := BoneSample.Matrix;
      GlobalTransform := Mat4Multiply(ParentTransform, BoneTransform);
      GlobalTransform := Mat4Multiply(GlobalTransform, Mat4Inverse(BoneTransform));

      BoneMatrices.Add(GlobalTransform);

      FBones.BonesByParentID(ABone.ID, Children);
      for I := 0 to Children.Count -1 do
        CalculateBones(SequenceName, FrameID, Children[I], GlobalTransform);
    finally
      Samples.Free();
      Children.Free();
    end;
end;
...
BoneMatrices := TBrainList<TBrainMatrix>.Create();
try
    //  A bone's offset matrix is the bind pose.
    CalculateBones('zhuxian2', 49, FBones.RootBone, FBones.RootBone.OffsetMatrix);
    glUniformMatrix4fv(FShader.GetUniformLocation('boneMatrices'),
        BoneMatrices.Count, False, BoneMatrices.ListPointer);
finally
    BoneMatrices.Free();
end;

A bone's OffsetMatrix is basically the bind pose (regarding to what the exporter says, it's in object space) of the bone. Now, a TAnimatedMeshAnimationSample contains position and rotation (in quaternion) of a bone in the given frame. The for loop at the top of CalculateBones() basically checks if in the given frame (in this case, 49th) there's transformation data for the given bone.

After launching the code above, all I get is this: (please ignore the lack of textures for now) enter image description here

But when I change the initial parent transform from FBones.RootBone.OffsetMatrix to Mat4Identity() (basically use an identity matrix), the model is displayed in its bind pose: enter image description here

Vertex shader code

#version 150

#define MAX_BONES_PER_VERTEX 4
#define MAX_BONES 32

uniform mat4 proj;
uniform mat4 modelview;
uniform mat4 boneMatrices[MAX_BONES];

in vec3 vertex;
in vec3 normal;
in vec2 texCoord;
in ivec4 boneIDs;
in vec4 boneWeights;

out vec4 fragmentColor;

float phong_weightCalc(in vec3 light_pos, in vec3 frag_normal) {
    return max(0.0, dot(frag_normal, light_pos));
}

mat4 getBoneMatrix(int boneIndex) {
    mat4 retMat = mat4(1.0);
    if (boneIDs[boneIndex] != 0) {
        retMat = boneMatrices[boneIDs[boneIndex]];
    }
    return retMat;  
}

float getWeight(int boneIndex) {
    float res = 1.0;
    if (boneIDs[boneIndex] != 0) {
        res = boneWeights[boneIndex];
    }
    return res;
}

void main() {
    vec3 EC_Light_location = vec3(0.0, 1.0, 1.0);

    float diffuse_weight = phong_weightCalc(
        normalize(EC_Light_location),
        normalize(normal)
    );

    fragmentColor = clamp(
        (
            // global component
            (vec4(0.2, 0.2, 0.2, 1.0))
            // material's interaction with the direct light from
            // the light.
            + (vec4(0.8, 0.8, 0.8, 1.0) * diffuse_weight)
        ), 
        0.0, 1.0);

    float totalWeight = 0.0;
    vec4 PosL = vec4(0.0, 0.0, 0.0, 1.0);
    for (int i = 0; i < MAX_BONES_PER_VERTEX; i++) {
        float w = getWeight(i);
        PosL += w * (vec4(vertex, 1.0) * getBoneMatrix(i));
        totalWeight += w;
    }
    PosL /= totalWeight;
    PosL.w = 1.0;

    gl_Position = proj * modelview * PosL;
}

The question is, can someone point out what is wrong with the code?

Thank you very much.

PS. If you need any more information, please let me know in the comments.

Edit 1. I've changed the vertex shader code a bit - final vertex is calculated in a different way, but still it does not seem to solve my problem.

share|improve this question
2  
Am I seeing correctly that you're summing bone matrices? That wouldn't be right... See my answer here for an overview. –  Torious Jan 17 '13 at 10:37
    
Yep, I'm summing the matrices, but I've tried with code like vec4 PosL = vec4(0.0, 0.0, 0.0, 1.0); for (int i = 0 ; i < MAX_BONES_PER_VERTEX; i++) { PosL += boneWeight * (boneMatrix * vertex); } and it still didn't work. I'm a bit lost with your explanation (although I do appreciate how exhaustive it is). Could you try and adapt it to what I have here, please? –  Pateman Jan 18 '13 at 12:49
1  
Add a debug mode that lets you draw your bones as a simple skeleton. That will help check whether the problem is the bone transformations or the vertex weighting/skinning itself. It's not a bad idea to have a CPU-side skinning solution either just to test your initial math and understanding of the technique on a significantly-easier to debug platform. –  Sean Middleditch Jun 5 '13 at 17:51
add comment

1 Answer

Looking at your screenshot I'd say you need to transpose your bone matrices, that should fix your problem.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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