scg3  0.6
bump_vert.glsl
Go to the documentation of this file.
1 /**
2  * \file bump_vert.glsl
3  * \brief Bump mapping vertex shader.
4  */
5 
6 #version 150
7 
8 in vec4 vVertex;
9 in vec3 vNormal;
10 in vec4 vTexCoord0;
11 in vec3 vTangent;
12 in vec3 vBinormal;
13 
14 const int MAX_NUMBER_OF_LIGHTS = 10;
15 
16 struct Light {
17  vec4 position;
18  vec4 ambient;
19  vec4 diffuse;
20  vec4 specular;
21  vec4 halfVector; // used as vec3, expected as normalized
22  vec4 spotDirection; // used as vec3, expected as normalized
23  float spotCosCutoff;
24  float spotExponent;
25 };
26 
27 layout(std140) uniform LightBlock {
28  Light lights[MAX_NUMBER_OF_LIGHTS];
29 };
30 
31 uniform int nLights;
32 uniform mat4 modelViewMatrix;
33 uniform mat4 projectionMatrix;
34 uniform mat4 mvpMatrix;
35 uniform mat3 normalMatrix;
36 uniform mat4 textureMatrix;
37 
38 smooth out vec3 ecVertex;
39 smooth out vec4 texCoord0;
40 smooth out vec3 tcView;
41 smooth out vec3 tcSource[MAX_NUMBER_OF_LIGHTS];
42 
43 void main() {
44 
45  // transform vertex position, normal, tangent, and binormal into eye coordinates
46  ecVertex = (modelViewMatrix * vVertex).xyz;
47  vec3 ecNormal = normalMatrix * vNormal;
48  vec3 ecTangent = normalMatrix * vTangent;
49  vec3 ecBinormal = normalMatrix * vBinormal;
50 
51  // create transformation to tangent space
52  vec3 ecN = normalize(ecNormal);
53  vec3 ecT = normalize(ecTangent);
54  vec3 ecB = normalize(ecBinormal);
55  mat3 ec2tcTrans = mat3(
56  ecT.x, ecB.x, ecN.x,
57  ecT.y, ecB.y, ecN.y,
58  ecT.z, ecB.z, ecN.z );
59 
60  // transform view direction to tangent space
61  tcView = ec2tcTrans * (-ecVertex);
62 
63  // transform light source directions to tangent space
64  for (int i = 0; i < nLights; ++i) {
65  if (lights[i].position.w < 0.001) {
66  // directional light
67  tcSource[i] = ec2tcTrans * lights[i].position.xyz;
68  }
69  else {
70  // point or spot light
71  tcSource[i] = ec2tcTrans * (lights[i].position.xyz - ecVertex);
72  }
73  }
74 
75  // set output values
76  gl_Position = mvpMatrix * vVertex;
77  texCoord0 = textureMatrix * vTexCoord0;
78 }