Added "count" texture.
[raymarcher.git] / Textures.lua
1 -- Textures for raymarcher
2 require "Vector"
3 local V = Vector
4
5 local function make_phong(lights, colour, amb, spec, shiny)
6    return function (location, ray_dir, normal, count)
7       local thiscol = {0, 0, 0}
8       local reflected_ray = V.normalize(normal*(ray_dir*normal*2) - ray_dir)
9       for _,light in ipairs(lights) do
10          local light_dir = V.normalize(light-location)
11          local Iamb = math.max(normal*light_dir, 0)
12          local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny)
13             
14          thiscol[1] = thiscol[1] + colour[1]*Iamb*amb + Ispec*spec
15          thiscol[2] = thiscol[2] + colour[2]*Iamb*amb + Ispec*spec
16          thiscol[3] = thiscol[3] + colour[3]*Iamb*amb + Ispec*spec
17       end
18
19       return thiscol
20    end
21 end
22
23 local function make_count_texture()
24    return function (location, ray_dir, normal, count)
25       return {count/10, count/10, count/10}
26    end
27 end
28
29    
30 Textures = {
31    make_phong = make_phong,
32    make_count_texture = make_count_texture
33 }
34
35 return Textures