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(vec3(0.05, 0.15, 0.08), GREY(0.8), 80.0, 0.8, 0.0, 0.0);
41 const Material mtl_glass = Material(GREY(0.02), GREY(0.8), 80.0, 0.99, 0.99, 1.52);
42 const Material mtl_air = Material(GREY(0.0), GREY(0.8), 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) * ray.energy;
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, 2.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;
112 stack[next].ray.org = hit.pos + norm * 1e-5;
113 stack[next].ray.dir = reflect(stack[top].ray.dir, norm);
114 stack[next].ray.energy = energy;
119 float energy = stack[top].ray.energy * hit.mtl.refr * (1.0 - fr);
121 float next_ior = mix(stack[top - 1].ray.ior, hit.mtl.ior, entering);
122 float ior = stack[top].ray.ior / next_ior;
125 stack[next].ray.org = hit.pos - norm * 1e-5;
126 stack[next].ray.dir = refract(stack[top].ray.dir, norm, ior);
127 stack[next].ray.energy = energy;
128 stack[next].ray.ior = next_ior;
137 gl_FragColor = vec4(color, 1.0);
140 vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit)
143 vec3 norm = faceforward(hit.norm, rd, hit.norm);
144 vec3 ldir = light_pos - hit.pos;
146 vec3 amb = hit.mtl.diffuse * 0.01;
148 isect_scene(hit.pos + norm * 0.01, ldir, shadow_hit);
150 vec3 l = normalize(ldir);
151 vec3 v = normalize(-rd);
152 vec3 h = normalize(v + l);
153 float ndotl = max(dot(norm, l), 0.0);
154 float ndoth = max(dot(norm, h), 0.0);
156 vec3 lit = hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin);
158 return amb + lit * shadow_hit.shadow;
161 #define M_PI 3.1415926
162 #define M_2PI (M_PI * 2.0)
164 vec3 backdrop(in vec3 dir)
166 return vec3(0.02, 0.38, 0.6) * 0.9;
169 #define FLOOR_OFFS vec3(3.0, 0.0, -9.0)
170 #define FLOOR_SIZE vec2(5.5, 12.0)
172 #define REFL_POS vec3(1.36, -0.5, 0.0)
173 #define GLASS_POS vec3(0.0, 0.2, 1.2)
175 bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
178 HitPoint hit, nearest;
180 nearest.dist = 10000.0;
182 if(isect_sphere(ro, rd, REFL_POS, 0.85, hit)) {
184 nearest.mtl = mtl_sph;
186 opacity *= mtl_sph.refr;
190 if(isect_sphere(ro, rd, GLASS_POS, 0.9, hit)) {
191 if(hit.dist < nearest.dist) {
193 nearest.mtl = mtl_glass;
196 opacity *= mtl_glass.refr * 0.25; // XXX hack quarter the refr, fresnel would reduce it drastically
200 if(isect_sphere(ro, rd, GLASS_POS, 0.86, hit)) {
201 if(hit.dist < nearest.dist) {
203 nearest.mtl = mtl_air;
207 if(isect_floor(ro, rd, FLOOR_SIZE, hit) && hit.dist < nearest.dist) {
209 nearest.mtl = mtl_floor;
210 nearest.mtl.diffuse = tex_chess(vec3(0.25, 0.01, 0.01), vec3(0.3, 0.3, 0.1), hit.surfpos);
213 if(nearest.dist >= 9999.0) {
214 hit_res.shadow = 1.0;
219 hit_res.shadow = opacity;
223 bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit)
225 if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.8), hit)) {
229 if(abs(hit.pos.x) >= sz.x || abs(hit.pos.z) >= sz.y) {
233 hit.pos += FLOOR_OFFS;
234 hit.surfpos /= sz * 2.0;
238 bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit)
240 float ndotrd = dot(rd, plane.xyz);
242 if(abs(ndotrd) < 1e-6) {
246 vec3 pp = plane.xyz * plane.w;
248 float t = dot(pdir, plane.xyz) / ndotrd;
255 hit.pos = ro + rd * t;
256 hit.norm = plane.xyz;
257 hit.surfpos = hit.pos.xz; /* XXX */
261 bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit)
263 float a = dot(rd, rd);
264 float b = dot(rd * 2.0, (ro - pos));
265 float c = dot(ro, ro) + dot(pos, pos) - 2.0 * dot(ro, pos) - rad * rad;
267 float d = b * b - 4.0 * a * c;
272 float t0 = (-b + sqrt(d)) / (2.0 * a);
273 float t1 = (-b - sqrt(d)) / (2.0 * a);
275 if(t0 < 0.0) t0 = t1;
276 if(t1 < 0.0) t1 = t0;
277 float t = min(t0, t1);
284 hit.pos = ro + rd * t;
285 hit.norm = normalize(hit.pos - pos);
286 hit.surfpos.x = atan(hit.norm.z, hit.norm.x);
287 hit.surfpos.y = acos(hit.norm.y);
291 /* schlick's approximation: [5.0, 1.0, R(0deg)] */
292 float hack_fresnel(vec3 i, vec3 n, vec3 fresnel_val)
294 return fresnel_val.z + pow(1.0 - dot(i, n), fresnel_val.x) * fresnel_val.y;
297 vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos)
299 float foo = step(0.5, mod(spos.x * 8.0, 1.0)) * 2.0 - 1.0;
300 float bar = step(0.5, mod(spos.y * 12.0, 1.0)) * 2.0 - 1.0;
302 float xor = (foo * bar) * 0.5 + 0.5;
304 return mix(col1, col2, xor);