Commit: 68f234b8ab2ef4f69498650ece54015d174bea07
Author: Aras Pranckevicius
Date:   Mon Aug 29 21:10:16 2022 +0300
Branches: master
https://developer.blender.org/rB68f234b8ab2ef4f69498650ece54015d174bea07

Cleanup: obj: simplify import/export syntax handling code

I want to add support for PBR materials extension to OBJ, but the way
current I/O code syntax handling was done made it quite cumbersome
to extend the number of MTL textures/parameters.

Simplify all that by removing FormatHandler template on "syntax"
that gets routed through keyword enums, and instead just have
simple `write_obj_*` and `write_mtl_*` functions.

Simplify MTLMaterial to not contain a map of textures (that is always
fully filled with all possible textures), instead now there's
a simple array. Rename `tex_map_XX` to `MTLTexMap`.

All this does not affect behavior or performance, but it does result
in 170 fewer lines of code, and saves a couple kilobytes of executable
size.

===================================================================

M       source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
M       source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh
M       source/blender/io/wavefront_obj/exporter/obj_export_io.hh
M       source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc
M       source/blender/io/wavefront_obj/exporter/obj_export_mtl.hh
M       source/blender/io/wavefront_obj/exporter/obj_exporter.cc
M       source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
M       source/blender/io/wavefront_obj/importer/obj_import_mtl.cc
M       source/blender/io/wavefront_obj/importer/obj_import_mtl.hh
M       source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
M       source/blender/io/wavefront_obj/tests/obj_mtl_parser_tests.cc

===================================================================

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 66dd71d4246..9bcc061caf7 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
@@ -44,7 +44,7 @@ static const char *DEFORM_GROUP_DISABLED = "off";
  * So an empty material name is written. */
 static const char *MATERIAL_GROUP_DISABLED = "";
 
-void OBJWriter::write_vert_uv_normal_indices(FormatHandler<eFileType::OBJ> &fh,
+void OBJWriter::write_vert_uv_normal_indices(FormatHandler &fh,
                                              const IndexOffsets &offsets,
                                              Span<int> vert_indices,
                                              Span<int> uv_indices,
@@ -57,12 +57,12 @@ void 
OBJWriter::write_vert_uv_normal_indices(FormatHandler<eFileType::OBJ> &fh,
   const int uv_offset = offsets.uv_vertex_offset + 1;
   const int normal_offset = offsets.normal_offset + 1;
   const int n = vert_indices.size();
-  fh.write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write_obj_poly_begin();
   if (!flip) {
     for (int j = 0; j < n; ++j) {
-      fh.write<eOBJSyntaxElement::vertex_uv_normal_indices>(vert_indices[j] + 
vertex_offset,
-                                                            uv_indices[j] + 
uv_offset,
-                                                            normal_indices[j] 
+ normal_offset);
+      fh.write_obj_poly_v_uv_normal(vert_indices[j] + vertex_offset,
+                                    uv_indices[j] + uv_offset,
+                                    normal_indices[j] + normal_offset);
     }
   }
   else {
@@ -71,15 +71,15 @@ void 
OBJWriter::write_vert_uv_normal_indices(FormatHandler<eFileType::OBJ> &fh,
      * then go backwards. Same logic in other write_*_indices functions below. 
*/
     for (int k = 0; k < n; ++k) {
       int j = k == 0 ? 0 : n - k;
-      fh.write<eOBJSyntaxElement::vertex_uv_normal_indices>(vert_indices[j] + 
vertex_offset,
-                                                            uv_indices[j] + 
uv_offset,
-                                                            normal_indices[j] 
+ normal_offset);
+      fh.write_obj_poly_v_uv_normal(vert_indices[j] + vertex_offset,
+                                    uv_indices[j] + uv_offset,
+                                    normal_indices[j] + normal_offset);
     }
   }
-  fh.write<eOBJSyntaxElement::poly_element_end>();
+  fh.write_obj_poly_end();
 }
 
-void OBJWriter::write_vert_normal_indices(FormatHandler<eFileType::OBJ> &fh,
+void OBJWriter::write_vert_normal_indices(FormatHandler &fh,
                                           const IndexOffsets &offsets,
                                           Span<int> vert_indices,
                                           Span<int> /*uv_indices*/,
@@ -90,24 +90,24 @@ void 
OBJWriter::write_vert_normal_indices(FormatHandler<eFileType::OBJ> &fh,
   const int vertex_offset = offsets.vertex_offset + 1;
   const int normal_offset = offsets.normal_offset + 1;
   const int n = vert_indices.size();
-  fh.write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write_obj_poly_begin();
   if (!flip) {
     for (int j = 0; j < n; ++j) {
-      fh.write<eOBJSyntaxElement::vertex_normal_indices>(vert_indices[j] + 
vertex_offset,
-                                                         normal_indices[j] + 
normal_offset);
+      fh.write_obj_poly_v_normal(vert_indices[j] + vertex_offset,
+                                 normal_indices[j] + normal_offset);
     }
   }
   else {
     for (int k = 0; k < n; ++k) {
       int j = k == 0 ? 0 : n - k;
-      fh.write<eOBJSyntaxElement::vertex_normal_indices>(vert_indices[j] + 
vertex_offset,
-                                                         normal_indices[j] + 
normal_offset);
+      fh.write_obj_poly_v_normal(vert_indices[j] + vertex_offset,
+                                 normal_indices[j] + normal_offset);
     }
   }
-  fh.write<eOBJSyntaxElement::poly_element_end>();
+  fh.write_obj_poly_end();
 }
 
-void OBJWriter::write_vert_uv_indices(FormatHandler<eFileType::OBJ> &fh,
+void OBJWriter::write_vert_uv_indices(FormatHandler &fh,
                                       const IndexOffsets &offsets,
                                       Span<int> vert_indices,
                                       Span<int> uv_indices,
@@ -118,24 +118,22 @@ void 
OBJWriter::write_vert_uv_indices(FormatHandler<eFileType::OBJ> &fh,
   const int vertex_offset = offsets.vertex_offset + 1;
   const int uv_offset = offsets.uv_vertex_offset + 1;
   const int n = vert_indices.size();
-  fh.write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write_obj_poly_begin();
   if (!flip) {
     for (int j = 0; j < n; ++j) {
-      fh.write<eOBJSyntaxElement::vertex_uv_indices>(vert_indices[j] + 
vertex_offset,
-                                                     uv_indices[j] + 
uv_offset);
+      fh.write_obj_poly_v_uv(vert_indices[j] + vertex_offset, uv_indices[j] + 
uv_offset);
     }
   }
   else {
     for (int k = 0; k < n; ++k) {
       int j = k == 0 ? 0 : n - k;
-      fh.write<eOBJSyntaxElement::vertex_uv_indices>(vert_indices[j] + 
vertex_offset,
-                                                     uv_indices[j] + 
uv_offset);
+      fh.write_obj_poly_v_uv(vert_indices[j] + vertex_offset, uv_indices[j] + 
uv_offset);
     }
   }
-  fh.write<eOBJSyntaxElement::poly_element_end>();
+  fh.write_obj_poly_end();
 }
 
-void OBJWriter::write_vert_indices(FormatHandler<eFileType::OBJ> &fh,
+void OBJWriter::write_vert_indices(FormatHandler &fh,
                                    const IndexOffsets &offsets,
                                    Span<int> vert_indices,
                                    Span<int> /*uv_indices*/,
@@ -144,27 +142,27 @@ void 
OBJWriter::write_vert_indices(FormatHandler<eFileType::OBJ> &fh,
 {
   const int vertex_offset = offsets.vertex_offset + 1;
   const int n = vert_indices.size();
-  fh.write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write_obj_poly_begin();
   if (!flip) {
     for (int j = 0; j < n; ++j) {
-      fh.write<eOBJSyntaxElement::vertex_indices>(vert_indices[j] + 
vertex_offset);
+      fh.write_obj_poly_v(vert_indices[j] + vertex_offset);
     }
   }
   else {
     for (int k = 0; k < n; ++k) {
       int j = k == 0 ? 0 : n - k;
-      fh.write<eOBJSyntaxElement::vertex_indices>(vert_indices[j] + 
vertex_offset);
+      fh.write_obj_poly_v(vert_indices[j] + vertex_offset);
     }
   }
-  fh.write<eOBJSyntaxElement::poly_element_end>();
+  fh.write_obj_poly_end();
 }
 
 void OBJWriter::write_header() const
 {
   using namespace std::string_literals;
-  FormatHandler<eFileType::OBJ> fh;
-  fh.write<eOBJSyntaxElement::string>("# Blender "s + 
BKE_blender_version_string() + "\n");
-  fh.write<eOBJSyntaxElement::string>("# www.blender.org\n");
+  FormatHandler fh;
+  fh.write_string("# Blender "s + BKE_blender_version_string());
+  fh.write_string("# www.blender.org");
   fh.write_to_file(outfile_);
 }
 
@@ -174,8 +172,8 @@ void OBJWriter::write_mtllib_name(const StringRefNull 
mtl_filepath) const
   char mtl_file_name[FILE_MAXFILE];
   char mtl_dir_name[FILE_MAXDIR];
   BLI_split_dirfile(mtl_filepath.data(), mtl_dir_name, mtl_file_name, 
FILE_MAXDIR, FILE_MAXFILE);
-  FormatHandler<eFileType::OBJ> fh;
-  fh.write<eOBJSyntaxElement::mtllib>(mtl_file_name);
+  FormatHandler fh;
+  fh.write_obj_mtllib(mtl_file_name);
   fh.write_to_file(outfile_);
 }
 
@@ -184,18 +182,17 @@ static void spaces_to_underscores(std::string &r_name)
   std::replace(r_name.begin(), r_name.end(), ' ', '_');
 }
 
-void OBJWriter::write_object_name(FormatHandler<eFileType::OBJ> &fh,
-                                  const OBJMesh &obj_mesh_data) const
+void OBJWriter::write_object_name(FormatHandler &fh, const OBJMesh 
&obj_mesh_data) const
 {
   std::string object_name = obj_mesh_data.get_object_name();
   spaces_to_underscores(object_name);
   if (export_params_.export_object_groups) {
     std::string mesh_name = obj_mesh_data.get_object_mesh_name();
     spaces_to_underscores(mesh_name);
-    fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + mesh_name);
+    fh.write_obj_group(object_name + "_" + mesh_name);
     return;
   }
-  fh.write<eOBJSyntaxElement::object_name>(object_name);
+  fh.write_obj_object(object_name);
 }
 
 /* Split up large meshes into multi-threaded jobs; each job processes
@@ -213,9 +210,7 @@ static int calc_chunk_count(int count)
  * will be written into the final /fh/ buffer at the end.
  */
 template<typename Function>
-void obj_parallel_chunked_output(FormatHandler<eFileType::OBJ> &fh,
-                                 int tot_count,
-                                 const Function &function)
+void obj_parallel_chunked_output(FormatHandler &fh, int tot_count, const 
Function &function)
 {
   if (tot_count <= 0) {
     return;
@@ -231,7 +226,7 @@ void 
obj_parallel_chunked_output(FormatHandler<eFileType::OBJ> &fh,
     return;
   }
   /* Give each chunk its own temporary output buffer, and process them in 
parallel. */
-  std::vector<FormatHandler<eFileType::OBJ>> buffers(chunk_count);
+  std::vector<FormatHandler> buffers(chunk_count);
   blender::threading::parallel_for(IndexRange(chunk_count), 1, [&](IndexRange 
range) {
     for (const int r : range) {
       int i_start = r * chunk_size;
@@ -248,7 +243,7 @@ void 
obj_parallel_chunked_output(FormatHandler<eFileType::OBJ> &fh,
   }
 }
 
-void OBJWriter::write_vertex_coords(FormatHandler<eFileType::OBJ> &fh,
+void OBJWriter::write_vertex_coords(FormatHandler &fh,
                                     const OBJMesh &obj_mesh_data,
                                     bool write_colors) const
 {
@@ -265,41 +260,40 @@ void 
OBJWriter::write_vertex_coords(FormatHandler<eFileType::OBJ> &fh,
         colors_layer->name, ATTR_DOMAIN_POINT, {0.0f, 0.0f, 0.0f, 0.0f});
 
     BLI_assert(tot_count == attribute.size());
-    obj_parallel_chunked_output(fh, tot_count, 
[&](FormatHandler<eFileType::OBJ> &buf, int i) {
+    obj_parallel_chunked_output(fh, tot_count, [&](FormatHandler &buf, int i) {
       float3 vertex = obj_me

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to