qvolray

diff volray.p.glsl @ 0:b050ce167ff1

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 31 Mar 2012 02:08:42 +0300
parents
children 57072295eb83
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/volray.p.glsl	Sat Mar 31 02:08:42 2012 +0300
     1.3 @@ -0,0 +1,72 @@
     1.4 +uniform sampler3D volume;
     1.5 +uniform sampler2D ray_tex;
     1.6 +
     1.7 +struct Ray {
     1.8 +	vec3 origin, dir;
     1.9 +};
    1.10 +
    1.11 +struct ISect {
    1.12 +	bool hit;
    1.13 +	float t;
    1.14 +	vec3 pos;
    1.15 +	vec3 normal;
    1.16 +};
    1.17 +
    1.18 +vec3 ray_march(Ray ray);
    1.19 +vec3 shade(Ray ray, ISect isect);
    1.20 +Ray get_primary_ray();
    1.21 +
    1.22 +void main()
    1.23 +{
    1.24 +	Ray ray = get_primary_ray();
    1.25 +
    1.26 +	gl_FragColor = vec4(ray_march(ray), 1.0);
    1.27 +}
    1.28 +
    1.29 +/*vec3 sky(Ray ray)
    1.30 +{
    1.31 +	vec3 col1 = vec3(0.75, 0.78, 0.8);
    1.32 +	vec3 col2 = vec3(0.56, 0.7, 1.0);
    1.33 +
    1.34 +	float t = max(ray.dir.y, -0.5);
    1.35 +	return mix(col1, col2, t);
    1.36 +}*/
    1.37 +
    1.38 +vec3 ray_march(Ray ray)
    1.39 +{
    1.40 +	const float ray_step = 0.05;
    1.41 +	float energy = 1.0;
    1.42 +	vec3 pos = ray.origin;
    1.43 +
    1.44 +	// assuming view space
    1.45 +	while(pos.z < 1.0) {
    1.46 +		energy -= texture3D(volume, pos).x;
    1.47 +		pos += ray.dir * ray_step;
    1.48 +	}
    1.49 +
    1.50 +	return vec3(energy, energy, energy);
    1.51 +}
    1.52 +
    1.53 +vec3 shade(Ray ray, ISect isect)
    1.54 +{
    1.55 +	vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
    1.56 +	vec3 vdir = -ray.dir;
    1.57 +	vec3 hdir = normalize(ldir + vdir);
    1.58 +
    1.59 +	float ndotl = dot(ldir, isect.normal);
    1.60 +	float ndoth = dot(hdir, isect.normal);
    1.61 +
    1.62 +	vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
    1.63 +	vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
    1.64 +
    1.65 +	return vec3(0.01, 0.01, 0.01) + dcol + scol;
    1.66 +}
    1.67 +
    1.68 +Ray get_primary_ray()
    1.69 +{
    1.70 +	Ray ray;
    1.71 +	vec2 tc = gl_TexCoord[0].xy;
    1.72 +	ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
    1.73 +	ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
    1.74 +	return ray;
    1.75 +}