Skip to content

Commit f972a73

Browse files
committed
refactor(mesh): simplify hit detection logic
1 parent 9e1cf95 commit f972a73

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

libs/Prism/src/mesh.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ Mesh::Mesh(ObjReader& reader):material(std::move(reader.curMaterial)){
2222
bool Mesh::hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) const {
2323
Ray transformed_ray = ray.transform(inverseTransform);
2424

25-
bool hit_anything = false;
26-
rec.t = INFINITY;
25+
rec.t = t_max;
2726
for (const auto& triangle : mesh) {
28-
if (triangle.hit(transformed_ray, 0.001, rec.t, rec)) {
29-
hit_anything = true;
30-
rec.p = transform * transformed_ray.at(rec.t);
31-
Vector3 world_normal = (inverseTransposeTransform * rec.normal).normalize();
32-
rec.set_face_normal(ray, world_normal);
33-
rec.material = material;
34-
}
27+
triangle.hit(transformed_ray, 0.001, rec.t, rec);
3528
}
36-
return hit_anything;
29+
30+
if (rec.t < t_max) {
31+
rec.p = transform * transformed_ray.at(rec.t);
32+
Vector3 world_normal = (inverseTransposeTransform * rec.normal).normalize();
33+
rec.set_face_normal(ray, world_normal);
34+
rec.material = material;
35+
return true;
36+
}
37+
38+
return false;
3739
};
3840

3941
};

0 commit comments

Comments
 (0)