My “engine” is heavily based on the article in learnopengl (skeletal animation)
The overall Question: How to attach weapon to “character arm”?
Although animations work for all characters and animations at mixamorg website including multiple animations im having the hardest time trying to take a separate model and attach that model to the character’s ARM/Hand.
void ArmedAnimatedModel::attachHand(const glm::vec3& playerPosition) {
std::string boneName = "mixamorig_RightHand";
std::string parentBoneName = "mixamorig_RightForeArm";
Bone* bone = GetBoneByName(boneName);
Bone* parentBone = GetBoneByName(parentBoneName);
glm::mat4 childfinalBoneMatrix = GetFinalBoneMatrixByID(bone->GetBoneID());
glm::mat4 parentfinalBoneMatrix = GetFinalBoneMatrixByID(parentBone->GetBoneID());
// Combine the parent and child bone transformations maybe?
glm::mat4 finalBoneMatrix = childfinalBoneMatrix;
glm::mat4 weaponModelMatrix = glm::mat4(1.0f);
weaponModelMatrix = glm::scale(weaponModelMatrix, glm::vec3(0.01f));
weaponModelMatrix = glm::translate(weaponModelMatrix, glm::vec3(X,Y,Z));
LogMatrix(weaponModelMatrix, "final Weapon Model matrix");
weaponModel->GetShader()->use();
LogMatrix(finalBoneMatrix, "finalBoneMatrix");
auto transforms = finalBoneMatrix;
weaponModel->GetShader()->setMat4("finalBoneMatrix", transforms);
GLint uniformLocation = glGetUniformLocation(weaponModel->GetShader()->ID,
"finalBoneMatrix");
glUniformMatrix4fv(uniformLocation, 1, GL_FALSE,&transforms[0][0]);
GLuint modelLoc = glGetUniformLocation(weaponModel->GetShader()->ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(weaponModelMatrix));
weaponModel->Draw();
}
#version 330 core
layout(location = 0) in vec3 aPos; // Vertex position
layout(location = 1) in vec3 aNormal; // Vertex normal
layout(location = 2) in vec2 aTexCoord; // Texture coordinates
out vec2 TexCoord;
out vec3 Normal;
out vec3 FragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 finalBoneMatrix;
void main()
{
vec4 totalPosition = vec4(0.0f);
Normal = mat3(transpose(inverse(model))) * aNormal; // Correct normal transformat
ion
TexCoord = aTexCoord;
vec4 localPosition = finalBoneMatrix * vec4(aPos,1.0f);
totalPosition += localPosition;
mat4 viewModel = view * model;
gl_Position = projection * viewModel * totalPosition; // Final position in clip s
pace
}
I tried many alternative approaches including multiplying parent by child matrices, but i never get the right location of the arm in WORLD Space or Model Space.
I know the bones are right because its correct relative to each other AND animations work without a hitch…
Although the weapon does render it is at Character’s feet glm::vec3(0,0,0)
and i noted that when changing anmiations from aiming to walking the gun displaces UP Y direction rather than negative Y direction which is what we would have expected…
Please advise how should the matrix multiplication (if any) work to move from Bone space to model space at the location of Character Arm/Hand?