Files
raytracer_challenge/tests/06_light_shading.cpp

315 lines
8.8 KiB
C++

/*!
* 06_light_shading.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: 14/02/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch.hpp>
#include "raytracing.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
SCENARIO("The normal on a sphere at point a on the x axis", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
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)")
{
REQUIRE(n == Tuple::Vector(1, 0, 0));
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The normal on a sphere at point a on the y axis", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
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)")
{
REQUIRE(n == Tuple::Vector(0, 1, 0));
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The normal on a sphere at point a on the z axis", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
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)")
{
REQUIRE(n == Tuple::Vector(0, 0, 1));
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The normal on a sphere at a nonaxial point", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
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))")
{
REQUIRE(n == Tuple::Vector(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3));
}
}
}
}
/* ------------------------------------------------------------------------- */
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))")
{
Tuple n = s.normal_at(Tuple::Point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3));
THEN("n = normalize(n)")
{
REQUIRE(n == n.normalize());
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Computing the normal on a translated sphere", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
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))")
{
Tuple n = s.normal_at(Tuple::Point(0, 1.70711, -0.70711));
THEN("n = vector(0,0.70711, -0.70711)")
{
REQUIRE(n == Tuple::Vector(0, 0.70711, -0.70711));
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Computing the normal on a transformed sphere", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
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))")
{
Tuple n = s.normal_at(Tuple::Point(0, sqrt(2) / 2, -sqrt(2) / 2));
THEN("n = vector(0,97014, -0.24254)")
{
REQUIRE(n == Tuple::Vector(0, 0.97014, -0.24254));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Reflecting a vector approaching at 45°", "[features/tuples.feature]")
{
GIVEN("v <-vector(1, -1, 0)")
{
Tuple v = Tuple::Vector(1, -1, 0);
AND_GIVEN("n <-vector(0, 1, 0)")
{
Tuple n = Tuple::Vector(0, 1, 0);
WHEN("r <- reflect(v,n)")
{
Tuple r = v.reflect(n);
THEN("r = vector(1,1,0)")
{
REQUIRE(r == Tuple::Vector(1, 1, 0));
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Reflecting a vector off a slanted surface", "[features/tuples.feature]")
{
GIVEN("v <-vector(0, -1, 0)")
{
Tuple v = Tuple::Vector(0, -1, 0);
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)")
{
Tuple r = v.reflect(n);
THEN("r = vector(1,0,0)")
{
REQUIRE(r == Tuple::Vector(1, 0, 0));
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A point light has a position and intensity", "[features/lights.feature]")
{
GIVEN("intensity <- color(1, 1, 1)")
{
Color intensity(1, 1, 1);
AND_GIVEN("position <- point(0, 0, 0)")
{
Tuple position = Tuple::Point(0, 0, 0);
WHEN("light <- point_light(position(position, intensity))")
{
PointLight light = PointLight(position, intensity);
THEN("light.position = position")
{
REQUIRE(light.position() == position);
}
AND_THEN("light.intensity = intensity")
{
REQUIRE(light.intensity() == intensity);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The default material", "[features/materials.feature]")
{
GIVEN("m <- material()")
{
Material m;
THEN("m.color = color(1,1,1)")
{
REQUIRE(m.color() == Color(1, 1, 1));
}
AND_THEN("m.ambient = 0.1")
{
REQUIRE(m.ambient() == 0.1);
}
AND_THEN("m.diffuse = 0.9")
{
REQUIRE(m.diffuse() == 0.9);
}
AND_THEN("m.specular = 0.9")
{
REQUIRE(m.specular() == 0.9);
}
AND_THEN("m.shininess = 200.0")
{
REQUIRE(m.shininess() == 200.0);
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A sphere has a default material", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
WHEN("m <- s.material")
{
Material m = s.material();
THEN("m = material()")
{
REQUIRE(m == Material());
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A sphere may be assigned a material", "[features/spheres.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("m <- material")
{
Material m;
AND_GIVEN("m.ambient <- 1")
{
m.set_ambient(1);
WHEN("s.material <- m")
{
s.set_material(m);
THEN("s.material = m")
{
s.material() == m;
}
}
}
}
}
}