[Bf-blender-cvs] [501ead4bbc8] soc-2020-io-performance: Exporter: fix vertex normals not being exported

Ankit Meel noreply at git.blender.org
Thu Jul 16 12:34:22 CEST 2020


Commit: 501ead4bbc8a75bf2e49f1ff8b8e830f067b9fd4
Author: Ankit Meel
Date:   Thu Jul 16 13:15:06 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB501ead4bbc8a75bf2e49f1ff8b8e830f067b9fd4

Exporter: fix vertex normals not being exported

The smooth group calculation must be done before normals are
written, to decide between vertex normals and face normals.
So moved the smooth group array in the OBJMesh's private members.

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

M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.hh

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

diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
index 64a709171f3..2f83870f0a3 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
@@ -186,6 +186,7 @@ void OBJWriter::write_uv_coords(OBJMesh &obj_mesh_data, Vector<Vector<uint>> &uv
 void OBJWriter::write_poly_normals(OBJMesh &obj_mesh_data)
 {
   obj_mesh_data.ensure_mesh_normals();
+  obj_mesh_data.calc_smooth_groups();
   if (export_params_.export_smooth_groups && obj_mesh_data.tot_smooth_groups() > 0) {
     float vertex_normal[3];
     for (uint i = 0; i < obj_mesh_data.tot_vertices(); i++) {
@@ -208,14 +209,13 @@ void OBJWriter::write_poly_normals(OBJMesh &obj_mesh_data)
  */
 void OBJWriter::write_smooth_group(OBJMesh &obj_mesh_data,
                                    int &r_last_face_smooth_group,
-                                   const int *poly_smooth_groups,
                                    uint poly_index)
 {
-  if (!export_params_.export_smooth_groups || !poly_smooth_groups) {
+  if (!export_params_.export_smooth_groups || !obj_mesh_data.tot_smooth_groups()) {
     return;
   }
   if ((obj_mesh_data.get_ith_poly(poly_index).flag & ME_SMOOTH) == true) {
-    int curr_group = poly_smooth_groups[poly_index];
+    int curr_group = obj_mesh_data.ith_smooth_group(poly_index);
     if (curr_group == r_last_face_smooth_group) {
       return;
     }
@@ -297,8 +297,8 @@ void OBJWriter::write_poly_elements(OBJMesh &obj_mesh_data, Span<Vector<uint>> u
 {
   Vector<uint> vertex_indices;
   Vector<uint> normal_indices;
-  int *poly_smooth_groups = obj_mesh_data.calc_smooth_groups();
 
+  /* -1 has no significant value, it could have been any negative number. */
   int last_face_smooth_group = -1;
   /* -1 is used for a face having no vertex group. It could have been any _other_ negative
    * number. */
@@ -314,7 +314,7 @@ void OBJWriter::write_poly_elements(OBJMesh &obj_mesh_data, Span<Vector<uint>> u
         obj_mesh_data.calc_poly_normal_indices(normal_indices, i);
         const MPoly &poly_to_write = obj_mesh_data.get_ith_poly(i);
 
-        write_smooth_group(obj_mesh_data, last_face_smooth_group, poly_smooth_groups, i);
+        write_smooth_group(obj_mesh_data, last_face_smooth_group, i);
         write_vertex_group(obj_mesh_data, last_face_vertex_group, i);
         write_poly_material(obj_mesh_data, last_face_mat_nr, i);
         write_vert_uv_normal_indices(vertex_indices, uv_indices[i], normal_indices, poly_to_write);
@@ -327,7 +327,7 @@ void OBJWriter::write_poly_elements(OBJMesh &obj_mesh_data, Span<Vector<uint>> u
         obj_mesh_data.calc_poly_normal_indices(normal_indices, i);
         const MPoly &poly_to_write = obj_mesh_data.get_ith_poly(i);
 
-        write_smooth_group(obj_mesh_data, last_face_smooth_group, poly_smooth_groups, i);
+        write_smooth_group(obj_mesh_data, last_face_smooth_group, i);
         write_vertex_group(obj_mesh_data, last_face_vertex_group, i);
         write_poly_material(obj_mesh_data, last_face_mat_nr, i);
         write_vert_normal_indices(vertex_indices, normal_indices, poly_to_write);
@@ -341,7 +341,7 @@ void OBJWriter::write_poly_elements(OBJMesh &obj_mesh_data, Span<Vector<uint>> u
         obj_mesh_data.calc_poly_vertex_indices(vertex_indices, i);
         const MPoly &poly_to_write = obj_mesh_data.get_ith_poly(i);
 
-        write_smooth_group(obj_mesh_data, last_face_smooth_group, poly_smooth_groups, i);
+        write_smooth_group(obj_mesh_data, last_face_smooth_group, i);
         write_vertex_group(obj_mesh_data, last_face_vertex_group, i);
         write_poly_material(obj_mesh_data, last_face_mat_nr, i);
         write_vert_uv_indices(vertex_indices, uv_indices[i], poly_to_write);
@@ -353,17 +353,13 @@ void OBJWriter::write_poly_elements(OBJMesh &obj_mesh_data, Span<Vector<uint>> u
         obj_mesh_data.calc_poly_vertex_indices(vertex_indices, i);
         const MPoly &poly_to_write = obj_mesh_data.get_ith_poly(i);
 
-        write_smooth_group(obj_mesh_data, last_face_smooth_group, poly_smooth_groups, i);
+        write_smooth_group(obj_mesh_data, last_face_smooth_group, i);
         write_vertex_group(obj_mesh_data, last_face_vertex_group, i);
         write_poly_material(obj_mesh_data, last_face_mat_nr, i);
         write_vert_indices(vertex_indices, poly_to_write);
       }
     }
   }
-
-  if (poly_smooth_groups) {
-    MEM_freeN(poly_smooth_groups);
-  }
 }
 
 /**
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
index e4de61306f2..2070a85b5a6 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
@@ -68,7 +68,6 @@ class OBJWriter {
   void write_poly_normals(OBJMesh &obj_mesh_data);
   void write_smooth_group(OBJMesh &obj_mesh_data,
                           int &r_last_face_smooth_group,
-                          const int *poly_smooth_groups,
                           uint poly_index);
   void write_poly_material(OBJMesh &obj_mesh_data, short &r_last_face_mat_nr, uint poly_index);
   void write_vertex_group(OBJMesh &obj_mesh_data,
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.cc
index 88de442b346..29e6b8d441e 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.cc
@@ -99,6 +99,9 @@ OBJMesh::~OBJMesh()
   if (mesh_eval_needs_free_) {
     BKE_id_free(NULL, export_mesh_eval_);
   }
+  if (poly_smooth_groups_) {
+    MEM_freeN(poly_smooth_groups_);
+  }
 }
 
 /**
@@ -187,11 +190,43 @@ const uint OBJMesh::tot_smooth_groups()
   return tot_smooth_groups_;
 }
 
+/**
+ * Return smooth group of the polygon at the given index.
+ */
+const int OBJMesh::ith_smooth_group(int poly_index)
+{
+  BLI_assert(poly_smooth_groups_);
+  return poly_smooth_groups_[poly_index];
+}
+
 void OBJMesh::ensure_mesh_normals()
 {
   BKE_mesh_ensure_normals(export_mesh_eval_);
 }
 
+/**
+ * Calculate smooth groups of a smooth shaded object.
+ * \return A polygon aligned array of smooth group numbers or bitflags if export
+ * settings specify so.
+ */
+void OBJMesh::calc_smooth_groups()
+{
+  if (!export_params_.export_smooth_groups) {
+    poly_smooth_groups_ = nullptr;
+  }
+  int tot_smooth_groups = 0;
+  bool use_bitflags = export_params_.smooth_groups_bitflags;
+  poly_smooth_groups_ = BKE_mesh_calc_smoothgroups(export_mesh_eval_->medge,
+                                                   export_mesh_eval_->totedge,
+                                                   export_mesh_eval_->mpoly,
+                                                   export_mesh_eval_->totpoly,
+                                                   export_mesh_eval_->mloop,
+                                                   export_mesh_eval_->totloop,
+                                                   &tot_smooth_groups,
+                                                   use_bitflags);
+  tot_smooth_groups_ = tot_smooth_groups;
+}
+
 /**
  * Return mat_nr-th material of the object.
  */
@@ -347,30 +382,6 @@ void OBJMesh::calc_poly_normal_indices(Vector<uint> &r_normal_indices, uint poly
   }
 }
 
-/**
- * Calculate smooth groups of a smooth shaded object.
- * \return A polygon aligned array of smooth group numbers or bitflags if export
- * settings specify so.
- */
-int *OBJMesh::calc_smooth_groups()
-{
-  if (!export_params_.export_smooth_groups) {
-    return nullptr;
-  }
-  int tot_smooth_groups = 0;
-  bool use_bitflags = export_params_.smooth_groups_bitflags;
-  int *groups_array = BKE_mesh_calc_smoothgroups(export_mesh_eval_->medge,
-                                                 export_mesh_eval_->totedge,
-                                                 export_mesh_eval_->mpoly,
-                                                 export_mesh_eval_->totpoly,
-                                                 export_mesh_eval_->mloop,
-                                                 export_mesh_eval_->totloop,
-                                                 &tot_smooth_groups,
-                                                 use_bitflags);
-  tot_smooth_groups_ = tot_smooth_groups;
-  return groups_array;
-}
-
 /**
  * Find the name of the vertex group with the maximum number of vertices in a poly.
  *
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.hh
index b2fe660b442..74513103dac 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mesh.hh
@@ -76,6 +76,10 @@ class OBJMesh : NonMovable, NonCopyable {
    * Total smooth groups in an object.
    */
   uint tot_smooth_groups_ = 0;
+  /**
+   * Smooth group of all the polygons. 0 if the polygon is not shaded smooth.
+   */
+  int *poly_smooth_groups_;
 
  public:
   OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Object *export_object);
@@ -87,8 +91,10 @@ class OBJMesh : NonMovable, NonCopyable {
   const uint tot_edges();
   const short tot_col();
   const uint tot_smooth_groups();
+  const int ith_smooth_group(int poly_index);
 
   void ensure_mesh_normals();
+  void calc_smooth_groups();
   Material *get_object_material(short mat_nr);
   const MPoly &get_ith_poly(uint i);
 
@@ -103,7 +109,6 @@ class OBJMesh : NonMovable, NonCopyable {
   void calc_poly_normal(float r_poly_normal[3], uint poly_index);
   void calc_vertex_normal(float r_vertex_normal[3], uint vertex_index);
   void calc_poly_normal_indices(Vector<uint> &r_normal_indices, uint poly_index);
-  int *calc_smooth_groups();
   const char *get_poly_deform_group_name(const MPoly &mpoly, short &r_last_vertex_group);
   void calc_edge_vert_indices(uint r_vert_indices[2], uint edge_index);



More information about the Bf-blender-cvs mailing list