volray

annotate 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
rev   line source
nuclear@0 1 uniform sampler3D volume;
nuclear@0 2 uniform sampler2D ray_tex;
nuclear@4 3 uniform sampler1D xfer_tex;
nuclear@0 4
nuclear@0 5 struct Ray {
nuclear@0 6 vec3 origin, dir;
nuclear@0 7 };
nuclear@0 8
nuclear@4 9 struct AABBox {
nuclear@4 10 vec3 min, max;
nuclear@4 11 };
nuclear@4 12
nuclear@0 13 struct ISect {
nuclear@0 14 bool hit;
nuclear@0 15 float t;
nuclear@0 16 vec3 pos;
nuclear@0 17 vec3 normal;
nuclear@0 18 };
nuclear@0 19
nuclear@1 20 vec3 sky(Ray ray);
nuclear@0 21 vec3 ray_march(Ray ray);
nuclear@0 22 vec3 shade(Ray ray, ISect isect);
nuclear@0 23 Ray get_primary_ray();
nuclear@4 24 bool intersect_aabb(Ray ray, AABBox aabb, out float t);
nuclear@0 25
nuclear@0 26 void main()
nuclear@0 27 {
nuclear@4 28 const AABBox aabb = AABBox(vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0));
nuclear@0 29 Ray ray = get_primary_ray();
nuclear@0 30
nuclear@4 31 vec3 color = vec3(0.0, 0.0, 0.0);
nuclear@4 32
nuclear@4 33 float start_t;
nuclear@4 34 if(intersect_aabb(ray, aabb, start_t)) {
nuclear@4 35 ray.origin += ray.dir * start_t;
nuclear@4 36 color = vec3(1.0, 0.0, 0.0);//ray_march(ray);
nuclear@4 37 }
nuclear@4 38
nuclear@4 39 gl_FragColor = vec4(color, 1.0);
nuclear@0 40 }
nuclear@0 41
nuclear@1 42 vec3 sky(Ray ray)
nuclear@0 43 {
nuclear@0 44 vec3 col1 = vec3(0.75, 0.78, 0.8);
nuclear@0 45 vec3 col2 = vec3(0.56, 0.7, 1.0);
nuclear@0 46
nuclear@0 47 float t = max(ray.dir.y, -0.5);
nuclear@0 48 return mix(col1, col2, t);
nuclear@1 49 }
nuclear@0 50
nuclear@4 51 float eval(vec3 pos)
nuclear@4 52 {
nuclear@4 53 return texture1D(xfer_tex, texture3D(volume, pos).x).x;
nuclear@4 54 }
nuclear@4 55
nuclear@0 56 vec3 ray_march(Ray ray)
nuclear@0 57 {
nuclear@1 58 const float ray_step = 0.1;
nuclear@0 59 float energy = 1.0;
nuclear@0 60 vec3 pos = ray.origin;
nuclear@1 61 float col = 0.0;
nuclear@0 62
nuclear@1 63 for(int i=0; i<40; i++) {
nuclear@4 64 float val = eval(pos) * energy;
nuclear@1 65 col += val;
nuclear@1 66 energy -= val;
nuclear@1 67 if(energy < 0.001) {
nuclear@1 68 break;
nuclear@1 69 }
nuclear@0 70 pos += ray.dir * ray_step;
nuclear@0 71 }
nuclear@0 72
nuclear@1 73 return vec3(col, col, col);
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 vec3 shade(Ray ray, ISect isect)
nuclear@0 77 {
nuclear@0 78 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
nuclear@0 79 vec3 vdir = -ray.dir;
nuclear@0 80 vec3 hdir = normalize(ldir + vdir);
nuclear@0 81
nuclear@0 82 float ndotl = dot(ldir, isect.normal);
nuclear@0 83 float ndoth = dot(hdir, isect.normal);
nuclear@0 84
nuclear@0 85 vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
nuclear@0 86 vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
nuclear@0 87
nuclear@0 88 return vec3(0.01, 0.01, 0.01) + dcol + scol;
nuclear@0 89 }
nuclear@0 90
nuclear@0 91 Ray get_primary_ray()
nuclear@0 92 {
nuclear@0 93 Ray ray;
nuclear@0 94 vec2 tc = gl_TexCoord[0].xy;
nuclear@0 95 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
nuclear@0 96 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
nuclear@0 97 return ray;
nuclear@0 98 }
nuclear@4 99
nuclear@4 100 bool intersect_aabb(Ray ray, AABBox aabb, out float t)
nuclear@4 101 {
nuclear@4 102 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
nuclear@4 103 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
nuclear@4 104 return true;
nuclear@4 105 }
nuclear@4 106
nuclear@4 107 vec4 bbox[2];
nuclear@4 108 bbox[0] = vec4(aabb.min.x, aabb.min.y, aabb.min.z, 0);
nuclear@4 109 bbox[1] = vec4(aabb.max.x, aabb.max.y, aabb.max.z, 0);
nuclear@4 110
nuclear@4 111 int xsign = int(ray.dir.x < 0.0);
nuclear@4 112 float invdirx = 1.0 / ray.dir.x;
nuclear@4 113 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
nuclear@4 114 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
nuclear@4 115
nuclear@4 116 int ysign = int(ray.dir.y < 0.0);
nuclear@4 117 float invdiry = 1.0 / ray.dir.y;
nuclear@4 118 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
nuclear@4 119 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
nuclear@4 120
nuclear@4 121 if(tmin > tymax || tymin > tmax) {
nuclear@4 122 return false;
nuclear@4 123 }
nuclear@4 124
nuclear@4 125 if(tymin > tmin) tmin = tymin;
nuclear@4 126 if(tymax < tmax) tmax = tymax;
nuclear@4 127
nuclear@4 128 int zsign = int(ray.dir.z < 0.0);
nuclear@4 129 float invdirz = 1.0 / ray.dir.z;
nuclear@4 130 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
nuclear@4 131 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
nuclear@4 132
nuclear@4 133 if(tmin > tzmax || tzmin > tmax) {
nuclear@4 134 return false;
nuclear@4 135 }
nuclear@4 136
nuclear@4 137 t = tmin;
nuclear@4 138 return tmin < 1.0 && tmax > 0.0;
nuclear@4 139 }
nuclear@4 140