[FEAT] Chapter 07 is done.
This commit is contained in:
108
apps/chapter_07.cpp
Normal file
108
apps/chapter_07.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
/*!
|
||||
* chapter_07.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: 26/02/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 <cstdio>
|
||||
|
||||
#include <raytracing.h>
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
using namespace Raytracer;
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
World the_world;
|
||||
Camera the_camera;
|
||||
Canvas the_canvas;
|
||||
Sphere *the_floor, *the_left_wall, *the_right_wall;
|
||||
Sphere *the_middle, *the_right, *the_left;
|
||||
printf("Chapter 07 example.\n");
|
||||
|
||||
// Floor is an extremely flattened sphere with a matte texture.
|
||||
the_floor = new Sphere();
|
||||
the_floor->set_transform(Matrix::scaling(10, 0.01, 10));
|
||||
the_floor->material().set_color(Color(1, 0.9, 0.9));
|
||||
the_floor->material().set_specular(0);
|
||||
the_world.add_object(the_floor);
|
||||
|
||||
// The Wall on the left has the same scale and color as the floor,
|
||||
// But is also rotated and translated into place.
|
||||
the_left_wall = new Sphere();
|
||||
the_left_wall->set_transform(Matrix::translation(0, 0, 5) * Matrix::rotation_y(-std::numbers::pi / 4) *
|
||||
Matrix::rotation_x(std::numbers::pi / 2) * Matrix::scaling(10, 0.01, 10));
|
||||
the_left_wall->set_material(the_floor->material());
|
||||
the_world.add_object(the_left_wall);
|
||||
|
||||
// The Wall on the right is identical to the left wall, but is rotated the opposite direction in y.
|
||||
the_right_wall = new Sphere();
|
||||
the_right_wall->set_transform(Matrix::translation(0, 0, 5) * Matrix::rotation_y(std::numbers::pi / 4) *
|
||||
Matrix::rotation_x(std::numbers::pi / 2) * Matrix::scaling(10, 0.01, 10));
|
||||
the_right_wall->set_material(the_floor->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.5, 1, 0.5));
|
||||
the_middle->material().set_color(Color(0.1, 1, 0.5));
|
||||
the_middle->material().set_diffuse(0.7);
|
||||
the_middle->material().set_specular(0.3);
|
||||
the_world.add_object(the_middle);
|
||||
|
||||
// The smaller green sphere on the right is scaled in half
|
||||
the_right = new Sphere();
|
||||
the_right->set_transform(Matrix::translation(1.5, 0.5, -0.5) * Matrix::scaling(0.5, 0.5, 0.5));
|
||||
the_right->material().set_color(Color(0.5, 1, 0.1));
|
||||
the_right->material().set_diffuse(0.7);
|
||||
the_right->material().set_specular(0.3);
|
||||
the_world.add_object(the_right);
|
||||
|
||||
// The smallest sphere is scaled by a third, before being translated
|
||||
the_left = new Sphere();
|
||||
the_left->set_transform(Matrix::translation(-1.5, 0.33, -0.75) * Matrix::scaling(0.33, 0.33, 0.33));
|
||||
the_left->material().set_color(Color(1, 0.8, 0.1));
|
||||
the_left->material().set_diffuse(0.7);
|
||||
the_left->material().set_specular(0.3);
|
||||
the_world.add_object(the_left);
|
||||
|
||||
// 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 / 2);
|
||||
the_camera = Camera(320, 200, std::numbers::pi / 2);
|
||||
the_camera.set_transform(
|
||||
Matrix::view_transform(Tuple::Point(0, 1.5, -5), Tuple::Point(0, 1, 0), Tuple::Vector(0, 1, 0)));
|
||||
|
||||
the_canvas = the_camera.render(the_world);
|
||||
|
||||
the_canvas.save_to_file("chapter07.ppm");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user