47 lines
1.4 KiB
C++
47 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::IntersectList Sphere::intersect(const Ray & ray)
|
|
{
|
|
Ray ray_inv = m_inverse.transform_ray(ray);
|
|
|
|
IntersectList 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)
|
|
{
|
|
res.push_back(m_transform.transform_point(
|
|
ray_inv[quadSolutions.results[i]]));
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
Vector Sphere::getNormalAt(const Vector & pt)
|
|
{
|
|
Vector normal = m_inverse.transform_point(pt);
|
|
|
|
normal.normalize();
|
|
|
|
return m_transform.transform_normal(normal);
|
|
}
|