Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Jan 31, 2024
1 parent 13ef58d commit 6ffca8e
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 79 deletions.
1 change: 0 additions & 1 deletion libs/yocto/yocto_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <stdexcept>

#include "yocto_cutrace.h"
#include "yocto_raytracing.h"

#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
Expand Down
22 changes: 0 additions & 22 deletions libs/yocto/yocto_raytracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bbox3f>{};
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

// -----------------------------------------------------------------------------
Expand Down
4 changes: 0 additions & 4 deletions libs/yocto/yocto_raytracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

// -----------------------------------------------------------------------------
Expand Down
94 changes: 94 additions & 0 deletions libs/yocto/yocto_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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{}; }

Expand Down Expand Up @@ -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<bbox3f>{};
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

// -----------------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions libs/yocto/yocto_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<vec4b>& 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

// -----------------------------------------------------------------------------
Expand Down
12 changes: 0 additions & 12 deletions libs/yocto/yocto_sceneio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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

// -----------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion libs/yocto/yocto_sceneio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 0 additions & 39 deletions libs/yocto/yocto_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6ffca8e

Please sign in to comment.