Files
raytracer_challenge/apps/chapter_11_p2.cpp
2024-03-13 13:23:36 +01:00

210 lines
8.7 KiB
C++

/*!
* chapter_11_p2.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: 12/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_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");
// 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->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(-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(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);
// 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);
// 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(-4.9, 4.9, -1), Color(1, 1, 1)));
// Configure the camera.
// 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(-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();
the_canvas.save_to_file("chapter_11_p2.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