@@ -59,44 +59,7 @@ function __init__()
59
59
nothing
60
60
end
61
61
62
- # Reset the per-thread random seeds to make results reproducible
63
- reseed! () = for i in 1 : Threads. nthreads () Random. seed! (TRNG[i], i) end
64
-
65
- " Per-thread rand()"
66
- @inline function trand ()
67
- @inbounds rng = TRNG[Threads. threadid ()]
68
- rand (rng)
69
- end
70
-
71
- @inline function trand (:: Type{T} ) where T
72
- @inbounds rng = TRNG[Threads. threadid ()]
73
- rand (rng, T)
74
- end
75
-
76
- @inline function random_vec3_in_sphere (:: Type{T} ) where T # equiv to random_in_unit_sphere()
77
- while true
78
- p = random_vec3 (T (- 1 ), T (1 ))
79
- if p⋅ p <= 1
80
- return p
81
- end
82
- end
83
- end
84
-
85
- @inline random_between (min:: T = 0 , max:: T = 1 ) where T = trand (T)* (max- min) + min # equiv to random_double()
86
- @inline random_vec3 (min:: T , max:: T ) where T = @inbounds @SVector [random_between (min, max) for i ∈ 1 : 3 ]
87
- @inline random_vec2 (min:: T , max:: T ) where T = @inbounds @SVector [random_between (min, max) for i ∈ 1 : 2 ]
88
-
89
- " Random unit vector. Equivalent to C++'s `unit_vector(random_in_unit_sphere())`"
90
- @inline random_vec3_on_sphere (:: Type{T} ) where T = normalize (random_vec3_in_sphere (T))
91
-
92
- @inline function random_vec2_in_disk (:: Type{T} ) where T # equiv to random_in_unit_disk()
93
- while true
94
- p = random_vec2 (T (- 1 ), T (1 ))
95
- if p⋅ p <= 1
96
- return p
97
- end
98
- end
99
- end
62
+ include (" rand.jl" )
100
63
101
64
" An object that can be hit by Ray"
102
65
abstract type Hittable end
223
186
Scatter (Ray (rec. p, reflected), mat. albedo)
224
187
end
225
188
226
- struct Camera{T <: AbstractFloat }
227
- origin:: Vec3{T}
228
- lower_left_corner:: Vec3{T}
229
- horizontal:: Vec3{T}
230
- vertical:: Vec3{T}
231
- u:: Vec3{T}
232
- v:: Vec3{T}
233
- w:: Vec3{T}
234
- lens_radius:: T
235
- end
236
-
237
- """
238
- Args:
239
- vfov: vertical field-of-view in degrees
240
- aspect_ratio: horizontal/vertical ratio of pixels
241
- aperture: if 0 - no depth-of-field
242
- """
243
- function default_camera (lookfrom:: Vec3{T} = (SA{T}[0 ,0 ,0 ]),
244
- lookat:: Vec3{T} = (SA{T}[0 ,0 ,- 1 ]),
245
- vup:: Vec3{T} = (SA{T}[0 ,1 ,0 ]),
246
- vfov:: T = T (90 ), aspect_ratio:: T = T (16 / 9 ),
247
- aperture:: T = T (0 ), focus_dist:: T = T (1 )) where T
248
- viewport_height = T (2 ) * tand (vfov/ T (2 ))
249
- viewport_width = aspect_ratio * viewport_height
250
-
251
- w = normalize (lookfrom - lookat)
252
- u = normalize (vup × w)
253
- v = w × u
254
-
255
- origin = lookfrom
256
- horizontal = focus_dist * viewport_width * u
257
- vertical = focus_dist * viewport_height * v
258
- lower_left_corner = origin - horizontal/ T (2 ) - vertical/ T (2 ) - focus_dist* w
259
- lens_radius = aperture/ T (2 )
260
- Camera {T} (origin, lower_left_corner, horizontal, vertical, u, v, w, lens_radius)
261
- end
262
-
263
- default_camera (lookfrom, lookat, vup, vfov, aspect_ratio, aperture, focus_dist; elem_type:: Type{T} ) where T =
264
- default_camera (Vec3 {T} (lookfrom), Vec3 {T} (lookat), Vec3 {T} (vup),
265
- T (vfov), T (aspect_ratio), T (aperture), T (focus_dist)
266
- )
267
-
268
- @inline @fastmath function get_ray (c:: Camera{T} , s:: T , t:: T ) where T
269
- rd = SVector {2,T} (c. lens_radius * random_vec2_in_disk (T))
270
- offset = c. u * rd. x + c. v * rd. y # offset = c.u * rd.x + c.v * rd.y
271
- Ray (c. origin + offset, normalize (c. lower_left_corner + s* c. horizontal +
272
- t* c. vertical - c. origin - offset))
273
- end
274
-
275
189
""" Compute color for a ray, recursively
276
190
277
191
Args:
302
216
end
303
217
end
304
218
219
+ include (" camera.jl" )
220
+
305
221
""" Render an image of `scene` using the specified camera, number of samples.
306
222
307
223
Args:
0 commit comments