1- #version 330 core
1+ #version 420 core
2+
3+ #include "vert_uniforms.glsl"
4+ #define MAX_BONES 100
5+ #define MAX_BONE_INFLUENCE 4
26
37layout (location = 0 ) in vec3 vertex;
48layout (location = 1 ) in vec3 normal;
59layout (location = 2 ) in vec2 tex_coords;
10+ layout (location = 3 ) in vec3 tangent;
11+ layout (location = 4 ) in vec3 bitangent;
12+ layout (location = 5 ) in ivec4 bone_ids;
13+ layout (location = 6 ) in vec4 bone_weights;
614
7- uniform mat4 projection;
8- uniform mat4 view;
915uniform mat4 model;
16+ uniform mat4 bonesTransformMatrices[MAX_BONES];
17+ uniform mat4 bonesOffsetMatrices[MAX_BONES];
1018
1119out vec3 fnormal;
20+ out vec3 ftangent;
21+ out vec3 fbitangent;
1222out vec3 fposition;
1323out vec3 fview;
1424out vec2 ftex_coords;
1525
1626void main() {
17- fview = (inverse(view) * vec4 (0 , 0 , 0 , 1 )).xyz;
18- fnormal = normalize (mat3 (transpose (inverse(model))) * normal);
19- fposition = (model * vec4 (vertex, 1.0 )).xyz;
27+ vec4 totalPosition = vec4 (0 .0f);
28+ vec3 totalNormal = vec3 (0 .0f);
29+ vec3 totalTangent = vec3 (0 .0f);
30+ vec3 totalBitangent = vec3 (0 .0f);
31+
32+ if (bone_ids == ivec4 (- 1 )) {
33+ totalPosition = vec4 (vertex, 1 .0f);
34+ totalNormal = normal;
35+ } else {
36+ for (int i = 0 ; i < MAX_BONE_INFLUENCE ; i++ ) {
37+ if (bone_ids[i] == - 1 ) continue ;
38+
39+ if (bone_ids[i] >= MAX_BONES) {
40+ totalPosition = vec4 (vertex, 1 .0f);
41+ totalNormal = normal;
42+ break ;
43+ }
44+
45+ mat4 finalBonesMatrix = bonesTransformMatrices[bone_ids[i]] * bonesOffsetMatrices[bone_ids[i]];
46+
47+ totalPosition += finalBonesMatrix * vec4 (vertex, 1 .0f) * bone_weights[i];
48+
49+ mat3 normalMatrix = mat3 (transpose (inverse(finalBonesMatrix)));
50+ totalNormal += normalMatrix * normal * bone_weights[i];
51+ totalTangent += normalMatrix * tangent * bone_weights[i];
52+ totalBitangent += normalMatrix * bitangent * bone_weights[i];
53+ }
54+ }
55+
56+
57+ fposition = (model * totalPosition).xyz;
58+ mat3 normalModelMatrix = mat3 (transpose (inverse(model)));
59+ fnormal = normalize (normalModelMatrix * totalNormal);
60+ ftangent = normalize (normalModelMatrix * totalTangent);
61+ fbitangent = normalize (normalModelMatrix * totalBitangent);
62+
63+ gl_Position = uProjection * uView * model * totalPosition;
64+
65+ fview = (inverse(uView) * vec4 (0 , 0 , 0 , 1 )).xyz;
2066 ftex_coords = tex_coords;
21- gl_Position = projection * view * model * vec4 (vertex, 1.0 );
2267}
0 commit comments