code for assignment 2
[MicroTrace.git] / Sphere.cxx
diff --git a/Sphere.cxx b/Sphere.cxx
new file mode 100644 (file)
index 0000000..3c19afa
--- /dev/null
@@ -0,0 +1,43 @@
+#include "Sphere.hxx"
+
+Sphere::Sphere(const Vec3f& center, float radius, Shader* shader)
+  : Primitive(shader), 
+    m_center(center),
+    m_radius(radius)
+{
+}
+
+Sphere::~Sphere()
+{
+}
+
+bool
+Sphere::Intersect(Ray& ray)
+{
+  float A = ray.direction().dot(ray.direction());
+  float C = (ray.origin()-m_center).dot(ray.origin()-m_center) - m_radius*m_radius;
+  float B = 2 * ray.direction().dot(ray.origin()-m_center);
+
+  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);
+      if( t < 1e-6 || t > ray.t())
+       return false;
+    }
+  ray.setT(t);
+  return true;
+}
+
+Vec3f
+Sphere::GetNormal(Ray& ray)
+{
+  return Vec3f();
+}
This page took 0.023957 seconds and 4 git commands to generate.