X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=sdr%2Fwhitted.p.glsl;h=0124f1bfd1e4f0effac1732bf67ebcdb025c89e6;hb=3a9f6854df479d81442273c9d0b133c49c5c8f66;hp=fc197f67e42b429e3985da2b608e33ada9e37205;hpb=ec776ad8bf37d25b9308e2c770d66247135b46ea;p=demo_prior diff --git a/sdr/whitted.p.glsl b/sdr/whitted.p.glsl index fc197f6..0124f1b 100644 --- a/sdr/whitted.p.glsl +++ b/sdr/whitted.p.glsl @@ -18,10 +18,16 @@ 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); +vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos); + +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); + +const vec3 light_pos = vec3(-10, 50, 30); void main() { @@ -39,7 +45,22 @@ void main() vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit) { - return (hit.norm * 0.5 + 0.5) * hit.mtl.diffuse; + HitPoint shadow_hit; + vec3 ldir = light_pos - hit.pos; + + vec3 col = vec3(0.03, 0.03, 0.03); // ambient + + 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); + + col += hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin); + } + + return col; } #define M_PI 3.1415926 @@ -50,8 +71,8 @@ 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)); } -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); +#define FLOOR_OFFS vec3(3.0, 0.0, 0.0) +#define FLOOR_SIZE vec2(5.5, 15.0) bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res) { @@ -59,14 +80,15 @@ bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res) nearest.dist = 10000.0; - if(isect_sphere(ro, rd, vec3(0.0, 0.0, 0.0), 1.0, hit)) { + if(isect_sphere(ro, rd, vec3(1.0, 0.0, 0.0), 1.0, hit)) { nearest = hit; nearest.mtl = mtl_sph; } - 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(1.0, 0.0, 0.0), vec3(1.0, 1.0, 0.0), hit.surfpos); } if(nearest.dist >= 10000.0) { @@ -77,15 +99,18 @@ bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res) return true; } -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, 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; } @@ -101,6 +126,10 @@ bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit) vec3 pdir = pp - ro; float t = dot(pdir, plane.xyz) / ndotrd; + if(t < 1e-6) { + return false; + } + hit.dist = t; hit.pos = ro + rd * t; hit.norm = plane.xyz; @@ -137,3 +166,13 @@ 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; } + +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 * 24.0, 1.0)) * 2.0 - 1.0; + + float xor = (foo * bar) * 0.5 + 0.5; + + return mix(col1, col2, xor); +}