X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=sdr%2Fwhitted.p.glsl;h=1c40d1da28a07c074f990c28c979807773fe8afe;hb=7f33f4df3bdd41375f215548adc8016a55f03472;hp=368903d5a16748ac06e2cbd19a80bf9b51f10ed4;hpb=44a7a61d2bec54ed741930572e63e5015326daca;p=demo_prior diff --git a/sdr/whitted.p.glsl b/sdr/whitted.p.glsl index 368903d..1c40d1d 100644 --- a/sdr/whitted.p.glsl +++ b/sdr/whitted.p.glsl @@ -1,6 +1,13 @@ +struct Ray { + vec3 org, dir; + float energy, ior; +}; + struct Material { vec3 diffuse, specular; float shin; + float refl, refr; + float ior; }; struct HitPoint { @@ -8,6 +15,7 @@ struct HitPoint { vec3 pos, norm; vec2 surfpos; Material mtl; + float shadow; /* opacity along the line of sight up to dist=1 */ }; varying vec3 v_rorg, v_rdir; @@ -18,48 +26,136 @@ vec3 backdrop(in vec3 dir); bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit); -bool isect_floor(in vec3 ro, in vec3 rd, float rad, out HitPoint hit); +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); -const Material mtl_sph = Material(vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0), 80.0); -const Material mtl_floor = Material(vec3(0.5, 0.5, 0.5), vec3(0.0, 0.0, 0.0), 1.0); +vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos); -const vec3 light_pos = vec3(-20, 20, 30); +#define GREY(x) vec3(x, x, x) -void main() +const Material mtl_sph = Material(vec3(0.05, 0.15, 0.08), GREY(0.8), 80.0, 0.8, 0.0, 0.0); +const Material mtl_glass = Material(GREY(0.02), 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); + +bool cast_ray(in Ray ray, inout vec3 color, out HitPoint hit) { - vec3 rdir = normalize(v_rdir); - gl_FragColor.rgb = backdrop(rdir); + if(isect_scene(ray.org, ray.dir, hit)) { + color += shade(ray.org, ray.dir, hit) * ray.energy; + return true; + } + color += backdrop(ray.dir) * ray.energy; + return false; +} + +#define MAX_LEVEL 8 +struct StackFrame { + int op; + Ray ray; HitPoint hit; - if(isect_scene(v_rorg, rdir, hit)) { - gl_FragColor.rgb = shade(v_rorg, rdir, hit); - } else { - gl_FragColor.rgb = backdrop(rdir); +}; + +void main() +{ + StackFrame stack[MAX_LEVEL]; + + vec3 color = vec3(0.0, 0.0, 0.0); + + stack[0].op = 0; + stack[0].ray.org = v_rorg; + stack[0].ray.dir = normalize(v_rdir); + stack[0].ray.energy = stack[0].ray.ior = 1.0; + + int top = 0; + while(top >= 0) { + if(top >= MAX_LEVEL - 1) { + color += backdrop(stack[top].ray.dir) * stack[top].ray.energy; + top--; + continue; + } + if(stack[top].ray.energy < 1e-3) { + top--; + continue; + } + + if(!cast_ray(stack[top].ray, color, stack[top].hit)) { + /* no hit, return */ + top--; + continue; + } + + /* found a hit, recurse for reflection/refraction */ + HitPoint hit = stack[top].hit; + + // 1.0 when entering, 0.0 when leaving + 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 * fr; + if(energy > 1e-4) { + int next = top + 1; + stack[next].op = 0; + stack[next].ray.org = hit.pos + norm * 1e-5; + stack[next].ray.dir = reflect(stack[top].ray.dir, norm); + stack[next].ray.energy = energy; + top = next; + } + } else if(op == 1) { + // refraction + 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; + int next = top + 1; + stack[next].op = 0; + stack[next].ray.org = hit.pos - norm * 1e-5; + stack[next].ray.dir = refract(stack[top].ray.dir, norm, ior); + stack[next].ray.energy = energy; + stack[next].ray.ior = next_ior; + top = next; + } + } else if(op == 2) { + // return + top--; + } } - gl_FragColor.a = 1.0; + + gl_FragColor = vec4(color, 1.0); } vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit) { HitPoint shadow_hit; + vec3 norm = faceforward(hit.norm, rd, hit.norm); vec3 ldir = light_pos - hit.pos; - vec3 col = vec3(0.03, 0.03, 0.03); // ambient + vec3 amb = hit.mtl.diffuse * 0.01; - if(!isect_scene(hit.pos + hit.norm * 0.01, ldir, shadow_hit) || shadow_hit.dist > 1.0) { - vec3 l = normalize(ldir); - vec3 v = normalize(-rd); - vec3 h = normalize(v + l); - float ndotl = max(dot(hit.norm, l), 0.0); - float ndoth = max(dot(hit.norm, h), 0.0); + isect_scene(hit.pos + norm * 0.01, ldir, shadow_hit); - col += hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin); - } + vec3 l = normalize(ldir); + vec3 v = normalize(-rd); + vec3 h = normalize(v + l); + float ndotl = max(dot(norm, l), 0.0); + float ndoth = max(dot(norm, h), 0.0); - return col; + vec3 lit = hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin); + + return amb + lit * shadow_hit.shadow; } #define M_PI 3.1415926 @@ -67,45 +163,75 @@ vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit) vec3 backdrop(in vec3 dir) { - return mix(vec3(0.8, 0.3, 0.2), vec3(0.2, 0.3, 0.8), smoothstep(-0.1, 0.1, dir.y)); + return vec3(0.02, 0.38, 0.6) * 0.9; } +#define FLOOR_OFFS vec3(3.0, 0.0, -9.0) +#define FLOOR_SIZE vec2(5.5, 12.0) + +#define REFL_POS vec3(1.36, -0.5, 0.0) +#define GLASS_POS vec3(0.0, 0.2, 1.2) + bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res) { + float opacity = 1.0; HitPoint hit, nearest; nearest.dist = 10000.0; - if(isect_sphere(ro, rd, vec3(0.0, 0.0, 0.0), 1.0, hit)) { + if(isect_sphere(ro, rd, REFL_POS, 0.85, hit)) { nearest = hit; nearest.mtl = mtl_sph; + if(hit.dist < 1.0) { + opacity *= mtl_sph.refr; + } + } + + if(isect_sphere(ro, rd, GLASS_POS, 0.9, hit)) { + if(hit.dist < nearest.dist) { + nearest = hit; + nearest.mtl = mtl_glass; + } + if(hit.dist < 1.0) { + opacity *= mtl_glass.refr * 0.25; // XXX hack quarter the refr, fresnel would reduce it drastically + } + } + + if(isect_sphere(ro, rd, GLASS_POS, 0.86, hit)) { + if(hit.dist < nearest.dist) { + nearest = hit; + nearest.mtl = mtl_air; + } } - if(isect_floor(ro, rd, 5.0, hit) && hit.dist < nearest.dist) { + if(isect_floor(ro, rd, FLOOR_SIZE, hit) && hit.dist < nearest.dist) { nearest = hit; nearest.mtl = mtl_floor; + nearest.mtl.diffuse = tex_chess(vec3(0.25, 0.01, 0.01), vec3(0.3, 0.3, 0.1), hit.surfpos); } - if(nearest.dist >= 10000.0) { + if(nearest.dist >= 9999.0) { + hit_res.shadow = 1.0; return false; } hit_res = nearest; + hit_res.shadow = opacity; return true; } -#define FLOOR_OFFS vec3(2.0, 0.0, 0.0) - -bool isect_floor(in vec3 ro, in vec3 rd, float rad, out HitPoint hit) +bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit) { - if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.0), hit)) { + if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.8), hit)) { return false; } - float d = max(abs(hit.pos.x), abs(hit.pos.z)); - if(d >= rad) return false; + if(abs(hit.pos.x) >= sz.x || abs(hit.pos.z) >= sz.y) { + return false; + } hit.pos += FLOOR_OFFS; + hit.surfpos /= sz * 2.0; return true; } @@ -161,3 +287,19 @@ bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint h hit.surfpos.y = acos(hit.norm.y); 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; + float bar = step(0.5, mod(spos.y * 12.0, 1.0)) * 2.0 - 1.0; + + float xor = (foo * bar) * 0.5 + 0.5; + + return mix(col1, col2, xor); +}