whitted: fresnel, needs work
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 8 Dec 2020 13:22:29 +0000 (15:22 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 8 Dec 2020 13:22:29 +0000 (15:22 +0200)
sdr/whitted.p.glsl

index 055a818..72a7c75 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.1), GREY(1.0), 80.0, 0.8, 0.0, 0.0);
+const Material mtl_glass = Material(GREY(0.0), GREY(0.99), 80.0, 0.99, 0.99, 1.52);
+const Material mtl_air = Material(GREY(0.0), GREY(0.99), 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);
 
@@ -93,10 +98,17 @@ 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, 4.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(hit.mtl.refr > 1e-4) {
+                               energy += fr * 0.01;
+                       }
                        if(energy > 1e-4) {
                                int next = top + 1;
                                stack[next].op = 0;
@@ -107,7 +119,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;
@@ -278,6 +290,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;