fixed Box::Extend(Vec3f&)
[MicroTrace.git] / PerspectiveCamera.cxx
index b7faff6..e97e956 100644 (file)
@@ -1,3 +1,7 @@
+#include <limits>
+#include <iostream>
+#include <cassert>
+
 #include "PerspectiveCamera.hxx"
 
 PerspectiveCamera::PerspectiveCamera()
@@ -18,6 +22,23 @@ PerspectiveCamera::PerspectiveCamera(const Vec3f& pos,
     m_angle(angle)    
 {
   // preprocess the values and fill the rest of the member variables here
+  m_dir.normalize();
+  m_up.normalize();
+
+  // compute local coordinate system
+  m_zAxis = dir;
+  m_xAxis = dir.cross(up);
+  m_yAxis = m_xAxis.cross(m_zAxis);
+
+  m_xAxis.normalize();
+  m_yAxis.normalize();
+  m_zAxis.normalize();
+
+  m_aspect = resX/static_cast<float>(resY);
+  
+  // derive focal length from opening angle
+  float angle_in_rad = angle * M_PI / 180.0f;
+  m_focus = 1.0 / tan(angle_in_rad * 0.5);
 }
 
 PerspectiveCamera::~PerspectiveCamera()
@@ -32,6 +53,7 @@ PerspectiveCamera::PerspectiveCamera(const PerspectiveCamera& )
 PerspectiveCamera&
 PerspectiveCamera::operator=(const PerspectiveCamera& )
 {
+  assert(!"Not implemented!");
   return *this;
 }
 
@@ -39,8 +61,18 @@ bool
 PerspectiveCamera::InitRay(float x, //!< pixel x-coordinate 
                           float y, //!< pixel y-coordinate
                           Ray& ray //!< should be filled by this function
-                          )
+                          ) const
 {
   // define direction,position and maximum hit distance of Ray here
-  return false;
+  Vec3f dir = m_xAxis * ( 2.0f * ((x/static_cast<float>(m_resX) - 0.5 ) * m_aspect ) )
+    + m_yAxis * ( 2.0f * (y/static_cast<float>(m_resY) - 0.5 ) )
+    + ( m_zAxis * m_focus );
+  dir.normalize();
+  
+  ray.setDir(dir);
+  ray.setOrigin(m_pos);
+  float t = std::numeric_limits<float>::max();
+  ray.setT(t);
+  
+  return true;
 }
This page took 0.028146 seconds and 4 git commands to generate.