#include "sphere.h" RayIntersect Sphere::CalcRayIntersect(Ray ray) { RayIntersect ri; ri.E = 0; ri.param_t = -1.0; vector3d o_c = ray.o - c; double A = ray.d * ray.d; double B_2 = ray.d * o_c; double C = o_c * o_c - r * r; double D = B_2 * B_2 - A * C; if(D <= 0.0) return ri; // Does not intersect D = sqrt(D); double t = -B_2 - D; if(t <= 0) // Behind ray { t = B_2 - D; if(t <= 0) return ri; // This one is behind the ray too. } ri.E = this; ri.param_t = t / A; return ri; } vector3d Sphere::CalcNormal(Ray ray, RayIntersect&ri) { return (ray.o + ray.d * ri.param_t - c).ret_unit(); } int Sphere::IntersectionTest(Ray ray) { vector3d o_c = ray.o - c; double A = ray.d * ray.d; double B_2 = ray.d * o_c; double C = o_c * o_c - r * r; double D = B_2 * B_2 - A * C; // no intersection if(D <= 0) return 0; // intersects behind me if(B_2 > 0 || B_2*B_2 < D + 1e-10) return 0; return 1; }