1 | #version 120 |
---|
2 | |
---|
3 | uniform vec4 lightPosition; // object space |
---|
4 | uniform vec3 eyePosition; // object space |
---|
5 | uniform mat4 worldViewProj; // not actually used but here for compat with HLSL |
---|
6 | |
---|
7 | varying vec3 oEyeDir; |
---|
8 | varying vec3 oLightDir; |
---|
9 | varying vec3 oHalfAngle; |
---|
10 | varying vec4 oUv0; |
---|
11 | |
---|
12 | attribute vec3 normal; |
---|
13 | attribute vec3 tangent; |
---|
14 | attribute vec4 uv0; |
---|
15 | attribute vec4 position; |
---|
16 | |
---|
17 | /* Vertex program that moves light and eye vectors into texture tangent space at vertex */ |
---|
18 | |
---|
19 | void main() |
---|
20 | { |
---|
21 | // Calculate output position |
---|
22 | gl_Position = worldViewProj * position; |
---|
23 | |
---|
24 | // Pass the main uvs straight through unchanged |
---|
25 | oUv0 = uv0; |
---|
26 | |
---|
27 | vec3 lightDir = lightPosition.xyz - (position.xyz * lightPosition.w); |
---|
28 | |
---|
29 | vec3 eyeDir = eyePosition - position.xyz; |
---|
30 | |
---|
31 | // Calculate the binormal (NB we assume both normal and tangent are |
---|
32 | // already normalised) |
---|
33 | // NB looks like nvidia cross params are BACKWARDS to what you'd expect |
---|
34 | // this equates to NxT, not TxN |
---|
35 | vec3 localbinormal = cross(tangent, normal); |
---|
36 | |
---|
37 | // Form a rotation matrix out of the vectors, column major for glsl es |
---|
38 | mat3 TBN = mat3(vec3(tangent[0], localbinormal[0], normal[0]), |
---|
39 | vec3(tangent[1], localbinormal[1], normal[1]), |
---|
40 | vec3(tangent[2], localbinormal[2], normal[2])); |
---|
41 | |
---|
42 | |
---|
43 | // Transform the light vector according to this matrix |
---|
44 | oLightDir = normalize(TBN * lightDir); |
---|
45 | oEyeDir = normalize(TBN * eyeDir); |
---|
46 | oHalfAngle = normalize(oEyeDir + oLightDir); |
---|
47 | } |
---|