-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbumpmapping.vs.glsl
executable file
·60 lines (50 loc) · 1.99 KB
/
bumpmapping.vs.glsl
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#version 410 core
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec3 tangent;
// Although the model file used in this example includes
// a bitangent, we're not using it and will calculate our
// own from the normal and tangent.
// layout (location = 3) in vec3 bitangent;
layout (location = 4) in vec2 texcoord;
out VS_OUT
{
vec2 texcoord;
vec3 eyeDir;
vec3 lightDir;
vec3 normal;
} vs_out;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
uniform vec3 light_pos = vec3(0.0, 0.0, 100.0);
void main(void)
{
// Calculate vertex position in view space.
vec4 P = mv_matrix * position;
// Calculate normal (N) and tangent (T) vectors in view space from
// incoming object space vectors.
vec3 V = P.xyz;
vec3 N = normalize(mat3(mv_matrix) * normal);
vec3 T = normalize(mat3(mv_matrix) * tangent);
// Calculate the bitangent vector (B) from the normal and tangent
// vectors.
vec3 B = cross(N, T);
// The light vector (L) is the vector from the point of interest to
// the light. Calculate that and multiply it by the TBN matrix.
vec3 L = light_pos - P.xyz;
vs_out.lightDir = normalize(vec3(dot(L, T), dot(L, B), dot(L, N)));
// The view vector is the vector from the point of interest to the
// viewer, which in view space is simply the negative of the position.
// Calculate that and multiply it by the TBN matrix.
V = -P.xyz;
vs_out.eyeDir = normalize(vec3(dot(V, T), dot(V, B), dot(V, N)));
// Pass the texture coordinate through unmodified so that the fragment
// shader can fetch from the normal and color maps.
vs_out.texcoord = texcoord;
// Pass the per-vertex normal so that the fragment shader can
// turn the normal map on and off.
vs_out.normal = N;
// Calculate clip coordinates by multiplying our view position by
// the projection matrix.
gl_Position = proj_matrix * P;
}