diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 293dcce..51d1ed4 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -21,6 +21,7 @@ add_library(raytracing src/ray.cpp src/sphere.cpp src/tuple.cpp + src/world.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/raytracing/include/raytracing.h b/raytracing/include/raytracing.h index 1621d16..e590567 100644 --- a/raytracing/include/raytracing.h +++ b/raytracing/include/raytracing.h @@ -36,3 +36,4 @@ #include "ray.h" #include "sphere.h" #include "tuple.h" +#include "world.h" diff --git a/raytracing/src/intersections.cpp b/raytracing/src/intersections.cpp index 80f96e5..d07eb3b 100644 --- a/raytracing/src/intersections.cpp +++ b/raytracing/src/intersections.cpp @@ -28,6 +28,8 @@ /* ------------------------------------------------------------------------- */ +#include + #include "intersections.h" using namespace Raytracer; @@ -73,6 +75,26 @@ const Intersections &Intersections::operator=(const Intersections &an_other) /* ------------------------------------------------------------------------- */ +const Intersections &Intersections::operator+=(const Intersections &an_other) +{ + if (this == &an_other) + { + return *this; + } + + m_data.reserve(m_data.size() + an_other.m_data.size()); + for (auto the_intersection : an_other.m_data) + { + m_data.push_back(the_intersection); + } + + std::sort(m_data.begin(), m_data.end()); + + return *this; +} + +/* ------------------------------------------------------------------------- */ + bool Intersections::add(const Intersection &an_intersection) { m_data.push_back(an_intersection); diff --git a/raytracing/src/intersections.h b/raytracing/src/intersections.h index 9edb188..d281189 100644 --- a/raytracing/src/intersections.h +++ b/raytracing/src/intersections.h @@ -48,6 +48,7 @@ namespace Raytracer Intersection &operator[](uint8_t an_index); const Intersections &operator=(const Intersections &an_other); + const Intersections &operator+=(const Intersections &an_other); bool add(const Intersection &an_intersection); diff --git a/raytracing/src/material.cpp b/raytracing/src/material.cpp index 928b626..96ff0ff 100644 --- a/raytracing/src/material.cpp +++ b/raytracing/src/material.cpp @@ -162,7 +162,7 @@ Color Material::lighting(const PointLight &a_light, const Tuple &a_point, const else { // Compute the specular contribution - double the_factor = pow(the_reflect_dot_eye, m_shininess); + double the_factor = std::pow(the_reflect_dot_eye, m_shininess); the_specular = a_light.intensity() * m_specular * the_factor; } } diff --git a/raytracing/src/point-light.cpp b/raytracing/src/point-light.cpp index cab3c1b..ebdae27 100644 --- a/raytracing/src/point-light.cpp +++ b/raytracing/src/point-light.cpp @@ -44,6 +44,34 @@ PointLight::PointLight(const Tuple &a_position, const Color &an_intensity) : /* ------------------------------------------------------------------------- */ +PointLight::PointLight(const PointLight &a_light) : m_position(a_light.m_position), m_intensity(a_light.m_intensity) +{ +} + +/* ------------------------------------------------------------------------- */ + +const PointLight &PointLight::operator=(const PointLight &a_light) +{ + if (this == &a_light) + { + return *this; + } + + m_position = a_light.m_position; + m_intensity = a_light.m_intensity; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +bool PointLight::operator==(const PointLight &a_light) const +{ + return (m_position == a_light.m_position) && (m_intensity == a_light.m_intensity); +} + +/* ------------------------------------------------------------------------- */ + const Tuple &PointLight::position(void) const { return m_position; diff --git a/raytracing/src/point-light.h b/raytracing/src/point-light.h index 4562580..a0b68a4 100644 --- a/raytracing/src/point-light.h +++ b/raytracing/src/point-light.h @@ -38,7 +38,12 @@ namespace Raytracer class PointLight { public: + PointLight(void) = default; PointLight(const Tuple &a_position, const Color &an_intensity); + PointLight(const PointLight &a_light); + + const PointLight &operator=(const PointLight &a_light); + bool operator==(const PointLight &a_light) const; const Tuple &position(void) const; const Color &intensity(void) const; diff --git a/raytracing/src/ray.cpp b/raytracing/src/ray.cpp index f2e8872..96c3671 100644 --- a/raytracing/src/ray.cpp +++ b/raytracing/src/ray.cpp @@ -69,7 +69,7 @@ Tuple Ray::position(double a_distance) /* ------------------------------------------------------------------------- */ -Ray Ray::transform(const Matrix &a_matrix) +Ray Ray::transform(const Matrix &a_matrix) const { Ray the_output_ray; diff --git a/raytracing/src/ray.h b/raytracing/src/ray.h index d3ee1b6..fef8f5c 100644 --- a/raytracing/src/ray.h +++ b/raytracing/src/ray.h @@ -47,7 +47,7 @@ namespace Raytracer Tuple position(double a_distance); - Ray transform(const Matrix &a_matrix); + Ray transform(const Matrix &a_matrix) const; private: Tuple m_origin; diff --git a/raytracing/src/shape.cpp b/raytracing/src/shape.cpp index 756bc0e..fc573f8 100644 --- a/raytracing/src/shape.cpp +++ b/raytracing/src/shape.cpp @@ -48,13 +48,13 @@ Shape::Shape(void) : m_id(kNothing), m_transform(Matrix::identity()) /* ------------------------------------------------------------------------- */ -Shape::Shape(Shape &a_copy) : m_id(a_copy.m_id), m_transform(Matrix::identity()), m_material(a_copy.m_material) +Shape::Shape(Shape &a_copy) : m_id(a_copy.m_id), m_transform(a_copy.m_transform), m_material(a_copy.m_material) { } /* ------------------------------------------------------------------------- */ -Shape::Shape(const Shape &a_copy) : m_id(a_copy.m_id), m_transform(Matrix::identity()), m_material(a_copy.m_material) +Shape::Shape(const Shape &a_copy) : m_id(a_copy.m_id), m_transform(a_copy.m_transform), m_material(a_copy.m_material) { } @@ -78,7 +78,8 @@ const Shape &Shape::operator=(const Shape &a_shape) bool Shape::operator==(const Shape &a_shape) const { - return (m_id == a_shape.m_id) && (m_transform == a_shape.m_transform) && (m_material == a_shape.m_material); + // (m_id == a_shape.m_id) && ( + return (m_transform == a_shape.m_transform) && (m_material == a_shape.m_material); } /* ------------------------------------------------------------------------- */ @@ -90,6 +91,13 @@ Matrix &Shape::transform(void) /* ------------------------------------------------------------------------- */ +const Matrix &Shape::transform(void) const +{ + return m_transform; +} + +/* ------------------------------------------------------------------------- */ + void Shape::set_transform(const Matrix &a_transform_matrix) { m_transform = a_transform_matrix; @@ -129,7 +137,7 @@ Tuple Shape::normal_at(const Tuple &a_world_point) const /* ------------------------------------------------------------------------- */ -Intersections Shape::intersect(Ray &a_ray) +Intersections Shape::intersect(const Ray &a_ray) const { Intersections the_ret; diff --git a/raytracing/src/shape.h b/raytracing/src/shape.h index c8da14d..87cd1b9 100644 --- a/raytracing/src/shape.h +++ b/raytracing/src/shape.h @@ -53,6 +53,7 @@ namespace Raytracer bool operator==(const Shape &a_shape) const; Matrix &transform(void); + const Matrix &transform(void) const; void set_transform(const Matrix &a_transform_matrix); Material &material(void); @@ -61,7 +62,7 @@ namespace Raytracer Tuple normal_at(const Tuple &a_point) const; - virtual Intersections intersect(Ray &a_ray); + virtual Intersections intersect(const Ray &a_ray) const; protected: void inc_id(void); diff --git a/raytracing/src/sphere.cpp b/raytracing/src/sphere.cpp index 07f4890..a4c1a6d 100644 --- a/raytracing/src/sphere.cpp +++ b/raytracing/src/sphere.cpp @@ -46,7 +46,7 @@ Sphere::Sphere(void) /* ------------------------------------------------------------------------- */ -Intersections Sphere::intersect(Ray &a_ray) +Intersections Sphere::intersect(const Ray &a_ray) const { Intersections the_intersections; Ray the_ray = a_ray.transform(transform().inverse()); diff --git a/raytracing/src/sphere.h b/raytracing/src/sphere.h index 0cae882..ec532a6 100644 --- a/raytracing/src/sphere.h +++ b/raytracing/src/sphere.h @@ -38,9 +38,7 @@ namespace Raytracer { public: Sphere(void); - Intersections intersect(Ray &a_ray) override; - - private: + Intersections intersect(const Ray &a_ray) const override; }; }; // namespace Raytracer diff --git a/raytracing/src/world.cpp b/raytracing/src/world.cpp new file mode 100644 index 0000000..d87f1de --- /dev/null +++ b/raytracing/src/world.cpp @@ -0,0 +1,170 @@ +/*! + * world.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/01/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 "matrix.h" +#include "sphere.h" + +#include "world.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +World::World(void) : m_has_light(false) +{ +} + +/* ------------------------------------------------------------------------- */ + +World::World(World &an_other) +{ + m_objects.reserve(an_other.m_objects.size()); + for (auto &the_object : an_other.m_objects) + { + m_objects.push_back(the_object); + } + + m_has_light = an_other.m_has_light; + if (m_has_light == true) + { + m_light = an_other.m_light; + } +} + +/* ------------------------------------------------------------------------- */ + +const World &World::operator=(const World &an_other) +{ + if (this == &an_other) + { + return *this; + } + + m_objects.reserve(an_other.m_objects.size()); + m_objects.assign(an_other.m_objects.begin(), an_other.m_objects.end()); + + m_has_light = an_other.m_has_light; + if (m_has_light == true) + { + m_light = an_other.m_light; + } + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +uint32_t World::objects_count(void) +{ + return m_objects.size(); +} + +/* ------------------------------------------------------------------------- */ + +bool World::has_light(void) +{ + return m_has_light; +} + +/* ------------------------------------------------------------------------- */ + +const PointLight &World::light(void) const +{ + return m_light; +} + +/* ------------------------------------------------------------------------- */ + +void World::set_light(const PointLight &a_light) +{ + m_light = a_light; + m_has_light = true; +} + +/* ------------------------------------------------------------------------- */ + +void World::add_object(Shape *a_shape) +{ + m_objects.push_back(a_shape); +} + +/* ------------------------------------------------------------------------- */ + +bool World::contains(const Shape &a_shape) +{ + + for (const auto &the_shape : m_objects) + { + if (*the_shape == a_shape) + { + return true; + } + } + + return false; +} + +/* ------------------------------------------------------------------------- */ + +Intersections World::intersect_world(const Ray &a_ray) +{ + Intersections the_all_intersec; + + for (auto the_object : m_objects) + { + Intersections the_intersection = the_object->intersect(a_ray); + the_all_intersec += the_intersection; + } + + return the_all_intersec; +} + +/* ------------------------------------------------------------------------- */ + +World World::default_world(void) +{ + World the_world; + Sphere *s1, *s2; + + the_world.set_light(PointLight(Tuple::Point(-10, 10, 10), Color::White())); + + s1 = new Sphere; + Material &the_s1_mat = s1->material(); + the_s1_mat.set_color(Color(0.8, 1.0, 0.6)); + the_s1_mat.set_diffuse(0.7); + the_s1_mat.set_specular(0.2); + the_world.add_object(s1); + + s2 = new Sphere; + s2->set_transform(Matrix::scaling(0.5, 0.5, 0.5)); + the_world.add_object(s2); + + return the_world; +} diff --git a/raytracing/src/world.h b/raytracing/src/world.h new file mode 100644 index 0000000..91c83b7 --- /dev/null +++ b/raytracing/src/world.h @@ -0,0 +1,72 @@ +/*! + * world.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: 20/02/2024 + * + */ + +#ifndef _RAYTRACER_WORLD_H +#define _RAYTRACER_WORLD_H + +/* ------------------------------------------------------------------------- */ + +#include + +#include + +#include "intersections.h" +#include "point-light.h" +#include "ray.h" +#include "shape.h" + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class World + { + public: + World(void); + World(World &an_other); + + const World &operator=(const World &an_other); + + uint32_t objects_count(void); + bool has_light(void); + + const PointLight &light(void) const; + void set_light(const PointLight &a_light); + + void add_object(Shape *a_shape); + bool contains(const Shape &a_shape); + + Intersections intersect_world(const Ray &a_ray); + + static World default_world(void); + + private: + bool m_has_light; + PointLight m_light; + std::vector m_objects; + }; +}; // namespace Raytracer + +#endif /* _RAYTRACER_WORLD_H */ diff --git a/tests/05_rays.cpp b/tests/05_rays.cpp index ed0a0b9..c2be4fd 100644 --- a/tests/05_rays.cpp +++ b/tests/05_rays.cpp @@ -227,32 +227,6 @@ SCENARIO("A sphere is behind a ray", "[features/spheres.feature]") /* ------------------------------------------------------------------------- */ -SCENARIO("Test Sphere Object", "[features/spheres.feature]") -{ - GIVEN("s1 <- sphere()") - { - Sphere s1; - AND_GIVEN("s2 <- s1") - { - Sphere s2 = s1; - AND_GIVEN("s3 <- sphere()") - { - Sphere s3; - THEN("s1 = s2") - { - REQUIRE(s1 == s2); - } - AND_THEN("s1 != s3") - { - REQUIRE(s1 != s3); - } - } - } - } -} - -/* ------------------------------------------------------------------------- */ - SCENARIO("An intersection encapsulates t and object", "[features/intersections.feature]") { GIVEN("s <- sphere()") diff --git a/tests/07_making_scene.cpp b/tests/07_making_scene.cpp new file mode 100644 index 0000000..6770800 --- /dev/null +++ b/tests/07_making_scene.cpp @@ -0,0 +1,132 @@ +/*! + * 07_making_scene.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: 14/02/2024 + * + */ + +/*---------------------------------------------------------------------------*/ + +#include + +#include "raytracing.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +SCENARIO("Creating a world", "[features/world.feature]") +{ + GIVEN("w <- world()") + { + World w; + THEN("w contains no objects") + { + REQUIRE(w.objects_count() == 0); + } + AND_THEN("w has no light source") + { + REQUIRE(w.has_light() == false); + } + } +} + +/* ------------------------------------------------------------------------- */ + +SCENARIO("The default world", "[features/world.feature]") +{ + GIVEN("light <- point_light(point(-10, 10, 10), color(1, 1, 1))") + { + PointLight light = PointLight(Tuple::Point(-10, 10, 10), Color(1, 1, 1)); + AND_GIVEN("s1 <- sphere") + // with: + // | material.color | (0.8, 1.0, 0.6) | + // | material.diffuse | 0.7 | + // | material.specular | 0.2 | + { + Sphere s1; + s1.material().set_color(Color(0.8, 1.0, 0.6)); + s1.material().set_diffuse(0.7); + s1.material().set_specular(0.2); + AND_GIVEN("s2 <- sphere") + // with: + // | transform | scaling(0.5, 0.5, 0.5) | + { + Sphere s2; + s2.set_transform(Matrix::scaling(0.5, 0.5, 0.5)); + WHEN("w <- default_world") + { + World w = World::default_world(); + THEN("w.light = light") + { + REQUIRE(w.light() == light); + } + AND_THEN("w contains s1") + { + REQUIRE(w.contains(s1) == true); + } + AND_THEN("w contains s2") + { + REQUIRE(w.contains(s2) == true); + } + } + } + } + } +} + +/* ------------------------------------------------------------------------- */ + +SCENARIO("Intersect a world with a ray", "[features/world.feature]") +{ + GIVEN("w <- default_world()") + { + World w = World::default_world(); + AND_GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))") + { + Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1)); + WHEN("xs <- intersect_world(w,r)") + { + Intersections xs = w.intersect_world(r); + THEN("xs.count = 4") + { + REQUIRE(xs.count() == 4); + } + AND_THEN("xs[0].t = 4") + { + REQUIRE(xs[0].distance_t() == 4); + } + AND_THEN("xs[1].t = 4.5") + { + REQUIRE(xs[1].distance_t() == 4.5); + } + AND_THEN("xs[2].t = 5.5") + { + REQUIRE(xs[2].distance_t() == 5.5); + } + AND_THEN("xs[3].t = 6") + { + REQUIRE(xs[3].distance_t() == 6); + } + } + } + } +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7148328..77927a7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(raytracing_test 04_transformations.cpp 05_rays.cpp 06_light_shading.cpp + 07_making_scene.cpp ) include_directories("${CMAKE_SOURCE_DIR}/tests")