From d0d667d6d2b3833ad72fee93776d55d07141b58f Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 9 Aug 2014 09:59:00 -0700 Subject: [PATCH] Object::Intersect A new intersect method that converts to object space before doing the intersection. Now all objects are at their own origin! --- src/object.cc | 34 ++++++++++++++++++++++++++++++++++ src/object.h | 17 ++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/object.cc b/src/object.cc index 22a173f..3b6ce8c 100644 --- a/src/object.cc +++ b/src/object.cc @@ -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 diff --git a/src/object.h b/src/object.h index c51f908..c0d8071 100644 --- a/src/object.h +++ b/src/object.h @@ -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;