#include "sphere.h" #include using namespace std; RayIntersect Sphere::CalcRayIntersect(Ray ray, int flags, int special) { RayIntersect ri; ri.E = 0; ri.param_t = -1.0; vector3d o_c = ray.o - c; double B_2 = ray.d * o_c; if(B_2 > 0 && (flags & INTERSECT_ONLY)) return ri; double C = o_c * o_c - r * r; double D = B_2 * B_2 - C; if(D <= 0.0) return ri; // Does not intersect // if we only need to know the intersection, we can save time if(flags & INTERSECT_ONLY) { if(B_2*B_2 < D + 1e-10) return ri; ri.E = this; return ri; } D = sqrt(D); double t = -B_2 - D; if(t <= 1e-8) // Behind ray { t = B_2 - D; if(t <= 1e-8) return ri; // This one is behind the ray too. } ri.E = this; ri.param_t = t; 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, int flags, int special) { // assert("depreciated" && 0); vector3d o_c = ray.o - c; double B_2 = ray.d * o_c; if(B_2 > 0) return 0; double C = o_c * o_c - r * r; double D = B_2 * B_2 - C; // no intersection if(D <= 0) return 0; // intersects behind me if(B_2*B_2 < D + 1e-10) return 0; return 1; } Color Sphere::GetTextureColor(Ray ray, RayIntersect& ri) { static double pi = atan(1.0) * 4; if(!texture) return Color(); vector3d v = (ray.o + ray.d * ri.param_t - c); v /= v.mag(); // cout << v << endl; double x = v * unitx; double y = v * unity; double z = v * unitz; double theta = atan2(y, x); // if(y < 0) theta += pi; if(theta < 0) theta += 2 * pi; double phi = acos(-z); long col = texture->GetColor(theta/(2*pi), phi/pi); return Color(col); }