[12115] | 1 | #version 120 |
---|
| 2 | |
---|
| 3 | /* Bump mapping vertex program |
---|
| 4 | In this program, we want to calculate the tangent space light vector |
---|
| 5 | on a per-vertex level which will get passed to the fragment program, |
---|
| 6 | or to the fixed function dot3 operation, to produce the per-pixel |
---|
| 7 | lighting effect. |
---|
| 8 | */ |
---|
| 9 | // parameters |
---|
| 10 | uniform vec4 lightPosition; // object space |
---|
| 11 | uniform mat4 worldViewProj; |
---|
| 12 | |
---|
| 13 | attribute vec4 vertex; |
---|
| 14 | attribute vec3 normal; |
---|
| 15 | attribute vec3 tangent; |
---|
| 16 | attribute vec2 uv0; |
---|
| 17 | |
---|
| 18 | varying vec2 oUv0; |
---|
| 19 | varying vec3 oTSLightDir; |
---|
| 20 | |
---|
| 21 | void main() |
---|
| 22 | { |
---|
| 23 | // Calculate output position |
---|
| 24 | gl_Position = worldViewProj * vertex; |
---|
| 25 | |
---|
| 26 | // Pass the main uvs straight through unchanged |
---|
| 27 | oUv0 = uv0; |
---|
| 28 | |
---|
| 29 | // Calculate tangent space light vector |
---|
| 30 | // Get object space light direction |
---|
| 31 | // Non-normalised since we'll do that in the fragment program anyway |
---|
| 32 | vec3 lightDir = lightPosition.xyz - (vertex * lightPosition.w).xyz; |
---|
| 33 | |
---|
| 34 | // Calculate the binormal (NB we assume both normal and tangent are |
---|
| 35 | // already normalised) |
---|
| 36 | |
---|
| 37 | // Fixed handedness |
---|
| 38 | vec3 binormal = cross(normal, tangent); |
---|
| 39 | |
---|
| 40 | // Form a rotation matrix out of the vectors, column major for glsl es |
---|
| 41 | mat3 rotation = mat3(vec3(tangent[0], binormal[0], normal[0]), |
---|
| 42 | vec3(tangent[1], binormal[1], normal[1]), |
---|
| 43 | vec3(tangent[2], binormal[2], normal[2])); |
---|
| 44 | |
---|
| 45 | // Transform the light vector according to this matrix |
---|
| 46 | oTSLightDir = rotation * lightDir; |
---|
| 47 | } |
---|