git-svn-id: svn://anubis/fart/branches/2009-03-09_intersect_returning_normals@200 7f9b0f55-74a9-4bce-be96-3c2cd072584d
44 lines
1.4 KiB
C++
44 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]];
|
|
Vector normal = isect_point;
|
|
normal.normalize();
|
|
res.add(
|
|
Intersection(_this,
|
|
m_transform.transform_point(isect_point),
|
|
m_transform.transform_normal(normal))
|
|
);
|
|
}
|
|
}
|
|
return res;
|
|
}
|