added Scene::ShapeDistance construct
git-svn-id: svn://anubis/fart/trunk@52 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
e8fd42f81f
commit
ecace9f5e8
@ -3,8 +3,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <utility>
|
#include <utility> /* pair */
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <algorithm> /* sort() */
|
||||||
|
#include <functional> /* binary_function */
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "BMP.h"
|
#include "BMP.h"
|
||||||
#include "shapes/Shape.h"
|
#include "shapes/Shape.h"
|
||||||
@ -158,7 +160,7 @@ Vector Scene::traceRay(const Ray & ray)
|
|||||||
{
|
{
|
||||||
Vector color;
|
Vector color;
|
||||||
|
|
||||||
vector< pair<Shape *, double> > hits = getRayHits(ray, 0.0);
|
vector<ShapeDistance> hits = getRayHits(ray);
|
||||||
if (hits.size() > 0)
|
if (hits.size() > 0)
|
||||||
{
|
{
|
||||||
color[0] = color[1] = color[2] = 1.0;
|
color[0] = color[1] = color[2] = 1.0;
|
||||||
@ -167,10 +169,9 @@ Vector Scene::traceRay(const Ray & ray)
|
|||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector< pair<Shape *, double> >
|
vector<Scene::ShapeDistance> Scene::getRayHits(const Ray & ray)
|
||||||
Scene::getRayHits(const Ray & ray, double minDist)
|
|
||||||
{
|
{
|
||||||
vector< pair<Shape *, double> > hits;
|
vector<ShapeDistance> hits;
|
||||||
|
|
||||||
/* loop through all shapes in the scene */
|
/* loop through all shapes in the scene */
|
||||||
for (vector<Shape *>::iterator it = m_shapes.begin();
|
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);
|
Solver::Result intersections = (*it)->intersect(ray);
|
||||||
|
|
||||||
if (intersections.numResults > 0)
|
for (int i = 0; i < intersections.numResults; i++)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < intersections.numResults; i++)
|
Vector normal =
|
||||||
|
(*it)->getNormalAt(ray[intersections.results[i]]);
|
||||||
|
double dot = normal % ray.getDirection();
|
||||||
|
if (dot < 0.0) /* cull back faces */
|
||||||
{
|
{
|
||||||
if (intersections.results[i] > minDist)
|
hits.push_back(ShapeDistance(*it, intersections.results[i]));
|
||||||
{
|
|
||||||
hits.push_back(pair<Shape *, double>(*it, intersections.results[i]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* now that we have ALL the hits, sort them by distance */
|
||||||
|
sort(hits.begin(), hits.end());
|
||||||
|
|
||||||
return hits;
|
return hits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator<(const Scene::ShapeDistance & sd1,
|
||||||
|
const Scene::ShapeDistance & sd2)
|
||||||
|
{
|
||||||
|
return sd1.second < sd2.second;
|
||||||
|
}
|
||||||
|
15
main/Scene.h
15
main/Scene.h
@ -16,6 +16,15 @@
|
|||||||
class Scene
|
class Scene
|
||||||
{
|
{
|
||||||
public:
|
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,
|
Scene(std::map<std::string, const char *> options,
|
||||||
const char * filename);
|
const char * filename);
|
||||||
~Scene();
|
~Scene();
|
||||||
@ -26,8 +35,7 @@ class Scene
|
|||||||
void load(const char * filename);
|
void load(const char * filename);
|
||||||
void renderPixel(int x, int y, unsigned char * pixel);
|
void renderPixel(int x, int y, unsigned char * pixel);
|
||||||
Vector traceRay(const Ray & ray);
|
Vector traceRay(const Ray & ray);
|
||||||
std::vector< std::pair<Shape *, double> >
|
std::vector<ShapeDistance> getRayHits(const Ray & ray);
|
||||||
getRayHits(const Ray & ray, double minDist);
|
|
||||||
|
|
||||||
/* rendering parameters */
|
/* rendering parameters */
|
||||||
int m_width;
|
int m_width;
|
||||||
@ -50,5 +58,8 @@ class Scene
|
|||||||
unsigned char * m_data;
|
unsigned char * m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool operator<(const Scene::ShapeDistance & sd1,
|
||||||
|
const Scene::ShapeDistance & sd2);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user