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 vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos);
35 const Material mtl_sph = Material(vec3(1.0, 1.0, 1.0) * 0.1, vec3(1.0, 1.0, 1.0), 80.0, 0.8, 0.0, 1.0);
36 const Material mtl_glass = Material(vec3(1.0, 1.0, 1.0) * 0.01, vec3(1.0, 1.0, 1.0), 80.0, 0.0, 0.99, 1.52);
37 const Material mtl_air = Material(vec3(1.0, 1.0, 1.0) * 0.01, vec3(1.0, 1.0, 1.0), 80.0, 0.0, 0.99, 1.0);
38 const Material mtl_floor = Material(vec3(0.5, 0.5, 0.5), vec3(0.0, 0.0, 0.0), 1.0, 0.0, 0.0, 1.0);
40 const vec3 light_pos = vec3(-10, 50, 30);
42 bool cast_ray(in Ray ray, inout vec3 color, out HitPoint hit)
44 if(isect_scene(ray.org, ray.dir, hit)) {
45 color += shade(ray.org, ray.dir, hit) * ray.energy;
48 color += backdrop(ray.dir);
62 StackFrame stack[MAX_LEVEL];
64 vec3 color = vec3(0.0, 0.0, 0.0);
67 stack[0].ray.org = v_rorg;
68 stack[0].ray.dir = normalize(v_rdir);
69 stack[0].ray.energy = stack[0].ray.ior = 1.0;
73 if(top >= MAX_LEVEL - 1) {
74 color += backdrop(stack[top].ray.dir) * stack[top].ray.energy;
78 if(stack[top].ray.energy < 1e-3) {
83 if(!cast_ray(stack[top].ray, color, stack[top].hit)) {
89 /* found a hit, recurse for reflection/refraction */
90 HitPoint hit = stack[top].hit;
92 // 1.0 when entering, 0.0 when leaving
93 float entering = step(0.0, dot(-stack[top].ray.dir, hit.norm));
94 vec3 norm = faceforward(hit.norm, stack[top].ray.dir, hit.norm);
96 int op = stack[top].op++;
99 float energy = stack[top].ray.energy * hit.mtl.refl;
103 stack[next].ray.org = hit.pos + norm * 1e-5;
104 stack[next].ray.dir = reflect(stack[top].ray.dir, norm);
105 stack[next].ray.energy = energy;
110 float energy = stack[top].ray.energy * hit.mtl.refr;
112 float next_ior = mix(stack[top - 1].ray.ior, hit.mtl.ior, entering);
113 float ior = stack[top].ray.ior / next_ior;
116 stack[next].ray.org = hit.pos - norm * 1e-5;
117 stack[next].ray.dir = refract(stack[top].ray.dir, norm, ior);
118 stack[next].ray.energy = energy;
119 stack[next].ray.ior = next_ior;
128 gl_FragColor = vec4(color, 1.0);
131 vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit)
134 vec3 norm = faceforward(hit.norm, rd, hit.norm);
135 vec3 ldir = light_pos - hit.pos;
137 vec3 amb = hit.mtl.diffuse * 0.02;
139 isect_scene(hit.pos + norm * 0.01, ldir, shadow_hit);
141 vec3 l = normalize(ldir);
142 vec3 v = normalize(-rd);
143 vec3 h = normalize(v + l);
144 float ndotl = max(dot(norm, l), 0.0);
145 float ndoth = max(dot(norm, h), 0.0);
147 vec3 lit = hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin);
149 return amb + lit * shadow_hit.shadow;
152 #define M_PI 3.1415926
153 #define M_2PI (M_PI * 2.0)
155 vec3 backdrop(in vec3 dir)
157 return vec3(0.1, 0.15, 1.0);
160 #define FLOOR_OFFS vec3(3.0, 0.0, 0.0)
161 #define FLOOR_SIZE vec2(5.5, 15.0)
163 #define GLASS_POS vec3(0.0, 0.2, 1.2)
165 bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
168 HitPoint hit, nearest;
170 nearest.dist = 10000.0;
172 if(isect_sphere(ro, rd, vec3(1.5, -0.5, 0.0), 0.85, hit)) {
174 nearest.mtl = mtl_sph;
176 opacity *= mtl_sph.refr;
180 if(isect_sphere(ro, rd, GLASS_POS, 0.9, hit)) {
181 if(hit.dist < nearest.dist) {
183 nearest.mtl = mtl_glass;
186 opacity *= mtl_glass.refr;
190 if(isect_sphere(ro, rd, GLASS_POS, 0.86, hit)) {
191 if(hit.dist < nearest.dist) {
193 nearest.mtl = mtl_air;
197 if(isect_floor(ro, rd, FLOOR_SIZE, hit) && hit.dist < nearest.dist) {
199 nearest.mtl = mtl_floor;
200 nearest.mtl.diffuse = tex_chess(vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 0.0), hit.surfpos);
203 if(nearest.dist >= 10000.0) {
204 hit_res.shadow = 1.0;
209 hit_res.shadow = opacity;
213 bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit)
215 if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.8), hit)) {
219 if(abs(hit.pos.x) >= sz.x || abs(hit.pos.z) >= sz.y) {
223 hit.pos += FLOOR_OFFS;
224 hit.surfpos /= sz * 2.0;
228 bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit)
230 float ndotrd = dot(rd, plane.xyz);
232 if(abs(ndotrd) < 1e-6) {
236 vec3 pp = plane.xyz * plane.w;
238 float t = dot(pdir, plane.xyz) / ndotrd;
245 hit.pos = ro + rd * t;
246 hit.norm = plane.xyz;
247 hit.surfpos = hit.pos.xz; /* XXX */
251 bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit)
253 float a = dot(rd, rd);
254 float b = dot(rd * 2.0, (ro - pos));
255 float c = dot(ro, ro) + dot(pos, pos) - 2.0 * dot(ro, pos) - rad * rad;
257 float d = b * b - 4.0 * a * c;
262 float t0 = (-b + sqrt(d)) / (2.0 * a);
263 float t1 = (-b - sqrt(d)) / (2.0 * a);
265 if(t0 < 0.0) t0 = t1;
266 if(t1 < 0.0) t1 = t0;
267 float t = min(t0, t1);
274 hit.pos = ro + rd * t;
275 hit.norm = normalize(hit.pos - pos);
276 hit.surfpos.x = atan(hit.norm.z, hit.norm.x);
277 hit.surfpos.y = acos(hit.norm.y);
281 vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos)
283 float foo = step(0.5, mod(spos.x * 8.0, 1.0)) * 2.0 - 1.0;
284 float bar = step(0.5, mod(spos.y * 24.0, 1.0)) * 2.0 - 1.0;
286 float xor = (foo * bar) * 0.5 + 0.5;
288 return mix(col1, col2, xor);