Files
raytracer_challenge/tests/10_patterns.cpp
2024-02-29 18:39:45 +01:00

269 lines
9.7 KiB
C++

/*!
* 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 <catch.hpp>
#include "raytracing.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
SCENARIO("Creating a stripe pattern", "[features/patterns.feature]")
{
GIVEN("pattern <- strip_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 <- strip_pattern(white, black)")
{
StripePattern pattern(Color::White(), Color::Black());
THEN("stripe_at(pattern, point(0, 0, 0) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White());
}
AND_THEN("stripe_at(pattern, point(0, 1, 0) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0, 1, 0)) == Color::White());
}
AND_THEN("stripe_at(pattern, point(0, 2, 0) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0, 2, 0)) == Color::White());
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A stripe pattern is constant in z", "[features/patterns.feature]")
{
GIVEN("pattern <- strip_pattern(white, black)")
{
StripePattern pattern(Color::White(), Color::Black());
THEN("stripe_at(pattern, point(0, 0, 0) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White());
}
AND_THEN("stripe_at(pattern, point(0, 0, 1) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 1)) == Color::White());
}
AND_THEN("stripe_at(pattern, point(0, 0, 2) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 2)) == Color::White());
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A stripe pattern alternates in x", "[features/patterns.feature]")
{
GIVEN("pattern <- strip_pattern(white, black)")
{
StripePattern pattern(Color::White(), Color::Black());
THEN("stripe_at(pattern, point(0, 0, 0) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White());
}
AND_THEN("stripe_at(pattern, point(0.9, 0, 0) = white")
{
REQUIRE(pattern.stripe_at(Tuple::Point(0.9, 0, 0)) == Color::White());
}
AND_THEN("stripe_at(pattern, point(1, 0, 0) = black")
{
REQUIRE(pattern.stripe_at(Tuple::Point(1, 0, 0)) == Color::Black());
}
AND_THEN("stripe_at(pattern, point(-0.1, 0, 0) = black")
{
REQUIRE(pattern.stripe_at(Tuple::Point(-0.1, 0, 0)) == Color::Black());
}
AND_THEN("stripe_at(pattern, point(-1, 0, 0) = black")
{
REQUIRE(pattern.stripe_at(Tuple::Point(-1, 0, 0)) == Color::Black());
}
AND_THEN("stripe_at(pattern, point(-1.1, 0, 0) = white")
{
REQUIRE(pattern.stripe_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 <- strip_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 <- stripe_at_object(pattern, object, point(1.5, 0, 0))")
{
Color c = pattern.stripe_at_object(&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_pattern_transform(Matrix::scaling(2, 2, 2));
WHEN("c <- stripe_at_object(pattern, object, point(1.5, 0, 0))")
{
Color c = pattern.stripe_at_object(&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_pattern_transform(Matrix::translation(0.5, 0, 0));
WHEN("c <- stripe_at_object(pattern, object, point(2.5, 0, 0))")
{
Color c = pattern.stripe_at_object(&object, Tuple::Point(1.5, 0, 0));
THEN("c = white")
{
REQUIRE(c == Color::White());
}
}
}
}
}
}
}