+struct Ray {
+ vec3 org, dir;
+ float energy, ior;
+};
+
struct Material {
vec3 diffuse, specular;
float shin;
+ float refl, refr;
+ float ior;
};
struct HitPoint {
vec3 pos, norm;
vec2 surfpos;
Material mtl;
+ float shadow; /* opacity along the line of sight up to dist=1 */
};
varying vec3 v_rorg, v_rdir;
vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos);
-const Material mtl_sph = Material(vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0), 80.0);
-const Material mtl_floor = Material(vec3(0.5, 0.5, 0.5), vec3(0.0, 0.0, 0.0), 1.0);
+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);
+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);
+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);
+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);
const vec3 light_pos = vec3(-10, 50, 30);
-void main()
+bool cast_ray(in Ray ray, inout vec3 color, out HitPoint hit)
{
- vec3 rdir = normalize(v_rdir);
- gl_FragColor.rgb = backdrop(rdir);
+ if(isect_scene(ray.org, ray.dir, hit)) {
+ color += shade(ray.org, ray.dir, hit) * ray.energy;
+ return true;
+ }
+ color += backdrop(ray.dir);
+ return false;
+}
+#define MAX_LEVEL 8
+
+struct StackFrame {
+ int op;
+ Ray ray;
HitPoint hit;
- if(isect_scene(v_rorg, rdir, hit)) {
- gl_FragColor.rgb = shade(v_rorg, rdir, hit);
- } else {
- gl_FragColor.rgb = backdrop(rdir);
+};
+
+void main()
+{
+ StackFrame stack[MAX_LEVEL];
+
+ vec3 color = vec3(0.0, 0.0, 0.0);
+
+ stack[0].op = 0;
+ stack[0].ray.org = v_rorg;
+ stack[0].ray.dir = normalize(v_rdir);
+ stack[0].ray.energy = stack[0].ray.ior = 1.0;
+
+ int top = 0;
+ while(top >= 0) {
+ if(top >= MAX_LEVEL - 1) {
+ color += backdrop(stack[top].ray.dir) * stack[top].ray.energy;
+ top--;
+ continue;
+ }
+ if(stack[top].ray.energy < 1e-3) {
+ top--;
+ continue;
+ }
+
+ if(!cast_ray(stack[top].ray, color, stack[top].hit)) {
+ /* no hit, return */
+ top--;
+ continue;
+ }
+
+ /* found a hit, recurse for reflection/refraction */
+ HitPoint hit = stack[top].hit;
+
+ // 1.0 when entering, 0.0 when leaving
+ float entering = step(0.0, dot(-stack[top].ray.dir, hit.norm));
+ vec3 norm = faceforward(hit.norm, stack[top].ray.dir, hit.norm);
+
+ int op = stack[top].op++;
+ if(op == 0) {
+ // reflection
+ float energy = stack[top].ray.energy * hit.mtl.refl;
+ if(energy > 1e-4) {
+ int next = top + 1;
+ stack[next].op = 0;
+ stack[next].ray.org = hit.pos + norm * 1e-5;
+ stack[next].ray.dir = reflect(stack[top].ray.dir, norm);
+ stack[next].ray.energy = energy;
+ top = next;
+ }
+ } else if(op == 1) {
+ // refraction
+ float energy = stack[top].ray.energy * hit.mtl.refr;
+ if(energy > 1e-4) {
+ float next_ior = mix(stack[top - 1].ray.ior, hit.mtl.ior, entering);
+ float ior = stack[top].ray.ior / next_ior;
+ int next = top + 1;
+ stack[next].op = 0;
+ stack[next].ray.org = hit.pos - norm * 1e-5;
+ stack[next].ray.dir = refract(stack[top].ray.dir, norm, ior);
+ stack[next].ray.energy = energy;
+ stack[next].ray.ior = next_ior;
+ top = next;
+ }
+ } else if(op == 2) {
+ // return
+ top--;
+ }
}
- gl_FragColor.a = 1.0;
+
+ gl_FragColor = vec4(color, 1.0);
}
vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit)
{
HitPoint shadow_hit;
+ vec3 norm = faceforward(hit.norm, rd, hit.norm);
vec3 ldir = light_pos - hit.pos;
- vec3 col = vec3(0.03, 0.03, 0.03); // ambient
+ vec3 amb = hit.mtl.diffuse * 0.02;
- if(!isect_scene(hit.pos + hit.norm * 0.01, ldir, shadow_hit) || shadow_hit.dist > 1.0) {
- vec3 l = normalize(ldir);
- vec3 v = normalize(-rd);
- vec3 h = normalize(v + l);
- float ndotl = max(dot(hit.norm, l), 0.0);
- float ndoth = max(dot(hit.norm, h), 0.0);
+ isect_scene(hit.pos + norm * 0.01, ldir, shadow_hit);
- col += hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin);
- }
+ vec3 l = normalize(ldir);
+ vec3 v = normalize(-rd);
+ vec3 h = normalize(v + l);
+ float ndotl = max(dot(norm, l), 0.0);
+ float ndoth = max(dot(norm, h), 0.0);
- return col;
+ vec3 lit = hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin);
+
+ return amb + lit * shadow_hit.shadow;
}
#define M_PI 3.1415926
vec3 backdrop(in vec3 dir)
{
- return mix(vec3(0.8, 0.3, 0.2), vec3(0.2, 0.3, 0.8), smoothstep(-0.1, 0.1, dir.y));
+ return vec3(0.1, 0.15, 1.0);
}
#define FLOOR_OFFS vec3(3.0, 0.0, 0.0)
#define FLOOR_SIZE vec2(5.5, 15.0)
+#define GLASS_POS vec3(0.0, 0.2, 1.2)
+
bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
{
+ float opacity = 1.0;
HitPoint hit, nearest;
nearest.dist = 10000.0;
- if(isect_sphere(ro, rd, vec3(1.0, 0.0, 0.0), 1.0, hit)) {
+ if(isect_sphere(ro, rd, vec3(1.5, -0.5, 0.0), 0.85, hit)) {
nearest = hit;
nearest.mtl = mtl_sph;
+ if(hit.dist < 1.0) {
+ opacity *= mtl_sph.refr;
+ }
+ }
+
+ if(isect_sphere(ro, rd, GLASS_POS, 0.9, hit)) {
+ if(hit.dist < nearest.dist) {
+ nearest = hit;
+ nearest.mtl = mtl_glass;
+ }
+ if(hit.dist < 1.0) {
+ opacity *= mtl_glass.refr;
+ }
+ }
+
+ if(isect_sphere(ro, rd, GLASS_POS, 0.86, hit)) {
+ if(hit.dist < nearest.dist) {
+ nearest = hit;
+ nearest.mtl = mtl_air;
+ }
}
if(isect_floor(ro, rd, FLOOR_SIZE, hit) && hit.dist < nearest.dist) {
}
if(nearest.dist >= 10000.0) {
+ hit_res.shadow = 1.0;
return false;
}
hit_res = nearest;
+ hit_res.shadow = opacity;
return true;
}