qvolray

view sdr/volray.p.glsl @ 26:f4cc61b5a3eb

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Apr 2012 04:59:14 +0300
parents 63bc059778d0
children 437e1ba9cf39
line source
1 uniform sampler3D volume;
2 uniform sampler2D ray_tex;
3 uniform sampler1D xfer_tex;
4 uniform float ray_step;
5 uniform float zclip;
7 struct Ray {
8 vec3 origin, dir;
9 };
11 struct AABBox {
12 vec3 min, max;
13 };
15 struct ISect {
16 bool hit;
17 float t0, t1;
18 vec3 pos;
19 vec3 normal;
20 };
22 vec3 sky(Ray ray);
23 vec3 ray_march(Ray ray, float t0, float t1);
24 vec3 shade(Ray ray, vec3 pos, vec3 norm, vec3 ldir);
25 Ray get_primary_ray();
26 ISect intersect_aabb(Ray ray, AABBox aabb);
27 ISect intersect_sphere(Ray ray, float rad);
29 //#define USE_AABB
31 void main()
32 {
33 Ray ray = get_primary_ray();
35 vec3 color = vec3(0.0, 0.0, 0.0);
37 #ifdef USE_AABB
38 const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0));
39 ISect res = intersect_aabb(ray, aabb);
40 if(res.hit) {
41 color += ray_march(ray, res.t0, res.t1);
42 }
43 #else
44 ISect res = intersect_sphere(ray, 1.5);
45 if(res.hit) {
46 color += ray_march(ray, res.t0, res.t1);
47 }
48 #endif
50 gl_FragColor = vec4(color, 1.0);
51 }
53 vec3 sky(Ray ray)
54 {
55 vec3 c0 = vec3(0.2, 0.5, 0.8);
56 vec3 c1 = vec3(0.8, 0.5, 0.2);
57 float t = smoothstep(-0.2, 0.2, ray.dir.z);
58 return mix(c0, c1, t);
59 }
61 float eval(vec3 pos, out vec3 grad)
62 {
63 vec3 tc = pos * 0.5 + 0.5;
65 if(tc.x < 0.0 || tc.y < 0.0 || tc.z < zclip || tc.x > 1.0 || tc.y > 1.0 || tc.z > 1.0) {
66 grad = vec3(0.0, 0.0, 0.0);
67 return 0.0;
68 }
70 vec4 texel = texture3D(volume, tc);
71 grad = texel.xyz;
73 return texture1D(xfer_tex, texel.a).x;
74 }
76 vec3 ray_march(Ray ray, float t0, float t1)
77 {
78 float energy = 1.0;
79 float t = t0;
80 vec3 col = vec3(0.0, 0.0, 0.0);
81 const vec3 ldir = normalize(vec3(-1, 1, -4));
84 while(t < t1) {
85 vec3 pos = ray.origin + ray.dir * t;
86 t += ray_step;
88 vec3 norm;
89 float val = eval(pos, norm);
91 float energy_drop = exp(val * -ray_step); // * scatter_coeff ?
92 energy *= energy_drop;
94 vec3 irrad = shade(ray, pos, normalize(norm), ldir);
96 col += (1.0 - energy_drop) * energy * irrad;
97 if(energy < 0.001) {
98 break;
99 }
100 }
102 return col;
103 }
105 vec3 shade(Ray ray, vec3 pos, vec3 norm, vec3 ldir)
106 {
107 vec3 vdir = -normalize(ray.dir);
108 vec3 hdir = normalize(ldir + vdir);
110 float ndotl = abs(dot(ldir, norm));
111 float ndoth = abs(dot(hdir, norm));
113 vec3 dcol = vec3(0.9, 0.9, 0.9) * max(ndotl, 0.0);
114 vec3 scol = vec3(0.5, 0.5, 0.5) * pow(max(ndoth, 0.0), 50.0);
116 return dcol + scol;
117 }
119 Ray get_primary_ray()
120 {
121 Ray ray;
122 vec2 tc = gl_TexCoord[0].xy;
123 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
124 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
125 return ray;
126 }
128 ISect intersect_aabb(Ray ray, AABBox aabb)
129 {
130 ISect res;
131 res.hit = false;
132 res.t0 = res.t1 = 0.0;
134 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
135 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
136 res.hit = true;
137 return res;
138 }
140 vec4 bbox[2];
141 bbox[0] = vec4(aabb.min.x, aabb.min.y, aabb.min.z, 0);
142 bbox[1] = vec4(aabb.max.x, aabb.max.y, aabb.max.z, 0);
144 int xsign = int(ray.dir.x < 0.0);
145 float invdirx = 1.0 / ray.dir.x;
146 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
147 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
149 int ysign = int(ray.dir.y < 0.0);
150 float invdiry = 1.0 / ray.dir.y;
151 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
152 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
154 if(tmin > tymax || tymin > tmax) {
155 return res;
156 }
158 if(tymin > tmin) tmin = tymin;
159 if(tymax < tmax) tmax = tymax;
161 int zsign = int(ray.dir.z < 0.0);
162 float invdirz = 1.0 / ray.dir.z;
163 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
164 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
166 if(tmin > tzmax || tzmin > tmax) {
167 return res;
168 }
170 res.t0 = tmin;
171 res.t1 = tmax;
172 res.hit = true;
173 return res;
174 }
177 ISect intersect_sphere(Ray ray, float rad)
178 {
179 ISect res;
180 res.hit = false;
181 res.t0 = res.t1 = 0.0;
183 float a = dot(ray.dir, ray.dir);
184 float b = 2.0 * dot(ray.dir, ray.origin);
185 float c = dot(ray.origin, ray.origin) - rad * rad;
186 float d = b * b - 4.0 * a * c;
188 if(d < 0.0) {
189 return res;
190 }
192 float sqrt_d = sqrt(d);
193 float t0 = (-b + sqrt_d) / (2.0 * a);
194 float t1 = (-b - sqrt_d) / (2.0 * a);
196 res.t0 = min(t0, t1);
197 res.t1 = max(t0, t1);
198 res.hit = true;
200 /*res.pos = ray.origin + ray.dir * res.t0;
201 res.normal = normalize(res.pos);*/
202 return res;
203 }