From 73c6e5479652158412ec16884e2db74de674a76f Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 28 Feb 2024 14:51:19 +0100 Subject: [PATCH] [WIP] start chapter 10. --- raytracing/CMakeLists.txt | 4 + raytracing/include/raytracing.h | 4 + raytracing/src/renderer/stripe-pattern.cpp | 67 +++++++++++ raytracing/src/renderer/stripe-pattern.h | 54 +++++++++ tests/10_patterns.cpp | 125 +++++++++++++++++++++ tests/CMakeLists.txt | 3 +- 6 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 raytracing/src/renderer/stripe-pattern.cpp create mode 100644 raytracing/src/renderer/stripe-pattern.h create mode 100644 tests/10_patterns.cpp diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 56a1ad4..dfbc7c6 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -18,12 +18,16 @@ add_library(raytracing src/core/intersections.cpp src/core/matrix.cpp src/core/tuple.cpp + src/lights/point-light.cpp + src/renderer/camera.cpp src/renderer/canvas.cpp src/renderer/material.cpp src/renderer/ray.cpp + src/renderer/stripe-pattern.cpp src/renderer/world.cpp + src/shapes/plane.cpp src/shapes/shape.cpp src/shapes/sphere.cpp diff --git a/raytracing/include/raytracing.h b/raytracing/include/raytracing.h index 66ee8b5..d1bb314 100644 --- a/raytracing/include/raytracing.h +++ b/raytracing/include/raytracing.h @@ -32,11 +32,15 @@ #include "core/intersections.h" #include "core/matrix.h" #include "core/tuple.h" + #include "lights/point-light.h" + #include "renderer/camera.h" #include "renderer/canvas.h" #include "renderer/material.h" #include "renderer/ray.h" +#include "renderer/stripe-pattern.h" #include "renderer/world.h" + #include "shapes/plane.h" #include "shapes/sphere.h" diff --git a/raytracing/src/renderer/stripe-pattern.cpp b/raytracing/src/renderer/stripe-pattern.cpp new file mode 100644 index 0000000..6799651 --- /dev/null +++ b/raytracing/src/renderer/stripe-pattern.cpp @@ -0,0 +1,67 @@ +/*! + * stripe-pattern.cpp + * + * Copyright (c) 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: 28/02/2024 + * + */ + +// This is an independent project of an individual developer. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com + +/* ------------------------------------------------------------------------- */ + +#include + +#include "core/common.h" + +#include "stripe-pattern.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +StripePattern::StripePattern(const Color &a_color_a, const Color &a_color_b) : m_a(a_color_a), m_b(a_color_b) +{ +} + +/* ------------------------------------------------------------------------- */ + +const Color &StripePattern::a(void) const +{ + return m_a; +} + +/* ------------------------------------------------------------------------- */ + +const Color &StripePattern::b(void) const +{ + return m_b; +} + +/* ------------------------------------------------------------------------- */ + +const Color &StripePattern::stripe_at(Tuple a_point) const +{ + if (((int)std::floor(a_point.x()) % 2) == 0) + return m_a; + + return m_b; +} diff --git a/raytracing/src/renderer/stripe-pattern.h b/raytracing/src/renderer/stripe-pattern.h new file mode 100644 index 0000000..e133ca0 --- /dev/null +++ b/raytracing/src/renderer/stripe-pattern.h @@ -0,0 +1,54 @@ +/*! + * stripe-pattern.h + * + * Copyright (c) 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: 28/02/2024 + * + */ + +#ifndef _RAYTRACER_STRIPE_PATTERN_H +#define _RAYTRACER_STRIPE_PATTERN_H + +/* ------------------------------------------------------------------------- */ + +#include "core/color.h" +#include "core/tuple.h" + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class StripePattern + { + public: + StripePattern(const Color &a_color_a, const Color &a_color_b); + + const Color &a(void) const; + const Color &b(void) const; + + const Color &stripe_at(Tuple a_point) const; + + private: + Color m_a; + Color m_b; + }; +}; // namespace Raytracer + +#endif /* _RAYTRACER_STRIPE_PATTERN_H */ diff --git a/tests/10_patterns.cpp b/tests/10_patterns.cpp new file mode 100644 index 0000000..3d4682d --- /dev/null +++ b/tests/10_patterns.cpp @@ -0,0 +1,125 @@ +/*! + * 10_patterns.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: 27/02/2024 + * + */ + +/*---------------------------------------------------------------------------*/ + +#include + +#include "raytracing.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +SCENARIO("Creating a stripe pattern", "[features/patterns.feature]") +{ + GIVEN("pattern <- strip_pattern(white, black)") + { + StripePattern pattern(Color::White(), Color::Black()); + THEN("pattern.a = white") + { + REQUIRE(pattern.a() == Color::White()); + } + AND_THEN("pattern.b = black") + { + REQUIRE(pattern.b() == Color::Black()); + } + } +} + +/* ------------------------------------------------------------------------- */ + +SCENARIO("A stripe pattern is constant in y", "[features/patterns.feature]") +{ + GIVEN("pattern <- strip_pattern(white, black)") + { + StripePattern pattern(Color::White(), Color::Black()); + THEN("stripe_at(pattern, point(0, 0, 0) = white") + { + } + AND_THEN("stripe_at(pattern, point(0, 1, 0) = white") + { + } + AND_THEN("stripe_at(pattern, point(0, 2, 0) = white") + { + } + } +} + +/* ------------------------------------------------------------------------- */ + +SCENARIO("A stripe pattern is constant in z", "[features/patterns.feature]") +{ + GIVEN("pattern <- strip_pattern(white, black)") + { + StripePattern pattern(Color::White(), Color::Black()); + THEN("stripe_at(pattern, point(0, 0, 0) = white") + { + REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White()); + } + AND_THEN("stripe_at(pattern, point(0, 0, 1) = white") + { + REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 1)) == Color::White()); + } + AND_THEN("stripe_at(pattern, point(0, 0, 2) = white") + { + REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 2)) == Color::White()); + } + } +} + +/* ------------------------------------------------------------------------- */ + +SCENARIO("A stripe pattern alternates in x", "[features/patterns.feature]") +{ + GIVEN("pattern <- strip_pattern(white, black)") + { + StripePattern pattern(Color::White(), Color::Black()); + THEN("stripe_at(pattern, point(0, 0, 0) = white") + { + REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White()); + } + AND_THEN("stripe_at(pattern, point(0.9, 0, 0) = white") + { + REQUIRE(pattern.stripe_at(Tuple::Point(0.9, 0, 0)) == Color::White()); + } + AND_THEN("stripe_at(pattern, point(1, 0, 0) = black") + { + REQUIRE(pattern.stripe_at(Tuple::Point(1, 0, 0)) == Color::Black()); + } + AND_THEN("stripe_at(pattern, point(-0.1, 0, 0) = black") + { + REQUIRE(pattern.stripe_at(Tuple::Point(-0.1, 0, 0)) == Color::Black()); + } + AND_THEN("stripe_at(pattern, point(-1, 0, 0) = black") + { + REQUIRE(pattern.stripe_at(Tuple::Point(-1, 0, 0)) == Color::Black()); + } + AND_THEN("stripe_at(pattern, point(-1.1, 0, 0) = white") + { + REQUIRE(pattern.stripe_at(Tuple::Point(-1.1, 0, 0)) == Color::White()); + } + } +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fffcdb6..f16ad24 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.19) project(raytracing_test) @@ -20,6 +20,7 @@ add_executable(raytracing_test 07_making_scene.cpp 08_shadows.cpp 09_planes.cpp + 10_patterns.cpp ) include_directories("${CMAKE_SOURCE_DIR}/tests")