OpenGL Skeletal Animation (GLSL) with Assimp

I'm trying to implement GLSL-enabled skeletal animation in a program ... and instead of nice 3D animation I created a monster: https://i.imgur.com/dmpKg6n.gif

After trying several different tricks, I created a less scary but still monstrous thing:

Monster

The model is the main person in the suit, the animation should be a leg sweep while everything else remains stationary (very simple test animation). All bones have static keyframes at t = 0 in their original locations, trying to minimize their bouncing every few seconds.

The problem ... should be quite visible. Also, I'm sure the model is higher than intended.

Anyway, this was my original Vertex shader:

#version 430 core
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texcoords;
layout (location = 3) in vec4 color;
layout (location = 4) in vec4 Weights;
layout (location = 5) in vec4 BoneID;
layout (location = 0) out vec4 f_position;
layout (location = 1) out vec3 f_normal;
layout (location = 2) out vec2 f_texcoord;
const int MAX_BONES = 50;
layout (location = 1) uniform mat4 proj_matrix;
layout (location = 2) uniform mat4 mv_matrix;
layout (location = 6) uniform mat4 boneTrans[MAX_BONES];
void main(void)
{
mat4 boneTransform = (boneTrans[int(BoneID[0])] * Weights[0]) +
(boneTrans[int(BoneID[1])] * Weights[1]) +
(boneTrans[int(BoneID[2])] * Weights[2]) +
(boneTrans[int(BoneID[3])] * Weights[3]);
float rem = 1 - (Weights[0] + Weights[1] + Weights[2] + Weights[3]);
boneTransform += mat4(1.0) * rem;
f_texcoord = texcoords;
f_position = mv_matrix * (boneTransform * position);
mat4 mv_mat_simple = mv_matrix;
mv_mat_simple[3][0] = 0.0;
mv_mat_simple[3][1] = 0.0;
mv_mat_simple[3][2] = 0.0;
vec4 norm1 = boneTransform * vec4(normal, 1.0);
vec4 nnormal = mv_mat_simple * vec4(norm1.xyz, 1.0);
f_normal = nnormal.xyz / nnormal.w; // TODO: Normalize?
vec4 pos1 = boneTransform * position;
gl_Position = proj_matrix * mv_matrix * vec4(pos1.xyz, 1.0);
}

      

and this is my new one:

#version 430 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texcoords;
layout (location = 3) in vec4 color;
layout (location = 4) in vec4 Weights;
layout (location = 5) in vec4 BoneID;

layout (location = 0) out vec4 f_position;
layout (location = 1) out vec3 f_normal;
layout (location = 2) out vec2 f_texcoord;

const int MAX_BONES = 50;

layout (location = 1) uniform mat4 proj_matrix;
layout (location = 2) uniform mat4 mv_matrix;
layout (location = 6) uniform mat4 boneTrans[MAX_BONES];

void main(void)
{
    vec4 pos1 = vec4(position, 1.0);
    pos1 += (boneTrans[int(BoneID[0])] * vec4(position, 0.0)) * Weights[0];
    pos1 += (boneTrans[int(BoneID[1])] * vec4(position, 0.0)) * Weights[1];
    pos1 += (boneTrans[int(BoneID[2])] * vec4(position, 0.0)) * Weights[2];
    pos1 += (boneTrans[int(BoneID[3])] * vec4(position, 0.0)) * Weights[3];
    vec4 norm1 = vec4(normal, 1.0);
    norm1 += (boneTrans[int(BoneID[0])] * vec4(normal, 0.0)) * Weights[0];
    norm1 += (boneTrans[int(BoneID[1])] * vec4(normal, 0.0)) * Weights[1];
    norm1 += (boneTrans[int(BoneID[2])] * vec4(normal, 0.0)) * Weights[2];
    norm1 += (boneTrans[int(BoneID[3])] * vec4(normal, 0.0)) * Weights[3];
    f_texcoord = texcoords;
    f_position = mv_matrix * vec4(pos1.xyz, 1.0);
    mat4 mv_mat_simple = mv_matrix;
    mv_mat_simple[3][0] = 0.0;
    mv_mat_simple[3][1] = 0.0;
    mv_mat_simple[3][2] = 0.0;
    //vec4 norm1 = boneTransform * vec4(normal, 1.0);
    vec4 nnormal = mv_mat_simple * vec4(norm1.xyz, 1.0);
    f_normal = nnormal.xyz / nnormal.w; // TODO: Normalize?
    gl_Position = proj_matrix * mv_matrix * vec4(pos1.xyz, 1.0);
}

      

The bone processing code is neither pretty nor short: http://pastebin.com/A8x1GdUw

This code and source shader was obtained from http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html

The new shader was obtained from a Google search.

EDIT: I now get the following:

Leg twisty twisty

With this code: http://pastebin.com/PvCWUdJn

Now ... what am I doing wrong ?! What's the "correct" method to handle this? Are there any better quality tutorials I should be using instead?

UPDATE: FULL CODE SOURCE FOR TEST APP: https://github.com/mcmonkey4eva/skeletalanimationtest

+3


source to share


1 answer


So the answer is simple - don't multiply the matrix vector x, multiple times the vector matrix x in the shader.



+1


source







All Articles