1 | #version 100 |
---|
2 | precision mediump int; |
---|
3 | precision mediump float; |
---|
4 | |
---|
5 | // General functions |
---|
6 | // parameters |
---|
7 | uniform vec4 lightPosition; // object space |
---|
8 | uniform vec3 eyePosition; // object space |
---|
9 | uniform mat4 worldViewProj; |
---|
10 | |
---|
11 | attribute vec4 vertex; |
---|
12 | attribute vec3 normal; |
---|
13 | attribute vec3 tangent; |
---|
14 | attribute vec4 uv0; |
---|
15 | |
---|
16 | varying vec4 oUv0; |
---|
17 | varying vec3 oTSLightDir; |
---|
18 | varying vec3 oTSHalfAngle; |
---|
19 | |
---|
20 | /* Vertex program which includes specular component */ |
---|
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 | vec3 lightDir = normalize(lightPosition.xyz - (vertex * lightPosition.w).xyz); |
---|
32 | |
---|
33 | // Calculate the binormal (NB we assume both normal and tangent are |
---|
34 | // already normalised) |
---|
35 | vec3 binormal = cross(normal, tangent); |
---|
36 | |
---|
37 | // Form a rotation matrix out of the vectors |
---|
38 | mat3 rotation = mat3(vec3(tangent[0], binormal[0], normal[0]), |
---|
39 | vec3(tangent[1], binormal[1], normal[1]), |
---|
40 | vec3(tangent[2], binormal[2], normal[2])); |
---|
41 | |
---|
42 | // Transform the light vector according to this matrix |
---|
43 | oTSLightDir = rotation * lightDir; |
---|
44 | |
---|
45 | // Calculate half-angle in tangent space |
---|
46 | vec3 eyeDir = normalize(eyePosition - vertex.xyz); |
---|
47 | vec3 halfAngle = normalize(eyeDir + lightDir); |
---|
48 | oTSHalfAngle = rotation * halfAngle; |
---|
49 | } |
---|