d8c97f4df3c9a73169770d7dcca7a512782a70fe
[opengl.git] / texturevertex.glsl
1 #version 330
2 in vec3 vPosition;
3 in vec4 vColor;
4 in vec3 vNormal;
5 in vec3 tangent;
6 in vec3 bitangent;
7 in vec2 vTexCoord;
8 uniform mat4 model;
9 uniform mat4 view;
10 uniform mat4 projection;
11 out vec3 defNormal;
12 out vec2 texCoord;
13
14 uniform vec3 vLightPos;
15 uniform vec3 vViewPos;
16
17 out vec3 lightPos;
18 out vec3 viewPos;
19 out vec3 fragPos;
20
21 struct Material {
22         vec3 ambient;
23         vec3 diffuse;
24         sampler2D diffuseMap;
25         vec3 specular;
26         sampler2D specularMap;
27         float shininess;
28         
29         bool hasTexture;
30         bool hasSpecularMap;
31
32         sampler2D normalMap;
33         bool hasNormalMap;
34 };
35
36 uniform Material material;
37
38
39 void main() {
40         vec4 pos = model * vec4(vPosition, 1.f);
41         defNormal = mat3(transpose(inverse(model))) * vNormal;
42         texCoord = vTexCoord;
43
44         //tangent space stuff
45         if (material.hasNormalMap) {
46                 vec3 T = normalize(vec3(model * vec4(tangent,   0.0)));
47                 vec3 B = normalize(vec3(model * vec4(bitangent, 0.0)));
48                 vec3 N = normalize(vec3(model * vec4(vNormal,    0.0)));
49                 // convert TBN to world->tangent space
50                 mat3 TBN = transpose(mat3(T, B, N));
51                 lightPos = TBN * vLightPos;
52                 viewPos = TBN * vViewPos;
53                 fragPos = TBN * vec3(pos);
54         } else {
55                 lightPos = vLightPos;
56                 viewPos = vViewPos;
57                 fragPos = vec3(pos);
58         }
59         
60         gl_Position = projection * view * pos;
61 }