[Bf-blender-cvs] [0f99e37602c] soc-2020-io-performance: Add polygon normal indices calculation method to OBJMesh.

Ankit Meel noreply at git.blender.org
Wed Sep 16 13:05:54 CEST 2020


Commit: 0f99e37602c25d3894d8eee0f2975d87a72d0f79
Author: Ankit Meel
Date:   Wed Sep 16 14:38:22 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB0f99e37602c25d3894d8eee0f2975d87a72d0f79

Add polygon normal indices calculation method to OBJMesh.

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

M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
M	source/blender/io/wavefront_obj/intern/obj_export_mesh.hh

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

diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
index e02096046b4..71af2cd1f2a 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
@@ -321,29 +321,20 @@ void OBJWriter::write_poly_elements(const OBJMesh &obj_mesh_data)
   }
 
   Vector<uint> vertex_indices;
-  Vector<uint> normal_indices;
-  /* Reset for every object. */
-  tot_normals_ = 0;
-  for (uint i = 0; i < obj_mesh_data.tot_polygons(); i++) {
     const int totloop = obj_mesh_data.ith_poly_totloop(i);
-    normal_indices.resize(totloop);
     vertex_indices.resize(totloop);
     obj_mesh_data.calc_poly_vertex_indices(i, vertex_indices);
 
-    if (obj_mesh_data.is_ith_poly_smooth(i)) {
-      for (int j = 0; j < totloop; j++) {
-        /* This is guaranteed because normals are written
-         * by going over `MLoop`s in the same order. */
-        normal_indices[j] = tot_normals_ + j + 1;
-      }
-      tot_normals_ += totloop;
-    }
-    else {
-      for (int j = 0; j < totloop; j++) {
-        normal_indices[j] = tot_normals_ + 1;
-      }
-      tot_normals_ += 1;
-    }
+  Vector<uint> face_normal_indices;
+  /* Reset for every Object. */
+  per_object_tot_normals_ = 0;
+  const int tot_polygons = obj_mesh_data.tot_polygons();
+  for (uint i = 0; i < tot_polygons; i++) {
+    /* For an Object, a normal index depends on how many have been written before it.
+     * This is unknown because of smooth shading. So pass "per object total normals"
+     * and update it after each call. */
+    per_object_tot_normals_ += obj_mesh_data.calc_poly_normal_indices(
+        i, per_object_tot_normals_, face_normal_indices);
 
     write_smooth_group(obj_mesh_data, i, last_face_smooth_group);
     write_vertex_group(obj_mesh_data, i, last_face_vertex_group);
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
index cb6b0da3a14..22bef835171 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -397,6 +397,35 @@ void OBJMesh::calc_loop_normals(const uint poly_index, Vector<float3> &r_loop_no
 
 /**
  * Find the name of the vertex group with the maximum number of vertices in a poly.
+ * Calculate a polygon's face/loop normal indices.
+ * \param Number of normals of this Object written so far.
+ * \return Number of distinct normal indices.
+ */
+int OBJMesh::calc_poly_normal_indices(const uint poly_index,
+                                      const int object_tot_prev_normals,
+                                      Vector<uint> &r_face_normal_indices) const
+{
+  const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index];
+  const int totloop = mpoly.totloop;
+  r_face_normal_indices.resize(totloop);
+
+  if (is_ith_poly_smooth(poly_index)) {
+    for (int face_loop_index = 0; face_loop_index < totloop; face_loop_index++) {
+      /* Using face loop index is fine because face/loop normals and their normal indices are
+       * written by looping over `Mpoly`s/`MLoop`s in the same order. */
+      r_face_normal_indices[face_loop_index] = object_tot_prev_normals + face_loop_index;
+    }
+    /* For a smooth-shaded face, `Mesh.totloop`-many loop normals are written. */
+    return totloop;
+  }
+  else {
+    for (int face_loop_index = 0; face_loop_index < totloop; face_loop_index++) {
+      r_face_normal_indices[face_loop_index] = object_tot_prev_normals;
+    }
+    /* For a flat-shaded face, one face normal is written.  */
+    return 1;
+  }
+}
  *
  * If no vertex belongs to any group, returned name is "off".
  * If two or more groups have the same number of vertices (maximum), group name depends on the
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
index 636992abba2..66d3efa0e6a 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
@@ -114,6 +114,9 @@ class OBJMesh : NonMovable, NonCopyable {
   void store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords);
   float3 calc_poly_normal(const uint poly_index) const;
   const char *get_poly_deform_group_name(const uint poly_index, short &r_last_vertex_group) const;
+  int calc_poly_normal_indices(const uint poly_index,
+                               const int object_tot_prev_normals,
+                               Vector<uint> &r_face_normal_indices) const;
   void calc_loop_normals(const uint poly_index, Vector<float3> &r_loop_normals) const;
 
   std::optional<std::array<int, 2>> calc_loose_edge_vert_indices(const uint edge_index) const;



More information about the Bf-blender-cvs mailing list