X-Git-Url: https://git.rohieb.name/MicroTrace.git/blobdiff_plain/464aa50da8460f3e20b8601750cde04386e23374..430ddbc0e443b60b6d0c7893a96e045411368dbf:/Scene.cxx diff --git a/Scene.cxx b/Scene.cxx index 69ea9ed..4998bd1 100644 --- a/Scene.cxx +++ b/Scene.cxx @@ -1,12 +1,13 @@ #include "Scene.hxx" #include "PerspectiveCamera.hxx" +#include 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)) @@ -29,7 +30,7 @@ Scene::operator=(const Scene& s) return *this; } -void +void Scene::Add(Primitive* p) { m_primitives.push_back(p); @@ -40,9 +41,16 @@ bool Scene::Intersect(Ray& ray) { bool hit = false; - for( std::vector::iterator i = m_primitives.begin(); - i != m_primitives.end(); i++ ) { - hit |= (*i)->Intersect(ray); + float t = std::numeric_limits::max(); + for (std::vector::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; } @@ -56,10 +64,14 @@ Scene::Occluded(Ray& ray) 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;