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