X-Git-Url: https://git.rohieb.name/MicroTrace.git/blobdiff_plain/0e3446ceb6fd6db0cb292671f37b46daaa2aed5b..0b29fae9afb220c13cc0c48c91031f0798b91ec7:/Sphere.cxx diff --git a/Sphere.cxx b/Sphere.cxx index 3c19afa..77d4320 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,12 +20,12 @@ 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 ) { t = (-B+root)/(2.0f*A); @@ -39,5 +39,20 @@ Sphere::Intersect(Ray& ray) Vec3f Sphere::GetNormal(Ray& ray) { - return Vec3f(); + // We don't want to modify the ray (probably this is not needed, but I am + // too lazy to think about it now ...) + Ray tempRay = ray; + + // Surface normal is the difference between intersection and center point + if(Intersect(tempRay)) { + // intersection point + Vec3f i = tempRay.origin() + tempRay.direction() * (tempRay.t() - Epsilon); + // normal + Vec3f n = (i - m_center); + n.normalize(); + return n; + } else { + // no intersection with ray, so no surface normal + return Vec3f(0,0,0); + } }