qvolray

diff volray.p.glsl @ 7:f40e4edfee7e

shading
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 05 Apr 2012 00:52:11 +0300
parents 0c3874aa717a
children ae10631bb11b
line diff
     1.1 --- a/volray.p.glsl	Wed Apr 04 01:37:58 2012 +0300
     1.2 +++ b/volray.p.glsl	Thu Apr 05 00:52:11 2012 +0300
     1.3 @@ -1,6 +1,8 @@
     1.4  uniform sampler3D volume;
     1.5  uniform sampler2D ray_tex;
     1.6  uniform sampler1D xfer_tex;
     1.7 +uniform float ray_step;
     1.8 +uniform float zclip;
     1.9  
    1.10  struct Ray {
    1.11  	vec3 origin, dir;
    1.12 @@ -12,14 +14,14 @@
    1.13  
    1.14  struct ISect {
    1.15  	bool hit;
    1.16 -	float t;
    1.17 +	float t0, t1;
    1.18  	vec3 pos;
    1.19  	vec3 normal;
    1.20  };
    1.21  
    1.22  vec3 sky(Ray ray);
    1.23 -vec3 ray_march(Ray ray);
    1.24 -vec3 shade(Ray ray, ISect isect);
    1.25 +vec3 ray_march(Ray ray, float t0, float t1);
    1.26 +vec3 shade(Ray ray, vec3 pos, vec3 norm);
    1.27  Ray get_primary_ray();
    1.28  ISect intersect_aabb(Ray ray, AABBox aabb);
    1.29  
    1.30 @@ -32,38 +34,44 @@
    1.31  
    1.32  	ISect res = intersect_aabb(ray, aabb);
    1.33  	if(res.hit) {
    1.34 -		ray.origin += ray.dir * res.t;
    1.35 -		color = ray_march(ray);
    1.36 +		color = ray_march(ray, res.t0, res.t1);
    1.37  	}
    1.38  
    1.39  	gl_FragColor = vec4(color, 1.0);
    1.40  }
    1.41  
    1.42 -vec3 sky(Ray ray)
    1.43 -{
    1.44 -	vec3 col1 = vec3(0.75, 0.78, 0.8);
    1.45 -	vec3 col2 = vec3(0.56, 0.7, 1.0);
    1.46 -
    1.47 -	float t = max(ray.dir.y, -0.5);
    1.48 -	return mix(col1, col2, t);
    1.49 -}
    1.50 -
    1.51  float eval(vec3 pos)
    1.52  {
    1.53  	vec3 tc = pos * 0.5 + 0.5;
    1.54 +
    1.55 +	if(tc.x < 0.0 || tc.y < 0.0 || tc.z < zclip || tc.x > 1.0 || tc.y > 1.0 || tc.z > 1.0) {
    1.56 +		return 0.0;
    1.57 +	}
    1.58 +
    1.59  	return texture1D(xfer_tex, texture3D(volume, tc).x).x;
    1.60  }
    1.61  
    1.62 -vec3 ray_march(Ray ray)
    1.63 +#define OFFS	0.01
    1.64 +vec3 ray_march(Ray ray, float t0, float t1)
    1.65  {
    1.66 -	const float ray_step = 0.1;
    1.67  	float energy = 1.0;
    1.68 -	vec3 pos = ray.origin;
    1.69 -	float col = 0.0;
    1.70 +	float t = t0;
    1.71 +	vec3 col = vec3(0.0, 0.0, 0.0);
    1.72  
    1.73 -	for(int i=0; i<40; i++) {
    1.74 +
    1.75 +	while(t < t1) {
    1.76 +		vec3 pos = ray.origin + ray.dir * t;
    1.77 +		t += ray_step;
    1.78 +
    1.79  		float val = eval(pos) * energy;
    1.80 -		col += val;
    1.81 +		vec3 norm;
    1.82 +
    1.83 +		norm.x = eval(pos + vec3(OFFS, 0.0, 0.0)) - val;
    1.84 +		norm.y = eval(pos + vec3(0.0, OFFS, 0.0)) - val;
    1.85 +		norm.z = eval(pos + vec3(0.0, 0.0, OFFS)) - val;
    1.86 +		norm = normalize(norm);
    1.87 +
    1.88 +		col += shade(ray, pos, norm) * val;
    1.89  		energy -= val;
    1.90  		if(energy < 0.001) {
    1.91  			break;
    1.92 @@ -71,17 +79,17 @@
    1.93  		pos += ray.dir * ray_step;
    1.94  	}
    1.95  
    1.96 -	return vec3(col, col, col);
    1.97 +	return col;
    1.98  }
    1.99  
   1.100 -vec3 shade(Ray ray, ISect isect)
   1.101 +vec3 shade(Ray ray, vec3 pos, vec3 norm)
   1.102  {
   1.103 -	vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
   1.104 +	vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - pos);
   1.105  	vec3 vdir = -ray.dir;
   1.106  	vec3 hdir = normalize(ldir + vdir);
   1.107  
   1.108 -	float ndotl = dot(ldir, isect.normal);
   1.109 -	float ndoth = dot(hdir, isect.normal);
   1.110 +	float ndotl = dot(ldir, norm);
   1.111 +	float ndoth = dot(hdir, norm);
   1.112  
   1.113  	vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
   1.114  	vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
   1.115 @@ -102,10 +110,10 @@
   1.116  {
   1.117  	ISect res;
   1.118  	res.hit = false;
   1.119 +	res.t0 = res.t1 = 0.0;
   1.120  
   1.121  	if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
   1.122  			ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
   1.123 -		res.t = 0.0;
   1.124  		res.hit = true;
   1.125  		return res;
   1.126  	}
   1.127 @@ -140,28 +148,9 @@
   1.128  		return res;
   1.129  	}
   1.130  
   1.131 -	res.t = tmin;
   1.132 +	res.t0 = tmin;
   1.133 +	res.t1 = tmax;
   1.134  	res.hit = true;
   1.135 -	res.pos = ray.origin + ray.dir * res.t;
   1.136 -
   1.137 -	/*if(res.pos.x > res.pos.y && res.pos.x > res.pos.z) {
   1.138 -		res.normal = vec3(1.0, 0.0, 0.0);
   1.139 -	} else if(res.pos.x < res.pos.y && res.pos.x < res.pos.z) {
   1.140 -		res.normal = vec3(-1.0, 0.0, 0.0);
   1.141 -	} else if(res.pos.y > res.pos.x && res.pos.y > res.pos.z) {
   1.142 -		res.normal = vec3(0.0, 1.0, 0.0);
   1.143 -	} else if(res.pos.y < res.pos.x && res.pos.y < res.pos.z) {
   1.144 -		res.normal = vec3(0.0, -1.0, 0.0);
   1.145 -	} else if(res.pos.z > res.pos.x && res.pos.z > res.pos.y) {
   1.146 -		res.normal = vec3(0.0, 0.0, 1.0);
   1.147 -	} else {
   1.148 -		res.normal = vec3(0.0, 0.9, -1.0);
   1.149 -	}
   1.150 -	if(res.pos.x < 0.0) {
   1.151 -		res.normal = vec3(1.0, 0.0, 0.0);
   1.152 -	} else {
   1.153 -		res.normal = vec3(0.0, 0.0, 1.0);
   1.154 -	}*/
   1.155  	return res;
   1.156  }
   1.157