diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 8001080..a98e572 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -7,4 +7,4 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(chapter_05 chapter_05.cpp) -target_link_libraries(chapter_05 PRIVATE raytracing) +target_link_libraries(chapter_05 PRIVATE raytracing gcov) diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index ebf7be1..8407a0e 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -1,9 +1,11 @@ cmake_minimum_required(VERSION 3.14) +project(raytracing) + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -project(raytracing) +add_definitions(--coverage) add_library(raytracing diff --git a/raytracing/src/object.cpp b/raytracing/src/object.cpp index 64bfb49..04cb7cc 100644 --- a/raytracing/src/object.cpp +++ b/raytracing/src/object.cpp @@ -103,6 +103,14 @@ void Object::set_transform(const Matrix &a_transform_matrix) /* ------------------------------------------------------------------------- */ +Tuple Object::normal_at(const Tuple &a_point) +{ + Tuple the_result = a_point - Tuple::Point(0, 0, 0); + return the_result.normalize(); +} + +/* ------------------------------------------------------------------------- */ + Intersections Object::intersect(Ray &a_ray) { Intersections the_ret; diff --git a/raytracing/src/object.h b/raytracing/src/object.h index 8fd71e6..5bba391 100644 --- a/raytracing/src/object.h +++ b/raytracing/src/object.h @@ -56,6 +56,8 @@ namespace Raytracer Matrix &transform(void); void set_transform(const Matrix &a_transform_matrix); + Tuple normal_at(const Tuple &a_point); + virtual Intersections intersect(Ray &a_ray); protected: diff --git a/tests/06_light_shading.cpp b/tests/06_light_shading.cpp index aec573b..d9776d7 100644 --- a/tests/06_light_shading.cpp +++ b/tests/06_light_shading.cpp @@ -37,10 +37,13 @@ SCENARIO("The normal on a sphere at point a on the x axis", "[features/spheres.f { 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)); } } } @@ -52,10 +55,13 @@ SCENARIO("The normal on a sphere at point a on the y axis", "[features/spheres.f { 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)); } } } @@ -67,10 +73,13 @@ SCENARIO("The normal on a sphere at point a on the z axis", "[features/spheres.f { 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)); } } } @@ -82,10 +91,13 @@ SCENARIO("The normal on a sphere at a nonaxial point", "[features/spheres.featur { GIVEN("s <- sphere()") { + Sphere s; WHEN("n <- normal_at(s, point(sqrt(3)/3,sqrt(3)/3,sqrt(3)/3))") { - THEN("n = vector(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)); } } } @@ -97,10 +109,13 @@ 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()); } } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 478fbd4..7148328 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,4 +21,6 @@ add_executable(raytracing_test include_directories("${CMAKE_SOURCE_DIR}/tests") -target_link_libraries(raytracing_test PRIVATE raytracing) +target_link_libraries(raytracing_test PRIVATE raytracing gcov) + +add_test(NAME raytracing_test COMMAND tests/raytracing_test)