learn-opengl/45-animation/shaders/45-framebuffers.vert.glsl
2026-10-08 14:34:08 -04:00

64 lines
1.7 KiB
GLSL

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent;
layout (location = 5) in ivec4 boneIds;
layout (location = 6) in vec4 weights;
layout (location = 7) in int bone_count;
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;
out vec4 FragPosLightSpace;
out mat3 TBN;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat4 lightSpaceMatrix;
uniform bool hasBones;
const int MAX_BONES = 100;
uniform mat4 finalBonesMatrices[MAX_BONES];
vec4 AnimateBones() {
vec4 totalPosition = vec4(0.0f);
for(int i = 0; i < bone_count; i++) {
int bone_id = boneIds[i];
if(bone_id >= MAX_BONES) {
totalPosition = vec4(aPos, 1.0f);
break;
}
// bone found, mark it
vec4 localPosition = finalBonesMatrices[bone_id] * vec4(aPos, 1.0f);
totalPosition += localPosition * weights[i];
vec3 localNormal = mat3(finalBonesMatrices[bone_id]) * aNormal;
}
if(totalPosition == vec4(0.0f)) {
return vec4(aPos, 1.0f);
} else {
return totalPosition;
}
}
void main()
{
vec4 totalPosition = hasBones ? AnimateBones() : vec4(aPos, 1.0);
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = transpose(inverse(mat3(model))) * aNormal;
TexCoords = aTexCoords;
FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0);
gl_Position = projection * view * model * totalPosition;
vec3 T = normalize(vec3(model * vec4(aTangent, 0.0)));
vec3 B = normalize(vec3(model * vec4(aBitangent, 0.0)));
vec3 N = normalize(vec3(model * vec4(aNormal, 0.0)));
TBN = mat3(T, B, N);
}