World is on the verge of working!

This commit is contained in:
Godzil
2020-02-20 01:41:53 +00:00
parent be6b472472
commit 999419dfe1
6 changed files with 170 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
*
*/
#include <intersect.h>
#include <intersection.h>
#include <sphere.h>
#include <gtest/gtest.h>
@@ -129,4 +130,47 @@ TEST(IntersectTest, The_hit_is_always_the_lowest_nonnegative_intersection)
Intersection i = xs.hit();
ASSERT_EQ(i, i4);
}
TEST(IntersectTest, Precomputing_the_state_of_an_intersection)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Sphere shape = Sphere();
Intersection i = Intersection(4, &shape);
Computation comps = i.prepareComputation(r);
ASSERT_EQ(comps.t, i.t);
ASSERT_EQ(comps.object, i.object);
ASSERT_EQ(comps.hitPoint, Point(0, 0, -1));
ASSERT_EQ(comps.eyeVector, Vector(0, 0, -1));
ASSERT_EQ(comps.normalVector, Vector(0, 0, -1));
}
TEST(IntersectTest, The_hit_when_an_intersection_occurs_on_the_outside)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Sphere shape = Sphere();
Intersection i = Intersection(4, &shape);
Computation comps = i.prepareComputation(r);
ASSERT_EQ(comps.inside, false);
}
TEST(IntersectTest, The_hit_when_an_intersection_occurs_on_the_inside)
{
Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
Sphere shape = Sphere();
Intersection i = Intersection(1, &shape);
Computation comps = i.prepareComputation(r);
ASSERT_EQ(comps.hitPoint, Point(0, 0, 1));
ASSERT_EQ(comps.eyeVector, Vector(0, 0, -1));
ASSERT_EQ(comps.inside, true);
/* Normal vector would have been (0, 0, 1); but is inverted ! */
ASSERT_EQ(comps.normalVector, Vector(0, 0, -1));
}

View File

@@ -58,3 +58,57 @@ TEST(WorldTest, Intersect_a_world_with_a_ray)
ASSERT_EQ(xs[3].t, 6);
}
TEST(WorldTest, Shading_an_intersection)
{
World w = DefaultWorld();
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Shape *s = w.getObject(0);
Intersection i = Intersection(4, s);
Computation comps = i.prepareComputation(r);
Tuple c = w.shadeHit(comps);
/* Temporary lower the precision */
set_equal_precision(0.00001);
ASSERT_EQ(c, Colour(0.38066, 0.47583, 0.2855));
set_equal_precision(FLT_EPSILON);
}
TEST(WorldTest, The_when_ray_miss)
{
World w = DefaultWorld();
Ray r = Ray(Point(0, 0, -5), Vector(0, 1, 0));
Tuple c = w.colourAt(r);
ASSERT_EQ(c, Colour(0, 0, 0));
}
TEST(WorldTest, The_when_ray_hit)
{
World w = DefaultWorld();
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Tuple c = w.colourAt(r);
/* Temporary lower the precision */
set_equal_precision(0.00001);
ASSERT_EQ(c, Colour(0.38066, 0.47583, 0.2855));
set_equal_precision(FLT_EPSILON);
}
TEST(WorldTest, The_colour_with_an_intersection_behind_the_ray)
{
World w = DefaultWorld();
Shape *outer = w.getObject(0);
outer->material.ambient = 1;
Shape *inner = w.getObject(1);
inner->material.ambient = 1;
Ray r = Ray(Point(0, 0, 0.75), Vector(0, 0, -1));
Tuple c = w.colourAt(r);
ASSERT_EQ(c, inner->material.colour);
}