broke traceRay() out into getRayHits()
git-svn-id: svn://anubis/fart/trunk@51 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
38b2304641
commit
e8fd42f81f
@ -2,11 +2,14 @@
|
||||
#include "Scene.h"
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <map>
|
||||
#include <math.h>
|
||||
#include "BMP.h"
|
||||
#include "shapes/Shape.h"
|
||||
#include "shapes/Sphere.h"
|
||||
#include "PointLight.h"
|
||||
using namespace std;
|
||||
|
||||
Scene::Scene(map<string, const char *> options,
|
||||
@ -70,6 +73,12 @@ Scene::~Scene()
|
||||
{
|
||||
delete (*it);
|
||||
}
|
||||
for (vector<Light *>::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<Shape *, double> > hits = getRayHits(ray, 0.0);
|
||||
if (hits.size() > 0)
|
||||
{
|
||||
color[0] = color[1] = color[2] = 1.0;
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
vector< pair<Shape *, double> >
|
||||
Scene::getRayHits(const Ray & ray, double minDist)
|
||||
{
|
||||
vector< pair<Shape *, double> > hits;
|
||||
|
||||
/* loop through all shapes in the scene */
|
||||
for (vector<Shape *>::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
|
||||
for (int i = 0; i < intersections.numResults; i++)
|
||||
{
|
||||
color[0] = 0.0;
|
||||
color[1] = 0.0;
|
||||
color[2] = 0.0;
|
||||
if (intersections.results[i] > minDist)
|
||||
{
|
||||
hits.push_back(pair<Shape *, double>(*it, intersections.results[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
return hits;
|
||||
}
|
||||
|
@ -5,8 +5,13 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#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<Shape *, double> >
|
||||
getRayHits(const Ray & ray, double minDist);
|
||||
|
||||
/* rendering parameters */
|
||||
int m_width;
|
||||
@ -32,6 +39,7 @@ class Scene
|
||||
|
||||
/* private data */
|
||||
std::vector<Shape *> m_shapes;
|
||||
std::vector<Light *> m_lights;
|
||||
Transform m_transform;
|
||||
double m_view_plane_dist;
|
||||
int m_multisample_level_squared;
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user