Skip to content

Commit 891e5ac

Browse files
committed
Better ordering of hit record in ray_color() func
Originally due to GitHub user @sshaplygin.
1 parent 52b6c0f commit 891e5ac

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,16 +2387,13 @@
23872387
...
23882388
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
23892389
color ray_color(const ray& r, int depth, const hittable& world) const {
2390-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2391-
hit_record rec;
2392-
2393-
2394-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
23952390
// If we've exceeded the ray bounce limit, no more light is gathered.
23962391
if (depth <= 0)
23972392
return color(0,0,0);
23982393
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
23992394

2395+
hit_record rec;
2396+
24002397
if (world.hit(r, interval(0, infinity), rec)) {
24012398
vec3 direction = random_on_hemisphere(rec.normal);
24022399
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2462,12 +2459,12 @@
24622459
private:
24632460
...
24642461
color ray_color(const ray& r, int depth, const hittable& world) const {
2465-
hit_record rec;
2466-
24672462
// If we've exceeded the ray bounce limit, no more light is gathered.
24682463
if (depth <= 0)
24692464
return color(0,0,0);
24702465

2466+
hit_record rec;
2467+
24712468

24722469
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
24732470
if (world.hit(r, interval(0.001, infinity), rec)) {
@@ -2533,12 +2530,12 @@
25332530
class camera {
25342531
...
25352532
color ray_color(const ray& r, int depth, const hittable& world) const {
2536-
hit_record rec;
2537-
25382533
// If we've exceeded the ray bounce limit, no more light is gathered.
25392534
if (depth <= 0)
25402535
return color(0,0,0);
25412536

2537+
hit_record rec;
2538+
25422539
if (world.hit(r, interval(0.001, infinity), rec)) {
25432540
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
25442541
vec3 direction = rec.normal + random_unit_vector();
@@ -2594,12 +2591,12 @@
25942591
class camera {
25952592
...
25962593
color ray_color(const ray& r, int depth, const hittable& world) const {
2597-
hit_record rec;
2598-
25992594
// If we've exceeded the ray bounce limit, no more light is gathered.
26002595
if (depth <= 0)
26012596
return color(0,0,0);
26022597

2598+
hit_record rec;
2599+
26032600
if (world.hit(r, interval(0.001, infinity), rec)) {
26042601
vec3 direction = rec.normal + random_unit_vector();
26052602
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2983,12 +2980,12 @@
29832980
private:
29842981
...
29852982
color ray_color(const ray& r, int depth, const hittable& world) const {
2986-
hit_record rec;
2987-
29882983
// If we've exceeded the ray bounce limit, no more light is gathered.
29892984
if (depth <= 0)
29902985
return color(0,0,0);
29912986

2987+
hit_record rec;
2988+
29922989
if (world.hit(r, interval(0.001, infinity), rec)) {
29932990
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
29942991
ray scattered;

books/RayTracingTheNextWeek.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,12 +3126,12 @@
31263126
private:
31273127
...
31283128
color ray_color(const ray& r, int depth, const hittable& world) const {
3129-
hit_record rec;
3130-
31313129
// If we've exceeded the ray bounce limit, no more light is gathered.
31323130
if (depth <= 0)
31333131
return color(0,0,0);
31343132

3133+
hit_record rec;
3134+
31353135

31363136
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
31373137
// If the ray hits nothing, return the background color.

books/RayTracingTheRestOfYourLife.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,12 +1677,12 @@
16771677
private:
16781678
...
16791679
color ray_color(const ray& r, int depth, const hittable& world) const {
1680-
hit_record rec;
1681-
16821680
// If we've exceeded the ray bounce limit, no more light is gathered.
16831681
if (depth <= 0)
16841682
return color(0,0,0);
16851683

1684+
hit_record rec;
1685+
16861686
// If the ray hits nothing, return the background color.
16871687
if (!world.hit(r, interval(0.001, infinity), rec))
16881688
return background;
@@ -2384,12 +2384,12 @@
23842384
...
23852385
private:
23862386
color ray_color(const ray& r, int depth, const hittable& world) const {
2387-
hit_record rec;
2388-
23892387
// If we've exceeded the ray bounce limit, no more light is gathered.
23902388
if (depth <= 0)
23912389
return color(0,0,0);
23922390

2391+
hit_record rec;
2392+
23932393
// If the ray hits nothing, return the background color.
23942394
if (!world.hit(r, interval(0.001, infinity), rec))
23952395
return background;
@@ -2609,12 +2609,12 @@
26092609
private:
26102610
...
26112611
color ray_color(const ray& r, int depth, const hittable& world) const {
2612-
hit_record rec;
2613-
26142612
// If we've exceeded the ray bounce limit, no more light is gathered.
26152613
if (depth <= 0)
26162614
return color(0,0,0);
26172615

2616+
hit_record rec;
2617+
26182618
// If the ray hits nothing, return the background color.
26192619
if (!world.hit(r, interval(0.001, infinity), rec))
26202620
return background;
@@ -3204,12 +3204,12 @@
32043204
...
32053205
color ray_color(const ray& r, int depth, const hittable& world, const hittable& lights)
32063206
const {
3207-
hit_record rec;
3208-
32093207
// If we've exceeded the ray bounce limit, no more light is gathered.
32103208
if (depth <= 0)
32113209
return color(0,0,0);
32123210

3211+
hit_record rec;
3212+
32133213
// If the ray hits nothing, return the background color.
32143214
if (!world.hit(r, interval(0.001, infinity), rec))
32153215
return background;

src/TheRestOfYourLife/camera.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ class camera {
144144

145145
color ray_color(const ray& r, int depth, const hittable& world, const hittable& lights)
146146
const {
147-
hit_record rec;
148-
149147
// If we've exceeded the ray bounce limit, no more light is gathered.
150148
if (depth <= 0)
151149
return color(0,0,0);
152150

151+
hit_record rec;
152+
153153
// If the ray hits nothing, return the background color.
154154
if (!world.hit(r, interval(0.001, infinity), rec))
155155
return background;

0 commit comments

Comments
 (0)