From f4781e2889d441ba10daefb3e1c1a4ea05ad8022 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 27 Jan 2009 01:34:00 +0000 Subject: [PATCH] updated getRayHits(), fixed bug in Scene::ShapeDistance(Shape *, double) git-svn-id: svn://anubis/fart/trunk@53 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Scene.cc | 37 ++++++++++++++++++++++++++++++++++++- main/Scene.h | 3 ++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/main/Scene.cc b/main/Scene.cc index 44dcb27..b41b596 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -172,6 +172,7 @@ Vector Scene::traceRay(const Ray & ray) vector Scene::getRayHits(const Ray & ray) { vector hits; + double minSolidDist = 0.0; /* loop through all shapes in the scene */ for (vector::iterator it = m_shapes.begin(); @@ -187,7 +188,19 @@ vector Scene::getRayHits(const Ray & ray) double dot = normal % ray.getDirection(); if (dot < 0.0) /* cull back faces */ { - hits.push_back(ShapeDistance(*it, intersections.results[i])); + double transparency = (*it)->getTransparency(); + if (transparency == 0.0 && + (minSolidDist == 0.0 + || minSolidDist > intersections.results[i])) + { + minSolidDist = intersections.results[i]; + } + if (minSolidDist == 0.0 + || minSolidDist >= intersections.results[i]) + { + hits.push_back(ShapeDistance(*it, + intersections.results[i])); + } } } } @@ -195,6 +208,28 @@ vector Scene::getRayHits(const Ray & ray) /* now that we have ALL the hits, sort them by distance */ sort(hits.begin(), hits.end()); + double transparency = 1.0; + /* now go through them all until we get to the maximum number + * of hits, or until the transparency left is below the threshold */ + for (unsigned int hits_index = 0, sz = hits.size(); + hits_index < sz; + hits_index++) + { + transparency *= hits[hits_index].first->getTransparency(); + if (hits_index >= (SCENE_MAX_TRANSPARENT_HITS - 1) + || transparency < SCENE_TRANSPARENCY_THRESHOLD) + { + /* delete the rest of the hits */ + for (int i = 0, num_to_del = sz - hits_index - 1; + i < num_to_del; + i++) + { + hits.pop_back(); + } + break; + } + } + return hits; } diff --git a/main/Scene.h b/main/Scene.h index 4e1a0ae..a82f5c6 100755 --- a/main/Scene.h +++ b/main/Scene.h @@ -20,7 +20,8 @@ class Scene class ShapeDistance : public std::pair { public: - ShapeDistance(Shape *, double) : std::pair() + ShapeDistance(Shape * shape, double dist) + : std::pair(shape, dist) { }; };