-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.jl
67 lines (55 loc) · 1.75 KB
/
main.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
using LinearAlgebra, Random
using Images, Term.Progress
include("camera.jl")
include("ray.jl")
include("materials.jl")
include("hittable.jl")
include("utils.jl")
include("scene.jl")
function main()
# image
aspect_ratio = 3 / 2
image_width = 200
image_height = floor(Int, image_width / aspect_ratio)
output = Matrix{RGB}(undef, image_height, image_width)
# ray parameters
n_samples = 100
max_depth = 50
# camera
lookfrom = [13.0, 2.0, 3.0]
lookat = [0.0, 0.0, 0.0]
vup = [0.0, 1.0, 0.0]
focus_dist = 10.0
aperture = 0.1
camera = Camera(lookfrom, lookat, vup, 20, aspect_ratio, aperture, focus_dist)
# world - list of hittable objects
world = random_scene()
# render
pb = ProgressBar()
job = addjob!(pb; N=image_width)
with(pb) do
Threads.@threads for i in 1:image_width
for j in 1:image_height
c = zeros(3)
# repeat to get average pixel colour
for _ in 1:n_samples
# relative position of pixel in image
u = (i - 1 + rand()) / (image_width - 1)
v = (j - 1 + rand()) / (image_height - 1)
# ray from camera origin to point in world coordinates
r = get_ray(camera, u, v)
# determine colour of ray
_c = RGB(ray_colour(world, r, max_depth)...)
c += [_c.r, _c.g, _c.b] / n_samples
end
# reverse index to plot in same direction
jj = reverse(1:image_height)[j]
output[jj, i] = RGB(sqrt.(c)...)
end
update!(job)
end
end
return output
end
img = @time main()
save("image.png", img)