Skip to content

Commit

Permalink
Add option not to save image & enable press ESC to close window
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonKang130 committed Apr 11, 2024
1 parent 18c0260 commit 7c69bda
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/base/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Integrator::Integrator(Scene *scene, const SceneNodeDesc *desc) noexcept
"sampler", SceneNodeDesc::shared_default_sampler("independent")))},
_light_sampler{scene->load_light_sampler(desc->property_node_or_default(
"light_sampler", SceneNodeDesc::shared_default_light_sampler("uniform")))},
_video{desc->property_bool_or_default("video", false)} {}
_video{desc->property_bool_or_default("video", false)},
_save{desc->property_bool_or_default("save", true)} {}

Integrator::Instance::Instance(Pipeline &pipeline, CommandBuffer &command_buffer, const Integrator *integrator) noexcept
: _pipeline{pipeline}, _integrator{integrator},
Expand Down Expand Up @@ -45,7 +46,9 @@ void ProgressiveIntegrator::Instance::render(Stream &stream) noexcept {
command_buffer << compute::synchronize();
camera->film()->release();
auto film_path = camera->node()->file();
save_image(film_path, reinterpret_cast<const float *>(pixels.data()), resolution);
if (node()->save()) {
save_image(film_path, reinterpret_cast<const float *>(pixels.data()), resolution);
}
}
}

Expand Down Expand Up @@ -117,11 +120,13 @@ void ProgressiveIntegrator::Instance::_render_one_camera(
camera->film()->download(command_buffer, local_pixels.data());
command_buffer << compute::synchronize();
camera->film()->clear(command_buffer);
auto film_path = camera->node()->file();
//film_path is a std::filesystem::path, add number to its name
auto new_name = film_path.stem().string() + std::format("{:05}", shutter_id) + film_path.extension().string();
auto new_film_path = film_path.replace_filename(new_name);
save_image(new_film_path, reinterpret_cast<const float *>(local_pixels.data()), resolution);
if (node()->save()) {
auto film_path = camera->node()->file();
//film_path is a std::filesystem::path, add number to its name
auto new_name = film_path.stem().string() + std::format("{:05}", shutter_id) + film_path.extension().string();
auto new_film_path = film_path.replace_filename(new_name);
save_image(new_film_path, reinterpret_cast<const float *>(local_pixels.data()), resolution);
}
shutter_id++;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/base/integrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ class Integrator : public SceneNode {
const Sampler *_sampler;
const LightSampler *_light_sampler;
const bool _video;
const bool _save;
public:
Integrator(Scene *scene, const SceneNodeDesc *desc) noexcept;
[[nodiscard]] auto sampler() const noexcept { return _sampler; }
[[nodiscard]] auto light_sampler() const noexcept { return _light_sampler; }
[[nodiscard]] auto video() const noexcept { return _video; }
[[nodiscard]] auto save() const noexcept { return _save; }
[[nodiscard]] virtual luisa::unique_ptr<Instance> build(
Pipeline &pipeline, CommandBuffer &command_buffer) const noexcept = 0;
};
Expand Down
6 changes: 6 additions & 0 deletions src/films/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ class DisplayInstance final : public Film::Instance {
void release() noexcept override {
while (_window && !_window->should_close()) {
_window->poll_events();
if (_window->is_key_down(Key::KEY_ESCAPE)) {
_window->set_should_close();
}
}
_framebuffer = {};
_swapchain = {};
Expand All @@ -194,6 +197,9 @@ class DisplayInstance final : public Film::Instance {
current_time - _last_frame_time >= interval) {
_last_frame_time = current_time;
_window->poll_events();
if (_window->is_key_down(Key::KEY_ESCAPE)) {
_window->set_should_close();
}
if (_window->should_close()) {
command_buffer << synchronize();
exit(0);// FIXME: exit gracefully
Expand Down

0 comments on commit 7c69bda

Please sign in to comment.