From f959bd58b6b94f8b9ea71c33b326679b33249c56 Mon Sep 17 00:00:00 2001 From: TheNumbat Date: Tue, 13 Oct 2020 16:27:07 -0400 Subject: [PATCH] pathtracer adjustments --- src/gui/manager.h | 2 +- src/gui/widgets.cpp | 2 +- src/rays/pathtracer.cpp | 38 +++----------------------------------- src/rays/pathtracer.h | 3 +-- src/rays/tri_mesh.h | 3 ++- src/student/shapes.cpp | 4 ++++ src/student/tri_mesh.cpp | 8 ++++++-- 7 files changed, 18 insertions(+), 42 deletions(-) diff --git a/src/gui/manager.h b/src/gui/manager.h index 66c393d..cfda2b5 100644 --- a/src/gui/manager.h +++ b/src/gui/manager.h @@ -89,7 +89,7 @@ private: void frame(Scene &scene, Camera &cam); static inline const char *scene_file_types = "dae,obj,fbx,glb,gltf,3ds,blend,stl,ply"; - static inline const char *image_file_types = "exr,jpg,jpeg,png,tga,bmp,psd,gif"; + static inline const char *image_file_types = "exr,hdr,hdri,jpg,jpeg,png,tga,bmp,psd,gif"; void render_selected(Scene_Object &obj); void load_scene(Scene &scene, Undo &undo, bool clear); diff --git a/src/gui/widgets.cpp b/src/gui/widgets.cpp index 2d545a1..9f71799 100644 --- a/src/gui/widgets.cpp +++ b/src/gui/widgets.cpp @@ -635,7 +635,7 @@ std::string Widget_Render::step(Animate &animate, Scene &scene) { return "Failed to write output!"; } - pathtracer.begin_render(scene, cam, true); + pathtracer.begin_render(scene, cam); next_frame++; } } diff --git a/src/rays/pathtracer.cpp b/src/rays/pathtracer.cpp index f868dd9..9683fef 100644 --- a/src/rays/pathtracer.cpp +++ b/src/rays/pathtracer.cpp @@ -20,35 +20,6 @@ Pathtracer::Pathtracer(Gui::Widget_Render &gui, Vec2 screen_dim) Pathtracer::~Pathtracer() { thread_pool.stop(); } -void Pathtracer::refit_scene(Scene &layout_scene) { - - std::unordered_map obj_map; - std::vector objs = scene.destructure(); - for (auto &o : objs) - obj_map.insert({o.id(), std::move(o)}); - - std::set light_ids; - for (auto &l : lights) - light_ids.insert(l.id()); - - layout_scene.for_items([&](const Scene_Item &item) { - auto entry = obj_map.find(item.id()); - if (entry != obj_map.end()) { - entry->second.set_trans(item.pose().transform()); - if (light_ids.count(entry->first)) { - obj_map.erase(entry); - } - } - }); - - objs.clear(); - for (auto &o : obj_map) - objs.push_back(std::move(o.second)); - - build_lights(layout_scene, objs); - scene.build(std::move(objs)); -} - void Pathtracer::build_lights(Scene &layout_scene, std::vector &objs) { lights.clear(); @@ -160,7 +131,7 @@ void Pathtracer::build_scene(Scene &layout_scene) { obj_list.push_back( Object(std::move(shape), obj.id(), idx, obj.pose.transform())); } else { - Tri_Mesh mesh(obj.posed_mesh()); + Tri_Mesh mesh(obj.posed_mesh(), obj.get_mesh().flipped()); std::lock_guard lock(obj_mut); obj_list.push_back( Object(std::move(mesh), obj.id(), idx, obj.pose.transform())); @@ -236,7 +207,7 @@ size_t Pathtracer::visualize_bvh(GL::Lines &lines, GL::Lines &active, size_t dep return scene.visualize(lines, active, depth, Mat4::I); } -void Pathtracer::begin_render(Scene &layout_scene, const Camera &cam, bool refit) { +void Pathtracer::begin_render(Scene &layout_scene, const Camera &cam) { size_t n_threads = std::thread::hardware_concurrency(); size_t samples_per_epoch = std::max(size_t(1), n_samples / (n_threads * 10)); @@ -247,10 +218,7 @@ void Pathtracer::begin_render(Scene &layout_scene, const Camera &cam, bool refit total_epochs = n_samples / samples_per_epoch + !!(n_samples % samples_per_epoch); build_time = SDL_GetPerformanceCounter(); - if (refit) - refit_scene(layout_scene); - else - build_scene(layout_scene); + build_scene(layout_scene); render_time = SDL_GetPerformanceCounter(); build_time = render_time - build_time; diff --git a/src/rays/pathtracer.h b/src/rays/pathtracer.h index 779f9b5..134c74b 100644 --- a/src/rays/pathtracer.h +++ b/src/rays/pathtracer.h @@ -32,7 +32,7 @@ public: const GL::Tex2D &get_output_texture(float exposure); size_t visualize_bvh(GL::Lines &lines, GL::Lines &active, size_t level); - void begin_render(Scene &scene, const Camera &camera, bool refit = false); + void begin_render(Scene &scene, const Camera &camera); void cancel(); bool in_progress() const; float progress() const; @@ -41,7 +41,6 @@ public: private: // Internal void build_scene(Scene &scene); - void refit_scene(Scene &scene); void build_lights(Scene &scene, std::vector &objs); void do_trace(size_t samples); void accumulate(const HDR_Image &sample); diff --git a/src/rays/tri_mesh.h b/src/rays/tri_mesh.h index 999d35f..4038b76 100644 --- a/src/rays/tri_mesh.h +++ b/src/rays/tri_mesh.h @@ -32,7 +32,7 @@ private: class Tri_Mesh { public: Tri_Mesh() = default; - Tri_Mesh(const GL::Mesh &mesh); + Tri_Mesh(const GL::Mesh &mesh, bool flip = false); BBox bbox() const; Trace hit(const Ray &ray) const; @@ -44,6 +44,7 @@ public: private: std::vector verts; BVH triangles; + bool flip_normals = false; }; } // namespace PT diff --git a/src/student/shapes.cpp b/src/student/shapes.cpp index 8c9e52e..27a2fd2 100644 --- a/src/student/shapes.cpp +++ b/src/student/shapes.cpp @@ -25,6 +25,10 @@ Trace Sphere::hit(const Ray &ray) const { // but only the _later_ one is within ray.time_bounds, you should // return that one! + // Note: ray.dir is not necessarily a unit vector if this object has a scale transform. + // If you want it to be so, you should use ray.dir.unit(), and also note that you will + // have to re-scale ret.time to be the distance along the original non-unit ray.dir. + Trace ret; ret.hit = false; // was there an intersection? ret.time = 0.0f; // at what time did the intersection occur? diff --git a/src/student/tri_mesh.cpp b/src/student/tri_mesh.cpp index 903f668..c14fdc9 100644 --- a/src/student/tri_mesh.cpp +++ b/src/student/tri_mesh.cpp @@ -57,11 +57,15 @@ void Tri_Mesh::build(const GL::Mesh &mesh) { triangles.build(std::move(tris), 4); } -Tri_Mesh::Tri_Mesh(const GL::Mesh &mesh) { build(mesh); } +Tri_Mesh::Tri_Mesh(const GL::Mesh &mesh, bool flip) { build(mesh); flip_normals = flip; } BBox Tri_Mesh::bbox() const { return triangles.bbox(); } -Trace Tri_Mesh::hit(const Ray &ray) const { return triangles.hit(ray); } +Trace Tri_Mesh::hit(const Ray &ray) const { + Trace t = triangles.hit(ray); + if(flip_normals) t.normal = -t.normal; + return t; +} size_t Tri_Mesh::visualize(GL::Lines &lines, GL::Lines &active, size_t level, const Mat4 &trans) const { -- GitLab