[FEAT] Finish the Chapter 11

This commit is contained in:
NADAL Jean-Baptiste
2024-03-13 13:23:36 +01:00
parent 0da549d969
commit d2fe968717
23 changed files with 506 additions and 58 deletions

View File

@@ -26,4 +26,4 @@ add_executable(chapter_11_p1 chapter_11_p1.cpp)
target_link_libraries(chapter_11_p1 PRIVATE raytracing gcov OpenMP::OpenMP_CXX)
add_executable(chapter_11_p2 chapter_11_p2.cpp)
target_link_libraries(chapter_11_p2 PRIVATE raytracing gcov OpenMP::OpenMP_CXX)
target_link_libraries(chapter_11_p2 PRIVATE raytracing OpenMP::OpenMP_CXX)

View File

@@ -43,70 +43,158 @@ 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;
Plane *the_floor, *the_ceiling, *the_left_wall, *the_right_wall, *the_front_wall, *the_back_wall;
Sphere *the_back_sphere1, *the_back_sphere2, *the_back_sphere3, *the_back_sphere4;
Sphere *the_red_sphere, *the_green_sphere, *the_blue_sphere;
chrono::time_point<chrono::high_resolution_clock> the_start, the_end;
printf("Chapter 11 part2 example.\n");
// Floor is an extremely flattened sphere with a matte texture.
// Wall Material
Material the_wall_material;
the_wall_material.set_pattern(new StripePattern(Color(0.45, 0.45, 0.45), Color(0.35, 0.35, 0.35)));
the_wall_material.pattern()->set_transform(Matrix::rotation_y(std::numbers::pi / 2) *
Matrix::scaling(0.25, 0.25, 0.25));
the_wall_material.set_ambient(0.0);
the_wall_material.set_diffuse(0.4);
the_wall_material.set_specular(0.0);
the_wall_material.set_reflective(0.3);
// Floor
the_floor = new Plane();
Material &the_floor_material = the_floor->material();
the_floor_material.set_pattern(new CheckersPattern(Color(0.35, 0.35, 0.35), Color(0.65, 0.65, 0.65)));
the_floor_material.set_specular(0.0);
the_floor_material.set_reflective(0.4);
the_floor_material.set_pattern(new CheckersPattern(Color(0.35, 0.35, 0.35), Color(0.65, 0.65, 0.65)));
the_floor->set_transform(Matrix::rotation_y(-std::numbers::pi / 10));
the_world.add_object(the_floor);
// Ceiling
the_ceiling = new Plane();
Material &the_ceiling_material = the_ceiling->material();
the_ceiling_material.set_color(Color(0.8, 0.8, 0.8));
the_ceiling_material.set_ambient(0.3);
the_ceiling_material.set_specular(0.0);
the_ceiling->set_transform(Matrix::translation(0, 5, 0));
the_world.add_object(the_ceiling);
// 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_left_wall->set_transform(Matrix::translation(-5, 0, 0) *
Matrix::rotation_z(-std::numbers::pi / 2) *
Matrix::rotation_y(std::numbers::pi / 2));
the_left_wall->set_material(the_wall_material);
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_right_wall->set_transform(Matrix::translation(5, 0, 0) *
Matrix::rotation_z(-std::numbers::pi / 2) *
Matrix::rotation_y(std::numbers::pi / 2));
the_right_wall->set_material(the_wall_material);
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));
// Front Wall
the_front_wall = new Plane();
the_front_wall->set_transform(Matrix::translation(0, 0, 5) *
Matrix::rotation_x(-std::numbers::pi / 2));
the_front_wall->set_material(the_wall_material);
the_world.add_object(the_front_wall);
Material &the_middle_material = the_middle->material();
the_middle_material.set_color(Color(0, 0.2, 0.));
the_middle_material.set_ambient(0.0);
the_middle_material.set_diffuse(0.4);
the_middle_material.set_specular(0.9);
the_middle_material.set_shininess(300);
the_middle_material.set_transparency(0.9);
the_middle_material.set_reflective(0.9);
the_middle_material.set_refractive_index(1.5);
the_world.add_object(the_middle);
// Back Wall
the_back_wall = new Plane();
the_back_wall->set_transform(Matrix::translation(0, 0, -5) *
Matrix::rotation_x(-std::numbers::pi / 2));
the_back_wall->set_material(the_wall_material);
the_world.add_object(the_back_wall);
// Back Sphere 1
the_back_sphere1 = new Sphere();
the_back_sphere1->set_transform(Matrix::translation(4.6, 0.4, 1.0) *
Matrix::scaling(0.4, 0.4, 0.4));
Material &the_back_sphere1_material = the_back_sphere1->material();
the_back_sphere1_material.set_color(Color(0.8, 0.5, 0.3));
the_back_sphere1_material.set_shininess(50.0);
the_world.add_object(the_back_sphere1);
// Back Sphere 2
the_back_sphere2 = new Sphere();
the_back_sphere2->set_transform(Matrix::translation(4.7, 0.3, 0.4) *
Matrix::scaling(0.3, 0.3, 0.3));
Material &the_back_sphere2_material = the_back_sphere2->material();
the_back_sphere2_material.set_color(Color(0.9, 0.4, 0.5));
the_back_sphere2_material.set_shininess(50.0);
the_world.add_object(the_back_sphere2);
// Back Sphere 3
the_back_sphere3 = new Sphere();
the_back_sphere3->set_transform(Matrix::translation(-1, 0.5, 4.5) *
Matrix::scaling(0.5, 0.5, 0.5));
Material &the_back_sphere3_material = the_back_sphere3->material();
the_back_sphere3_material.set_color(Color(0.4, 0.9, 0.6));
the_back_sphere3_material.set_shininess(50.0);
the_world.add_object(the_back_sphere3);
// Back Sphere 4
the_back_sphere4 = new Sphere();
the_back_sphere4->set_transform(Matrix::translation(-1.7, 0.3, 4.7) *
Matrix::scaling(0.3, 0.3, 0.3));
Material &the_back_sphere4_material = the_back_sphere4->material();
the_back_sphere4_material.set_color(Color(0.4, 0.6, 0.9));
the_back_sphere4_material.set_shininess(50.0);
the_world.add_object(the_back_sphere4);
// Red Sphere
the_red_sphere = new Sphere();
the_red_sphere->set_transform(Matrix::translation(-0.6, 1, 0.6));
Material &the_red_sphere_material = the_red_sphere->material();
the_red_sphere_material.set_color(Color(1, 0.3, 0.2));
the_red_sphere_material.set_specular(0.4);
the_red_sphere_material.set_shininess(5.0);
the_world.add_object(the_red_sphere);
// Green Sphere
the_green_sphere = new Sphere();
the_green_sphere->set_transform(Matrix::translation(-0.7, 0.5, -0.8) *
Matrix::scaling(0.5, 0.5, 0.5));
Material &the_green_sphere_material = the_green_sphere->material();
the_green_sphere_material.set_color(Color(0, 0.2, 0));
the_green_sphere_material.set_ambient(0.0);
the_green_sphere_material.set_diffuse(0.4);
the_green_sphere_material.set_specular(0.9);
the_green_sphere_material.set_shininess(300.0);
the_green_sphere_material.set_reflective(0.9);
the_green_sphere_material.set_transparency(0.9);
the_green_sphere_material.set_refractive_index(1.5);
the_world.add_object(the_green_sphere);
// Blue Sphere
the_blue_sphere = new Sphere();
the_blue_sphere->set_transform(Matrix::translation(0.6, 0.7, -0.6) *
Matrix::scaling(0.7, 0.7, 0.7));
Material &the_blue_sphere_material = the_blue_sphere->material();
the_blue_sphere_material.set_color(Color(0, 0., 0.2));
the_blue_sphere_material.set_ambient(0.0);
the_blue_sphere_material.set_diffuse(0.4);
the_blue_sphere_material.set_specular(0.9);
the_blue_sphere_material.set_shininess(300.0);
the_blue_sphere_material.set_reflective(0.9);
the_blue_sphere_material.set_transparency(0.9);
the_blue_sphere_material.set_refractive_index(1.5);
the_world.add_object(the_blue_sphere);
// 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)));
the_world.set_light(PointLight(Tuple::Point(-4.9, 4.9, -1), 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 = Camera(100, 50, 1.152);
// the_camera = Camera(320, 200, 1.152);
the_camera = Camera(640, 480, 1.152);
the_camera.set_transform(
Matrix::view_transform(Tuple::Point(0, 1.5, -5), Tuple::Point(0, 1, 0), Tuple::Vector(0, 1, 0)));
Matrix::view_transform(Tuple::Point(-2.6, 1.5, -3.9), Tuple::Point(-0.6, 1, -0.8), Tuple::Vector(0, 1, 0)));
the_start = chrono::high_resolution_clock::now();
the_camera.show_progress_bar();
the_canvas = the_camera.render(the_world);
the_end = chrono::high_resolution_clock::now();