[FEAT] Finish the Chapter 05 with the render of a sphere shadow.
This commit is contained in:
@@ -5,7 +5,6 @@ project(main)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_executable(raytracer main.cpp)
|
||||
# We need hello.h and the hello library
|
||||
target_link_libraries(raytracer
|
||||
PRIVATE raytracing)
|
||||
add_executable(chapter_05 chapter_05.cpp)
|
||||
|
||||
target_link_libraries(chapter_05 PRIVATE raytracing)
|
||||
|
||||
91
apps/chapter_05.cpp
Normal file
91
apps/chapter_05.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
/*!
|
||||
* chapter_05.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: 14/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);
|
||||
Color the_color = Color(1, 0, 0); // Red
|
||||
Sphere the_shape;
|
||||
Tuple the_ray_origin = Tuple::Point(0, 0, -5);
|
||||
|
||||
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())
|
||||
{
|
||||
the_canvas.write_pixel(x, y, the_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
the_canvas.save_to_file("chapter05.ppm");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Chapter 05 example.\n");
|
||||
return shadow_sphere(kImageSize, kWallSize, kWallZ);
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*!
|
||||
* main.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: 30/01/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 <raytracing.h>
|
||||
#include <cstdio>
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Later Here we will have main function\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user