Skip to content

Commit

Permalink
Improve & Update progress struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ilim0t committed Oct 24, 2018
1 parent 607d4b6 commit 824c6f1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
57 changes: 35 additions & 22 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,35 @@
#include "camera.h"

struct Progress {
int num;
int num = 0;
int total;
std::chrono::system_clock::time_point start;
double delta_t = 0;
double previous_elapsed_time = 0;
int count = 0;
int unit;

Progress(int total) : num(0), total(total) , start(std::chrono::system_clock::now()){}
Progress(int total) : total(total), start(std::chrono::system_clock::now()), previous_elapsed_time(elapsed()),
unit(total / 400) {}

void show() {
double percent = (double)num / total * 100;
delta_t += 1. / std::max(elapsed(), 1.) * (elapsed() / std::max(num, 1) - delta_t);
double elapsed_time = elapsed();
double percent = (double) num / total * 100;

double recently_delta_t = num ? (elapsed_time - previous_elapsed_time) / (num - unit * count) : 0;
if (num >= unit * (count + 1)) {
count = num / unit;
previous_elapsed_time = elapsed_time;
}
delta_t += 0.005 / std::max(elapsed_time, 1.) * (recently_delta_t - delta_t);
double remain = delta_t * (total - num);
std::cout << "\r" << std::fixed << std::setprecision(2) <<
percent << "%\tremain: " << (int)remain << "[s]" << std::flush;
percent << "%\tremain: " << (int) remain << "[s]\ttotal: " << elapsed_time << "[s]" << std::flush;
}

double elapsed() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count() / 1000.;
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count() /
1000.;
}

void end() {
Expand All @@ -42,35 +54,37 @@ struct Progress {
};

int main() {
Image img(512, 512);
Image img(256, 256);

// コーネルボックス
// コーネルボックス
// std::vector<std::shared_ptr<ShapeBase>> shapes{
// std::make_shared<Sphere>(Vector3(0, 10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 上壁
// std::make_shared<Sphere>(Vector3(10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.25, 0.25, 0.75))) , //右壁
// std::make_shared<Sphere>(Vector3(0, -10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 下壁
// std::make_shared<Sphere>(Vector3(-10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.75, 0.25, 0.25))), // 左壁
// std::make_shared<Sphere>(Vector3(0, 0, 10004), 10000, Vector3(0), Diffuse(Vector3(0.75))), // 奥壁
// std::make_shared<Sphere>(Vector3(0, 23.48, 2), 20, Vector3(12), Diffuse(Vector3())), // ライト
// std::make_shared<Sphere>(Vector3(0, 10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 上壁
// std::make_shared<Sphere>(Vector3(10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.25, 0.25, 0.75))), //右壁
// std::make_shared<Sphere>(Vector3(0, -10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 下壁
// std::make_shared<Sphere>(Vector3(-10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.75, 0.25, 0.25))), // 左壁
// std::make_shared<Sphere>(Vector3(0, 0, 10004), 10000, Vector3(0), Diffuse(Vector3(0.75))), // 奥壁
// std::make_shared<Sphere>(Vector3(0, 23.48, 2), 20, Vector3(12), Diffuse(Vector3())), // ライト
//
// std::make_shared<Sphere>(Vector3(-2, -2.3, 3), 1.2, Vector3(), Diffuse(Vector3(0.999))),
// std::make_shared<Sphere>(Vector3(2, -2.3, 2), 1.2, Vector3(), Diffuse(Vector3(0.25, 0.75, 0.25))),
//};
// std::make_shared<Sphere>(Vector3(-2, -2.3, 3), 1.2, Vector3(), Diffuse(Vector3(0.999))),
// std::make_shared<Sphere>(Vector3(2, -2.3, 2), 1.2, Vector3(), Diffuse(Vector3(0.25, 0.75, 0.25))),
// std::make_shared<ParallelLight>(Vector3(0, 0, -1), M_PI * 15/180, Vector3(1), Diffuse(Vector3(0))),
// };

//
std::vector<std::shared_ptr<ShapeBase>> shapes{
std::make_shared<Sphere>(Vector3(0, -10004, 0), 10000, Vector3(), Diffuse(Vector3(0.75))),
std::make_shared<Sphere>(Vector3(-2, -2.8, 3), 1.2, Vector3(), Diffuse(Vector3(0.999))),
std::make_shared<Sphere>(Vector3(2, -2.8, 2), 1.2, Vector3(), Diffuse(Vector3(0.25, 0.75, 0.25))),
std::make_shared<Sphere>(Vector3(0, -3, 6), 1, Vector3(), Fuzz(Vector3(0.75), 0.2 * M_PI)),
std::make_shared<ParallelLight>(Vector3(1, 2, 1), M_PI * 30/180, Vector3(2), Diffuse(Vector3(0))) //太陽の半頂角
std::make_shared<ParallelLight>(Vector3(1, 2, 1), M_PI * 15/180, Vector3(1), Diffuse(Vector3(0))),
};

Scene scene(shapes, Vector3(0.5, 0.6, 0.9), 1024, 100);
std::shared_ptr<CameraBase> camera = std::make_shared<NormalCamera>(img, Vector3(0, 0, -6), Vector3(0), M_PI*3/8);
Scene scene(shapes, Vector3(0.15, 0.2, 0.35), 4*1000, 1e5);
std::shared_ptr<CameraBase> camera = std::make_shared<NormalCamera>(
img, Vector3(0, 0, -6), Vector3(0), M_PI * 3 / 8);
Progress progress(img.size);

#pragma omp parallel for schedule(auto)
#pragma omp parallel for schedule(dynamic, 1)
for (int x = 0; x < img.width; ++x) {
for (int y = 0; y < img.height; ++y) {
#ifdef NDEBUG
Expand All @@ -89,7 +103,6 @@ int main() {
img.set_pixel(x, y, L / scene.spp);
}
}

img.output_ppm("./result.ppm");
progress.end();
return 0;
Expand Down
6 changes: 2 additions & 4 deletions scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ struct Scene {
if (!hit || ray.depth >= lim_depth) {
return sky_color;
}
Vector3 L_illuminance = hit->hit_shape_ptr->illuminance;

auto[next_ray, f] = hit->hit_shape_ptr->reflect(hit->point, ray.direction, hit->normal);
auto[next_ray, f, L_illuminance] = hit->hit_shape_ptr->reflect(hit->point, ray.direction, hit->normal);
next_ray.depth = ray.depth + 1;

Vector3 next_L = random_::rand() < roulette_p && f != Vector3() ? L(next_ray, roulette_p) : Vector3();
Vector3 next_L = random_::rand() > roulette_p || f == Vector3(0) ? Vector3(0) : L(next_ray, roulette_p);
return L_illuminance + next_L * f / roulette_p;
}
};
Expand Down
13 changes: 7 additions & 6 deletions shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ShapeBase {

virtual std::optional<Hit> intersect(const Ray &ray) const = 0;

virtual std::tuple<Ray, Vector3>
virtual std::tuple<Ray, Vector3, Vector3>
reflect(const Vector3 &point, const Vector3 &in_direction, const Vector3 &normal) const = 0;
};

Expand Down Expand Up @@ -61,8 +61,9 @@ struct Sphere : public ShapeBase {
return {};
}

std::tuple<Ray, Vector3> reflect(const Vector3 &point, const Vector3 &in_direction, const Vector3 &normal) const {
return material_ptr->reflect(point, in_direction, normal);
std::tuple<Ray, Vector3, Vector3> reflect(const Vector3 &point, const Vector3 &in_direction, const Vector3 &normal) const {
auto [next_ray, f] = material_ptr->reflect(point, in_direction, normal);
return {next_ray, f, illuminance};
};

};
Expand All @@ -74,7 +75,7 @@ struct ParallelLight : ShapeBase {

template<class T>
ParallelLight(const Vector3 &direction, double half_vertex_angle, const Vector3 &illuminance, T material_ptr) :
ShapeBase(illuminance / 2 / M_PI / (1 - std::cos(half_vertex_angle)), std::make_shared<T>(material_ptr)),
ShapeBase(illuminance, std::make_shared<T>(material_ptr)),
direction(direction), half_vertex_angle(half_vertex_angle) {}

std::optional<Hit> intersect(const Ray &ray) const {
Expand All @@ -87,8 +88,8 @@ struct ParallelLight : ShapeBase {
return {};
};

std::tuple<Ray, Vector3> reflect(const Vector3 &, const Vector3 &, const Vector3 &) const {
return {Ray(), Vector3()};
std::tuple<Ray, Vector3, Vector3> reflect(const Vector3 &, const Vector3 &, const Vector3 &) const {
return {Ray(), Vector3(), illuminance / 2 / M_PI / (1 - std::cos(half_vertex_angle))};
};
};

Expand Down

0 comments on commit 824c6f1

Please sign in to comment.