Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pateman

Member Since 14 Feb 2010
Offline Last Active Feb 16 2013 04:24 PM

Topics I've Started

GLSL Skinning problem

21 January 2013 - 07:21 AM

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.

 


[OpenGL 3.x] Camera Matrix Problem

14 February 2010 - 06:36 PM

Hello to all members of Gamedev.net! :D My name is Patryk and I'm new here. I started programming in OGL 3.x and encountered a problem with the camera matrix. Here's the code I'm using to calculate the matrix (in Delphi):
{ .: TMainForm.Render :. }
procedure TMainForm.Render;
var
  ProjMat, Cam, ModelView: TMatrix;
begin
  glViewport(0, 0, ClientWidth, ClientHeight);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  //  Projection update
  if (ClientHeight <= 0) then
    ClientHeight := 1;
  ProjMat := MatrixProjection(40.0, ClientWidth / ClientHeight, 0.1, 1000.0);

  //  Camera transformations
  Cam := MatrixTransform(VectorNegate(Camera.Pos), VectorCreate(Camera.Pitch,
    Camera.Yaw, 0.0), VectorUniform(1.0));
  ModelView := MatrixTransform(VectorCreate(0.0, 0.0, 0.0),
    VectorCreate(0.0, TriangleRot, 0.0), VectorUniform(0.5));
  ModelView := MatrixMultiply(ModelView, Cam);
  ModelView := MatrixMultiply(ModelView, ProjMat);

  //  Update the shader
  glUniformMatrix4fv(glGetUniformLocation(ShaderManager.GLSLPrograms['minimal'].
    ProgramHandle, 'mvpmatrix'), 1, ByteBool(GL_FALSE), @ModelView);

  //  Render the triangle
  glBindVertexArray(TriangleVAO);
	glDrawArrays(GL_TRIANGLES, 0, 3);
  glBindVertexArray(0);
end;
And here's the code of my vertex shader:
#version 150

precision highp float;

uniform mat4 mvpmatrix;

in vec3 in_Position;
in vec3 in_Color;

out vec3 ex_Color;
void main(void) {
	gl_Position = mvpmatrix * vec4(in_Position, 1.0);

	ex_Color = in_Color;
}
The thing is that whenever I change either the Camera.Pitch or Camera.Yaw values, the object rotates instead of the camera. How do I change the code so that I have a Quake-like FPS camera?

PARTNERS