Soft shadows.
[raymarcher.git] / raymarch.lua
1 require "Vector"
2 local V = Vector
3
4 require "Primitives"
5 local P = Primitives
6
7 require "Operations"
8 local O = Operations
9
10 require "Textures"
11 local T = Textures
12
13 local eps = 0.01
14 local max_dist2 = 30^2
15 local bg_col = {0.1,0.1,0.1}
16
17 local function calculate_normal(sdf, l)
18    local delta = 1e-3;
19    return V.new{sdf(l+V.x*delta).dist-sdf(l-V.x*delta).dist,
20                 sdf(l+V.y*delta).dist-sdf(l-V.y*delta).dist,
21                 sdf(l+V.z*delta).dist-sdf(l-V.z*delta).dist}/(2*delta)
22 end
23
24 local function march(start, location, ray_dir, sdf, count)
25    if V.norm2(location-start) > max_dist2 then
26       return bg_col
27    end
28    
29    local p = sdf(location)
30    if p.dist < eps then
31       return p.texture(location, ray_dir,
32                        calculate_normal(sdf, location-(ray_dir*eps)),
33                        count, sdf)
34    else
35       return march(start, location + ray_dir*p.dist,
36                    ray_dir, sdf, count+1)
37    end
38 end
39
40 local function render(scene, width, height, filename) 
41    print("Rendering to file " .. filename .. "...")
42    
43    local f = io.open(filename, "w")
44    f:write("P3 ", width, " ", height, " 255\n")
45
46    local c = scene.camera
47    local cam_dir = V.normalize(c.point_at - c.location)
48    local right = V.normalize(c.right)
49    local up = V.cross(right,cam_dir)
50    local aspect = height/width;
51
52    local normalize = V.normalize -- optimization
53    
54    for y=1,height do
55
56       if y % math.floor(height/10) == 0 then
57          print(y/math.floor(height/10) * 10 .. "%")
58       end
59
60       local rayy = cam_dir + up*((0.5 - y/height)*c.fov*aspect)
61
62       for x=1,width do
63          local ray_dir = normalize(rayy + right*((x/width - 0.5)*c.fov))
64          local col = march(c.location, c.location, ray_dir, scene.sdf, 0)
65          col = {math.min(col[1]*255, 255),
66                 math.min(col[2]*255, 255),
67                 math.min(col[3]*255, 255)}
68
69          f:write(math.floor(col[1]), " ", math.floor(col[2]), " ", math.floor(col[3]), " ")
70       end
71       f:write("\n")
72    end
73    f:write("\n")
74
75    f:close()
76
77    print("done")
78 end
79
80
81 local lights = {V.new{3,-3,1}}
82
83 local scene = {
84    sdf =
85       O.union(
86          O.diff(
87             O.diff(
88                O.diff(
89                   P.make_sphere(V.new{0,0,0}, 1,
90                                 T.make_phong_texture(lights,
91                                                      T.make_solid_pigment({0,1,0}),
92                                                      0.2, 0.7, 1.0, 100)),
93                   P.make_sphere(V.new{0,0,0}, 0.8)),
94                P.make_pipe(V.new{0,0,0}, 0.5, V.new{0,1,0})),
95             P.make_pipe(V.new{0,0,0}, 0.5, V.new{1,0,0})),
96
97          P.make_plane(V.new{0,0,-1.0}, V.new{0,0,1},
98                       T.make_phong_texture(lights,
99                          T.make_checkered_pigment({0.5,0,0.2}, {1,1,1}),
100                          0.2, 1.0, 0, 1))),
101                       -- T.make_flat_texture(T.make_checkered_pigment({0,0,1}, {1,1,1})))),
102                                
103    camera = {location = V.new{2,-5,1},
104              point_at = V.new{0,0,0},
105              right = V.x,
106              fov = 1}}
107
108 -- render(scene, 320, 200, "test.ppm")
109 render(scene, 640, 480, "test.ppm")