Post by brendanleber on Aug 14, 2022 23:17:04 GMT
I have all my tests passing up to this point. I'm adding the test and implementation for reflected_color which should be easy, right? It's just a couple of lines of code calling existing functions. Yet, here we are...
The problem is I'm getting (0.55033, 0.68791, 0.41275) for the color tuple and not the expected values of (0.19032, 0.2379, 0.14274). I've stepped through the test with gdb and manually performed the calculations by hand, which has caught most of my problems in the past, but this time both my scratch paper and gdb show the same results. Does anyone have any pointers of where the problem is?
Here is my C++20 implementation of reflected_color for reference:
Color World::reflected_color(const Computations& comps) const
{
if (equal(comps.object->material.reflective, 0))
return Color{0, 0, 0};
Ray reflect_ray{comps.over_point, comps.reflectv};
auto color = color_at(reflect_ray);
return color * comps.object->material.reflective;
}
And the test I've extracted from world.feature:
{
printf("Scenario: The reflected color for a reflective material.\n");
// Given w ← default_world()
auto w = default_world();
// And shape ← plane() with:
// | material.reflective | 0.5 |
// | transform | translation(0, -1, 0) |
Plane shape;
shape.material.reflective = 0.5;
shape.transform = translation(0, -1, 0);
// And shape is added to w
w.objects.push_back(&shape);
// And r ← ray(point(0, 0, -3), vector(0, -√2/2, √2/2))
Ray r{point(0, 0, -3), vector(0, -M_SQRT2 / 2, M_SQRT2 / 2)};
// And i ← intersection(√2, shape)
Intersection i{M_SQRT2, &shape};
// When comps ← prepare_computations(i, r)
auto comps = prepare_computations(i, r);
// And color ← reflected_color(w, comps)
auto color = w.reflected_color(comps);
// Then color = color(0.19032, 0.2379, 0.14274)
assert(color == Color(0.19032, 0.2379, 0.14274));
}
The problem is I'm getting (0.55033, 0.68791, 0.41275) for the color tuple and not the expected values of (0.19032, 0.2379, 0.14274). I've stepped through the test with gdb and manually performed the calculations by hand, which has caught most of my problems in the past, but this time both my scratch paper and gdb show the same results. Does anyone have any pointers of where the problem is?
Here is my C++20 implementation of reflected_color for reference:
Color World::reflected_color(const Computations& comps) const
{
if (equal(comps.object->material.reflective, 0))
return Color{0, 0, 0};
Ray reflect_ray{comps.over_point, comps.reflectv};
auto color = color_at(reflect_ray);
return color * comps.object->material.reflective;
}
And the test I've extracted from world.feature:
{
printf("Scenario: The reflected color for a reflective material.\n");
// Given w ← default_world()
auto w = default_world();
// And shape ← plane() with:
// | material.reflective | 0.5 |
// | transform | translation(0, -1, 0) |
Plane shape;
shape.material.reflective = 0.5;
shape.transform = translation(0, -1, 0);
// And shape is added to w
w.objects.push_back(&shape);
// And r ← ray(point(0, 0, -3), vector(0, -√2/2, √2/2))
Ray r{point(0, 0, -3), vector(0, -M_SQRT2 / 2, M_SQRT2 / 2)};
// And i ← intersection(√2, shape)
Intersection i{M_SQRT2, &shape};
// When comps ← prepare_computations(i, r)
auto comps = prepare_computations(i, r);
// And color ← reflected_color(w, comps)
auto color = w.reflected_color(comps);
// Then color = color(0.19032, 0.2379, 0.14274)
assert(color == Color(0.19032, 0.2379, 0.14274));
}