[ADD] Add View treansform

This commit is contained in:
2024-02-23 14:49:26 +01:00
parent ea33f1b985
commit 9b85b5940a
3 changed files with 132 additions and 0 deletions

View File

@@ -390,3 +390,116 @@ SCENARIO("The color with an intersection behind the ray", "[features/world.featu
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The transformation matrix for the default orientation", "[features/transformations.feature]")
{
GIVEN("from <- point(0, 0, 0)")
{
Tuple from = Tuple::Point(0, 0, 0);
AND_GIVEN("to <- point(0, 0, -1)")
{
Tuple to = Tuple::Point(0, 0, -1);
AND_GIVEN("up <- vector(0, 1, 0)")
{
Tuple up = Tuple::Vector(0, 1, 0);
WHEN("t <- view_transform(from, to, up)")
{
Matrix t = Matrix::view_transform(from, to, up);
THEN("t = identify_matrix")
{
REQUIRE(t == Matrix::identity());
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A view transformation matrix looking in positive z direction", "[features/transformations.feature]")
{
GIVEN("from <- point(0, 0, 0)")
{
Tuple from = Tuple::Point(0, 0, 0);
AND_GIVEN("to <- point(0, 0, 1)")
{
Tuple to = Tuple::Point(0, 0, 1);
AND_GIVEN("up <- vector(0, 1, 0)")
{
Tuple up = Tuple::Vector(0, 1, 0);
WHEN("t <- view_transform(from, to, up)")
{
Matrix t = Matrix::view_transform(from, to, up);
THEN("t = scaling(-1, 1, -1)")
{
REQUIRE(t == Matrix::scaling(-1, 1, -1));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A view transformation moves the world", "[features/transformations.feature]")
{
GIVEN("from <- point(0, 0, 8)")
{
Tuple from = Tuple::Point(0, 0, 8);
AND_GIVEN("to <- point(0, 0, 0)")
{
Tuple to = Tuple::Point(0, 0, 0);
AND_GIVEN("up <- vector(0, 1, 0)")
{
Tuple up = Tuple::Vector(0, 1, 0);
WHEN("t <- view_transform(from, to, up)")
{
Matrix t = Matrix::view_transform(from, to, up);
THEN("t = translation(0, 0, -8)")
{
REQUIRE(t == Matrix::translation(0, 0, -8));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("An arbitrary view transformation", "[features/transformations.feature]")
{
GIVEN("from <- point(1, 3, 2)")
{
Tuple from = Tuple::Point(1, 3, 2);
AND_GIVEN("to <- point(4, -2, 8)")
{
Tuple to = Tuple::Point(4, -2, 8);
AND_GIVEN("up <- vector(1, 1, 0)")
{
Tuple up = Tuple::Vector(1, 1, 0);
WHEN("t <- view_transform(from, to, up)")
{
Matrix t = Matrix::view_transform(from, to, up);
THEN("t iq the following 4x4 matrix")
{
// | -0.50709 | 0.50709 | 0.67612 | -2.36643 |
// | 0.76772 | 0.60609 | 0.12122 | -2.82843 |
// | -0.35857 | 0.59761 | -0.71714 | 0.00000 |
// | 0.00000 | 0.00000 | 0.00000 | 1.00000 |
REQUIRE(t == Matrix({
{-0.50709, 0.50709, 0.67612, -2.36643},
{ 0.76772, 0.60609, 0.12122, -2.82843},
{-0.35857, 0.59761, -0.71714, 0.00000},
{ 0.00000, 0.00000, 0.00000, 1.00000}
}));
}
}
}
}
}
}