/*! * 10_patterns.cpp * * Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA * * @Author: NADAL Jean-Baptiste * @Date: 27/02/2024 * */ /*---------------------------------------------------------------------------*/ #include #include "raytracing.h" #include "common_data.h" using namespace Raytracer; /* ------------------------------------------------------------------------- */ SCENARIO("Creating a stripe pattern", "[features/patterns.feature]") { GIVEN("pattern <- stripe_pattern(white, black)") { StripePattern pattern(Color::White(), Color::Black()); THEN("pattern.a = white") { REQUIRE(pattern.a() == Color::White()); } AND_THEN("pattern.b = black") { REQUIRE(pattern.b() == Color::Black()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("A stripe pattern is constant in y", "[features/patterns.feature]") { GIVEN("pattern <- stripe_pattern(white, black)") { StripePattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 1, 0) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 1, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 2, 0) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 2, 0)) == Color::White()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("A stripe pattern is constant in z", "[features/patterns.feature]") { GIVEN("pattern <- stripe_pattern(white, black)") { StripePattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 0, 1) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 1)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 0, 2) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 2)) == Color::White()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("A stripe pattern alternates in x", "[features/patterns.feature]") { GIVEN("pattern <- stripe_pattern(white, black)") { StripePattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0.9, 0, 0) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(0.9, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(1, 0, 0) = black") { REQUIRE(pattern.pattern_at(Tuple::Point(1, 0, 0)) == Color::Black()); } AND_THEN("pattern_at(pattern, point(-0.1, 0, 0) = black") { REQUIRE(pattern.pattern_at(Tuple::Point(-0.1, 0, 0)) == Color::Black()); } AND_THEN("pattern_at(pattern, point(-1, 0, 0) = black") { REQUIRE(pattern.pattern_at(Tuple::Point(-1, 0, 0)) == Color::Black()); } AND_THEN("pattern_at(pattern, point(-1.1, 0, 0) = white") { REQUIRE(pattern.pattern_at(Tuple::Point(-1.1, 0, 0)) == Color::White()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("Lightning with a pattern applied", "[features/materials.feature]") { GIVEN("object <- sphere()") { Sphere object; Material m; GIVEN("m.pattern <- stripe_pattern(color(1, 1, 1), color(0, 0, 0))") { m.set_pattern(new StripePattern(Color(1, 1, 1), Color(0, 0, 0))); AND_GIVEN("m.ambient <- 1") { m.set_ambient(1); AND_GIVEN("m.diffuse <- 0") { m.set_diffuse(0); AND_GIVEN("m.specular <- 0") { m.set_specular(0); AND_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("c1 <- lighting(m, light, point(0.9, 0, 0), eyev, normalv, false)") { Color c1 = m.lighting(&object, light, Tuple::Point(0.9, 0, 0), eyev, normalv, false); AND_WHEN("c2 <- lighting(m, light, point(1.1, 0, 0), eyev, normalv, false)") { Color c2 = m.lighting(&object, light, Tuple::Point(1.1, 0, 0), eyev, normalv, false); THEN("c1 = color(1, 1, 1)") { REQUIRE(c1 == Color(1, 1, 1)); } AND_THEN("c2 = color(0, 0, 0)") { REQUIRE(c2 == Color(0, 0, 0)); } } } } } } } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Stripes with an object transformation", "[features/patterns.feature]") { GIVEN("object <- sphere()") { Sphere object; AND_GIVEN("set_transform(object, scaling(2, 2, 2))") { object.set_transform(Matrix::scaling(2, 2, 2)); AND_GIVEN("pattern <- stripe_pattern(white, black)") { StripePattern pattern(Color::White(), Color::Black()); WHEN("c <- pattern_at_shape(pattern, object, point(1.5, 0, 0))") { Color c = pattern.pattern_at_shape(&object, Tuple::Point(1.5, 0, 0)); THEN("c = white") { REQUIRE(c == Color::White()); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Stripes with an pattern transformation", "[features/patterns.feature]") { GIVEN("object <- sphere()") { Sphere object; AND_GIVEN("pattern <- stripe_pattern(white, black)") { StripePattern pattern(Color::White(), Color::Black()); AND_GIVEN("set_transform(pattern, scaling(2, 2, 2))") { pattern.set_transform(Matrix::scaling(2, 2, 2)); WHEN("c <- pattern_at_shape(pattern, object, point(1.5, 0, 0))") { Color c = pattern.pattern_at_shape(&object, Tuple::Point(1.5, 0, 0)); THEN("c = white") { REQUIRE(c == Color::White()); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Stripes with both an object and pattern transformation", "[features/patterns.feature]") { GIVEN("object <- sphere()") { Sphere object; AND_GIVEN("set_transform(object, scaling(2, 2, 2))") { object.set_transform(Matrix::scaling(2, 2, 2)); AND_GIVEN("pattern <- stripe_pattern(white, black)") { StripePattern pattern(Color::White(), Color::Black()); AND_GIVEN("set_pattern_transform(pattern, translation(0.5, 0, 0)))") { pattern.set_transform(Matrix::translation(0.5, 0, 0)); WHEN("c <- pattern_at_object(pattern, object, point(2.5, 0, 0))") { Color c = pattern.pattern_at_shape(&object, Tuple::Point(1.5, 0, 0)); THEN("c = white") { REQUIRE(c == Color::White()); } } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("The default pattern transformation", "[features/patterns.feature]") { GIVEN("pattern <- test_pattern()") { TestPattern pattern; THEN("pattern.transform = identity_matrix") { REQUIRE(pattern.transform() == Matrix::identity()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("Assigning a transformation to the pattern", "[features/patterns.feature]") { GIVEN("pattern <- test_pattern()") { TestPattern pattern; WHEN("set_pattern_transform(pattern, translation(1,2,3))") { pattern.set_transform(Matrix::translation(1, 2, 3)); THEN("pattern.transform = translation(1, 2, 3)") { REQUIRE(pattern.transform() == Matrix::translation(1, 2, 3)); } } } } /* ------------------------------------------------------------------------- */ SCENARIO("A pattern with an object transformation", "[features/patterns.feature]") { GIVEN("shape <- sphere()") { Sphere shape; AND_GIVEN("set_transform(shape, scaling(2, 2, 2))") { shape.set_transform(Matrix::scaling(2, 2, 2)); AND_GIVEN("pattern <- test_pattern()") { TestPattern pattern; WHEN("Color c <- pattern_at_shape(pattern, shape, point(2, 3, 4))") { Color c = pattern.pattern_at_shape(&shape, Tuple::Point(2, 3, 4)); THEN("c = color(1, 1.5, 2)") { REQUIRE(c == Color(1, 1.5, 2)); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("A pattern with a pattern transformation", "[features/patterns.feature]") { GIVEN("shape <- sphere()") { Sphere shape; AND_GIVEN("pattern <- test_pattern()") { TestPattern pattern; AND_GIVEN("set_pattern_transform(pattern, scaling(2, 2, 2))") { pattern.set_transform(Matrix::scaling(2, 2, 2)); WHEN("c <- pattern_at_shape(pattern, shape, point(2, 3, 4))") { Color c = pattern.pattern_at_shape(&shape, Tuple::Point(2, 3, 4)); THEN("c = color(1, 1.5, 2)") { REQUIRE(c == Color(1, 1.5, 2)); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("A pattern with both an object and a pattern transformation", "[features/patterns.feature]") { GIVEN("shape <- sphere()") { Sphere shape; AND_GIVEN("set_transform(shape, scaling(2, 2, 2))") { shape.set_transform(Matrix::scaling(2, 2, 2)); AND_GIVEN("pattern <- test_pattern()") { TestPattern pattern; AND_GIVEN("set_pattern_transform(pattern, translation(0.5, 1, 1.5))") { pattern.set_transform(Matrix::translation(0.5, 1, 1.5)); WHEN("c <- pattern_at_shape(pattern, shape, point(2.5, 3, 3.5))") { Color c = pattern.pattern_at_shape(&shape, Tuple::Point(2.5, 3, 3.5)); THEN("c = color(0.75, 0.5, 0.25)") { REQUIRE(c == Color(0.75, 0.5, 0.25)); } } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("A gradient linearly interpolates between colors", "[features/patterns.feature]") { GIVEN("pattern <- gradient_pattern(white, black)") { GradientPattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0.25, 0, 0)) == color(0.75, 0.75, 0.75)") { REQUIRE(pattern.pattern_at(Tuple::Point(0.25, 0, 0)) == Color(0.75, 0.75, 0.75)); } AND_THEN("pattern_at(pattern, point(0.5, 0, 0)) == color(0.5, 0.5, 0.5)") { REQUIRE(pattern.pattern_at(Tuple::Point(0.5, 0, 0)) == Color(0.5, 0.5, 0.5)); } AND_THEN("pattern_at(pattern, point(0.75, 0, 0)) == color(0,25, 0.25, 0.25)") { REQUIRE(pattern.pattern_at(Tuple::Point(0.75, 0, 0)) == Color(0.25, 0.25, 0.25)); } } } /* ------------------------------------------------------------------------- */ SCENARIO("A ring should extend in both x and z", "[features/patterns.feature]") { GIVEN("pattern <- ring_pattern(white, black)") { RingPattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(1, 0, 0)) == black") { REQUIRE(pattern.pattern_at(Tuple::Point(1, 0, 0)) == Color::Black()); } AND_THEN("pattern_at(pattern, point(0, 0, 1)) == black") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 1)) == Color::Black()); } // 0.708 = just slightly more than sqrt(2)2 AND_THEN("pattern_at(pattern, point(0.708, 0, 0.708)) == black") { REQUIRE(pattern.pattern_at(Tuple::Point(0.708, 0, 0.708)) == Color::Black()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("Checkers should repeat in x", "[features/patterns.feature]") { GIVEN("pattern <- checkers_pattern(white, black)") { CheckersPattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0.99, 0, 0)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0.99, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(1.01, 0, 0)) == black") { REQUIRE(pattern.pattern_at(Tuple::Point(1.01, 0, 0)) == Color::Black()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("Checkers should repeat in y", "[features/patterns.feature]") { GIVEN("pattern <- checkers_pattern(white, black)") { CheckersPattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 0.99, 0)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0.99, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 1.01, 0)) == black") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 1.01, 0)) == Color::Black()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("Checkers should repeat in z", "[features/patterns.feature]") { GIVEN("pattern <- checkers_pattern(white, black)") { CheckersPattern pattern(Color::White(), Color::Black()); THEN("pattern_at(pattern, point(0, 0, 0)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 0, 0.99)) == white") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0.99)) == Color::White()); } AND_THEN("pattern_at(pattern, point(0, 0, 1.01)) == black") { REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 1.01)) == Color::Black()); } } }