bool sub-objects using relative coordinates, shape definitions using relative coordinates, this allows transform blocks in each, and shape references to combine absolute transform with the relative transform

git-svn-id: svn://anubis/fart/trunk@287 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2010-07-09 19:37:05 +00:00
parent b4a2c8ef8e
commit b0dfa2ee1f
7 changed files with 46 additions and 30 deletions

View File

@ -532,6 +532,7 @@ refptr<Shape> Scene::processBool(refptr<Node> node)
refptr<Material> material; refptr<Material> material;
bool restore_transform = processTransforms(node); bool restore_transform = processTransforms(node);
m_transforms.push(Transform());
for (Node_Iterator it = node->getChildren().begin(); for (Node_Iterator it = node->getChildren().begin();
it != node->getChildren().end(); it != node->getChildren().end();
@ -559,11 +560,6 @@ refptr<Shape> Scene::processBool(refptr<Node> node)
{ {
processShapeDefinition(*it); processShapeDefinition(*it);
} }
else
{
cerr << "Error: unknown boolean object item" << endl;
exit(3);
}
} }
if (shapes.size() < 2) if (shapes.size() < 2)
@ -589,8 +585,9 @@ refptr<Shape> Scene::processBool(refptr<Node> node)
if ( ! material.isNull() ) if ( ! material.isNull() )
shape->setMaterial(material); shape->setMaterial(material);
shape->setTransform(m_transforms.top());
m_transforms.pop();
shape->setTransform(m_transforms.top());
if (restore_transform) if (restore_transform)
m_transforms.pop(); m_transforms.pop();
@ -715,7 +712,7 @@ refptr<Shape> Scene::processShapeRef(refptr<Node> node)
!= m_shape_definitions.end()) != m_shape_definitions.end())
{ {
refptr<Shape> shape = m_shape_definitions[node->getString()]->clone(); refptr<Shape> shape = m_shape_definitions[node->getString()]->clone();
shape->setTransform(m_transforms.top()); shape->setTransform(m_transforms.top() * shape->getTransform());
return shape; return shape;
} }
cerr << "Error: no shape definition for '" << node->getString() cerr << "Error: no shape definition for '" << node->getString()
@ -766,6 +763,8 @@ bool Scene::processTransforms(refptr<Node> node)
void Scene::processShapeDefinition(refptr<Node> node) void Scene::processShapeDefinition(refptr<Node> node)
{ {
m_transforms.push(Transform());
m_shape_definitions[node->getString()] m_shape_definitions[node->getString()]
= processShape(node->getChildren()[0]); = processShape(node->getChildren()[0]);
m_transforms.pop();
} }

View File

@ -31,9 +31,10 @@ Intersect::Intersect(const vector< refptr<Shape> > & shapes)
Shape::IntersectionList Intersect::intersect(refptr<Shape> _this, const Ray & ray) Shape::IntersectionList Intersect::intersect(refptr<Shape> _this, const Ray & ray)
{ {
IntersectionList res1 = m_shape1->intersect(m_shape1, ray); Ray ray_inv = m_inverse.transform_ray(ray);
IntersectionList res2 = m_shape2->intersect(m_shape2, ray); IntersectionList res1 = m_shape1->intersect(m_shape1, ray_inv);
BoolIntersectionList merged(res1, res2, ray.getOrigin()); IntersectionList res2 = m_shape2->intersect(m_shape2, ray_inv);
BoolIntersectionList merged(res1, res2, ray_inv.getOrigin());
IntersectionList res; IntersectionList res;
bool in1 = false, in2 = false; bool in1 = false, in2 = false;
@ -45,7 +46,7 @@ Shape::IntersectionList Intersect::intersect(refptr<Shape> _this, const Ray & ra
i++) i++)
{ {
Vector normal = merged[i].intersection.normal; Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal); double dot = - (ray_inv.getDirection() % normal);
bool back = dot < 0.0; bool back = dot < 0.0;
bool left = merged[i].left; bool left = merged[i].left;
if (back) if (back)
@ -65,7 +66,7 @@ Shape::IntersectionList Intersect::intersect(refptr<Shape> _this, const Ray & ra
for (int i = 0, sz = merged.size(); i < sz; i++) for (int i = 0, sz = merged.size(); i < sz; i++)
{ {
Vector normal = merged[i].intersection.normal; Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal); double dot = - (ray_inv.getDirection() % normal);
bool front = dot > 0.0; bool front = dot > 0.0;
bool left = merged[i].left; bool left = merged[i].left;
if (front) if (front)
@ -78,7 +79,7 @@ Shape::IntersectionList Intersect::intersect(refptr<Shape> _this, const Ray & ra
{ {
/* we found an intersection point with the boolean object */ /* we found an intersection point with the boolean object */
in_bool = true; in_bool = true;
res.add( merged[i].intersection ); res.add( merged[i].intersection.transform(m_transform) );
} }
} }
else else
@ -86,7 +87,7 @@ Shape::IntersectionList Intersect::intersect(refptr<Shape> _this, const Ray & ra
if (in_bool && in1 && in2) if (in_bool && in1 && in2)
{ {
/* we found an intersection point with the boolean object */ /* we found an intersection point with the boolean object */
res.add( merged[i].intersection ); res.add( merged[i].intersection.transform(m_transform) );
} }
if (left) if (left)
in1 = false; in1 = false;

View File

@ -29,6 +29,12 @@ class Shape
refptr<Shape> shape; refptr<Shape> shape;
Vector position; Vector position;
Vector normal; Vector normal;
Intersection transform(Transform & t)
{
return Intersection(shape,
t.transform_point(position),
t.transform_normal(normal));
}
}; };
class BoolIntersection class BoolIntersection
{ {
@ -103,10 +109,10 @@ class Shape
const Ray & ray) = 0; const Ray & ray) = 0;
virtual refptr<Shape> clone() = 0; virtual refptr<Shape> clone() = 0;
void setTransform(Transform & t) void setTransform(const Transform & t)
{ {
m_transform = t; m_transform = t;
m_inverse = t.getInverse(); m_inverse = m_transform.getInverse();
} }
Transform & getTransform() { return m_transform; } Transform & getTransform() { return m_transform; }

View File

@ -31,9 +31,10 @@ Subtract::Subtract(const vector< refptr<Shape> > & shapes)
Shape::IntersectionList Subtract::intersect(refptr<Shape> _this, const Ray & ray) Shape::IntersectionList Subtract::intersect(refptr<Shape> _this, const Ray & ray)
{ {
IntersectionList res1 = m_shape1->intersect(m_shape1, ray); Ray ray_inv = m_inverse.transform_ray(ray);
IntersectionList res2 = m_shape2->intersect(m_shape2, ray); IntersectionList res1 = m_shape1->intersect(m_shape1, ray_inv);
BoolIntersectionList merged(res1, res2, ray.getOrigin()); IntersectionList res2 = m_shape2->intersect(m_shape2, ray_inv);
BoolIntersectionList merged(res1, res2, ray_inv.getOrigin());
IntersectionList res; IntersectionList res;
bool in1 = false, in2 = false; bool in1 = false, in2 = false;
@ -45,7 +46,7 @@ Shape::IntersectionList Subtract::intersect(refptr<Shape> _this, const Ray & ray
i++) i++)
{ {
Vector normal = merged[i].intersection.normal; Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal); double dot = - (ray_inv.getDirection() % normal);
bool back = dot < 0.0; bool back = dot < 0.0;
bool left = merged[i].left; bool left = merged[i].left;
if (back) if (back)
@ -65,7 +66,7 @@ Shape::IntersectionList Subtract::intersect(refptr<Shape> _this, const Ray & ray
for (int i = 0, sz = merged.size(); i < sz; i++) for (int i = 0, sz = merged.size(); i < sz; i++)
{ {
Vector normal = merged[i].intersection.normal; Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal); double dot = - (ray_inv.getDirection() % normal);
bool front = dot > 0.0; bool front = dot > 0.0;
bool left = merged[i].left; bool left = merged[i].left;
if (left) if (left)
@ -81,7 +82,7 @@ Shape::IntersectionList Subtract::intersect(refptr<Shape> _this, const Ray & ray
Intersection i = bi.intersection; Intersection i = bi.intersection;
if ( ! left ) /* if this point came from object B (A - B) */ if ( ! left ) /* if this point came from object B (A - B) */
i.normal = - i.normal; i.normal = - i.normal;
res.add(i); res.add(i.transform(m_transform));
} }
else if (in_bool && !(in1 && !in2)) else if (in_bool && !(in1 && !in2))
{ {
@ -91,7 +92,7 @@ Shape::IntersectionList Subtract::intersect(refptr<Shape> _this, const Ray & ray
Intersection i = bi.intersection; Intersection i = bi.intersection;
if ( ! left ) /* if this point came from object B (A - B) */ if ( ! left ) /* if this point came from object B (A - B) */
i.normal = - i.normal; i.normal = - i.normal;
res.add(i); res.add(i.transform(m_transform));
in_bool = false; in_bool = false;
} }
} }

View File

@ -32,9 +32,10 @@ Union::Union(const vector< refptr<Shape> > & shapes)
Shape::IntersectionList Union::intersect(refptr<Shape> _this, const Ray & ray) Shape::IntersectionList Union::intersect(refptr<Shape> _this, const Ray & ray)
{ {
IntersectionList res1 = m_shape1->intersect(m_shape1, ray); Ray ray_inv = m_inverse.transform_ray(ray);
IntersectionList res2 = m_shape2->intersect(m_shape2, ray); IntersectionList res1 = m_shape1->intersect(m_shape1, ray_inv);
BoolIntersectionList merged(res1, res2, ray.getOrigin()); IntersectionList res2 = m_shape2->intersect(m_shape2, ray_inv);
BoolIntersectionList merged(res1, res2, ray_inv.getOrigin());
IntersectionList res; IntersectionList res;
bool in1 = false, in2 = false; bool in1 = false, in2 = false;
@ -46,7 +47,7 @@ Shape::IntersectionList Union::intersect(refptr<Shape> _this, const Ray & ray)
i++) i++)
{ {
Vector normal = merged[i].intersection.normal; Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal); double dot = - (ray_inv.getDirection() % normal);
bool back = dot < 0.0; bool back = dot < 0.0;
bool left = merged[i].left; bool left = merged[i].left;
if (back) if (back)
@ -66,7 +67,7 @@ Shape::IntersectionList Union::intersect(refptr<Shape> _this, const Ray & ray)
for (int i = 0, sz = merged.size(); i < sz; i++) for (int i = 0, sz = merged.size(); i < sz; i++)
{ {
Vector normal = merged[i].intersection.normal; Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal); double dot = - (ray_inv.getDirection() % normal);
bool front = dot > 0.0; bool front = dot > 0.0;
bool left = merged[i].left; bool left = merged[i].left;
if (front) if (front)
@ -79,7 +80,7 @@ Shape::IntersectionList Union::intersect(refptr<Shape> _this, const Ray & ray)
{ {
/* we found an intersection point with the boolean object */ /* we found an intersection point with the boolean object */
in_bool = true; in_bool = true;
res.add( merged[i].intersection ); res.add( merged[i].intersection.transform(m_transform) );
} }
} }
else else
@ -91,7 +92,7 @@ Shape::IntersectionList Union::intersect(refptr<Shape> _this, const Ray & ray)
if (in_bool && !(in1 || in2)) if (in_bool && !(in1 || in2))
{ {
/* we found an intersection point with the boolean object */ /* we found an intersection point with the boolean object */
res.add( merged[i].intersection ); res.add( merged[i].intersection.transform(m_transform) );
in_bool = false; in_bool = false;
} }
} }

View File

@ -104,3 +104,10 @@ Ray Transform::transform_ray(const Ray & r)
Ray res(newPosition, newDirection); Ray res(newPosition, newDirection);
return res; return res;
} }
Transform Transform::operator*(const Transform & other) const
{
Transform t;
t.m_matrix = m_matrix * other.m_matrix;
return t;
}

View File

@ -36,6 +36,7 @@ class Transform
Vector transform_direction(const Vector & v); Vector transform_direction(const Vector & v);
Vector transform_normal(const Vector & v); Vector transform_normal(const Vector & v);
Ray transform_ray(const Ray & r); Ray transform_ray(const Ray & r);
Transform operator*(const Transform & other) const;
protected: protected:
Matrix m_matrix; Matrix m_matrix;