From e5601e7f431c1efec648cce15fd00136de37ccda Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 10 Sep 2013 15:45:13 -0700 Subject: [PATCH] Define constructors for Object and Shape; move a bunch of Sphere code to object_sphere --- src/object.cc | 101 +++++++++++++++++--------------------------------- src/object.h | 6 ++- 2 files changed, 38 insertions(+), 69 deletions(-) diff --git a/src/object.cc b/src/object.cc index c7c0c76..5aff30f 100644 --- a/src/object.cc +++ b/src/object.cc @@ -1,11 +1,10 @@ /* object.c * - * Definition of scene Objects. + * Definition of generic scene objects. * * Eryn Wells */ - #include #include #include @@ -13,11 +12,7 @@ #include "basics.h" #include "object.h" - -static int sphere_does_intersect(Object *obj, Ray ray, float **t); -static int sphere_point_lies_on_surface(Object *obj, Vector3 p); -static Vector3 sphere_compute_normal(Object *obj, Vector3 p); - +#pragma mark - Objects /* * Object::Object -- @@ -25,7 +20,17 @@ static Vector3 sphere_compute_normal(Object *obj, Vector3 p); * Default constructor. Create a new Object with an origin at (0, 0, 0). */ Object::Object() - : origin() + : Object(Vector3::Zero) +{ } + + +/* + * Object::Object -- + * + * Constructor. Create a new Object with an origin at o. + */ +Object::Object(Vector3 o) + : origin(o) { } @@ -37,6 +42,7 @@ Object::Object() */ Vector3 Object::get_origin() + const { return origin; } @@ -47,77 +53,33 @@ Object::set_origin(Vector3 v) origin = v; } +#pragma mark - Shapes + /* - * Sphere::does_intersect -- + * Shape::Shape -- * - * Compute the intersection of a ray with this Sphere. All intersection t values are returned in the **t argument. The - * number of values returned therein is indicated by the return value. Memory is allocated at *t. It is the caller's - * responsibility to free it when it is no longer needed. If 0 is returned, no memory needs to be freed. + * Default constructor. Create a new Shape with an origin at (0, 0, 0). */ -int -Sphere::does_intersect(const Ray &ray, float **t) -{ - // Origin of the vector in object space. - Vector3 ray_origin_obj = ray.origin - get_origin(); +Shape::Shape() + : Object() +{ } - // Coefficients for quadratic equation. - float a = ray.direction.dot(ray.direction); - float b = ray.direction.dot(ray_origin_obj) * 2.0; - float c = ray_origin_obj.dot(ray_origin_obj) - (radius * radius); - - // Discriminant for the quadratic equation. - float discrim = (b * b) - (4.0 * a * c); - - // If the discriminant is less than zero, there are no real (as in not imaginary) solutions to this intersection. - if (discrim < 0) { - return 0; - } - - // Compute the intersections, the roots of the quadratic equation. Spheres have at most two intersections. - float sqrt_discrim = sqrtf(discrim); - float t0 = (-b - sqrt_discrim) / (2.0 * a); - float t1 = (-b + sqrt_discrim) / (2.0 * a); - - // If t[1] is less than t[0], swap them (t[0] will always be the first intersection). - if (t1 < t0) { - float tmp = t0; - t0 = t1; - t1 = tmp; - } - - /* - * If the farther intersection of the two is in the negative direction, the sphere is in the ray's negative - * direction. - */ - if (t1 < 0) { - return 0; - } - - /* - * Allocate the memory and store the values. It's possible the two values are equal. Only allocate enough memory to - * store the required number of values. - */ - int nints = (t0 != t1) ? 2 : 1; - if (t != NULL) { - *t = malloc(sizeof(float) * nints); - if (*t == NULL) { - return 0; - } - (*t)[0] = t0; - if (nints > 1) { - (*t)[1] = t1; - } - } - - return nints; -} +/* + * Shape::Shape -- + * + * Constructor. Create a new Shape with an origin at o. + */ +Shape::Shape(Vector3 o) + : Object(o) +{ } /* * sphere_point_lies_on_surface -- * * Determine if a point lies on the given sphere. */ +#if 0 int sphere_point_lies_on_surface(Object *obj, Vector3 p) { @@ -131,6 +93,7 @@ sphere_point_lies_on_surface(Object *obj, Vector3 p) return (x * x) + (y * y) + (z * z) == (r * r); } +#endif /* @@ -139,6 +102,7 @@ sphere_point_lies_on_surface(Object *obj, Vector3 p) * Compute the normal for the given Object (which must be a Sphere) at the given point. This point must lie on the * surface of the object. */ +#if 0 /* static */ Vector3 sphere_compute_normal(Object *obj, Vector3 p) { @@ -152,3 +116,4 @@ sphere_compute_normal(Object *obj, Vector3 p) // The fun thing about sphere is the normal to any point on the sphere is the point itself. Woo! return p; } +#endif diff --git a/src/object.h b/src/object.h index 874958f..eb2cec8 100644 --- a/src/object.h +++ b/src/object.h @@ -17,8 +17,9 @@ class Object { public: Object(); + Object(Vector3 o); - Vector3 get_origin(); + Vector3 get_origin() const; void set_origin(Vector3 v); private: @@ -30,6 +31,9 @@ class Shape : public Object { public: + Shape(); + Shape(Vector3 o); + virtual int does_intersect(const Ray &ray, float **t) = 0; virtual Vector3 compute_normal(const Vector3 &p) = 0; };