7 vec3 diffuse, specular;
18 float shadow; /* opacity along the line of sight up to dist=1 */
21 varying vec3 v_rorg, v_rdir;
24 vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit);
25 vec3 backdrop(in vec3 dir);
27 bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit);
29 bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit);
30 bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit);
31 bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit);
33 /* schlick's approximation: [5.0, 1.0, R(0deg)] */
34 float hack_fresnel(vec3 i, vec3 n, vec3 fresnel_val);
36 vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos);
38 #define GREY(x) vec3(x, x, x)
40 const Material mtl_sph = Material(GREY(0.1), GREY(1.0), 80.0, 0.8, 0.0, 0.0);
41 const Material mtl_glass = Material(GREY(0.0), GREY(0.99), 80.0, 0.99, 0.99, 1.52);
42 const Material mtl_air = Material(GREY(0.0), GREY(0.99), 80.0, 0.99, 0.99, 1.0);
43 const Material mtl_floor = Material(GREY(0.5), GREY(0.0), 1.0, 0.0, 0.0, 0.0);
45 const vec3 light_pos = vec3(-10, 50, 30);
47 bool cast_ray(in Ray ray, inout vec3 color, out HitPoint hit)
49 if(isect_scene(ray.org, ray.dir, hit)) {
50 color += shade(ray.org, ray.dir, hit) * ray.energy;
53 color += backdrop(ray.dir);
67 StackFrame stack[MAX_LEVEL];
69 vec3 color = vec3(0.0, 0.0, 0.0);
72 stack[0].ray.org = v_rorg;
73 stack[0].ray.dir = normalize(v_rdir);
74 stack[0].ray.energy = stack[0].ray.ior = 1.0;
78 if(top >= MAX_LEVEL - 1) {
79 color += backdrop(stack[top].ray.dir) * stack[top].ray.energy;
83 if(stack[top].ray.energy < 1e-3) {
88 if(!cast_ray(stack[top].ray, color, stack[top].hit)) {
94 /* found a hit, recurse for reflection/refraction */
95 HitPoint hit = stack[top].hit;
97 // 1.0 when entering, 0.0 when leaving
98 float entering = step(0.0, dot(-stack[top].ray.dir, hit.norm));
99 vec3 norm = faceforward(hit.norm, stack[top].ray.dir, hit.norm);
101 float fr = hack_fresnel(-stack[top].ray.dir, norm, vec3(5.0, 4.0, 0.01));
102 // fr = 1 everywhere when ior < 0.5 to avoid affecting the metal sphere
103 fr = min(1.0, (1.0 - step(0.5, hit.mtl.ior)) + fr);
105 int op = stack[top].op++;
108 float energy = stack[top].ray.energy * hit.mtl.refl * fr;
109 if(hit.mtl.refr > 1e-4) {
115 stack[next].ray.org = hit.pos + norm * 1e-5;
116 stack[next].ray.dir = reflect(stack[top].ray.dir, norm);
117 stack[next].ray.energy = energy;
122 float energy = stack[top].ray.energy * hit.mtl.refr * (1.0 - fr);
124 float next_ior = mix(stack[top - 1].ray.ior, hit.mtl.ior, entering);
125 float ior = stack[top].ray.ior / next_ior;
128 stack[next].ray.org = hit.pos - norm * 1e-5;
129 stack[next].ray.dir = refract(stack[top].ray.dir, norm, ior);
130 stack[next].ray.energy = energy;
131 stack[next].ray.ior = next_ior;
140 gl_FragColor = vec4(color, 1.0);
143 vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit)
146 vec3 norm = faceforward(hit.norm, rd, hit.norm);
147 vec3 ldir = light_pos - hit.pos;
149 vec3 amb = hit.mtl.diffuse * 0.02;
151 isect_scene(hit.pos + norm * 0.01, ldir, shadow_hit);
153 vec3 l = normalize(ldir);
154 vec3 v = normalize(-rd);
155 vec3 h = normalize(v + l);
156 float ndotl = max(dot(norm, l), 0.0);
157 float ndoth = max(dot(norm, h), 0.0);
159 vec3 lit = hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin);
161 return amb + lit * shadow_hit.shadow;
164 #define M_PI 3.1415926
165 #define M_2PI (M_PI * 2.0)
167 vec3 backdrop(in vec3 dir)
169 return vec3(0.1, 0.15, 1.0);
172 #define FLOOR_OFFS vec3(3.0, 0.0, 0.0)
173 #define FLOOR_SIZE vec2(5.5, 15.0)
175 #define GLASS_POS vec3(0.0, 0.2, 1.2)
177 bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
180 HitPoint hit, nearest;
182 nearest.dist = 10000.0;
184 if(isect_sphere(ro, rd, vec3(1.5, -0.5, 0.0), 0.85, hit)) {
186 nearest.mtl = mtl_sph;
188 opacity *= mtl_sph.refr;
192 if(isect_sphere(ro, rd, GLASS_POS, 0.9, hit)) {
193 if(hit.dist < nearest.dist) {
195 nearest.mtl = mtl_glass;
198 opacity *= mtl_glass.refr;
202 if(isect_sphere(ro, rd, GLASS_POS, 0.86, hit)) {
203 if(hit.dist < nearest.dist) {
205 nearest.mtl = mtl_air;
209 if(isect_floor(ro, rd, FLOOR_SIZE, hit) && hit.dist < nearest.dist) {
211 nearest.mtl = mtl_floor;
212 nearest.mtl.diffuse = tex_chess(vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 0.0), hit.surfpos);
215 if(nearest.dist >= 10000.0) {
216 hit_res.shadow = 1.0;
221 hit_res.shadow = opacity;
225 bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit)
227 if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.8), hit)) {
231 if(abs(hit.pos.x) >= sz.x || abs(hit.pos.z) >= sz.y) {
235 hit.pos += FLOOR_OFFS;
236 hit.surfpos /= sz * 2.0;
240 bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit)
242 float ndotrd = dot(rd, plane.xyz);
244 if(abs(ndotrd) < 1e-6) {
248 vec3 pp = plane.xyz * plane.w;
250 float t = dot(pdir, plane.xyz) / ndotrd;
257 hit.pos = ro + rd * t;
258 hit.norm = plane.xyz;
259 hit.surfpos = hit.pos.xz; /* XXX */
263 bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit)
265 float a = dot(rd, rd);
266 float b = dot(rd * 2.0, (ro - pos));
267 float c = dot(ro, ro) + dot(pos, pos) - 2.0 * dot(ro, pos) - rad * rad;
269 float d = b * b - 4.0 * a * c;
274 float t0 = (-b + sqrt(d)) / (2.0 * a);
275 float t1 = (-b - sqrt(d)) / (2.0 * a);
277 if(t0 < 0.0) t0 = t1;
278 if(t1 < 0.0) t1 = t0;
279 float t = min(t0, t1);
286 hit.pos = ro + rd * t;
287 hit.norm = normalize(hit.pos - pos);
288 hit.surfpos.x = atan(hit.norm.z, hit.norm.x);
289 hit.surfpos.y = acos(hit.norm.y);
293 /* schlick's approximation: [5.0, 1.0, R(0deg)] */
294 float hack_fresnel(vec3 i, vec3 n, vec3 fresnel_val)
296 return fresnel_val.z + pow(1.0 - dot(i, n), fresnel_val.x) * fresnel_val.y;
299 vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos)
301 float foo = step(0.5, mod(spos.x * 8.0, 1.0)) * 2.0 - 1.0;
302 float bar = step(0.5, mod(spos.y * 24.0, 1.0)) * 2.0 - 1.0;
304 float xor = (foo * bar) * 0.5 + 0.5;
306 return mix(col1, col2, xor);