#include "dxstdafx.h" #include "Sphere.hpp" bool Sphere::intersectBackSide (const Ray& ray, float& depth, float rayMin, float rayMax) { lastTestedRayId = ray.id; lastTestedRayResult.isIntersect = false; Vector diff = (ray.origin - centre); float b = 2.0f * (ray.dir * diff); float c = diff * diff - radius * radius; float discriminant = b * b - 4.0f * c; if(discriminant < 0.0f) return false; else { depth = (-b + sqrtf(discriminant)) * 0.5f; lastTestedRayResult.isIntersect = true; lastTestedRayResult.depth = depth; lastTestedRayResult.material = this->material; if(ray.isShadowRay) return true; lastTestedRayResult.point = ray.origin + ray.dir * depth; lastTestedRayResult.normal.setDifference(lastTestedRayResult.point, centre); lastTestedRayResult.normal *= 1.0f / radius; lastTestedRayResult.object = this; return true; } } bool Sphere::intersect (const Ray& ray, float& depth, float rayMin, float rayMax) { lastTestedRayId = ray.id; lastTestedRayResult.isIntersect = false; Vector diff = (ray.origin - centre); float b = 2.0f * (ray.dir * diff); float c = diff * diff - radius * radius; float discriminant = b * b - 4.0f * c; if(discriminant < 0.0f) return false; else { depth = (-b - sqrtf(discriminant)) * 0.5f; lastTestedRayResult.isIntersect = true; lastTestedRayResult.depth = depth; lastTestedRayResult.material = this->material; if(ray.isShadowRay) return true; lastTestedRayResult.point = ray.origin + ray.dir * depth; lastTestedRayResult.normal.setDifference(lastTestedRayResult.point, centre); lastTestedRayResult.normal *= 1.0f / radius; lastTestedRayResult.object = this; return true; } }