2013-09-07 16:09:19 -07:00
|
|
|
/* object.c
|
|
|
|
*
|
2013-09-10 15:45:13 -07:00
|
|
|
* Definition of generic scene objects.
|
2013-09-07 16:09:19 -07:00
|
|
|
*
|
|
|
|
* Eryn Wells <eryn@erynwells.me>
|
|
|
|
*/
|
|
|
|
|
2013-09-10 09:33:59 -07:00
|
|
|
#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"
|
|
|
|
|
2013-09-10 15:45:13 -07:00
|
|
|
#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()
|
2013-09-10 15:45:13 -07:00
|
|
|
: 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()
|
2013-09-10 15:45:13 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-10 15:45:13 -07:00
|
|
|
#pragma mark - Shapes
|
|
|
|
|
2013-09-07 18:26:32 -07:00
|
|
|
/*
|
2013-09-10 15:45:13 -07:00
|
|
|
* Shape::Shape --
|
2013-09-07 18:26:32 -07:00
|
|
|
*
|
2013-09-10 15:45:13 -07:00
|
|
|
* Default constructor. Create a new Shape with an origin at (0, 0, 0).
|
2013-09-07 18:26:32 -07:00
|
|
|
*/
|
2013-09-10 15:45:13 -07:00
|
|
|
Shape::Shape()
|
|
|
|
: Object()
|
|
|
|
{ }
|
2013-09-07 18:26:32 -07:00
|
|
|
|
2013-09-09 08:52:14 -07:00
|
|
|
|
2013-09-10 15:45:13 -07:00
|
|
|
/*
|
|
|
|
* Shape::Shape --
|
|
|
|
*
|
|
|
|
* Constructor. Create a new Shape with an origin at o.
|
|
|
|
*/
|
|
|
|
Shape::Shape(Vector3 o)
|
|
|
|
: Object(o)
|
|
|
|
{ }
|
2013-09-09 08:52:14 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* sphere_point_lies_on_surface --
|
|
|
|
*
|
|
|
|
* Determine if a point lies on the given sphere.
|
|
|
|
*/
|
2013-09-10 15:45:13 -07:00
|
|
|
#if 0
|
2013-09-09 08:52:14 -07:00
|
|
|
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);
|
|
|
|
}
|
2013-09-10 15:45:13 -07:00
|
|
|
#endif
|
2013-09-09 08:52:14 -07:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-09-10 15:45:13 -07:00
|
|
|
#if 0
|
2013-09-09 08:52:14 -07:00
|
|
|
/* 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;
|
|
|
|
}
|
2013-09-10 15:45:13 -07:00
|
|
|
#endif
|