-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalMapping.vertexshader
More file actions
39 lines (31 loc) · 1.24 KB
/
NormalMapping.vertexshader
File metadata and controls
39 lines (31 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 vertexNormal_modelspace;
layout(location = 3) in vec3 vertexTangent_modelspace;
layout(location = 4) in vec3 vertexBitangent_modelspace;
out vec2 UV;
out vec3 FragPos_tangentspace;
out vec3 ViewPos_tangentspace;
#define MAX_LIGHTS 9
out vec3 LightPos_tangentspace[MAX_LIGHTS];
uniform mat4 MVP;
uniform mat4 M;
uniform vec3 ViewPos_worldspace;
uniform vec3 LightPositions[MAX_LIGHTS];
void main(){
gl_Position = MVP * vec4(vertexPosition_modelspace, 1.0);
UV = vertexUV;
vec3 FragPos_worldspace = vec3(M * vec4(vertexPosition_modelspace, 1.0));
// 1. Create TBN Matrix
vec3 T = normalize(vec3(M * vec4(vertexTangent_modelspace, 0.0)));
vec3 B = normalize(vec3(M * vec4(vertexBitangent_modelspace, 0.0)));
vec3 N = normalize(vec3(M * vec4(vertexNormal_modelspace, 0.0)));
mat3 TBN = transpose(mat3(T, B, N)); // Inverse matrix
// 2. Transform everything to Tangent Space
FragPos_tangentspace = TBN * FragPos_worldspace;
ViewPos_tangentspace = TBN * ViewPos_worldspace;
for(int i=0; i<MAX_LIGHTS; i++){
LightPos_tangentspace[i] = TBN * LightPositions[i];
}
}