Use a for loop for four bones (thanks leyyin suggestion!)

This commit is contained in:
Benau 2016-12-09 23:21:41 +08:00
parent 94f6d09bf5
commit c68492f42b
3 changed files with 22 additions and 84 deletions

View File

@ -37,43 +37,19 @@ void main(void)
{ {
mat4 ModelMatrix = getWorldMatrix(Origin, Orientation, Scale); mat4 ModelMatrix = getWorldMatrix(Origin, Orientation, Scale);
mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin, Orientation, Scale) * InverseViewMatrix); mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin, Orientation, Scale) * InverseViewMatrix);
vec4 idle_position = vec4(Position, 1.); vec4 idle_position = vec4(Position, 1.);
vec4 idle_normal = vec4(Normal, 0.); vec4 idle_normal = vec4(Normal, 0.);
vec4 skinned_position = vec4(0.); vec4 skinned_position = vec4(0.);
vec4 skinned_normal = vec4(0.); vec4 skinned_normal = vec4(0.);
// Note : For normal we assume no scale factor in bone (otherwise we'll have to compute inversematrix for each bones...) // Note : For normal we assume no scale factor in bone (otherwise we'll have to compute inversematrix for each bones...)
vec4 single_bone_influenced_position; for (int i = 0; i < 4; i++)
vec4 single_bone_influenced_normal; {
vec4 single_bone_influenced_position = joint_matrices[clamp(Joint[i] + skinning_offset, 0, MAX_BONES)] * idle_position;
// First bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[0] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w; single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[0] + skinning_offset, 0, MAX_BONES)] * idle_normal; vec4 single_bone_influenced_normal = joint_matrices[clamp(Joint[i] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[0] * single_bone_influenced_position; skinned_position += Weight[i] * single_bone_influenced_position;
skinned_normal += Weight[0] * single_bone_influenced_normal; skinned_normal += Weight[i] * single_bone_influenced_normal;
}
// Second bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[1] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[1] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[1] * single_bone_influenced_position;
skinned_normal += Weight[1] * single_bone_influenced_normal;
// Third bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[2] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[2] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[2] * single_bone_influenced_position;
skinned_normal += Weight[2] * single_bone_influenced_normal;
// Fourth bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[3] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[3] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[3] * single_bone_influenced_position;
skinned_normal += Weight[3] * single_bone_influenced_normal;
gl_Position = ProjectionViewMatrix * ModelMatrix * skinned_position; gl_Position = ProjectionViewMatrix * ModelMatrix * skinned_position;
// Keep orthogonality // Keep orthogonality

View File

@ -32,27 +32,12 @@ void main(void)
mat4 ModelMatrix = getWorldMatrix(Origin, Orientation, Scale); mat4 ModelMatrix = getWorldMatrix(Origin, Orientation, Scale);
vec4 idle_position = vec4(Position, 1.); vec4 idle_position = vec4(Position, 1.);
vec4 skinned_position = vec4(0.); vec4 skinned_position = vec4(0.);
vec4 single_bone_influenced_position; for (int i = 0; i < 4; i++)
{
// First bone: vec4 single_bone_influenced_position = joint_matrices[clamp(Joint[i] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position = joint_matrices[clamp(Joint[0] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w; single_bone_influenced_position /= single_bone_influenced_position.w;
skinned_position += Weight[0] * single_bone_influenced_position; skinned_position += Weight[i] * single_bone_influenced_position;
}
// Second bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[1] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
skinned_position += Weight[1] * single_bone_influenced_position;
// Third bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[2] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
skinned_position += Weight[2] * single_bone_influenced_position;
// Fourth bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[3] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
skinned_position += Weight[3] * single_bone_influenced_position;
#ifdef VSLayer #ifdef VSLayer
gl_Layer = layer; gl_Layer = layer;

View File

@ -41,38 +41,15 @@ void main(void)
vec4 idle_normal = vec4(Normal, 0.); vec4 idle_normal = vec4(Normal, 0.);
vec4 skinned_position = vec4(0.); vec4 skinned_position = vec4(0.);
vec4 skinned_normal = vec4(0.); vec4 skinned_normal = vec4(0.);
// Note : For normal we assume no scale factor in bone (otherwise we'll have to compute inversematrix for each bones...) // Note : For normal we assume no scale factor in bone (otherwise we'll have to compute inversematrix for each bones...)
vec4 single_bone_influenced_position; for (int i = 0; i < 4; i++)
vec4 single_bone_influenced_normal; {
vec4 single_bone_influenced_position = joint_matrices[clamp(Joint[i] + skinning_offset, 0, MAX_BONES)] * idle_position;
// First bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[0] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w; single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[0] + skinning_offset, 0, MAX_BONES)] * idle_normal; vec4 single_bone_influenced_normal = joint_matrices[clamp(Joint[i] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[0] * single_bone_influenced_position; skinned_position += Weight[i] * single_bone_influenced_position;
skinned_normal += Weight[0] * single_bone_influenced_normal; skinned_normal += Weight[i] * single_bone_influenced_normal;
}
// Second bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[1] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[1] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[1] * single_bone_influenced_position;
skinned_normal += Weight[1] * single_bone_influenced_normal;
// Third bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[2] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[2] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[2] * single_bone_influenced_position;
skinned_normal += Weight[2] * single_bone_influenced_normal;
// Fourth bone:
single_bone_influenced_position = joint_matrices[clamp(Joint[3] + skinning_offset, 0, MAX_BONES)] * idle_position;
single_bone_influenced_position /= single_bone_influenced_position.w;
single_bone_influenced_normal = joint_matrices[clamp(Joint[3] + skinning_offset, 0, MAX_BONES)] * idle_normal;
skinned_position += Weight[3] * single_bone_influenced_position;
skinned_normal += Weight[3] * single_bone_influenced_normal;
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * skinned_position; gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * skinned_position;
// Keep orthogonality // Keep orthogonality