[wIP] Reflection is working
This commit is contained in:
@@ -19,3 +19,6 @@ 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)
|
||||
|
||||
add_executable(chapter_11 chapter_11.cpp)
|
||||
target_link_libraries(chapter_11 PRIVATE raytracing gcov)
|
||||
|
||||
118
apps/chapter_11.cpp
Normal file
118
apps/chapter_11.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/*!
|
||||
* chapter_11.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: 05/03/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 <chrono>
|
||||
#include <cstdio>
|
||||
|
||||
#include <raytracing.h>
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
using namespace Raytracer;
|
||||
using namespace std;
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
World the_world;
|
||||
Camera the_camera;
|
||||
Canvas the_canvas;
|
||||
Plane *the_floor, *the_left_wall, *the_right_wall;
|
||||
Sphere *the_middle, *the_right, *the_left;
|
||||
chrono::time_point<chrono::high_resolution_clock> the_start, the_end;
|
||||
|
||||
printf("Chapter 11 example.\n");
|
||||
|
||||
// Floor is an extremely flattened sphere with a matte texture.
|
||||
the_floor = new Plane();
|
||||
Material &the_floor_material = the_floor->material();
|
||||
the_floor_material.set_color(Color(1, 0.9, 0.9));
|
||||
the_floor_material.set_specular(0);
|
||||
the_floor_material.set_reflective(0.5);
|
||||
the_floor_material.set_pattern(new CheckersPattern(Color(0.52, 0.52, 0.52), Color::Black()));
|
||||
the_world.add_object(the_floor);
|
||||
|
||||
// Left Wall
|
||||
the_left_wall = new Plane();
|
||||
the_left_wall->set_transform(Matrix::translation(0, 0, 10) *
|
||||
Matrix::rotation_y(-std::numbers::pi / 4) *
|
||||
Matrix::rotation_x(std::numbers::pi / 2));
|
||||
Material &the_left_wall_material = the_left_wall->material();
|
||||
the_left_wall_material.set_specular(0);
|
||||
the_left_wall_material.set_pattern(new StripePattern(Color(0.52, 0.52, 0.52), Color::Black()));
|
||||
the_left_wall_material.pattern()->set_transform(Matrix::rotation_y(std::numbers::pi / 2));
|
||||
the_world.add_object(the_left_wall);
|
||||
|
||||
// Right Wall
|
||||
the_right_wall = new Plane();
|
||||
the_right_wall->set_transform(Matrix::translation(0, 5, 10) *
|
||||
Matrix::rotation_y(std::numbers::pi / 4) *
|
||||
Matrix::rotation_x(std::numbers::pi / 2));
|
||||
Material &the_right_wall_material = the_right_wall->material();
|
||||
the_right_wall_material.set_specular(0);
|
||||
the_right_wall_material.set_pattern(new StripePattern(Color(0.52, 0.52, 0.52), Color::Black()));
|
||||
the_right_wall_material.pattern()->set_transform(Matrix::rotation_y(std::numbers::pi / 2));
|
||||
the_world.add_object(the_right_wall);
|
||||
|
||||
// The large sphere in the middle is a unit sphere, translated upward slightly and colored green.
|
||||
the_middle = new Sphere();
|
||||
the_middle->set_transform(Matrix::translation(-0.25, 1, 1.5) *
|
||||
Matrix::rotation_y(-std::numbers::pi / 1.5) *
|
||||
Matrix::rotation_z(-std::numbers::pi / 6));
|
||||
|
||||
Material &the_middle_material = the_middle->material();
|
||||
the_middle_material.set_color(Color(0.75, 0.24, 0.14));
|
||||
the_middle_material.set_diffuse(0.7);
|
||||
the_middle_material.set_specular(0.3);
|
||||
the_world.add_object(the_middle);
|
||||
|
||||
// The Light source is white, shining from above and to the left
|
||||
the_world.set_light(PointLight(Tuple::Point(-10, 10, -10), Color(1, 1, 1)));
|
||||
|
||||
// Configure the camera.
|
||||
// the_camera = Camera(100, 50, std::numbers::pi / 3);
|
||||
// the_camera = Camera(320, 200, std::numbers::pi / 3);
|
||||
the_camera = Camera(640, 480, std::numbers::pi / 3);
|
||||
the_camera.set_transform(
|
||||
Matrix::view_transform(Tuple::Point(0, 1.5, -5), Tuple::Point(0, 1, 0), Tuple::Vector(0, 1, 0)));
|
||||
|
||||
the_start = chrono::high_resolution_clock::now();
|
||||
the_canvas = the_camera.render(the_world);
|
||||
the_end = chrono::high_resolution_clock::now();
|
||||
|
||||
the_canvas.save_to_file("chapter11.ppm");
|
||||
|
||||
chrono::duration<double> the_elapsed_time = the_end - the_start;
|
||||
printf("Execution Time: %f secondes\n", the_elapsed_time.count());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Chapter 11 example.
|
||||
// Execution Time: 904.052568 secondes
|
||||
BIN
data/chapter_11.png
Normal file
BIN
data/chapter_11.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 45 KiB |
@@ -62,6 +62,13 @@ Color::Color(double a_red, double a_green, double a_blue) :
|
||||
|
||||
bool Color::operator==(const Color &a_color) const
|
||||
{
|
||||
#if 0
|
||||
bool the_eq_red = double_equal(m_red, a_color.m_red);
|
||||
bool the_eq_green = double_equal(m_green, a_color.m_green);
|
||||
bool the_eq_blue = double_equal(m_blue, a_color.m_blue);
|
||||
|
||||
#endif
|
||||
|
||||
if (double_equal(m_red, a_color.m_red) && double_equal(m_green, a_color.m_green) &&
|
||||
double_equal(m_blue, a_color.m_blue))
|
||||
{
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#ifndef _RAYTRACER_COMMON_H
|
||||
#define _RAYTRACER_COMMON_H
|
||||
|
||||
#define kEpsilon 0.00001
|
||||
#define kEpsilon 0.0001
|
||||
|
||||
namespace Raytracer
|
||||
{
|
||||
|
||||
@@ -158,31 +158,38 @@ Intersections World::intersect_world(const Ray &a_ray) const
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color World::shade_hit(const IntersectionData &an_intersection_data) const
|
||||
Color World::shade_hit(const IntersectionData &an_intersection_data, uint32_t a_remainging) const
|
||||
{
|
||||
bool the_shadowed = is_shadowed(an_intersection_data.over_point());
|
||||
Shape *the_object = an_intersection_data.object();
|
||||
return the_object->material().lighting(the_object, m_light, an_intersection_data.over_point(),
|
||||
Color the_surface = the_object->material().lighting(the_object, m_light, an_intersection_data.over_point(),
|
||||
an_intersection_data.eyev(), an_intersection_data.normalv(), the_shadowed);
|
||||
Color the_reflected = reflected_color(an_intersection_data, a_remainging);
|
||||
return the_surface + the_reflected;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color World::reflected_color(const IntersectionData &a_data) const
|
||||
Color World::reflected_color(const IntersectionData &a_data, uint32_t a_remainging) const
|
||||
{
|
||||
if (a_remainging <= 0)
|
||||
{
|
||||
return Color(0, 0, 0);
|
||||
}
|
||||
|
||||
if (a_data.object()->material().reflective() == 0.0)
|
||||
{
|
||||
return Color(0, 0, 0);
|
||||
}
|
||||
Ray the_reflected_ray(a_data.over_point(), a_data.reflectv());
|
||||
Color the_color = color_at(the_reflected_ray);
|
||||
Color the_color = color_at(the_reflected_ray, a_remainging - 1);
|
||||
|
||||
return the_color * a_data.object()->material().reflective();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color World::color_at(const Ray &a_ray) const
|
||||
Color World::color_at(const Ray &a_ray, uint32_t a_remainging) const
|
||||
{
|
||||
Color the_color = Color::Black();
|
||||
|
||||
@@ -200,7 +207,7 @@ Color World::color_at(const Ray &a_ray) const
|
||||
|
||||
IntersectionData the_comps = the_intersec.prepare_computations(a_ray);
|
||||
|
||||
the_color = shade_hit(the_comps);
|
||||
the_color = shade_hit(the_comps, a_remainging);
|
||||
|
||||
return the_color;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
#define kRemainingDefaultDepth 4
|
||||
|
||||
namespace Raytracer
|
||||
{
|
||||
class Shape;
|
||||
@@ -63,9 +65,9 @@ namespace Raytracer
|
||||
bool contains(const Shape &a_shape);
|
||||
|
||||
Intersections intersect_world(const Ray &a_ray) const;
|
||||
Color shade_hit(const IntersectionData &an_intersection_data) const;
|
||||
Color reflected_color(const IntersectionData &an_intersection_data) const;
|
||||
Color color_at(const Ray &a_ray) const;
|
||||
Color shade_hit(const IntersectionData &an_intersection_data, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
||||
Color reflected_color(const IntersectionData &an_intersection_data, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
||||
Color color_at(const Ray &a_ray, uint32_t a_remainging = kRemainingDefaultDepth) const;
|
||||
|
||||
bool is_shadowed(const Tuple &a_point) const;
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ SCENARIO("The reflected color for a reflective material", "[features/world.featu
|
||||
AND_GIVEN("ray(point(0, 0, -3), vector(0, -sqrt(2) / 2, sqrt(2) / 2)")
|
||||
{
|
||||
Ray r(Tuple::Point(0, 0, -3), Tuple::Vector(0, -sqrt(2) / 2, sqrt(2) / 2));
|
||||
AND_GIVEN("i <- intersection(1, shape)")
|
||||
AND_GIVEN("i <- intersection(sqrt(2), shape)")
|
||||
{
|
||||
Intersection i(sqrt(2), &shape);
|
||||
WHEN("comps <- prepare_computation(i, r)")
|
||||
@@ -138,7 +138,7 @@ SCENARIO("The reflected color for a reflective material", "[features/world.featu
|
||||
AND_WHEN("color <- reflected_color(w, comps)")
|
||||
{
|
||||
Color color = w.reflected_color(comps);
|
||||
THEN("color = color(0, 0, 0)")
|
||||
THEN("color = color(0.19032, 0.2379, 0.14274)")
|
||||
{
|
||||
REQUIRE(color == Color(0.19032, 0.2379, 0.14274));
|
||||
}
|
||||
@@ -150,3 +150,133 @@ SCENARIO("The reflected color for a reflective material", "[features/world.featu
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("shade_it() with a reflective material", "[features/world.feature]")
|
||||
{
|
||||
GIVEN("w <- default_world()")
|
||||
{
|
||||
World w = World::default_world();
|
||||
AND_GIVEN("shape <- plane() with:")
|
||||
// | material.reflective | 0.5 |
|
||||
// | transform | translation(0, -1, 0) |
|
||||
{
|
||||
Plane shape;
|
||||
shape.material().set_reflective(0.5);
|
||||
shape.set_transform(Matrix::translation(0, -1, 0));
|
||||
AND_GIVEN("shape is added to w")
|
||||
{
|
||||
w.add_object(&shape);
|
||||
AND_GIVEN("ray(point(0, 0, -3), vector(0, -sqrt(2) / 2, sqrt(2) / 2)")
|
||||
{
|
||||
Ray r(Tuple::Point(0, 0, -3), Tuple::Vector(0, -sqrt(2) / 2, sqrt(2) / 2));
|
||||
AND_GIVEN("i <- intersection(sqrt(2), shape)")
|
||||
{
|
||||
Intersection i(sqrt(2), &shape);
|
||||
WHEN("comps <- prepare_computation(i, r)")
|
||||
{
|
||||
IntersectionData comps = i.prepare_computations(r);
|
||||
AND_WHEN("color <- shade_hit(w, comps)")
|
||||
{
|
||||
Color color = w.shade_hit(comps);
|
||||
THEN("color = color(0.87677, 0.92436, 0.82918)")
|
||||
{
|
||||
REQUIRE(color == Color(0.87677, 0.92436, 0.82918));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("color_at() with mutually reflective surfaces", "[features/world.feature]")
|
||||
{
|
||||
GIVEN("w <- default_world()")
|
||||
{
|
||||
World w = World::default_world();
|
||||
AND_GIVEN("w.light <- point_light(point(0, 0, 0), color(1, 1, 1))")
|
||||
{
|
||||
w.set_light(PointLight(Tuple::Point(0, 0, 0), Color(1, 1, 1)));
|
||||
AND_GIVEN("lower <- plane() with:")
|
||||
// | material.reflective | 1 |
|
||||
// | transform | translation(0, -1, 0) |
|
||||
{
|
||||
Plane lower;
|
||||
lower.material().set_reflective(1);
|
||||
lower.set_transform(Matrix::translation(0, -1, 0));
|
||||
AND_GIVEN("lower is added to w")
|
||||
{
|
||||
w.add_object(&lower);
|
||||
AND_GIVEN("upper <- plane() with:")
|
||||
// | material.reflective | 1 |
|
||||
// | transform | translation(0, 1, 0) |
|
||||
{
|
||||
Plane upper;
|
||||
upper.material().set_reflective(1);
|
||||
upper.set_transform(Matrix::translation(0, 1, 0));
|
||||
AND_GIVEN("upper is added to w")
|
||||
{
|
||||
w.add_object(&upper);
|
||||
AND_GIVEN("ray(point(0, 0, 0), vector(0, 1, 0)")
|
||||
{
|
||||
Ray r(Tuple::Point(0, 0, 0), Tuple::Vector(0, 1, 0));
|
||||
THEN("color_at(w, r) terminate successfully")
|
||||
{
|
||||
w.color_at(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("The reflected color at the maximum recursive depth", "[features/world.feature]")
|
||||
{
|
||||
GIVEN("w <- default_world()")
|
||||
{
|
||||
World w = World::default_world();
|
||||
AND_GIVEN("shape <- plane() with:")
|
||||
// | material.reflective | 0.5 |
|
||||
// | transform | translation(0, -1, 0) |
|
||||
{
|
||||
Plane shape;
|
||||
shape.material().set_reflective(0.5);
|
||||
shape.set_transform(Matrix::translation(0, -1, 0));
|
||||
AND_GIVEN("shape is added to w")
|
||||
{
|
||||
w.add_object(&shape);
|
||||
AND_GIVEN("ray(point(0, 0, -3), vector(0, -sqrt(2) / 2, sqrt(2) / 2)")
|
||||
{
|
||||
Ray r(Tuple::Point(0, 0, -3), Tuple::Vector(0, -sqrt(2) / 2, sqrt(2) / 2));
|
||||
AND_GIVEN("i <- intersection(sqrt(2), shape)")
|
||||
{
|
||||
Intersection i(sqrt(2), &shape);
|
||||
WHEN("comps <- prepare_computation(i, r)")
|
||||
{
|
||||
IntersectionData comps = i.prepare_computations(r);
|
||||
AND_WHEN("color <- reflected_color(w, comps, 0)")
|
||||
{
|
||||
Color color = w.reflected_color(comps, 0);
|
||||
THEN("color = color(0.19032, 0.2379, 0.14274)")
|
||||
{
|
||||
REQUIRE(color == Color(0, 0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user