whitted: refraction/fresnel fixed
[demo_prior] / sdr / whitted.p.glsl
index 055a818..80b6bc9 100644 (file)
@@ -30,12 +30,17 @@ bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit);
 bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit);
 bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit);
 
+/* schlick's approximation: [5.0, 1.0, R(0deg)] */
+float hack_fresnel(vec3 i, vec3 n, vec3 fresnel_val);
+
 vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos);
 
-const Material mtl_sph = Material(vec3(1.0, 1.0, 1.0) * 0.1, vec3(1.0, 1.0, 1.0), 80.0, 0.8, 0.0, 1.0);
-const Material mtl_glass = Material(vec3(1.0, 1.0, 1.0) * 0.01, vec3(1.0, 1.0, 1.0), 80.0, 0.0, 0.99, 1.52);
-const Material mtl_air = Material(vec3(1.0, 1.0, 1.0) * 0.01, vec3(1.0, 1.0, 1.0), 80.0, 0.0, 0.99, 1.0);
-const Material mtl_floor = Material(vec3(0.5, 0.5, 0.5), vec3(0.0, 0.0, 0.0), 1.0, 0.0, 0.0, 1.0);
+#define GREY(x)        vec3(x, x, x)
+
+const Material mtl_sph = Material(GREY(0.05), GREY(0.8), 80.0, 0.8, 0.0, 0.0);
+const Material mtl_glass = Material(GREY(0.0), GREY(0.8), 80.0, 0.99, 0.99, 1.52);
+const Material mtl_air = Material(GREY(0.0), GREY(0.8), 80.0, 0.99, 0.99, 1.0);
+const Material mtl_floor = Material(GREY(0.5), GREY(0.0), 1.0, 0.0, 0.0, 0.0);
 
 const vec3 light_pos = vec3(-10, 50, 30);
 
@@ -45,7 +50,7 @@ bool cast_ray(in Ray ray, inout vec3 color, out HitPoint hit)
                color += shade(ray.org, ray.dir, hit) * ray.energy;
                return true;
        }
-       color += backdrop(ray.dir);
+       color += backdrop(ray.dir) * ray.energy;
        return false;
 }
 
@@ -93,10 +98,14 @@ void main()
                float entering = step(0.0, dot(-stack[top].ray.dir, hit.norm));
                vec3 norm = faceforward(hit.norm, stack[top].ray.dir, hit.norm);
 
+               float fr = hack_fresnel(-stack[top].ray.dir, norm, vec3(5.0, 2.0, 0.01));
+               // fr = 1 everywhere when ior < 0.5 to avoid affecting the metal sphere
+               fr = min(1.0, (1.0 - step(0.5, hit.mtl.ior)) + fr);
+
                int op = stack[top].op++;
                if(op == 0) {
                        // reflection
-                       float energy = stack[top].ray.energy * hit.mtl.refl;
+                       float energy = stack[top].ray.energy * hit.mtl.refl * fr;
                        if(energy > 1e-4) {
                                int next = top + 1;
                                stack[next].op = 0;
@@ -107,7 +116,7 @@ void main()
                        }
                } else if(op == 1) {
                        // refraction
-                       float energy = stack[top].ray.energy * hit.mtl.refr;
+                       float energy = stack[top].ray.energy * hit.mtl.refr * (1.0 - fr);
                        if(energy > 1e-4) {
                                float next_ior = mix(stack[top - 1].ray.ior, hit.mtl.ior, entering);
                                float ior = stack[top].ray.ior / next_ior;
@@ -200,7 +209,7 @@ bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
                nearest.mtl.diffuse = tex_chess(vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 0.0), hit.surfpos);
        }
 
-       if(nearest.dist >= 10000.0) {
+       if(nearest.dist >= 9999.0) {
                hit_res.shadow = 1.0;
                return false;
        }
@@ -278,6 +287,12 @@ bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint h
        return true;
 }
 
+/* schlick's approximation: [5.0, 1.0, R(0deg)] */
+float hack_fresnel(vec3 i, vec3 n, vec3 fresnel_val)
+{
+       return fresnel_val.z + pow(1.0 - dot(i, n), fresnel_val.x) * fresnel_val.y;
+}
+
 vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos)
 {
        float foo = step(0.5, mod(spos.x * 8.0, 1.0)) * 2.0 - 1.0;