+ 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) {
+ nearest = hit;
+ nearest.mtl = mtl_floor;
+ nearest.mtl.diffuse = tex_chess(vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 0.0), hit.surfpos);
+ }
+
+ if(nearest.dist >= 9999.0) {
+ hit_res.shadow = 1.0;
+ return false;
+ }
+
+ hit_res = nearest;
+ hit_res.shadow = opacity;
+ return true;
+}
+
+bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit)
+{
+ if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.8), hit)) {
+ return false;
+ }
+
+ if(abs(hit.pos.x) >= sz.x || abs(hit.pos.z) >= sz.y) {
+ return false;
+ }
+
+ hit.pos += FLOOR_OFFS;
+ hit.surfpos /= sz * 2.0;
+ return true;
+}
+
+bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit)
+{
+ float ndotrd = dot(rd, plane.xyz);
+
+ if(abs(ndotrd) < 1e-6) {
+ return false;
+ }
+
+ vec3 pp = plane.xyz * plane.w;
+ vec3 pdir = pp - ro;
+ float t = dot(pdir, plane.xyz) / ndotrd;
+
+ if(t < 1e-6) {
+ return false;
+ }
+
+ hit.dist = t;
+ hit.pos = ro + rd * t;
+ hit.norm = plane.xyz;
+ hit.surfpos = hit.pos.xz; /* XXX */
+ return true;
+}
+
+bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit)
+{
+ float a = dot(rd, rd);
+ float b = dot(rd * 2.0, (ro - pos));
+ float c = dot(ro, ro) + dot(pos, pos) - 2.0 * dot(ro, pos) - rad * rad;
+
+ float d = b * b - 4.0 * a * c;
+ if(d < 1e-6) {
+ return false;
+ }
+
+ float t0 = (-b + sqrt(d)) / (2.0 * a);
+ float t1 = (-b - sqrt(d)) / (2.0 * a);
+
+ if(t0 < 0.0) t0 = t1;
+ if(t1 < 0.0) t1 = t0;
+ float t = min(t0, t1);
+
+ if(t < 1e-6) {
+ return false;
+ }
+
+ hit.dist = t;
+ hit.pos = ro + rd * t;
+ hit.norm = normalize(hit.pos - pos);
+ hit.surfpos.x = atan(hit.norm.z, hit.norm.x);
+ hit.surfpos.y = acos(hit.norm.y);
+ return true;