From 86a9c74c1ef7597143c6fd2823a2da30e6a5441e Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 10 Sep 2013 22:32:33 -0700 Subject: [PATCH] Activate point_is_on_surface() and calculate_normal() --- src/object.h | 1 + src/object_sphere.cc | 31 +++++++++++++++---------------- src/object_sphere.h | 1 + 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/object.h b/src/object.h index 4be5b5e..40e0d65 100644 --- a/src/object.h +++ b/src/object.h @@ -35,6 +35,7 @@ public: Shape(Vector3 o); virtual int does_intersect(const Ray &ray, float **t) const = 0; + virtual bool point_is_on_surface(const Vector3 &p) const = 0; virtual Vector3 compute_normal(const Vector3 &p) const = 0; }; diff --git a/src/object_sphere.cc b/src/object_sphere.cc index fc94451..5284b86 100644 --- a/src/object_sphere.cc +++ b/src/object_sphere.cc @@ -128,25 +128,21 @@ Sphere::does_intersect(const Ray &ray, float **t) /* - * sphere_point_lies_on_surface -- + * Sphere::point_is_on_surface -- * - * Determine if a point lies on the given sphere. + * Determine if a point lies on the surface of this Sphere. */ -#if 0 -int -sphere_point_lies_on_surface(Object *obj, Vector3 p) +bool +Sphere::point_is_on_surface(const Vector3 &p) + const { - assert(obj != NULL && object_get_type(obj) == ObjectTypeSphere); + Vector3 o = get_origin(); + float x = p.x - o.x; + float y = p.y - o.y; + float z = p.z - o.z; - Vector3 loc = object_get_location(obj); - float x = p.x - loc.x; - float y = p.y - loc.y; - float z = p.z - loc.z; - float r = object_sphere_get_radius(obj); - - return (x * x) + (y * y) + (z * z) == (r * r); + return x*x + y*y + z*z == radius*radius; } -#endif /* @@ -159,8 +155,11 @@ Vector3 Sphere::compute_normal(const Vector3 &p) const { - // TODO: Make sure the given point is actually on the surface of the sphere. + // Make sure the given point is actually on the surface of the sphere. + if (!point_is_on_surface(p)) { + return Vector3::Zero; + } // The fun thing about sphere is the normal to any point on the sphere is the point itself. Woo! - return Vector3::Zero; + return p - get_origin(); } diff --git a/src/object_sphere.h b/src/object_sphere.h index d9e0ac6..3a14160 100644 --- a/src/object_sphere.h +++ b/src/object_sphere.h @@ -24,6 +24,7 @@ public: void set_radius(float r); int does_intersect(const Ray &ray, float **t) const; + bool point_is_on_surface(const Vector3 &p) const; Vector3 compute_normal(const Vector3 &p) const; private: float radius;