[ADD] ray_for_pixel to camera

This commit is contained in:
2024-02-25 23:47:40 +01:00
parent 11890d6273
commit 45ff10ecc8
5 changed files with 123 additions and 8 deletions

View File

@@ -569,3 +569,69 @@ SCENARIO("The pixel size for a vertical canvas", "[features/camera.feature]")
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Constructing a ray through the center of the canvas", "[features/camera.feature]")
{
GIVEN("c <- camera(201, 101, pi/2)")
{
Camera c(201, 101, std::numbers::pi / 2);
WHEN("r <- ray_for_pixel(c, 100, 50)")
{
Ray r = c.ray_for_pixel(100, 50);
THEN("r.origin = point(0, 0, 0)")
{
REQUIRE(r.origin() == Tuple::Point(0, 0, 0));
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Constructing a ray through a corner of the canvas", "[features/camera.feature]")
{
GIVEN("c <- camera(201, 101, pi/2)")
{
Camera c(201, 101, std::numbers::pi / 2);
WHEN("r <- ray_for_pixel(c, 0, 0)")
{
Ray r = c.ray_for_pixel(0, 0);
THEN("r.origin = point(0, 0, 0)")
{
REQUIRE(r.origin() == Tuple::Point(0, 0, 0));
}
AND_THEN("r.direction = vector(0.66519, 0.33259, -0.66851)")
{
REQUIRE(r.direction() == Tuple::Vector(0.66519, 0.33259, -0.66851));
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Constructing a ray when the camera is transformed", "[features/camera.feature]")
{
GIVEN("c <- camera(201, 101, pi/2)")
{
Camera c(201, 101, std::numbers::pi / 2);
WHEN("c.transform <- rotation_y(pi/4) * translation(0, -2, 5)")
{
c.set_transform(Matrix::rotation_y(std::numbers::pi / 4) * Matrix::translation(0, -2, 5));
AND_WHEN("r <- ray_for_pixel(c, 100, 50)")
{
Ray r = c.ray_for_pixel(100, 50);
THEN("r.origin = point(0, 2, -5)")
{
REQUIRE(r.origin() == Tuple::Point(0, 2, -5));
}
AND_THEN("r.direction = vector(sqrt(2) / 2, 0, -sqrt(2) / 2)")
{
REQUIRE(r.direction() == Tuple::Vector(sqrt(2) / 2, 0, -sqrt(2) / 2));
}
}
}
}
}