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 "Scene.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <utility>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "BMP.h"
|
#include "BMP.h"
|
||||||
#include "shapes/Shape.h"
|
#include "shapes/Shape.h"
|
||||||
#include "shapes/Sphere.h"
|
#include "shapes/Sphere.h"
|
||||||
|
#include "PointLight.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Scene::Scene(map<string, const char *> options,
|
Scene::Scene(map<string, const char *> options,
|
||||||
@ -70,6 +73,12 @@ Scene::~Scene()
|
|||||||
{
|
{
|
||||||
delete (*it);
|
delete (*it);
|
||||||
}
|
}
|
||||||
|
for (vector<Light *>::iterator it = m_lights.begin();
|
||||||
|
it != m_lights.end();
|
||||||
|
it++)
|
||||||
|
{
|
||||||
|
delete (*it);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::load(const char * filename)
|
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);
|
m_transform.translate(1.0, 5.0, 0.5);
|
||||||
shape->setTransform(m_transform);
|
shape->setTransform(m_transform);
|
||||||
m_shapes.push_back(shape);
|
m_shapes.push_back(shape);
|
||||||
|
|
||||||
|
Light * light = new PointLight(Vector(-1, -1, 1));
|
||||||
|
m_lights.push_back(light);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::render()
|
void Scene::render()
|
||||||
@ -146,25 +158,38 @@ Vector Scene::traceRay(const Ray & ray)
|
|||||||
{
|
{
|
||||||
Vector color;
|
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 */
|
/* loop through all shapes in the scene */
|
||||||
for (vector<Shape *>::iterator it = m_shapes.begin();
|
for (vector<Shape *>::iterator it = m_shapes.begin();
|
||||||
it != m_shapes.end();
|
it != m_shapes.end();
|
||||||
it++)
|
it++)
|
||||||
{
|
{
|
||||||
/* then intersect this inversely transformed ray with the shape */
|
|
||||||
Solver::Result intersections = (*it)->intersect(ray);
|
Solver::Result intersections = (*it)->intersect(ray);
|
||||||
|
|
||||||
if (intersections.numResults > 0)
|
if (intersections.numResults > 0)
|
||||||
{
|
{
|
||||||
color[0] = color[1] = color[2] = 1.0;
|
for (int i = 0; i < intersections.numResults; i++)
|
||||||
}
|
{
|
||||||
else
|
if (intersections.results[i] > minDist)
|
||||||
{
|
{
|
||||||
color[0] = 0.0;
|
hits.push_back(pair<Shape *, double>(*it, intersections.results[i]));
|
||||||
color[1] = 0.0;
|
}
|
||||||
color[2] = 0.0;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return color;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return hits;
|
||||||
|
}
|
||||||
|
@ -5,8 +5,13 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <utility>
|
||||||
#include "util/Ray.h"
|
#include "util/Ray.h"
|
||||||
#include "shapes/Shape.h"
|
#include "shapes/Shape.h"
|
||||||
|
#include "Light.h"
|
||||||
|
|
||||||
|
#define SCENE_MAX_TRANSPARENT_HITS 8
|
||||||
|
#define SCENE_TRANSPARENCY_THRESHOLD 0.01
|
||||||
|
|
||||||
class Scene
|
class Scene
|
||||||
{
|
{
|
||||||
@ -21,6 +26,8 @@ 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> >
|
||||||
|
getRayHits(const Ray & ray, double minDist);
|
||||||
|
|
||||||
/* rendering parameters */
|
/* rendering parameters */
|
||||||
int m_width;
|
int m_width;
|
||||||
@ -32,6 +39,7 @@ class Scene
|
|||||||
|
|
||||||
/* private data */
|
/* private data */
|
||||||
std::vector<Shape *> m_shapes;
|
std::vector<Shape *> m_shapes;
|
||||||
|
std::vector<Light *> m_lights;
|
||||||
Transform m_transform;
|
Transform m_transform;
|
||||||
double m_view_plane_dist;
|
double m_view_plane_dist;
|
||||||
int m_multisample_level_squared;
|
int m_multisample_level_squared;
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
class Shape
|
class Shape
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Shape();
|
||||||
virtual Solver::Result intersect(const Ray & ray) = 0;
|
virtual Solver::Result intersect(const Ray & ray) = 0;
|
||||||
virtual Vector getNormalAt(const Vector & pt) = 0;
|
virtual Vector getNormalAt(const Vector & pt) = 0;
|
||||||
|
|
||||||
@ -19,10 +20,13 @@ class Shape
|
|||||||
m_inverse = t.getInverse();
|
m_inverse = t.getInverse();
|
||||||
}
|
}
|
||||||
Transform & getTransform() { return m_transform; }
|
Transform & getTransform() { return m_transform; }
|
||||||
|
void setTransparency(double t) { m_transparency = t; }
|
||||||
|
double getTransparency() const { return m_transparency; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Transform m_transform;
|
Transform m_transform;
|
||||||
Transform m_inverse;
|
Transform m_inverse;
|
||||||
|
double m_transparency;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -9,7 +9,7 @@ const Color Color::blue = Color(0, 0, 1);
|
|||||||
|
|
||||||
Color::Color()
|
Color::Color()
|
||||||
{
|
{
|
||||||
r = g = b = 1.0;
|
r = g = b = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color::Color(double r, double g, double b)
|
Color::Color(double r, double g, double b)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user