m_t = std::numeric_limits<float>::max();
}
-Ray::Ray(const Vec3f& org,
+Ray::Ray(const Vec3f& org,
const Vec3f& dir)
: m_org(org),
m_dir(dir),
m_t = t;
}
+void
+Ray::setHit(Primitive * p)
+{
+ m_hit = p;
+}
+
Primitive*
Ray::hit()
{
return m_hit;
}
-unsigned int
+unsigned int
Ray::recursionDepth() const
{
return m_level;
~Ray();
Ray(const Ray& r);
Ray& operator=(const Ray& r);
-
+
const Vec3f& origin() const;
const Vec3f& direction() const;
float t() const;
+ void setHit(Primitive * p);
Primitive* hit();
unsigned int recursionDepth() const;
Vec3f m_org; //!< ray origin
Vec3f m_dir; //!< ray direction
float m_t; //!< current/maximum hit distance
-
+
unsigned int m_level; //!< level of recursion
-
+
Primitive* m_hit;
};
#include "Scene.hxx"
#include "PerspectiveCamera.hxx"
+#include <limits>
Scene::Scene()
- : m_camera(new PerspectiveCamera(Vec3f(0,0,8),
- Vec3f(0,0,-1),
- Vec3f(0,1,0),
- 50,
- 640,
+ : m_camera(new PerspectiveCamera(Vec3f(0,0,8),
+ Vec3f(0,0,-1),
+ Vec3f(0,1,0),
+ 50,
+ 640,
480)
),
m_bgColor(Vec3f(0,0,0))
return *this;
}
-void
+void
Scene::Add(Primitive* p)
{
m_primitives.push_back(p);
Scene::Intersect(Ray& ray)
{
bool hit = false;
- for( std::vector<Primitive*>::iterator i = m_primitives.begin();
- i != m_primitives.end(); i++ ) {
- hit |= (*i)->Intersect(ray);
+ float t = std::numeric_limits<float>::max();
+ for (std::vector<Primitive*>::iterator i = m_primitives.begin();
+ i != m_primitives.end(); i++) {
+ // store closest object hit
+ if (hit |= (*i)->Intersect(ray)) {
+ if (ray.t() < t) {
+ ray.setHit(*i);
+ t = ray.t();
+ }
+ }
}
return hit;
}
Vec3f
Scene::RayTrace(Ray& ray)
{
- return (Intersect(ray)) ? Vec3f(255,255,255) : Vec3f(0,0,0);
+ if (Intersect(ray)) {
+ return ray.hit()->shader()->Shade(ray);
+ } else {
+ return Vec3f(0,0,0);
+ }
}
-const Camera*
+const Camera*
Scene::camera() const
{
return m_camera;