ray class api change

This commit is contained in:
h4570
2022-08-14 20:01:49 +02:00
parent a78f430a6e
commit 7ef0e73543
2 changed files with 27 additions and 60 deletions
+7 -28
View File
@@ -29,34 +29,17 @@ class Ray {
Ray(const Vec4& origin, const Vec4& direction); Ray(const Vec4& origin, const Vec4& direction);
~Ray(); ~Ray();
// Methods Vec4 origin;
Vec4 direction;
/**
* @param origin Vec4 starting point of the ray;
* @param direction Vec4 normalized vector pointing to direction;
*/
void set(const Vec4& origin, const Vec4& direction);
/**
* @brief Set origin of the ray;
* @param origin Vec4 starting point of the ray;
*/
void setOrigin(const Vec4& origin);
/**
* @brief Set direction of the ray;
* @param direction Vec4 normalized vector pointing to direction;
*/
void setDirection(const Vec4& direction);
/** /**
* @return Vec4 that is a given distance along this Ray * @return Vec4 that is a given distance along this Ray
* @param t - the distance along the Ray to retrieve a position for. * @param t - the distance along the Ray to retrieve a position for.
*/ */
Vec4 at(const float& t); Vec4 at(const float& t) const;
/** @return Distance from the Vec4 point to the origin */ /** @return Distance from the Vec4 point to the origin */
float distanceToPoint(const Vec4& point); float distanceToPoint(const Vec4& point) const;
/** /**
* @param minCorner - pointer to min corner position. (bottom left) * @param minCorner - pointer to min corner position. (bottom left)
@@ -64,15 +47,11 @@ class Ray {
* @param distance - the box to intersect with. * @param distance - the box to intersect with.
* @return Vec4 point of intersection * @return Vec4 point of intersection
*/ */
u8 intersectBox(const Vec4& minCorner, const Vec4& maxCorner, bool intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
float& distance); float& distance) const;
/** Returns inverse direction */ /** Returns inverse direction */
const Vec4 invDir(); Vec4 invDir() const;
private:
Vec4 _origin;
Vec4 _direction;
}; };
} // namespace Tyra } // namespace Tyra
+20 -32
View File
@@ -16,40 +16,28 @@
namespace Tyra { namespace Tyra {
Ray::Ray() {} Ray::Ray() {}
Ray::Ray(const Vec4& origin, const Vec4& direction) { Ray::Ray(const Vec4& t_origin, const Vec4& t_direction) {
this->_origin.set(origin); origin.set(t_origin);
this->_direction.set(direction); direction.set(t_direction);
} }
Ray::~Ray() {} Ray::~Ray() {}
void Ray::set(const Vec4& origin, const Vec4& direction) { Vec4 Ray::at(const float& t) const { return (direction * t) + origin; }
this->_origin.set(origin);
this->_direction.set(direction); float Ray::distanceToPoint(const Vec4& point) const {
return origin.distanceTo(point);
} }
void Ray::setOrigin(const Vec4& origin) { this->_origin.set(origin); } bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
float& distance) const {
auto inv = invDir();
inv.normalize();
void Ray::setDirection(const Vec4& direction) { float tmin = (minCorner.x - this->origin.x) * inv.x;
this->_direction.set(direction); float tmax = (maxCorner.x - this->origin.x) * inv.x;
} float tymin = (minCorner.y - this->origin.y) * inv.y;
float tymax = (maxCorner.y - this->origin.y) * inv.y;
Vec4 Ray::at(const float& t) { return (this->_direction * t) + this->_origin; }
float Ray::distanceToPoint(const Vec4& point) {
return this->_origin.distanceTo(point);
}
u8 Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
float& distance) {
float tmin, tmax, tymin, tymax, tzmin, tzmax;
Vec4 invDir = this->invDir();
invDir.normalize();
tmin = (minCorner.x - this->_origin.x) * invDir.x;
tmax = (maxCorner.x - this->_origin.x) * invDir.x;
tymin = (minCorner.y - this->_origin.y) * invDir.y;
tymax = (maxCorner.y - this->_origin.y) * invDir.y;
if ((tmin > tymax) || (tymin > tmax)) { if ((tmin > tymax) || (tymin > tmax)) {
distance = -1.0f; distance = -1.0f;
@@ -60,8 +48,8 @@ u8 Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
if (tymax < tmax) tmax = tymax; if (tymax < tmax) tmax = tymax;
tzmin = (minCorner.z - this->_origin.z) * invDir.z; float tzmin = (minCorner.z - this->origin.z) * inv.z;
tzmax = (maxCorner.z - this->_origin.z) * invDir.z; float tzmax = (maxCorner.z - this->origin.z) * inv.z;
if ((tmin > tzmax) || (tzmin > tmax)) { if ((tmin > tzmax) || (tzmin > tmax)) {
distance = -1.0f; distance = -1.0f;
@@ -81,9 +69,9 @@ u8 Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
return 1; return 1;
} }
const Vec4 Ray::invDir() { Vec4 Ray::invDir() const {
return Vec4(1 / this->_direction.x, 1 / this->_direction.y, return Vec4(1 / this->direction.x, 1 / this->direction.y,
1 / this->_direction.z, 1); 1 / this->direction.z, 1);
} }
} // Namespace Tyra } // Namespace Tyra