qvolray

view sdr/volray.p.glsl @ 35:6ca076bf5084

sucks ass
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Apr 2012 02:22:13 +0300
parents 437e1ba9cf39
children
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 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 #ifdef USE_AABB
129 ISect intersect_aabb(Ray ray, AABBox aabb)
130 {
131 ISect res;
132 res.hit = false;
133 res.t0 = res.t1 = 0.0;
135 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
136 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
137 res.hit = true;
138 return res;
139 }
141 vec4 bbox[2];
142 bbox[0] = vec4(aabb.min.x, aabb.min.y, aabb.min.z, 0);
143 bbox[1] = vec4(aabb.max.x, aabb.max.y, aabb.max.z, 0);
145 int xsign = int(ray.dir.x < 0.0);
146 float invdirx = 1.0 / ray.dir.x;
147 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
148 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
150 int ysign = int(ray.dir.y < 0.0);
151 float invdiry = 1.0 / ray.dir.y;
152 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
153 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
155 if(tmin > tymax || tymin > tmax) {
156 return res;
157 }
159 if(tymin > tmin) tmin = tymin;
160 if(tymax < tmax) tmax = tymax;
162 int zsign = int(ray.dir.z < 0.0);
163 float invdirz = 1.0 / ray.dir.z;
164 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
165 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
167 if(tmin > tzmax || tzmin > tmax) {
168 return res;
169 }
171 res.t0 = tmin;
172 res.t1 = tmax;
173 res.hit = true;
174 return res;
175 }
176 #else
178 ISect intersect_sphere(Ray ray, float rad)
179 {
180 ISect res;
181 res.hit = false;
182 res.t0 = res.t1 = 0.0;
184 float a = dot(ray.dir, ray.dir);
185 float b = 2.0 * dot(ray.dir, ray.origin);
186 float c = dot(ray.origin, ray.origin) - rad * rad;
187 float d = b * b - 4.0 * a * c;
189 if(d < 0.0) {
190 return res;
191 }
193 float sqrt_d = sqrt(d);
194 float t0 = (-b + sqrt_d) / (2.0 * a);
195 float t1 = (-b - sqrt_d) / (2.0 * a);
197 res.t0 = max(min(t0, t1), 0.0);
198 res.t1 = max(max(t0, t1), 0.0);
199 res.hit = true;
201 /*res.pos = ray.origin + ray.dir * res.t0;
202 res.normal = normalize(res.pos);*/
203 return res;
204 }
205 #endif