volray

annotate 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
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@0 15 vec3 ray_march(Ray ray);
nuclear@0 16 vec3 shade(Ray ray, ISect isect);
nuclear@0 17 Ray get_primary_ray();
nuclear@0 18
nuclear@0 19 void main()
nuclear@0 20 {
nuclear@0 21 Ray ray = get_primary_ray();
nuclear@0 22
nuclear@0 23 gl_FragColor = vec4(ray_march(ray), 1.0);
nuclear@0 24 }
nuclear@0 25
nuclear@0 26 /*vec3 sky(Ray ray)
nuclear@0 27 {
nuclear@0 28 vec3 col1 = vec3(0.75, 0.78, 0.8);
nuclear@0 29 vec3 col2 = vec3(0.56, 0.7, 1.0);
nuclear@0 30
nuclear@0 31 float t = max(ray.dir.y, -0.5);
nuclear@0 32 return mix(col1, col2, t);
nuclear@0 33 }*/
nuclear@0 34
nuclear@0 35 vec3 ray_march(Ray ray)
nuclear@0 36 {
nuclear@0 37 const float ray_step = 0.05;
nuclear@0 38 float energy = 1.0;
nuclear@0 39 vec3 pos = ray.origin;
nuclear@0 40
nuclear@0 41 // assuming view space
nuclear@0 42 while(pos.z < 1.0) {
nuclear@0 43 energy -= texture3D(volume, pos).x;
nuclear@0 44 pos += ray.dir * ray_step;
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 return vec3(energy, energy, energy);
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 vec3 shade(Ray ray, ISect isect)
nuclear@0 51 {
nuclear@0 52 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
nuclear@0 53 vec3 vdir = -ray.dir;
nuclear@0 54 vec3 hdir = normalize(ldir + vdir);
nuclear@0 55
nuclear@0 56 float ndotl = dot(ldir, isect.normal);
nuclear@0 57 float ndoth = dot(hdir, isect.normal);
nuclear@0 58
nuclear@0 59 vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
nuclear@0 60 vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
nuclear@0 61
nuclear@0 62 return vec3(0.01, 0.01, 0.01) + dcol + scol;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 Ray get_primary_ray()
nuclear@0 66 {
nuclear@0 67 Ray ray;
nuclear@0 68 vec2 tc = gl_TexCoord[0].xy;
nuclear@0 69 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
nuclear@0 70 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
nuclear@0 71 return ray;
nuclear@0 72 }