0
\$\begingroup\$

I have created a simple application to make skinning work with OpenGL. so I want to share with my vertex shader:

#version 330 core

layout (location = 0) in vec3 RealPos;
layout (location = 1) in vec3 vertex_color;
layout (location = 2) in vec2 vertex_textcoord;
layout (location = 3) in vec3 RealNor;

layout (location = 4) in vec4 Joint;
layout (location = 5) in vec4 Weight;

uniform mat4 u_jointMatrix[2];

out vec2 vs_text;
out vec3 vs_color;
out vec3 normal;
out vec3 FragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform bool isSkin;

void main()
{

    mat4 skinMatrix = Weight.x * u_jointMatrix[int(Joint.x)] + Weight.y * u_jointMatrix[int(Joint.y)] + Weight.z * u_jointMatrix[int(Joint.z)] + Weight.w * u_jointMatrix[int(Joint.w)];

    vs_text = vertex_textcoord;

    vs_color = vertex_color;

    FragPos = vec3(model * vec4(RealPos, 1.0f));

    normal = mat3(transpose(inverse(model))) * RealNor;

    if(isSkin)
        gl_Position = projection * view * model * skinMatrix * vec4(RealPos, 1.0f);
    else
        gl_Position = projection * view * model * vec4(RealPos, 1.0f);
}

so as you can see, if my model is skinned then it will call the first statement. and here my C++ code:

unsigned j = 0;
for (unsigned u = 0; u < m.numOfJoints; u++)
{
    glm::mat4 g = glm::mat4(inverseBindMatriceHandler.at(j), inverseBindMatriceHandler.at(j + 1), 
    inverseBindMatriceHandler.at(j + 2), inverseBindMatriceHandler.at(j + 
     3),inverseBindMatriceHandler.at(j + 4), inverseBindMatriceHandler.at(j + 5), 
     inverseBindMatriceHandler.at(j + 6), inverseBindMatriceHandler.at(j + 7), 
     inverseBindMatriceHandler.at(j + 8), inverseBindMatriceHandler.at(j + 9), 
     inverseBindMatriceHandler.at(j + 10), inverseBindMatriceHandler.at(j + 11), 
     inverseBindMatriceHandler.at(j + 12), inverseBindMatriceHandler.at(j + 13), 
     inverseBindMatriceHandler.at(j + 14), inverseBindMatriceHandler.at(j + 15));
     m.inverseBindMatrice.push_back(g);
     glm::mat4 gTransform = glm::inverse(g);
     m.globalTransform.push_back(gTransform);
     j += 16 ;
}
                        }

 if (Model.isSkinned)
{
    for (unsigned l = 0; l < Model.numOfJoints; l++)
        jointM[l] = Model.globalTransform.at(l) * Model.inverseBindMatrice.at(l);
    int isSkinLoc = glGetUniformLocation(program, "isSkin");
    glUniform1i(isSkinLoc, 1);
    for (unsigned l = 0; l < Model.numOfJoints; l++) //2
    {
        std::string name = "u_jointMatrix[" + std::to_string(l) + "]";
        int jointMatrixLoc = glGetUniformLocation(program, name.c_str());
        glUniformMatrix4fv(jointMatrixLoc, 1, GL_FALSE, glm::value_ptr(jointM[l]));
    }
}
else
{
    int isSkinLoc = glGetUniformLocation(program, "isSkin");
    glUniform1i(isSkinLoc, 0);
}

inverseBindMatriceHandler is a vector.

I have 2 joints (numOfJoints).

I can't understand why my model doesn't work properly.

can someone help me?

Thanks

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.