Skip to content

Commit 27bdccb

Browse files
committed
Rewrote src to use new function
1 parent ffd889e commit 27bdccb

File tree

9 files changed

+20
-178
lines changed

9 files changed

+20
-178
lines changed

src/InOneWeekend/sphere.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
4040
rec.t = temp;
4141
rec.p = r.at(rec.t);
4242
vec3 outward_normal = (rec.p - center) / radius;
43-
if (dot(r.direction(), outward_normal) > 0.0) {
44-
// ray is inside the sphere
45-
rec.normal = -outward_normal;
46-
rec.front_face = false;
47-
}
48-
else {
49-
// ray is outside the sphere
50-
rec.normal = outward_normal;
51-
rec.front_face = true;
52-
}
43+
rec.set_face_normal(r, outward_normal);
5344
rec.mat_ptr = mat_ptr;
5445
return true;
5546
}
@@ -58,16 +49,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
5849
rec.t = temp;
5950
rec.p = r.at(rec.t);
6051
vec3 outward_normal = (rec.p - center) / radius;
61-
if (dot(r.direction(), outward_normal) > 0.0) {
62-
// ray is inside the sphere
63-
rec.normal = -outward_normal;
64-
rec.front_face = false;
65-
}
66-
else {
67-
// ray is outside the sphere
68-
rec.normal = outward_normal;
69-
rec.front_face = true;
70-
}
52+
rec.set_face_normal(r, outward_normal);
7153
rec.mat_ptr = mat_ptr;
7254
return true;
7355
}

src/TheNextWeek/aarect.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,7 @@ bool xy_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
8181
rec.v = (y-y0)/(y1-y0);
8282
rec.t = t;
8383
vec3 outward_normal = vec3(0, 0, 1);
84-
if (dot(r.direction(), outward_normal) > 0.0) {
85-
rec.normal = -outward_normal;
86-
rec.front_face = false;
87-
}
88-
else {
89-
rec.normal = outward_normal;
90-
rec.front_face = true;
91-
}
84+
rec.set_face_normal(r, outward_normal);
9285
rec.mat_ptr = mp;
9386
rec.p = r.at(t);
9487
return true;
@@ -106,14 +99,7 @@ bool xz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
10699
rec.v = (z-z0)/(z1-z0);
107100
rec.t = t;
108101
vec3 outward_normal = vec3(0, 1, 0);
109-
if (dot(r.direction(), outward_normal) > 0.0) {
110-
rec.normal = -outward_normal;
111-
rec.front_face = false;
112-
}
113-
else {
114-
rec.normal = outward_normal;
115-
rec.front_face = true;
116-
}
102+
rec.set_face_normal(r, outward_normal);
117103
rec.mat_ptr = mp;
118104
rec.p = r.at(t);
119105
return true;
@@ -131,14 +117,7 @@ bool yz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
131117
rec.v = (z-z0)/(z1-z0);
132118
rec.t = t;
133119
vec3 outward_normal = vec3(1, 0, 0);
134-
if (dot(r.direction(), outward_normal) > 0.0) {
135-
rec.normal = -outward_normal;
136-
rec.front_face = false;
137-
}
138-
else {
139-
rec.normal = outward_normal;
140-
rec.front_face = true;
141-
}
120+
rec.set_face_normal(r, outward_normal);
142121
rec.mat_ptr = mp;
143122
rec.p = r.at(t);
144123
return true;

src/TheNextWeek/hittable.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,7 @@ bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) c
7676
ray moved_r(r.origin() - offset, r.direction(), r.time());
7777
if (ptr->hit(moved_r, t_min, t_max, rec)) {
7878
rec.p += offset;
79-
if (dot(moved_r.direction(), rec.normal) > 0.0) {
80-
rec.normal = -rec.normal;
81-
rec.front_face = false;
82-
}
83-
else {
84-
rec.front_face = true;
85-
}
79+
rec.set_face_normal(moved_r, rec.normal);
8680
return true;
8781
}
8882
else
@@ -159,14 +153,7 @@ bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) co
159153
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
160154
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
161155
rec.p = p;
162-
if (dot(rotated_r.direction(), normal) > 0.0) {
163-
rec.normal = -normal;
164-
rec.front_face = false;
165-
}
166-
else {
167-
rec.normal = normal;
168-
rec.front_face = true;
169-
}
156+
rec.set_face_normal(rotated_r, normal);
170157
return true;
171158
}
172159
else

src/TheNextWeek/moving_sphere.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,7 @@ bool moving_sphere::hit(const ray& r, double t_min, double t_max, hit_record& re
5959
rec.t = temp;
6060
rec.p = r.at(rec.t);
6161
vec3 outward_normal = (rec.p - center(r.time())) / radius;
62-
if (dot(r.direction(), outward_normal) > 0.0) {
63-
// ray is inside the sphere
64-
rec.normal = -outward_normal;
65-
rec.front_face = false;
66-
}
67-
else {
68-
// ray is outside the sphere
69-
rec.normal = outward_normal;
70-
rec.front_face = true;
71-
}
62+
rec.set_face_normal(r, outward_normal);
7263
rec.mat_ptr = mat_ptr;
7364
return true;
7465
}
@@ -77,16 +68,7 @@ bool moving_sphere::hit(const ray& r, double t_min, double t_max, hit_record& re
7768
rec.t = temp;
7869
rec.p = r.at(rec.t);
7970
vec3 outward_normal = (rec.p - center(r.time())) / radius;
80-
if (dot(r.direction(), outward_normal) > 0.0) {
81-
// ray is inside the sphere
82-
rec.normal = -outward_normal;
83-
rec.front_face = false;
84-
}
85-
else {
86-
// ray is outside the sphere
87-
rec.normal = outward_normal;
88-
rec.front_face = true;
89-
}
71+
rec.set_face_normal(r, outward_normal);
9072
rec.mat_ptr = mat_ptr;
9173
return true;
9274
}

src/TheNextWeek/sphere.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
5050
rec.t = temp;
5151
rec.p = r.at(rec.t);
5252
vec3 outward_normal = (rec.p - center) / radius;
53-
if (dot(r.direction(), outward_normal) > 0.0) {
54-
// ray is inside the sphere
55-
rec.normal = -outward_normal;
56-
rec.front_face = false;
57-
}
58-
else {
59-
// ray is outside the sphere
60-
rec.normal = outward_normal;
61-
rec.front_face = true;
62-
}
53+
rec.set_face_normal(r, outward_normal);
6354
get_sphere_uv((rec.p-center)/radius, rec.u, rec.v);
6455
rec.mat_ptr = mat_ptr;
6556
return true;
@@ -70,16 +61,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
7061
rec.t = temp;
7162
rec.p = r.at(rec.t);
7263
vec3 outward_normal = (rec.p - center) / radius;
73-
if (dot(r.direction(), outward_normal) > 0.0) {
74-
// ray is inside the sphere
75-
rec.normal = -outward_normal;
76-
rec.front_face = false;
77-
}
78-
else {
79-
// ray is outside the sphere
80-
rec.normal = outward_normal;
81-
rec.front_face = true;
82-
}
64+
rec.set_face_normal(r, outward_normal);
8365
get_sphere_uv((rec.p-center)/radius, rec.u, rec.v);
8466
rec.mat_ptr = mat_ptr;
8567
return true;

src/TheRestOfYourLife/aarect.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,7 @@ bool xy_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
9898
rec.v = (y-y0)/(y1-y0);
9999
rec.t = t;
100100
vec3 outward_normal = vec3(0, 0, 1);
101-
if (dot(r.direction(), outward_normal) > 0.0) {
102-
rec.normal = -outward_normal;
103-
rec.front_face = false;
104-
}
105-
else {
106-
rec.normal = outward_normal;
107-
rec.front_face = true;
108-
}
101+
rec.set_face_normal(r, outward_normal);
109102
rec.mat_ptr = mp;
110103
rec.p = r.at(t);
111104
return true;
@@ -123,14 +116,7 @@ bool xz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
123116
rec.v = (z-z0)/(z1-z0);
124117
rec.t = t;
125118
vec3 outward_normal = vec3(0, 1, 0);
126-
if (dot(r.direction(), outward_normal) > 0.0) {
127-
rec.normal = -outward_normal;
128-
rec.front_face = false;
129-
}
130-
else {
131-
rec.normal = outward_normal;
132-
rec.front_face = true;
133-
}
119+
rec.set_face_normal(r, outward_normal);
134120
rec.mat_ptr = mp;
135121
rec.p = r.at(t);
136122
return true;
@@ -148,14 +134,7 @@ bool yz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
148134
rec.v = (z-z0)/(z1-z0);
149135
rec.t = t;
150136
vec3 outward_normal = vec3(1, 0, 0);
151-
if (dot(r.direction(), outward_normal) > 0.0) {
152-
rec.normal = -outward_normal;
153-
rec.front_face = false;
154-
}
155-
else {
156-
rec.normal = outward_normal;
157-
rec.front_face = true;
158-
}
137+
rec.set_face_normal(r, outward_normal);
159138
rec.mat_ptr = mp;
160139
rec.p = r.at(t);
161140
return true;

src/TheRestOfYourLife/hittable.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,7 @@ bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) c
7878
ray moved_r(r.origin() - offset, r.direction(), r.time());
7979
if (ptr->hit(moved_r, t_min, t_max, rec)) {
8080
rec.p += offset;
81-
if (dot(moved_r.direction(), rec.normal) > 0.0) {
82-
rec.normal = -rec.normal;
83-
rec.front_face = false;
84-
}
85-
else {
86-
rec.front_face = true;
87-
}
81+
rec.set_face_normal(moved_r, rec.normal);
8882
return true;
8983
}
9084
else
@@ -161,14 +155,7 @@ bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) co
161155
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
162156
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
163157
rec.p = p;
164-
if (dot(rotated_r.direction(), normal) > 0.0) {
165-
rec.normal = -normal;
166-
rec.front_face = false;
167-
}
168-
else {
169-
rec.normal = normal;
170-
rec.front_face = true;
171-
}
158+
rec.set_face_normal(rotated_r, normal);
172159
return true;
173160
}
174161
else

src/TheRestOfYourLife/moving_sphere.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,7 @@ bool moving_sphere::hit(const ray& r, double t_min, double t_max, hit_record& re
5959
rec.t = temp;
6060
rec.p = r.at(rec.t);
6161
vec3 outward_normal = (rec.p - center(r.time())) / radius;
62-
if (dot(r.direction(), outward_normal) > 0.0) {
63-
// ray is inside the sphere
64-
rec.normal = -outward_normal;
65-
rec.front_face = false;
66-
}
67-
else {
68-
// ray is outside the sphere
69-
rec.normal = outward_normal;
70-
rec.front_face = true;
71-
}
62+
rec.set_face_normal(r, outward_normal);
7263
rec.mat_ptr = mat_ptr;
7364
return true;
7465
}
@@ -77,16 +68,7 @@ bool moving_sphere::hit(const ray& r, double t_min, double t_max, hit_record& re
7768
rec.t = temp;
7869
rec.p = r.at(rec.t);
7970
vec3 outward_normal = (rec.p - center(r.time())) / radius;
80-
if (dot(r.direction(), outward_normal) > 0.0) {
81-
// ray is inside the sphere
82-
rec.normal = -outward_normal;
83-
rec.front_face = false;
84-
}
85-
else {
86-
// ray is outside the sphere
87-
rec.normal = outward_normal;
88-
rec.front_face = true;
89-
}
71+
rec.set_face_normal(r, outward_normal);
9072
rec.mat_ptr = mat_ptr;
9173
return true;
9274
}

src/TheRestOfYourLife/sphere.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
7272
rec.t = temp;
7373
rec.p = r.at(rec.t);
7474
vec3 outward_normal = (rec.p - center) / radius;
75-
if (dot(r.direction(), outward_normal) > 0.0) {
76-
// ray is inside the sphere
77-
rec.normal = -outward_normal;
78-
rec.front_face = false;
79-
}
80-
else {
81-
// ray is outside the sphere
82-
rec.normal = outward_normal;
83-
rec.front_face = true;
84-
}
75+
rec.set_face_normal(r, outward_normal);
8576
get_sphere_uv((rec.p-center)/radius, rec.u, rec.v);
8677
rec.mat_ptr = mat_ptr;
8778
return true;
@@ -92,16 +83,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
9283
rec.t = temp;
9384
rec.p = r.at(rec.t);
9485
vec3 outward_normal = (rec.p - center) / radius;
95-
if (dot(r.direction(), outward_normal) > 0.0) {
96-
// ray is inside the sphere
97-
rec.normal = -outward_normal;
98-
rec.front_face = false;
99-
}
100-
else {
101-
// ray is outside the sphere
102-
rec.normal = outward_normal;
103-
rec.front_face = true;
104-
}
86+
rec.set_face_normal(r, outward_normal);
10587
get_sphere_uv((rec.p-center)/radius, rec.u, rec.v);
10688
rec.mat_ptr = mat_ptr;
10789
return true;

0 commit comments

Comments
 (0)