From d3fe032c8ca1e7a6e7f34d81c48a525ebf50c2e0 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 14 Feb 2024 16:44:13 +0100 Subject: [PATCH] [FEAT] Finish the Chapter 05 with the render of a sphere shadow. --- Doc.md | 6 +++ README.md | 18 +++++-- apps/CMakeLists.txt | 7 ++- apps/chapter_05.cpp | 91 ++++++++++++++++++++++++++++++++ apps/main.cpp | 40 -------------- data/chapter_05.png | Bin 0 -> 776 bytes raytracing/include/raytracing.h | 1 + raytracing/src/canvas.cpp | 32 ++++++++++- raytracing/src/canvas.h | 1 + raytracing/src/intersection.cpp | 9 +++- raytracing/src/intersection.h | 1 + tests/05_rays.cpp | 4 ++ 12 files changed, 160 insertions(+), 50 deletions(-) create mode 100644 Doc.md create mode 100644 apps/chapter_05.cpp delete mode 100644 apps/main.cpp create mode 100644 data/chapter_05.png diff --git a/Doc.md b/Doc.md new file mode 100644 index 0000000..c850c22 --- /dev/null +++ b/Doc.md @@ -0,0 +1,6 @@ +## Le Livre est ici: + +https://lire.amazon.fr/?asin=B07Q84TQ91&ref_=kwl_kr_iv_rec_1 + + +https://github.com/sraaphorst/raytracer-cpp/blob/b57794ce9f4ed80d782711adb9131f3e3097d4b0/CMakeLists.txt diff --git a/README.md b/README.md index 0b3bfc1..26b7594 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,22 @@ +# Ray Tracer Challenge in C++ +**Current status:** In progress [![Build Status](https://drone.nadal-fr.com/api/badges/raytracing/raytracing_challenge/status.svg)](https://drone.nadal-fr.com/raytracing/raytracing_challenge) -## Le Livre est ici: +## Presentation -https://lire.amazon.fr/?asin=B07Q84TQ91&ref_=kwl_kr_iv_rec_1 +Implementation of the book **The Ray Tracer Challenge** by Jamis Buck. + +This Git repository contains my C++ implementation of the Ray Tracer Challenge. + +![book](data/the_raytracing_challenge.png) -https://github.com/sraaphorst/raytracer-cpp/blob/b57794ce9f4ed80d782711adb9131f3e3097d4b0/CMakeLists.txt +### Useful links: + +Ray Tracing: The Next Week book: http://raytracerchallenge.com/ + +# Output +### Chapter 05 +![chapter_05](data/chapter_05.png) diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index faed794..8001080 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -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) diff --git a/apps/chapter_05.cpp b/apps/chapter_05.cpp new file mode 100644 index 0000000..ae5d13c --- /dev/null +++ b/apps/chapter_05.cpp @@ -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 + +#include + +/* ------------------------------------------------------------------------- */ + +#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); +} diff --git a/apps/main.cpp b/apps/main.cpp deleted file mode 100644 index 83310e0..0000000 --- a/apps/main.cpp +++ /dev/null @@ -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 -#include - -/* ------------------------------------------------------------------------- */ - -int main(void) -{ - printf("Later Here we will have main function\n"); - - return 0; -} diff --git a/data/chapter_05.png b/data/chapter_05.png new file mode 100644 index 0000000000000000000000000000000000000000..6ff70ea51b9d888a89bc6322618e3071155d7de9 GIT binary patch literal 776 zcmV+j1NZ!iP)Px%!bwCyRCr$P+R<`@FbqUd^8X**4(-rp(h}LSBro{pwFZ+tY%tKrwrw99ADILm zfXG;U9S9&=9)O5>00M|w0%FI*H3Se}0ugKT{p?f!$TlPbzzSaxND8C^;S5OyM*?If z#4FHjn5iI25GpWJK$Ib7!<2$t31SvZ8OW6}4xEbXOFGCMAS&CE3UWt`?6#zV+!-Si zB?V*!j4YIJkd-hpP(ndg#E3=-16dg(5+wu#jNgUv=)KA?2pE5#ACI66fq-%T#>I;Q zi1ilB`S)@V+F?t1P+ScG!^ZHgyZ~v>)-|%XCEXyjqu-?m1(5!HySGRKp&i{8KO3~7 zbTtGJArLV97(FYl=7sj#AEDx1SBU{_g(>UN0+nV5C}h8u_(j;xd;d#u?dLnEuT z;wR#IqX_Vt$z?!BFt4;NAYxj803w!v*x}F+K*SOdJDg84@VfyjM095W0000 +#include + #include "canvas.h" #include "common.h" @@ -91,7 +94,7 @@ std::string Canvas::to_ppm(void) // Header the_result = "P3\n"; - the_result += std::to_string(m_width) + " " + std::to_string(m_height) + "\n"; + the_result += std::to_string(m_width) + " " + std::to_string(m_height) + '\n'; the_result += "255\n"; for (int j = 0; j < m_height; j++) @@ -122,7 +125,7 @@ bool Canvas::add_color_component(std::string &a_str_value, uint32_t &a_col_numbe if ((the_color_value.length() + 1 + a_col_number) >= 70) { - a_str_value += "\n"; + a_str_value += '\n'; a_col_number = 0; } @@ -137,3 +140,28 @@ bool Canvas::add_color_component(std::string &a_str_value, uint32_t &a_col_numbe return true; } + +/* ------------------------------------------------------------------------- */ + +bool Canvas::save_to_file(const std::string &a_filename) +{ + std::string the_data; + + int the_fd = open(a_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (the_fd == -1) + { + return false; + } + + the_data = to_ppm(); + + size_t the_written = write(the_fd, the_data.data(), the_data.size()); + if (the_written != the_data.size()) + { + close(the_fd); + return false; + } + + close(the_fd); + return true; +} \ No newline at end of file diff --git a/raytracing/src/canvas.h b/raytracing/src/canvas.h index 84791b0..9bb18cd 100644 --- a/raytracing/src/canvas.h +++ b/raytracing/src/canvas.h @@ -52,6 +52,7 @@ namespace Raytracer bool write_pixel(uint16_t a_pos_x, uint16_t a_pos_y, const Color &a_color); std::string to_ppm(void); + bool save_to_file(const std::string &a_filename); private: bool add_color_component(std::string &a_str_value, uint32_t &a_col_number, uint8_t a_color); diff --git a/raytracing/src/intersection.cpp b/raytracing/src/intersection.cpp index feae686..bdf223c 100644 --- a/raytracing/src/intersection.cpp +++ b/raytracing/src/intersection.cpp @@ -43,7 +43,7 @@ Intersection::Intersection(void) : m_is_nothing(true), m_distance_t(0.0), m_obje /* ------------------------------------------------------------------------- */ Intersection::Intersection(double a_distance_t, const Object &an_object) : - m_is_nothing(true), + m_is_nothing(false), m_distance_t(a_distance_t), m_object(an_object) { @@ -152,3 +152,10 @@ bool Intersection::is_nothing(void) { return m_is_nothing; } + +/* ------------------------------------------------------------------------- */ + +bool Intersection::is_defined(void) +{ + return !m_is_nothing; +} diff --git a/raytracing/src/intersection.h b/raytracing/src/intersection.h index 3521624..8f56295 100644 --- a/raytracing/src/intersection.h +++ b/raytracing/src/intersection.h @@ -57,6 +57,7 @@ namespace Raytracer double distance_t(void) const; const Object &object(void) const; bool is_nothing(void); + bool is_defined(void); private: bool m_is_nothing; diff --git a/tests/05_rays.cpp b/tests/05_rays.cpp index eeb6f55..cc059ab 100644 --- a/tests/05_rays.cpp +++ b/tests/05_rays.cpp @@ -459,6 +459,10 @@ SCENARIO("The hit is always the lowest nonnegative intersection", "[features/int { REQUIRE(i == i4); } + AND_THEN("i is defined") + { + REQUIRE(i.is_defined()); + } } } }