|
209 | 209 | rec.t = temp;
|
210 | 210 | rec.p = r.at(rec.t);
|
211 | 211 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
212 |
| - rec.normal = (rec.p - center(r.time())) / radius; |
| 212 | + vec3 outward_normal = (rec.p - center(r.time())) / radius; |
213 | 213 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
| 214 | + rec.set_face_normal(r, outward_normal); |
214 | 215 | rec.mat_ptr = mat_ptr;
|
215 | 216 | return true;
|
216 | 217 | }
|
|
219 | 220 | rec.t = temp;
|
220 | 221 | rec.p = r.at(rec.t);
|
221 | 222 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
222 |
| - rec.normal = (rec.p - center(r.time())) / radius; |
| 223 | + vec3 outward_normal = (rec.p - center(r.time())) / radius; |
223 | 224 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
| 225 | + rec.set_face_normal(r, outward_normal); |
224 | 226 | rec.mat_ptr = mat_ptr;
|
225 | 227 | return true;
|
226 | 228 | }
|
|
1728 | 1730 | rec.u = (x-x0)/(x1-x0);
|
1729 | 1731 | rec.v = (y-y0)/(y1-y0);
|
1730 | 1732 | rec.t = t;
|
| 1733 | + vec3 outward_normal = vec3(0, 0, 1); |
| 1734 | + rec.set_face_normal(r, outward_normal); |
1731 | 1735 | rec.mat_ptr = mp;
|
1732 | 1736 | rec.p = r.at(t);
|
1733 |
| - rec.normal = vec3(0, 0, 1); |
1734 | 1737 | return true;
|
1735 | 1738 | }
|
1736 | 1739 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
1834 | 1837 | rec.u = (x-x0)/(x1-x0);
|
1835 | 1838 | rec.v = (z-z0)/(z1-z0);
|
1836 | 1839 | rec.t = t;
|
| 1840 | + vec3 outward_normal = vec3(0, 1, 0); |
| 1841 | + rec.set_face_normal(r, outward_normal); |
1837 | 1842 | rec.mat_ptr = mp;
|
1838 | 1843 | rec.p = r.at(t);
|
1839 |
| - rec.normal = vec3(0, 1, 0); |
1840 | 1844 | return true;
|
1841 | 1845 | }
|
1842 | 1846 |
|
|
1851 | 1855 | rec.u = (y-y0)/(y1-y0);
|
1852 | 1856 | rec.v = (z-z0)/(z1-z0);
|
1853 | 1857 | rec.t = t;
|
| 1858 | + vec3 outward_normal = vec3(1, 0, 0); |
| 1859 | + rec.set_face_normal(r, outward_normal); |
1854 | 1860 | rec.mat_ptr = mp;
|
1855 | 1861 | rec.p = r.at(t);
|
1856 |
| - rec.normal = vec3(1, 0, 0); |
1857 | 1862 | return true;
|
1858 | 1863 | }
|
1859 | 1864 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
1903 | 1908 | <div class='together'>
|
1904 | 1909 | We get:
|
1905 | 1910 |
|
1906 |
| -  |
| 1911 | +  |
1907 | 1912 |
|
1908 | 1913 | </div>
|
1909 | 1914 |
|
1910 | 1915 | <div class='together'>
|
1911 |
| -This is very noisy because the light is small. But why are the other walls missing? They are facing |
1912 |
| -the wrong way. We need outward facing normals. Let’s make a hittable that does nothing but hold |
1913 |
| -another hittable, but reverses the normals: |
| 1916 | +This is very noisy because the light is small. But we have a problem: some of the walls are facing |
| 1917 | +the wrong way. We haven't specified that a diffuse material should behave differently on different |
| 1918 | +faces of the object, but what if the Cornell box had a different pattern on the inside and outside |
| 1919 | +walls? The rectangle objects are described such that their front faces are always in the |
| 1920 | +$(1, 0, 0)$, $(0, 1, 0)$, or $(0, 0, 1)$ directions. We need a way to switch the faces of an |
| 1921 | +object. Let’s make a hittable that does nothing but hold another hittable, and flips the face: |
1914 | 1922 |
|
1915 | 1923 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1916 |
| - class flip_normals : public hittable { |
| 1924 | + class flip_face : public hittable { |
1917 | 1925 | public:
|
1918 |
| - flip_normals(hittable *p) : ptr(p) {} |
| 1926 | + flip_face(hittable *p) : ptr(p) {} |
1919 | 1927 |
|
1920 | 1928 | virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
|
1921 | 1929 | if (ptr->hit(r, t_min, t_max, rec)) {
|
1922 |
| - rec.normal = -rec.normal; |
| 1930 | + rec.front_face = !rec.front_face; |
1923 | 1931 | return true;
|
1924 | 1932 | }
|
1925 | 1933 | else
|
|
1933 | 1941 | hittable *ptr;
|
1934 | 1942 | };
|
1935 | 1943 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
1936 |
| - [Listing [flip-normals]: <kbd>[hittable.h]</kbd> Flip-normals function] |
| 1944 | + [Listing [flip-face]: <kbd>[hittable.h]</kbd> Flip-Face function] |
1937 | 1945 | </div>
|
1938 | 1946 |
|
1939 | 1947 | <div class='together'>
|
|
1949 | 1957 | material *light = new diffuse_light(new constant_texture(vec3(15, 15, 15)));
|
1950 | 1958 |
|
1951 | 1959 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1952 |
| - objects.add(new flip_normals(new yz_rect(0, 555, 0, 555, 555, green))); |
| 1960 | + objects.add(new flip_face(new yz_rect(0, 555, 0, 555, 555, green))); |
1953 | 1961 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1954 | 1962 | objects.add(new yz_rect(0, 555, 0, 555, 0, red));
|
1955 | 1963 | objects.add(new xz_rect(213, 343, 227, 332, 554, light));
|
1956 | 1964 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1957 |
| - objects.add(new flip_normals(new xz_rect(0, 555, 0, 555, 555, white))); |
| 1965 | + objects.add(new flip_face(new xz_rect(0, 555, 0, 555, 555, white))); |
1958 | 1966 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1959 | 1967 | objects.add(new xz_rect(0, 555, 0, 555, 0, white));
|
1960 | 1968 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1961 |
| - objects.add(new flip_normals(new xy_rect(0, 555, 0, 555, 555, white))); |
| 1969 | + objects.add(new flip_face(new xy_rect(0, 555, 0, 555, 555, white))); |
1962 | 1970 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1963 | 1971 |
|
1964 | 1972 | return objects;
|
|
2004 | 2012 | pmax = p1;
|
2005 | 2013 | hittable **list = new hittable*[6];
|
2006 | 2014 | list[0] = new xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), ptr);
|
2007 |
| - list[1] = new flip_normals(new xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), ptr)); |
| 2015 | + list[1] = new flip_face(new xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), ptr)); |
2008 | 2016 | list[2] = new xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), ptr);
|
2009 |
| - list[3] = new flip_normals(new xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), ptr)); |
| 2017 | + list[3] = new flip_face(new xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), ptr)); |
2010 | 2018 | list[4] = new yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), ptr);
|
2011 |
| - list[5] = new flip_normals(new yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), ptr)); |
| 2019 | + list[5] = new flip_face(new yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), ptr)); |
2012 | 2020 | list_ptr = new hittable_list(list,6);
|
2013 | 2021 | }
|
2014 | 2022 |
|
|
2071 | 2079 | ray moved_r(r.origin() - offset, r.direction(), r.time());
|
2072 | 2080 | if (ptr->hit(moved_r, t_min, t_max, rec)) {
|
2073 | 2081 | rec.p += offset;
|
| 2082 | + rec.set_face_normal(moved_r, rec.normal); |
2074 | 2083 | return true;
|
2075 | 2084 | }
|
2076 | 2085 | else
|
|
2210 | 2219 | normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
|
2211 | 2220 | normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
|
2212 | 2221 | rec.p = p;
|
2213 |
| - rec.normal = normal; |
| 2222 | + rec.set_face_normal(rotated_r, normal); |
2214 | 2223 | return true;
|
2215 | 2224 | }
|
2216 | 2225 | else
|
|
2363 | 2372 | }
|
2364 | 2373 |
|
2365 | 2374 | rec.normal = vec3(1,0,0); // arbitrary
|
| 2375 | + rec.front_face = true; // also arbitrary |
2366 | 2376 | rec.mat_ptr = phase_function;
|
2367 | 2377 | return true;
|
2368 | 2378 | }
|
|
2391 | 2401 | auto green = new lambertian(new constant_texture(vec3(0.12, 0.45, 0.15)));
|
2392 | 2402 | auto light = new diffuse_light(new constant_texture(vec3(7, 7, 7)));
|
2393 | 2403 |
|
2394 |
| - objects.add(new flip_normals(new yz_rect(0, 555, 0, 555, 555, green))); |
| 2404 | + objects.add(new flip_face(new yz_rect(0, 555, 0, 555, 555, green))); |
2395 | 2405 | objects.add(new yz_rect(0, 555, 0, 555, 0, red));
|
2396 | 2406 | objects.add(new xz_rect(113, 443, 127, 432, 554, light));
|
2397 |
| - objects.add(new flip_normals(new xz_rect(0, 555, 0, 555, 555, white))); |
| 2407 | + objects.add(new flip_face(new xz_rect(0, 555, 0, 555, 555, white))); |
2398 | 2408 | objects.add(new xz_rect(0, 555, 0, 555, 0, white));
|
2399 |
| - objects.add(new flip_normals(new xy_rect(0, 555, 0, 555, 555, white))); |
| 2409 | + objects.add(new flip_face(new xy_rect(0, 555, 0, 555, 555, white))); |
2400 | 2410 |
|
2401 | 2411 | hittable* box1 = new box(vec3(0,0,0), vec3(165,330,165), white);
|
2402 | 2412 | box1 = new rotate_y(box1, 15);
|
|
0 commit comments