Added missing export of mandel pigment.
[raymarcher.git] / Textures.lua
1 -- Textures for raymarcher
2 require "Vector"
3 local V = Vector
4
5 local function make_phong_texture(lights, pigment, amb, diff, spec, shiny)
6    local normalize = V.normalize
7
8    local eps = 0.0001
9    local function light_visibility(location, light_dir, pos, dest, res, sdf)
10       local p = sdf(location)
11       if pos + p.dist > dest then
12          return res
13       elseif p.dist < eps then
14          return 0.0
15       else
16          return light_visibility(location + light_dir*p.dist, light_dir, pos+p.dist,
17                                  dest, math.min(res, 30*p.dist/(pos+p.dist)), sdf)
18       end
19    end
20    
21    return function (location, ray_dir, normal, count, sdf)
22       local colour = pigment(location[1],location[2])
23       local thiscol = {colour[1]*amb, colour[2]*amb, colour[3]*amb}
24
25       local reflected_ray = ray_dir - normal*(ray_dir*normal*2)
26       for _,light in ipairs(lights) do
27          local light_dist = V.norm(light-location)
28          local light_dir = (light-location)/light_dist
29
30          local vis = light_visibility(location + light_dir*eps, light_dir, eps, light_dist, 1, sdf)
31
32          local Idiff = math.max(normal*light_dir, 0)
33          local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny)
34             
35          thiscol[1] = thiscol[1] + vis*(colour[1]*Idiff*diff + Ispec*spec)
36          thiscol[2] = thiscol[2] + vis*(colour[2]*Idiff*diff + Ispec*spec)
37          thiscol[3] = thiscol[3] + vis*(colour[3]*Idiff*diff + Ispec*spec)
38       end
39
40       return thiscol
41    end
42 end
43
44 local function make_count_texture(scale)
45    return function (location, ray_dir, normal, count, sdf)
46       return {count*scale, count*scale, count*scale}
47    end
48 end
49
50 local function make_flat_texture(pigment)
51    return function (location, ray_dir, normal, count, sdf)
52       return pigment(location[1], location[2])
53    end
54 end
55
56 local function make_solid_pigment(colour)
57    return function (x,y)
58       return colour
59    end
60 end
61
62 local function make_checkered_pigment(colour1, colour2)
63    return function (x,y)
64       if (x%1 < 0.5 and y%1 < 0.5) or (x%1 > 0.5 and y%1 > 0.5) then
65          return colour1
66       else
67          return colour2
68       end
69    end
70 end
71
72 local function make_mandelbrot_pigment(set_colour, max_iter)
73
74    local function get_col(x,y,cx,cy,iter)
75       if iter == 0 then
76          return set_colour
77       elseif x^2 + y^2 > 4 then
78          local I = iter/max_iter
79          return {I,I,1-I}
80       else
81          return get_col(x^2 - y^2 + cx,
82                         2*x*y + cy,
83                         cx, cy, iter-1)
84       end
85    end
86
87    return function (x,y)
88       get_col(0,0,x,y,max_iter)
89    end
90 end
91    
92 Textures = {
93    make_phong_texture = make_phong_texture,
94    make_flat_texture = make_flat_texture,
95    make_count_texture = make_count_texture,
96    make_solid_pigment = make_solid_pigment,
97    make_checkered_pigment = make_checkered_pigment,
98    make_mandelbrot_pigment = make_mandelbrot_pigment
99 }
100
101 return Textures