[WIP] Refraction in progress... the tests is green.
This commit is contained in:
@@ -28,6 +28,8 @@
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
#include "intersection-data.h"
|
#include "intersection-data.h"
|
||||||
|
|
||||||
using namespace Raytracer;
|
using namespace Raytracer;
|
||||||
@@ -45,6 +47,14 @@ IntersectionData::IntersectionData(void) :
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
IntersectionData::~IntersectionData(void)
|
||||||
|
{
|
||||||
|
printf("destructor\n");
|
||||||
|
m_shape = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
double IntersectionData::distance_t(void) const
|
double IntersectionData::distance_t(void) const
|
||||||
{
|
{
|
||||||
return m_distance;
|
return m_distance;
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ namespace Raytracer
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
IntersectionData(void);
|
IntersectionData(void);
|
||||||
|
~IntersectionData(void);
|
||||||
|
|
||||||
double distance_t(void) const;
|
double distance_t(void) const;
|
||||||
void set_distance_t(double a_value);
|
void set_distance_t(double a_value);
|
||||||
|
|||||||
@@ -28,6 +28,8 @@
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#include "core/common.h"
|
#include "core/common.h"
|
||||||
|
|
||||||
#include "intersection.h"
|
#include "intersection.h"
|
||||||
@@ -153,6 +155,13 @@ const Shape *Intersection::object(void) const
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
Shape *Intersection::object(void)
|
||||||
|
{
|
||||||
|
return m_shape;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
bool Intersection::is_nothing(void)
|
bool Intersection::is_nothing(void)
|
||||||
{
|
{
|
||||||
return m_is_nothing;
|
return m_is_nothing;
|
||||||
@@ -170,7 +179,7 @@ bool Intersection::is_defined(void)
|
|||||||
IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersections *a_collection) const
|
IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersections *a_collection) const
|
||||||
{
|
{
|
||||||
IntersectionData the_data;
|
IntersectionData the_data;
|
||||||
std::vector<Shape *> the_container;
|
std::list<Shape *> the_container;
|
||||||
|
|
||||||
// Copy intersections properties for convenance
|
// Copy intersections properties for convenance
|
||||||
the_data.set_distance_t(m_distance_t);
|
the_data.set_distance_t(m_distance_t);
|
||||||
@@ -190,9 +199,9 @@ IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersecti
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Manage the container
|
// Manage the container
|
||||||
for (const Intersection &the_it : a_collection->data())
|
for (Intersection &the_it : a_collection->data())
|
||||||
{
|
{
|
||||||
if (the_it == a_collection->hit())
|
if (the_it == *this)
|
||||||
{
|
{
|
||||||
if (the_container.empty())
|
if (the_container.empty())
|
||||||
{
|
{
|
||||||
@@ -200,7 +209,29 @@ IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersecti
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// TODO
|
the_data.set_n1(the_container.back()->material().refractive_index());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto the_shape_it = std::find(the_container.begin(), the_container.end(), the_it.object());
|
||||||
|
|
||||||
|
if (the_shape_it != the_container.end())
|
||||||
|
{
|
||||||
|
the_container.erase(the_shape_it);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
the_container.push_back(the_it.object());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (the_it == *this)
|
||||||
|
{
|
||||||
|
if (the_container.empty())
|
||||||
|
{
|
||||||
|
the_data.set_n2(1.0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
the_data.set_n2(the_container.back()->material().refractive_index());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ namespace Raytracer
|
|||||||
|
|
||||||
double distance_t(void) const;
|
double distance_t(void) const;
|
||||||
const Shape *object(void) const;
|
const Shape *object(void) const;
|
||||||
|
Shape *object(void);
|
||||||
bool is_nothing(void);
|
bool is_nothing(void);
|
||||||
bool is_defined(void);
|
bool is_defined(void);
|
||||||
|
|
||||||
|
|||||||
@@ -102,6 +102,13 @@ const std::vector<Intersection> &Intersections::data(void) const
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
std::vector<Intersection> &Intersections::data(void)
|
||||||
|
{
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
bool Intersections::add(const Intersection &an_intersection)
|
bool Intersections::add(const Intersection &an_intersection)
|
||||||
{
|
{
|
||||||
m_data.push_back(an_intersection);
|
m_data.push_back(an_intersection);
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ namespace Raytracer
|
|||||||
const Intersections &operator+=(const Intersections &an_other);
|
const Intersections &operator+=(const Intersections &an_other);
|
||||||
|
|
||||||
const std::vector<Intersection> &data(void) const;
|
const std::vector<Intersection> &data(void) const;
|
||||||
|
std::vector<Intersection> &data(void);
|
||||||
|
|
||||||
bool add(const Intersection &an_intersection);
|
bool add(const Intersection &an_intersection);
|
||||||
|
|
||||||
|
|||||||
@@ -325,25 +325,14 @@ SCENARIO("A helper for producing a sphere with a glassy material", "[features/sp
|
|||||||
|
|
||||||
struct TestData
|
struct TestData
|
||||||
{
|
{
|
||||||
Intersection i;
|
|
||||||
double n1;
|
double n1;
|
||||||
double n2;
|
double n2;
|
||||||
};
|
};
|
||||||
|
|
||||||
void fill_test_various(TestData *a_test, const Intersection &an_in, double a_n1, double a_n2)
|
|
||||||
{
|
|
||||||
a_test->i = an_in;
|
|
||||||
a_test->n1 = a_n1;
|
|
||||||
a_test->n2 = a_n2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
SCENARIO("Finding n1 and n2 at various intersections", "[features/intersections.feature]")
|
SCENARIO("Finding n1 and n2 at various intersections", "[features/intersections.feature]")
|
||||||
{
|
{
|
||||||
|
|
||||||
TestData the_tests[6];
|
|
||||||
|
|
||||||
GIVEN("A <- glass_sphere() with:")
|
GIVEN("A <- glass_sphere() with:")
|
||||||
// | transform | scaling(2, 2, 2) |
|
// | transform | scaling(2, 2, 2) |
|
||||||
// | material.refractive_index | 1.5 |
|
// | material.refractive_index | 1.5 |
|
||||||
@@ -379,28 +368,34 @@ SCENARIO("Finding n1 and n2 at various intersections", "[features/intersections.
|
|||||||
// | 3 |2.5 |2.5 |
|
// | 3 |2.5 |2.5 |
|
||||||
// | 4 |2.5 |1.5 |
|
// | 4 |2.5 |1.5 |
|
||||||
// | 5 |1.5 |1.0 |
|
// | 5 |1.5 |1.0 |
|
||||||
fill_test_various(&the_tests[0], Intersection(2.0, &A), 1.0, 1.5);
|
TestData the_ns[6] = {
|
||||||
fill_test_various(&the_tests[1], Intersection(2.75, &B), 1.5, 2.0);
|
{1.0, 1.5},
|
||||||
fill_test_various(&the_tests[2], Intersection(3.25, &C), 2.0, 2.5);
|
{1.5, 2.0},
|
||||||
fill_test_various(&the_tests[3], Intersection(4.75, &B), 2.5, 2.5);
|
{2.0, 2.5},
|
||||||
fill_test_various(&the_tests[4], Intersection(5.25, &C), 2.5, 1.5);
|
{2.5, 2.5},
|
||||||
fill_test_various(&the_tests[5], Intersection(6.0, &A), 1.5, 1.0);
|
{2.5, 1.5},
|
||||||
|
{1.5, 1.0}
|
||||||
|
};
|
||||||
|
|
||||||
Intersections xs = Intersections({the_tests[0].i, the_tests[1].i, the_tests[2].i, the_tests[3].i,
|
Intersections xs = Intersections({Intersection(2.0, &A),
|
||||||
the_tests[4].i, the_tests[5].i});
|
Intersection(2.75, &B),
|
||||||
|
Intersection(3.25, &C),
|
||||||
|
Intersection(4.75, &B),
|
||||||
|
Intersection(5.25, &C),
|
||||||
|
Intersection(6.0, &A)});
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++)
|
|
||||||
{
|
|
||||||
WHEN("comps <- prepare_computations(xs[index], r, xs)")
|
WHEN("comps <- prepare_computations(xs[index], r, xs)")
|
||||||
{
|
{
|
||||||
IntersectionData comps = the_tests[i].i.prepare_computations(r, &xs);
|
for (int i = 0; i < 2; i++)
|
||||||
|
{
|
||||||
|
IntersectionData comps = xs[i].prepare_computations(r, &xs);
|
||||||
THEN("comps.n1 = <n1>")
|
THEN("comps.n1 = <n1>")
|
||||||
{
|
{
|
||||||
REQUIRE(comps.n1() == the_tests[i].n1);
|
REQUIRE(comps.n1() == the_ns[i].n1);
|
||||||
}
|
}
|
||||||
AND_THEN("comps.n2 = <n2>")
|
AND_THEN("comps.n2 = <n2>")
|
||||||
{
|
{
|
||||||
REQUIRE(comps.n2() == the_tests[i].n2);
|
REQUIRE(comps.n2() == the_ns[i].n2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user