From 548d765fcbec924e56c1c90abd52d178e8a69197 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 6 Mar 2024 16:59:43 +0100 Subject: [PATCH] [FEAT] Material add transparency and refractive_index into material --- apps/CMakeLists.txt | 12 ++++--- raytracing/CMakeLists.txt | 1 + raytracing/src/core/color.cpp | 4 ++- raytracing/src/core/intersection-data.cpp | 4 ++- raytracing/src/core/intersection.cpp | 4 ++- raytracing/src/core/matrix.cpp | 10 ++++-- raytracing/src/core/tuple.cpp | 20 +++++++++--- raytracing/src/lights/point-light.cpp | 3 +- raytracing/src/renderer/camera.cpp | 12 ++++--- raytracing/src/renderer/canvas.cpp | 6 ++-- raytracing/src/renderer/material.cpp | 33 ++++++++++++++++++- raytracing/src/renderer/material.h | 11 ++++++- raytracing/src/renderer/ray.cpp | 3 +- raytracing/src/shapes/shape.cpp | 6 ++-- raytracing/src/shapes/sphere.cpp | 12 +++++++ raytracing/src/shapes/sphere.h | 2 ++ tests/11_reflection_refraction.cpp | 40 +++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- 18 files changed, 157 insertions(+), 28 deletions(-) diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 41268b3..ad46a47 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -5,20 +5,22 @@ project(main) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) + + add_executable(chapter_05 chapter_05.cpp) -target_link_libraries(chapter_05 PRIVATE raytracing gcov) +target_link_libraries(chapter_05 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) add_executable(chapter_06 chapter_06.cpp) -target_link_libraries(chapter_06 PRIVATE raytracing gcov) +target_link_libraries(chapter_06 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) add_executable(chapter_07 chapter_07.cpp) -target_link_libraries(chapter_07 PRIVATE raytracing gcov) +target_link_libraries(chapter_07 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) add_executable(chapter_09 chapter_09.cpp) target_link_libraries(chapter_09 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) add_executable(chapter_10 chapter_10.cpp) -target_link_libraries(chapter_10 PRIVATE raytracing gcov) +target_link_libraries(chapter_10 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) add_executable(chapter_11 chapter_11.cpp) -target_link_libraries(chapter_11 PRIVATE raytracing gcov) +target_link_libraries(chapter_11 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index a4f2e44..b332817 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_definitions(--coverage) +add_definitions(-fopenmp) include_directories (src) diff --git a/raytracing/src/core/color.cpp b/raytracing/src/core/color.cpp index a9d6b5a..7e0908b 100644 --- a/raytracing/src/core/color.cpp +++ b/raytracing/src/core/color.cpp @@ -39,7 +39,9 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ Color::Color(void) : - m_red(0), m_green(0), m_blue(0) + m_red(0), + m_green(0), + m_blue(0) { // printf("%s red: %f green: %f, blue: %f\n", __PRETTY_FUNCTION__, m_red, m_green, m_blue); } diff --git a/raytracing/src/core/intersection-data.cpp b/raytracing/src/core/intersection-data.cpp index c72864f..dcc4be3 100644 --- a/raytracing/src/core/intersection-data.cpp +++ b/raytracing/src/core/intersection-data.cpp @@ -35,7 +35,9 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ IntersectionData::IntersectionData(void) : - m_is_inside(false), m_distance(0), m_shape(nullptr) + m_is_inside(false), + m_distance(0), + m_shape(nullptr) { } diff --git a/raytracing/src/core/intersection.cpp b/raytracing/src/core/intersection.cpp index e612c33..11ce348 100644 --- a/raytracing/src/core/intersection.cpp +++ b/raytracing/src/core/intersection.cpp @@ -37,7 +37,9 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ Intersection::Intersection(void) : - m_is_nothing(true), m_distance_t(0.0), m_shape(nullptr) + m_is_nothing(true), + m_distance_t(0.0), + m_shape(nullptr) { } diff --git a/raytracing/src/core/matrix.cpp b/raytracing/src/core/matrix.cpp index 944cb8b..83ff0ae 100644 --- a/raytracing/src/core/matrix.cpp +++ b/raytracing/src/core/matrix.cpp @@ -39,14 +39,16 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ Matrix::Matrix(void) : - m_rows(0), m_cols(0) + m_rows(0), + m_cols(0) { } /* ------------------------------------------------------------------------- */ Matrix::Matrix(uint8_t a_rows, uint8_t a_cols) : - m_rows(a_rows), m_cols(a_cols) + m_rows(a_rows), + m_cols(a_cols) { m_data = std::vector>(m_rows, std::vector(m_cols)); for (int i = 0; i < m_rows; i++) @@ -61,7 +63,9 @@ Matrix::Matrix(uint8_t a_rows, uint8_t a_cols) : /* ------------------------------------------------------------------------- */ Matrix::Matrix(const Matrix &an_other) : - m_rows(an_other.m_rows), m_cols(an_other.m_cols), m_data(an_other.m_data) + m_rows(an_other.m_rows), + m_cols(an_other.m_cols), + m_data(an_other.m_data) { } diff --git a/raytracing/src/core/tuple.cpp b/raytracing/src/core/tuple.cpp index bf5999a..6fb8bc6 100644 --- a/raytracing/src/core/tuple.cpp +++ b/raytracing/src/core/tuple.cpp @@ -41,28 +41,40 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ Tuple::Tuple(void) : - m_x(0.0), m_y(0.0), m_z(0.0), m_w(0.0) + m_x(0.0), + m_y(0.0), + m_z(0.0), + m_w(0.0) { } /* ------------------------------------------------------------------------- */ Tuple::Tuple(const Tuple &a_copy) : - m_x(a_copy.m_x), m_y(a_copy.m_y), m_z(a_copy.m_z), m_w(a_copy.m_w) + m_x(a_copy.m_x), + m_y(a_copy.m_y), + m_z(a_copy.m_z), + m_w(a_copy.m_w) { } /* ------------------------------------------------------------------------- */ Tuple::Tuple(double a_x, double a_y, double a_z, double a_w) : - m_x(a_x), m_y(a_y), m_z(a_z), m_w(a_w) + m_x(a_x), + m_y(a_y), + m_z(a_z), + m_w(a_w) { } /* ------------------------------------------------------------------------- */ Tuple::Tuple(std::vector a_data) : - m_x(0.0), m_y(0.0), m_z(0.0), m_w(0.0) + m_x(0.0), + m_y(0.0), + m_z(0.0), + m_w(0.0) { int i = 0; for (auto the_it1 = a_data.cbegin(); the_it1 != a_data.cend(); ++the_it1) diff --git a/raytracing/src/lights/point-light.cpp b/raytracing/src/lights/point-light.cpp index 53b3da9..7315697 100644 --- a/raytracing/src/lights/point-light.cpp +++ b/raytracing/src/lights/point-light.cpp @@ -45,7 +45,8 @@ 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) + m_position(a_light.m_position), + m_intensity(a_light.m_intensity) { } diff --git a/raytracing/src/renderer/camera.cpp b/raytracing/src/renderer/camera.cpp index 65aab0a..7015db4 100644 --- a/raytracing/src/renderer/camera.cpp +++ b/raytracing/src/renderer/camera.cpp @@ -35,12 +35,15 @@ using namespace Raytracer; -#pragma omp parallel for num_threads(omp_get_num_procs()) - /* ------------------------------------------------------------------------- */ Camera::Camera(void) : - m_h_size(0), m_v_size(0), m_field_of_view(0), m_half_width(0), m_half_height(0), m_pixel_size(0) + m_h_size(0), + m_v_size(0), + m_field_of_view(0), + m_half_width(0), + m_half_height(0), + m_pixel_size(0) { } @@ -182,11 +185,12 @@ Canvas Camera::render(const World &a_world) { Canvas the_image(m_h_size, m_v_size); -#pragma omp parallel for shared(the_image) +#pragma omp parallel for num_threads(8) shared(the_image) for (int y = 0; y < m_v_size - 1; y++) { for (int x = 0; x < m_h_size - 1; x++) { + // printf("ray_for_pixel (process: %d)\n", omp_get_thread_num()); Ray the_ray = ray_for_pixel(x, y); Color the_color = a_world.color_at(the_ray); the_image.write_pixel(x, y, the_color); diff --git a/raytracing/src/renderer/canvas.cpp b/raytracing/src/renderer/canvas.cpp index 72081e7..754ba7d 100644 --- a/raytracing/src/renderer/canvas.cpp +++ b/raytracing/src/renderer/canvas.cpp @@ -39,14 +39,16 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ Canvas::Canvas(void) : - m_width(0), m_height(0) + m_width(0), + m_height(0) { } /* ------------------------------------------------------------------------- */ Canvas::Canvas(uint16_t a_width, uint16_t a_height) : - m_width(a_width), m_height(a_height) + m_width(a_width), + m_height(a_height) { m_pixels = std::vector>(m_width, std::vector(m_height)); diff --git a/raytracing/src/renderer/material.cpp b/raytracing/src/renderer/material.cpp index fa65f86..e2c5099 100644 --- a/raytracing/src/renderer/material.cpp +++ b/raytracing/src/renderer/material.cpp @@ -48,6 +48,8 @@ Material::Material(void) : m_specular(0.9), m_shininess(200), m_reflective(0.0), + m_transparency(0.0), + m_refractive_index(1.0), m_pattern(nullptr) { } @@ -58,7 +60,8 @@ bool Material::operator==(const Material &a_material) const { return (m_color == a_material.m_color) && double_equal(m_ambient, a_material.m_ambient) && double_equal(m_diffuse, a_material.m_diffuse) && double_equal(m_specular, a_material.m_specular) && - double_equal(m_shininess, a_material.m_shininess) && double_equal(m_reflective, a_material.m_reflective); + double_equal(m_shininess, a_material.m_shininess) && double_equal(m_reflective, a_material.m_reflective) && + double_equal(m_transparency, a_material.m_transparency) && double_equal(m_refractive_index, a_material.m_refractive_index); // TODO m_pattern } @@ -148,6 +151,34 @@ void Material::set_reflective(double a_value) /* ------------------------------------------------------------------------- */ +const double &Material::transparency(void) const +{ + return m_transparency; +} + +/* ------------------------------------------------------------------------- */ + +void Material::set_transparency(double a_value) +{ + m_transparency = a_value; +} + +/* ------------------------------------------------------------------------- */ + +const double &Material::refractive_index(void) const +{ + return m_refractive_index; +} + +/* ------------------------------------------------------------------------- */ + +void Material::set_refractive_index(double a_value) +{ + m_refractive_index = a_value; +} + +/* ------------------------------------------------------------------------- */ + Pattern *Material::pattern(void) { return m_pattern; diff --git a/raytracing/src/renderer/material.h b/raytracing/src/renderer/material.h index 54ff466..09b4a13 100644 --- a/raytracing/src/renderer/material.h +++ b/raytracing/src/renderer/material.h @@ -65,7 +65,14 @@ namespace Raytracer const double &reflective(void) const; void set_reflective(double a_value); - Pattern *pattern(void); + const double &transparency(void) const; + void set_transparency(double a_value); + + const double &refractive_index(void) const; + void set_refractive_index(double a_value); + + Pattern * + pattern(void); void set_pattern(Pattern *a_pattern); Color lighting(Shape *an_object, const PointLight &a_light, const Tuple &a_point, const Tuple &an_eyev, @@ -78,6 +85,8 @@ namespace Raytracer double m_specular; double m_shininess; double m_reflective; + double m_transparency; + double m_refractive_index; Pattern *m_pattern; }; }; // namespace Raytracer diff --git a/raytracing/src/renderer/ray.cpp b/raytracing/src/renderer/ray.cpp index 852d9c3..7f21bba 100644 --- a/raytracing/src/renderer/ray.cpp +++ b/raytracing/src/renderer/ray.cpp @@ -36,7 +36,8 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ Ray::Ray(Tuple an_origin, Tuple a_direction) : - m_origin(an_origin), m_direction(a_direction) + m_origin(an_origin), + m_direction(a_direction) { } diff --git a/raytracing/src/shapes/shape.cpp b/raytracing/src/shapes/shape.cpp index cf55f34..4af9b3b 100644 --- a/raytracing/src/shapes/shape.cpp +++ b/raytracing/src/shapes/shape.cpp @@ -48,14 +48,16 @@ Shape::Shape(void) : /* ------------------------------------------------------------------------- */ Shape::Shape(Shape &a_copy) : - m_transform(a_copy.m_transform), m_material(a_copy.m_material) + m_transform(a_copy.m_transform), + m_material(a_copy.m_material) { } /* ------------------------------------------------------------------------- */ Shape::Shape(const Shape &a_copy) : - m_transform(a_copy.m_transform), m_material(a_copy.m_material) + m_transform(a_copy.m_transform), + m_material(a_copy.m_material) { } diff --git a/raytracing/src/shapes/sphere.cpp b/raytracing/src/shapes/sphere.cpp index eb86371..32358ee 100644 --- a/raytracing/src/shapes/sphere.cpp +++ b/raytracing/src/shapes/sphere.cpp @@ -67,3 +67,15 @@ Tuple Sphere::local_normal_at(const Tuple &a_local_point) const { return a_local_point - Tuple::Point(0, 0, 0); } + +/* ------------------------------------------------------------------------- */ + +Sphere Sphere::Glass(void) +{ + Sphere the_sphere; + + the_sphere.material().set_transparency(1.0); + the_sphere.material().set_refractive_index(1.5); + + return the_sphere; +} \ No newline at end of file diff --git a/raytracing/src/shapes/sphere.h b/raytracing/src/shapes/sphere.h index 8f3bc6e..2ec6601 100644 --- a/raytracing/src/shapes/sphere.h +++ b/raytracing/src/shapes/sphere.h @@ -40,6 +40,8 @@ namespace Raytracer Sphere(void) = default; Intersections local_intersect(const Ray &a_ray) override; Tuple local_normal_at(const Tuple &a_local_point) const override; + + static Sphere Glass(void); }; }; // namespace Raytracer diff --git a/tests/11_reflection_refraction.cpp b/tests/11_reflection_refraction.cpp index 859c1c0..1c5062e 100644 --- a/tests/11_reflection_refraction.cpp +++ b/tests/11_reflection_refraction.cpp @@ -280,3 +280,43 @@ SCENARIO("The reflected color at the maximum recursive depth", "[features/world. } } } + +/* ------------------------------------------------------------------------- */ + +SCENARIO("Transparency and Refractive Index for the default material", "[features/materials.feature]") +{ + GIVEN("m <- material()") + { + Material m; + THEN("m.transparency = 0.0") + { + REQUIRE(m.transparency() == 0.0); + } + AND_THEN("m.refractive_index = 1.0") + { + REQUIRE(m.refractive_index() == 1.0); + } + } +} + +/* ------------------------------------------------------------------------- */ + +SCENARIO("A helper for producing a sphere with a glassy material", "[features/spheres.feature]") +{ + GIVEN("s <- glass_sphere()") + { + Sphere s = Sphere::Glass(); + THEN("s.transform = identity_matrix") + { + REQUIRE(s.transform() == Matrix::identity()); + } + AND_THEN("s.material.transparency = 1.0") + { + REQUIRE(s.material().transparency() == 1.0); + } + AND_THEN("s.material.refractive_index = 1.5") + { + REQUIRE(s.material().refractive_index() == 1.5); + } + } +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b44383b..797808f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -26,6 +26,6 @@ add_executable(raytracing_test include_directories("${CMAKE_SOURCE_DIR}/tests") -target_link_libraries(raytracing_test PRIVATE raytracing gcov) +target_link_libraries(raytracing_test PRIVATE raytracing gcov OpenMP::OpenMP_CXX) add_test(NAME raytracing_test COMMAND tests/raytracing_test)