Update object_sphere for C++; inherit from Shape

This commit is contained in:
Eryn Wells 2013-09-10 21:04:31 -07:00
parent df7a8988b2
commit aa694e8128
2 changed files with 13 additions and 15 deletions

View file

@ -8,6 +8,7 @@
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include <cstdio>
#include "basics.h" #include "basics.h"
#include "object.h" #include "object.h"
@ -68,6 +69,7 @@ Sphere::set_radius(float r)
*/ */
int int
Sphere::does_intersect(const Ray &ray, float **t) Sphere::does_intersect(const Ray &ray, float **t)
const
{ {
// Origin of the vector in object space. // Origin of the vector in object space.
Vector3 ray_origin_obj = ray.origin - get_origin(); Vector3 ray_origin_obj = ray.origin - get_origin();
@ -148,23 +150,17 @@ sphere_point_lies_on_surface(Object *obj, Vector3 p)
/* /*
* sphere_compute_normal -- * Sphere::compute_normal --
* *
* Compute the normal for the given Object (which must be a Sphere) at the given point. This point must lie on the * Compute the normal for this Sphere at the given point. If the point does not lie on the surface of the sphere, a zero
* surface of the object. * vector is returned.
*/ */
#if 0 Vector3
/* static */ Vector3 Sphere::compute_normal(const Vector3 &p)
sphere_compute_normal(Object *obj, Vector3 p) const
{ {
assert(obj != NULL && object_get_type(obj) == ObjectTypeSphere); // 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 (!sphere_point_lies_on_surface(obj, p)) {
return Vector3Zero;
}
// The fun thing about sphere is the normal to any point on the sphere is the point itself. Woo! // The fun thing about sphere is the normal to any point on the sphere is the point itself. Woo!
return p; return Vector3::Zero;
} }
#endif

View file

@ -15,6 +15,7 @@
class Sphere class Sphere
: public Shape : public Shape
{ {
public:
Sphere(); Sphere();
Sphere(float r); Sphere(float r);
Sphere(Vector3 o, float r); Sphere(Vector3 o, float r);
@ -22,7 +23,8 @@ class Sphere
float get_radius(); float get_radius();
void set_radius(float r); void set_radius(float r);
int does_intersect(const Ray &ray, float **t); int does_intersect(const Ray &ray, float **t) const;
Vector3 compute_normal(const Vector3 &p) const;
private: private:
float radius; float radius;
}; };