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:
Godzil
2020-02-25 18:03:12 +00:00
parent 831a096281
commit 2ea4abdce7
21 changed files with 406 additions and 34 deletions

View File

@@ -129,10 +129,13 @@ BoundingBox Cone::getBounds()
double a = fabs(this->minCap);
double b = fabs(this->maxCap);
double limit = (a < b)?a:b;
double limit = (a > b)?a:b;
ret.min = this->objectToWorld(Point(-limit, this->minCap, -limit));
ret.max = this->objectToWorld(Point(limit, this->maxCap, limit));
ret.min.fixPoint();
ret.max.fixPoint();
return ret;
}

View File

@@ -115,5 +115,8 @@ BoundingBox Cylinder::getBounds()
ret.min = this->objectToWorld(Point(-1, this->minCap, -1));
ret.max = this->objectToWorld(Point(1, this->maxCap, 1));
ret.min.fixPoint();
ret.max.fixPoint();
return ret;
}

View File

@@ -19,6 +19,11 @@ Group::Group() : Shape(SHAPE_GROUP)
this->allocatedObjectCount = MIN_ALLOC;
this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
this->objectCount = 0;
this->allocatedUnboxableObjectCount = MIN_ALLOC;
this->unboxableObjectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
this->unboxableObjectCount = 0;
}
Intersect Group::intersect(Ray r)
@@ -27,9 +32,28 @@ Intersect Group::intersect(Ray r)
int i, j;
if (this->objectCount > 0)
{
for(i = 0; i < this->objectCount; i++)
if (this->bounds.intesectMe(r))
{
Intersect xs = this->objectList[i]->intersect(r);
for (i = 0 ; i < this->objectCount ; i++)
{
Intersect xs = this->objectList[i]->intersect(r);
if (xs.count() > 0)
{
for (j = 0 ; j < xs.count() ; j++)
{
ret.add(xs[j]);
}
}
}
}
}
/* We are force to do them all the time */
if (this->unboxableObjectCount > 0)
{
for(i = 0; i < this->unboxableObjectCount; i++)
{
Intersect xs = this->unboxableObjectList[i]->intersect(r);
if (xs.count() > 0)
{
for(j = 0; j < xs.count(); j++)
@@ -39,7 +63,6 @@ Intersect Group::intersect(Ray r)
}
}
}
return ret;
}
@@ -53,48 +76,64 @@ Tuple Group::localNormalAt(Tuple point)
return Vector(1, 0, 0);
}
/* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
void Group::addObject(Shape *s)
{
if ((this->objectCount + 1) > this->allocatedObjectCount)
if (s->haveFiniteBounds())
{
this->allocatedObjectCount *= 2;
this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
if ((this->objectCount + 1) > this->allocatedObjectCount)
{
this->allocatedObjectCount *= 2;
this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
}
s->parent = this;
s->updateTransform();
this->objectList[this->objectCount++] = s;
this->bounds | s->getBounds();
}
else
{
if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
{
this->allocatedUnboxableObjectCount *= 2;
this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
}
s->parent = this;
s->updateTransform();
s->parent = this;
s->updateTransform();
this->objectList[this->objectCount++] = s;
this->unboxableObjectList[this->unboxableObjectCount++] = s;
}
}
bool Group::isEmpty()
{
return (this->objectCount == 0);
return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
}
BoundingBox Group::getBounds()
{
BoundingBox ret;
if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
return this->bounds;
}
void Group::updateBoundingBox()
{
this->bounds.reset();
if (this->objectCount > 0)
{
ret.min = Point(INFINITY, INFINITY, INFINITY);
ret.max = Point(-INFINITY, -INFINITY, -INFINITY);
int i;
for(i = 0; i < this->objectCount; i++)
{
BoundingBox obj = this->objectList[i]->getBounds();
if (ret.min.x > obj.min.x) { ret.min.x = obj.min.x; }
if (ret.min.y > obj.min.y) { ret.min.y = obj.min.y; }
if (ret.min.z > obj.min.z) { ret.min.z = obj.min.z; }
if (ret.max.x < obj.max.x) { ret.max.x = obj.max.x; }
if (ret.max.y < obj.max.y) { ret.max.y = obj.max.y; }
if (ret.max.z < obj.max.z) { ret.max.z = obj.max.z; }
if (!this->objectList[i]->haveFiniteBounds())
{
BoundingBox objB = this->objectList[i]->getBounds();
this->bounds | objB;
}
}
}
return ret;
}

View File

@@ -42,5 +42,8 @@ BoundingBox Plane::getBounds()
ret.min = this->objectToWorld(Point(-INFINITY, 0-getEpsilon(), -INFINITY));
ret.max = this->objectToWorld(Point(INFINITY, 0+getEpsilon(), INFINITY));
ret.min.fixPoint();
ret.max.fixPoint();
return ret;
}