[FEAT] ADD lighting method

This commit is contained in:
2024-02-20 09:14:01 +01:00
parent ee5b2c1c95
commit aba3450f2e
7 changed files with 278 additions and 39 deletions

View File

@@ -38,10 +38,10 @@ SCENARIO("The normal on a sphere at point a on the x axis", "[features/spheres.f
GIVEN("s <- sphere()")
{
Sphere s;
WHEN("n <- normal_at(s, point(1,0,0))")
WHEN("n <- normal_at(s, point(1, 0, 0))")
{
Tuple n = s.normal_at(Tuple::Point(1, 0, 0));
THEN("n = vector(1,0,0)")
THEN("n = vector(1, 0, 0)")
{
REQUIRE(n == Tuple::Vector(1, 0, 0));
}
@@ -56,10 +56,10 @@ SCENARIO("The normal on a sphere at point a on the y axis", "[features/spheres.f
GIVEN("s <- sphere()")
{
Sphere s;
WHEN("n <- normal_at(s, point(0,1,0))")
WHEN("n <- normal_at(s, point(0, 1, 0))")
{
Tuple n = s.normal_at(Tuple::Point(0, 1, 0));
THEN("n = vector(0,1,0)")
THEN("n = vector(0, 1, 0)")
{
REQUIRE(n == Tuple::Vector(0, 1, 0));
}
@@ -74,10 +74,10 @@ SCENARIO("The normal on a sphere at point a on the z axis", "[features/spheres.f
GIVEN("s <- sphere()")
{
Sphere s;
WHEN("n <- normal_at(s, point(0,0,1))")
WHEN("n <- normal_at(s, point(0, 0, 1))")
{
Tuple n = s.normal_at(Tuple::Point(0, 0, 1));
THEN("n = vector(0,0,1)")
THEN("n = vector(0, 0, 1)")
{
REQUIRE(n == Tuple::Vector(0, 0, 1));
}
@@ -92,10 +92,10 @@ SCENARIO("The normal on a sphere at a nonaxial point", "[features/spheres.featur
GIVEN("s <- sphere()")
{
Sphere s;
WHEN("n <- normal_at(s, point(sqrt(3)/3,sqrt(3)/3,sqrt(3)/3))")
WHEN("n <- normal_at(s, point(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3))")
{
Tuple n = s.normal_at(Tuple::Point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3));
THEN("n = vector(sqrt(3)/3,sqrt(3)/3,sqrt(3)/3))")
THEN("n = vector(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3))")
{
REQUIRE(n == Tuple::Vector(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3));
}
@@ -110,7 +110,7 @@ SCENARIO("The normal is a normalized vector", "[features/spheres.feature]")
GIVEN("s <- sphere()")
{
Sphere s;
WHEN("n <- normal_at(s, point(sqrt(3)/3,sqrt(3)/3,sqrt(3)/3))")
WHEN("n <- normal_at(s, point(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3))")
{
Tuple n = s.normal_at(Tuple::Point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3));
THEN("n = normalize(n)")
@@ -128,13 +128,13 @@ SCENARIO("Computing the normal on a translated sphere", "[features/spheres.featu
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("set_transform(s, translation(0,1,0))")
AND_GIVEN("set_transform(s, translation(0, 1, 0))")
{
s.set_transform(Matrix::translation(0, 1, 0));
WHEN("n <- normal_at(s,point(0,1.70711,-0.70711))")
WHEN("n <- normal_at(s,point(0, 1.70711, -0.70711))")
{
Tuple n = s.normal_at(Tuple::Point(0, 1.70711, -0.70711));
THEN("n = vector(0,0.70711, -0.70711)")
THEN("n = vector(0, 0.70711, -0.70711)")
{
REQUIRE(n == Tuple::Vector(0, 0.70711, -0.70711));
}
@@ -150,13 +150,13 @@ SCENARIO("Computing the normal on a transformed sphere", "[features/spheres.feat
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("m <- scaling(1,0.5,1) * rotation_z(pi/5)")
AND_GIVEN("m <- scaling(1, 0. 5,1) * rotation_z(pi/5)")
{
Matrix m = Matrix::scaling(1, 0.5, 1) * Matrix::rotation_z(std::numbers::pi / 5);
AND_GIVEN("set_transform(s, m)")
{
s.set_transform(m);
WHEN("n <- normal_at(s,point(0,sqrt(2)/2,sqrt(2)/2))")
WHEN("n <- normal_at(s,point(0, sqrt(2)/2, sqrt(2)/2))")
{
Tuple n = s.normal_at(Tuple::Point(0, sqrt(2) / 2, -sqrt(2) / 2));
THEN("n = vector(0,97014, -0.24254)")
@@ -179,10 +179,10 @@ SCENARIO("Reflecting a vector approaching at 45°", "[features/tuples.feature]")
AND_GIVEN("n <-vector(0, 1, 0)")
{
Tuple n = Tuple::Vector(0, 1, 0);
WHEN("r <- reflect(v,n)")
WHEN("r <- reflect(v, n)")
{
Tuple r = v.reflect(n);
THEN("r = vector(1,1,0)")
THEN("r = vector(1, 1, 0)")
{
REQUIRE(r == Tuple::Vector(1, 1, 0));
}
@@ -201,10 +201,10 @@ SCENARIO("Reflecting a vector off a slanted surface", "[features/tuples.feature]
AND_GIVEN("n <-vector(sqrt(2)/2, sqrt(2)/2, 0)")
{
Tuple n = Tuple::Vector(sqrt(2) / 2, sqrt(2) / 2, 0);
WHEN("r <- reflect(v,n)")
WHEN("r <- reflect(v, n)")
{
Tuple r = v.reflect(n);
THEN("r = vector(1,0,0)")
THEN("r = vector(1, 0, 0)")
{
REQUIRE(r == Tuple::Vector(1, 0, 0));
}
@@ -246,7 +246,7 @@ SCENARIO("The default material", "[features/materials.feature]")
GIVEN("m <- material()")
{
Material m;
THEN("m.color = color(1,1,1)")
THEN("m.color = color(1, 1, 1)")
{
REQUIRE(m.color() == Color(1, 1, 1));
}
@@ -312,3 +312,148 @@ SCENARIO("A sphere may be assigned a material", "[features/spheres.feature]")
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Lighting with the eye between the light and the surface", "[features/materials.feature]")
{
Tuple position = Tuple::Point(0, 0, 0);
Material m;
GIVEN("eyev <- vector(0, 0, -1)")
{
Tuple eyev = Tuple::Vector(0, 0, -1);
AND_GIVEN("normalv <- vector(0, 0, -1)")
{
Tuple normalv = Tuple::Vector(0, 0, -1);
AND_GIVEN("light <- point_light(point(0, 0, -10),color(1, 1, 1))")
{
PointLight light = PointLight(Tuple::Point(0, 0, -10), Color(1, 1, 1));
WHEN("result <- lighting(m, light, position, eyev, normalv)")
{
Color result = m.lighting(light, position, eyev, normalv);
THEN("result = color(1.9, 1.9, 1.9)")
{
REQUIRE(result == Color(1.9, 1.9, 1.9));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Lighting with eye between the light & surface, eye offset 45°", "[features/materials.feature]")
{
Tuple position = Tuple::Point(0, 0, 0);
Material m;
GIVEN("eyev <- vector(0, sqrt(2)/2, -sqrt(2)/2)")
{
Tuple eyev = Tuple::Vector(0, sqrt(2) / 2, -sqrt(2) / 2);
AND_GIVEN("normalv <- vector(0, 0, -1)")
{
Tuple normalv = Tuple::Vector(0, 0, -1);
AND_GIVEN("light <- point_light(point(0, 0, -10),color(1, 1, 1))")
{
PointLight light = PointLight(Tuple::Point(0, 0, -10), Color(1, 1, 1));
WHEN("result <- lighting(m, light, position, eyev, normalv)")
{
Color result = m.lighting(light, position, eyev, normalv);
THEN("result = color(1.0, 1.0, 1.0)")
{
REQUIRE(result == Color(1.0, 1.0, 1.0));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Lighting with the eye opposite surface, light offset 45°", "[features/materials.feature]")
{
Tuple position = Tuple::Point(0, 0, 0);
Material m;
GIVEN("eyev <- vector(0, 0, -1")
{
Tuple eyev = Tuple::Vector(0, 0, -1);
AND_GIVEN("normalv <- vector(0, 0, -1)")
{
Tuple normalv = Tuple::Vector(0, 0, -1);
AND_GIVEN("light <- point_light(point(0, 10, -10),color(1, 1, 1))")
{
PointLight light = PointLight(Tuple::Point(0, 10, -10), Color(1, 1, 1));
WHEN("result <- lighting(m, light, position, eyev, normalv)")
{
Color result = m.lighting(light, position, eyev, normalv);
THEN("result = color(0.7364, 0.7364, 0.7364)")
{
REQUIRE(result == Color(0.7364, 0.7364, 0.7364));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Lighting with the eye in the path of the reflection vector", "[features/materials.feature]")
{
Tuple position = Tuple::Point(0, 0, 0);
Material m;
GIVEN("eyev <- vector(0, -sqrt(2)/2, -sqrt(2)/2")
{
Tuple eyev = Tuple::Vector(0, -sqrt(2) / 2, -sqrt(2) / 2);
AND_GIVEN("normalv <- vector(0, 0, -1)")
{
Tuple normalv = Tuple::Vector(0, 0, -1);
AND_GIVEN("light <- point_light(point(0, 10, -10),color(1, 1, 1))")
{
PointLight light = PointLight(Tuple::Point(0, 10, -10), Color(1, 1, 1));
WHEN("result <- lighting(m, light, position, eyev, normalv)")
{
Color result = m.lighting(light, position, eyev, normalv);
THEN("result = color(1.6364, 1.6364, 1.6364)")
{
REQUIRE(result == Color(1.6364, 1.6364, 1.6364));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Lighting with the light behind the surface", "[features/materials.feature]")
{
Tuple position = Tuple::Point(0, 0, 0);
Material m;
GIVEN("eyev <- vector(0, 0, -1")
{
Tuple eyev = Tuple::Vector(0, 0, -1);
AND_GIVEN("normalv <- vector(0, 0, -1)")
{
Tuple normalv = Tuple::Vector(0, 0, -1);
AND_GIVEN("light <- point_light(point(0, 0, 10),color(1, 1, 1))")
{
PointLight light = PointLight(Tuple::Point(0, 0, 10), Color(1, 1, 1));
WHEN("result <- lighting(m, light, position, eyev, normalv)")
{
Color result = m.lighting(light, position, eyev, normalv);
THEN("result = color(0.1, 0.1, 0.1)")
{
REQUIRE(result == Color(0.1, 0.1, 0.1));
}
}
}
}
}
}