qvolray

annotate volray.p.glsl @ 3:6f275934717b

foon
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 02 Apr 2012 14:42:03 +0300
parents b050ce167ff1
children 3e53a16d4667
rev   line source
nuclear@0 1 uniform sampler3D volume;
nuclear@0 2 uniform sampler2D ray_tex;
nuclear@0 3
nuclear@0 4 struct Ray {
nuclear@0 5 vec3 origin, dir;
nuclear@0 6 };
nuclear@0 7
nuclear@0 8 struct ISect {
nuclear@0 9 bool hit;
nuclear@0 10 float t;
nuclear@0 11 vec3 pos;
nuclear@0 12 vec3 normal;
nuclear@0 13 };
nuclear@0 14
nuclear@1 15 vec3 sky(Ray ray);
nuclear@0 16 vec3 ray_march(Ray ray);
nuclear@0 17 vec3 shade(Ray ray, ISect isect);
nuclear@0 18 Ray get_primary_ray();
nuclear@0 19
nuclear@0 20 void main()
nuclear@0 21 {
nuclear@0 22 Ray ray = get_primary_ray();
nuclear@0 23
nuclear@0 24 gl_FragColor = vec4(ray_march(ray), 1.0);
nuclear@1 25 //gl_FragColor = vec4(sky(ray), 1.0);
nuclear@0 26 }
nuclear@0 27
nuclear@1 28 vec3 sky(Ray ray)
nuclear@0 29 {
nuclear@0 30 vec3 col1 = vec3(0.75, 0.78, 0.8);
nuclear@0 31 vec3 col2 = vec3(0.56, 0.7, 1.0);
nuclear@0 32
nuclear@0 33 float t = max(ray.dir.y, -0.5);
nuclear@0 34 return mix(col1, col2, t);
nuclear@1 35 }
nuclear@0 36
nuclear@0 37 vec3 ray_march(Ray ray)
nuclear@0 38 {
nuclear@1 39 const float ray_step = 0.1;
nuclear@0 40 float energy = 1.0;
nuclear@0 41 vec3 pos = ray.origin;
nuclear@1 42 float col = 0.0;
nuclear@0 43
nuclear@1 44 for(int i=0; i<40; i++) {
nuclear@1 45 float val = texture3D(volume, pos).x;
nuclear@1 46 val *= energy;
nuclear@1 47 col += val;
nuclear@1 48 energy -= val;
nuclear@1 49 if(energy < 0.001) {
nuclear@1 50 break;
nuclear@1 51 }
nuclear@0 52 pos += ray.dir * ray_step;
nuclear@0 53 }
nuclear@0 54
nuclear@1 55 return vec3(col, col, col);
nuclear@0 56 }
nuclear@0 57
nuclear@0 58 vec3 shade(Ray ray, ISect isect)
nuclear@0 59 {
nuclear@0 60 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
nuclear@0 61 vec3 vdir = -ray.dir;
nuclear@0 62 vec3 hdir = normalize(ldir + vdir);
nuclear@0 63
nuclear@0 64 float ndotl = dot(ldir, isect.normal);
nuclear@0 65 float ndoth = dot(hdir, isect.normal);
nuclear@0 66
nuclear@0 67 vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
nuclear@0 68 vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
nuclear@0 69
nuclear@0 70 return vec3(0.01, 0.01, 0.01) + dcol + scol;
nuclear@0 71 }
nuclear@0 72
nuclear@0 73 Ray get_primary_ray()
nuclear@0 74 {
nuclear@0 75 Ray ray;
nuclear@0 76 vec2 tc = gl_TexCoord[0].xy;
nuclear@0 77 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
nuclear@0 78 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
nuclear@0 79 return ray;
nuclear@0 80 }