diff --git a/openVulkanoCpp/Scene/Ray.cpp b/openVulkanoCpp/Scene/Ray.cpp index 289e46d..7e2a30c 100644 --- a/openVulkanoCpp/Scene/Ray.cpp +++ b/openVulkanoCpp/Scene/Ray.cpp @@ -51,13 +51,14 @@ namespace OpenVulkano::Scene const Math::Vector3f& v2) const { RayHit hitRes; - if (intersectRayTriangle(m_origin, m_dir, v0, v1, v2, m_baryPos, hitRes.distance) && hitRes.distance >= 0) + float d; + if (intersectRayTriangle(m_origin, m_dir, v0, v1, v2, m_baryPos, d) && d >= 0) { hitRes.point = (1.f - m_baryPos.x - m_baryPos.y) * v0 + m_baryPos.x * v1 + m_baryPos.y * v2; Math::Vector3f e = v1 - v0; Math::Vector3f e2 = v2 - v0; // triangle normal. won't work if triangle is smoothly shaded. use other overloaded method instead. - hitRes.distance2 = distance2(m_origin, hitRes.point); + hitRes.SetDistance(d); hitRes.normal = normalize(cross(e, e2)); return hitRes; } @@ -180,10 +181,11 @@ namespace OpenVulkano::Scene { RayHit hit; Math::Vector3f norm = normalize(pNorm); - if (intersectRayPlane(m_origin, m_dir, pOrigin, pNorm, hit.distance)) + float d; + if (intersectRayPlane(m_origin, m_dir, pOrigin, pNorm, d)) { - hit.point = m_origin + m_dir * hit.distance; - hit.distance2 = distance2(m_origin, hit.point); + hit.SetDistance(d); + hit.point = m_origin + m_dir * d; hit.normal = norm; return hit; } diff --git a/openVulkanoCpp/Scene/Ray.hpp b/openVulkanoCpp/Scene/Ray.hpp index 6413eaa..999888b 100644 --- a/openVulkanoCpp/Scene/Ray.hpp +++ b/openVulkanoCpp/Scene/Ray.hpp @@ -28,9 +28,13 @@ namespace OpenVulkano::Scene } return distance; } + void SetDistance(float d) + { + this->distance = d; + this->distance2 = d * d; + } bool operator==(const RayHit& other) const; bool operator!=(const RayHit& other) const; - friend class Ray; private: mutable float distance = -1; };