volray

diff volray.p.glsl @ 4:3e53a16d4667

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 02 Apr 2012 17:59:46 +0300
parents 57072295eb83
children 0c3874aa717a
line diff
     1.1 --- a/volray.p.glsl	Mon Apr 02 14:42:03 2012 +0300
     1.2 +++ b/volray.p.glsl	Mon Apr 02 17:59:46 2012 +0300
     1.3 @@ -1,10 +1,15 @@
     1.4  uniform sampler3D volume;
     1.5  uniform sampler2D ray_tex;
     1.6 +uniform sampler1D xfer_tex;
     1.7  
     1.8  struct Ray {
     1.9  	vec3 origin, dir;
    1.10  };
    1.11  
    1.12 +struct AABBox {
    1.13 +	vec3 min, max;
    1.14 +};
    1.15 +
    1.16  struct ISect {
    1.17  	bool hit;
    1.18  	float t;
    1.19 @@ -16,13 +21,22 @@
    1.20  vec3 ray_march(Ray ray);
    1.21  vec3 shade(Ray ray, ISect isect);
    1.22  Ray get_primary_ray();
    1.23 +bool intersect_aabb(Ray ray, AABBox aabb, out float t);
    1.24  
    1.25  void main()
    1.26  {
    1.27 +	const AABBox aabb = AABBox(vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0));
    1.28  	Ray ray = get_primary_ray();
    1.29  
    1.30 -	gl_FragColor = vec4(ray_march(ray), 1.0);
    1.31 -	//gl_FragColor = vec4(sky(ray), 1.0);
    1.32 +	vec3 color = vec3(0.0, 0.0, 0.0);
    1.33 +
    1.34 +	float start_t;
    1.35 +	if(intersect_aabb(ray, aabb, start_t)) {
    1.36 +		ray.origin += ray.dir * start_t;
    1.37 +		color = vec3(1.0, 0.0, 0.0);//ray_march(ray);
    1.38 +	}
    1.39 +
    1.40 +	gl_FragColor = vec4(color, 1.0);
    1.41  }
    1.42  
    1.43  vec3 sky(Ray ray)
    1.44 @@ -34,6 +48,11 @@
    1.45  	return mix(col1, col2, t);
    1.46  }
    1.47  
    1.48 +float eval(vec3 pos)
    1.49 +{
    1.50 +	return texture1D(xfer_tex, texture3D(volume, pos).x).x;
    1.51 +}
    1.52 +
    1.53  vec3 ray_march(Ray ray)
    1.54  {
    1.55  	const float ray_step = 0.1;
    1.56 @@ -42,8 +61,7 @@
    1.57  	float col = 0.0;
    1.58  
    1.59  	for(int i=0; i<40; i++) {
    1.60 -		float val = texture3D(volume, pos).x;
    1.61 -		val *= energy;
    1.62 +		float val = eval(pos) * energy;
    1.63  		col += val;
    1.64  		energy -= val;
    1.65  		if(energy < 0.001) {
    1.66 @@ -78,3 +96,45 @@
    1.67  	ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
    1.68  	return ray;
    1.69  }
    1.70 +
    1.71 +bool intersect_aabb(Ray ray, AABBox aabb, out float t)
    1.72 +{
    1.73 +	if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
    1.74 +			ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
    1.75 +		return true;
    1.76 +	}
    1.77 +
    1.78 +	vec4 bbox[2];
    1.79 +	bbox[0] = vec4(aabb.min.x, aabb.min.y, aabb.min.z, 0);
    1.80 +	bbox[1] = vec4(aabb.max.x, aabb.max.y, aabb.max.z, 0);
    1.81 +
    1.82 +	int xsign = int(ray.dir.x < 0.0);
    1.83 +	float invdirx = 1.0 / ray.dir.x;
    1.84 +	float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
    1.85 +	float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
    1.86 +
    1.87 +	int ysign = int(ray.dir.y < 0.0);
    1.88 +	float invdiry = 1.0 / ray.dir.y;
    1.89 +	float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
    1.90 +	float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
    1.91 +
    1.92 +	if(tmin > tymax || tymin > tmax) {
    1.93 +		return false;
    1.94 +	}
    1.95 +
    1.96 +	if(tymin > tmin) tmin = tymin;
    1.97 +	if(tymax < tmax) tmax = tymax;
    1.98 +
    1.99 +	int zsign = int(ray.dir.z < 0.0);
   1.100 +	float invdirz = 1.0 / ray.dir.z;
   1.101 +	float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
   1.102 +	float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
   1.103 +
   1.104 +	if(tmin > tzmax || tzmin > tmax) {
   1.105 +		return false;
   1.106 +	}
   1.107 +
   1.108 +	t = tmin;
   1.109 +	return tmin < 1.0 && tmax > 0.0;
   1.110 +}
   1.111 +