diff options
author | Kenneth Graunke <kenneth@whitecape.org> | 2010-11-10 21:51:42 -0800 |
---|---|---|
committer | Kenneth Graunke <kenneth@whitecape.org> | 2010-11-17 15:09:43 -0800 |
commit | 4da64513401741de42d065e64ff5ce31243dd7a8 (patch) | |
tree | a3fed135ea1e3c253553b20a7ca3b703a8b66379 | |
parent | a3881fc5d64cd8e3a5454c622b36bb4e194657d6 (diff) |
Combine the environment mapping and bump mapping via a Fresnel factor.
-rwxr-xr-x | src/phong.frag | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/phong.frag b/src/phong.frag index 9bd054d..d2b74a3 100755 --- a/src/phong.frag +++ b/src/phong.frag @@ -14,6 +14,21 @@ const vec3 base_color = vec3(1.0, 0.0, 0.0); */ uniform float s; +float schlick(vec3 n, vec3 l) +{ + // Material constants... 1.0 = air, 3.5 = sphere (arbitrary) + const float ratio = 1.0 / 3.5; + + // Value of the Fresnel reflection when N = L. + const float r0 = ((1 - ratio) * (1 - ratio)) / ((1 + ratio) * (1 + ratio)); + + /* According idr's slides on Fresnel reflection, page 45 and 47, + * the input "theta" to the Frensel function is the angle between the + * light and the normal vectors...so cos(theta) = dot(n, l). + */ + return r0 + (1 - r0) * pow(1.0 - dot(n, l), 5.0); +} + /* Adapted from the Orange Book, 3rd edition, chapter 11. */ vec3 perturbed_normal() { @@ -43,10 +58,15 @@ void main(void) vec3 h = normalize(l + v); vec3 n = perturbed_normal(); vec3 i = reflect(eye_ss, n); - //vec3 diff = vec3(textureCube(sampler, i)); - vec3 diff = base_color * dot(n, l); + //vec3 diff = base_color * dot(n, l); float spec = pow(dot(n, h), s); - gl_FragColor = step(0.0, dot(n, l)) * vec4(diff + vec3(spec), 1.0); - //gl_FragColor = textureCube(sampler, reflect(eye_direction_ws, normal_ws)); + float F = schlick(n, l); + + vec4 envir = textureCube(sampler, reflect(eye_direction_ws, normal_ws)); + gl_FragColor = (1 - F) * envir + F * vec4(spec); } + + //gl_FragColor = vec4((1 - F) * diff + F * vec3(spec), 1.0); + //gl_FragColor = step(0.0, dot(n, l)) * vec4(diff + vec3(spec), 1.0); + //gl_FragColor = textureCube(sampler, reflect(eye_direction_ws, normal_ws)); |