Object::Intersect

A new intersect method that converts to object space before doing the intersection. Now all objects are at their own origin!
This commit is contained in:
Eryn Wells 2014-08-09 09:59:00 -07:00
parent 519eb347d1
commit d0d667d6d2
2 changed files with 50 additions and 1 deletions

View file

@ -22,6 +22,9 @@ namespace charles {
#pragma mark - Objects
/*
* charles::Object::Object --
*/
Object::Object(const Vector4& origin)
: mTranslation(basics::TranslationMatrix(origin.X(), origin.Y(), origin.Z())),
mMaterial()
@ -49,6 +52,37 @@ Object::SetMaterial(const Material& material)
}
/*
* charles::Object::Intersect --
*/
bool
Object::Intersect(const basics::Ray& ray,
TVector& t,
Stats& stats)
const
{
/* TODO: Remove basics:: when the old Ray class goes away. */
basics::Ray objRay = ToObjectSpace(ray);
return DoIntersect(objRay, t, stats);
}
/*
* charles::Object::ToObjectSpace --
*/
/* TODO: Remove basics:: when the old Ray class goes away. */
basics::Ray
Object::ToObjectSpace(const basics::Ray& ray)
const
{
/* TODO: Remove basics:: when the old Ray class goes away. */
basics::Ray objRay(ray);
objRay.origin = mTranslation * objRay.origin;
objRay.direction = mTranslation * objRay.direction;
return objRay;
}
void
Object::Write(std::ostream& ost)
const

View file

@ -37,7 +37,15 @@ struct Object
void SetMaterial(const Material& material);
/**
* Determines if the given ray intersects with this object. All intersection
* Determine if the given ray intersects with this object. Converts the
* ray's origin and direction to object space before calling the protected
* Object::DoIntersect method. All intersection t values are returned in the
* `t` argument, in ascending order.
*/
bool Intersect(const basics::Ray& ray, TVector& t, Stats& stats) const;
/**
* Determine if the given ray intersects with this object. All intersection
* t values are returned in the `t` argument, in increasing order.
*
* @param [in] ray The ray to test for intersection
@ -50,7 +58,14 @@ struct Object
virtual void Write(std::ostream& ost) const;
protected:
virtual bool DoIntersect(const basics::Ray& ray, TVector& t, Stats& stats) const;
private:
basics::Ray ToObjectSpace(const basics::Ray& ray) const;
/** A translation matrix from global coordinates to this object's space. */
basics::Matrix4 mTranslation;
/** This object's material, surface properties, etc. */
Material mMaterial;