git-svn-id: svn://anubis/fart/branches/2009-02-23_Shape_IntersectList_Update@144 7f9b0f55-74a9-4bce-be96-3c2cd072584d
49 lines
1.4 KiB
C++
49 lines
1.4 KiB
C++
|
|
#include "Sphere.h"
|
|
#include "util/Solver.h"
|
|
#include <math.h>
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
Sphere::Sphere(double radius)
|
|
{
|
|
m_radius = radius;
|
|
m_radius2 = radius * radius;
|
|
}
|
|
|
|
Shape::IntersectionList Sphere::intersect(refptr<Shape> _this, const Ray & ray)
|
|
{
|
|
Ray ray_inv = m_inverse.transform_ray(ray);
|
|
|
|
IntersectionList res;
|
|
QuadraticSolver solver(1.0,
|
|
2 * ( ray_inv.getOrigin()[0] * ray_inv.getDirection()[0]
|
|
+ ray_inv.getOrigin()[1] * ray_inv.getDirection()[1]
|
|
+ ray_inv.getOrigin()[2] * ray_inv.getDirection()[2] ),
|
|
ray_inv.getOrigin()[0] * ray_inv.getOrigin()[0]
|
|
+ ray_inv.getOrigin()[1] * ray_inv.getOrigin()[1]
|
|
+ ray_inv.getOrigin()[2] * ray_inv.getOrigin()[2]
|
|
- m_radius2);
|
|
Solver::Result quadSolutions = solver.solve();
|
|
for (int i = 0; i < quadSolutions.numResults; i++)
|
|
{
|
|
if (quadSolutions.results[i] >= 0.0)
|
|
{
|
|
Vector isect_point = ray_inv[quadSolutions.results[i]];
|
|
res.push_back(
|
|
Intersection(_this, m_transform.transform_point(isect_point))
|
|
);
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
Vector Sphere::getNormalAt(const Vector & pt)
|
|
{
|
|
Vector normal = m_inverse.transform_point(pt);
|
|
|
|
normal.normalize();
|
|
|
|
return m_transform.transform_normal(normal);
|
|
}
|