bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit);
-bool isect_floor(in vec3 ro, in vec3 rd, float rad, out HitPoint hit);
+bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit);
bool isect_plane(in vec3 ro, in vec3 rd, in vec4 plane, out HitPoint hit);
bool isect_sphere(in vec3 ro, in vec3 rd, in vec3 pos, float rad, out HitPoint hit);
+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 vec3 light_pos = vec3(-10, 50, 30);
void main()
{
vec3 shade(in vec3 ro, in vec3 rd, in HitPoint hit)
{
- return (hit.norm * 0.5 + 0.5) * hit.mtl.diffuse;
+ HitPoint shadow_hit;
+ vec3 ldir = light_pos - hit.pos;
+
+ vec3 col = vec3(0.03, 0.03, 0.03); // ambient
+
+ 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);
+
+ col += hit.mtl.diffuse * ndotl + hit.mtl.specular * pow(ndoth, hit.mtl.shin);
+ }
+
+ return col;
}
#define M_PI 3.1415926
return mix(vec3(0.8, 0.3, 0.2), vec3(0.2, 0.3, 0.8), smoothstep(-0.1, 0.1, dir.y));
}
-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);
+#define FLOOR_OFFS vec3(3.0, 0.0, 0.0)
+#define FLOOR_SIZE vec2(5.5, 15.0)
bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
{
nearest.dist = 10000.0;
- if(isect_sphere(ro, rd, vec3(0.0, 0.0, 0.0), 1.0, hit)) {
+ if(isect_sphere(ro, rd, vec3(1.0, 0.0, 0.0), 1.0, hit)) {
nearest = hit;
nearest.mtl = mtl_sph;
}
- if(isect_floor(ro, rd, 5.0, hit) && hit.dist < nearest.dist) {
+ 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 >= 10000.0) {
return true;
}
-bool isect_floor(in vec3 ro, in vec3 rd, float rad, out HitPoint hit)
+bool isect_floor(in vec3 ro, in vec3 rd, in vec2 sz, out HitPoint hit)
{
- if(!isect_plane(ro, rd, vec4(0.0, 1.0, 0.0, -1.0), hit)) {
+ if(!isect_plane(ro - FLOOR_OFFS, rd, vec4(0.0, 1.0, 0.0, -1.8), hit)) {
return false;
}
- float d = max(abs(hit.pos.x), abs(hit.pos.z));
- if(d >= rad) 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;
}
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.y = acos(hit.norm.y);
return true;
}
+
+vec3 tex_chess(in vec3 col1, in vec3 col2, in vec2 spos)
+{
+ float foo = step(0.5, mod(spos.x * 8.0, 1.0)) * 2.0 - 1.0;
+ float bar = step(0.5, mod(spos.y * 24.0, 1.0)) * 2.0 - 1.0;
+
+ float xor = (foo * bar) * 0.5 + 0.5;
+
+ return mix(col1, col2, xor);
+}