Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 42 additions & 40 deletions books/RayTracingInOneWeekend.html
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@

Now let's update the camera class to define and use a new `camera::get_ray(i,j)` function, which
will generate different samples for each pixel. This function will use a new helper function
`pixel_sample_square()` that generates a random sample point within the unit square centered at the
`sample_square()` that generates a random sample point within the unit square centered at the
origin. We then transform the random sample from this ideal square back to the particular pixel
we're currently sampling.

Expand All @@ -2068,7 +2068,7 @@
ray r = get_ray(i, j);
pixel_color += ray_color(r, world);
}
write_color(std::cout, pixel_sample_scale * pixel_color);
write_color(std::cout, pixel_samples_scale * pixel_color);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
}
}
Expand All @@ -2077,11 +2077,11 @@
}
...
private:
int image_height; // Rendered image height
int image_height; // Rendered image height
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
double pixel_sample_scale; // Color scale factor for a pixel sample
double pixel_samples_scale; // Color scale factor for a sum of pixel samples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
point3 center; // Camera center
point3 center; // Camera center
...

void initialize() {
Expand All @@ -2090,7 +2090,7 @@


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
pixel_sample_scale = 1.0 / samples_per_pixel;
pixel_samples_scale = 1.0 / samples_per_pixel;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

center = point3(0, 0, 0);
Expand All @@ -2102,20 +2102,20 @@
ray get_ray(int i, int j) const {
// Get a randomly sampled camera ray for the pixel at location i,j.

auto pixel_center = pixel00_loc + (i * pixel_delta_u) + (j * pixel_delta_v);
auto pixel_sample = pixel_center + pixel_sample_square();
auto offset = sample_square();
auto pixel_sample = pixel00_loc
+ ((i + offset.x()) * pixel_delta_u)
+ ((j + offset.y()) * pixel_delta_v);

auto ray_origin = center;
auto ray_direction = pixel_sample - ray_origin;

return ray(ray_origin, ray_direction);
}

vec3 pixel_sample_square() const {
// Returns a random point in the square surrounding a pixel at the origin.
auto px = -0.5 + random_double();
auto py = -0.5 + random_double();
return (px * pixel_delta_u) + (py * pixel_delta_v);
vec3 sample_square() const {
// Returns the vector to a random point in the [-.5,-.5]-[+.5,+.5] unit square.
return vec3(random_double() - 0.5, random_double() - 0.5, 0);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

Expand All @@ -2128,10 +2128,10 @@

</div>

(In addition to the new `pixel_sample_square()` function above, you'll also find the function
`pixel_sample_disk()` in the Github source code. This is included in case you'd like to experiment
with non-square pixels, but we won't be using it in this book. `pixel_sample_disk()` depends on the
function `random_in_unit_disk()` which is defined later on.)
(In addition to the new `sample_square()` function above, you'll also find the function
`sample_disk()` in the Github source code. This is included in case you'd like to experiment with
non-square pixels, but we won't be using it in this book. `sample_disk()` depends on the function
`random_in_unit_disk()` which is defined later on.)

<div class='together'>
Main is updated to set the new camera parameter.
Expand Down Expand Up @@ -2398,7 +2398,7 @@
pixel_color += ray_color(r, max_depth, world);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
}
write_color(std::cout, pixel_sample_scale * pixel_color);
write_color(std::cout, pixel_samples_scale * pixel_color);
}
}

Expand Down Expand Up @@ -3572,7 +3572,7 @@
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

pixel_sample_scale = 1.0 / samples_per_pixel;
pixel_samples_scale = 1.0 / samples_per_pixel;

center = point3(0, 0, 0);

Expand Down Expand Up @@ -3698,21 +3698,21 @@
...

private:
int image_height; // Rendered image height
double pixel_sample_scale; // Color scale factor for a pixel sample
point3 center; // Camera center
point3 pixel00_loc; // Location of pixel 0, 0
vec3 pixel_delta_u; // Offset to pixel to the right
vec3 pixel_delta_v; // Offset to pixel below
int image_height; // Rendered image height
double pixel_samples_scale; // Color scale factor for a sum of pixel samples
point3 center; // Camera center
point3 pixel00_loc; // Location of pixel 0, 0
vec3 pixel_delta_u; // Offset to pixel to the right
vec3 pixel_delta_v; // Offset to pixel below
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
vec3 u, v, w; // Camera frame basis vectors
vec3 u, v, w; // Camera frame basis vectors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

void initialize() {
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

pixel_sample_scale = 1.0 / samples_per_pixel;
pixel_samples_scale = 1.0 / samples_per_pixel;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
Expand Down Expand Up @@ -3944,23 +3944,23 @@
...

private:
int image_height; // Rendered image height
double pixel_sample_scale; // Color scale factor for a pixel sample
point3 center; // Camera center
point3 pixel00_loc; // Location of pixel 0, 0
vec3 pixel_delta_u; // Offset to pixel to the right
vec3 pixel_delta_v; // Offset to pixel below
vec3 u, v, w; // Camera frame basis vectors
int image_height; // Rendered image height
double pixel_samples_scale; // Color scale factor for a sum of pixel samples
point3 center; // Camera center
point3 pixel00_loc; // Location of pixel 0, 0
vec3 pixel_delta_u; // Offset to pixel to the right
vec3 pixel_delta_v; // Offset to pixel below
vec3 u, v, w; // Camera frame basis vectors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
vec3 defocus_disk_u; // Defocus disk horizontal radius
vec3 defocus_disk_v; // Defocus disk vertical radius
vec3 defocus_disk_u; // Defocus disk horizontal radius
vec3 defocus_disk_v; // Defocus disk vertical radius
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

void initialize() {
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

pixel_sample_scale = 1.0 / samples_per_pixel;
pixel_samples_scale = 1.0 / samples_per_pixel;

center = lookfrom;

Expand Down Expand Up @@ -4009,8 +4009,10 @@
// the camera defocus disk.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

auto pixel_center = pixel00_loc + (i * pixel_delta_u) + (j * pixel_delta_v);
auto pixel_sample = pixel_center + pixel_sample_square();
auto offset = sample_square();
auto pixel_sample = pixel00_loc
+ ((i + offset.x()) * pixel_delta_u)
+ ((j + offset.y()) * pixel_delta_v);


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
Expand All @@ -4021,7 +4023,7 @@
return ray(ray_origin, ray_direction);
}

vec3 pixel_sample_square() const {
vec3 sample_square() const {
...
}

Expand Down
6 changes: 4 additions & 2 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@
// Get a randomly-sampled camera ray for the pixel at location i,j, originating from
// the camera defocus disk.

auto pixel_center = pixel00_loc + (i * pixel_delta_u) + (j * pixel_delta_v);
auto pixel_sample = pixel_center + pixel_sample_square();
auto offset = sample_square();
auto pixel_sample = pixel00_loc
+ ((i + offset.x()) * pixel_delta_u)
+ ((j + offset.y()) * pixel_delta_v);

auto ray_origin = (defocus_angle <= 0) ? center : defocus_disk_sample();
auto ray_direction = pixel_sample - ray_origin;
Expand Down
43 changes: 25 additions & 18 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -324,21 +324,21 @@
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
write_color(std::cout, pixel_sample_scale * pixel_color);
write_color(std::cout, pixel_samples_scale * pixel_color);
}
}

std::clog << "\rDone. \n";
}
...
private:
int image_height; // Rendered image height
double pixel_sample_scale; // Color scale factor for a pixel sample
int image_height; // Rendered image height
double pixel_samples_scale; // Color scale factor for a sum of pixel samples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
int sqrt_spp; // Square root of number of samples per pixel
double recip_sqrt_spp; // 1 / sqrt_spp
int sqrt_spp; // Square root of number of samples per pixel
double recip_sqrt_spp; // 1 / sqrt_spp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
point3 center; // Camera center
point3 center; // Camera center
...

void initialize() {
Expand All @@ -348,7 +348,7 @@

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
sqrt_spp = int(sqrt(samples_per_pixel));
pixel_sample_scale = 1.0 / (sqrt_spp * sqrt_spp);
pixel_samples_scale = 1.0 / (sqrt_spp * sqrt_spp);
recip_sqrt_spp = 1.0 / sqrt_spp;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

Expand All @@ -357,14 +357,15 @@
}
...
ray get_ray(int i, int j, int s_i, int s_j) const {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
// Get a randomly-sampled camera ray for the pixel at location i,j, originating from
// the camera defocus disk, and randomly sampled around the pixel location.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

auto pixel_center = pixel00_loc + (i * pixel_delta_u) + (j * pixel_delta_v);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
auto pixel_sample = pixel_center + pixel_sample_square(s_i, s_j);
auto offset = sample_square_stratified(s_i, s_j);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
auto pixel_sample = pixel00_loc
+ ((i + offset.x()) * pixel_delta_u)
+ ((j + offset.y()) * pixel_delta_v);

auto ray_origin = (defocus_angle <= 0) ? center : defocus_disk_sample();
auto ray_direction = pixel_sample - ray_origin;
Expand All @@ -375,13 +376,19 @@


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
vec3 pixel_sample_square(int s_i, int s_j) const {
// Returns a random point in the square surrounding a pixel at the origin, given
// the two subpixel indices.
auto px = -0.5 + recip_sqrt_spp * (s_i + random_double());
auto py = -0.5 + recip_sqrt_spp * (s_j + random_double());
vec3 sample_square_stratified(int s_i, int s_j) const {
// Returns the vector to a random point in the square sub-pixel specified by grid
// indices s_i and s_j, for an idealized unit square pixel [-.5,-.5] to [+.5,+.5].

auto px = ((s_i + random_double()) * recip_sqrt_spp) - 0.5;
auto py = ((s_j + random_double()) * recip_sqrt_spp) - 0.5;

return vec3(px, py, 0);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
return (px * pixel_delta_u) + (py * pixel_delta_v);

vec3 sample_square() const {
...
}

...
Expand Down Expand Up @@ -2836,7 +2843,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
}
}
write_color(std::cout, pixel_sample_scale * pixel_color);
write_color(std::cout, pixel_samples_scale * pixel_color);
}
}

Expand Down
41 changes: 20 additions & 21 deletions src/InOneWeekend/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,29 @@ class camera {
ray r = get_ray(i, j);
pixel_color += ray_color(r, max_depth, world);
}
write_color(std::cout, pixel_sample_scale * pixel_color);
write_color(std::cout, pixel_samples_scale * pixel_color);
}
}

std::clog << "\rDone. \n";
}

private:
int image_height; // Rendered image height
double pixel_sample_scale; // Color scale factor for a pixel sample
point3 center; // Camera center
point3 pixel00_loc; // Location of pixel 0, 0
vec3 pixel_delta_u; // Offset to pixel to the right
vec3 pixel_delta_v; // Offset to pixel below
vec3 u, v, w; // Camera frame basis vectors
vec3 defocus_disk_u; // Defocus disk horizontal radius
vec3 defocus_disk_v; // Defocus disk vertical radius
int image_height; // Rendered image height
double pixel_samples_scale; // Color scale factor for a sum of pixel samples
point3 center; // Camera center
point3 pixel00_loc; // Location of pixel 0, 0
vec3 pixel_delta_u; // Offset to pixel to the right
vec3 pixel_delta_v; // Offset to pixel below
vec3 u, v, w; // Camera frame basis vectors
vec3 defocus_disk_u; // Defocus disk horizontal radius
vec3 defocus_disk_v; // Defocus disk vertical radius

void initialize() {
image_height = int(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

pixel_sample_scale = 1.0 / samples_per_pixel;
pixel_samples_scale = 1.0 / samples_per_pixel;

center = lookfrom;

Expand Down Expand Up @@ -107,26 +107,25 @@ class camera {
// Get a randomly-sampled camera ray for the pixel at location i,j, originating from
// the camera defocus disk.

auto pixel_center = pixel00_loc + (i * pixel_delta_u) + (j * pixel_delta_v);
auto pixel_sample = pixel_center + pixel_sample_square();
auto offset = sample_square();
auto pixel_sample = pixel00_loc
+ ((i + offset.x()) * pixel_delta_u)
+ ((j + offset.y()) * pixel_delta_v);

auto ray_origin = (defocus_angle <= 0) ? center : defocus_disk_sample();
auto ray_direction = pixel_sample - ray_origin;

return ray(ray_origin, ray_direction);
}

vec3 pixel_sample_square() const {
vec3 sample_square() const {
// Returns a random point in the square surrounding a pixel at the origin.
auto px = -0.5 + random_double();
auto py = -0.5 + random_double();
return (px * pixel_delta_u) + (py * pixel_delta_v);
return vec3(random_double() - 0.5, random_double() - 0.5, 0);
}

vec3 pixel_sample_disk(double radius) const {
// Generate a sample from the disk of given radius around a pixel at the origin.
auto p = radius * random_in_unit_disk();
return (p[0] * pixel_delta_u) + (p[1] * pixel_delta_v);
vec3 sample_disk(double radius) const {
// Returns a random point in the unit (radius 0.5) disk centered at the origin.
return radius * random_in_unit_disk();
}

point3 defocus_disk_sample() const {
Expand Down
Loading