[WIP] Test 7 of refraction is in wip
This commit is contained in:
@@ -49,7 +49,6 @@ IntersectionData::IntersectionData(void) :
|
|||||||
|
|
||||||
IntersectionData::~IntersectionData(void)
|
IntersectionData::~IntersectionData(void)
|
||||||
{
|
{
|
||||||
printf("destructor\n");
|
|
||||||
m_shape = nullptr;
|
m_shape = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,6 +110,20 @@ void IntersectionData::set_over_point(const Tuple &a_point)
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
const Tuple &IntersectionData::under_point(void) const
|
||||||
|
{
|
||||||
|
return m_under_point;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
void IntersectionData::set_under_point(const Tuple &a_point)
|
||||||
|
{
|
||||||
|
m_under_point = a_point;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
const Tuple &IntersectionData::eyev(void) const
|
const Tuple &IntersectionData::eyev(void) const
|
||||||
{
|
{
|
||||||
return m_eyev;
|
return m_eyev;
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ namespace Raytracer
|
|||||||
const Tuple &over_point(void) const;
|
const Tuple &over_point(void) const;
|
||||||
void set_over_point(const Tuple &a_point);
|
void set_over_point(const Tuple &a_point);
|
||||||
|
|
||||||
|
const Tuple &under_point(void) const;
|
||||||
|
void set_under_point(const Tuple &a_point);
|
||||||
|
|
||||||
const Tuple &eyev(void) const;
|
const Tuple &eyev(void) const;
|
||||||
void set_eyev(const Tuple &an_eyev);
|
void set_eyev(const Tuple &an_eyev);
|
||||||
|
|
||||||
@@ -78,6 +81,7 @@ namespace Raytracer
|
|||||||
Shape *m_shape;
|
Shape *m_shape;
|
||||||
Tuple m_point;
|
Tuple m_point;
|
||||||
Tuple m_over_point;
|
Tuple m_over_point;
|
||||||
|
Tuple m_under_point;
|
||||||
Tuple m_eyev;
|
Tuple m_eyev;
|
||||||
Tuple m_normalv;
|
Tuple m_normalv;
|
||||||
Tuple m_reflectv;
|
Tuple m_reflectv;
|
||||||
|
|||||||
@@ -190,6 +190,7 @@ IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersecti
|
|||||||
the_data.set_eyev(-a_ray.direction());
|
the_data.set_eyev(-a_ray.direction());
|
||||||
the_data.set_normalv(m_shape->normal_at(the_data.point()));
|
the_data.set_normalv(m_shape->normal_at(the_data.point()));
|
||||||
the_data.set_over_point(the_data.point() + the_data.normalv() * kEpsilon);
|
the_data.set_over_point(the_data.point() + the_data.normalv() * kEpsilon);
|
||||||
|
the_data.set_under_point(the_data.point() - the_data.normalv() * kEpsilon);
|
||||||
the_data.set_inside();
|
the_data.set_inside();
|
||||||
the_data.set_reflectv(a_ray.direction().reflect(the_data.normalv()));
|
the_data.set_reflectv(a_ray.direction().reflect(the_data.normalv()));
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,8 @@
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include "core/common.h"
|
#include "core/common.h"
|
||||||
#include "core/matrix.h"
|
#include "core/matrix.h"
|
||||||
|
|
||||||
@@ -159,21 +161,21 @@ Intersections World::intersect_world(const Ray &a_ray) const
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
Color World::shade_hit(const IntersectionData &an_intersection_data, uint32_t a_remainging) const
|
Color World::shade_hit(const IntersectionData &an_intersection_data, uint32_t a_remaining) const
|
||||||
{
|
{
|
||||||
bool the_shadowed = is_shadowed(an_intersection_data.over_point());
|
bool the_shadowed = is_shadowed(an_intersection_data.over_point());
|
||||||
Shape *the_object = an_intersection_data.object();
|
Shape *the_object = an_intersection_data.object();
|
||||||
Color the_surface = the_object->material().lighting(the_object, m_light, an_intersection_data.over_point(),
|
Color the_surface = the_object->material().lighting(the_object, m_light, an_intersection_data.over_point(),
|
||||||
an_intersection_data.eyev(), an_intersection_data.normalv(), the_shadowed);
|
an_intersection_data.eyev(), an_intersection_data.normalv(), the_shadowed);
|
||||||
Color the_reflected = reflected_color(an_intersection_data, a_remainging);
|
Color the_reflected = reflected_color(an_intersection_data, a_remaining);
|
||||||
return the_surface + the_reflected;
|
return the_surface + the_reflected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
Color World::reflected_color(const IntersectionData &a_data, uint32_t a_remainging) const
|
Color World::reflected_color(const IntersectionData &a_data, uint32_t a_remaining) const
|
||||||
{
|
{
|
||||||
if (a_remainging <= 0)
|
if (a_remaining <= 0)
|
||||||
{
|
{
|
||||||
return Color(0, 0, 0);
|
return Color(0, 0, 0);
|
||||||
}
|
}
|
||||||
@@ -183,14 +185,63 @@ Color World::reflected_color(const IntersectionData &a_data, uint32_t a_remaingi
|
|||||||
return Color(0, 0, 0);
|
return Color(0, 0, 0);
|
||||||
}
|
}
|
||||||
Ray the_reflected_ray(a_data.over_point(), a_data.reflectv());
|
Ray the_reflected_ray(a_data.over_point(), a_data.reflectv());
|
||||||
Color the_color = color_at(the_reflected_ray, a_remainging - 1);
|
Color the_color = color_at(the_reflected_ray, a_remaining - 1);
|
||||||
|
|
||||||
return the_color * a_data.object()->material().reflective();
|
return the_color * a_data.object()->material().reflective();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
Color World::color_at(const Ray &a_ray, uint32_t a_remainging) const
|
Color World::refracted_color(const IntersectionData &an_intersection_data, uint32_t a_remaining) const
|
||||||
|
{
|
||||||
|
double the_n_ratio, the_cos_i, the_cos_t, the_sin_t;
|
||||||
|
Tuple the_direction;
|
||||||
|
Color the_color;
|
||||||
|
double the_transparency;
|
||||||
|
|
||||||
|
if (a_remaining <= 0)
|
||||||
|
{
|
||||||
|
return Color(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
the_transparency = an_intersection_data.object()->material().transparency();
|
||||||
|
if (double_equal(the_transparency, 0))
|
||||||
|
{
|
||||||
|
return Color(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the ratio of first index of refraction to the second.
|
||||||
|
// Yup, this inverted for the definition of the snell's Law
|
||||||
|
the_n_ratio = an_intersection_data.n1() / an_intersection_data.n2();
|
||||||
|
|
||||||
|
// Cos(theta_i) is the same as the dot product of the two vectors.
|
||||||
|
the_cos_i = an_intersection_data.eyev().dot(an_intersection_data.normalv());
|
||||||
|
|
||||||
|
// Find sin(theta_t)^2 via trigonometric identity
|
||||||
|
the_sin_t = the_n_ratio * the_n_ratio * (1.0 - the_cos_i * the_cos_i);
|
||||||
|
if (the_sin_t > 1)
|
||||||
|
{
|
||||||
|
return Color(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find cos(theta_t) via trigonometric identity
|
||||||
|
the_cos_t = std::sqrt(1.0 - the_sin_t);
|
||||||
|
|
||||||
|
// Compute the direction of the refracted ray
|
||||||
|
the_direction = an_intersection_data.normalv() * (the_n_ratio * the_cos_i - the_cos_t) -
|
||||||
|
an_intersection_data.eyev() * the_n_ratio;
|
||||||
|
Ray the_refracted_ray(an_intersection_data.under_point(), the_direction);
|
||||||
|
|
||||||
|
// Find the color of the refracted ray, making sure to multiply by the transparency value
|
||||||
|
// to account for any opacity
|
||||||
|
the_color = color_at(the_refracted_ray, a_remaining - 1) * an_intersection_data.object()->material().transparency();
|
||||||
|
|
||||||
|
return the_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
Color World::color_at(const Ray &a_ray, uint32_t a_remaining) const
|
||||||
{
|
{
|
||||||
Color the_color = Color::Black();
|
Color the_color = Color::Black();
|
||||||
|
|
||||||
@@ -208,7 +259,7 @@ Color World::color_at(const Ray &a_ray, uint32_t a_remainging) const
|
|||||||
|
|
||||||
IntersectionData the_comps = the_intersec.prepare_computations(a_ray);
|
IntersectionData the_comps = the_intersec.prepare_computations(a_ray);
|
||||||
|
|
||||||
the_color = shade_hit(the_comps, a_remainging);
|
the_color = shade_hit(the_comps, a_remaining);
|
||||||
|
|
||||||
return the_color;
|
return the_color;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,8 @@ namespace Raytracer
|
|||||||
Intersections intersect_world(const Ray &a_ray) const;
|
Intersections intersect_world(const Ray &a_ray) const;
|
||||||
Color shade_hit(const IntersectionData &an_intersection_data, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
Color shade_hit(const IntersectionData &an_intersection_data, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
||||||
Color reflected_color(const IntersectionData &an_intersection_data, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
Color reflected_color(const IntersectionData &an_intersection_data, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
||||||
|
Color refracted_color(const IntersectionData &an_intersection_data, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
||||||
|
|
||||||
Color color_at(const Ray &a_ray, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
Color color_at(const Ray &a_ray, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
||||||
|
|
||||||
bool is_shadowed(const Tuple &a_point) const;
|
bool is_shadowed(const Tuple &a_point) const;
|
||||||
|
|||||||
@@ -33,6 +33,19 @@ using namespace Raytracer;
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
class TestPattern : public Pattern
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TestPattern(void) = default;
|
||||||
|
|
||||||
|
const Color pattern_at(const Tuple &a_point) const override
|
||||||
|
{
|
||||||
|
return Color(a_point.x(), a_point.y(), a_point.z());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
SCENARIO("Reflectivity for the default material", "[features/materials.feature]")
|
SCENARIO("Reflectivity for the default material", "[features/materials.feature]")
|
||||||
{
|
{
|
||||||
GIVEN("m <- material()")
|
GIVEN("m <- material()")
|
||||||
@@ -405,3 +418,218 @@ SCENARIO("Finding n1 and n2 at various intersections", "[features/intersections.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
SCENARIO("The under point is offset below the surface", "[features/intersections.feature]")
|
||||||
|
{
|
||||||
|
GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1)")
|
||||||
|
{
|
||||||
|
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
|
||||||
|
AND_GIVEN("shape <- glass_sphere() with:")
|
||||||
|
// | transform | translation(0, 0, 1) |
|
||||||
|
{
|
||||||
|
Sphere shape = Sphere::Glass();
|
||||||
|
shape.set_transform(Matrix::translation(0, 0, 1));
|
||||||
|
AND_GIVEN("i <- intersection(5, shape)")
|
||||||
|
{
|
||||||
|
Intersection i(5, &shape);
|
||||||
|
AND_GIVEN("xs <- intersections(i)")
|
||||||
|
{
|
||||||
|
Intersections xs = Intersections({i});
|
||||||
|
WHEN("comps <- prepare_computations(i, r, xs)")
|
||||||
|
{
|
||||||
|
IntersectionData comps = i.prepare_computations(r, &xs);
|
||||||
|
THEN("comps.under_point.z > EPSILON / 2")
|
||||||
|
{
|
||||||
|
REQUIRE(comps.under_point().z() > kEpsilon / 2);
|
||||||
|
}
|
||||||
|
AND_THEN("comps.point.z < comps.under_point.z")
|
||||||
|
{
|
||||||
|
REQUIRE(comps.point().z() < comps.under_point().z());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
SCENARIO("The refracted color with a, opaque surface", "[features/world.feature]")
|
||||||
|
{
|
||||||
|
GIVEN("w <- default_world()")
|
||||||
|
{
|
||||||
|
World w = World::default_world();
|
||||||
|
AND_GIVEN("shape <- first object of w")
|
||||||
|
{
|
||||||
|
Shape *shape = w.objects(0);
|
||||||
|
AND_GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
|
||||||
|
{
|
||||||
|
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
|
||||||
|
AND_GIVEN("xs <- intersections(4:shape, 6:shape)")
|
||||||
|
{
|
||||||
|
Intersections xs = Intersections({Intersection(4.0, shape),
|
||||||
|
Intersection(6.0, shape)});
|
||||||
|
WHEN("comps <- prepare_computations(xs[0], r, xs)")
|
||||||
|
{
|
||||||
|
IntersectionData comps = xs[0].prepare_computations(r, &xs);
|
||||||
|
AND_WHEN("c <- refracted_color(w, comps, 5)")
|
||||||
|
{
|
||||||
|
Color c = w.refracted_color(comps, 5);
|
||||||
|
THEN("c = color(0, 0, 0)")
|
||||||
|
{
|
||||||
|
REQUIRE(c == Color(0, 0, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
SCENARIO("The refracted color at the maximum recursive depth", "[features/world.feature]")
|
||||||
|
{
|
||||||
|
GIVEN("w <- default_world()")
|
||||||
|
{
|
||||||
|
World w = World::default_world();
|
||||||
|
AND_GIVEN("shape <- first object of w")
|
||||||
|
{
|
||||||
|
Shape *shape = w.objects(0);
|
||||||
|
AND_GIVEN("shape has:")
|
||||||
|
{
|
||||||
|
// | material.transparency | 1.0 |
|
||||||
|
// | material.refractive_index | 1.5 |
|
||||||
|
shape->material().set_transparency(1.0);
|
||||||
|
shape->material().set_refractive_index(1.5);
|
||||||
|
AND_GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
|
||||||
|
{
|
||||||
|
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
|
||||||
|
AND_GIVEN("xs <- intersections(4:shape, 6:shape)")
|
||||||
|
{
|
||||||
|
Intersections xs = Intersections({Intersection(4.0, shape),
|
||||||
|
Intersection(6.0, shape)});
|
||||||
|
WHEN("comps <- prepare_computations(xs[0], r, xs)")
|
||||||
|
{
|
||||||
|
IntersectionData comps = xs[0].prepare_computations(r, &xs);
|
||||||
|
AND_WHEN("c <- refracted_color(w, comps, 0)")
|
||||||
|
{
|
||||||
|
Color c = w.refracted_color(comps, 0);
|
||||||
|
THEN("c = color(0, 0, 0)")
|
||||||
|
{
|
||||||
|
REQUIRE(c == Color(0, 0, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
SCENARIO("The refracted color under total internal reflection", "[features/world.feature]")
|
||||||
|
{
|
||||||
|
GIVEN("w <- default_world()")
|
||||||
|
{
|
||||||
|
World w = World::default_world();
|
||||||
|
AND_GIVEN("shape <- first object of w")
|
||||||
|
{
|
||||||
|
Shape *shape = w.objects(0);
|
||||||
|
AND_GIVEN("shape has:")
|
||||||
|
{
|
||||||
|
// | material.transparency | 1.0 |
|
||||||
|
// | material.refractive_index | 1.5 |
|
||||||
|
shape->material().set_transparency(1.0);
|
||||||
|
shape->material().set_refractive_index(1.5);
|
||||||
|
AND_GIVEN("r <- ray(point(0, 0, sqrt(2) / 2), vector(0, 1, 0))")
|
||||||
|
{
|
||||||
|
Ray r(Tuple::Point(0, 0, sqrt(2) / 2), Tuple::Vector(0, 1, 0));
|
||||||
|
AND_GIVEN("xs <- intersections(-(sqrt(2) / 2):shape, sqrt(2) / 2:shape)")
|
||||||
|
{
|
||||||
|
Intersections xs = Intersections({Intersection(-(sqrt(2) / 2), shape),
|
||||||
|
Intersection(sqrt(2) / 2, shape)});
|
||||||
|
// NOTE: this time you're inside the sphere, so you need to look at the
|
||||||
|
// second intersection, xs[1] and not xs[0]
|
||||||
|
WHEN("comps <- prepare_computations(xs[1], r, xs)")
|
||||||
|
{
|
||||||
|
IntersectionData comps = xs[1].prepare_computations(r, &xs);
|
||||||
|
AND_WHEN("c <- refracted_color(w, comps, 5)")
|
||||||
|
{
|
||||||
|
Color c = w.refracted_color(comps, 5);
|
||||||
|
THEN("c = color(0, 0, 0)")
|
||||||
|
{
|
||||||
|
REQUIRE(c == Color(0, 0, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
SCENARIO("The refracted color with a refracted ray", "[features/world.feature]")
|
||||||
|
{
|
||||||
|
GIVEN("w <- default_world()")
|
||||||
|
{
|
||||||
|
World w = World::default_world();
|
||||||
|
AND_GIVEN("A <- first object of w")
|
||||||
|
{
|
||||||
|
Shape *A = w.objects(0);
|
||||||
|
AND_GIVEN("A has:")
|
||||||
|
{
|
||||||
|
// | material.ambient | 1.0 |
|
||||||
|
// | material.pattern | test_pattern() |
|
||||||
|
A->material().set_ambient(1.0);
|
||||||
|
TestPattern the_pattern;
|
||||||
|
A->material().set_pattern(&the_pattern);
|
||||||
|
AND_GIVEN("B <- second object of w")
|
||||||
|
{
|
||||||
|
Shape *B = w.objects(1);
|
||||||
|
AND_GIVEN("shape has:")
|
||||||
|
{
|
||||||
|
// | material.transparency | 1.0 |
|
||||||
|
// | material.refractive_index | 1.5 |
|
||||||
|
B->material().set_transparency(1.0);
|
||||||
|
B->material().set_refractive_index(1.5);
|
||||||
|
AND_GIVEN("r <- ray(point(0, 0, 0.1, vector(0, 1, 0))")
|
||||||
|
{
|
||||||
|
Ray r(Tuple::Point(0, 0, 0.1), Tuple::Vector(0, 1, 0));
|
||||||
|
AND_GIVEN("xs <- intersections(-0.9899:A, -0.4899:B, 0.4899:B, 0.9899:A)")
|
||||||
|
{
|
||||||
|
Intersections xs = Intersections({
|
||||||
|
Intersection(-0.9899, A),
|
||||||
|
Intersection(-0.4899, B),
|
||||||
|
Intersection(0.4899, B),
|
||||||
|
Intersection(0.9899, A),
|
||||||
|
});
|
||||||
|
WHEN("comps <- prepare_computations(xs[2], r, xs)")
|
||||||
|
{
|
||||||
|
IntersectionData comps = xs[2].prepare_computations(r, &xs);
|
||||||
|
AND_WHEN("c <- refracted_color(w, comps, 5)")
|
||||||
|
{
|
||||||
|
Color c = w.refracted_color(comps, 5);
|
||||||
|
THEN("c = color(0, 0.99888, 0.04725)")
|
||||||
|
{
|
||||||
|
REQUIRE(c == Color(0, 0.99888, 0.04725));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user