1 | #version 150 |
---|
2 | |
---|
3 | // General functions |
---|
4 | // parameters |
---|
5 | uniform vec4 lightPosition; // object space |
---|
6 | uniform vec3 eyePosition; // object space |
---|
7 | uniform mat4 worldViewProj; |
---|
8 | |
---|
9 | in vec4 vertex; |
---|
10 | in vec3 normal; |
---|
11 | in vec3 tangent; |
---|
12 | in vec4 uv0; |
---|
13 | |
---|
14 | out vec4 oUv0; |
---|
15 | out vec3 oTSLightDir; |
---|
16 | out vec3 oTSHalfAngle; |
---|
17 | |
---|
18 | /* Vertex program which includes specular component */ |
---|
19 | void main() |
---|
20 | { |
---|
21 | // calculate output position |
---|
22 | gl_Position = worldViewProj * vertex; |
---|
23 | |
---|
24 | // pass the main uvs straight through unchanged |
---|
25 | oUv0 = uv0; |
---|
26 | |
---|
27 | // calculate tangent space light vector |
---|
28 | // Get object space light direction |
---|
29 | vec3 lightDir = normalize(lightPosition.xyz - (vertex * lightPosition.w).xyz); |
---|
30 | |
---|
31 | // Calculate the binormal (NB we assume both normal and tangent are |
---|
32 | // already normalised) |
---|
33 | vec3 binormal = cross(normal, tangent); |
---|
34 | |
---|
35 | // Form a rotation matrix out of the vectors |
---|
36 | mat3 rotation = mat3(vec3(tangent[0], binormal[0], normal[0]), |
---|
37 | vec3(tangent[1], binormal[1], normal[1]), |
---|
38 | vec3(tangent[2], binormal[2], normal[2])); |
---|
39 | |
---|
40 | // Transform the light vector according to this matrix |
---|
41 | oTSLightDir = rotation * lightDir; |
---|
42 | |
---|
43 | // Calculate half-angle in tangent space |
---|
44 | vec3 eyeDir = normalize(eyePosition - vertex.xyz); |
---|
45 | vec3 halfAngle = normalize(eyeDir + lightDir); |
---|
46 | oTSHalfAngle = rotation * halfAngle; |
---|
47 | } |
---|