change distance to output distance pointer

This commit is contained in:
h4570
2022-08-14 20:29:56 +02:00
parent 156973714b
commit d5261dbb0c
2 changed files with 11 additions and 11 deletions
+2 -2
View File
@@ -45,11 +45,11 @@ class Ray {
/** /**
* @param minCorner - pointer to min corner position. (bottom left) * @param minCorner - pointer to min corner position. (bottom left)
* @param maxCorner - pointer to max corner position. (top right) * @param maxCorner - pointer to max corner position. (top right)
* @param distance - the box to intersect with. * @param outputDistance - optional - distance the box to intersect with.
* @return Vec4 point of intersection * @return Vec4 point of intersection
*/ */
bool intersectBox(const Vec4& minCorner, const Vec4& maxCorner, bool intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
const float& distance) const; float* outputDistance = nullptr) const;
/** Returns inverse direction */ /** Returns inverse direction */
Vec4 invDir() const; Vec4 invDir() const;
+9 -9
View File
@@ -32,7 +32,7 @@ float Ray::distanceToPoint(const Vec4& point) const {
} }
bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner, bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
const float& distance) const { float* outputDistance) const {
auto inv = invDir(); auto inv = invDir();
inv.normalize(); inv.normalize();
@@ -42,8 +42,7 @@ bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
float tymax = (maxCorner.y - this->origin.y) * inv.y; float tymax = (maxCorner.y - this->origin.y) * inv.y;
if ((tmin > tymax) || (tymin > tmax)) { if ((tmin > tymax) || (tymin > tmax)) {
distance = -1.0f; return false;
return 0;
} }
if (tymin > tmin) tmin = tymin; if (tymin > tmin) tmin = tymin;
@@ -54,8 +53,7 @@ bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
float tzmax = (maxCorner.z - this->origin.z) * inv.z; float tzmax = (maxCorner.z - this->origin.z) * inv.z;
if ((tmin > tzmax) || (tzmin > tmax)) { if ((tmin > tzmax) || (tzmin > tmax)) {
distance = -1.0f; return false;
return 0;
} }
if (tzmin > tmin) tmin = tzmin; if (tzmin > tmin) tmin = tzmin;
@@ -63,12 +61,14 @@ bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
if (tzmax < tmax) tmax = tzmax; if (tzmax < tmax) tmax = tzmax;
if (tmax < 0) { if (tmax < 0) {
distance = -1.0f; return false;
return 0;
} }
distance = tmin >= 0 ? tmin : tmax; if (outputDistance != nullptr) {
return 1; *outputDistance = tmin >= 0 ? tmin : tmax;
}
return true;
} }
Vec4 Ray::invDir() const { Vec4 Ray::invDir() const {