[FEAT] Add abstraction for stripe_patern

This commit is contained in:
NADAL Jean-Baptiste
2024-03-01 12:27:01 +01:00
parent 22a3e46a36
commit 49d06737a5
9 changed files with 309 additions and 74 deletions

View File

@@ -21,11 +21,13 @@ add_library(raytracing
src/lights/point-light.cpp src/lights/point-light.cpp
src/patterns/pattern.cpp
src/patterns/stripe-pattern.cpp
src/renderer/camera.cpp src/renderer/camera.cpp
src/renderer/canvas.cpp src/renderer/canvas.cpp
src/renderer/material.cpp src/renderer/material.cpp
src/renderer/ray.cpp src/renderer/ray.cpp
src/renderer/stripe-pattern.cpp
src/renderer/world.cpp src/renderer/world.cpp
src/shapes/plane.cpp src/shapes/plane.cpp

View File

@@ -35,11 +35,12 @@
#include "lights/point-light.h" #include "lights/point-light.h"
#include "patterns/stripe-pattern.h"
#include "renderer/camera.h" #include "renderer/camera.h"
#include "renderer/canvas.h" #include "renderer/canvas.h"
#include "renderer/material.h" #include "renderer/material.h"
#include "renderer/ray.h" #include "renderer/ray.h"
#include "renderer/stripe-pattern.h"
#include "renderer/world.h" #include "renderer/world.h"
#include "shapes/plane.h" #include "shapes/plane.h"

View File

@@ -0,0 +1,68 @@
/*!
* 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: 29/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 <cmath>
#include "core/common.h"
#include "pattern.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
Pattern::Pattern(void)
{
m_transform = Matrix::identity();
}
/* ------------------------------------------------------------------------- */
const Matrix &Pattern::transform(void) const
{
return m_transform;
}
/* ------------------------------------------------------------------------- */
void Pattern::set_transform(const Matrix &a_transform_matrix)
{
m_transform = a_transform_matrix;
}
/* ------------------------------------------------------------------------- */
const Color Pattern::pattern_at_shape(Shape *a_shape, const Tuple &a_world_point) const
{
Tuple the_objet_point = a_shape->transform().inverse() * a_world_point;
Tuple the_pattern_point = m_transform.inverse() * the_objet_point;
return pattern_at(the_pattern_point);
}

View File

@@ -0,0 +1,58 @@
/*!
* 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: 29/02/2024
*
*/
#ifndef _RAYTRACER_PATTERN_H
#define _RAYTRACER_PATTERN_H
/* ------------------------------------------------------------------------- */
#include "core/color.h"
#include "core/matrix.h"
#include "core/tuple.h"
#include "shapes/shape.h"
/* ------------------------------------------------------------------------- */
namespace Raytracer
{
class Pattern
{
public:
Pattern(void);
const Matrix &transform(void) const;
void set_transform(const Matrix &a_transform_matrix);
const Color pattern_at_shape(Shape *a_shape, const Tuple &a_world_point) const;
virtual const Color pattern_at(const Tuple &a_point) const = 0;
private:
Matrix m_transform;
};
}; // namespace Raytracer
#endif /* _RAYTRACER_PATTERN_H */

View File

@@ -38,9 +38,8 @@ 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) StripePattern::StripePattern(const Color &a_color_a, const Color &a_color_b) : Pattern(), m_a(a_color_a), m_b(a_color_b)
{ {
m_transform = Matrix::identity();
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@@ -66,27 +65,10 @@ const Color &StripePattern::b(void) const
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
const Color &StripePattern::stripe_at(const Tuple &a_point) const const Color StripePattern::pattern_at(const Tuple &a_point) const
{ {
if (((int)std::floor(a_point.x()) % 2) == 0) if (((int)std::floor(a_point.x()) % 2) == 0)
return m_a; return m_a;
return m_b; return m_b;
} }
/* ------------------------------------------------------------------------- */
void StripePattern::set_pattern_transform(const Matrix &a_transform_matrix)
{
m_transform = a_transform_matrix;
}
/* ------------------------------------------------------------------------- */
const Color &StripePattern::stripe_at_object(Shape *an_object, const Tuple &a_world_point) const
{
Tuple the_objet_point = an_object->transform().inverse() * a_world_point;
Tuple the_pattern_point = m_transform.inverse() * the_objet_point;
return stripe_at(the_pattern_point);
}

View File

@@ -32,13 +32,15 @@
#include "core/matrix.h" #include "core/matrix.h"
#include "core/tuple.h" #include "core/tuple.h"
#include "patterns/pattern.h"
#include "shapes/shape.h" #include "shapes/shape.h"
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
namespace Raytracer namespace Raytracer
{ {
class StripePattern class StripePattern : public Pattern
{ {
public: public:
StripePattern(const Color &a_color_a, const Color &a_color_b); StripePattern(const Color &a_color_a, const Color &a_color_b);
@@ -48,14 +50,9 @@ namespace Raytracer
const Color &a(void) const; const Color &a(void) const;
const Color &b(void) const; const Color &b(void) const;
const Color &stripe_at(const Tuple &a_point) const; const Color pattern_at(const Tuple &a_point) const override;
void set_pattern_transform(const Matrix &a_transform_matrix);
const Color &stripe_at_object(Shape *an_object, const Tuple &a_world_point) const;
private: private:
Matrix m_transform;
Color m_a; Color m_a;
Color m_b; Color m_b;
}; };

View File

@@ -33,7 +33,7 @@
#include "core/common.h" #include "core/common.h"
#include "shapes/shape.h" #include "shapes/shape.h"
#include "renderer/stripe-pattern.h" #include "patterns/pattern.h"
#include "material.h" #include "material.h"
@@ -133,14 +133,14 @@ void Material::set_shininess(double a_value)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
const StripePattern *Material::pattern(void) const const Pattern *Material::pattern(void) const
{ {
return m_pattern; return m_pattern;
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
void Material::set_pattern(StripePattern *a_pattern) void Material::set_pattern(Pattern *a_pattern)
{ {
m_pattern = a_pattern; m_pattern = a_pattern;
} }
@@ -156,7 +156,7 @@ Color Material::lighting(Shape *an_object, const PointLight &a_light, const Tupl
if (m_pattern != nullptr) if (m_pattern != nullptr)
{ {
the_color = m_pattern->stripe_at_object(an_object, a_point); the_color = m_pattern->pattern_at_shape(an_object, a_point);
} }
else else
{ {

View File

@@ -38,7 +38,7 @@
namespace Raytracer namespace Raytracer
{ {
class Shape; class Shape;
class StripePattern; class Pattern;
class Material class Material
{ {
@@ -62,8 +62,8 @@ namespace Raytracer
const double &shininess(void) const; const double &shininess(void) const;
void set_shininess(double a_value); void set_shininess(double a_value);
const StripePattern *pattern(void) const; const Pattern *pattern(void) const;
void set_pattern(StripePattern *a_pattern); void set_pattern(Pattern *a_pattern);
Color lighting(Shape *an_object, const PointLight &a_light, const Tuple &a_point, const Tuple &an_eyev, Color lighting(Shape *an_object, const PointLight &a_light, const Tuple &a_point, const Tuple &an_eyev,
const Tuple &a_normalv, bool is_shadowed) const; const Tuple &a_normalv, bool is_shadowed) const;
@@ -74,7 +74,7 @@ namespace Raytracer
double m_diffuse; double m_diffuse;
double m_specular; double m_specular;
double m_shininess; double m_shininess;
StripePattern *m_pattern; Pattern *m_pattern;
}; };
}; // namespace Raytracer }; // namespace Raytracer

View File

@@ -33,9 +33,22 @@ using namespace Raytracer;
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
class TestPattern : public Pattern
{
public:
TestPattern(void) = default;
const Color pattern_at(const Tuple &a_point) const override
{
return Color(a_point.x(), a_point.y(), a_point.z());
}
};
/* ------------------------------------------------------------------------- */
SCENARIO("Creating a stripe pattern", "[features/patterns.feature]") SCENARIO("Creating a stripe pattern", "[features/patterns.feature]")
{ {
GIVEN("pattern <- strip_pattern(white, black)") GIVEN("pattern <- stripe_pattern(white, black)")
{ {
StripePattern pattern(Color::White(), Color::Black()); StripePattern pattern(Color::White(), Color::Black());
THEN("pattern.a = white") THEN("pattern.a = white")
@@ -53,20 +66,20 @@ SCENARIO("Creating a stripe pattern", "[features/patterns.feature]")
SCENARIO("A stripe pattern is constant in y", "[features/patterns.feature]") SCENARIO("A stripe pattern is constant in y", "[features/patterns.feature]")
{ {
GIVEN("pattern <- strip_pattern(white, black)") GIVEN("pattern <- stripe_pattern(white, black)")
{ {
StripePattern pattern(Color::White(), Color::Black()); StripePattern pattern(Color::White(), Color::Black());
THEN("stripe_at(pattern, point(0, 0, 0) = white") THEN("pattern_at(pattern, point(0, 0, 0) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White());
} }
AND_THEN("stripe_at(pattern, point(0, 1, 0) = white") AND_THEN("pattern_at(pattern, point(0, 1, 0) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0, 1, 0)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0, 1, 0)) == Color::White());
} }
AND_THEN("stripe_at(pattern, point(0, 2, 0) = white") AND_THEN("pattern_at(pattern, point(0, 2, 0) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0, 2, 0)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0, 2, 0)) == Color::White());
} }
} }
} }
@@ -75,20 +88,20 @@ SCENARIO("A stripe pattern is constant in y", "[features/patterns.feature]")
SCENARIO("A stripe pattern is constant in z", "[features/patterns.feature]") SCENARIO("A stripe pattern is constant in z", "[features/patterns.feature]")
{ {
GIVEN("pattern <- strip_pattern(white, black)") GIVEN("pattern <- stripe_pattern(white, black)")
{ {
StripePattern pattern(Color::White(), Color::Black()); StripePattern pattern(Color::White(), Color::Black());
THEN("stripe_at(pattern, point(0, 0, 0) = white") THEN("pattern_at(pattern, point(0, 0, 0) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White());
} }
AND_THEN("stripe_at(pattern, point(0, 0, 1) = white") AND_THEN("pattern_at(pattern, point(0, 0, 1) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 1)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 1)) == Color::White());
} }
AND_THEN("stripe_at(pattern, point(0, 0, 2) = white") AND_THEN("pattern_at(pattern, point(0, 0, 2) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 2)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 2)) == Color::White());
} }
} }
} }
@@ -97,32 +110,32 @@ SCENARIO("A stripe pattern is constant in z", "[features/patterns.feature]")
SCENARIO("A stripe pattern alternates in x", "[features/patterns.feature]") SCENARIO("A stripe pattern alternates in x", "[features/patterns.feature]")
{ {
GIVEN("pattern <- strip_pattern(white, black)") GIVEN("pattern <- stripe_pattern(white, black)")
{ {
StripePattern pattern(Color::White(), Color::Black()); StripePattern pattern(Color::White(), Color::Black());
THEN("stripe_at(pattern, point(0, 0, 0) = white") THEN("pattern_at(pattern, point(0, 0, 0) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0, 0, 0)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0, 0, 0)) == Color::White());
} }
AND_THEN("stripe_at(pattern, point(0.9, 0, 0) = white") AND_THEN("pattern_at(pattern, point(0.9, 0, 0) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(0.9, 0, 0)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(0.9, 0, 0)) == Color::White());
} }
AND_THEN("stripe_at(pattern, point(1, 0, 0) = black") AND_THEN("pattern_at(pattern, point(1, 0, 0) = black")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(1, 0, 0)) == Color::Black()); REQUIRE(pattern.pattern_at(Tuple::Point(1, 0, 0)) == Color::Black());
} }
AND_THEN("stripe_at(pattern, point(-0.1, 0, 0) = black") AND_THEN("pattern_at(pattern, point(-0.1, 0, 0) = black")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(-0.1, 0, 0)) == Color::Black()); REQUIRE(pattern.pattern_at(Tuple::Point(-0.1, 0, 0)) == Color::Black());
} }
AND_THEN("stripe_at(pattern, point(-1, 0, 0) = black") AND_THEN("pattern_at(pattern, point(-1, 0, 0) = black")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(-1, 0, 0)) == Color::Black()); REQUIRE(pattern.pattern_at(Tuple::Point(-1, 0, 0)) == Color::Black());
} }
AND_THEN("stripe_at(pattern, point(-1.1, 0, 0) = white") AND_THEN("pattern_at(pattern, point(-1.1, 0, 0) = white")
{ {
REQUIRE(pattern.stripe_at(Tuple::Point(-1.1, 0, 0)) == Color::White()); REQUIRE(pattern.pattern_at(Tuple::Point(-1.1, 0, 0)) == Color::White());
} }
} }
} }
@@ -135,7 +148,7 @@ SCENARIO("Lightning with a pattern applied", "[features/materials.feature]")
{ {
Sphere object; Sphere object;
Material m; Material m;
GIVEN("m.pattern <- strip_pattern(color(1, 1, 1), color(0, 0, 0))") GIVEN("m.pattern <- stripe_pattern(color(1, 1, 1), color(0, 0, 0))")
{ {
m.set_pattern(new StripePattern(Color(1, 1, 1), Color(0, 0, 0))); m.set_pattern(new StripePattern(Color(1, 1, 1), Color(0, 0, 0)));
AND_GIVEN("m.ambient <- 1") AND_GIVEN("m.ambient <- 1")
@@ -198,9 +211,9 @@ SCENARIO("Stripes with an object transformation", "[features/patterns.feature]")
AND_GIVEN("pattern <- stripe_pattern(white, black)") AND_GIVEN("pattern <- stripe_pattern(white, black)")
{ {
StripePattern pattern(Color::White(), Color::Black()); StripePattern pattern(Color::White(), Color::Black());
WHEN("c <- stripe_at_object(pattern, object, point(1.5, 0, 0))") WHEN("c <- pattern_at_shape(pattern, object, point(1.5, 0, 0))")
{ {
Color c = pattern.stripe_at_object(&object, Tuple::Point(1.5, 0, 0)); Color c = pattern.pattern_at_shape(&object, Tuple::Point(1.5, 0, 0));
THEN("c = white") THEN("c = white")
{ {
REQUIRE(c == Color::White()); REQUIRE(c == Color::White());
@@ -223,10 +236,10 @@ SCENARIO("Stripes with an pattern transformation", "[features/patterns.feature]"
StripePattern pattern(Color::White(), Color::Black()); StripePattern pattern(Color::White(), Color::Black());
AND_GIVEN("set_transform(pattern, scaling(2, 2, 2))") AND_GIVEN("set_transform(pattern, scaling(2, 2, 2))")
{ {
pattern.set_pattern_transform(Matrix::scaling(2, 2, 2)); pattern.set_transform(Matrix::scaling(2, 2, 2));
WHEN("c <- stripe_at_object(pattern, object, point(1.5, 0, 0))") WHEN("c <- pattern_at_shape(pattern, object, point(1.5, 0, 0))")
{ {
Color c = pattern.stripe_at_object(&object, Tuple::Point(1.5, 0, 0)); Color c = pattern.pattern_at_shape(&object, Tuple::Point(1.5, 0, 0));
THEN("c = white") THEN("c = white")
{ {
REQUIRE(c == Color::White()); REQUIRE(c == Color::White());
@@ -252,10 +265,10 @@ SCENARIO("Stripes with both an object and pattern transformation", "[features/pa
StripePattern pattern(Color::White(), Color::Black()); StripePattern pattern(Color::White(), Color::Black());
AND_GIVEN("set_pattern_transform(pattern, translation(0.5, 0, 0)))") AND_GIVEN("set_pattern_transform(pattern, translation(0.5, 0, 0)))")
{ {
pattern.set_pattern_transform(Matrix::translation(0.5, 0, 0)); pattern.set_transform(Matrix::translation(0.5, 0, 0));
WHEN("c <- stripe_at_object(pattern, object, point(2.5, 0, 0))") WHEN("c <- pattern_at_object(pattern, object, point(2.5, 0, 0))")
{ {
Color c = pattern.stripe_at_object(&object, Tuple::Point(1.5, 0, 0)); Color c = pattern.pattern_at_shape(&object, Tuple::Point(1.5, 0, 0));
THEN("c = white") THEN("c = white")
{ {
REQUIRE(c == Color::White()); REQUIRE(c == Color::White());
@@ -266,3 +279,117 @@ SCENARIO("Stripes with both an object and pattern transformation", "[features/pa
} }
} }
} }
/* ------------------------------------------------------------------------- */
SCENARIO("The default pattern transformation", "[features/patterns.feature]")
{
GIVEN("pattern <- test_pattern()")
{
TestPattern pattern;
THEN("pattern.transform = identity_matrix")
{
REQUIRE(pattern.transform() == Matrix::identity());
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Assigning a transformation to the pattern", "[features/patterns.feature]")
{
GIVEN("pattern <- test_pattern()")
{
TestPattern pattern;
WHEN("set_pattern_transform(pattern, translation(1,2,3))")
{
pattern.set_transform(Matrix::translation(1, 2, 3));
THEN("pattern.transform = translation(1, 2, 3)")
{
REQUIRE(pattern.transform() == Matrix::translation(1, 2, 3));
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A pattern with an object transformation", "[features/patterns.feature]")
{
GIVEN("shape <- sphere()")
{
Sphere shape;
AND_GIVEN("set_transform(shape, scaling(2, 2, 2))")
{
shape.set_transform(Matrix::scaling(2, 2, 2));
AND_GIVEN("pattern <- test_pattern()")
{
TestPattern pattern;
WHEN("Color c <- pattern_at_shape(pattern, shape, point(2, 3, 4))")
{
Color c = pattern.pattern_at_shape(&shape, Tuple::Point(2, 3, 4));
THEN("c = color(1, 1.5, 2)")
{
REQUIRE(c == Color(1, 1.5, 2));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A pattern with a pattern transformation", "[features/patterns.feature]")
{
GIVEN("shape <- sphere()")
{
Sphere shape;
AND_GIVEN("pattern <- test_pattern()")
{
TestPattern pattern;
AND_GIVEN("set_pattern_transform(pattern, scaling(2, 2, 2))")
{
pattern.set_transform(Matrix::scaling(2, 2, 2));
WHEN("c <- pattern_at_shape(pattern, shape, point(2, 3, 4))")
{
Color c = pattern.pattern_at_shape(&shape, Tuple::Point(2, 3, 4));
THEN("c = color(1, 1.5, 2)")
{
REQUIRE(c == Color(1, 1.5, 2));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A pattern with both an object and a pattern transformation", "[features/patterns.feature]")
{
GIVEN("shape <- sphere()")
{
Sphere shape;
AND_GIVEN("set_transform(shape, scaling(2, 2, 2))")
{
shape.set_transform(Matrix::scaling(2, 2, 2));
AND_GIVEN("pattern <- test_pattern()")
{
TestPattern pattern;
AND_GIVEN("set_pattern_transform(pattern, translation(0.5, 1, 1.5))")
{
pattern.set_transform(Matrix::translation(0.5, 1, 1.5));
WHEN("c <- pattern_at_shape(pattern, shape, point(2.5, 3, 3.5))")
{
Color c = pattern.pattern_at_shape(&shape, Tuple::Point(2.5, 3, 3.5));
THEN("c = color(0.75, 0.5, 0.25)")
{
REQUIRE(c == Color(0.75, 0.5, 0.25));
}
}
}
}
}
}
}