[FEAT] Finish the Chapter 11

This commit is contained in:
NADAL Jean-Baptiste
2024-03-13 13:23:36 +01:00
parent 0da549d969
commit d2fe968717
23 changed files with 506 additions and 58 deletions

View File

@@ -105,7 +105,8 @@
"/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_07.dir", "/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_07.dir",
"/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_09.dir", "/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_09.dir",
"/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_10.dir", "/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_10.dir",
"/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_11.dir", "/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_11_p1.dir",
"/home/jbnadal/sources/jb/raytracer_challenge/build/apps/CMakeFiles/chapter_11_p2.dir",
"/home/jbnadal/sources/jb/raytracer_challenge/build/tests/CMakeFiles/raytracing_test.dir" "/home/jbnadal/sources/jb/raytracer_challenge/build/tests/CMakeFiles/raytracing_test.dir"
] ]
} }

View File

@@ -29,4 +29,4 @@ The Web Site of the book: http://raytracerchallenge.com/
| Chapiter 10 | Chapiter 11 partie 01 | Chapiter 11 partie 02 | | Chapiter 10 | Chapiter 11 partie 01 | Chapiter 11 partie 02 |
|:------------------------: | :---------------------------: | :----------------------------: | |:------------------------: | :---------------------------: | :----------------------------: |
|![10](data/chapter_10.png) | ![11](data/chapter_11_p1.png) | | |![10](data/chapter_10.png) | ![11](data/chapter_11_p1.png) | ![11](data/chapter_11_p2.png) |

View File

@@ -26,4 +26,4 @@ add_executable(chapter_11_p1 chapter_11_p1.cpp)
target_link_libraries(chapter_11_p1 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) target_link_libraries(chapter_11_p1 PRIVATE raytracing gcov OpenMP::OpenMP_CXX)
add_executable(chapter_11_p2 chapter_11_p2.cpp) add_executable(chapter_11_p2 chapter_11_p2.cpp)
target_link_libraries(chapter_11_p2 PRIVATE raytracing gcov OpenMP::OpenMP_CXX) target_link_libraries(chapter_11_p2 PRIVATE raytracing OpenMP::OpenMP_CXX)

View File

@@ -43,70 +43,158 @@ int main(void)
World the_world; World the_world;
Camera the_camera; Camera the_camera;
Canvas the_canvas; Canvas the_canvas;
Plane *the_floor, *the_left_wall, *the_right_wall; Plane *the_floor, *the_ceiling, *the_left_wall, *the_right_wall, *the_front_wall, *the_back_wall;
Sphere *the_middle, *the_right, *the_left; Sphere *the_back_sphere1, *the_back_sphere2, *the_back_sphere3, *the_back_sphere4;
Sphere *the_red_sphere, *the_green_sphere, *the_blue_sphere;
chrono::time_point<chrono::high_resolution_clock> the_start, the_end; chrono::time_point<chrono::high_resolution_clock> the_start, the_end;
printf("Chapter 11 part2 example.\n"); printf("Chapter 11 part2 example.\n");
// Floor is an extremely flattened sphere with a matte texture. // Wall Material
Material the_wall_material;
the_wall_material.set_pattern(new StripePattern(Color(0.45, 0.45, 0.45), Color(0.35, 0.35, 0.35)));
the_wall_material.pattern()->set_transform(Matrix::rotation_y(std::numbers::pi / 2) *
Matrix::scaling(0.25, 0.25, 0.25));
the_wall_material.set_ambient(0.0);
the_wall_material.set_diffuse(0.4);
the_wall_material.set_specular(0.0);
the_wall_material.set_reflective(0.3);
// Floor
the_floor = new Plane(); the_floor = new Plane();
Material &the_floor_material = the_floor->material(); Material &the_floor_material = the_floor->material();
the_floor_material.set_pattern(new CheckersPattern(Color(0.35, 0.35, 0.35), Color(0.65, 0.65, 0.65)));
the_floor_material.set_specular(0.0); the_floor_material.set_specular(0.0);
the_floor_material.set_reflective(0.4); the_floor_material.set_reflective(0.4);
the_floor_material.set_pattern(new CheckersPattern(Color(0.35, 0.35, 0.35), Color(0.65, 0.65, 0.65))); the_floor->set_transform(Matrix::rotation_y(-std::numbers::pi / 10));
the_world.add_object(the_floor); the_world.add_object(the_floor);
// Ceiling
the_ceiling = new Plane();
Material &the_ceiling_material = the_ceiling->material();
the_ceiling_material.set_color(Color(0.8, 0.8, 0.8));
the_ceiling_material.set_ambient(0.3);
the_ceiling_material.set_specular(0.0);
the_ceiling->set_transform(Matrix::translation(0, 5, 0));
the_world.add_object(the_ceiling);
// Left Wall // Left Wall
the_left_wall = new Plane(); the_left_wall = new Plane();
the_left_wall->set_transform(Matrix::translation(0, 0, 10) * the_left_wall->set_transform(Matrix::translation(-5, 0, 0) *
Matrix::rotation_y(-std::numbers::pi / 4) * Matrix::rotation_z(-std::numbers::pi / 2) *
Matrix::rotation_x(std::numbers::pi / 2)); Matrix::rotation_y(std::numbers::pi / 2));
Material &the_left_wall_material = the_left_wall->material(); the_left_wall->set_material(the_wall_material);
the_left_wall_material.set_specular(0);
the_left_wall_material.set_pattern(new StripePattern(Color(0.52, 0.52, 0.52), Color::Black()));
the_left_wall_material.pattern()->set_transform(Matrix::rotation_y(std::numbers::pi / 2));
the_world.add_object(the_left_wall); the_world.add_object(the_left_wall);
// Right Wall // Right Wall
the_right_wall = new Plane(); the_right_wall = new Plane();
the_right_wall->set_transform(Matrix::translation(0, 5, 10) * the_right_wall->set_transform(Matrix::translation(5, 0, 0) *
Matrix::rotation_y(std::numbers::pi / 4) * Matrix::rotation_z(-std::numbers::pi / 2) *
Matrix::rotation_x(std::numbers::pi / 2)); Matrix::rotation_y(std::numbers::pi / 2));
Material &the_right_wall_material = the_right_wall->material(); the_right_wall->set_material(the_wall_material);
the_right_wall_material.set_specular(0);
the_right_wall_material.set_pattern(new StripePattern(Color(0.52, 0.52, 0.52), Color::Black()));
the_right_wall_material.pattern()->set_transform(Matrix::rotation_y(std::numbers::pi / 2));
the_world.add_object(the_right_wall); the_world.add_object(the_right_wall);
// The large sphere in the middle is a unit sphere, translated upward slightly and colored green. // Front Wall
the_middle = new Sphere(); the_front_wall = new Plane();
the_middle->set_transform(Matrix::translation(-0.25, 1, 1.5) * the_front_wall->set_transform(Matrix::translation(0, 0, 5) *
Matrix::rotation_y(-std::numbers::pi / 1.5) * Matrix::rotation_x(-std::numbers::pi / 2));
Matrix::rotation_z(-std::numbers::pi / 6)); the_front_wall->set_material(the_wall_material);
the_world.add_object(the_front_wall);
Material &the_middle_material = the_middle->material(); // Back Wall
the_middle_material.set_color(Color(0, 0.2, 0.)); the_back_wall = new Plane();
the_middle_material.set_ambient(0.0); the_back_wall->set_transform(Matrix::translation(0, 0, -5) *
the_middle_material.set_diffuse(0.4); Matrix::rotation_x(-std::numbers::pi / 2));
the_middle_material.set_specular(0.9); the_back_wall->set_material(the_wall_material);
the_middle_material.set_shininess(300); the_world.add_object(the_back_wall);
the_middle_material.set_transparency(0.9);
the_middle_material.set_reflective(0.9); // Back Sphere 1
the_middle_material.set_refractive_index(1.5); the_back_sphere1 = new Sphere();
the_world.add_object(the_middle); the_back_sphere1->set_transform(Matrix::translation(4.6, 0.4, 1.0) *
Matrix::scaling(0.4, 0.4, 0.4));
Material &the_back_sphere1_material = the_back_sphere1->material();
the_back_sphere1_material.set_color(Color(0.8, 0.5, 0.3));
the_back_sphere1_material.set_shininess(50.0);
the_world.add_object(the_back_sphere1);
// Back Sphere 2
the_back_sphere2 = new Sphere();
the_back_sphere2->set_transform(Matrix::translation(4.7, 0.3, 0.4) *
Matrix::scaling(0.3, 0.3, 0.3));
Material &the_back_sphere2_material = the_back_sphere2->material();
the_back_sphere2_material.set_color(Color(0.9, 0.4, 0.5));
the_back_sphere2_material.set_shininess(50.0);
the_world.add_object(the_back_sphere2);
// Back Sphere 3
the_back_sphere3 = new Sphere();
the_back_sphere3->set_transform(Matrix::translation(-1, 0.5, 4.5) *
Matrix::scaling(0.5, 0.5, 0.5));
Material &the_back_sphere3_material = the_back_sphere3->material();
the_back_sphere3_material.set_color(Color(0.4, 0.9, 0.6));
the_back_sphere3_material.set_shininess(50.0);
the_world.add_object(the_back_sphere3);
// Back Sphere 4
the_back_sphere4 = new Sphere();
the_back_sphere4->set_transform(Matrix::translation(-1.7, 0.3, 4.7) *
Matrix::scaling(0.3, 0.3, 0.3));
Material &the_back_sphere4_material = the_back_sphere4->material();
the_back_sphere4_material.set_color(Color(0.4, 0.6, 0.9));
the_back_sphere4_material.set_shininess(50.0);
the_world.add_object(the_back_sphere4);
// Red Sphere
the_red_sphere = new Sphere();
the_red_sphere->set_transform(Matrix::translation(-0.6, 1, 0.6));
Material &the_red_sphere_material = the_red_sphere->material();
the_red_sphere_material.set_color(Color(1, 0.3, 0.2));
the_red_sphere_material.set_specular(0.4);
the_red_sphere_material.set_shininess(5.0);
the_world.add_object(the_red_sphere);
// Green Sphere
the_green_sphere = new Sphere();
the_green_sphere->set_transform(Matrix::translation(-0.7, 0.5, -0.8) *
Matrix::scaling(0.5, 0.5, 0.5));
Material &the_green_sphere_material = the_green_sphere->material();
the_green_sphere_material.set_color(Color(0, 0.2, 0));
the_green_sphere_material.set_ambient(0.0);
the_green_sphere_material.set_diffuse(0.4);
the_green_sphere_material.set_specular(0.9);
the_green_sphere_material.set_shininess(300.0);
the_green_sphere_material.set_reflective(0.9);
the_green_sphere_material.set_transparency(0.9);
the_green_sphere_material.set_refractive_index(1.5);
the_world.add_object(the_green_sphere);
// Blue Sphere
the_blue_sphere = new Sphere();
the_blue_sphere->set_transform(Matrix::translation(0.6, 0.7, -0.6) *
Matrix::scaling(0.7, 0.7, 0.7));
Material &the_blue_sphere_material = the_blue_sphere->material();
the_blue_sphere_material.set_color(Color(0, 0., 0.2));
the_blue_sphere_material.set_ambient(0.0);
the_blue_sphere_material.set_diffuse(0.4);
the_blue_sphere_material.set_specular(0.9);
the_blue_sphere_material.set_shininess(300.0);
the_blue_sphere_material.set_reflective(0.9);
the_blue_sphere_material.set_transparency(0.9);
the_blue_sphere_material.set_refractive_index(1.5);
the_world.add_object(the_blue_sphere);
// The Light source is white, shining from above and to 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))); the_world.set_light(PointLight(Tuple::Point(-4.9, 4.9, -1), Color(1, 1, 1)));
// Configure the camera. // Configure the camera.
the_camera = Camera(100, 50, std::numbers::pi / 3); // the_camera = Camera(100, 50, 1.152);
// the_camera = Camera(320, 200, std::numbers::pi / 3); // the_camera = Camera(320, 200, 1.152);
// the_camera = Camera(640, 480, std::numbers::pi / 3); the_camera = Camera(640, 480, 1.152);
the_camera.set_transform( the_camera.set_transform(
Matrix::view_transform(Tuple::Point(0, 1.5, -5), Tuple::Point(0, 1, 0), Tuple::Vector(0, 1, 0))); Matrix::view_transform(Tuple::Point(-2.6, 1.5, -3.9), Tuple::Point(-0.6, 1, -0.8), Tuple::Vector(0, 1, 0)));
the_start = chrono::high_resolution_clock::now(); the_start = chrono::high_resolution_clock::now();
the_camera.show_progress_bar();
the_canvas = the_camera.render(the_world); the_canvas = the_camera.render(the_world);
the_end = chrono::high_resolution_clock::now(); the_end = chrono::high_resolution_clock::now();

BIN
data/chapter_11_p2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

View File

@@ -5,7 +5,7 @@ project(raytracing)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(--coverage) # add_definitions(--coverage)
add_definitions(-fopenmp) add_definitions(-fopenmp)
include_directories (src) include_directories (src)

View File

@@ -0,0 +1,327 @@
#pragma once
#ifndef PROGRESSBAR_HPP__
#define PROGRESSBAR_HPP__
/*****************************************************************************
ProgressBar.hpp: CUI Progress bar
******************************************************************************
ProgressBar.hpp is under MIT license
----------------------------------
Copyright (c) 2023 Kitanokitsune
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include <stdio.h> /* <cstdio> causes errors to BCC 5.5 */
#include <limits.h>
#include <string>
#include <iomanip>
#ifdef _WIN32
# include <windows.h>
# ifndef COMMON_LVB_GRID_HORIZONTAL
# define COMMON_LVB_GRID_HORIZONTAL 0x0400
# endif
# ifndef COMMON_LVB_UNDERSCORE
# define COMMON_LVB_UNDERSCORE 0x8000
# endif
#else
# include <sys/ioctl.h>
# include <unistd.h>
#endif
#define MAX_BAR_LENGTH 256
#define DEFAULT_CONSOLE_WIDTH 80
#define pbar_max(a,b) ((a)>(b) ? (a) : (b))
#define pbar_round(x) static_cast<int>(x + 0.5) /* NOTE: x >= 0.0 */
#define pbar_floor(x) static_cast<int>(x) /* NOTE: x >= 0.0 */
#define MAXDIGITS(x) ((int)((sizeof(x)*CHAR_BIT*1233UL)>>12)+1) /* NOTE: 680 >= sizeof(x)*CHAR_BIT */
#define MAXDIGITS_ULONG MAXDIGITS(unsigned long)
class ProgressBar {
private:
double percent;
double step;
double tick;
double checkpoint;
unsigned long total;
unsigned long state;
int consolewidth;
char charBarOn;
char charBarOff;
char charBarArrow;
char charBracketLeft;
char charBracketRight;
std::string bartitle;
std::string strtotal;
bool unknown_console;
bool num_style_is_percent;
#ifdef _WIN32
CONSOLE_CURSOR_INFO cursor_info;
HANDLE hStdoutHandle;
DWORD consoleattribute;
#endif
private:
#ifdef _WIN32
inline void invert_attribute(void) {
SetConsoleTextAttribute(hStdoutHandle,
// COMMON_LVB_UNDERSCORE |
COMMON_LVB_GRID_HORIZONTAL |
((consoleattribute>>4) & 0x0fU) |
((consoleattribute<<4) & 0xf0U));
};
inline void restore_attribute(void) {
SetConsoleTextAttribute(hStdoutHandle, consoleattribute);
};
int get_consolewidth(void) {
BOOL bRet;
CONSOLE_SCREEN_BUFFER_INFO info;
bRet = GetConsoleScreenBufferInfo(
hStdoutHandle,
&info);
if (bRet) {
return (1 + info.srWindow.Right);
}
return DEFAULT_CONSOLE_WIDTH;
};
#else
int get_consolewidth(void) {
struct winsize ws;
if( ioctl( STDERR_FILENO, TIOCGWINSZ, &ws ) != -1 ) {
if( 0 < ws.ws_col && ws.ws_col == (size_t)ws.ws_col ) {
return ws.ws_col;
}
}
return DEFAULT_CONSOLE_WIDTH;
};
#endif
void redraw(void) {
int bar_len;
double f;
char *ptr;
int i;
int count;
char buf[MAX_BAR_LENGTH + 1]; /* bar length + NUL('\0') */
char str[1 + MAXDIGITS_ULONG + 1]; /* sign + digits + NUL */
char fmt[1 + MAXDIGITS_ULONG + 1];
/* calculate bar length */
bar_len = consolewidth - bartitle.size();
if (num_style_is_percent) {
bar_len -= 8;
} else {
bar_len -= (5 + 2*strtotal.size());
}
if (bar_len < 1) {
bar_len = 1;
} else if (bar_len > MAX_BAR_LENGTH) {
bar_len = MAX_BAR_LENGTH;
}
/* print progress bar */
f = percent * 0.01 * static_cast<double>(bar_len);
count = pbar_round(f);
fprintf(stderr, "\r%s%c", bartitle.c_str(), charBracketLeft);
ptr = buf;
if (unknown_console) {
for (i=0; i<count; ++i) *(ptr++) = '#';
*ptr = '\0';
fprintf(stderr, "%s", buf);
} else if (charBarOn == '\0') {
for (i=0; i<count; ++i) *(ptr++) = ' ';
*ptr = '\0';
#ifdef _WIN32
invert_attribute();
fprintf(stderr, "%s", buf);
restore_attribute();
#else
fprintf(stderr, "\x1b[7m%s\x1b[m", buf);
#endif
} else {
for (i=0; i<count; ++i) *(ptr++) = charBarOn;
*ptr = '\0';
fprintf(stderr, "%s", buf);
}
ptr = buf;
if (i<bar_len) *(ptr++) = charBarArrow;
if (charBarOff != '\0') for (i=count+1; i<bar_len; ++i) *(ptr++) = charBarOff;
*ptr = '\0';
if (num_style_is_percent) {
fprintf(stderr, "%s%c %3d%%", buf, charBracketRight, pbar_floor(percent));
} else {
sprintf(fmt, "%%%ulu", static_cast<unsigned int>(strtotal.size()));
sprintf(str, fmt, state);
fprintf(stderr, "%s%c %s/%s", buf, charBracketRight, str, strtotal.c_str());
}
};
void init_(void) {
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO info;
hStdoutHandle = GetStdHandle(STD_ERROR_HANDLE);
if (GetConsoleScreenBufferInfo(hStdoutHandle, &info)) {
consoleattribute = info.wAttributes;
GetConsoleCursorInfo(hStdoutHandle, &cursor_info);
unknown_console = false;
} else {
unknown_console = true;
}
#else
struct winsize ws;
unknown_console = true;
if( ioctl( STDERR_FILENO, TIOCGWINSZ, &ws ) != -1 ) {
if( 0 < ws.ws_col && ws.ws_col == (size_t)ws.ws_col ) {
unknown_console = false;
}
}
#endif
};
public:
ProgressBar(unsigned long t=1) {
init_();
set_total(t);
set_barstyle();
set_width();
set_title("");
set_bracketstyle();
use_percentstyle();
};
ProgressBar(unsigned long t, std::string str, char fgch='\0', char arrch='.', char bgch='.', int wid=-1) {
init_();
set_total(t);
set_barstyle(fgch, arrch, bgch);
set_width(wid);
set_title(str);
set_bracketstyle();
use_percentstyle();
};
void set_barstyle(char fg='\0', char arrw='.', char bg='.') {
charBarOn = fg;
charBarArrow = arrw;
charBarOff = bg;
};
void set_total(unsigned long t) {
char str[1 + MAXDIGITS_ULONG + 1];
total = pbar_max(t, 1);
sprintf(str, "%lu", total);
strtotal = str;
step = 100.0/static_cast<double>(total);
tick = pbar_max(1.0, step);
checkpoint = tick;
percent = 0.0;
};
void set_width(int width = -1) {
if (width > 0) {
consolewidth = width;
} else {
consolewidth = get_consolewidth();
}
};
void set_title(std::string str) {
bartitle = str;
};
void set_bracketstyle(char lbra='|', char rbra='|') {
charBracketLeft = lbra;
charBracketRight = rbra;
};
void use_percentstyle(void) {
num_style_is_percent = true;
};
void use_ratiostyle(void) {
num_style_is_percent = false;
};
void reset(void) {
percent = 0.0;
state = 0;
checkpoint = tick;
};
void start(void) {
reset();
set_cursor_visible(false);
redraw();
};
void set_cursor_visible(bool onoff) {
#ifdef _WIN32
cursor_info.bVisible = onoff;
SetConsoleCursorInfo(hStdoutHandle, &cursor_info);
#else
if (onoff) {
fprintf(stderr, "\e[?25h");
} else {
fprintf(stderr, "\e[?25l");
}
#endif
};
void update(void) {
if (state >= total) return;
++state;
percent += step;
if (state >= total) {
percent = 100.03125;
redraw();
fprintf(stderr, "\n");
set_cursor_visible(true);
fflush(stderr);
} else if (percent >= checkpoint) {
redraw();
checkpoint = percent + tick;
}
};
inline void operator ++() {
update();
};
inline void operator ++(int n) {
update();
};
};
#endif /* PROGRESSBAR_HPP__ */

View File

@@ -31,6 +31,8 @@
#include <cmath> #include <cmath>
#include <omp.h> #include <omp.h>
#include <external/ProgressBar.hpp>
#include "camera.h" #include "camera.h"
using namespace Raytracer; using namespace Raytracer;
@@ -43,7 +45,8 @@ Camera::Camera(void) :
m_field_of_view(0), m_field_of_view(0),
m_half_width(0), m_half_width(0),
m_half_height(0), m_half_height(0),
m_pixel_size(0) m_pixel_size(0),
m_show_progress_bar(false)
{ {
} }
@@ -56,7 +59,8 @@ Camera::Camera(uint16_t a_h_size, uint16_t a_v_size, double a_field_of_view) :
m_transform(Matrix::identity()), m_transform(Matrix::identity()),
m_half_width(0), m_half_width(0),
m_half_height(0), m_half_height(0),
m_pixel_size(0) m_pixel_size(0),
m_show_progress_bar(false)
{ {
double the_half_view = tan(m_field_of_view / 2); double the_half_view = tan(m_field_of_view / 2);
@@ -87,6 +91,7 @@ Camera::Camera(const Camera &an_other)
m_half_width = an_other.m_half_width; m_half_width = an_other.m_half_width;
m_half_height = an_other.m_half_height; m_half_height = an_other.m_half_height;
m_pixel_size = an_other.m_pixel_size; m_pixel_size = an_other.m_pixel_size;
m_show_progress_bar = an_other.m_show_progress_bar;
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@@ -105,6 +110,7 @@ const Camera &Camera::operator=(const Camera &an_other)
m_half_width = an_other.m_half_width; m_half_width = an_other.m_half_width;
m_half_height = an_other.m_half_height; m_half_height = an_other.m_half_height;
m_pixel_size = an_other.m_pixel_size; m_pixel_size = an_other.m_pixel_size;
m_show_progress_bar = an_other.m_show_progress_bar;
return *this; return *this;
} }
@@ -183,19 +189,42 @@ Ray Camera::ray_for_pixel(double an_x, double an_y) const
Canvas Camera::render(const World &a_world) Canvas Camera::render(const World &a_world)
{ {
int8_t the_percent = -1;
uint32_t the_cpt = 0;
uint32_t the_loop_size = m_h_size * m_v_size;
Canvas the_image(m_h_size, m_v_size); Canvas the_image(m_h_size, m_v_size);
ProgressBar the_progress_bar(100, "[=>]");
#pragma omp parallel for num_threads(8) shared(the_image)
for (int y = 0; y < m_v_size - 1; y++) for (int y = 0; y < m_v_size - 1; y++)
{ {
#pragma omp parallel for shared(the_image)
for (int x = 0; x < m_h_size - 1; x++) for (int x = 0; x < m_h_size - 1; x++)
{ {
// printf("ray_for_pixel (process: %d)\n", omp_get_thread_num()); // printf("ray_for_pixel (process: %d)\n", omp_get_thread_num());
Ray the_ray = ray_for_pixel(x, y); Ray the_ray = ray_for_pixel(x, y);
Color the_color = a_world.color_at(the_ray); Color the_color = a_world.color_at(the_ray);
the_image.write_pixel(x, y, the_color); the_image.write_pixel(x, y, the_color);
if (m_show_progress_bar == true)
{
double the_temp_perc;
the_cpt++;
the_temp_perc = the_cpt * 100 / the_loop_size;
if (the_percent != (int8_t)the_temp_perc)
{
the_percent = (int8_t)the_temp_perc;
// printf("percent: %d\n", the_percent);
the_progress_bar.update();
}
}
} }
} }
return the_image; return the_image;
} }
/* ------------------------------------------------------------------------- */
void Camera::show_progress_bar(void)
{
m_show_progress_bar = true;
}

View File

@@ -61,6 +61,8 @@ namespace Raytracer
Canvas render(const World &a_world); Canvas render(const World &a_world);
void show_progress_bar(void);
private: private:
uint16_t m_h_size; uint16_t m_h_size;
@@ -70,6 +72,7 @@ namespace Raytracer
double m_half_width; double m_half_width;
double m_half_height; double m_half_height;
double m_pixel_size; double m_pixel_size;
bool m_show_progress_bar;
}; };
}; // namespace Raytracer }; // namespace Raytracer

View File

@@ -27,7 +27,7 @@
#include <cmath> #include <cmath>
#include <catch.hpp> #include <external/catch.hpp>
#include "core/tuple.h" #include "core/tuple.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "core/color.h" #include "core/color.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "renderer/canvas.h" #include "renderer/canvas.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -25,7 +25,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include <catch.hpp> #include <external/catch.hpp>
#include "raytracing.h" #include "raytracing.h"

View File

@@ -26,4 +26,4 @@
#define CATCH_INTERNAL_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS #define CATCH_INTERNAL_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS
#define CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS #define CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include "catch.hpp" #include "external/catch.hpp"