Skip to content

Commit 4a32299

Browse files
committed
Rewrote the books to use new function. There was also a mistake in the book because hadn't been correctly updated to
1 parent 27bdccb commit 4a32299

File tree

2 files changed

+24
-101
lines changed

2 files changed

+24
-101
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,11 @@
858858
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
859859
double t;
860860
bool front_face;
861+
862+
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
863+
front_face = dot(r.direction(), outward_normal) < 0;
864+
normal = front_face ? outward_normal :-outward_normal;
865+
}
861866
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
862867
};
863868

@@ -888,16 +893,7 @@
888893
rec.p = r.at(rec.t);
889894
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
890895
vec3 outward_normal = (rec.p - center) / radius;
891-
if (dot(r.direction(), outward_normal) > 0.0) {
892-
// ray is inside the sphere
893-
rec.normal = -outward_normal;
894-
rec.front_face = false;
895-
}
896-
else {
897-
// ray is outside the sphere
898-
rec.normal = outward_normal;
899-
rec.front_face = true;
900-
}
896+
rec.set_face_normal(r, outward_normal);
901897
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
902898
return true;
903899
}
@@ -907,16 +903,7 @@
907903
rec.p = r.at(rec.t);
908904
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
909905
vec3 outward_normal = (rec.p - center) / radius;
910-
if (dot(r.direction(), outward_normal) > 0.0) {
911-
// ray is inside the sphere
912-
rec.normal = -outward_normal;
913-
rec.front_face = false;
914-
}
915-
else {
916-
// ray is outside the sphere
917-
rec.normal = outward_normal;
918-
rec.front_face = true;
919-
}
906+
rec.set_face_normal(r, outward_normal);
920907
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
921908
return true;
922909
}
@@ -1675,6 +1662,12 @@
16751662
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16761663
double t;
16771664
bool front_face;
1665+
1666+
1667+
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
1668+
front_face = dot(r.direction(), outward_normal) < 0;
1669+
normal = front_face ? outward_normal :-outward_normal;
1670+
}
16781671
};
16791672

16801673
class hittable {
@@ -1724,16 +1717,7 @@
17241717
rec.t = temp;
17251718
rec.p = r.at(rec.t);
17261719
vec3 outward_normal = (rec.p - center) / radius;
1727-
if (dot(r.direction(), outward_normal) > 0.0) {
1728-
// ray is inside the sphere
1729-
rec.normal = -outward_normal;
1730-
rec.front_face = false;
1731-
}
1732-
else {
1733-
// ray is outside the sphere
1734-
rec.normal = outward_normal;
1735-
rec.front_face = true;
1736-
}
1720+
rec.set_face_normal(r, outward_normal);
17371721
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17381722
rec.mat_ptr = mat_ptr;
17391723
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1743,17 +1727,8 @@
17431727
if (temp < t_max && temp > t_min) {
17441728
rec.t = temp;
17451729
rec.p = r.at(rec.t);
1746-
vec3 outward_normal = (rec.p - center) / radius;
1747-
if (dot(r.direction(), outward_normal) > 0.0) {
1748-
// ray is inside the sphere
1749-
rec.normal = -outward_normal;
1750-
rec.front_face = false;
1751-
}
1752-
else {
1753-
// ray is outside the sphere
1754-
rec.normal = outward_normal;
1755-
rec.front_face = true;
1756-
}
1730+
vec3 outward_normal = (rec.p - center) / radius;
1731+
rec.set_face_normal(r, outward_normal);
17571732
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17581733
rec.mat_ptr = mat_ptr;
17591734
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

books/RayTracingTheNextWeek.html

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,7 @@
211211
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
212212
vec3 outward_normal = (rec.p - center(r.time())) / radius;
213213
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
214-
if (dot(r.direction(), outward_normal) > 0.0) {
215-
// ray is inside the sphere
216-
rec.normal = -outward_normal;
217-
rec.front_face = false;
218-
}
219-
else {
220-
// ray is outside the sphere
221-
rec.normal = outward_normal;
222-
rec.front_face = true;
223-
}
214+
rec.set_face_normal(r, outward_normal);
224215
rec.mat_ptr = mat_ptr;
225216
return true;
226217
}
@@ -231,16 +222,7 @@
231222
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
232223
vec3 outward_normal = (rec.p - center(r.time())) / radius;
233224
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
234-
if (dot(r.direction(), outward_normal) > 0.0) {
235-
// ray is inside the sphere
236-
rec.normal = -outward_normal;
237-
rec.front_face = false;
238-
}
239-
else {
240-
// ray is outside the sphere
241-
rec.normal = outward_normal;
242-
rec.front_face = true;
243-
}
225+
rec.set_face_normal(r, outward_normal);
244226
rec.mat_ptr = mat_ptr;
245227
return true;
246228
}
@@ -1744,14 +1726,7 @@
17441726
rec.v = (y-y0)/(y1-y0);
17451727
rec.t = t;
17461728
vec3 outward_normal = vec3(0, 0, 1);
1747-
if (dot(r.direction(), outward_normal) > 0.0) {
1748-
rec.normal = -outward_normal;
1749-
rec.front_face = false;
1750-
}
1751-
else {
1752-
rec.normal = outward_normal;
1753-
rec.front_face = true;
1754-
}
1729+
rec.set_face_normal(r, outward_normal);
17551730
rec.mat_ptr = mp;
17561731
rec.p = r.at(t);
17571732
return true;
@@ -1858,14 +1833,7 @@
18581833
rec.v = (z-z0)/(z1-z0);
18591834
rec.t = t;
18601835
vec3 outward_normal = vec3(0, 1, 0);
1861-
if (dot(r.direction(), outward_normal) > 0.0) {
1862-
rec.normal = -outward_normal;
1863-
rec.front_face = false;
1864-
}
1865-
else {
1866-
rec.normal = outward_normal;
1867-
rec.front_face = true;
1868-
}
1836+
rec.set_face_normal(r, outward_normal);
18691837
rec.mat_ptr = mp;
18701838
rec.p = r.at(t);
18711839
return true;
@@ -1883,14 +1851,7 @@
18831851
rec.v = (z-z0)/(z1-z0);
18841852
rec.t = t;
18851853
vec3 outward_normal = vec3(1, 0, 0);
1886-
if (dot(r.direction(), outward_normal) > 0.0) {
1887-
rec.normal = -outward_normal;
1888-
rec.front_face = false;
1889-
}
1890-
else {
1891-
rec.normal = outward_normal;
1892-
rec.front_face = true;
1893-
}
1854+
rec.set_face_normal(r, outward_normal);
18941855
rec.mat_ptr = mp;
18951856
rec.p = r.at(t);
18961857
return true;
@@ -1961,7 +1922,7 @@
19611922

19621923
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
19631924
if (ptr->hit(r, t_min, t_max, rec)) {
1964-
rec.normal = -rec.normal;
1925+
rec.front_face = !rec.front_face;
19651926
return true;
19661927
}
19671928
else
@@ -2113,13 +2074,7 @@
21132074
ray moved_r(r.origin() - offset, r.direction(), r.time());
21142075
if (ptr->hit(moved_r, t_min, t_max, rec)) {
21152076
rec.p += offset;
2116-
if (dot(moved_r.direction(), rec.normal) > 0.0) {
2117-
rec.normal = -rec.normal;
2118-
rec.front_face = false;
2119-
}
2120-
else {
2121-
rec.front_face = true;
2122-
}
2077+
rec.set_face_normal(moved_r, rec.normal);
21232078
return true;
21242079
}
21252080
else
@@ -2259,14 +2214,7 @@
22592214
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
22602215
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
22612216
rec.p = p;
2262-
if (dot(rotated_r.direction(), normal) > 0.0) {
2263-
rec.normal = -normal;
2264-
rec.front_face = false;
2265-
}
2266-
else {
2267-
rec.normal = normal;
2268-
rec.front_face = true;
2269-
}
2217+
rec.set_face_normal(rotated_r, normal);
22702218
return true;
22712219
}
22722220
else

0 commit comments

Comments
 (0)