fart/shapes/Subtract.cc
Josh Holtrop 60b29d658d modified shapes and Scene to return surface normal in the intersect object
git-svn-id: svn://anubis/fart/branches/2009-03-09_intersect_returning_normals@200 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2009-03-09 23:17:31 +00:00

58 lines
1.7 KiB
C++

#include "Subtract.h"
#include <iostream>
using namespace std;
Subtract::Subtract(refptr<Shape> shape1, refptr<Shape> shape2)
{
m_shape1 = shape1;
m_shape2 = shape2;
}
Shape::IntersectionList Subtract::intersect(refptr<Shape> _this, const Ray & ray)
{
IntersectionList res1 = m_shape1->intersect(m_shape1, ray);
IntersectionList res2 = m_shape2->intersect(m_shape2, ray);
BoolIntersectionList merged(res1, res2, ray.getOrigin());
IntersectionList res;
bool in1 = false, in2 = false;
bool in_bool = false;
for (int i = 0, sz = merged.size(); i < sz; i++)
{
Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal);
bool front = dot > 0.0;
bool left = merged[i].left;
if (left)
in1 = front;
else
in2 = front;
if (!in_bool && in1 && !in2)
{
/* we found an intersection point
* to get into the boolean object */
in_bool = true;
BoolIntersection bi = merged[i];
Intersection i = bi.intersection;
if ( ! left ) /* if this point came from object B (A - B) */
i.normal = - i.normal;
res.add(i);
}
else if (in_bool && !(in1 && !in2))
{
/* we found an intersection point
* to get out of the boolean object */
BoolIntersection bi = merged[i];
Intersection i = bi.intersection;
if ( ! left ) /* if this point came from object B (A - B) */
i.normal = - i.normal;
res.add(i);
in_bool = false;
}
}
return res;
}