15 local bg_col = {1,0,1}
17 local function calculate_normal(sdf, l)
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)
24 local function march(location, ray_dir, sdf, count)
25 local p = sdf(location)
27 return p.texture(location, ray_dir, calculate_normal(sdf, location-(ray_dir*eps)), count)
28 elseif p.dist > max_dist then
31 return march(location + ray_dir*p.dist,
32 ray_dir, sdf, count+1)
36 local function render(scene, width, height, filename)
37 print("Rendering to file " .. filename .. "...")
39 local f = io.open(filename, "w")
40 f:write("P3 ", width, " ", height, " 255\n")
42 local c = scene.camera
43 local cam_dir = V.normalize(c.point_at - c.location)
44 local right = V.normalize(c.right)
45 local up = V.cross(right,cam_dir)
46 local aspect = height/width;
50 if y % math.floor(height/10) == 0 then
51 print(y/math.floor(height/10) * 10 .. "%")
54 local rayy = cam_dir + up*((y/height - 0.5)*c.fov*aspect)
57 local ray_dir = V.normalize(rayy + right*((x/width - 0.5)*c.fov))
58 local col = march(c.location, ray_dir, scene.sdf, 0)
59 col = {math.min(col[1]*255, 255),
60 math.min(col[2]*255, 255),
61 math.min(col[3]*255, 255)}
63 f:write(math.floor(col[1]), " ", math.floor(col[2]), " ", math.floor(col[3]), " ")
77 sdf = O.union(P.make_sphere(V.new{0,0,0}, 1,
78 -- T.make_count_texture()),
79 T.make_phong({V.new{2,-1,2}}, {0,1,0},
81 P.make_sphere(V.new{1.5,0,1.0}, 0.5,
82 T.make_phong({V.new{2,-1,2}}, {0,0,1},
85 camera = {location = V.new{0,-5,0},
86 point_at = V.new{0,0,0},
90 render(scene, 640, 480, "test.ppm")