Huge speed up by changing how Intersect are shared.

This commit is contained in:
Godzil
2020-03-12 00:11:26 +00:00
parent 0aa949c60b
commit b00bb75189
38 changed files with 116 additions and 153 deletions

View File

@@ -51,10 +51,8 @@ void Cone::intersectCaps(Ray r, Intersect &xs)
}
}
Intersect Cone::localIntersect(Ray r)
void Cone::localIntersect(Ray r, Intersect &xs)
{
Intersect ret;
double A = (r.direction.x * r.direction.x) -
(r.direction.y * r.direction.y) +
(r.direction.z * r.direction.z);
@@ -70,7 +68,7 @@ Intersect Cone::localIntersect(Ray r)
if ((fabs(A) <= getEpsilon()) && (fabs(B) >= getEpsilon()))
{
double t = -C / (2*B);
ret.add(Intersection(t, this));
xs.add(Intersection(t, this));
}
else if (fabs(A) >= getEpsilon())
{
@@ -83,20 +81,18 @@ Intersect Cone::localIntersect(Ray r)
double y0 = r.origin.y + t0 * r.direction.y;
if ((this->minCap < y0) && (y0 < this->maxCap))
{
ret.add(Intersection(t0, this));
xs.add(Intersection(t0, this));
}
double y1 = r.origin.y + t1 * r.direction.y;
if ((this->minCap < y1) && (y1 < this->maxCap))
{
ret.add(Intersection(t1, this));
xs.add(Intersection(t1, this));
}
}
}
this->intersectCaps(r, ret);
return ret;
this->intersectCaps(r, xs);
}
Tuple Cone::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -24,31 +24,24 @@ CSG::CSG(OperationType operation, Shape *left, Shape *right) : Shape(Shape::CSG)
this->bounds | this->right->getBounds();
}
Intersect CSG::localIntersect(Ray r)
void CSG::localIntersect(Ray r, Intersect &xs)
{
return this->intersect(r);
this->intersect(r, xs);
}
Intersect CSG::intersect(Ray r)
void CSG::intersect(Ray &r, Intersect &xs)
{
int i;
Intersect ret = Intersect();
Intersect tmp = Intersect();
if (this->bounds.intesectMe(r))
{
Intersect leftxs = this->left->intersect(r);
Intersect rightxs = this->right->intersect(r);
this->left->intersect(r, tmp);
this->right->intersect(r, tmp);
for (i = 0 ; i < rightxs.count() ; i++)
{
leftxs.add(rightxs[i]);
}
this->filterIntersections(leftxs, ret);
this->filterIntersections(tmp, xs);
}
return ret;
}
Tuple CSG::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -36,10 +36,8 @@ void Cube::checkAxis(double axeOrigin, double axeDirection, double *axeMin, doub
}
}
Intersect Cube::localIntersect(Ray r)
void Cube::localIntersect(Ray r, Intersect &xs)
{
Intersect ret;
double xtMin, xtMax, ytMin, ytMax, ztMin, ztMax;
double tMin, tMax;
@@ -52,11 +50,9 @@ Intersect Cube::localIntersect(Ray r)
if (tMin <= tMax)
{
ret.add(Intersection(tMin, this));
ret.add(Intersection(tMax, this));
xs.add(Intersection(tMin, this));
xs.add(Intersection(tMax, this));
}
return ret;
}
Tuple Cube::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -51,10 +51,8 @@ void Cylinder::intersectCaps(Ray r, Intersect &xs)
}
}
Intersect Cylinder::localIntersect(Ray r)
void Cylinder::localIntersect(Ray r, Intersect &xs)
{
Intersect ret;
double A = r.direction.x * r.direction.x + r.direction.z * r.direction.z;
/* Ray is parallel to the Y axis */
@@ -74,20 +72,18 @@ Intersect Cylinder::localIntersect(Ray r)
double y0 = r.origin.y + t0 * r.direction.y;
if ((this->minCap < y0) && (y0 < this->maxCap))
{
ret.add(Intersection(t0, this));
xs.add(Intersection(t0, this));
}
double y1 = r.origin.y + t1 * r.direction.y;
if ((this->minCap < y1) && (y1 < this->maxCap))
{
ret.add(Intersection(t1, this));
xs.add(Intersection(t1, this));
}
}
}
this->intersectCaps(r, ret);
return ret;
this->intersectCaps(r, xs);
}
Tuple Cylinder::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -36,9 +36,8 @@ Group::Group(const char *name) : Shape(Shape::GROUP)
}
}
Intersect Group::intersect(Ray r)
void Group::intersect(Ray &r, Intersect &xs)
{
Intersect ret;
int i, j;
if (this->objectCount > 0)
{
@@ -46,14 +45,7 @@ Intersect Group::intersect(Ray 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]);
}
}
this->objectList[i]->intersect(r, xs);
}
}
}
@@ -63,17 +55,9 @@ Intersect Group::intersect(Ray r)
{
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++)
{
ret.add(xs[j]);
}
}
this->unboxableObjectList[i]->intersect(r, xs);
}
}
return ret;
}
bool Group::includes(Shape *b)
@@ -104,9 +88,9 @@ bool Group::includes(Shape *b)
return false;
}
Intersect Group::localIntersect(Ray r)
void Group::localIntersect(Ray r, Intersect &xs)
{
return Intersect();
this->intersect(r, xs);
}
Tuple Group::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -160,9 +160,9 @@ Group *OBJFile::groups(const char *groupName)
return nullptr;
}
Intersect OBJFile::intersect(Ray r)
void OBJFile::intersect(Ray &r, Intersect &xs)
{
return this->baseGroup->intersect(r);
this->baseGroup->intersect(r, xs);
}
bool OBJFile::includes(Shape *b)
@@ -170,9 +170,9 @@ bool OBJFile::includes(Shape *b)
return this->baseGroup->includes(b);
}
Intersect OBJFile::localIntersect(Ray r)
void OBJFile::localIntersect(Ray r, Intersect &xs)
{
return Intersect();
this->intersect(r, xs);
}
Tuple OBJFile::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -12,22 +12,20 @@
#include <plane.h>
#include <math_helper.h>
Intersect Plane::localIntersect(Ray r)
void Plane::localIntersect(Ray r, Intersect &xs)
{
double t;
Intersect ret = Intersect();
if (fabs(r.direction.y) < getEpsilon())
{
/* With a direction == 0, the ray can't intersect the plane */
return ret;
}
else
{
t = -r.origin.y / r.direction.y;
t = -r.origin.y / r.direction.y;
ret.add(Intersection(t, this));
return ret;
xs.add(Intersection(t, this));
}
}
Tuple Plane::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -25,9 +25,9 @@ Shape::Shape(ShapeType type)
this->updateTransform();
}
Intersect Shape::intersect(Ray r)
void Shape::intersect(Ray &r, Intersect &xs)
{
return this->localIntersect(this->invTransform(r));
this->localIntersect(this->invTransform(r), xs);
};
Tuple Shape::normalToWorld(Tuple normalVector)

View File

@@ -13,9 +13,8 @@
#include <tuple.h>
#include <intersect.h>
Intersect Sphere::localIntersect(Ray r)
void Sphere::localIntersect(Ray r, Intersect &xs)
{
Intersect ret;
double a, b, c, discriminant;
Tuple sphere_to_ray = r.origin - Point(0, 0, 0);
@@ -28,11 +27,9 @@ Intersect Sphere::localIntersect(Ray r)
if (discriminant >= 0)
{
ret.add(Intersection((-b - sqrt(discriminant)) / (2 * a), this));
ret.add(Intersection((-b + sqrt(discriminant)) / (2 * a), this));
xs.add(Intersection((-b - sqrt(discriminant)) / (2 * a), this));
xs.add(Intersection((-b + sqrt(discriminant)) / (2 * a), this));
}
return ret;
}
Tuple Sphere::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -13,10 +13,9 @@ TestShape::TestShape() : localRay(Point(0, 0, 0), Vector(0, 0, 0))
{
}
Intersect TestShape::localIntersect(Ray r)
void TestShape::localIntersect(Ray r, Intersect &xs)
{
this->localRay = r;
return Intersect();
}
Tuple TestShape::localNormalAt(Tuple point, Intersection *hit)

View File

@@ -21,15 +21,13 @@ Triangle::Triangle(Point p1, Point p2, Point p3) : Shape(Shape::TRIANGLE), p1(p1
this->normal = e2.cross(e1).normalise();
}
Intersect Triangle::localIntersect(Ray r)
void Triangle::localIntersect(Ray r, Intersect &xs)
{
Intersect ret;
Tuple dirCrossE2 = r.direction.cross(this->e2);
double determinant = this->e1.dot(dirCrossE2);
if (fabs(determinant) < getEpsilon())
{
return ret;
return;
}
double f = 1.0 / determinant;
@@ -40,18 +38,16 @@ Intersect Triangle::localIntersect(Ray r)
if ((u < 0) || (u > 1))
{
return ret;
return;
}
if ((v < 0) || ((u + v) > 1))
{
return ret;
return;
}
double t = f * this->e2.dot(originCrossE1);
ret.add(Intersection(t, this, u, v));
return ret;
xs.add(Intersection(t, this, u, v));
}
Tuple Triangle::localNormalAt(Tuple point, Intersection *hit)