From 42b1a7d4c660a4304ae8e6e45abc60824211917a Mon Sep 17 00:00:00 2001 From: Aras Pranckevicius Date: Mon, 10 Oct 2022 10:10:33 +0300 Subject: OBJ: add global scale factor import setting Requested in D16095 proposal - also USD & Alembic have import scale option; OBJ has an export scale object but the import scale was not there for some reason. --- source/blender/io/wavefront_obj/IO_wavefront_obj.h | 3 ++- source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc | 6 +++--- source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc | 4 ++-- source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh | 2 +- source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc | 4 ++-- source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh | 2 +- source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc | 3 +++ source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc | 4 ++-- source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh | 2 +- source/blender/io/wavefront_obj/tests/obj_importer_tests.cc | 1 + 10 files changed, 18 insertions(+), 13 deletions(-) (limited to 'source/blender/io') diff --git a/source/blender/io/wavefront_obj/IO_wavefront_obj.h b/source/blender/io/wavefront_obj/IO_wavefront_obj.h index 0a92bbca477..cf6464eeb37 100644 --- a/source/blender/io/wavefront_obj/IO_wavefront_obj.h +++ b/source/blender/io/wavefront_obj/IO_wavefront_obj.h @@ -35,7 +35,7 @@ struct OBJExportParams { /* Geometry Transform options. */ eIOAxis forward_axis; eIOAxis up_axis; - float scaling_factor; + float global_scale; /* File Write Options. */ bool export_selected_objects; @@ -65,6 +65,7 @@ struct OBJImportParams { char filepath[FILE_MAX]; /** Value 0 disables clamping. */ float clamp_size; + float global_scale; eIOAxis forward_axis; eIOAxis up_axis; bool import_vertex_groups; diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc index 95be927589e..5c81cf7abca 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc @@ -261,7 +261,7 @@ void OBJWriter::write_vertex_coords(FormatHandler &fh, BLI_assert(tot_count == attribute.size()); obj_parallel_chunked_output(fh, tot_count, [&](FormatHandler &buf, int i) { - float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.scaling_factor); + float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.global_scale); ColorGeometry4f linear = attribute.get(i); float srgb[3]; linearrgb_to_srgb_v3_v3(srgb, linear); @@ -270,7 +270,7 @@ void OBJWriter::write_vertex_coords(FormatHandler &fh, } else { obj_parallel_chunked_output(fh, tot_count, [&](FormatHandler &buf, int i) { - float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.scaling_factor); + float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.global_scale); buf.write_obj_vertex(vertex[0], vertex[1], vertex[2]); }); } @@ -435,7 +435,7 @@ void OBJWriter::write_nurbs_curve(FormatHandler &fh, const OBJCurve &obj_nurbs_d const int total_vertices = obj_nurbs_data.total_spline_vertices(spline_idx); for (int vertex_idx = 0; vertex_idx < total_vertices; vertex_idx++) { const float3 vertex_coords = obj_nurbs_data.vertex_coordinates( - spline_idx, vertex_idx, export_params_.scaling_factor); + spline_idx, vertex_idx, export_params_.global_scale); fh.write_obj_vertex(vertex_coords[0], vertex_coords[1], vertex_coords[2]); } diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc index 9f19a6390d3..d00c09b9013 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc @@ -265,13 +265,13 @@ const char *OBJMesh::get_object_material_name(const int16_t mat_nr) const return mat->id.name + 2; } -float3 OBJMesh::calc_vertex_coords(const int vert_index, const float scaling_factor) const +float3 OBJMesh::calc_vertex_coords(const int vert_index, const float global_scale) const { float3 r_coords; const Span verts = export_mesh_eval_->verts(); copy_v3_v3(r_coords, verts[vert_index].co); mul_m4_v3(world_and_axes_transform_, r_coords); - mul_v3_fl(r_coords, scaling_factor); + mul_v3_fl(r_coords, global_scale); return r_coords; } diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh index db29f5651ed..ec98468e2de 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh +++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh @@ -161,7 +161,7 @@ class OBJMesh : NonCopyable { /** * Calculate coordinates of the vertex at the given index. */ - float3 calc_vertex_coords(int vert_index, float scaling_factor) const; + float3 calc_vertex_coords(int vert_index, float global_scale) const; /** * Calculate vertex indices of all vertices of the polygon at the given index. */ diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc index 172a59e5341..812c3e7b5d4 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc @@ -55,14 +55,14 @@ int OBJCurve::total_spline_vertices(const int spline_index) const float3 OBJCurve::vertex_coordinates(const int spline_index, const int vertex_index, - const float scaling_factor) const + const float global_scale) const { const Nurb *const nurb = static_cast(BLI_findlink(&export_curve_->nurb, spline_index)); float3 r_coord; const BPoint &bpoint = nurb->bp[vertex_index]; copy_v3_v3(r_coord, bpoint.vec); mul_m4_v3(world_axes_transform_, r_coord); - mul_v3_fl(r_coord, scaling_factor); + mul_v3_fl(r_coord, global_scale); return r_coord; } diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh index 65389d44f59..3f93112200f 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh +++ b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh @@ -37,7 +37,7 @@ class OBJCurve : NonCopyable { /** * Get coordinates of the vertex at the given index on the given spline. */ - float3 vertex_coordinates(int spline_index, int vertex_index, float scaling_factor) const; + float3 vertex_coordinates(int spline_index, int vertex_index, float global_scale) const; /** * Get total control points of the NURBS spline at the given index. This is different than total * vertices of a spline. diff --git a/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc b/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc index f33753d720d..204237088ab 100644 --- a/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc +++ b/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc @@ -103,6 +103,9 @@ void transform_object(Object *object, const OBJImportParams &import_params) IO_AXIS_Y, IO_AXIS_Z, import_params.forward_axis, import_params.up_axis, axes_transform); copy_m4_m3(obmat, axes_transform); + float scale_vec[3] = { + import_params.global_scale, import_params.global_scale, import_params.global_scale}; + rescale_m4(obmat, scale_vec); BKE_object_apply_mat4(object, obmat, true, false); if (import_params.clamp_size != 0.0f) { diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc index dcba78ac99e..5de3cdcd851 100644 --- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc +++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc @@ -306,7 +306,7 @@ TEST_F(obj_exporter_regression_test, all_tris) TEST_F(obj_exporter_regression_test, all_quads) { OBJExportParamsDefault _export; - _export.params.scaling_factor = 2.0f; + _export.params.global_scale = 2.0f; _export.params.export_materials = false; compare_obj_export_to_golden( "io_tests/blend_geometry/all_quads.blend", "io_tests/obj/all_quads.obj", "", _export.params); @@ -429,7 +429,7 @@ TEST_F(obj_exporter_regression_test, cubes_positioned) { OBJExportParamsDefault _export; _export.params.export_materials = false; - _export.params.scaling_factor = 2.0f; + _export.params.global_scale = 2.0f; compare_obj_export_to_golden("io_tests/blend_geometry/cubes_positioned.blend", "io_tests/obj/cubes_positioned.obj", "", diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh index 006d86312b6..a4d452e1309 100644 --- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh +++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh @@ -19,7 +19,7 @@ struct OBJExportParamsDefault { params.forward_axis = IO_AXIS_NEGATIVE_Z; params.up_axis = IO_AXIS_Y; - params.scaling_factor = 1.f; + params.global_scale = 1.f; params.apply_modifiers = true; params.export_eval_mode = DAG_EVAL_VIEWPORT; diff --git a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc index 1657bf81dc7..f459e1ab1bd 100644 --- a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc +++ b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc @@ -60,6 +60,7 @@ class obj_importer_test : public BlendfileLoadingBaseTest { } OBJImportParams params; + params.global_scale = 1.0f; params.clamp_size = 0; params.forward_axis = IO_AXIS_NEGATIVE_Z; params.up_axis = IO_AXIS_Y; -- cgit v1.2.3