Skip to content

Commit 2db9419

Browse files
committed
add tonemapping and scene
1 parent a82630e commit 2db9419

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

docs/code/basic-scene.jl

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using GeometryBasics
2+
using Trace
3+
using FileIO
4+
using ImageCore
5+
using BenchmarkTools
6+
using Makie, FileIO, ImageShow
7+
8+
9+
catmesh = load(Makie.assetpath("cat.obj"))
10+
img = load(Makie.assetpath("diffusemap.png"))
11+
m = normal_mesh(Tesselation(Sphere(Point3f(0), 1), 32))
12+
13+
function tmesh(prim, material)
14+
prim = prim isa Sphere ? Tesselation(prim, 64) : prim
15+
mesh = normal_mesh(prim)
16+
triangles = Trace.create_triangle_mesh(mesh, Trace.ShapeCore())
17+
return [Trace.GeometricPrimitive(t, material) for t in triangles]
18+
end
19+
20+
material_red = Trace.MatteMaterial(
21+
Trace.ConstantTexture(Trace.RGBSpectrum(0.796f0, 0.235f0, 0.2f0)),
22+
Trace.ConstantTexture(0.0f0),
23+
)
24+
material_blue = Trace.MatteMaterial(
25+
Trace.ConstantTexture(Trace.RGBSpectrum(0.251f0, 0.388f0, 0.847f0)),
26+
Trace.ConstantTexture(0.0f0),
27+
)
28+
material_white = Trace.MatteMaterial(
29+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
30+
Trace.ConstantTexture(0.0f0),
31+
)
32+
mirror = Trace.MirrorMaterial(Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)))
33+
glass = Trace.GlassMaterial(
34+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
35+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
36+
Trace.ConstantTexture(0.0f0),
37+
Trace.ConstantTexture(0.0f0),
38+
Trace.ConstantTexture(1.5f0),
39+
true,
40+
)
41+
42+
LowSphere(radius, contact=Point3f(0)) = Sphere(contact .+ Point3f(0, 0, radius), radius)
43+
44+
begin
45+
s1 = tmesh(LowSphere(0.5f0), material_white)
46+
s2 = tmesh(LowSphere(0.3f0, Point3f(0.5, 0.5, 0)), material_blue)
47+
s3 = tmesh(LowSphere(0.3f0, Point3f(-0.5, 0.5, 0)), mirror)
48+
s4 = tmesh(LowSphere(0.4f0, Point3f(0.0, 1.0, 0)), material_white)
49+
ground = tmesh(Rect3f(Vec3f(-5, -5, 0), Vec3f(10, 10, -0.1)), mirror)
50+
left = tmesh(Rect3f(Vec3f(-3, 0, 0), Vec3f(-0.1, 10, -0.1)), mirror)
51+
52+
bvh = Trace.BVHAccel([s1..., s2..., s3..., s4..., ground...], 1);
53+
54+
lights = [
55+
Trace.PointLight(Vec3f(0, 2.5, 4), Trace.RGBSpectrum(60.0f0)),
56+
Trace.PointLight(Vec3f(-5, 2, 0), Trace.RGBSpectrum(60.0f0)),
57+
]
58+
scene = Trace.Scene(lights, bvh);
59+
resolution = Point2f(1024)
60+
f = Trace.LanczosSincFilter(Point2f(1.0f0), 3.0f0)
61+
film = Trace.Film(resolution,
62+
Trace.Bounds2(Point2f(0.0f0), Point2f(1.0f0)),
63+
f, 1.0f0, 1.0f0,
64+
"shadows_sppm_res.png",
65+
)
66+
screen_window = Trace.Bounds2(Point2f(-1), Point2f(1))
67+
camera = Trace.PerspectiveCamera(
68+
Trace.look_at(Point3f(0, 3, 3), Point3f(0, 0, 0), Vec3f(0, 0, 1)),
69+
screen_window, 0.0f0, 1.0f0, 0.0f0, 1.0f6, 45.0f0, film,
70+
)
71+
integrator = Trace.WhittedIntegrator(camera, Trace.UniformSampler(8), 1)
72+
integrator(scene)
73+
img = reverse(film.framebuffer, dims=1)
74+
end
75+
x = Trace.scale(2, 2, 2)
76+
x.inv_m == inv(x.m)
77+
GLMakie.activate!(inline=true)
78+
# Computer projective camera transformations.
79+
resolution = Point2f(1024)
80+
resolution = Trace.scale(resolution[1], resolution[2], 1)
81+
82+
window_width = screen_window.p_max .- screen_window.p_min
83+
inv_bounds = Trace.scale((1.0f0 ./ window_width)..., 1)
84+
85+
offset = Trace.translate(Vec3f(
86+
-screen_window.p_min..., 0.0f0,
87+
))
88+
89+
ray = camera.core.raster_to_camera(Point3f(1024 / 2, 1024 / 2, 0))
90+
camera.core.screen_to_raster(camera.core.camera_to_screen(ray))
91+
92+
camera.core.screen_to_raster(Point3f(-1, -1, 0))
93+
camera.core.screen_to_raster(Point3f(1, 1, 0))
94+
95+
camera.core.raster_to_screen(Point3f(1024, 1024, 0))
96+
97+
camera.core.screen_to_raster(Point3f(-1, -1, 0))# == (0, 0, 0)
98+
camera.core.screen_to_raster(Point3f(1, 1, 0))# == (1024, 1024, 0)
99+
100+
camera.core.raster_to_screen(Point3f(0, 0, 0)) # == (0, 0, 0)
101+
camera.core.screen_to_raster(Point3f(1, 1, 0)) # == (1024, 1024, 0)
102+
103+
104+
res_t = Trace.scale(resolution..., 1)
105+
window_width = screen_window.p_max .- screen_window.p_min
106+
inv_bounds = Trace.scale((1f0 ./ window_width)..., 1)
107+
108+
offset = Trace.translate(Vec3f(
109+
(-screen_window.p_min)..., 0f0,
110+
))
111+
112+
inv(res_t.m)
113+
screen_to_raster = res_t * inv_bounds * offset
114+
115+
screen_to_raster(Point3f(1, 1, 0))
116+
117+
raster_to_screen = inv(offset) * inv(inv_bounds) * inv(res_t)
118+
119+
raster_to_screen(Point3f(1024, 1024, 0))

src/materials/tonemap.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Colors, Statistics
2+
3+
luminosity(c::RGB{T}) where {T} = (max(c.r, c.g, c.b) + min(c.r, c.g, c.b)) / 2.0
4+
5+
function lum_max(rgb_m)
6+
lum_max = 0.0
7+
for pix in rgb_m
8+
(lum_max > luminosity(pix)) || (lum_max = luminosity(pix))
9+
end
10+
lum_max
11+
end
12+
13+
function avg_lum(rgb_m, δ::Number=1e-10)
14+
cumsum = 0.0
15+
for pix in rgb_m
16+
cumsum += log10+ luminosity(pix))
17+
end
18+
return 10^(cumsum / (prod(size(rgb_m))))
19+
end
20+
21+
function normalize_image(
22+
rgb_m,
23+
a::Float64=0.18,
24+
lum::Union{Number,Nothing}=nothing,
25+
δ::Number=1e-10
26+
)
27+
28+
(isnothing(lum) || lum 0.0) && (lum = avg_lum(rgb_m, δ))
29+
return rgb_m .* a .* (1.0 / lum)
30+
end
31+
32+
function clamp_image(img::AbstractMatrix{T}) where {T}
33+
return map(img) do col
34+
return T(clamp(col.r, 0, 1), clamp(col.g, 0, 1), clamp(col.b, 0, 1))
35+
end
36+
end
37+
38+
function γ_correction(img::AbstractMatrix{T}, γ::Float64=1.0, k::Float64=1.0) where T
39+
return map(img) do c
40+
return T(
41+
floor(255 * c.r^(1 / γ)),
42+
floor(255 * c.g^(1 / γ)),
43+
floor(255 * c.b^(1 / γ))
44+
)
45+
end
46+
end
47+
48+
function tone_mapping(img;
49+
a::Float64=0.18,
50+
γ::Float64=1.0,
51+
lum::Union{Number,Nothing}=nothing
52+
)
53+
img = normalize_image(img, a, lum)
54+
img = clamp_image(img)
55+
return γ_correction(img, γ)
56+
end
57+
58+
tone_mapping(img)

0 commit comments

Comments
 (0)