|
1068 | 1068 | void clear() { objects.clear(); } |
1069 | 1069 | void add(shared_ptr<hittable> object) { objects.push_back(object); } |
1070 | 1070 |
|
1071 | | - bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override; |
| 1071 | + bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override { |
| 1072 | + hit_record temp_rec; |
| 1073 | + bool hit_anything = false; |
| 1074 | + auto closest_so_far = ray_tmax; |
| 1075 | + |
| 1076 | + for (const auto& object : objects) { |
| 1077 | + if (object->hit(r, ray_tmin, closest_so_far, temp_rec)) { |
| 1078 | + hit_anything = true; |
| 1079 | + closest_so_far = temp_rec.t; |
| 1080 | + rec = temp_rec; |
| 1081 | + } |
| 1082 | + } |
| 1083 | + |
| 1084 | + return hit_anything; |
| 1085 | + } |
1072 | 1086 |
|
1073 | 1087 | public: |
1074 | 1088 | std::vector<shared_ptr<hittable>> objects; |
1075 | 1089 | }; |
1076 | 1090 |
|
1077 | | - bool hittable_list::hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const { |
1078 | | - hit_record temp_rec; |
1079 | | - bool hit_anything = false; |
1080 | | - auto closest_so_far = ray_tmax; |
1081 | | - |
1082 | | - for (const auto& object : objects) { |
1083 | | - if (object->hit(r, ray_tmin, closest_so_far, temp_rec)) { |
1084 | | - hit_anything = true; |
1085 | | - closest_so_far = temp_rec.t; |
1086 | | - rec = temp_rec; |
1087 | | - } |
1088 | | - } |
1089 | | - |
1090 | | - return hit_anything; |
1091 | | - } |
1092 | | - |
1093 | 1091 | #endif |
1094 | 1092 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1095 | 1093 | [Listing [hittable-list-initial]: <kbd>[hittable_list.h]</kbd> The hittable_list class] |
|
1326 | 1324 | class hittable_list : public hittable { |
1327 | 1325 | ... |
1328 | 1326 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1329 | | - bool hit(const ray& r, interval ray_t, hit_record& rec) const override; |
| 1327 | + bool hit(const ray& r, interval ray_t, hit_record& rec) const override { |
1330 | 1328 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1331 | | - ... |
1332 | | - }; |
1333 | | - |
1334 | | - |
1335 | | - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1336 | | - bool hittable_list::hit(const ray& r, interval ray_t, hit_record& rec) const { |
1337 | | - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1338 | | - hit_record temp_rec; |
1339 | | - bool hit_anything = false; |
| 1329 | + hit_record temp_rec; |
| 1330 | + bool hit_anything = false; |
1340 | 1331 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1341 | | - auto closest_so_far = ray_t.max; |
| 1332 | + auto closest_so_far = ray_t.max; |
1342 | 1333 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1343 | 1334 |
|
1344 | | - for (const auto& object : objects) { |
| 1335 | + for (const auto& object : objects) { |
1345 | 1336 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1346 | | - if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) { |
| 1337 | + if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) { |
1347 | 1338 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1348 | | - hit_anything = true; |
1349 | | - closest_so_far = temp_rec.t; |
1350 | | - rec = temp_rec; |
| 1339 | + hit_anything = true; |
| 1340 | + closest_so_far = temp_rec.t; |
| 1341 | + rec = temp_rec; |
| 1342 | + } |
1351 | 1343 | } |
1352 | | - } |
1353 | 1344 |
|
1354 | | - return hit_anything; |
1355 | | - } |
| 1345 | + return hit_anything; |
| 1346 | + } |
1356 | 1347 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1357 | 1348 | [Listing [hittable-list-with-interval]: <kbd>[hittable.h]</kbd> |
1358 | 1349 | hittable_list::hit() using interval] |
1359 | 1350 |
|
1360 | 1351 |
|
1361 | | - |
1362 | 1352 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1363 | 1353 | class sphere : public hittable { |
1364 | 1354 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
0 commit comments