qvolray

diff volray.p.glsl @ 5:0c3874aa717a

slice
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 04 Apr 2012 01:37:33 +0300
parents 3e53a16d4667
children f40e4edfee7e
line diff
     1.1 --- a/volray.p.glsl	Mon Apr 02 17:59:46 2012 +0300
     1.2 +++ b/volray.p.glsl	Wed Apr 04 01:37:33 2012 +0300
     1.3 @@ -21,19 +21,19 @@
     1.4  vec3 ray_march(Ray ray);
     1.5  vec3 shade(Ray ray, ISect isect);
     1.6  Ray get_primary_ray();
     1.7 -bool intersect_aabb(Ray ray, AABBox aabb, out float t);
     1.8 +ISect intersect_aabb(Ray ray, AABBox aabb);
     1.9  
    1.10  void main()
    1.11  {
    1.12 -	const AABBox aabb = AABBox(vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0));
    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 -	float start_t;
    1.19 -	if(intersect_aabb(ray, aabb, start_t)) {
    1.20 -		ray.origin += ray.dir * start_t;
    1.21 -		color = vec3(1.0, 0.0, 0.0);//ray_march(ray);
    1.22 +	ISect res = intersect_aabb(ray, aabb);
    1.23 +	if(res.hit) {
    1.24 +		ray.origin += ray.dir * res.t;
    1.25 +		color = ray_march(ray);
    1.26  	}
    1.27  
    1.28  	gl_FragColor = vec4(color, 1.0);
    1.29 @@ -50,7 +50,8 @@
    1.30  
    1.31  float eval(vec3 pos)
    1.32  {
    1.33 -	return texture1D(xfer_tex, texture3D(volume, pos).x).x;
    1.34 +	vec3 tc = pos * 0.5 + 0.5;
    1.35 +	return texture1D(xfer_tex, texture3D(volume, tc).x).x;
    1.36  }
    1.37  
    1.38  vec3 ray_march(Ray ray)
    1.39 @@ -97,11 +98,16 @@
    1.40  	return ray;
    1.41  }
    1.42  
    1.43 -bool intersect_aabb(Ray ray, AABBox aabb, out float t)
    1.44 +ISect intersect_aabb(Ray ray, AABBox aabb)
    1.45  {
    1.46 +	ISect res;
    1.47 +	res.hit = false;
    1.48 +
    1.49  	if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
    1.50  			ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
    1.51 -		return true;
    1.52 +		res.t = 0.0;
    1.53 +		res.hit = true;
    1.54 +		return res;
    1.55  	}
    1.56  
    1.57  	vec4 bbox[2];
    1.58 @@ -119,7 +125,7 @@
    1.59  	float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
    1.60  
    1.61  	if(tmin > tymax || tymin > tmax) {
    1.62 -		return false;
    1.63 +		return res;
    1.64  	}
    1.65  
    1.66  	if(tymin > tmin) tmin = tymin;
    1.67 @@ -131,10 +137,31 @@
    1.68  	float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
    1.69  
    1.70  	if(tmin > tzmax || tzmin > tmax) {
    1.71 -		return false;
    1.72 +		return res;
    1.73  	}
    1.74  
    1.75 -	t = tmin;
    1.76 -	return tmin < 1.0 && tmax > 0.0;
    1.77 +	res.t = tmin;
    1.78 +	res.hit = true;
    1.79 +	res.pos = ray.origin + ray.dir * res.t;
    1.80 +
    1.81 +	/*if(res.pos.x > res.pos.y && res.pos.x > res.pos.z) {
    1.82 +		res.normal = vec3(1.0, 0.0, 0.0);
    1.83 +	} else if(res.pos.x < res.pos.y && res.pos.x < res.pos.z) {
    1.84 +		res.normal = vec3(-1.0, 0.0, 0.0);
    1.85 +	} else if(res.pos.y > res.pos.x && res.pos.y > res.pos.z) {
    1.86 +		res.normal = vec3(0.0, 1.0, 0.0);
    1.87 +	} else if(res.pos.y < res.pos.x && res.pos.y < res.pos.z) {
    1.88 +		res.normal = vec3(0.0, -1.0, 0.0);
    1.89 +	} else if(res.pos.z > res.pos.x && res.pos.z > res.pos.y) {
    1.90 +		res.normal = vec3(0.0, 0.0, 1.0);
    1.91 +	} else {
    1.92 +		res.normal = vec3(0.0, 0.9, -1.0);
    1.93 +	}
    1.94 +	if(res.pos.x < 0.0) {
    1.95 +		res.normal = vec3(1.0, 0.0, 0.0);
    1.96 +	} else {
    1.97 +		res.normal = vec3(0.0, 0.0, 1.0);
    1.98 +	}*/
    1.99 +	return res;
   1.100  }
   1.101