diff --git a/FModel/Resources/default.vert b/FModel/Resources/default.vert index 92f3a62f..1cae7fda 100644 --- a/FModel/Resources/default.vert +++ b/FModel/Resources/default.vert @@ -12,14 +12,15 @@ layout (location = 9) in mat4 vInstanceMatrix; layout (location = 13) in vec3 vMorphTargetPos; layout (location = 14) in vec3 vMorphTargetTangent; -uniform mat4 uView; -uniform mat4 uProjection; -uniform float uMorphTime; -layout(std430, binding = 1) buffer layoutName +layout(std430, binding = 1) buffer BoneMatrices { mat4 uFinalBonesMatrix[]; }; +uniform mat4 uView; +uniform mat4 uProjection; +uniform float uMorphTime; + out vec3 fPos; out vec3 fNormal; out vec3 fTangent; @@ -44,11 +45,12 @@ void main() if(boneIndex < 0) break; mat4 boneMatrix = uFinalBonesMatrix[boneIndex]; + mat4 inverseBoneMatrix = transpose(inverse(boneMatrix)); float weight = vBoneWeights[i]; finalPos += boneMatrix * bindPos * weight; - finalNormal += boneMatrix * bindNormal * weight; - finalTangent += boneMatrix * bindTangent * weight; + finalNormal += inverseBoneMatrix * bindNormal * weight; + finalTangent += inverseBoneMatrix * bindTangent * weight; } } else diff --git a/FModel/Resources/outline.vert b/FModel/Resources/outline.vert index 12cab0be..a7b20e4d 100644 --- a/FModel/Resources/outline.vert +++ b/FModel/Resources/outline.vert @@ -7,14 +7,15 @@ layout (location = 8) in vec4 vBoneWeights; layout (location = 9) in mat4 vInstanceMatrix; layout (location = 13) in vec3 vMorphTargetPos; +layout(std430, binding = 1) buffer BoneMatrices +{ + mat4 uFinalBonesMatrix[]; +}; + uniform mat4 uView; uniform vec3 uViewPos; uniform mat4 uProjection; uniform float uMorphTime; -layout(std430, binding = 1) buffer layoutName -{ - mat4 uFinalBonesMatrix[]; -}; void main() { @@ -34,7 +35,7 @@ void main() float weight = vBoneWeights[i]; finalPos += boneMatrix * bindPos * weight; - finalNormal += boneMatrix * bindNormal * weight; + finalNormal += transpose(inverse(boneMatrix)) * bindNormal * weight; } } else @@ -43,9 +44,10 @@ void main() finalNormal = bindNormal; } + finalPos = vInstanceMatrix * finalPos; float scaleFactor = distance(vec3(finalPos), uViewPos) * 0.0025; vec4 nor = transpose(inverse(vInstanceMatrix)) * finalNormal * scaleFactor; finalPos.xyz += nor.xyz; - gl_Position = uProjection * uView * vInstanceMatrix * finalPos; + gl_Position = uProjection * uView * finalPos; } diff --git a/FModel/Resources/picking.vert b/FModel/Resources/picking.vert index f4c40d9a..9f354e9f 100644 --- a/FModel/Resources/picking.vert +++ b/FModel/Resources/picking.vert @@ -6,14 +6,15 @@ layout (location = 8) in vec4 vBoneWeights; layout (location = 9) in mat4 vInstanceMatrix; layout (location = 13) in vec3 vMorphTargetPos; -uniform mat4 uView; -uniform mat4 uProjection; -uniform float uMorphTime; -layout(std430, binding = 1) buffer layoutName +layout(std430, binding = 1) buffer BoneMatrices { mat4 uFinalBonesMatrix[]; }; +uniform mat4 uView; +uniform mat4 uProjection; +uniform float uMorphTime; + void main() { vec4 bindPos = vec4(mix(vPos, vMorphTargetPos, uMorphTime), 1.0); @@ -26,16 +27,10 @@ void main() int boneIndex = int(vBoneIds[i]); if(boneIndex < 0) break; - mat4 boneMatrix = uFinalBonesMatrix[boneIndex]; - float weight = vBoneWeights[i]; - - finalPos += boneMatrix * bindPos * weight; + finalPos += uFinalBonesMatrix[boneIndex] * bindPos * vBoneWeights[i]; } } - else - { - finalPos = bindPos; - } + else finalPos = bindPos; gl_Position = uProjection * uView * vInstanceMatrix * finalPos; } diff --git a/FModel/Views/Snooper/Buffers/BufferObject.cs b/FModel/Views/Snooper/Buffers/BufferObject.cs index 59fc6e03..58a6cc54 100644 --- a/FModel/Views/Snooper/Buffers/BufferObject.cs +++ b/FModel/Views/Snooper/Buffers/BufferObject.cs @@ -21,6 +21,11 @@ public class BufferObject : IDisposable where TDataType : unmanaged GL.BufferData(bufferTarget, data.Length * sizeof(TDataType), data, BufferUsageHint.StaticDraw); } + public unsafe BufferObject(int length, BufferTarget bufferTarget) : this(bufferTarget) + { + GL.BufferData(bufferTarget, length * sizeof(TDataType), IntPtr.Zero, BufferUsageHint.StaticDraw); + } + public unsafe void Update(int offset, TDataType data) { GL.BufferSubData(_bufferTarget, (IntPtr) (offset * sizeof(TDataType)), sizeof(TDataType), ref data); diff --git a/FModel/Views/Snooper/Models/Animations/Skeleton.cs b/FModel/Views/Snooper/Models/Animations/Skeleton.cs index b72a24af..15d71227 100644 --- a/FModel/Views/Snooper/Models/Animations/Skeleton.cs +++ b/FModel/Views/Snooper/Models/Animations/Skeleton.cs @@ -94,7 +94,7 @@ public class Skeleton : IDisposable public void Setup() { _handle = GL.CreateProgram(); - _ssbo = new BufferObject(InvertedBonesMatrixByIndex, BufferTarget.ShaderStorageBuffer); + _ssbo = new BufferObject(InvertedBonesMatrixByIndex.Length, BufferTarget.ShaderStorageBuffer); } public void Render(float deltaSeconds = 0f, bool update = false) @@ -105,7 +105,7 @@ public class Skeleton : IDisposable if (!HasAnim) { - foreach (var boneIndex in BonesTransformByIndex.Keys) + for (int boneIndex = 0; boneIndex < InvertedBonesMatrixByIndex.Length; boneIndex++) { _ssbo.Update(boneIndex, Matrix4x4.Identity); } @@ -113,7 +113,7 @@ public class Skeleton : IDisposable else { if (update) Anim.Update(deltaSeconds); - foreach (var boneIndex in BonesTransformByIndex.Keys) + for (int boneIndex = 0; boneIndex < InvertedBonesMatrixByIndex.Length; boneIndex++) { _ssbo.Update(boneIndex, InvertedBonesMatrixByIndex[boneIndex] * Anim.InterpolateBoneTransform(boneIndex)); } @@ -129,6 +129,7 @@ public class Skeleton : IDisposable BonesTransformByIndex.Clear(); Anim?.Dispose(); + _ssbo?.Dispose(); GL.DeleteProgram(_handle); } }