[Bf-blender-cvs] [a6ff8534f25] soc-2020-io-performance: fix smooth groups calculation

Ankit Meel noreply at git.blender.org
Tue Sep 1 00:07:48 CEST 2020


Commit: a6ff8534f25b439df585575a79320bd01af991c1
Author: Ankit Meel
Date:   Tue Sep 1 01:37:48 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBa6ff8534f25b439df585575a79320bd01af991c1

fix smooth groups calculation

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

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
M	source/blender/io/wavefront_obj/intern/obj_exporter.cc

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

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 237fa6f3550..795034edcd6 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
@@ -190,18 +190,15 @@ void OBJWriter::write_uv_coords(OBJMesh &obj_mesh_data, Vector<Vector<uint>> &uv
 void OBJWriter::write_poly_normals(OBJMesh &obj_mesh_data) const
 {
   obj_mesh_data.ensure_mesh_normals();
-  obj_mesh_data.calc_smooth_groups();
-  if (obj_mesh_data.tot_smooth_groups() > 0) {
-    float vertex_normal[3];
+  if (export_params_.export_smooth_groups && obj_mesh_data.tot_smooth_groups() > 0) {
     for (uint i = 0; i < obj_mesh_data.tot_vertices(); i++) {
-      obj_mesh_data.calc_vertex_normal(i, vertex_normal);
+      float3 vertex_normal = obj_mesh_data.calc_vertex_normal(i);
       fprintf(outfile_, "vn %f %f %f\n", vertex_normal[0], vertex_normal[1], vertex_normal[2]);
     }
   }
   else {
-    float poly_normal[3];
     for (uint i = 0; i < obj_mesh_data.tot_polygons(); i++) {
-      obj_mesh_data.calc_poly_normal(i, poly_normal);
+      float3 poly_normal = obj_mesh_data.calc_poly_normal(i);
       fprintf(outfile_, "vn %f %f %f\n", poly_normal[0], poly_normal[1], poly_normal[2]);
     }
   }
@@ -215,7 +212,7 @@ void OBJWriter::write_smooth_group(const OBJMesh &obj_mesh_data,
                                    const uint poly_index,
                                    int &r_last_face_smooth_group) const
 {
-  if (!export_params_.export_smooth_groups || !obj_mesh_data.tot_smooth_groups()) {
+  if (!export_params_.export_smooth_groups || obj_mesh_data.tot_smooth_groups() <= 0) {
     return;
   }
   if (obj_mesh_data.get_ith_poly(poly_index).flag & ME_SMOOTH) {
@@ -418,7 +415,10 @@ void OBJWriter::update_index_offsets(const OBJMesh &obj_mesh_data)
 {
   index_offset_[VERTEX_OFF] += obj_mesh_data.tot_vertices();
   index_offset_[UV_VERTEX_OFF] += obj_mesh_data.tot_uv_vertices();
-  index_offset_[NORMAL_OFF] += obj_mesh_data.tot_normals();
+  index_offset_[NORMAL_OFF] += (export_params_.export_smooth_groups &&
+                                obj_mesh_data.tot_smooth_groups()) ?
+                                   obj_mesh_data.tot_vertices() :
+                                   obj_mesh_data.tot_polygons();
 }
 
 /**
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 214cf734e50..82dea0a4efb 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -164,13 +164,6 @@ uint OBJMesh::tot_edges() const
   return export_mesh_eval_->totedge;
 }
 
-uint OBJMesh::tot_normals() const
-{
-  /* Calculate smooth groups first. Or use total polygons if suitable. */
-  BLI_assert(poly_smooth_groups_);
-  return tot_smooth_groups_ > 0 ? export_mesh_eval_->totvert : export_mesh_eval_->totpoly;
-}
-
 /**
  * Total materials in the object to export.
  */
@@ -184,6 +177,7 @@ short OBJMesh::tot_col() const
  */
 uint OBJMesh::tot_smooth_groups() const
 {
+  BLI_assert(tot_smooth_groups_ == -1);
   return tot_smooth_groups_;
 }
 
@@ -344,13 +338,15 @@ void OBJMesh::store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coo
 /**
  * Calculate face normal of a polygon at given index.
  */
-void OBJMesh::calc_poly_normal(const uint poly_index, float r_poly_normal[3]) const
+float3 OBJMesh::calc_poly_normal(const uint poly_index) const
 {
+  float3 r_poly_normal;
   const MPoly &poly_to_write = export_mesh_eval_->mpoly[poly_index];
   const MLoop &mloop = export_mesh_eval_->mloop[poly_to_write.loopstart];
   const MVert &mvert = *(export_mesh_eval_->mvert);
   BKE_mesh_calc_poly_normal(&poly_to_write, &mloop, &mvert, r_poly_normal);
   mul_mat3_m4_v3(world_and_axes_transform_, r_poly_normal);
+  return r_poly_normal;
 }
 
 /**
@@ -358,10 +354,12 @@ void OBJMesh::calc_poly_normal(const uint poly_index, float r_poly_normal[3]) co
  *
  * Should be used when a mesh is shaded smooth.
  */
-void OBJMesh::calc_vertex_normal(const uint vert_index, float r_vertex_normal[3]) const
+float3 OBJMesh::calc_vertex_normal(const uint vert_index) const
 {
+  float3 r_vertex_normal;
   normal_short_to_float_v3(r_vertex_normal, export_mesh_eval_->mvert[vert_index].no);
   mul_mat3_m4_v3(world_and_axes_transform_, r_vertex_normal);
+  return r_vertex_normal;
 }
 
 /**
@@ -370,7 +368,7 @@ void OBJMesh::calc_vertex_normal(const uint vert_index, float r_vertex_normal[3]
 void OBJMesh::calc_poly_normal_indices(const uint poly_index, Vector<uint> &r_normal_indices) const
 {
   r_normal_indices.resize(export_mesh_eval_->mpoly[poly_index].totloop);
-  if (tot_smooth_groups_ > 0) {
+  if (export_params_.export_smooth_groups && tot_smooth_groups_ > 0) {
     const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index];
     const MLoop *mloop = &export_mesh_eval_->mloop[mpoly.loopstart];
     for (int i = 0; i < r_normal_indices.size(); i++) {
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 c86885d1321..18b901a729c 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
@@ -59,7 +59,7 @@ class OBJMesh : NonMovable, NonCopyable {
   /**
    * Total smooth groups in an object.
    */
-  uint tot_smooth_groups_ = 0;
+  uint tot_smooth_groups_ = -1;
   /**
    * Smooth group of all the polygons. 0 if the polygon is not shaded smooth.
    */
@@ -73,7 +73,6 @@ class OBJMesh : NonMovable, NonCopyable {
   uint tot_polygons() const;
   uint tot_uv_vertices() const;
   uint tot_edges() const;
-  uint tot_normals() const;
   short tot_col() const;
   uint tot_smooth_groups() const;
   int ith_smooth_group(int poly_index) const;
@@ -92,8 +91,8 @@ class OBJMesh : NonMovable, NonCopyable {
   void calc_poly_vertex_indices(const uint poly_index, Vector<uint> &r_poly_vertex_indices) const;
   void store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords,
                                    Vector<Vector<uint>> &r_uv_indices);
-  void calc_poly_normal(const uint poly_index, float r_poly_normal[3]) const;
-  void calc_vertex_normal(const uint vert_index, float r_vertex_normal[3]) const;
+  float3 calc_poly_normal(const uint poly_index) const;
+  float3 calc_vertex_normal(const uint vert_index) const;
   void calc_poly_normal_indices(const uint poly_index, Vector<uint> &r_normal_indices) const;
   const char *get_poly_deform_group_name(const MPoly &mpoly, short &r_last_vertex_group) const;
   Array<int, 2> calc_edge_vert_indices(const uint edge_index) const;
diff --git a/source/blender/io/wavefront_obj/intern/obj_exporter.cc b/source/blender/io/wavefront_obj/intern/obj_exporter.cc
index 1f5451376de..32f9947f3f8 100644
--- a/source/blender/io/wavefront_obj/intern/obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_exporter.cc
@@ -133,6 +133,9 @@ static void export_frame(ViewLayer *view_layer,
 
     if (mesh_to_export->tot_polygons() > 0) {
       Vector<Vector<uint>> uv_indices;
+      if (export_params.export_smooth_groups) {
+        mesh_to_export->calc_smooth_groups();
+      }
       if (export_params.export_normals) {
         frame_writer.write_poly_normals(*mesh_to_export);
       }



More information about the Bf-blender-cvs mailing list