# HG changeset patch # User John Tsiombikas # Date 1334355223 -10800 # Node ID 63bc059778d0c80adeee8b61e3536ef6ce3dc2af # Parent c27ce79632dbbdd8ede55c13b8f9549ed2f54797 fixed ray marching diff -r c27ce79632db -r 63bc059778d0 sdr/volray.p.glsl --- a/sdr/volray.p.glsl Sat Apr 14 00:54:11 2012 +0300 +++ b/sdr/volray.p.glsl Sat Apr 14 01:13:43 2012 +0300 @@ -24,22 +24,40 @@ vec3 shade(Ray ray, vec3 pos, vec3 norm); Ray get_primary_ray(); ISect intersect_aabb(Ray ray, AABBox aabb); +ISect intersect_sphere(Ray ray, float rad); + +//#define USE_AABB void main() { - const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0)); Ray ray = get_primary_ray(); vec3 color = vec3(0.0, 0.0, 0.0); +#ifdef USE_AABB + const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0)); ISect res = intersect_aabb(ray, aabb); if(res.hit) { - color = ray_march(ray, res.t0, res.t1); + color += ray_march(ray, res.t0, res.t1); } +#else + ISect res = intersect_sphere(ray, 1.5); + if(res.hit) { + color += ray_march(ray, res.t0, res.t1); + } +#endif gl_FragColor = vec4(color, 1.0); } +vec3 sky(Ray ray) +{ + vec3 c0 = vec3(0.2, 0.5, 0.8); + vec3 c1 = vec3(0.8, 0.5, 0.2); + float t = smoothstep(-0.2, 0.2, ray.dir.z); + return mix(c0, c1, t); +} + float eval(vec3 pos, out vec3 grad) { vec3 tc = pos * 0.5 + 0.5; @@ -55,7 +73,6 @@ return texture1D(xfer_tex, texel.a).x; } -#define OFFS 0.01 vec3 ray_march(Ray ray, float t0, float t1) { float energy = 1.0; @@ -70,12 +87,15 @@ vec3 norm; float val = eval(pos, norm); - col += shade(ray, pos, normalize(norm)) * val * energy; - energy -= val; + float energy_drop = exp(val * -ray_step); // * scatter_coeff ? + energy *= energy_drop; + + vec3 irrad = shade(ray, pos, normalize(norm)); + + col += (1.0 - energy_drop) * energy * irrad; if(energy < 0.001) { break; } - pos += ray.dir * ray_step; } return col; @@ -83,17 +103,17 @@ vec3 shade(Ray ray, vec3 pos, vec3 norm) { - vec3 ldir = -pos;//normalize(vec3(10.0, 10.0, -10.0) - pos); - vec3 vdir = -ray.dir; - vec3 hdir = normalize(ldir + vdir); + vec3 ldir = -normalize(ray.dir); + vec3 vdir = ldir; + vec3 hdir = ldir;//normalize(ldir + vdir); - float ndotl = dot(ldir, norm); - float ndoth = dot(hdir, norm); + float ndotl = abs(dot(ldir, norm)); + float ndoth = abs(dot(hdir, norm)); vec3 dcol = vec3(0.9, 0.9, 0.9) * max(ndotl, 0.0); vec3 scol = vec3(0.5, 0.5, 0.5) * pow(max(ndoth, 0.0), 50.0); - return vec3(0.01, 0.01, 0.01) + dcol + scol; + return dcol + scol; } Ray get_primary_ray() @@ -153,3 +173,31 @@ return res; } + +ISect intersect_sphere(Ray ray, float rad) +{ + ISect res; + res.hit = false; + res.t0 = res.t1 = 0.0; + + float a = dot(ray.dir, ray.dir); + float b = 2.0 * dot(ray.dir, ray.origin); + float c = dot(ray.origin, ray.origin) - rad * rad; + float d = b * b - 4.0 * a * c; + + if(d < 0.0) { + return res; + } + + float sqrt_d = sqrt(d); + float t0 = (-b + sqrt_d) / (2.0 * a); + float t1 = (-b - sqrt_d) / (2.0 * a); + + res.t0 = min(t0, t1); + res.t1 = max(t0, t1); + res.hit = true; + + /*res.pos = ray.origin + ray.dir * res.t0; + res.normal = normalize(res.pos);*/ + return res; +}