[FEAT] Material add transparency and refractive_index into material

This commit is contained in:
NADAL Jean-Baptiste
2024-03-06 16:59:43 +01:00
parent cd67bdbda5
commit 548d765fcb
18 changed files with 157 additions and 28 deletions

View File

@@ -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)

View File

@@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(--coverage)
add_definitions(-fopenmp)
include_directories (src)

View File

@@ -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);
}

View File

@@ -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)
{
}

View File

@@ -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)
{
}

View File

@@ -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<std::vector<double>>(m_rows, std::vector<double>(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)
{
}

View File

@@ -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<double> 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)

View File

@@ -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)
{
}

View File

@@ -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);

View File

@@ -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<std::vector<Color>>(m_width, std::vector<Color>(m_height));

View File

@@ -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;

View File

@@ -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

View File

@@ -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)
{
}

View File

@@ -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)
{
}

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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);
}
}
}

View File

@@ -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)