Soft shadows.
[raymarcher.git] / Textures.lua
index a06faa7..1e919c7 100644 (file)
@@ -2,34 +2,79 @@
 require "Vector"
 local V = Vector
 
-local function make_phong(lights, colour, amb, spec, shiny)
-   return function (location, ray_dir, normal, count)
-      local thiscol = {0, 0, 0}
-      local reflected_ray = V.normalize(normal*(ray_dir*normal*2) - ray_dir)
+local function make_phong_texture(lights, pigment, amb, diff, spec, shiny)
+   local normalize = V.normalize
+
+   local eps = 0.0001
+   local function light_visibility(location, light_dir, pos, dest, res, sdf)
+      local p = sdf(location)
+      if pos + p.dist > dest then
+         return res
+      elseif p.dist < eps then
+         return 0.0
+      else
+         return light_visibility(location + light_dir*p.dist, light_dir, pos+p.dist,
+                                 dest, math.min(res, 30*p.dist/(pos+p.dist)), sdf)
+      end
+   end
+   
+   return function (location, ray_dir, normal, count, sdf)
+      local colour = pigment(location[1],location[2])
+      local thiscol = {colour[1]*amb, colour[2]*amb, colour[3]*amb}
+
+      local reflected_ray = ray_dir - normal*(ray_dir*normal*2)
       for _,light in ipairs(lights) do
-         local light_dir = V.normalize(light-location)
-         local Iamb = math.max(normal*light_dir, 0)
+         local light_dist = V.norm(light-location)
+         local light_dir = (light-location)/light_dist
+
+         local vis = light_visibility(location + light_dir*eps, light_dir, eps, light_dist, 1, sdf)
+
+         local Idiff = math.max(normal*light_dir, 0)
          local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny)
             
-         thiscol[1] = thiscol[1] + colour[1]*Iamb*amb + Ispec*spec
-         thiscol[2] = thiscol[2] + colour[2]*Iamb*amb + Ispec*spec
-         thiscol[3] = thiscol[3] + colour[3]*Iamb*amb + Ispec*spec
+         thiscol[1] = thiscol[1] + vis*(colour[1]*Idiff*diff + Ispec*spec)
+         thiscol[2] = thiscol[2] + vis*(colour[2]*Idiff*diff + Ispec*spec)
+         thiscol[3] = thiscol[3] + vis*(colour[3]*Idiff*diff + Ispec*spec)
       end
 
       return thiscol
    end
 end
 
-local function make_count_texture()
-   return function (location, ray_dir, normal, count)
-      return {count/10, count/10, count/10}
+local function make_count_texture(scale)
+   return function (location, ray_dir, normal, count, sdf)
+      return {count*scale, count*scale, count*scale}
+   end
+end
+
+local function make_flat_texture(pigment)
+   return function (location, ray_dir, normal, count, sdf)
+      return pigment(location[1], location[2])
+   end
+end
+
+local function make_solid_pigment(colour)
+   return function (x,y)
+      return colour
    end
 end
 
+local function make_checkered_pigment(colour1, colour2)
+   return function (x,y)
+      if (x%1 < 0.5 and y%1 < 0.5) or (x%1 > 0.5 and y%1 > 0.5) then
+         return colour1
+      else
+         return colour2
+      end
+   end
+end
    
 Textures = {
-   make_phong = make_phong,
-   make_count_texture = make_count_texture
+   make_phong_texture = make_phong_texture,
+   make_flat_texture = make_flat_texture,
+   make_count_texture = make_count_texture,
+   make_solid_pigment = make_solid_pigment,
+   make_checkered_pigment = make_checkered_pigment
 }
 
 return Textures