OBJ: Combine global scale parameter into transform matrix

Remove the need for the `calc_vertex_coords` function by combining
the scale into the existing transform needed to export vertex positions
in global space. This simplifies code and may slightly improve performance
(I only observed a few percent difference in the positions export, which is
not a slower part of the export).
This commit is contained in:
Hans Goudey
2024-01-25 13:54:30 -05:00
parent a08e636bbc
commit e2e4c95258
3 changed files with 24 additions and 16 deletions

View File

@@ -16,6 +16,7 @@
#include "BLI_color.hh"
#include "BLI_enumerable_thread_specific.hh"
#include "BLI_math_matrix.hh"
#include "BLI_path_util.h"
#include "BLI_task.hh"
@@ -259,6 +260,10 @@ void OBJWriter::write_vertex_coords(FormatHandler &fh,
const Mesh *mesh = obj_mesh_data.get_mesh();
const StringRef name = mesh->active_color_attribute;
const float4x4 transform = obj_mesh_data.get_world_axes_transform();
const Span<float3> positions = obj_mesh_data.get_mesh()->vert_positions();
if (write_colors && !name.is_empty()) {
const bke::AttributeAccessor attributes = mesh->attributes();
const VArray<ColorGeometry4f> attribute = *attributes.lookup_or_default<ColorGeometry4f>(
@@ -266,7 +271,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_.global_scale);
const float3 vertex = math::transform_point(transform, positions[i]);
ColorGeometry4f linear = attribute.get(i);
float srgb[3];
linearrgb_to_srgb_v3_v3(srgb, linear);
@@ -275,7 +280,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_.global_scale);
const float3 vertex = math::transform_point(transform, positions[i]);
buf.write_obj_vertex(vertex[0], vertex[1], vertex[2]);
});
}

View File

@@ -17,6 +17,7 @@
#include "BLI_listbase.h"
#include "BLI_map.hh"
#include "BLI_math_matrix.h"
#include "BLI_math_matrix.hh"
#include "BLI_math_rotation.h"
#include "BLI_sort.hh"
@@ -69,7 +70,8 @@ OBJMesh::OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Obj
this->materials[i] = BKE_object_material_get_eval(obj_eval, i + 1);
}
set_world_axes_transform(*obj_eval, export_params.forward_axis, export_params.up_axis);
set_world_axes_transform(
*obj_eval, export_params.forward_axis, export_params.up_axis, export_params.global_scale);
}
/**
@@ -145,7 +147,8 @@ void OBJMesh::triangulate_mesh_eval()
void OBJMesh::set_world_axes_transform(const Object &obj_eval,
const eIOAxis forward,
const eIOAxis up)
const eIOAxis up,
const float global_scale)
{
float3x3 axes_transform;
/* +Y-forward and +Z-up are the default Blender axis settings. */
@@ -158,6 +161,9 @@ void OBJMesh::set_world_axes_transform(const Object &obj_eval,
world_and_axes_transform_.location() = axes_transform * object_to_world.location();
world_and_axes_transform_[3][3] = object_to_world[3][3];
world_and_axes_transform_ = math::from_scale<float4x4>(float3(global_scale)) *
world_and_axes_transform_;
/* Normals need inverse transpose of the regular matrix to handle non-uniform scale. */
world_and_axes_normal_transform_ = math::transpose(math::invert(transform));
@@ -257,13 +263,6 @@ const char *OBJMesh::get_object_mesh_name() const
return export_mesh_->id.name + 2;
}
float3 OBJMesh::calc_vertex_coords(const int vert_index, const float global_scale) const
{
const float3 coords = math::transform_point(world_and_axes_transform_,
mesh_positions_[vert_index]);
return coords * global_scale;
}
Span<int> OBJMesh::calc_poly_vertex_indices(const int face_index) const
{
return mesh_corner_verts_.slice(mesh_faces_[face_index]);

View File

@@ -133,10 +133,11 @@ class OBJMesh : NonCopyable {
*/
const char *get_object_mesh_name() const;
/**
* Calculate coordinates of the vertex at the given index.
*/
float3 calc_vertex_coords(int vert_index, float global_scale) const;
const float4x4 &get_world_axes_transform() const
{
return world_and_axes_transform_;
}
/**
* Calculate vertex indices of all vertices of the polygon at the given index.
*/
@@ -222,6 +223,9 @@ class OBJMesh : NonCopyable {
/**
* Set the final transform after applying axes settings and an Object's world transform.
*/
void set_world_axes_transform(const Object &obj_eval, eIOAxis forward, eIOAxis up);
void set_world_axes_transform(const Object &obj_eval,
eIOAxis forward,
eIOAxis up,
float global_scale);
};
} // namespace blender::io::obj