refactor: apply review changes
This commit is contained in:
+22
-43
@@ -19,80 +19,59 @@ namespace Tyra {
|
|||||||
*/
|
*/
|
||||||
class Ray {
|
class Ray {
|
||||||
public:
|
public:
|
||||||
/**
|
/** @brief Constructor create a ampty ray */
|
||||||
* Constructor create a ampty ray
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
Ray();
|
Ray();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
|
||||||
* @param origin The origin of the ray
|
* @param origin The origin of the ray
|
||||||
* @param direction The direction of the ray (normalized Vec4)
|
* @param direction The direction of the ray (normalized Vec4)
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
Ray(Vec4* origin, Vec4* direction);
|
Ray(const Vec4& origin, const Vec4& direction);
|
||||||
~Ray();
|
~Ray();
|
||||||
|
|
||||||
Vec4 origin;
|
|
||||||
Vec4 direction;
|
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set origin and direction
|
* @param origin Vec4 starting point of the ray;
|
||||||
|
* @param direction Vec4 normalized vector pointing to direction;
|
||||||
*/
|
*/
|
||||||
void set(Vec4 origin, Vec4 direction);
|
void set(const Vec4& origin, const Vec4& direction);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set origin only
|
* @brief Set origin of the ray;
|
||||||
|
* @param origin Vec4 starting point of the ray;
|
||||||
*/
|
*/
|
||||||
void setOrigin(Vec4 origin);
|
void setOrigin(const Vec4& origin);
|
||||||
void setOrigin(const float& t_x, const float& t_y, const float& t_z);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set direction only
|
* @brief Set direction of the ray;
|
||||||
|
* @param direction Vec4 normalized vector pointing to direction;
|
||||||
*/
|
*/
|
||||||
void setDirection(Vec4 origin);
|
void setDirection(const Vec4& direction);
|
||||||
void setDirection(const float& t_x, const float& t_y, const float& t_z);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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(float t);
|
Vec4 at(const float& t);
|
||||||
|
|
||||||
|
/** @return Distance from the Vec4 point to the origin */
|
||||||
|
float distanceToPoint(const Vec4& point);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the distance from the Vec4 point to the origin
|
|
||||||
*/
|
|
||||||
float distanceToPoint(Vec4 point);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns distance from origin to intersected mesh
|
|
||||||
*/
|
|
||||||
float distanceTo(const Vec4& v) const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns Vec4 of intersection position
|
|
||||||
* @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 distance - the box to intersect with.
|
||||||
*
|
* @return Vec4 point of intersection
|
||||||
*/
|
*/
|
||||||
u8 intersectBox(Vec4* minCorner, Vec4* maxCorner, float& distance);
|
u8 intersectBox(const Vec4& minCorner, const Vec4& maxCorner, float& distance);
|
||||||
|
|
||||||
/**
|
/** Returns inverse direction */
|
||||||
* Returns distance from origin to intersected mesh
|
const Vec4 invDir();
|
||||||
*/
|
|
||||||
inline const Vec4 getPosition() { return _position; };
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns inverse direction
|
|
||||||
*/
|
|
||||||
Vec4 invDir();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vec4 _position;
|
Vec4 _origin;
|
||||||
|
Vec4 _direction;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Tyra
|
} // namespace Tyra
|
||||||
+23
-26
@@ -16,43 +16,40 @@
|
|||||||
namespace Tyra {
|
namespace Tyra {
|
||||||
|
|
||||||
Ray::Ray() {}
|
Ray::Ray() {}
|
||||||
Ray::Ray(Vec4* origin, Vec4* direction) {
|
Ray::Ray(const Vec4& origin, const Vec4& direction) {
|
||||||
this->origin.set(origin->x, origin->y, origin->z);
|
this->_origin.set(origin);
|
||||||
this->direction.set(direction->x, direction->y, direction->z);
|
this->_direction.set(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ray::~Ray() {}
|
Ray::~Ray() {}
|
||||||
|
|
||||||
void Ray::set(Vec4 origin, Vec4 direction) {
|
void Ray::set(const Vec4& origin, const Vec4& direction) {
|
||||||
this->origin.set(origin);
|
this->_origin.set(origin);
|
||||||
this->direction.set(direction);
|
this->_direction.set(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ray::setOrigin(Vec4 origin) { this->origin.set(origin); }
|
void Ray::setOrigin(const Vec4& origin) { this->_origin.set(origin); }
|
||||||
|
|
||||||
void Ray::setOrigin(const float& t_x, const float& t_y, const float& t_z) {
|
void Ray::setDirection(const Vec4& direction) {
|
||||||
this->origin.set(t_x, t_y, t_z);
|
this->_direction.set(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ray::setDirection(Vec4 direction) { this->direction.set(direction); }
|
Vec4 Ray::at(const float& t) { return (this->_direction * t) + this->_origin; }
|
||||||
|
|
||||||
void Ray::setDirection(const float& t_x, const float& t_y, const float& t_z) {
|
float Ray::distanceToPoint(const Vec4& point) {
|
||||||
this->direction.set(t_x, t_y, t_z);
|
return this->_origin.distanceTo(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Ray::distanceToPoint(Vec4 point) { return origin.distanceTo(point); }
|
u8 Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner,
|
||||||
|
float& distance) {
|
||||||
Vec4 Ray::at(float t) { return (this->direction * t) + this->origin; }
|
|
||||||
|
|
||||||
u8 Ray::intersectBox(Vec4* minCorner, Vec4* maxCorner, float& distance) {
|
|
||||||
float tmin, tmax, tymin, tymax, tzmin, tzmax;
|
float tmin, tmax, tymin, tymax, tzmin, tzmax;
|
||||||
Vec4 invDir = this->invDir();
|
Vec4 invDir = this->invDir();
|
||||||
invDir.normalize();
|
invDir.normalize();
|
||||||
|
|
||||||
tmin = (minCorner->x - this->origin.x) * invDir.x;
|
tmin = (minCorner.x - this->_origin.x) * invDir.x;
|
||||||
tmax = (maxCorner->x - this->origin.x) * invDir.x;
|
tmax = (maxCorner.x - this->_origin.x) * invDir.x;
|
||||||
tymin = (minCorner->y - this->origin.y) * invDir.y;
|
tymin = (minCorner.y - this->_origin.y) * invDir.y;
|
||||||
tymax = (maxCorner->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;
|
||||||
@@ -63,8 +60,8 @@ u8 Ray::intersectBox(Vec4* minCorner, Vec4* maxCorner, float& distance) {
|
|||||||
|
|
||||||
if (tymax < tmax) tmax = tymax;
|
if (tymax < tmax) tmax = tymax;
|
||||||
|
|
||||||
tzmin = (minCorner->z - this->origin.z) * invDir.z;
|
tzmin = (minCorner.z - this->_origin.z) * invDir.z;
|
||||||
tzmax = (maxCorner->z - this->origin.z) * invDir.z;
|
tzmax = (maxCorner.z - this->_origin.z) * invDir.z;
|
||||||
|
|
||||||
if ((tmin > tzmax) || (tzmin > tmax)) {
|
if ((tmin > tzmax) || (tzmin > tmax)) {
|
||||||
distance = -1.0f;
|
distance = -1.0f;
|
||||||
@@ -84,9 +81,9 @@ u8 Ray::intersectBox(Vec4* minCorner, Vec4* maxCorner, float& distance) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec4 Ray::invDir() {
|
const Vec4 Ray::invDir() {
|
||||||
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 / this->_direction.z, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // Namespace Tyra
|
} // Namespace Tyra
|
||||||
Reference in New Issue
Block a user