Colours are now Vectors, added rainbow pigment.
[raymarcher.git] / Textures.lua
index 1456f7d..324a2d2 100644 (file)
@@ -1,8 +1,12 @@
 -- Textures for raymarcher
+
 require "Vector"
 local V = Vector
 
-local function make_phong_texture(lights, pigment, amb, diff, spec, shiny)
+require "Colours"
+local C = Colours
+
+local function make_phong_texture(lights, mapped_pigment, amb, diff, spec, shiny)
    local normalize = V.normalize
 
    local eps = 0.0001
@@ -19,7 +23,7 @@ local function make_phong_texture(lights, pigment, amb, diff, spec, shiny)
    end
    
    return function (location, ray_dir, normal, count, sdf)
-      local colour = pigment(location[1],location[2])
+      local colour = mapped_pigment(location)
       local thiscol = {colour[1]*amb, colour[2]*amb, colour[3]*amb}
 
       local reflected_ray = ray_dir - normal*(ray_dir*normal*2)
@@ -28,6 +32,7 @@ local function make_phong_texture(lights, pigment, amb, diff, spec, shiny)
          local light_dir = (light-location)/light_dist
 
          local vis = light_visibility(location + light_dir*eps, light_dir, eps, light_dist, 1, sdf)
+         -- local vis = 1.0
 
          local Idiff = math.max(normal*light_dir, 0)
          local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny)
@@ -47,51 +52,129 @@ local function make_count_texture(scale)
    end
 end
 
-local function make_flat_texture(pigment)
+local function make_flat_texture(mapped_pigment)
    return function (location, ray_dir, normal, count, sdf)
-      return pigment(location[1], location[2])
+      return mapped_pigment(location)
    end
 end
 
+
+-- Pigments
+
 local function make_solid_pigment(colour)
-   return function (x,y)
+   return function(x,y)
       return colour
    end
 end
 
-local function make_checkered_pigment(colour1, colour2)
-   return function (x,y)
+local function make_checkered_pigment(pigment1, pigment2)
+   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
+         return pigment1(x,y)
       else
-         return colour2
+         return pigment2(x,y)
       end
    end
 end
 
-local function make_mandelbrot_pigment()
+local function make_image_pigment(filename,scale)
+
+   print("Loading image pigmant from '" .. filename .. "'...")
+
+   local f = assert(io.open(filename, "rb"))
+   local data = f:read("*all")
+   f:close()
 
-   local maxiter = 100
-   local set_colour = {0,0,0}
+   local start,_,wstr,hstr,dstr,raster =
+      string.find(data, "^P6[%s%c]+(%d+)[%s%c]+(%d+)[%s%c]+(%d+)[%s%c](.*)$")
+
+   if start ~= 1 then
+      error("Error reading image. Is it in PPM(P6) format?")
+   end
+
+   if dstr ~= "255" then
+      error("Only images with a depth of 255 are supported.")
+   end
+
+   local width = tonumber(wstr)
+   local height = tonumber(hstr)
+   local depth = tonumber(dstr)
+
+   local pixels = {}
+   local i = 1
+   for triple in string.gmatch(raster, "...") do
+      local _,_,r,g,b = string.find(triple, "(.)(.)(.)")
+      pixels[i] = {string.byte(r)/depth, string.byte(g)/depth, string.byte(b)/depth}
+      i = i + 1
+   end
+   
+   print("done.")
+   print("(Read " .. tostring(#pixels) .. " pixels.)")
+
+   local pixels_per_unit = math.max(width,height)
+
+   return function(x,y)
+      local xi = math.floor(((x+0.5)*pixels_per_unit/scale)%width) + 1
+      local yi = math.floor(((y+0.5)*pixels_per_unit/scale)%height) + 1
+      local i = width*(yi-1) + xi
+      if pixels[i] == nil then
+         error("Pixel not found at xi=" .. tostring(xi) .. " yi=" .. tostring(yi))
+      end
+      return pixels[i]
+   end
+end
+
+local function make_mandelbrot_pigment(set_pigment, nonset_pigment, max_iter)
 
    local function get_col(x,y,cx,cy,iter)
       if iter == 0 then
-         return set_colour
+         return set_pigment(cx, cy)
       elseif x^2 + y^2 > 4 then
-         local I = iter/max_iter
-         return {I,I,1-I}
+         return nonset_pigment(cx, cy)
       else
          return get_col(x^2 - y^2 + cx,
                         2*x*y + cy,
                         cx, cy, iter-1)
       end
+   end
 
-      local xp = x^2 - y^2 + cx
-      local yp = 2*x*y + cy
+   return function (x,y)
+      return get_col(0,0,x,y,max_iter)
    end
+end
 
+local function make_rainbow_pigment(scale)
    return function (x,y)
-      get_col(0,0,x,y,maxiter)
+
+      local Ired = math.sin(x/math.pi/scale)^2 * math.sin(y/math.pi/scale)^2
+      local Igreen = math.cos(x/math.pi/scale)^2 * math.sin(y/math.pi/scale)^2
+      local Iblue = math.sin(x/math.pi/scale)^2 * math.cos(y/math.pi/scale)^2
+
+      return C.red*Ired + C.green*Igreen + C.blue*Iblue
+   end
+end
+
+
+-- Mapping functions
+
+-- These functions define mappings from 3D world coordinates to 2D
+-- texture/pigment coordinates
+
+local function map_rectangular(pigment, xvec, yvec)
+   return function(location)
+      return pigment(location*xvec, location*yvec)
+   end
+end
+
+local function map_spherical(pigment, centre, scale_theta, scale_phi)
+   return function(location)
+      local r = location-centre
+      local rnorm = V.norm(r)
+      
+      local phi = math.acos((V.z*r)/rnorm)
+      local theta = math.acos((V.x*r)/V.norm(V.cross(V.z,r)))
+
+      return pigment(theta*scale_theta/math.pi, phi*scale_phi/math.pi)
    end
 end
    
@@ -100,7 +183,12 @@ Textures = {
    make_flat_texture = make_flat_texture,
    make_count_texture = make_count_texture,
    make_solid_pigment = make_solid_pigment,
-   make_checkered_pigment = make_checkered_pigment
+   make_checkered_pigment = make_checkered_pigment,
+   make_image_pigment = make_image_pigment,
+   make_rainbow_pigment = make_rainbow_pigment,
+   make_mandelbrot_pigment = make_mandelbrot_pigment,
+   map_rectangular = map_rectangular,
+   map_spherical = map_spherical
 }
 
 return Textures