[FEAT] Finish the Chapter 05 with the render of a sphere shadow.
This commit is contained in:
6
Doc.md
Normal file
6
Doc.md
Normal file
@@ -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
|
||||||
18
README.md
18
README.md
@@ -1,10 +1,22 @@
|
|||||||
|
# Ray Tracer Challenge in C++
|
||||||
|
|
||||||
|
**Current status:** In progress
|
||||||
|
|
||||||
[](https://drone.nadal-fr.com/raytracing/raytracing_challenge)
|
[](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.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
https://github.com/sraaphorst/raytracer-cpp/blob/b57794ce9f4ed80d782711adb9131f3e3097d4b0/CMakeLists.txt
|
### Useful links:
|
||||||
|
|
||||||
|
Ray Tracing: The Next Week book: http://raytracerchallenge.com/
|
||||||
|
|
||||||
|
# Output
|
||||||
|
### Chapter 05
|
||||||
|

|
||||||
|
|||||||
@@ -5,7 +5,6 @@ project(main)
|
|||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
add_executable(raytracer main.cpp)
|
add_executable(chapter_05 chapter_05.cpp)
|
||||||
# We need hello.h and the hello library
|
|
||||||
target_link_libraries(raytracer
|
target_link_libraries(chapter_05 PRIVATE raytracing)
|
||||||
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;
|
|
||||||
}
|
|
||||||
BIN
data/chapter_05.png
Normal file
BIN
data/chapter_05.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 776 B |
@@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "canvas.h"
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "intersection.h"
|
#include "intersection.h"
|
||||||
|
|||||||
@@ -28,6 +28,9 @@
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "canvas.h"
|
#include "canvas.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
@@ -91,7 +94,7 @@ std::string Canvas::to_ppm(void)
|
|||||||
|
|
||||||
// Header
|
// Header
|
||||||
the_result = "P3\n";
|
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";
|
the_result += "255\n";
|
||||||
|
|
||||||
for (int j = 0; j < m_height; j++)
|
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)
|
if ((the_color_value.length() + 1 + a_col_number) >= 70)
|
||||||
{
|
{
|
||||||
a_str_value += "\n";
|
a_str_value += '\n';
|
||||||
a_col_number = 0;
|
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;
|
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;
|
||||||
|
}
|
||||||
@@ -52,6 +52,7 @@ namespace Raytracer
|
|||||||
bool write_pixel(uint16_t a_pos_x, uint16_t a_pos_y, const Color &a_color);
|
bool write_pixel(uint16_t a_pos_x, uint16_t a_pos_y, const Color &a_color);
|
||||||
|
|
||||||
std::string to_ppm(void);
|
std::string to_ppm(void);
|
||||||
|
bool save_to_file(const std::string &a_filename);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool add_color_component(std::string &a_str_value, uint32_t &a_col_number, uint8_t a_color);
|
bool add_color_component(std::string &a_str_value, uint32_t &a_col_number, uint8_t a_color);
|
||||||
|
|||||||
@@ -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) :
|
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_distance_t(a_distance_t),
|
||||||
m_object(an_object)
|
m_object(an_object)
|
||||||
{
|
{
|
||||||
@@ -152,3 +152,10 @@ bool Intersection::is_nothing(void)
|
|||||||
{
|
{
|
||||||
return m_is_nothing;
|
return m_is_nothing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
bool Intersection::is_defined(void)
|
||||||
|
{
|
||||||
|
return !m_is_nothing;
|
||||||
|
}
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ namespace Raytracer
|
|||||||
double distance_t(void) const;
|
double distance_t(void) const;
|
||||||
const Object &object(void) const;
|
const Object &object(void) const;
|
||||||
bool is_nothing(void);
|
bool is_nothing(void);
|
||||||
|
bool is_defined(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_is_nothing;
|
bool m_is_nothing;
|
||||||
|
|||||||
@@ -459,6 +459,10 @@ SCENARIO("The hit is always the lowest nonnegative intersection", "[features/int
|
|||||||
{
|
{
|
||||||
REQUIRE(i == i4);
|
REQUIRE(i == i4);
|
||||||
}
|
}
|
||||||
|
AND_THEN("i is defined")
|
||||||
|
{
|
||||||
|
REQUIRE(i.is_defined());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user