From e8fd42f81f847bb199a4fad9b1b1a7b7d598a97f Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 27 Jan 2009 00:11:14 +0000 Subject: [PATCH] broke traceRay() out into getRayHits() git-svn-id: svn://anubis/fart/trunk@51 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Scene.cc | 45 +++++++++++++++++++++++++++++++++++---------- main/Scene.h | 8 ++++++++ shapes/Shape.h | 4 ++++ util/Color.cc | 2 +- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/main/Scene.cc b/main/Scene.cc index 8e17bca..f050f73 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -2,11 +2,14 @@ #include "Scene.h" #include #include +#include +#include #include #include #include "BMP.h" #include "shapes/Shape.h" #include "shapes/Sphere.h" +#include "PointLight.h" using namespace std; Scene::Scene(map options, @@ -70,6 +73,12 @@ Scene::~Scene() { delete (*it); } + for (vector::iterator it = m_lights.begin(); + it != m_lights.end(); + it++) + { + delete (*it); + } } void Scene::load(const char * filename) @@ -79,6 +88,9 @@ void Scene::load(const char * filename) m_transform.translate(1.0, 5.0, 0.5); shape->setTransform(m_transform); m_shapes.push_back(shape); + + Light * light = new PointLight(Vector(-1, -1, 1)); + m_lights.push_back(light); } void Scene::render() @@ -146,25 +158,38 @@ Vector Scene::traceRay(const Ray & ray) { Vector color; + vector< pair > hits = getRayHits(ray, 0.0); + if (hits.size() > 0) + { + color[0] = color[1] = color[2] = 1.0; + } + + return color; +} + +vector< pair > +Scene::getRayHits(const Ray & ray, double minDist) +{ + vector< pair > hits; + /* loop through all shapes in the scene */ for (vector::iterator it = m_shapes.begin(); it != m_shapes.end(); it++) { - /* then intersect this inversely transformed ray with the shape */ Solver::Result intersections = (*it)->intersect(ray); if (intersections.numResults > 0) { - color[0] = color[1] = color[2] = 1.0; - } - else - { - color[0] = 0.0; - color[1] = 0.0; - color[2] = 0.0; + for (int i = 0; i < intersections.numResults; i++) + { + if (intersections.results[i] > minDist) + { + hits.push_back(pair(*it, intersections.results[i])); + } + } } } - return color; -} + return hits; +} diff --git a/main/Scene.h b/main/Scene.h index fbbea00..221566e 100755 --- a/main/Scene.h +++ b/main/Scene.h @@ -5,8 +5,13 @@ #include #include #include +#include #include "util/Ray.h" #include "shapes/Shape.h" +#include "Light.h" + +#define SCENE_MAX_TRANSPARENT_HITS 8 +#define SCENE_TRANSPARENCY_THRESHOLD 0.01 class Scene { @@ -21,6 +26,8 @@ class Scene void load(const char * filename); void renderPixel(int x, int y, unsigned char * pixel); Vector traceRay(const Ray & ray); + std::vector< std::pair > + getRayHits(const Ray & ray, double minDist); /* rendering parameters */ int m_width; @@ -32,6 +39,7 @@ class Scene /* private data */ std::vector m_shapes; + std::vector m_lights; Transform m_transform; double m_view_plane_dist; int m_multisample_level_squared; diff --git a/shapes/Shape.h b/shapes/Shape.h index 566cb8c..d869b81 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -10,6 +10,7 @@ class Shape { public: + Shape(); virtual Solver::Result intersect(const Ray & ray) = 0; virtual Vector getNormalAt(const Vector & pt) = 0; @@ -19,10 +20,13 @@ class Shape m_inverse = t.getInverse(); } Transform & getTransform() { return m_transform; } + void setTransparency(double t) { m_transparency = t; } + double getTransparency() const { return m_transparency; } protected: Transform m_transform; Transform m_inverse; + double m_transparency; }; #endif diff --git a/util/Color.cc b/util/Color.cc index 17e0065..e208979 100644 --- a/util/Color.cc +++ b/util/Color.cc @@ -9,7 +9,7 @@ const Color Color::blue = Color(0, 0, 1); Color::Color() { - r = g = b = 1.0; + r = g = b = 0.0; } Color::Color(double r, double g, double b)