Boundingboxes should be ready.
Next step (later) would be to properly use them other than group to lower the number of intersection calculation per ray.
This commit is contained in:
@@ -11,11 +11,96 @@
|
||||
|
||||
struct BoundingBox
|
||||
{
|
||||
private:
|
||||
bool isReset;
|
||||
|
||||
public:
|
||||
Tuple min;
|
||||
Tuple max;
|
||||
|
||||
BoundingBox() : min(-0, -0, -0), max(0, 0, 0) { };
|
||||
BoundingBox(Tuple min, Tuple max) : min(min), max(max) { };
|
||||
BoundingBox() : min(INFINITY, INFINITY, INFINITY, 1.0), max(-INFINITY, -INFINITY, -INFINITY, 1.0), isReset(true) { };
|
||||
BoundingBox(Tuple min, Tuple max) : min(min), max(max), isReset(false) { };
|
||||
|
||||
void operator|(const BoundingBox &b) {
|
||||
isReset = false;
|
||||
|
||||
if (this->min.x > b.min.x) { this->min.x = b.min.x; }
|
||||
if (this->min.y > b.min.y) { this->min.y = b.min.y; }
|
||||
if (this->min.z > b.min.z) { this->min.z = b.min.z; }
|
||||
|
||||
if (this->max.x < b.max.x) { this->max.x = b.max.x; }
|
||||
if (this->max.y < b.max.y) { this->max.y = b.max.y; }
|
||||
if (this->max.z < b.max.z) { this->max.z = b.max.z; }
|
||||
}
|
||||
|
||||
bool haveFiniteBounds() { return this->min.isRepresentable() && this->max.isRepresentable(); };
|
||||
|
||||
bool fitsIn(const BoundingBox &other) {
|
||||
bool fits = true;
|
||||
|
||||
if (this->min.x > other.min.x) { fits = false; }
|
||||
if (this->min.y > other.min.y) { fits = false; }
|
||||
if (this->min.z > other.min.z) { fits = false; }
|
||||
|
||||
if (this->max.x < other.max.x) { fits = false; }
|
||||
if (this->max.y < other.max.y) { fits = false; }
|
||||
if (this->max.z < other.max.z) { fits = false; }
|
||||
|
||||
return fits;
|
||||
}
|
||||
|
||||
void checkAxis(double axeOrigin, double axeDirection, double xMin, double xMax, double *axeMin, double *axeMax)
|
||||
{
|
||||
double tMinNumerator = (xMin - axeOrigin);
|
||||
double tMaxNumerator = (xMax - axeOrigin);
|
||||
|
||||
if (fabs(axeDirection) >= getEpsilon())
|
||||
{
|
||||
*axeMin = tMinNumerator / axeDirection;
|
||||
*axeMax = tMaxNumerator / axeDirection;
|
||||
}
|
||||
else
|
||||
{
|
||||
*axeMin = tMinNumerator * INFINITY;
|
||||
*axeMax = tMaxNumerator * INFINITY;
|
||||
}
|
||||
|
||||
if (*axeMin > *axeMax)
|
||||
{
|
||||
double swap = *axeMax;
|
||||
*axeMax = *axeMin;
|
||||
*axeMin = swap;
|
||||
}
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
this->isReset = true;
|
||||
min.x = min.y = min.z = INFINITY;
|
||||
max.x = max.y = max.z = -INFINITY;
|
||||
}
|
||||
|
||||
bool isEmpty() { return this->isReset; };
|
||||
|
||||
bool intesectMe(Ray r) {
|
||||
|
||||
double xtMin, xtMax, ytMin, ytMax, ztMin, ztMax;
|
||||
double tMin, tMax;
|
||||
|
||||
this->checkAxis(r.origin.x, r.direction.x, this->min.x, this->max.x, &xtMin, &xtMax);
|
||||
this->checkAxis(r.origin.y, r.direction.y, this->min.y, this->max.y, &ytMin, &ytMax);
|
||||
this->checkAxis(r.origin.z, r.direction.z, this->min.z, this->max.z, &ztMin, &ztMax);
|
||||
|
||||
tMin = max3(xtMin, ytMin, ztMin);
|
||||
tMax = min3(xtMax, ytMax, ztMax);
|
||||
|
||||
if (tMin <= tMax)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif //DORAYME_BOUNDINGBOX_H
|
||||
|
||||
@@ -29,6 +29,7 @@ public:
|
||||
|
||||
Cone() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CONE) {};
|
||||
BoundingBox getBounds();
|
||||
bool haveFiniteBounds() { return !(isinf(this->minCap) || isinf(this->maxCap)); };
|
||||
};
|
||||
|
||||
#endif /* DORAYME_CONE_H */
|
||||
|
||||
@@ -30,6 +30,7 @@ public:
|
||||
Cylinder() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CYLINDER) {};
|
||||
|
||||
BoundingBox getBounds();
|
||||
bool haveFiniteBounds() { return !(isinf(this->minCap) || isinf(this->maxCap)); };
|
||||
};
|
||||
|
||||
#endif //DORAYME_CYLINDER_H
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
#include <shape.h>
|
||||
|
||||
/* TODO: Add a way to force(?) material from group to be applied on childs */
|
||||
|
||||
class Group : public Shape
|
||||
{
|
||||
private:
|
||||
@@ -18,11 +20,15 @@ private:
|
||||
Shape* *objectList;
|
||||
uint32_t objectCount;
|
||||
|
||||
uint32_t allocatedUnboxableObjectCount;
|
||||
Shape* *unboxableObjectList;
|
||||
uint32_t unboxableObjectCount;
|
||||
|
||||
protected:
|
||||
Intersect localIntersect(Ray r);
|
||||
Tuple localNormalAt(Tuple point);
|
||||
|
||||
|
||||
BoundingBox bounds;
|
||||
|
||||
public:
|
||||
bool isEmpty();
|
||||
@@ -34,6 +40,8 @@ public:
|
||||
|
||||
BoundingBox getBounds();
|
||||
|
||||
void updateBoundingBox();
|
||||
|
||||
Group();
|
||||
};
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ private:
|
||||
public:
|
||||
Plane() : Shape(SHAPE_PLANE) { };
|
||||
BoundingBox getBounds();
|
||||
bool haveFiniteBounds() { return false; };
|
||||
};
|
||||
|
||||
#endif //DORAYME_PLANE_H
|
||||
|
||||
@@ -53,10 +53,12 @@ public:
|
||||
Shape(ShapeType = SHAPE_NONE);
|
||||
|
||||
virtual Intersect intersect(Ray r);
|
||||
virtual Intersect intersectOOB(Ray r) { return this->intersect(r); };
|
||||
Tuple normalAt(Tuple point);
|
||||
|
||||
/* Bouding box points are always world value */
|
||||
/* Bounding box points are always world value */
|
||||
virtual BoundingBox getBounds();
|
||||
virtual bool haveFiniteBounds() { return true; };
|
||||
|
||||
void updateTransform();
|
||||
Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
|
||||
|
||||
@@ -40,6 +40,10 @@ public:
|
||||
Tuple operator/(const double &b) const { return Tuple(this->x / b, this->y / b,
|
||||
this->z / b, this->w / b); };
|
||||
|
||||
void fixPoint();
|
||||
void fixVector();
|
||||
bool isRepresentable();
|
||||
|
||||
void set(double nX, double nY, double nZ) { this->x = nX; this->y = nY; this->z = nZ; };
|
||||
double magnitude();
|
||||
Tuple normalise();
|
||||
|
||||
Reference in New Issue
Block a user