#include "Sphere.hxx"
Sphere::Sphere(const Vec3f& center, float radius, Shader* shader)
- : Primitive(shader),
+ : Primitive(shader),
m_center(center),
m_radius(radius)
{
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);
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
+ Intersect(tempRay);
+ // intersection
+ Vec3f i = tempRay.origin() + tempRay.direction() * tempRay.t();
+ // normal
+ Vec3f n = (i - m_center);
+ n.normalize();
+ return n;
}