aboutsummaryrefslogtreecommitdiff
path: root/09-september/resources/shaders/shader.vert
blob: 69a30e003b823b2bf7dc5eeaf82e114136dd1946 (plain)
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
#version 420

in layout(location=0) vec3 position;
in layout(location=1) vec2 Texture_UV;
in layout(location=2) vec3 normal;
in layout(location=3) vec3 tangent;

uniform mat4 M_MVP;   /* Total Transform matrix */
uniform mat4 M_model; /* Model to world space transformation matrix */

uniform vec3 lightPosition[4];
uniform vec3 World_eyePosition;

uniform float number_of_rows;
uniform vec2 offset;

out vec2 Fragment_UV;
out vec4 Fragment_Color;
out vec3 toLightVector[4];
out vec3 toEyeVector;

void main()
{
    /*We add a 0 on the vec4 so we can remove the translation from the matrix
	(WE DONT WANT THE NORMAL TO BE TRANSLATED) */
	vec3 n = normalize( (M_model * vec4(normal, 0.0)).xyz );
	vec3 t = normalize( (M_model * vec4(tangent, 0.0)).xyz );

	/* Orthogonalization */
	t = normalize(t - dot(t, n) * n);

	vec3 biTangent = normalize( cross(t, n) );

    /* Matrix use by normal mapping */
	mat3 tbnMatrix = mat3(t, biTangent, n);
	tbnMatrix = transpose(tbnMatrix);

	vec3 World_Position = vec3(M_model * vec4(position, 1.0));

    for(int i = 0; i < 4; i++)
    {
        /* vector que apunta hacia la luz*/
        toLightVector[i] = tbnMatrix * (lightPosition[i] - World_Position);
    }
    /* Vector hacia el ojo*/
    toEyeVector = normalize( tbnMatrix * (World_eyePosition - World_Position) );

    gl_Position = M_MVP * vec4(position, 1.0);

    Fragment_UV = vec2(Texture_UV.x, 1 - Texture_UV.y); /*Invert y axis*/
    Fragment_UV = (Fragment_UV / number_of_rows) + offset;
}