blob: d1f139758b8b87f614ddfb2c110bbb4da26b7ec2 [file] [log] [blame]
Jesse Barkerb00c4a22012-12-07 11:57:44 -08001uniform sampler2D DistanceMap;
Jesse Barkerd4748442012-12-10 15:17:15 -08002uniform sampler2D NormalMap;
3uniform sampler2D ImageMap;
Jesse Barkerb00c4a22012-12-07 11:57:44 -08004
5varying vec3 vertex_normal;
6varying vec4 vertex_position;
Jesse Barkerd4748442012-12-10 15:17:15 -08007varying vec4 MapCoord;
Jesse Barkerb00c4a22012-12-07 11:57:44 -08008
9void main()
10{
Jesse Barkerb00c4a22012-12-07 11:57:44 -080011 const vec4 lightSpecular = vec4(0.8, 0.8, 0.8, 1.0);
Jesse Barkerb00c4a22012-12-07 11:57:44 -080012 const vec4 matSpecular = vec4(1.0, 1.0, 1.0, 1.0);
13 const float matShininess = 100.0;
Jesse Barkerd4748442012-12-10 15:17:15 -080014 const vec2 point_five = vec2(0.5);
Jesse Barkere111ecb2012-12-11 10:08:49 -080015 // Need the normalized eye direction and surface normal vectors to
Jesse Barker90c50c72012-12-13 14:21:57 -080016 // compute the transmitted vector through the "front" surface of the object.
Jesse Barkere111ecb2012-12-11 10:08:49 -080017 vec3 eye_direction = normalize(-vertex_position.xyz);
Jesse Barkerb00c4a22012-12-07 11:57:44 -080018 vec3 normalized_normal = normalize(vertex_normal);
Jesse Barker6f808b72012-12-13 16:06:17 -080019 vec3 front_refraction = refract(eye_direction, normalized_normal, RefractiveIndex);
Jesse Barker90c50c72012-12-13 14:21:57 -080020 // 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 Barkerb44efa62012-12-12 16:00:35 -080031 vec3 back_normal = texture2D(NormalMap, normcoord).xyz;
Jesse Barker90c50c72012-12-13 14:21:57 -080032 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 Barkerd4748442012-12-10 15:17:15 -080037 vec4 texel = texture2D(ImageMap, imagecoord);
Jesse Barker90c50c72012-12-13 14:21:57 -080038 // Add in specular reflection, and we have our fragment value.
Jesse Barkere111ecb2012-12-11 10:08:49 -080039 vec3 light_direction = normalize(vertex_position.xyz/vertex_position.w -
40 LightSourcePosition.xyz/LightSourcePosition.w);
Jesse Barkerd4748442012-12-10 15:17:15 -080041 vec3 reflection = reflect(light_direction, normalized_normal);
Jesse Barkerb00c4a22012-12-07 11:57:44 -080042 float specularTerm = pow(max(0.0, dot(reflection, eye_direction)), matShininess);
Jesse Barkerb00c4a22012-12-07 11:57:44 -080043 vec4 specular = (lightSpecular * matSpecular);
Jesse Barkerd4748442012-12-10 15:17:15 -080044 gl_FragColor = (specular * specularTerm) + texel;
Jesse Barkerb00c4a22012-12-07 11:57:44 -080045}