added Scene::ShapeDistance construct

git-svn-id: svn://anubis/fart/trunk@52 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-01-27 00:47:21 +00:00
parent e8fd42f81f
commit ecace9f5e8
2 changed files with 34 additions and 13 deletions

View File

@ -3,8 +3,10 @@
#include <stdlib.h>
#include <string>
#include <vector>
#include <utility>
#include <utility> /* pair */
#include <map>
#include <algorithm> /* sort() */
#include <functional> /* binary_function */
#include <math.h>
#include "BMP.h"
#include "shapes/Shape.h"
@ -158,7 +160,7 @@ Vector Scene::traceRay(const Ray & ray)
{
Vector color;
vector< pair<Shape *, double> > hits = getRayHits(ray, 0.0);
vector<ShapeDistance> hits = getRayHits(ray);
if (hits.size() > 0)
{
color[0] = color[1] = color[2] = 1.0;
@ -167,10 +169,9 @@ Vector Scene::traceRay(const Ray & ray)
return color;
}
vector< pair<Shape *, double> >
Scene::getRayHits(const Ray & ray, double minDist)
vector<Scene::ShapeDistance> Scene::getRayHits(const Ray & ray)
{
vector< pair<Shape *, double> > hits;
vector<ShapeDistance> hits;
/* loop through all shapes in the scene */
for (vector<Shape *>::iterator it = m_shapes.begin();
@ -179,17 +180,26 @@ Scene::getRayHits(const Ray & ray, double minDist)
{
Solver::Result intersections = (*it)->intersect(ray);
if (intersections.numResults > 0)
{
for (int i = 0; i < intersections.numResults; i++)
{
if (intersections.results[i] > minDist)
Vector normal =
(*it)->getNormalAt(ray[intersections.results[i]]);
double dot = normal % ray.getDirection();
if (dot < 0.0) /* cull back faces */
{
hits.push_back(pair<Shape *, double>(*it, intersections.results[i]));
}
hits.push_back(ShapeDistance(*it, intersections.results[i]));
}
}
}
/* now that we have ALL the hits, sort them by distance */
sort(hits.begin(), hits.end());
return hits;
}
bool operator<(const Scene::ShapeDistance & sd1,
const Scene::ShapeDistance & sd2)
{
return sd1.second < sd2.second;
}

View File

@ -16,6 +16,15 @@
class Scene
{
public:
/* types */
class ShapeDistance : public std::pair<Shape *, double>
{
public:
ShapeDistance(Shape *, double) : std::pair<Shape *, double>()
{
};
};
Scene(std::map<std::string, const char *> options,
const char * filename);
~Scene();
@ -26,8 +35,7 @@ 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<Shape *, double> >
getRayHits(const Ray & ray, double minDist);
std::vector<ShapeDistance> getRayHits(const Ray & ray);
/* rendering parameters */
int m_width;
@ -50,5 +58,8 @@ class Scene
unsigned char * m_data;
};
bool operator<(const Scene::ShapeDistance & sd1,
const Scene::ShapeDistance & sd2);
#endif