I successfully did the skeletal animation using Quaternion Translation, (thanks to GClements & Dark Poton) but it is in immediate mode, now I try to convert the codes to the GLSL. I did the QT computation in my app and send the QT to the shader by converting it to matrix 2x4 and do the mesh deformation in the shader.
Here is my shader code:
#version 400 core
in vec3 GL_Vertex;
in vec2 GL_TexCoord;
in vec3 GL_Normal;
in int GL_Bone;
out vec2 GL_TexCoord0;
uniform mat4 m_Perspective, m_View, m_Model;
uniform mat2x4 m_QT[100];
vec3 Rotate_Vector(in vec4 q, in vec3 v)
{
return v + 2.0*cross(cross(v, q.xyz) + q.w*v, q.xyz);
}
void main()
{
vec4 q = vec4(m_QT[GL_Bone][0][0], m_QT[GL_Bone][0][1], m_QT[GL_Bone][0][2], m_QT[GL_Bone][0][3]);
vec3 t = vec3(m_QT[GL_Bone][0][0], m_QT[GL_Bone][1][1], m_QT[GL_Bone][1][2]);
vec3 vVertex = Rotate_Vector(q, GL_Vertex) + t;
mat4 mModelView = m_View * m_Model;
gl_Position = m_Perspective * mModelView * vec4(vVertex, 1.0);
GL_TexCoord0 = GL_TexCoord;
}
However, when I try to rotate the vertex position with the QT, it seems the result is 0. I tried to debug it in shader like this:
if (vVertex == GL_Vertex) {
vVertex = vec3(0.0, 0.0, 0.0);
}
and results the model not drawn which means the rotate_vector computation returns 0 so I assume like that. So far the model drawn without problem in the reference position but there’s no animation since the function returns 0.
I tried to debug the QT before send it to the shader, they are fine. Also try to compute vertex rotation in my app the QT is still fine, I don’t know why it returns 0 when computed in shader. I stored the QT in float m[2][4], first row is quaternion and second row is translation, they are in order x, y, z, w.
This is how I send it to the shader:
Shader_LoadUniformMatrix2x4fv(iShader, "m_QT", pModel->iBone, GL_FALSE, &(pModel->pBone[0].m[0][0]));
Thank you before!