#include <sphere.h>
Inheritance diagram for Sphere:
Public Member Functions | |
virtual bool | collides_with (const Ray &ray, double &t) const |
virtual Ray | get_normal (const Point3D &p) const |
Sphere (Point3D center, double r) | |
Create a sphere. | |
virtual | ~Sphere () |
Clean up the sphere's resources. | |
Protected Attributes | |
double | m_radius |
The radius of the sphere. | |
Static Protected Attributes | |
static StaticInit | m_init |
Force static initialization. | |
Classes | |
class | StaticInit |
Fake a static initializer. More... |
Definition at line 22 of file sphere.h.
Sphere::Sphere | ( | Point3D | center, | |
double | r | |||
) | [inline] |
Create a sphere.
Definition at line 44 of file sphere.h.
References log_debug, Renderable::m_center, Point3D::x, Point3D::y, and Point3D::z.
Sphere::~Sphere | ( | ) | [virtual] |
bool Sphere::collides_with | ( | const Ray & | ray, | |
double & | t | |||
) | const [virtual] |
Determine if a ray intersects this object.
Any point along a ray:
Where is the point along the ray, is the ray's origin, is the ray's direction and is how far down the ray the point is.
The equation of a sphere is:
Where is any point on the sphere's surface, is the sphere's m_center, and is the sphere's radius.
We need to find all points where the ray intersects the sphere. That is we need to find all places where the ray is on the surface of the sphere, so we substitute for , yielding:
We can safely drop the absolute value, since it will be squared anyway. Simplifying, we arrive at:
Further simplifying:
Arranging in a form more suitable for the quadratic equation:
Solving for using the quadratic equation :
It should be noted , , and are all dot products, which yield a single scalar value. Accordingly:
Finally, The equation for is:
Which can be further simplified to:
We want to keep performance high, so we can take some shortcuts. For example, we can disregard all non-real numbers. This means that we can stop processing the equation when is less than , since the square root of a negative number is not a real number.
ray | The ray we are testing to determine collision. | |
t | The z-depth where the ray collides with this sphere. |
Implements Renderable.
Definition at line 20 of file sphere.cpp.
References Ray::direction(), dot_product(), Renderable::m_center, m_radius, and Ray::origin().
Here is the call graph for this function:
Return the surface normal of the sphere. A sphere's surface normal is the vector from the center of the sphere to the intersection point, translated to the intersection point and normalized.
Implements Renderable.
Definition at line 46 of file sphere.cpp.
References Renderable::m_center.
Sphere::StaticInit Sphere::m_init [static, protected] |
double Sphere::m_radius [protected] |