[WIP] Add ray and sphere basic

This commit is contained in:
2024-02-05 23:33:48 +01:00
parent 4251291170
commit 480d8c56da
8 changed files with 353 additions and 0 deletions

121
tests/05_rays.cpp Normal file
View File

@@ -0,0 +1,121 @@
/*!
* 05_rays.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: 05/02/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch.hpp>
#include "raytracing.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
TEST_CASE("[05][Rays] Creating and querying a ray", "[Rays]")
{
Tuple origin = Tuple::Point(1, 2, 3);
Tuple direction = Tuple::Vector(4, 5, 6);
Ray r(origin, direction);
REQUIRE(r.origin() == origin);
REQUIRE(r.direction() == direction);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[05][Rays] Computing a point from a distance", "[Rays]")
{
Ray r(Tuple::Point(2, 3, 4), Tuple::Vector(1, 0, 0));
REQUIRE(r.position(0) == Tuple::Point(2, 3, 4));
REQUIRE(r.position(1) == Tuple::Point(3, 3, 4));
REQUIRE(r.position(-1) == Tuple::Point(1, 3, 4));
REQUIRE(r.position(2.5) == Tuple::Point(4.5, 3, 4));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[05][Rays] a ray intersects a sphere at two points", "[Rays]")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
Sphere s;
auto xs = r.intersect(s);
REQUIRE(xs.size() == 2);
REQUIRE(xs[0] == 4.0);
REQUIRE(xs[1] == 6.0);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[05][Rays] a ray intersects a sphere at a tangent", "[Rays]")
{
Ray r(Tuple::Point(0, 1, -5), Tuple::Vector(0, 0, 1));
Sphere s;
auto xs = r.intersect(s);
REQUIRE(xs.size() == 2);
REQUIRE(xs[0] == 5.0);
REQUIRE(xs[1] == 5.0);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[05][Rays] a ray misses a sphere", "[Rays]")
{
Ray r(Tuple::Point(0, 2, -5), Tuple::Vector(0, 0, 1));
Sphere s;
auto xs = r.intersect(s);
REQUIRE(xs.size() == 0);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[05][Rays] a originates inside a sphere", "[Rays]")
{
Ray r(Tuple::Point(0, 0, 0), Tuple::Vector(0, 0, 1));
Sphere s;
auto xs = r.intersect(s);
REQUIRE(xs.size() == 2);
REQUIRE(xs[0] == -1.0);
REQUIRE(xs[1] == 1.0);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[05][Rays] a sphere is behind a ray", "[Rays]")
{
Ray r(Tuple::Point(0, 0, 5), Tuple::Vector(0, 0, 1));
Sphere s;
auto xs = r.intersect(s);
REQUIRE(xs.size() == 2);
REQUIRE(xs[0] == -6.0);
REQUIRE(xs[1] == -4.0);
}

View File

@@ -15,6 +15,7 @@ add_executable(main_test
02_2_canvas.cpp
03_matrix.cpp
04_transformations.cpp
05_rays.cpp
)
include_directories("${CMAKE_SOURCE_DIR}/tests")