c# - Skeletal Animation in OpenGL (GLSL) with Assimp -
i trying implement glsl-powered skeletal animation in program... , rather beautiful 3d animation, i've created monster: https://i.imgur.com/dmpkg6n.gif
after trying few different techniques, created less terrifying still monstrous thing:

the model basic human in suit, animation supposed leg swinging while else stays still (a simple test animation). bones have static keyframes @ t=0 in original locations in attempt minimize them jumping out of place every few seconds.
the problem... should visible. also, i'm sure model taller it's supposed be.
anyway, 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 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 handling code neither pretty nor short: http://pastebin.com/a8x1gduw
that code , original shader derived http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html
the new shader derived googling @ random.
edit: this:

with code: http://pastebin.com/pvcwudjn
now... doing wrong?! what's "proper" method handle this? there higher-quality tutorials should use in place of one?
update: full source code test app: https://github.com/mcmonkey4eva/skeletalanimationtest
so answer simple - don't multiply matrix x vector, multiple vector x matrix in shader.
Comments
Post a Comment