X-Git-Url: https://git.rohieb.name/MicroTrace.git/blobdiff_plain/0e3446ceb6fd6db0cb292671f37b46daaa2aed5b..df8007c9ec4440ac2388c7a4971e708c2b697832:/Sphere.cxx diff --git a/Sphere.cxx b/Sphere.cxx index 3c19afa..137ce8f 100644 --- a/Sphere.cxx +++ b/Sphere.cxx @@ -1,7 +1,7 @@ #include "Sphere.hxx" Sphere::Sphere(const Vec3f& center, float radius, Shader* shader) - : Primitive(shader), + : Primitive(shader), m_center(center), m_radius(radius) { @@ -20,24 +20,47 @@ Sphere::Intersect(Ray& ray) if( B*B-4*A*C < 0 ) return false; - + float root = sqrtf(B*B-4*A*C); float t = (-B-root)/(2.0f*A); if(t > ray.t()) return false; - - if( t < 1e-6 ) + + if( t < Epsilon ) { t = (-B+root)/(2.0f*A); - if( t < 1e-6 || t > ray.t()) + if( t < Epsilon || t > ray.t()) return false; } + ray.setT(t); + ray.setHit(this); + return true; } Vec3f Sphere::GetNormal(Ray& ray) { - return Vec3f(); + Vec3f p = ray.origin()+ray.direction()*ray.t(); + Vec3f N = (p-m_center); + N.normalize(); + + return N; +} + +Box +Sphere::CalcBounds() +{ + Vec3f min(m_center[0] - m_radius/2, m_center[1] - m_radius/2, + m_center[2] - m_radius/2); + Vec3f max(m_center[0] + m_radius/2, m_center[1] + m_radius/2, + m_center[2] + m_radius/2); + return Box(min, max); +} + +bool +Sphere::InVoxel(const Box& box) +{ + return CalcBounds().Overlaps(box); }