Skip to content

Commit f5c2ebe

Browse files
committed
Extract scenes
1 parent 3354550 commit f5c2ebe

File tree

3 files changed

+91
-91
lines changed

3 files changed

+91
-91
lines changed

src/RayTracingWeekend.jl

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -223,30 +223,6 @@ end
223223
Scatter(Ray(rec.p, reflected), mat.albedo)
224224
end
225225

226-
"Scene with 2 Lambertian spheres"
227-
function scene_2_spheres(; elem_type::Type{T}) where T
228-
spheres = Sphere[]
229-
230-
# small center sphere
231-
push!(spheres, Sphere((SA{T}[0,0,-1]), T(0.5), Lambertian(SA{T}[0.7,0.3,0.3])))
232-
233-
# ground sphere
234-
push!(spheres, Sphere((SA{T}[0,-100.5,-1]), T(100), Lambertian(SA{T}[0.8,0.8,0.0])))
235-
HittableList(spheres)
236-
end
237-
238-
"""Scene with 2 Lambertian, 2 Metal spheres.
239-
240-
See https://raytracing.github.io/images/img-1.11-metal-shiny.png"""
241-
function scene_4_spheres(; elem_type::Type{T}) where T
242-
scene = scene_2_spheres(; elem_type=elem_type)
243-
244-
# left and right Metal spheres
245-
push!(scene, Sphere((SA{T}[-1,0,-1]), T(0.5), Metal((SA{T}[0.8,0.8,0.8]), T(0.3))))
246-
push!(scene, Sphere((SA{T}[ 1,0,-1]), T(0.5), Metal((SA{T}[0.8,0.6,0.2]), T(0.8))))
247-
return scene
248-
end
249-
250226
struct Camera{T <: AbstractFloat}
251227
origin::Vec3{T}
252228
lower_left_corner::Vec3{T}
@@ -408,65 +384,6 @@ end
408384
Scatter(Ray{T}(rec.p, dir), attenuation) # TODO: rename reflected -> !absorbed?
409385
end
410386

411-
@inline function scene_diel_spheres(left_radius=0.5; elem_type::Type{T}) where T # dielectric spheres
412-
spheres = Sphere[]
413-
414-
# small center sphere
415-
push!(spheres, Sphere((SA{T}[0,0,-1]), T(0.5), Lambertian(SA{T}[0.1,0.2,0.5])))
416-
417-
# ground sphere (planet?)
418-
push!(spheres, Sphere((SA{T}[0,-100.5,-1]), T(100), Lambertian(SA{T}[0.8,0.8,0.0])))
419-
420-
# # left and right spheres.
421-
# # Use a negative radius on the left sphere to create a "thin bubble"
422-
push!(spheres, Sphere((SA{T}[-1,0,-1]), T(left_radius), Dielectric(T(1.5))))
423-
push!(spheres, Sphere((SA{T}[1,0,-1]), T(0.5), Metal((SA{T}[0.8,0.6,0.2]), T(0))))
424-
HittableList(spheres)
425-
end
426-
427-
function scene_blue_red_spheres(; elem_type::Type{T}) where T # dielectric spheres
428-
spheres = Sphere[]
429-
R = cos(pi/4)
430-
push!(spheres, Sphere((SA{T}[-R,0,-1]), R, Lambertian(SA{T}[0,0,1])))
431-
push!(spheres, Sphere((SA{T}[ R,0,-1]), R, Lambertian(SA{T}[1,0,0])))
432-
HittableList(spheres)
433-
end
434-
435-
function scene_random_spheres(; elem_type::Type{T}) where T
436-
spheres = Sphere[]
437-
438-
# ground
439-
push!(spheres, Sphere((SA{T}[0,-1000,-1]), T(1000),
440-
Lambertian(SA{T}[0.5,0.5,0.5])))
441-
442-
for a in -11:10, b in -11:10
443-
choose_mat = trand(T)
444-
center = SA[a + T(0.9)*trand(T), T(0.2), b + T(0.9)*trand(T)]
445-
446-
# skip spheres too close?
447-
if norm(center - SA{T}[4,0.2,0]) < T(0.9) continue end
448-
449-
if choose_mat < T(0.8)
450-
# diffuse
451-
albedo = @SVector[trand(T) for i 1:3] .* @SVector[trand(T) for i 1:3]
452-
push!(spheres, Sphere(center, T(0.2), Lambertian(albedo)))
453-
elseif choose_mat < T(0.95)
454-
# metal
455-
albedo = @SVector[random_between(T(0.5),T(1.0)) for i 1:3]
456-
fuzz = random_between(T(0.0), T(5.0))
457-
push!(spheres, Sphere(center, T(0.2), Metal(albedo, fuzz)))
458-
else
459-
# glass
460-
push!(spheres, Sphere(center, T(0.2), Dielectric(T(1.5))))
461-
end
462-
end
463-
464-
push!(spheres, Sphere((SA{T}[0,1,0]), T(1), Dielectric(T(1.5))))
465-
push!(spheres, Sphere((SA{T}[-4,1,0]), T(1),
466-
Lambertian(SA{T}[0.4,0.2,0.1])))
467-
push!(spheres, Sphere((SA{T}[4,1,0]), T(1),
468-
Metal((SA{T}[0.7,0.6,0.5]), T(0))))
469-
HittableList(spheres)
470-
end
387+
include("scenes.jl")
471388

472389
end

src/proto/proto.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ ELEM_TYPE = Float64
1616

1717
t_default_cam = default_camera(SA{ELEM_TYPE}[0,0,0])
1818

19+
t_cam1 = default_camera([13,2,3], [0,0,0], [0,1,0], 20, 16/9, 0.1, 10.0; elem_type=ELEM_TYPE)
20+
21+
t_cam2 = default_camera([3,3,2], [0,0,-1], [0,1,0], 20, 16/9, 2.0, norm([3,3,2]-[0,0,-1]);
22+
elem_type=ELEM_TYPE)
23+
1924
# After some optimization:
2025
# 46.506 ms (917106 allocations: 16.33 MiB)
2126
# Using convert(Float32, ...) instead of MyFloat(...):
@@ -98,8 +103,6 @@ render(scene_2_spheres(; elem_type=ELEM_TYPE), t_default_cam, 96, 1) # 1 sample
98103

99104
#render(scene_blue_red_spheres(; elem_type=ELEM_TYPE), t_default_cam, 96, 16)
100105

101-
t_cam1 = default_camera([13,2,3], [0,0,0], [0,1,0], 20, 16/9, 0.1, 10.0; elem_type=ELEM_TYPE)
102-
103106
# took ~20s (really rough timing) in REPL, before optimization
104107
# after optimization:
105108
# 880.997 ms (48801164 allocations: 847.10 MiB)
@@ -192,7 +195,7 @@ t_cam1 = default_camera([13,2,3], [0,0,0], [0,1,0], 20, 16/9, 0.1, 10.0; elem_ty
192195
print("render(scene_random_spheres(; elem_type=ELEM_TYPE), t_cam1, 200, 32):")
193196
reseed!()
194197
_scene_random_spheres = scene_random_spheres(; elem_type=ELEM_TYPE)
195-
@btime render($_scene_random_spheres, $t_cam1, 200, 32)
198+
@benchmark render($_scene_random_spheres, $t_cam1, 200, 32)
196199

197200
# After some optimization, took ~5.6 hours:
198201
# 20171.646846 seconds (94.73 G allocations: 2.496 TiB, 1.06% gc time)
@@ -228,10 +231,6 @@ _scene_random_spheres = scene_random_spheres(; elem_type=ELEM_TYPE)
228231
#print("@time render(scene_random_spheres(; elem_type=ELEM_TYPE), t_cam1, 1920, 1000):")
229232
#@time render(scene_random_spheres(; elem_type=ELEM_TYPE), t_cam1, 1920, 1000)
230233

231-
232-
t_cam2 = default_camera([3,3,2], [0,0,-1], [0,1,0], 20, 16/9, 2.0, norm([3,3,2]-[0,0,-1]);
233-
elem_type=ELEM_TYPE)
234-
235234
# Before optimization:
236235
# 5.993 s (193097930 allocations: 11.92 GiB)
237236
# after disabling: `Base.getproperty(vec::SVector{3}, sym::Symbol)`

src/scenes.jl

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"Scene with 2 Lambertian spheres"
2+
function scene_2_spheres(; elem_type::Type{T}) where T
3+
spheres = Sphere[]
4+
5+
# small center sphere
6+
push!(spheres, Sphere((SA{T}[0,0,-1]), T(0.5), Lambertian(SA{T}[0.7,0.3,0.3])))
7+
8+
# ground sphere
9+
push!(spheres, Sphere((SA{T}[0,-100.5,-1]), T(100), Lambertian(SA{T}[0.8,0.8,0.0])))
10+
HittableList(spheres)
11+
end
12+
13+
"""Scene with 2 Lambertian, 2 Metal spheres.
14+
15+
See https://raytracing.github.io/images/img-1.11-metal-shiny.png"""
16+
function scene_4_spheres(; elem_type::Type{T}) where T
17+
scene = scene_2_spheres(; elem_type=elem_type)
18+
19+
# left and right Metal spheres
20+
push!(scene, Sphere((SA{T}[-1,0,-1]), T(0.5), Metal((SA{T}[0.8,0.8,0.8]), T(0.3))))
21+
push!(scene, Sphere((SA{T}[ 1,0,-1]), T(0.5), Metal((SA{T}[0.8,0.6,0.2]), T(0.8))))
22+
return scene
23+
end
24+
25+
@inline function scene_diel_spheres(left_radius=0.5; elem_type::Type{T}) where T # dielectric spheres
26+
spheres = Sphere[]
27+
28+
# small center sphere
29+
push!(spheres, Sphere((SA{T}[0,0,-1]), T(0.5), Lambertian(SA{T}[0.1,0.2,0.5])))
30+
31+
# ground sphere (planet?)
32+
push!(spheres, Sphere((SA{T}[0,-100.5,-1]), T(100), Lambertian(SA{T}[0.8,0.8,0.0])))
33+
34+
# # left and right spheres.
35+
# # Use a negative radius on the left sphere to create a "thin bubble"
36+
push!(spheres, Sphere((SA{T}[-1,0,-1]), T(left_radius), Dielectric(T(1.5))))
37+
push!(spheres, Sphere((SA{T}[1,0,-1]), T(0.5), Metal((SA{T}[0.8,0.6,0.2]), T(0))))
38+
HittableList(spheres)
39+
end
40+
41+
function scene_blue_red_spheres(; elem_type::Type{T}) where T # dielectric spheres
42+
spheres = Sphere[]
43+
R = cos(pi/4)
44+
push!(spheres, Sphere((SA{T}[-R,0,-1]), R, Lambertian(SA{T}[0,0,1])))
45+
push!(spheres, Sphere((SA{T}[ R,0,-1]), R, Lambertian(SA{T}[1,0,0])))
46+
HittableList(spheres)
47+
end
48+
49+
function scene_random_spheres(; elem_type::Type{T}) where T
50+
spheres = Sphere[]
51+
52+
# ground
53+
push!(spheres, Sphere((SA{T}[0,-1000,-1]), T(1000),
54+
Lambertian(SA{T}[0.5,0.5,0.5])))
55+
56+
for a in -11:10, b in -11:10
57+
choose_mat = trand(T)
58+
center = SA[a + T(0.9)*trand(T), T(0.2), b + T(0.9)*trand(T)]
59+
60+
# skip spheres too close?
61+
if norm(center - SA{T}[4,0.2,0]) < T(0.9) continue end
62+
63+
if choose_mat < T(0.8)
64+
# diffuse
65+
albedo = @SVector[trand(T) for i 1:3] .* @SVector[trand(T) for i 1:3]
66+
push!(spheres, Sphere(center, T(0.2), Lambertian(albedo)))
67+
elseif choose_mat < T(0.95)
68+
# metal
69+
albedo = @SVector[random_between(T(0.5),T(1.0)) for i 1:3]
70+
fuzz = random_between(T(0.0), T(5.0))
71+
push!(spheres, Sphere(center, T(0.2), Metal(albedo, fuzz)))
72+
else
73+
# glass
74+
push!(spheres, Sphere(center, T(0.2), Dielectric(T(1.5))))
75+
end
76+
end
77+
78+
push!(spheres, Sphere((SA{T}[0,1,0]), T(1), Dielectric(T(1.5))))
79+
push!(spheres, Sphere((SA{T}[-4,1,0]), T(1),
80+
Lambertian(SA{T}[0.4,0.2,0.1])))
81+
push!(spheres, Sphere((SA{T}[4,1,0]), T(1),
82+
Metal((SA{T}[0.7,0.6,0.5]), T(0))))
83+
HittableList(spheres)
84+
end

0 commit comments

Comments
 (0)