98 lines
3.4 KiB
C++
98 lines
3.4 KiB
C++
/*!
|
|
* chapter_06.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: 20/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>
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
#define kImageSize 100
|
|
#define kWallSize 7.0
|
|
#define kWallZ 10
|
|
|
|
using namespace Raytracer;
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
int shadow_sphere(uint8_t a_canvas_pixels, double a_wall_size, uint8_t a_wall_z)
|
|
{
|
|
Canvas the_canvas(a_canvas_pixels, a_canvas_pixels);
|
|
Sphere the_shape;
|
|
Tuple the_ray_origin = Tuple::Point(0, 0, -5);
|
|
PointLight the_light = PointLight(Tuple::Point(-10, 10, -10), Color(1, 1, 1));
|
|
|
|
the_shape.material().set_color(Color(1, 0.2, 1));
|
|
|
|
double the_pixel_size = a_wall_size / a_canvas_pixels;
|
|
double the_half = a_wall_size / 2;
|
|
double the_world_x, the_world_y;
|
|
|
|
// For each row of pixels in the canvas
|
|
for (int y = 0; y < a_canvas_pixels - 1; y++)
|
|
{
|
|
// Compute the world y coordinate (top = +half, bottom = -half)
|
|
the_world_y = the_half - the_pixel_size * y;
|
|
|
|
// For each pixel in the row
|
|
for (int x = 0; x < a_canvas_pixels; x++)
|
|
{
|
|
// Compute the world x coordinate (left = -half, right = half)
|
|
the_world_x = -the_half + the_pixel_size * x;
|
|
|
|
// Describe the point on the wall that the ray will target
|
|
Tuple the_position = Tuple::Point(the_world_x, the_world_y, a_wall_z);
|
|
the_position -= the_ray_origin;
|
|
Ray the_ray(the_ray_origin, the_position.normalize());
|
|
|
|
auto the_xs = the_shape.intersect(the_ray);
|
|
auto the_intersec = the_xs.hit();
|
|
if (the_intersec.is_defined())
|
|
{
|
|
Tuple the_point = the_ray.position(the_intersec.distance_t());
|
|
Tuple the_normal = the_intersec.object().normal_at(the_point);
|
|
Tuple the_eye = -the_ray.direction();
|
|
Color the_color = the_intersec.object().material().lighting(the_light, the_point, the_eye, the_normal);
|
|
the_canvas.write_pixel(x, y, the_color);
|
|
}
|
|
}
|
|
}
|
|
|
|
the_canvas.save_to_file("chapter06.ppm");
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
int main(void)
|
|
{
|
|
printf("Chapter 06 example.\n");
|
|
return shadow_sphere(kImageSize, kWallSize, kWallZ);
|
|
}
|