From: Roland Hieber Date: Mon, 18 Jan 2010 05:03:21 +0000 (+0100) Subject: solution for assignment 2.2: working flat shader X-Git-Url: https://git.rohieb.name/MicroTrace.git/commitdiff_plain/80400444b6db0076159fd1952693f03f12e5bc53 solution for assignment 2.2: working flat shader --- diff --git a/FlatShader.cxx b/FlatShader.cxx index fcae006..3699cd4 100644 --- a/FlatShader.cxx +++ b/FlatShader.cxx @@ -12,7 +12,7 @@ FlatShader::~FlatShader() } FlatShader::FlatShader() - : Shader(0), + : Shader(0), m_color(Vec3f()) { } @@ -20,5 +20,5 @@ FlatShader::FlatShader() Vec3f FlatShader::Shade(Ray& ray) { - return Vec3f(); + return m_color; } diff --git a/Ray.cxx b/Ray.cxx index b7e5457..86b1294 100644 --- a/Ray.cxx +++ b/Ray.cxx @@ -10,7 +10,7 @@ Ray::Ray() m_t = std::numeric_limits::max(); } -Ray::Ray(const Vec3f& org, +Ray::Ray(const Vec3f& org, const Vec3f& dir) : m_org(org), m_dir(dir), @@ -80,13 +80,19 @@ Ray::setT(float t) 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; diff --git a/Ray.hxx b/Ray.hxx index f37425c..0b4e796 100644 --- a/Ray.hxx +++ b/Ray.hxx @@ -13,10 +13,11 @@ public: ~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; @@ -29,9 +30,9 @@ private: 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; }; 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;