Skip to content

Commit 8bc2d63

Browse files
authored
Merge pull request #648 from RayTracing/fix-cam-break
Fix up world and cam setup in book 3 main()
2 parents f86517b + 0583472 commit 8bc2d63

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ Change Log -- Ray Tracing in One Weekend
4040
- Fix: Listing 63: Show reverted scene declarations
4141
- Fix: Listing 68: Show final scene render parameters with highlighting
4242
- New: Listing 50: Show the updated material definitions
43+
- New: Add new isotropic constructor taking color argument (#644)
4344

4445
### _The Next Week_
4546
- Fix: Listing 7: Show reverted viewing parameters from book 1 final scene
47+
- Change: Listing 10: Separate out world & camera definitions in main (#646)
48+
- New: Add new isotropic constructor taking color argument (#644)
4649

4750

4851
----------------------------------------------------------------------------------------------------

books/RayTracingTheRestOfYourLife.html

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,13 +751,16 @@
751751

752752
Returning to the Cornell Box
753753
-----------------------------
754-
<div class='together'>
755754
Let’s do a simple refactoring and temporarily remove all materials that aren’t Lambertian. We can
756755
use our Cornell Box scene again, and let’s generate the camera in the function that generates the
757-
model:
756+
model.
758757

759758
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
760-
hittable_list cornell_box(camera& cam, double aspect) {
759+
...
760+
color ray_color(
761+
...
762+
763+
hittable_list cornell_box() {
761764
hittable_list world;
762765

763766
auto red = make_shared<lambertian>(color(.65, .05, .05));
@@ -782,6 +785,30 @@
782785
box2 = make_shared<translate>(box2, vec3(130,0,65));
783786
world.add(box2);
784787

788+
return world;
789+
}
790+
791+
int main() {
792+
// Image
793+
794+
const auto aspect_ratio = 1.0 / 1.0;
795+
const int image_width = 600;
796+
const int image_height = static_cast<int>(image_width / aspect_ratio);
797+
const int samples_per_pixel = 100;
798+
const int max_depth = 50;
799+
800+
// World
801+
802+
auto lights = make_shared<hittable_list>();
803+
lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
804+
lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>()));
805+
806+
auto world = cornell_box();
807+
808+
color background(0,0,0);
809+
810+
// Camera
811+
785812
point3 lookfrom(278, 278, -800);
786813
point3 lookat(278, 278, 0);
787814
vec3 vup(0, 1, 0);
@@ -791,13 +818,17 @@
791818
auto t0 = 0.0;
792819
auto t1 = 1.0;
793820

794-
cam = camera(lookfrom, lookat, vup, vfov, aspect, aperture, dist_to_focus, t0, t1);
821+
camera cam(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, t0, t1);
795822

796-
return world;
823+
// Render
824+
825+
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
826+
827+
for (int j = image_height-1; j >= 0; --j) {
828+
...
797829
}
798830
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
799831
[Listing [cornell-box]: <kbd>[main.cc]</kbd> Cornell box, refactored]
800-
</div>
801832

802833
<div class='together'>
803834
At 500×500 my code produces this image in 10min on 1 core of my Macbook:

src/TheRestOfYourLife/main.cc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ color ray_color(
6262
}
6363

6464

65-
hittable_list cornell_box(camera& cam, double aspect) {
65+
hittable_list cornell_box() {
6666
hittable_list world;
6767

6868
auto red = make_shared<lambertian>(color(.65, .05, .05));
@@ -85,23 +85,11 @@ hittable_list cornell_box(camera& cam, double aspect) {
8585
auto glass = make_shared<dielectric>(1.5);
8686
world.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
8787

88-
point3 lookfrom(278, 278, -800);
89-
point3 lookat(278, 278, 0);
90-
vec3 up(0, 1, 0);
91-
auto dist_to_focus = 10.0;
92-
auto aperture = 0.0;
93-
auto vfov = 40.0;
94-
auto t0 = 0.0;
95-
auto t1 = 1.0;
96-
97-
cam = camera(lookfrom, lookat, up, vfov, aspect, aperture, dist_to_focus, t0, t1);
98-
9988
return world;
10089
}
10190

10291

10392
int main() {
104-
10593
// Image
10694

10795
const auto aspect_ratio = 1.0 / 1.0;
@@ -116,14 +104,23 @@ int main() {
116104
lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
117105
lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>()));
118106

119-
auto world = cornell_box(cam, aspect_ratio);
120-
121-
// Camera
107+
auto world = cornell_box();
122108

123109
color background(0,0,0);
124110

125-
camera cam;
111+
// Camera
112+
113+
point3 lookfrom(278, 278, -800);
114+
point3 lookat(278, 278, 0);
115+
vec3 vup(0, 1, 0);
116+
auto dist_to_focus = 10.0;
117+
auto aperture = 0.0;
118+
auto vfov = 40.0;
119+
auto t0 = 0.0;
120+
auto t1 = 1.0;
126121

122+
camera cam(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, t0, t1);
123+
127124
// Render
128125

129126
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";

0 commit comments

Comments
 (0)