Jump to content
  • Advertisement
Sign in to follow this  
Pateman

OpenGL GLSL Skinning problem

This topic is 2033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello community,

 
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:
3oZhZ.png
 
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);


      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 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)
HBJjC.png
 
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:
5hcab.png
 
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(
      (
         (vec4(0.2, 0.2, 0.2, 1.0))
         + (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.

 

Share this post


Link to post
Share on other sites
Advertisement

I'm afraid I haven't read through all the code, but one quick idea; when you create a 'Pad the remaining number of weights with zero' are you also setting the bone ID to something valid? I once had an issue on a console where a weight of zero combined with an invalid bone matrix to produce a non-zero garbage value on the GPU.

Share this post


Link to post
Share on other sites

[quote name='C0lumbo' timestamp='1358784975' post='5023946']
when you create a 'Pad the remaining number of weights with zero' are you also setting the bone ID to something valid?
[/quote]

Yes, I set it to zero, then in the shader I verify whether the bone ID is set to 0, like so:

 

 

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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!