qvolray

changeset 25:63bc059778d0

fixed ray marching
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Apr 2012 01:13:43 +0300
parents c27ce79632db
children f4cc61b5a3eb
files sdr/volray.p.glsl
diffstat 1 files changed, 60 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/sdr/volray.p.glsl	Sat Apr 14 00:54:11 2012 +0300
     1.2 +++ b/sdr/volray.p.glsl	Sat Apr 14 01:13:43 2012 +0300
     1.3 @@ -24,22 +24,40 @@
     1.4  vec3 shade(Ray ray, vec3 pos, vec3 norm);
     1.5  Ray get_primary_ray();
     1.6  ISect intersect_aabb(Ray ray, AABBox aabb);
     1.7 +ISect intersect_sphere(Ray ray, float rad);
     1.8 +
     1.9 +//#define USE_AABB
    1.10  
    1.11  void main()
    1.12  {
    1.13 -	const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0));
    1.14  	Ray ray = get_primary_ray();
    1.15  
    1.16  	vec3 color = vec3(0.0, 0.0, 0.0);
    1.17  
    1.18 +#ifdef USE_AABB
    1.19 +	const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0));
    1.20  	ISect res = intersect_aabb(ray, aabb);
    1.21  	if(res.hit) {
    1.22 -		color = ray_march(ray, res.t0, res.t1);
    1.23 +		color += ray_march(ray, res.t0, res.t1);
    1.24  	}
    1.25 +#else
    1.26 +	ISect res = intersect_sphere(ray, 1.5);
    1.27 +	if(res.hit) {
    1.28 +		color += ray_march(ray, res.t0, res.t1);
    1.29 +	}
    1.30 +#endif
    1.31  
    1.32  	gl_FragColor = vec4(color, 1.0);
    1.33  }
    1.34  
    1.35 +vec3 sky(Ray ray)
    1.36 +{
    1.37 +	vec3 c0 = vec3(0.2, 0.5, 0.8);
    1.38 +	vec3 c1 = vec3(0.8, 0.5, 0.2);
    1.39 +	float t = smoothstep(-0.2, 0.2, ray.dir.z);
    1.40 +	return mix(c0, c1, t);
    1.41 +}
    1.42 +
    1.43  float eval(vec3 pos, out vec3 grad)
    1.44  {
    1.45  	vec3 tc = pos * 0.5 + 0.5;
    1.46 @@ -55,7 +73,6 @@
    1.47  	return texture1D(xfer_tex, texel.a).x;
    1.48  }
    1.49  
    1.50 -#define OFFS	0.01
    1.51  vec3 ray_march(Ray ray, float t0, float t1)
    1.52  {
    1.53  	float energy = 1.0;
    1.54 @@ -70,12 +87,15 @@
    1.55  		vec3 norm;
    1.56  		float val = eval(pos, norm);
    1.57  
    1.58 -		col += shade(ray, pos, normalize(norm)) * val * energy;
    1.59 -		energy -= val;
    1.60 +		float energy_drop = exp(val * -ray_step);	// * scatter_coeff ?
    1.61 +		energy *= energy_drop;
    1.62 +
    1.63 +		vec3 irrad = shade(ray, pos, normalize(norm));
    1.64 +
    1.65 +		col += (1.0 - energy_drop) * energy * irrad;
    1.66  		if(energy < 0.001) {
    1.67  			break;
    1.68  		}
    1.69 -		pos += ray.dir * ray_step;
    1.70  	}
    1.71  
    1.72  	return col;
    1.73 @@ -83,17 +103,17 @@
    1.74  
    1.75  vec3 shade(Ray ray, vec3 pos, vec3 norm)
    1.76  {
    1.77 -	vec3 ldir = -pos;//normalize(vec3(10.0, 10.0, -10.0) - pos);
    1.78 -	vec3 vdir = -ray.dir;
    1.79 -	vec3 hdir = normalize(ldir + vdir);
    1.80 +	vec3 ldir = -normalize(ray.dir);
    1.81 +	vec3 vdir = ldir;
    1.82 +	vec3 hdir = ldir;//normalize(ldir + vdir);
    1.83  
    1.84 -	float ndotl = dot(ldir, norm);
    1.85 -	float ndoth = dot(hdir, norm);
    1.86 +	float ndotl = abs(dot(ldir, norm));
    1.87 +	float ndoth = abs(dot(hdir, norm));
    1.88  
    1.89  	vec3 dcol = vec3(0.9, 0.9, 0.9) * max(ndotl, 0.0);
    1.90  	vec3 scol = vec3(0.5, 0.5, 0.5) * pow(max(ndoth, 0.0), 50.0);
    1.91  
    1.92 -	return vec3(0.01, 0.01, 0.01) + dcol + scol;
    1.93 +	return dcol + scol;
    1.94  }
    1.95  
    1.96  Ray get_primary_ray()
    1.97 @@ -153,3 +173,31 @@
    1.98  	return res;
    1.99  }
   1.100  
   1.101 +
   1.102 +ISect intersect_sphere(Ray ray, float rad)
   1.103 +{
   1.104 +	ISect res;
   1.105 +	res.hit = false;
   1.106 +	res.t0 = res.t1 = 0.0;
   1.107 +
   1.108 +	float a = dot(ray.dir, ray.dir);
   1.109 +	float b = 2.0 * dot(ray.dir, ray.origin);
   1.110 +	float c = dot(ray.origin, ray.origin) - rad * rad;
   1.111 +	float d = b * b - 4.0 * a * c;
   1.112 +
   1.113 +	if(d < 0.0) {
   1.114 +		return res;
   1.115 +	}
   1.116 +
   1.117 +	float sqrt_d = sqrt(d);
   1.118 +	float t0 = (-b + sqrt_d) / (2.0 * a);
   1.119 +	float t1 = (-b - sqrt_d) / (2.0 * a);
   1.120 +
   1.121 +	res.t0 = min(t0, t1);
   1.122 +	res.t1 = max(t0, t1);
   1.123 +	res.hit = true;
   1.124 +
   1.125 +	/*res.pos = ray.origin + ray.dir * res.t0;
   1.126 +	res.normal = normalize(res.pos);*/
   1.127 +	return res;
   1.128 +}