Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 1 | uniform sampler2D DistanceMap; |
Jesse Barker | d474844 | 2012-12-10 15:17:15 -0800 | [diff] [blame] | 2 | uniform sampler2D NormalMap; |
| 3 | uniform sampler2D ImageMap; |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 4 | |
| 5 | varying vec3 vertex_normal; |
| 6 | varying vec4 vertex_position; |
Jesse Barker | d474844 | 2012-12-10 15:17:15 -0800 | [diff] [blame] | 7 | varying vec4 MapCoord; |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 8 | |
| 9 | void main() |
| 10 | { |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 11 | const vec4 lightSpecular = vec4(0.8, 0.8, 0.8, 1.0); |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 12 | const vec4 matSpecular = vec4(1.0, 1.0, 1.0, 1.0); |
| 13 | const float matShininess = 100.0; |
Jesse Barker | d474844 | 2012-12-10 15:17:15 -0800 | [diff] [blame] | 14 | const vec2 point_five = vec2(0.5); |
Jesse Barker | e111ecb | 2012-12-11 10:08:49 -0800 | [diff] [blame] | 15 | // Need the normalized eye direction and surface normal vectors to |
Jesse Barker | 90c50c7 | 2012-12-13 14:21:57 -0800 | [diff] [blame] | 16 | // compute the transmitted vector through the "front" surface of the object. |
Jesse Barker | e111ecb | 2012-12-11 10:08:49 -0800 | [diff] [blame] | 17 | vec3 eye_direction = normalize(-vertex_position.xyz); |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 18 | vec3 normalized_normal = normalize(vertex_normal); |
Jesse Barker | 6f808b7 | 2012-12-13 16:06:17 -0800 | [diff] [blame] | 19 | vec3 front_refraction = refract(eye_direction, normalized_normal, RefractiveIndex); |
Jesse Barker | 90c50c7 | 2012-12-13 14:21:57 -0800 | [diff] [blame] | 20 | // Find our best distance approximation through the object so we can |
| 21 | // project the transmitted vector to the back of the object to find |
| 22 | // the exit point. |
| 23 | vec3 mc_perspective = (MapCoord.xyz / MapCoord.w) + front_refraction; |
| 24 | vec2 dcoord = mc_perspective.st * point_five + point_five; |
| 25 | vec4 distance_value = texture2D(DistanceMap, dcoord); |
| 26 | vec3 back_position = vertex_position.xyz + front_refraction * distance_value.z; |
| 27 | // Use the exit point to index the map of back-side normals, and use the |
| 28 | // back-side position and normal to find the transmitted vector out of the |
| 29 | // object. |
| 30 | vec2 normcoord = back_position.st * point_five + point_five; |
Jesse Barker | b44efa6 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 31 | vec3 back_normal = texture2D(NormalMap, normcoord).xyz; |
Jesse Barker | 90c50c7 | 2012-12-13 14:21:57 -0800 | [diff] [blame] | 32 | vec3 back_refraction = refract(back_position, back_normal, 1.0); |
| 33 | // Use the transmitted vector from the exit point to determine where |
| 34 | // the vector would intersect the environment (in this case a background |
| 35 | // image. |
| 36 | vec2 imagecoord = back_refraction.st * point_five + point_five; |
Jesse Barker | d474844 | 2012-12-10 15:17:15 -0800 | [diff] [blame] | 37 | vec4 texel = texture2D(ImageMap, imagecoord); |
Jesse Barker | 90c50c7 | 2012-12-13 14:21:57 -0800 | [diff] [blame] | 38 | // Add in specular reflection, and we have our fragment value. |
Jesse Barker | e111ecb | 2012-12-11 10:08:49 -0800 | [diff] [blame] | 39 | vec3 light_direction = normalize(vertex_position.xyz/vertex_position.w - |
| 40 | LightSourcePosition.xyz/LightSourcePosition.w); |
Jesse Barker | d474844 | 2012-12-10 15:17:15 -0800 | [diff] [blame] | 41 | vec3 reflection = reflect(light_direction, normalized_normal); |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 42 | float specularTerm = pow(max(0.0, dot(reflection, eye_direction)), matShininess); |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 43 | vec4 specular = (lightSpecular * matSpecular); |
Jesse Barker | d474844 | 2012-12-10 15:17:15 -0800 | [diff] [blame] | 44 | gl_FragColor = (specular * specularTerm) + texel; |
Jesse Barker | b00c4a2 | 2012-12-07 11:57:44 -0800 | [diff] [blame] | 45 | } |