From 6ffca8e2e57657b3f9981ce6ddc7c021bbdfb59e Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Wed, 31 Jan 2024 08:44:00 +0100 Subject: [PATCH] updated --- libs/yocto/yocto_gui.cpp | 1 - libs/yocto/yocto_raytracing.cpp | 22 -------- libs/yocto/yocto_raytracing.h | 4 -- libs/yocto/yocto_scene.cpp | 94 +++++++++++++++++++++++++++++++++ libs/yocto/yocto_scene.h | 13 +++++ libs/yocto/yocto_sceneio.cpp | 12 ----- libs/yocto/yocto_sceneio.h | 1 - libs/yocto/yocto_trace.cpp | 39 -------------- 8 files changed, 107 insertions(+), 79 deletions(-) diff --git a/libs/yocto/yocto_gui.cpp b/libs/yocto/yocto_gui.cpp index 40657117f..fc0527019 100644 --- a/libs/yocto/yocto_gui.cpp +++ b/libs/yocto/yocto_gui.cpp @@ -39,7 +39,6 @@ #include #include "yocto_cutrace.h" -#include "yocto_raytracing.h" #ifdef __APPLE__ #define GL_SILENCE_DEPRECATION diff --git a/libs/yocto/yocto_raytracing.cpp b/libs/yocto/yocto_raytracing.cpp index 89cf054fd..841fa6973 100644 --- a/libs/yocto/yocto_raytracing.cpp +++ b/libs/yocto/yocto_raytracing.cpp @@ -789,28 +789,6 @@ void refit_scene_bvh(scene_bvh& sbvh, const scene_data& scene, }); } -// Compute scene bounding box -bbox3f compute_scene_bounds(const scene_data& scene) { - auto shape_bbox = vector{}; - auto bbox = invalidb3f; - for (auto& shape : scene.shapes) { - auto& sbbox = shape_bbox.emplace_back(); - for (auto p : shape.positions) sbbox = merge(sbbox, p); - } - for (auto& instance : scene.instances) { - auto& sbbox = shape_bbox[instance.shape]; - bbox = merge(bbox, transform_bbox(instance.frame, sbbox)); - } - return bbox; -} - -// Compute shape bounding box -bbox3f compute_shape_bounds(const shape_data& shape) { - auto bbox = invalidb3f; - for (auto p : shape.positions) bbox = merge(bbox, p); - return bbox; -} - } // namespace yocto // ----------------------------------------------------------------------------- diff --git a/libs/yocto/yocto_raytracing.h b/libs/yocto/yocto_raytracing.h index c15abb62f..c3f7e5b75 100644 --- a/libs/yocto/yocto_raytracing.h +++ b/libs/yocto/yocto_raytracing.h @@ -305,10 +305,6 @@ intersection3f overlap_shape_bvh(const shape_bvh& bvh, const shape_data& shape, intersection3f overlap_scene_bvh(const scene_bvh& bvh, const scene_data& scene, vec3f pos, float max_distance, bool find_any = false); -// Compute scene bounds -bbox3f compute_scene_bounds(const scene_data& scene); -bbox3f compute_shape_bounds(const shape_data& shape); - } // namespace yocto // ----------------------------------------------------------------------------- diff --git a/libs/yocto/yocto_scene.cpp b/libs/yocto/yocto_scene.cpp index 5af477fc5..f678e17f7 100644 --- a/libs/yocto/yocto_scene.cpp +++ b/libs/yocto/yocto_scene.cpp @@ -67,6 +67,44 @@ vec2i camera_resolution(const camera_data& camera, int resolution) { : vec2i{(int)round(resolution * camera.aspect), resolution}; } +// Generates a ray from a camera for image plane coordinate uv and +// the lens coordinates luv. +ray3f eval_camera(const camera_data& camera, vec2f image_uv, vec2f lens_uv) { + auto film = camera.aspect >= 1 + ? vec2f{camera.film, camera.film / camera.aspect} + : vec2f{camera.film * camera.aspect, camera.film}; + if (!camera.orthographic) { + auto q = vec3f{film.x * (0.5f - image_uv.x), film.y * (image_uv.y - 0.5f), + camera.lens}; + // ray direction through the lens center + auto dc = -normalize(q); + // point on the lens + auto e = vec3f{ + lens_uv.x * camera.aperture / 2, lens_uv.y * camera.aperture / 2, 0}; + // point on the focus plane + auto p = dc * camera.focus / abs(dc.z); + // correct ray direction to account for camera focusing + auto d = normalize(p - e); + // done + return ray3f{ + transform_point(camera.frame, e), transform_direction(camera.frame, d)}; + } else { + auto scale = 1 / camera.lens; + auto q = vec3f{film.x * (0.5f - image_uv.x) * scale, + film.y * (image_uv.y - 0.5f) * scale, camera.lens}; + // point on the lens + auto e = vec3f{-q.x, -q.y, 0} + vec3f{lens_uv.x * camera.aperture / 2, + lens_uv.y * camera.aperture / 2, 0}; + // point on the focus plane + auto p = vec3f{-q.x, -q.y, -camera.focus}; + // correct ray direction to account for camera focusing + auto d = normalize(p - e); + // done + return ray3f{ + transform_point(camera.frame, e), transform_direction(camera.frame, d)}; + } +} + } // namespace yocto // ----------------------------------------------------------------------------- @@ -589,6 +627,29 @@ vec3f eval_environment(const scene_data& scene, vec3f direction) { // ----------------------------------------------------------------------------- namespace yocto { +// Add camera. +void add_camera(scene_data& scene) { + if (!scene.cameras.empty()) return; + scene.camera_names.emplace_back("camera"); + auto& camera = scene.cameras.emplace_back(); + camera.orthographic = false; + camera.film = 0.036f; + camera.aspect = (float)16 / (float)9; + camera.aperture = 0; + camera.lens = 0.050f; + auto bbox = compute_scene_bounds(scene); + auto center = (bbox.max + bbox.min) / 2; + auto bbox_radius = length(bbox.max - bbox.min) / 2; + auto camera_dir = vec3f{0, 0, 1}; + auto camera_dist = bbox_radius * camera.lens / (camera.film / camera.aspect); + camera_dist *= 2.0f; // correction for tracer camera implementation + auto from = camera_dir * camera_dist + center; + auto to = center; + auto up = vec3f{0, 1, 0}; + camera.frame = lookat_frame(from, to, up); + camera.focus = length(from - to); +} + // get named camera or default if camera is empty int find_camera(const scene_data& scene, const string& name) { if (scene.cameras.empty()) return invalidid; @@ -625,6 +686,17 @@ bool has_lights(const scene_data& scene) { return false; } +// Add a sky environment +void add_sky(scene_data& scene, float sun_angle) { + scene.texture_names.emplace_back("sky"); + auto& texture = scene.textures.emplace_back(); + texture = image_to_texture(make_sunsky({1024, 512}, sun_angle), true); + scene.environment_names.emplace_back("sky"); + auto& environment = scene.environments.emplace_back(); + environment.emission = {1, 1, 1}; + environment.emission_tex = (int)scene.textures.size() - 1; +} + // Scene creation helpers scene_data make_scene() { return scene_data{}; } @@ -778,6 +850,28 @@ int add_texture(scene_data& scene, const string& name, return (int)scene.textures.size() - 1; } +// Compute scene bounding box +bbox3f compute_scene_bounds(const scene_data& scene) { + auto shape_bbox = vector{}; + auto bbox = invalidb3f; + for (auto& shape : scene.shapes) { + auto& sbbox = shape_bbox.emplace_back(); + for (auto p : shape.positions) sbbox = merge(sbbox, p); + } + for (auto& instance : scene.instances) { + auto& sbbox = shape_bbox[instance.shape]; + bbox = merge(bbox, transform_bbox(instance.frame, sbbox)); + } + return bbox; +} + +// Compute shape bounding box +bbox3f compute_shape_bounds(const shape_data& shape) { + auto bbox = invalidb3f; + for (auto p : shape.positions) bbox = merge(bbox, p); + return bbox; +} + } // namespace yocto // ----------------------------------------------------------------------------- diff --git a/libs/yocto/yocto_scene.h b/libs/yocto/yocto_scene.h index 5f5455783..805bb8ec0 100644 --- a/libs/yocto/yocto_scene.h +++ b/libs/yocto/yocto_scene.h @@ -231,6 +231,9 @@ namespace yocto { // Computes the image resolution from the camera. vec2i camera_resolution(const camera_data& camera, int resolution); +// Camera ray +ray3f eval_camera(const camera_data& camera, vec2f image_uv, vec2f lens_uv); + } // namespace yocto // ----------------------------------------------------------------------------- @@ -339,12 +342,18 @@ vec3f eval_environment(const scene_data& scene, vec3f direction); // ----------------------------------------------------------------------------- namespace yocto { +// add a default camera +void add_camera(scene_data& scene); + // get named camera or default if name is empty int find_camera(const scene_data& scene, const string& name); // Check if lights are present bool has_lights(const scene_data& scene); +// Add a sky texture +void add_sky(scene_data& scene, float sun_angle = pif / 4); + // Initialize scene scene_data make_scene(); @@ -392,6 +401,10 @@ int add_texture(scene_data& scene, const string& name, int add_texture(scene_data& scene, const string& name, const image_t& texture, bool linear = false); +// Compute scene bounds +bbox3f compute_scene_bounds(const scene_data& scene); +bbox3f compute_shape_bounds(const shape_data& shape); + } // namespace yocto // ----------------------------------------------------------------------------- diff --git a/libs/yocto/yocto_sceneio.cpp b/libs/yocto/yocto_sceneio.cpp index 42bc10189..06010caad 100644 --- a/libs/yocto/yocto_sceneio.cpp +++ b/libs/yocto/yocto_sceneio.cpp @@ -52,7 +52,6 @@ #include "yocto_image.h" #include "yocto_modelio.h" #include "yocto_pbrtio.h" -#include "yocto_raytracing.h" #include "yocto_shading.h" #include "yocto_shape.h" @@ -2115,17 +2114,6 @@ void add_environment( {1, 1, 1}, (int)scene.textures.size() - 1}); } -// Add a sky environment -void add_sky(scene_data& scene, float sun_angle) { - scene.texture_names.emplace_back("sky"); - auto& texture = scene.textures.emplace_back(); - texture = image_to_texture(make_sunsky({1024, 512}, sun_angle), true); - scene.environment_names.emplace_back("sky"); - auto& environment = scene.environments.emplace_back(); - environment.emission = {1, 1, 1}; - environment.emission_tex = (int)scene.textures.size() - 1; -} - } // namespace yocto // ----------------------------------------------------------------------------- diff --git a/libs/yocto/yocto_sceneio.h b/libs/yocto/yocto_sceneio.h index cdffd58d3..3ed52532c 100644 --- a/libs/yocto/yocto_sceneio.h +++ b/libs/yocto/yocto_sceneio.h @@ -205,7 +205,6 @@ void save_scene( // Add environment void add_environment( scene_data& scene, const string& name, const string& filename); -void add_sky(scene_data& scene, float sun_angle = pif / 4); // Make missing scene directories void make_scene_directories(const string& filename, const scene_data& scene); diff --git a/libs/yocto/yocto_trace.cpp b/libs/yocto/yocto_trace.cpp index 86aa68829..388bd9517 100644 --- a/libs/yocto/yocto_trace.cpp +++ b/libs/yocto/yocto_trace.cpp @@ -318,45 +318,6 @@ static float sample_scattering_pdf( return sample_phasefunction_pdf(material.scanisotropy, outgoing, incoming); } -// Generates a ray from a camera for image plane coordinate uv and -// the lens coordinates luv. -static ray3f eval_camera( - const camera_data& camera, vec2f image_uv, vec2f lens_uv) { - auto film = camera.aspect >= 1 - ? vec2f{camera.film, camera.film / camera.aspect} - : vec2f{camera.film * camera.aspect, camera.film}; - if (!camera.orthographic) { - auto q = vec3f{film.x * (0.5f - image_uv.x), film.y * (image_uv.y - 0.5f), - camera.lens}; - // ray direction through the lens center - auto dc = -normalize(q); - // point on the lens - auto e = vec3f{ - lens_uv.x * camera.aperture / 2, lens_uv.y * camera.aperture / 2, 0}; - // point on the focus plane - auto p = dc * camera.focus / abs(dc.z); - // correct ray direction to account for camera focusing - auto d = normalize(p - e); - // done - return ray3f{ - transform_point(camera.frame, e), transform_direction(camera.frame, d)}; - } else { - auto scale = 1 / camera.lens; - auto q = vec3f{film.x * (0.5f - image_uv.x) * scale, - film.y * (image_uv.y - 0.5f) * scale, camera.lens}; - // point on the lens - auto e = vec3f{-q.x, -q.y, 0} + vec3f{lens_uv.x * camera.aperture / 2, - lens_uv.y * camera.aperture / 2, 0}; - // point on the focus plane - auto p = vec3f{-q.x, -q.y, -camera.focus}; - // correct ray direction to account for camera focusing - auto d = normalize(p - e); - // done - return ray3f{ - transform_point(camera.frame, e), transform_direction(camera.frame, d)}; - } -} - // Sample camera static ray3f sample_camera(const camera_data& camera, vec2i ij, vec2i image_size, vec2f puv, vec2f luv, bool tent) {