-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path68_defocus_blur.jl
78 lines (65 loc) · 2.7 KB
/
68_defocus_blur.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Images, ImageView, BenchmarkTools
using RayTracingWeekend
import RayTracingWeekend.⋅ # Images has \cdot so specifying which one to use
using Random
Random.seed!(1234)
# Ray color with recursion depth
function ray_color(ray::Ray, world::Vector{<:Hittable}, depth::Int32)
# If we've exceeded the ray bounce limit, no more light is gathered.
if (depth <= 0)
return RGB{Float32}(0,0,0)
end
rec = get_hit_record(Metal(0.0f0, 0.0f0))
if hit(world, ray, Float32(0.001), typemax(Float32), rec)
result, attenuation, scattered = scatter(rec.material, ray, rec)
if result
r_color = ray_color(scattered, world, Int32(depth-1))
return RGB{Float32}(attenuation.r*r_color.r, attenuation.g*r_color.g, attenuation.b*r_color.b)
end
return RGB{Float32}(0,0,0)
end
unit_direction = unit_vector(ray.direction)
t = Float32(0.5*(unit_direction.y + 1.0))
return (1.0f0-t)*RGB{Float32}(1.0, 1.0, 1.0) + t*RGB{Float32}(0.5, 0.7, 1.0)
end
function render()
# Image
aspect_ratio = Float32(16/9)
image_width = Int32(400)
image_height = Int32(image_width / aspect_ratio)
samples_per_pixel = Int32(100)
max_depth = Int32(50)
# World
R = Float32(cos(pi/4)) # converting to float32, to avoid default Float64
world = Vector{Hittable}()
# Materials
material_ground = Lambertian(RGB{Float32}(0.8, 0.8, 0.0))
material_center = Lambertian(RGB{Float32}(0.1, 0.2, 0.5)) # set defaults to white color
material_left = Dielectric(RGB{Float32}(1.0, 1.0, 1.0), 1.5) # set defaults to white color
material_right = Metal(RGB{Float32}(0.8, 0.6, 0.2), Float32(0.0))
add!(world, Sphere2(Point{Float32}(0.0, -100.5, -1.0), 100.0, material_ground))
add!(world, Sphere2(Point{Float32}(0.0, 0.0, -1.0), 0.5, material_center))
add!(world, Sphere2(Point{Float32}(-1.0, 0.0, -1.0), 0.5, material_left))
add!(world, Sphere2(Point{Float32}(-1.0, 0.0, -1.0), -0.45, material_left))
add!(world, Sphere2(Point{Float32}(1.0, 0.0, -1.0), 0.5, material_right))
# Camera
cam = get_camera(Point{Float32}(3,3,2), Point{Float32}(0,0,-1), Vec{Float32}(0,1,0), 20.0f0, aspect_ratio, 2.0f0, len(Point{Float32}(3,3,3)))
img = rand(RGB{Float32}, image_height, image_width)
# col-major
for col in 1:image_width
for row in 1:image_height
pixel = RGB{Float32}(0, 0, 0)
for s in 1:samples_per_pixel
u = Float32((col + rand()) / image_width)
v = Float32(((image_height-row) + rand()) / image_height)
ray = get_ray(cam, u, v)
pixel += ray_color(ray, world, max_depth)
end
img[row, col] = get_sampled_color(pixel, samples_per_pixel)
end
end
img
end
print(@__FILE__)
@btime render() #
save("imgs/68_defocus_blur.png", render())