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