Sign in to follow this  
SapphireStorm

GLSL Skeletal Animation Shader

Recommended Posts

Does this skeletal animation shader make sense?

[code]
uniform mat4 u_WorldView;
uniform mat4 u_WorldViewProjection;
uniform mat4 u_BoneTransform[128];

in vec3 in_Position;
in vec3 in_Normal;
in vec2 in_TexCoords;
in int in_BoneID[4];
in vec4 in_Weights;

out vec3 v_ViewSpacePosition;
out vec3 v_VertexToLight;
out vec3 v_Normal;
out vec2 v_TexCoords;

void main(void)
{
// Transform the vertex information based on bones.
vec3 position = (in_Weights[0] * (u_BoneTransform[ in_BoneID[0] ] * vec4(in_Position, 0.0)).xyz) +
(in_Weights[1] * (u_BoneTransform[ in_BoneID[1] ] * vec4(in_Position, 0.0)).xyz) +
(in_Weights[2] * (u_BoneTransform[ in_BoneID[2] ] * vec4(in_Position, 0.0)).xyz) +
(in_Weights[3] * (u_BoneTransform[ in_BoneID[3] ] * vec4(in_Position, 0.0)).xyz);

vec3 normal = (in_Weights[0] * (u_BoneTransform[ in_BoneID[0] ] * vec4(in_Normal, 0.0)).xyz) +
(in_Weights[1] * (u_BoneTransform[ in_BoneID[1] ] * vec4(in_Normal, 0.0)).xyz) +
(in_Weights[2] * (u_BoneTransform[ in_BoneID[2] ] * vec4(in_Normal, 0.0)).xyz) +
(in_Weights[3] * (u_BoneTransform[ in_BoneID[3] ] * vec4(in_Normal, 0.0)).xyz);

// The normal graphic's pipeline transform.
gl_Position = u_WorldViewProjection * vec4( position, 1.0 );

// Only take to view space
v_Normal = ( normalize( u_WorldView * vec4(normal, 0.0) ) ).xyz;

.
.
.
[/code]

If you need any other information, let me know.

I have never done skeletal animation before and my current attempt does not work. I can load and render my models fine with just a phong shader. However, this vertex shader does not transform anything based on the bones. It just outputs the models in their default pose as if I was only transforming vertices by world, view, and projection matrices.

I'm just trying to narrow down the possibilities of what is wrong in my code at the moment. It is also probable I'm not handling my skeletal and animation data correctly on the CPU side.

Thanks.

Share this post


Link to post
Share on other sites
I detected one bug. You need to transform positions with w=1 instead of 0, else you would not translate the position. This is not necessary for normals (needed only to be rotated).

[code]
// Transform the vertex information based on bones.
vec3 position = (in_Weights[0] * (u_BoneTransform[ in_BoneID[0] ] * vec4(in_Position, 1.0)).xyz) +
(in_Weights[1] * (u_BoneTransform[ in_BoneID[1] ] * vec4(in_Position, 1.0)).xyz) +
(in_Weights[2] * (u_BoneTransform[ in_BoneID[2] ] * vec4(in_Position, 1.0)).xyz) +
(in_Weights[3] * (u_BoneTransform[ in_BoneID[3] ] * vec4(in_Position, 1.0)).xyz);

[/code]

The rest looks ok.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this