Skip to content

Commit b533377

Browse files
committed
Move random_in_unit_disk() from camera.h to vec3.h
In addition, I've updated the text around defocus blur.
1 parent 2ae3e61 commit b533377

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,31 +2164,43 @@
21642164

21652165
The reason we defocus blur in real cameras is because they need a big hole (rather than just a
21662166
pinhole) to gather light. This would defocus everything, but if we stick a lens in the hole, there
2167-
will be a certain distance where everything is in focus. The distance to that plane where things are
2168-
in focus is controlled by the distance between the lens and the film/sensor. That is why you see the
2169-
lens move relative to the camera when you change what is in focus (that may happen in your phone
2170-
camera too, but the sensor moves). The “aperture” is a hole to control how big the lens is
2171-
effectively. For a real camera, if you need more light you make the aperture bigger, and will get
2172-
more defocus blur. For our virtual camera, we can have a perfect sensor and never need more light,
2173-
so we only have an aperture when we want defocus blur.
2174-
2175-
A real camera has a compound lens that is complicated. For our code we could simulate the order:
2176-
sensor, then lens, then aperture, and figure out where to send the rays and flip the image once
2177-
computed (the image is projected upside down on the film). Graphics people usually use a thin lens
2178-
approximation.
2167+
will be a certain distance where everything is in focus. You can think of a lens this way: all light
2168+
rays coming _from_ a specific point at the focal distance -- and that hit the lens -- will be bent
2169+
back _to_ a single point on the image sensor.
2170+
2171+
In a physical camera, the distance to that plane where things are in focus is controlled by the
2172+
distance between the lens and the film/sensor. That is why you see the lens move relative to the
2173+
camera when you change what is in focus (that may happen in your phone camera too, but the sensor
2174+
moves). The “aperture” is a hole to control how big the lens is effectively. For a real camera, if
2175+
you need more light you make the aperture bigger, and will get more defocus blur. For our virtual
2176+
camera, we can have a perfect sensor and never need more light, so we only have an aperture when we
2177+
want defocus blur.
2178+
2179+
<div class="together">
2180+
A real camera has a complicated compound lens. For our code we could simulate the order: sensor,
2181+
then lens, then aperture, and figure out where to send the rays and flip the image once computed
2182+
(the image is projected upside down on the film). Graphics people usually use a thin lens
2183+
approximation:
21792184

21802185
![Figure [cam-lens]: Camera lens model](../images/fig.cam-lens.jpg)
21812186

2182-
We also don’t need to simulate any of the inside of the camera. For the purposes of rendering an
2183-
image outside the camera, that would be unnecessary complexity. Instead I usually start rays from
2184-
the surface of the lens, and send them toward a virtual film plane, by finding the projection of the
2187+
</div>
2188+
2189+
<div class="together">
2190+
We don’t need to simulate any of the inside of the camera. For the purposes of rendering an image
2191+
outside the camera, that would be unnecessary complexity. Instead, I usually start rays from the
2192+
surface of the lens, and send them toward a virtual film plane, by finding the projection of the
21852193
film on the plane that is in focus (at the distance `focus_dist`).
21862194

21872195
![Figure [cam-film-plane]: Camera focus plane](../images/fig.cam-film-plane.jpg)
21882196

2189-
<div class='together'>
2190-
For that we just need to have the ray origins be on a disk around `lookfrom` rather than from a
2191-
point:
2197+
</div>
2198+
2199+
Normally, all scene rays originate from the `lookfrom` point. In order to accomplish defocus blur,
2200+
generate random scene rays originating from inside a disk centered at the `lookfrom` point. The
2201+
larger the radius, the greater the defocus blur. You can think of our original camera as having a
2202+
defocus disk of radius zero (no blur at all), so all rays originated at the disk center
2203+
(`lookfrom`).
21922204

21932205
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
21942206
vec3 random_in_unit_disk() {
@@ -2198,7 +2210,10 @@
21982210
} while (dot(p,p) >= 1.0);
21992211
return p;
22002212
}
2213+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2214+
[Listing [rand-in-unit-disk]: <kbd>[vec3.h]</kbd> Generate random point inside unit disk]
22012215

2216+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
22022217
class camera {
22032218
public:
22042219
camera(
@@ -2253,7 +2268,6 @@
22532268
};
22542269
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22552270
[Listing [camera-dof]: <kbd>[camera.h]</kbd> Camera with adjustable depth-of-field (dof)]
2256-
</div>
22572271

22582272
<div class='together'>
22592273
Using a big aperture:

src/common/camera.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
#include "common/rtweekend.h"
1515

1616

17-
vec3 random_in_unit_disk() {
18-
vec3 p;
19-
do {
20-
p = 2.0*vec3(random_double(),random_double(),0) - vec3(1,1,0);
21-
} while (dot(p,p) >= 1.0);
22-
return p;
23-
}
24-
2517
class camera {
2618
public:
2719
camera(
@@ -52,7 +44,7 @@ class camera {
5244
}
5345

5446
ray get_ray(double s, double t) {
55-
vec3 rd = lens_radius*random_in_unit_disk();
47+
vec3 rd = lens_radius * random_in_unit_disk();
5648
vec3 offset = u * rd.x() + v * rd.y();
5749
auto time = time0 + random_double()*(time1-time0);
5850
return ray(

src/common/vec3.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,12 @@ inline vec3 unit_vector(vec3 v) {
123123
return v / v.length();
124124
}
125125

126+
vec3 random_in_unit_disk() {
127+
vec3 p;
128+
do {
129+
p = 2.0*vec3(random_double(),random_double(),0) - vec3(1,1,0);
130+
} while (dot(p,p) >= 1.0);
131+
return p;
132+
}
133+
126134
#endif

0 commit comments

Comments
 (0)