Search code examples
c++openglglm-mathassimp

Incorrect Bone Transforms Assimp


Im having troubles with creating skeletal animation, my model is having incorrect transformations

    void Animation(glm::mat4 a[])
    {
        float Factor= fmod(glfwGetTime(),1.0);
        for(int b=0;b<BoneList.size();b++)
        {
    
            Bone *BoneT = &BoneList[b];
            aiMatrix4x4 temp = inverse;
            while(BoneT)
            {
    
                    aiVector3D sc= BoneT->ScaleFrame[0] + (Factor * (BoneT->ScaleFrame[1] - BoneT->ScaleFrame[0]));
                    aiMatrix4x4 S=aiMatrix4x4();
                    S[0][0]=sc.x;
                    S[1][1]=sc.y;
                    S[2][2]=sc.z; 

                    aiVector3D tr= BoneT->LocFrame[0] + (Factor * (BoneT->LocFrame[1] - BoneT->LocFrame[0]));
                    aiMatrix4x4 T=aiMatrix4x4();
                    T[0][3]=tr.x;
                    T[1][3]=tr.y;
                    T[2][3]=tr.z;

                    aiQuaternion R;
                    aiQuaternion::Interpolate(R, BoneT->RotFrame[0], BoneT->RotFrame[1], Factor);
                    R = R.Normalize();

                    temp*=BoneT->NodeTransform*(T* aiMatrix4x4(R.GetMatrix()) * S );
                    BoneT=BoneT->BoneParent;
                
            }
            temp*=BoneList[b].offset;
            temp.Transpose();
            ai_to_glm(temp,a[b]);
        }
    }

im creating a Temp aiMatrix4x4 to preserve assimp matrix multiplcation order, then i convert the aiMatrix4x4 to glm::mat4 using function:

     void ai_to_glm(const aiMatrix4x4 &from, glm::mat4 &to)
{
    to[0][0] = from[0][0];
    to[0][1] = from[0][1];
    to[0][2] = from[0][2];
    to[0][3] = from[0][3];

    to[1][0] = from[1][0];
    to[1][1] = from[1][1];
    to[1][2] = from[1][2];
    to[1][3] = from[1][3];

    to[2][0] = from[2][0];
    to[2][1] = from[2][1];
    to[2][2] = from[2][2];
    to[2][3] = from[2][3];

    to[3][0] = from[3][0];
    to[3][1] = from[3][1];
    to[3][2] = from[3][2];
    to[3][3] = from[3][3];
}

however the end frame of the model looks like this:blender and program comparision

i noticed if i removed the translation matrix from the function, the model looks closer what it was supposed to beenter image description here

the skinning is done in shader

layout (location = 0) in vec3 aPos; 
layout (location = 1) in vec2 Tpos;
layout (location = 2) in ivec4 Bones;
layout (location = 3) in vec4 Weight;
uniform mat4 view;
uniform mat4 proj;
uniform mat4 LocRot;
uniform mat4 Test[8];
out vec3 vertexColor;
out vec2 TextCoord;

void main()
{
    mat4 BoneTransform = Test[Bones.x]* Weight.x;
    BoneTransform += Test[Bones.y] * Weight.y;
  BoneTransform += Test[Bones.z] * Weight.z;
   BoneTransform += Test[Bones.w] * Weight.w;
    gl_Position = proj * view *LocRot* BoneTransform *vec4(aPos, 1.0);
    vertexColor = vec3(1,1,1);
    TextCoord = Tpos;
}

and the uniform is acessed

    for(int i=0; i<4;i++)
    {
        glUniformMatrix4fv(glGetUniformLocation(ActiveShader->ID, "Test[0]")+i, 1, GL_FALSE, glm::value_ptr(AnimMatrix[i]));
    }

what i am aware: -inverse matrix is identity matrix, ,which doesnt do anything right now to this model.

-some weight sum arent equal 1.0 but i think its not the problem

-changing matrix multiplication order doesnt solve it

model is created and exported in blender

link to model https://send.firefox.com/download/fe0b85d3f4581630/#6S0Vr9EIjgLNN03rerMW0w

my bet is that ai_to_glm function is at fault here, but i am not sure.

Edit: I noticed that rotations are flipped aswell, as shown on images, however multiplying it by inverseai (inverted root bone transformation) does nothing.

Update: i transposed the assimp matrix before conversion and it fixed most problems, but the offsets and parent inheritance is bugged out


Solution

  • before any suspiction, i had no idea that i had account on stack overflow, and i answer this question from my real account

    to fix this, it required multiple things:

    1. Iterating bones and assigning children/parents were using unstable pointers and were corrupted, after solving it it fixed the major thing

    2. i have used codingadventures's answer from question Matrix calculations for gpu skinning

    3. my ai_to_glm was wrong, and after replacing it with

      glm::mat4 ai_to_glm(aiMatrix4x4* from) { glm::mat4 to = glm::mat4(1.0f); to[0][0] = (GLfloat)from->a1; to[0][1] = (GLfloat)from->b1; to[0][2] = (GLfloat)from->c1; to[0][3] = (GLfloat)from->d1; to[1][0] = (GLfloat)from->a2; to[1][1] = (GLfloat)from->b2; to[1][2] = (GLfloat)from->c2; to[1][3] = (GLfloat)from->d2; to[2][0] = (GLfloat)from->a3; to[2][1] = (GLfloat)from->b3; to[2][2] = (GLfloat)from->c3; to[2][3] = (GLfloat)from->d3; to[3][0] = (GLfloat)from->a4; to[3][1] = (GLfloat)from->b4; to[3][2] = (GLfloat)from->c4; to[3][3] = (GLfloat)from->d4; return to; };

    after doing that, it got fixed