Colours are now Vectors, added rainbow pigment.
[raymarcher.git] / Textures.lua
1 -- Textures for raymarcher
2
3 require "Vector"
4 local V = Vector
5
6 require "Colours"
7 local C = Colours
8
9 local function make_phong_texture(lights, mapped_pigment, amb, diff, spec, shiny)
10    local normalize = V.normalize
11
12    local eps = 0.0001
13    local function light_visibility(location, light_dir, pos, dest, res, sdf)
14       local p = sdf(location)
15       if pos + p.dist > dest then
16          return res
17       elseif p.dist < eps then
18          return 0.0
19       else
20          return light_visibility(location + light_dir*p.dist, light_dir, pos+p.dist,
21                                  dest, math.min(res, 30*p.dist/(pos+p.dist)), sdf)
22       end
23    end
24    
25    return function (location, ray_dir, normal, count, sdf)
26       local colour = mapped_pigment(location)
27       local thiscol = {colour[1]*amb, colour[2]*amb, colour[3]*amb}
28
29       local reflected_ray = ray_dir - normal*(ray_dir*normal*2)
30       for _,light in ipairs(lights) do
31          local light_dist = V.norm(light-location)
32          local light_dir = (light-location)/light_dist
33
34          local vis = light_visibility(location + light_dir*eps, light_dir, eps, light_dist, 1, sdf)
35          -- local vis = 1.0
36
37          local Idiff = math.max(normal*light_dir, 0)
38          local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny)
39             
40          thiscol[1] = thiscol[1] + vis*(colour[1]*Idiff*diff + Ispec*spec)
41          thiscol[2] = thiscol[2] + vis*(colour[2]*Idiff*diff + Ispec*spec)
42          thiscol[3] = thiscol[3] + vis*(colour[3]*Idiff*diff + Ispec*spec)
43       end
44
45       return thiscol
46    end
47 end
48
49 local function make_count_texture(scale)
50    return function (location, ray_dir, normal, count, sdf)
51       return {count*scale, count*scale, count*scale}
52    end
53 end
54
55 local function make_flat_texture(mapped_pigment)
56    return function (location, ray_dir, normal, count, sdf)
57       return mapped_pigment(location)
58    end
59 end
60
61
62 -- Pigments
63
64 local function make_solid_pigment(colour)
65    return function(x,y)
66       return colour
67    end
68 end
69
70 local function make_checkered_pigment(pigment1, pigment2)
71    return function(x,y)
72       if (x%1 < 0.5 and y%1 < 0.5) or (x%1 > 0.5 and y%1 > 0.5) then
73          return pigment1(x,y)
74       else
75          return pigment2(x,y)
76       end
77    end
78 end
79
80 local function make_image_pigment(filename,scale)
81
82    print("Loading image pigmant from '" .. filename .. "'...")
83
84    local f = assert(io.open(filename, "rb"))
85    local data = f:read("*all")
86    f:close()
87
88    local start,_,wstr,hstr,dstr,raster =
89       string.find(data, "^P6[%s%c]+(%d+)[%s%c]+(%d+)[%s%c]+(%d+)[%s%c](.*)$")
90
91    if start ~= 1 then
92       error("Error reading image. Is it in PPM(P6) format?")
93    end
94
95    if dstr ~= "255" then
96       error("Only images with a depth of 255 are supported.")
97    end
98
99    local width = tonumber(wstr)
100    local height = tonumber(hstr)
101    local depth = tonumber(dstr)
102
103    local pixels = {}
104    local i = 1
105    for triple in string.gmatch(raster, "...") do
106       local _,_,r,g,b = string.find(triple, "(.)(.)(.)")
107       pixels[i] = {string.byte(r)/depth, string.byte(g)/depth, string.byte(b)/depth}
108       i = i + 1
109    end
110    
111    print("done.")
112    print("(Read " .. tostring(#pixels) .. " pixels.)")
113
114    local pixels_per_unit = math.max(width,height)
115
116    return function(x,y)
117       local xi = math.floor(((x+0.5)*pixels_per_unit/scale)%width) + 1
118       local yi = math.floor(((y+0.5)*pixels_per_unit/scale)%height) + 1
119       local i = width*(yi-1) + xi
120       if pixels[i] == nil then
121          error("Pixel not found at xi=" .. tostring(xi) .. " yi=" .. tostring(yi))
122       end
123       return pixels[i]
124    end
125 end
126
127 local function make_mandelbrot_pigment(set_pigment, nonset_pigment, max_iter)
128
129    local function get_col(x,y,cx,cy,iter)
130       if iter == 0 then
131          return set_pigment(cx, cy)
132       elseif x^2 + y^2 > 4 then
133          return nonset_pigment(cx, cy)
134       else
135          return get_col(x^2 - y^2 + cx,
136                         2*x*y + cy,
137                         cx, cy, iter-1)
138       end
139    end
140
141    return function (x,y)
142       return get_col(0,0,x,y,max_iter)
143    end
144 end
145
146 local function make_rainbow_pigment(scale)
147    return function (x,y)
148
149       local Ired = math.sin(x/math.pi/scale)^2 * math.sin(y/math.pi/scale)^2
150       local Igreen = math.cos(x/math.pi/scale)^2 * math.sin(y/math.pi/scale)^2
151       local Iblue = math.sin(x/math.pi/scale)^2 * math.cos(y/math.pi/scale)^2
152
153       return C.red*Ired + C.green*Igreen + C.blue*Iblue
154    end
155 end
156
157
158 -- Mapping functions
159
160 -- These functions define mappings from 3D world coordinates to 2D
161 -- texture/pigment coordinates
162
163 local function map_rectangular(pigment, xvec, yvec)
164    return function(location)
165       return pigment(location*xvec, location*yvec)
166    end
167 end
168
169 local function map_spherical(pigment, centre, scale_theta, scale_phi)
170    return function(location)
171       local r = location-centre
172       local rnorm = V.norm(r)
173       
174       local phi = math.acos((V.z*r)/rnorm)
175       local theta = math.acos((V.x*r)/V.norm(V.cross(V.z,r)))
176
177       return pigment(theta*scale_theta/math.pi, phi*scale_phi/math.pi)
178    end
179 end
180    
181 Textures = {
182    make_phong_texture = make_phong_texture,
183    make_flat_texture = make_flat_texture,
184    make_count_texture = make_count_texture,
185    make_solid_pigment = make_solid_pigment,
186    make_checkered_pigment = make_checkered_pigment,
187    make_image_pigment = make_image_pigment,
188    make_rainbow_pigment = make_rainbow_pigment,
189    make_mandelbrot_pigment = make_mandelbrot_pigment,
190    map_rectangular = map_rectangular,
191    map_spherical = map_spherical
192 }
193
194 return Textures