233 lines
11 KiB
C++
233 lines
11 KiB
C++
/*!
|
|
* chapter_13.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: 18/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
|
|
|
|
// Coordinate came from:
|
|
// https://github.com/sraaphorst/raytracer-kotlin/blob/main/src/main/kotlin/apps/ch13_world.kt
|
|
|
|
#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;
|
|
Cylinder *the_cylinder, *the_glass_cylinder;
|
|
Cylinder *the_concentric_cylinder1, *the_concentric_cylinder2, *the_concentric_cylinder3, *the_concentric_cylinder4;
|
|
Cylinder *the_decorative_cylinder1, *the_decorative_cylinder2, *the_decorative_cylinder3, *the_decorative_cylinder4;
|
|
chrono::time_point<chrono::high_resolution_clock> the_start, the_end;
|
|
|
|
printf("Chapter 13 example.\n");
|
|
|
|
// Floor is an extremely flattened sphere with a matte texture.
|
|
the_floor = new Plane();
|
|
the_floor->set_transform(Matrix::rotation_y(0.3) *
|
|
Matrix::scaling(0.25, 0.25, 0.25));
|
|
Material &the_floor_material = the_floor->material();
|
|
the_floor_material.set_pattern(new CheckersPattern(Color(0.5, 0.5, 0.5), Color(0.75, 0.75, 0.75)));
|
|
the_floor_material.set_ambient(0.9);
|
|
the_floor_material.set_diffuse(0.9);
|
|
the_floor_material.set_specular(0);
|
|
the_world.add_object(the_floor);
|
|
|
|
// Cylinder
|
|
the_cylinder = new Cylinder();
|
|
the_cylinder->set_minimum(0);
|
|
the_cylinder->set_maximum(0.75);
|
|
the_cylinder->set_transform(Matrix::translation(-1, 0, 1) *
|
|
Matrix::scaling(0.5, 1, 0.5));
|
|
Material &the_cylinder_material = the_cylinder->material();
|
|
the_cylinder_material.set_color(Color(0, 0, 0.6));
|
|
the_cylinder_material.set_diffuse(0.1);
|
|
the_cylinder_material.set_specular(0.9);
|
|
the_cylinder_material.set_shininess(300);
|
|
the_cylinder_material.set_reflective(0.9);
|
|
the_world.add_object(the_cylinder);
|
|
|
|
// Concentric Cylinder 1
|
|
the_concentric_cylinder1 = new Cylinder();
|
|
the_concentric_cylinder1->set_minimum(0);
|
|
the_concentric_cylinder1->set_maximum(0.2);
|
|
the_concentric_cylinder1->set_transform(Matrix::translation(1, 0, 0) *
|
|
Matrix::scaling(0.8, 1, 0.8));
|
|
Material &the_concentric_cylinder1_material = the_concentric_cylinder1->material();
|
|
the_concentric_cylinder1_material.set_color(Color(1, 1, 0.3));
|
|
the_concentric_cylinder1_material.set_ambient(0.1);
|
|
the_concentric_cylinder1_material.set_diffuse(0.8);
|
|
the_concentric_cylinder1_material.set_specular(0.9);
|
|
the_concentric_cylinder1_material.set_shininess(300);
|
|
the_world.add_object(the_concentric_cylinder1);
|
|
|
|
// Concentric Cylinder 2
|
|
the_concentric_cylinder2 = new Cylinder();
|
|
the_concentric_cylinder2->set_minimum(0);
|
|
the_concentric_cylinder2->set_maximum(0.3);
|
|
the_concentric_cylinder2->set_transform(Matrix::translation(1, 0, 0) *
|
|
Matrix::scaling(0.6, 1, 0.6));
|
|
Material &the_concentric_cylinder2_material = the_concentric_cylinder2->material();
|
|
the_concentric_cylinder2_material.set_color(Color(1, 0.9, 0.4));
|
|
the_concentric_cylinder2_material.set_ambient(0.1);
|
|
the_concentric_cylinder2_material.set_diffuse(0.8);
|
|
the_concentric_cylinder2_material.set_specular(0.9);
|
|
the_concentric_cylinder2_material.set_shininess(300);
|
|
the_world.add_object(the_concentric_cylinder2);
|
|
|
|
// Concentric Cylinder 3
|
|
the_concentric_cylinder3 = new Cylinder();
|
|
the_concentric_cylinder3->set_minimum(0);
|
|
the_concentric_cylinder3->set_maximum(0.4);
|
|
the_concentric_cylinder3->set_transform(Matrix::translation(1, 0, 0) *
|
|
Matrix::scaling(0.4, 1, 0.4));
|
|
Material &the_concentric_cylinder3_material = the_concentric_cylinder3->material();
|
|
the_concentric_cylinder3_material.set_color(Color(1, 0.8, 0.5));
|
|
the_concentric_cylinder3_material.set_ambient(0.1);
|
|
the_concentric_cylinder3_material.set_diffuse(0.8);
|
|
the_concentric_cylinder3_material.set_specular(0.9);
|
|
the_concentric_cylinder3_material.set_shininess(300);
|
|
the_world.add_object(the_concentric_cylinder3);
|
|
|
|
// Concentric Cylinder 4
|
|
the_concentric_cylinder4 = new Cylinder();
|
|
the_concentric_cylinder4->set_minimum(0);
|
|
the_concentric_cylinder4->set_maximum(0.5);
|
|
the_concentric_cylinder4->set_transform(Matrix::translation(1, 0, 0) *
|
|
Matrix::scaling(0.2, 1, 0.2));
|
|
Material &the_concentric_cylinder4_material = the_concentric_cylinder4->material();
|
|
the_concentric_cylinder4_material.set_color(Color(1, 0.7, 0.6));
|
|
the_concentric_cylinder4_material.set_ambient(0.1);
|
|
the_concentric_cylinder4_material.set_diffuse(0.8);
|
|
the_concentric_cylinder4_material.set_specular(0.9);
|
|
the_concentric_cylinder4_material.set_shininess(300);
|
|
the_world.add_object(the_concentric_cylinder4);
|
|
|
|
// Decorative Cylinder 1
|
|
the_decorative_cylinder1 = new Cylinder();
|
|
the_decorative_cylinder1->set_minimum(0);
|
|
the_decorative_cylinder1->set_maximum(0.3);
|
|
the_decorative_cylinder1->set_transform(Matrix::translation(0, 0, -0.75) *
|
|
Matrix::scaling(0.05, 1, 0.05));
|
|
Material &the_decorative_cylinder1_material = the_decorative_cylinder1->material();
|
|
the_decorative_cylinder1_material.set_color(Color::Red());
|
|
the_decorative_cylinder1_material.set_ambient(0.1);
|
|
the_decorative_cylinder1_material.set_diffuse(0.9);
|
|
the_decorative_cylinder1_material.set_specular(0.9);
|
|
the_decorative_cylinder1_material.set_shininess(300);
|
|
the_world.add_object(the_decorative_cylinder1);
|
|
|
|
// Decorative Cylinder 2
|
|
the_decorative_cylinder2 = new Cylinder();
|
|
the_decorative_cylinder2->set_minimum(0);
|
|
the_decorative_cylinder2->set_maximum(0.3);
|
|
the_decorative_cylinder2->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.15) *
|
|
Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05));
|
|
Material &the_decorative_cylinder2_material = the_decorative_cylinder2->material();
|
|
the_decorative_cylinder2_material.set_color(Color::Yellow());
|
|
the_decorative_cylinder2_material.set_ambient(0.1);
|
|
the_decorative_cylinder2_material.set_diffuse(0.9);
|
|
the_decorative_cylinder2_material.set_specular(0.9);
|
|
the_decorative_cylinder2_material.set_shininess(300);
|
|
the_world.add_object(the_decorative_cylinder2);
|
|
|
|
// Decorative Cylinder 3
|
|
the_decorative_cylinder3 = new Cylinder();
|
|
the_decorative_cylinder3->set_minimum(0);
|
|
the_decorative_cylinder3->set_maximum(0.3);
|
|
the_decorative_cylinder3->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.3) *
|
|
Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05));
|
|
Material &the_decorative_cylinder3_material = the_decorative_cylinder3->material();
|
|
the_decorative_cylinder3_material.set_color(Color::Green());
|
|
the_decorative_cylinder3_material.set_ambient(0.1);
|
|
the_decorative_cylinder3_material.set_diffuse(0.9);
|
|
the_decorative_cylinder3_material.set_specular(0.9);
|
|
the_decorative_cylinder3_material.set_shininess(300);
|
|
the_world.add_object(the_decorative_cylinder3);
|
|
|
|
// Decorative Cylinder 4
|
|
the_decorative_cylinder4 = new Cylinder();
|
|
the_decorative_cylinder4->set_minimum(0);
|
|
the_decorative_cylinder4->set_maximum(0.3);
|
|
the_decorative_cylinder4->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.45) *
|
|
Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05));
|
|
Material &the_decorative_cylinder4_material = the_decorative_cylinder4->material();
|
|
the_decorative_cylinder4_material.set_color(Color::Cyan());
|
|
the_decorative_cylinder4_material.set_ambient(0.1);
|
|
the_decorative_cylinder4_material.set_diffuse(0.9);
|
|
the_decorative_cylinder4_material.set_specular(0.9);
|
|
the_decorative_cylinder4_material.set_shininess(300);
|
|
the_world.add_object(the_decorative_cylinder4);
|
|
|
|
// Glass Cylinder
|
|
the_glass_cylinder = new Cylinder();
|
|
the_glass_cylinder->set_minimum(0.0001);
|
|
the_glass_cylinder->set_maximum(0.5);
|
|
the_glass_cylinder->set_transform(Matrix::translation(0, 0, -1.5) *
|
|
Matrix::scaling(0.33, 1, 0.33));
|
|
Material &the_glass_cylinder_material = the_glass_cylinder->material();
|
|
the_glass_cylinder_material.set_color(Color(0.25, 0, 0));
|
|
the_glass_cylinder_material.set_ambient(0.1);
|
|
the_glass_cylinder_material.set_specular(0.9);
|
|
the_glass_cylinder_material.set_shininess(300);
|
|
the_glass_cylinder_material.set_reflective(0.9);
|
|
the_glass_cylinder_material.set_transparency(0.9);
|
|
the_glass_cylinder_material.set_refractive_index(1.5);
|
|
the_world.add_object(the_glass_cylinder);
|
|
|
|
// The Light source is white, shining from above and to the left
|
|
the_world.set_light(PointLight(Tuple::Point(1, 6.9, -4.9), Color(1, 1, 1)));
|
|
|
|
// Configure the camera.
|
|
// the_camera = Camera(100, 50, std::numbers::pi / 10);
|
|
// the_camera = Camera(320, 200, std::numbers::pi / 10);
|
|
the_camera = Camera(640, 480, std::numbers::pi / 10);
|
|
the_camera.show_progress_bar();
|
|
the_camera.set_transform(
|
|
Matrix::view_transform(Tuple::Point(8, 3.5, -9), Tuple::Point(0, 0.3, 0), 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("chapter13.ppm");
|
|
|
|
chrono::duration<double> the_elapsed_time = the_end - the_start;
|
|
printf("Execution Time: %f secondes\n", the_elapsed_time.count());
|
|
|
|
return 0;
|
|
}
|