Skip to content

Commit 16e6c3e

Browse files
committed
clean up
1 parent 334c03d commit 16e6c3e

File tree

6 files changed

+43
-94
lines changed

6 files changed

+43
-94
lines changed

docs/code/TraceMakie.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ function to_trace_camera(scene::Makie.Scene, film)
138138
)
139139
end
140140

141-
function render_scene(mscene::Makie.Scene)
141+
function render_scene(scene::Makie.Scene)
142+
return render_scene(c -> Trace.WhittedIntegrator(c, Trace.UniformSampler(8), 5), scene)
143+
end
144+
145+
function render_scene(integrator_f, mscene::Makie.Scene)
142146
# Only set background image if it isn't set by env light, since
143147
# background image takes precedence
144148
resolution = Point2f(size(mscene))
@@ -164,7 +168,7 @@ function render_scene(mscene::Makie.Scene)
164168
error("Must have at least one light")
165169
end
166170
bvh = Trace.BVHAccel(map(identity, primitives), 1)
167-
integrator = Trace.WhittedIntegrator(camera, Trace.UniformSampler(8), 5)
171+
integrator = integrator_f(camera)
168172
scene = Trace.Scene([lights...], bvh)
169173
integrator(scene)
170174
return reverse(film.framebuffer, dims=1)
@@ -187,10 +191,9 @@ begin
187191
Trace.ConstantTexture(0.010408001f0),
188192
true,
189193
)
190-
scene = Scene(size=(1024, 1024); lights=[AmbientLight(RGBf(0.5, 0.5, 0.5)), PointLight(Vec3f(0, 1, 0.5), RGBf(1, 1, 1))])
194+
scene = Scene(size=(1024, 1024); lights=[AmbientLight(RGBf(0.7, 0.6, 0.6)), PointLight(Vec3f(0, 1, 0.5), RGBf(1.3, 1.3, 1.3))])
191195
cam3d!(scene)
192-
mesh!(scene, catmesh, color=load(Makie.assetpath("diffusemap.png")), material=plastic)
193-
mesh!(scene, Sphere(Point3f(0, 0, 2), 1f0), material=glass)
196+
mesh!(scene, catmesh, color=load(Makie.assetpath("diffusemap.png")))
194197
center!(scene)
195198
render_scene(scene)
196199
end
@@ -205,5 +208,7 @@ begin
205208
zs = [cos(x) * sin(y) for x in xs, y in ys]
206209
surface!(scene, xs, ys, zs)
207210
center!(scene)
208-
render_scene(scene)
211+
render_scene(scene) do cam
212+
213+
end
209214
end

docs/code/caustic_glass.jl

Lines changed: 19 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using GeometryBasics
2-
using LinearAlgebra
1+
using GeometryBasics, ImageShow
2+
using LinearAlgebra, Makie
33
using Trace, FileIO, MeshIO
4+
45
begin
56
glass = Trace.GlassMaterial(
67
Trace.ConstantTexture(Trace.RGBSpectrum(1f0)),
@@ -16,84 +17,21 @@ begin
1617
Trace.ConstantTexture(0.010408001f0),
1718
true,
1819
)
19-
20-
model = load(joinpath(@__DIR__, "..", "src", "assets", "models", "caustic-glass.ply"))
21-
22-
23-
triangles = Trace.create_triangle_mesh(
24-
model, Trace.ShapeCore(Trace.translate(Vec3f(5, -1.49, -100)), false),
25-
)
26-
27-
floor_triangles = Trace.create_triangle_mesh(
28-
Trace.ShapeCore(Trace.translate(Vec3f(-10, 0, -87)), false),
29-
UInt32[1, 2, 3, 1, 4, 3],
30-
[
31-
Point3f(0, 0, 0), Point3f(0, 0, -30),
32-
Point3f(30, 0, -30), Point3f(30, 0, 0),
33-
],
34-
[
35-
Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0),
36-
Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0),
37-
],
38-
)
39-
40-
primitives = Trace.GeometricPrimitive[]
41-
for t in triangles
42-
push!(primitives, Trace.GeometricPrimitive(t, glass))
20+
scene = Scene(size=(1024, 1024); lights=[
21+
AmbientLight(RGBf(1, 1, 1)),
22+
PointLight(Vec3f(4, 4, 10), RGBf(150, 150, 150)),
23+
PointLight(Vec3f(-3, 10, 2.5), RGBf(60, 60, 60)),
24+
PointLight(Vec3f(0, 3, 0.5), RGBf(40, 40, 40))
25+
])
26+
cam3d!(scene)
27+
cm = scene.camera_controls
28+
mesh!(scene, model, material=glass)
29+
mini, maxi = extrema(Rect3f(decompose(Point, model)))
30+
floorrect = Rect3f(Vec3f(-10, mini[2], -10), Vec3f(20, -1, 20))
31+
mesh!(scene, floorrect, material=plastic)
32+
center!(scene)
33+
update_cam!(scene, Vec3f(-1.6, 6.2, 0.2), Vec3f(-3.6, 2.5, 2.4), Vec3f(0, 1, 0))
34+
render_scene(scene) do camera
35+
Trace.SPPMIntegrator(camera, 0.075f0, 5, 100)
4336
end
44-
for t in floor_triangles
45-
push!(primitives, Trace.GeometricPrimitive(t, plastic))
46-
end
47-
48-
bvh = Trace.BVHAccel(map(identity, primitives), 1);
49-
50-
from, to = Point3f(0, 2, 0), Point3f(-5, 0, 5)
51-
cone_angle, cone_δ_angle = 30f0, 10f0
52-
dir = normalize(Vec3f(to - from))
53-
dir, du, dv = Trace.coordinate_system(dir)
54-
55-
dir_to_z = Trace.Transformation(transpose(Mat4f(
56-
du[1], du[2], du[3], 0f0,
57-
dv[1], dv[2], dv[3], 0f0,
58-
dir[1], dir[2], dir[3], 0f0,
59-
0f0, 0f0, 0f0, 1f0,
60-
)))
61-
light_to_world = (
62-
Trace.translate(Vec3f(4.5, 0, -101))
63-
* Trace.translate(Vec3f(from))
64-
* inv(dir_to_z)
65-
)
66-
67-
lights = [
68-
Trace.SpotLight(
69-
light_to_world, Trace.RGBSpectrum(100f0),
70-
cone_angle, cone_angle - cone_δ_angle,
71-
),
72-
Trace.AmbientLight(Trace.RGBSpectrum(0.5f0)),
73-
]
74-
75-
scene = Trace.Scene(lights, bvh)
76-
77-
n_samples = 8
78-
ray_depth = 5
79-
80-
look_point = Point3f(-3, 0, 0)
81-
screen = Trace.Bounds2(Point2f(-1f0), Point2f(1f0))
82-
filter = Trace.LanczosSincFilter(Point2f(1f0), 3f0)
83-
resolution = Point2f(1024)
84-
ir = Int64.(resolution)
85-
film = Trace.Film(
86-
resolution, Trace.Bounds2(Point2f(0), Point2f(1)),
87-
filter, 1f0, 1f0,
88-
"./scenes/caustics-sppm-$(ir[1])x$(ir[2]).png",
89-
)
90-
camera = Trace.PerspectiveCamera(
91-
Trace.look_at(Point3f(0, 4, 4), look_point, Vec3f(0, 1, 0)),
92-
screen, 0f0, 1f0, 0f0, 1f6, 90f0, film,
93-
)
94-
integrator = Trace.SPPMIntegrator(camera, 0.075f0, ray_depth, 1)
95-
@time integrator(scene)
96-
reverse(film.framebuffer, dims=1)
9737
end
98-
mean(decompose(Point, Rect3f(model)))
99-
Rect3f(model)

src/lights/point.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ struct PointLight{S<:Spectrum} <: Light
2424
end
2525
end
2626

27+
function PointLight(position, i::S) where S<:Spectrum
28+
PointLight(translate(Vec3f(position)), i)
29+
end
30+
2731
"""
2832
Compute radiance arriving at `ref.p` interaction point at `ref.time` time
2933
due to that light, assuming there are no occluding objects between them.

src/materials/bsdf.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function sample_f(
136136
Vec3f(0f0), RGBSpectrum(0f0), 0f0, BSDF_NONE,
137137
)
138138
component = min(
139-
max(1, Int64(ceil(u[1] * matching_components))),
139+
max(1, ceil(Int64, u[1] * matching_components)),
140140
matching_components,
141141
)
142142
# Get BxDF for chosen component.
@@ -155,7 +155,7 @@ function sample_f(
155155
end
156156
end
157157
end
158-
@real_assert bxdf nothing "n bxdfs $(b.n_bxdfs), component $component, count $count"
158+
@real_assert !isnothing(bxdf) "n bxdfs $(b.n_bxdfs), component $component, count $count"
159159
# Remap BxDF sample u to [0, 1)^2.
160160
u_remapped = Point2f(
161161
min(u[1] * matching_components - component, 1f0), u[2],
@@ -208,7 +208,7 @@ end
208208
function compute_pdf(
209209
b::BSDF, wo_world::Vec3f, wi_world::Vec3f, flags::UInt8,
210210
)::Float32
211-
b.n_bxdfs == 0 && return 0f0
211+
b.bxdfs.last == 0 && return 0f0
212212
wo = world_to_local(b, wo_world)
213213
wo[3] 0f0 && return 0f0
214214
wi = world_to_local(b, wi_world)

src/materials/material.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Base.Base.@propagate_inbounds function matte_material(m::UberMaterial, si::Surf
1414
# TODO perform bump mapping
1515
# Evaluate textures and create BSDF.
1616
r = clamp(m.Kd(si))
17-
is_black(r) && return BSDF()
17+
is_black(r) && return BSDF(si)
1818
σ = clamp(m.σ(si), 0f0, 90f0)
1919
lambertian = 0.0f0)
2020
return BSDF(si, LambertianReflection(lambertian, r), OrenNayar(!lambertian, r, σ))
@@ -28,7 +28,6 @@ end
2828

2929
Base.Base.@propagate_inbounds function mirror_material(m::UberMaterial, si::SurfaceInteraction, ::Bool, transport)
3030
r = clamp(m.Kr(si))
31-
is_black(r) && return BSDF()
3231
return BSDF(si, SpecularReflection(!is_black(r), r, FresnelNoOp()))
3332
end
3433

src/shapes/Shape.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ struct ShapeCore
55
transform_swaps_handedness::Bool
66

77
function ShapeCore(
8-
object_to_world::Transformation, reverse_orientation::Bool,
9-
)
8+
object_to_world::Transformation, reverse_orientation::Bool = false,
9+
)
1010
new(
1111
object_to_world, inv(object_to_world), reverse_orientation,
1212
swaps_handedness(object_to_world),
1313
)
1414
end
1515
end
1616

17+
ShapeCore(reverse::Bool=false) = ShapeCore(Transformation(), reverse)
18+
ShapeCore(offset::Vec3, reverse::Bool=false) = ShapeCore(translate(Vec3f(offset)), reverse)
19+
1720
function world_bound(s::AbstractShape)::Bounds3
1821
s.core.object_to_world(object_bound(s))
1922
end

0 commit comments

Comments
 (0)