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 3b30967 commit cc744c8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 51 deletions.
15 changes: 6 additions & 9 deletions libs/yocto/yocto_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ namespace yocto {

// pixel access
vec4f lookup_texture(
const texture_data& texture, vec2i ij_, bool ldr_as_linear, bool no_flipv) {
auto size = max(texture.pixelsb.size(), texture.pixelsf.size());
auto ij = no_flipv ? ij_ : vec2i{ij_.x, size.y - 1 - ij_.y};
const texture_data& texture, vec2i ij, bool ldr_as_linear) {
if (!texture.pixelsf.empty()) return texture.pixelsf[ij];
if (!texture.pixelsb.empty())
return ldr_as_linear ? byte_to_float(texture.pixelsb[ij])
Expand All @@ -126,8 +124,7 @@ vec4f lookup_texture(
}

// Evaluates an image at a point `uv`.
vec4f eval_texture(
const texture_data& texture, vec2f uv, bool ldr_as_linear, bool no_flipv) {
vec4f eval_texture(const texture_data& texture, vec2f uv, bool ldr_as_linear) {
if (texture.pixelsf.empty() && texture.pixelsb.empty()) return {0, 0, 0, 0};

// get texture width/height
Expand Down Expand Up @@ -155,10 +152,10 @@ vec4f eval_texture(
}

// Helpers
vec4f eval_texture(const scene_data& scene, int texture, vec2f uv,
bool ldr_as_linear, bool no_flipv) {
vec4f eval_texture(
const scene_data& scene, int texture, vec2f uv, bool ldr_as_linear) {
if (texture == invalidid) return {1, 1, 1, 1};
return eval_texture(scene.textures[texture], uv, ldr_as_linear, no_flipv);
return eval_texture(scene.textures[texture], uv, ldr_as_linear);
}

// conversion from image
Expand Down Expand Up @@ -573,7 +570,7 @@ vec3f eval_environment(const scene_data& scene,
const environment_data& environment, vec3f direction) {
auto wl = transform_direction(inverse(environment.frame), direction);
auto texcoord = vec2f{
atan2(wl.z, wl.x) / (2 * pif), acos(-clamp(wl.y, -1.0f, 1.0f)) / pif};
atan2(wl.z, wl.x) / (2 * pif), acos(clamp(wl.y, -1.0f, 1.0f)) / pif};
if (texcoord.x < 0) texcoord.x += 1;
return environment.emission *
xyz(eval_texture(scene, environment.emission_tex, texcoord));
Expand Down
12 changes: 6 additions & 6 deletions libs/yocto/yocto_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ ray3f eval_camera(const camera_data& camera, vec2f image_uv, vec2f lens_uv);
namespace yocto {

// Evaluates a texture
vec4f eval_texture(const texture_data& texture, vec2f uv,
bool ldr_as_linear = false, bool no_vflip = false);
vec4f eval_texture(const scene_data& scene, int texture, vec2f uv,
bool ldr_as_linear = false, bool no_vflip = false);
vec4f eval_texture(
const texture_data& texture, vec2f uv, bool ldr_as_linear = false);
vec4f eval_texture(
const scene_data& scene, int texture, vec2f uv, bool ldr_as_linear = false);

// pixel access
vec4f lookup_texture(const texture_data& texture, vec2i ij,
bool ldr_as_linear = false, bool no_vflip = false);
vec4f lookup_texture(
const texture_data& texture, vec2i ij, bool ldr_as_linear = false);

// conversion from image
texture_data image_to_texture(const image_t<vec4f>& image, bool linear);
Expand Down
46 changes: 23 additions & 23 deletions libs/yocto/yocto_sceneio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,15 +700,16 @@ void save_imageb(const string& filename, const image_t<vec4b>& image) {
namespace yocto {

// Load mesh
shape_data load_shape(const string& filename) {
shape_data load_shape(const string& filename, bool flip_texcoords) {
auto shape = shape_data{};
auto ext = path_extension(filename);
if (ext == ".ply" || ext == ".PLY") {
auto ply = load_ply(filename);
// TODO: remove when all as arrays
get_positions(ply, (vector<array<float, 3>>&)shape.positions);
get_normals(ply, (vector<array<float, 3>>&)shape.normals);
get_texcoords(ply, (vector<array<float, 2>>&)shape.texcoords);
get_texcoords(
ply, (vector<array<float, 2>>&)shape.texcoords, flip_texcoords);
get_colors(ply, (vector<array<float, 4>>&)shape.colors);
get_radius(ply, shape.radius);
get_faces(ply, (vector<array<int, 3>>&)shape.triangles,
Expand All @@ -724,7 +725,8 @@ shape_data load_shape(const string& filename) {
// TODO: remove when all as arrays
get_positions(obj, (vector<array<float, 3>>&)shape.positions);
get_normals(obj, (vector<array<float, 3>>&)shape.normals);
get_texcoords(obj, (vector<array<float, 2>>&)shape.texcoords);
get_texcoords(
obj, (vector<array<float, 2>>&)shape.texcoords, flip_texcoords);
get_faces(obj, (vector<array<int, 3>>&)shape.triangles,
(vector<array<int, 4>>&)shape.quads, materials);
get_lines(obj, (vector<array<int, 2>>&)shape.lines, materials);
Expand All @@ -749,14 +751,16 @@ shape_data load_shape(const string& filename) {
}

// Save ply mesh
void save_shape(const string& filename, const shape_data& shape, bool ascii) {
void save_shape(const string& filename, const shape_data& shape,
bool flip_texcoords, bool ascii) {
auto ext = path_extension(filename);
if (ext == ".ply" || ext == ".PLY") {
auto ply = ply_model{};
// TODO: remove when all as arrays
add_positions(ply, (const vector<array<float, 3>>&)shape.positions);
add_normals(ply, (const vector<array<float, 3>>&)shape.normals);
add_texcoords(ply, (const vector<array<float, 2>>&)shape.texcoords);
add_texcoords(
ply, (const vector<array<float, 2>>&)shape.texcoords, flip_texcoords);
add_colors(ply, (const vector<array<float, 4>>&)shape.colors);
add_radius(ply, shape.radius);
add_faces(ply, (const vector<array<int, 3>>&)shape.triangles,
Expand All @@ -769,7 +773,8 @@ void save_shape(const string& filename, const shape_data& shape, bool ascii) {
// TODO: remove when all as arrays
add_positions(obj, (const vector<array<float, 3>>&)shape.positions);
add_normals(obj, (const vector<array<float, 3>>&)shape.normals);
add_texcoords(obj, (const vector<array<float, 2>>&)shape.texcoords);
add_texcoords(
obj, (const vector<array<float, 2>>&)shape.texcoords, flip_texcoords);
add_triangles(obj, (const vector<array<int, 3>>&)shape.triangles, 0,
!shape.normals.empty(), !shape.texcoords.empty());
add_quads(obj, (const vector<array<int, 4>>&)shape.quads, 0,
Expand Down Expand Up @@ -851,15 +856,16 @@ void save_shape(const string& filename, const shape_data& shape, bool ascii) {
}

// Load face-varying mesh
fvshape_data load_fvshape(const string& filename) {
fvshape_data load_fvshape(const string& filename, bool flip_texcoords) {
auto shape = fvshape_data{};
auto ext = path_extension(filename);
if (ext == ".ply" || ext == ".PLY") {
auto ply = load_ply(filename);
// TODO: remove when all as arrays
get_positions(ply, (vector<array<float, 3>>&)shape.positions);
get_normals(ply, (vector<array<float, 3>>&)shape.normals);
get_texcoords(ply, (vector<array<float, 2>>&)shape.texcoords);
get_texcoords(
ply, (vector<array<float, 2>>&)shape.texcoords, flip_texcoords);
get_quads(ply, (vector<array<int, 4>>&)shape.quadspos);
if (!shape.normals.empty()) shape.quadsnorm = shape.quadspos;
if (!shape.texcoords.empty()) shape.quadstexcoord = shape.quadspos;
Expand All @@ -870,7 +876,8 @@ fvshape_data load_fvshape(const string& filename) {
auto materials = vector<int>{};
get_positions(obj, (vector<array<float, 3>>&)shape.positions);
get_normals(obj, (vector<array<float, 3>>&)shape.normals);
get_texcoords(obj, (vector<array<float, 2>>&)shape.texcoords);
get_texcoords(
obj, (vector<array<float, 2>>&)shape.texcoords, flip_texcoords);
get_fvquads(obj, (vector<array<int, 4>>&)shape.quadspos,
(vector<array<int, 4>>&)shape.quadsnorm,
(vector<array<int, 4>>&)shape.quadstexcoord, materials);
Expand All @@ -895,23 +902,24 @@ fvshape_data load_fvshape(const string& filename) {
}

// Save ply mesh
void save_fvshape(
const string& filename, const fvshape_data& shape, bool ascii) {
void save_fvshape(const string& filename, const fvshape_data& shape,
bool flip_texcoords, bool ascii) {
auto ext = path_extension(filename);
if (ext == ".ply" || ext == ".PLY") {
return save_shape(filename, fvshape_to_shape(shape), ascii);
return save_shape(filename, fvshape_to_shape(shape), flip_texcoords, ascii);
} else if (ext == ".obj" || ext == ".OBJ") {
auto obj = obj_shape{};
// TODO: remove when all as arrays
add_positions(obj, (const vector<array<float, 3>>&)shape.positions);
add_normals(obj, (const vector<array<float, 3>>&)shape.normals);
add_texcoords(obj, (const vector<array<float, 2>>&)shape.texcoords);
add_texcoords(
obj, (const vector<array<float, 2>>&)shape.texcoords, flip_texcoords);
add_fvquads(obj, (const vector<array<int, 4>>&)shape.quadspos,
(const vector<array<int, 4>>&)shape.quadsnorm,
(const vector<array<int, 4>>&)shape.quadstexcoord, 0);
save_obj(filename, obj);
} else if (ext == ".stl" || ext == ".STL") {
return save_shape(filename, fvshape_to_shape(shape), ascii);
return save_shape(filename, fvshape_to_shape(shape), flip_texcoords, ascii);
} else if (ext == ".cpp" || ext == ".CPP") {
auto to_cpp = [](const string& name, const string& vname,
const auto& values) -> string {
Expand Down Expand Up @@ -3706,7 +3714,6 @@ static scene_data load_gltf_scene(const string& filename, bool noparallel) {
"cannot load " + filename + " for unsupported primitive type"};
}
}
for (auto& texcoord : shape.texcoords) texcoord.y = 1 - texcoord.y;
}
}

Expand Down Expand Up @@ -3870,13 +3877,6 @@ static void save_gltf_scene(
}
}

// UV flip
auto flip_uv = [](const vector<vec2f>& texcoords) {
auto ftexcoords = texcoords;
for (auto& uv : ftexcoords) uv.y = 1 - uv.y;
return ftexcoords;
};

// buffers
auto shape_accessor_start = vector<int>{};
if (!scene.shapes.empty()) {
Expand Down Expand Up @@ -3943,7 +3943,7 @@ static void save_gltf_scene(
add_vertex(cgltf, gbuffer, shape.normals.size(), cgltf_type_vec3,
(const float*)shape.normals.data());
add_vertex(cgltf, gbuffer, shape.texcoords.size(), cgltf_type_vec2,
(const float*)flip_uv(shape.texcoords).data());
(const float*)shape.texcoords.data());
add_vertex(cgltf, gbuffer, shape.colors.size(), cgltf_type_vec4,
(const float*)shape.colors.data());
add_vertex(cgltf, gbuffer, shape.radius.size(), cgltf_type_scalar,
Expand Down
12 changes: 6 additions & 6 deletions libs/yocto/yocto_sceneio.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ bool make_texture_preset(
namespace yocto {

// Load/save a shape
shape_data load_shape(const string& filename);
void save_shape(
const string& filename, const shape_data& shape, bool ascii = false);
shape_data load_shape(const string& filename, bool flip_texcoords = true);
void save_shape(const string& filename, const shape_data& shape,
bool flip_texcoords = true, bool ascii = false);

// Load/save a subdiv
fvshape_data load_fvshape(const string& filename);
void save_fvshape(
const string& filename, const fvshape_data& shape, bool ascii = false);
fvshape_data load_fvshape(const string& filename, bool flip_texcoords = true);
void save_fvshape(const string& filename, const fvshape_data& shape,
bool flip_texcoords = true, bool ascii = false);

// Make presets. Supported mostly in IO.
shape_data make_shape_preset(const string& type);
Expand Down
12 changes: 5 additions & 7 deletions libs/yocto/yocto_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,8 @@ static vec3f sample_lights(const scene_data& scene, const trace_lights& lights,
auto& texture = scene.textures[environment.emission_tex];
auto idx = sample_discrete(light.elements_cdf, rel);
auto size = max(texture.pixelsf.size(), texture.pixelsb.size());
// we avoid flipping twice the v in the uv and returned direction
auto uv = vec2f{((idx % size.x) + 0.5f) / size.x,
1 - ((idx / size.x) + 0.5f) / size.y};
auto uv = vec2f{
((idx % size.x) + 0.5f) / size.x, ((idx / size.x) + 0.5f) / size.y};
return transform_direction(environment.frame,
{cos(uv.x * 2 * pif) * sin(uv.y * pif), cos(uv.y * pif),
sin(uv.x * 2 * pif) * sin(uv.y * pif)});
Expand Down Expand Up @@ -407,9 +406,8 @@ static float sample_lights_pdf(const scene_data& scene, const trace_bvh& bvh,
if (environment.emission_tex != invalidid) {
auto& emission_tex = scene.textures[environment.emission_tex];
auto wl = transform_direction(inverse(environment.frame), direction);
// we avoid flipping twice the v in the texcoord and ij
auto texcoord = vec2f{atan2(wl.z, wl.x) / (2 * pif),
acos(-clamp(wl.y, -1.0f, 1.0f)) / pif};
auto texcoord = vec2f{atan2(wl.z, wl.x) / (2 * pif),
acos(clamp(wl.y, -1.0f, 1.0f)) / pif};
if (texcoord.x < 0) texcoord.x += 1;
auto size = max(
emission_tex.pixelsf.size(), emission_tex.pixelsb.size());
Expand Down Expand Up @@ -1590,7 +1588,7 @@ trace_lights make_trace_lights(
light.elements_cdf = vector<float>(size.x * size.y);
for (auto idx : range(light.elements_cdf.size())) {
auto ij = vec2i{(int)idx % size.x, (int)idx / size.x};
auto th = pif - (ij.y + 0.5f) * pif / size.y;
auto th = (ij.y + 0.5f) * pif / size.y;
auto value = lookup_texture(texture, ij);
light.elements_cdf[idx] = max(value) * sin(th);
if (idx != 0) light.elements_cdf[idx] += light.elements_cdf[idx - 1];
Expand Down

0 comments on commit cc744c8

Please sign in to comment.