charles/src/object.cc

120 lines
2 KiB
C++
Raw Normal View History

2013-09-07 16:09:19 -07:00
/* object.c
*
* Definition of generic scene objects.
2013-09-07 16:09:19 -07:00
*
* Eryn Wells <eryn@erynwells.me>
*/
#include <cassert>
#include <cmath>
#include <cstdlib>
2013-09-07 22:22:27 -07:00
2013-09-07 16:48:50 -07:00
#include "basics.h"
2013-09-07 16:09:19 -07:00
#include "object.h"
#pragma mark - Objects
2013-09-07 16:48:50 -07:00
/*
2013-09-10 09:18:05 -07:00
* Object::Object --
2013-09-07 16:48:50 -07:00
*
2013-09-10 09:18:05 -07:00
* Default constructor. Create a new Object with an origin at (0, 0, 0).
2013-09-07 16:48:50 -07:00
*/
2013-09-10 09:18:05 -07:00
Object::Object()
: Object(Vector3::Zero)
{ }
/*
* Object::Object --
*
* Constructor. Create a new Object with an origin at o.
*/
Object::Object(Vector3 o)
: origin(o)
2013-09-10 09:18:05 -07:00
{ }
2013-09-07 16:48:50 -07:00
/*
2013-09-10 09:18:05 -07:00
* Object::get_origin --
* Object::set_origin --
2013-09-07 16:48:50 -07:00
*
2013-09-10 09:18:05 -07:00
* Get and set the Object's origin.
2013-09-07 16:48:50 -07:00
*/
Vector3
2013-09-10 09:18:05 -07:00
Object::get_origin()
const
2013-09-07 22:22:27 -07:00
{
2013-09-10 09:18:05 -07:00
return origin;
2013-09-07 22:22:27 -07:00
}
void
2013-09-10 09:18:05 -07:00
Object::set_origin(Vector3 v)
2013-09-07 22:22:27 -07:00
{
2013-09-10 09:18:05 -07:00
origin = v;
2013-09-07 22:22:27 -07:00
}
#pragma mark - Shapes
2013-09-07 18:26:32 -07:00
/*
* Shape::Shape --
2013-09-07 18:26:32 -07:00
*
* Default constructor. Create a new Shape with an origin at (0, 0, 0).
2013-09-07 18:26:32 -07:00
*/
Shape::Shape()
: Object()
{ }
2013-09-07 18:26:32 -07:00
/*
* 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)
{
assert(obj != NULL && obj->type == ObjectTypeSphere);
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);
}
#endif
/*
* 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
* surface of the object.
*/
#if 0
/* static */ Vector3
sphere_compute_normal(Object *obj, Vector3 p)
{
assert(obj != NULL && obj->type == ObjectTypeSphere);
// 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!
return p;
}
#endif