[Bf-blender-cvs] [b96aec3691d] soc-2020-io-performance: Export loop normals not vertex normals.

Ankit Meel noreply at git.blender.org
Sat Sep 5 14:14:37 CEST 2020


Commit: b96aec3691d4eddeb47b5dcdf9a9ad58501fb4ed
Author: Ankit Meel
Date:   Fri Sep 4 16:58:45 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBb96aec3691d4eddeb47b5dcdf9a9ad58501fb4ed

Export loop normals not vertex normals.

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

M	source/blender/editors/io/io_obj.c
M	source/blender/io/wavefront_obj/IO_wavefront_obj.h
M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
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/editors/io/io_obj.c b/source/blender/editors/io/io_obj.c
index 8107ccff1d6..fe1d0a98d53 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -332,7 +332,7 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
                   "export_normals",
                   true,
                   "Export Normals",
-                  "Export per-face normals if the face is flat-shaded, per-face-per-vertex "
+                  "Export per-face normals if the face is flat-shaded, per-face-per-loop "
                   "normals if smooth-shaded");
   RNA_def_boolean(ot->srna,
                   "export_materials",
@@ -368,7 +368,7 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
       "export_vertex_groups",
       false,
       "Export Vertex Groups",
-      "Write the name of the vertex group of a face. It is approximated "
+      "Export the name of the vertex group of a face. It is approximated "
       "by choosing the vertex group with the most members among the vertices of a face");
   RNA_def_boolean(
       ot->srna,
diff --git a/source/blender/io/wavefront_obj/IO_wavefront_obj.h b/source/blender/io/wavefront_obj/IO_wavefront_obj.h
index 30c40d4066e..4a2d9a10d7d 100644
--- a/source/blender/io/wavefront_obj/IO_wavefront_obj.h
+++ b/source/blender/io/wavefront_obj/IO_wavefront_obj.h
@@ -79,8 +79,7 @@ struct OBJExportParams {
   bool export_material_groups;
   bool export_vertex_groups;
   /**
-   * Export vertex normals instead of face normals if mesh is shaded smooth and this option is
-   * true.
+   * Calculate smooth groups from sharp edges.
    */
   bool export_smooth_groups;
   /**
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 424ce9e8d80..ce329e00776 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
@@ -187,19 +187,21 @@ void OBJWriter::write_uv_coords(OBJMesh &obj_mesh_data) const
 }
 
 /**
- * Write vertex normals for smooth-shaded polygons, and face normals otherwise, as vn x y z .
+ * Write loop normals for smooth-shaded polygons, and face normals otherwise, as vn x y z .
  */
 void OBJWriter::write_poly_normals(OBJMesh &obj_mesh_data) const
 {
   obj_mesh_data.ensure_mesh_normals();
+  Vector<float3> lnormals;
   for (uint i = 0; i < obj_mesh_data.tot_polygons(); i++) {
     if (obj_mesh_data.is_ith_poly_smooth(i)) {
-      for (int j = 0; j < obj_mesh_data.ith_poly_totloop(i); j++) {
-        float3 vertex_normal = obj_mesh_data.calc_vertex_normal(j);
-        fprintf(outfile_, "vn %f %f %f\n", vertex_normal[0], vertex_normal[1], vertex_normal[2]);
+      obj_mesh_data.calc_loop_normal(i, lnormals);
+      for (int j = 0; j < lnormals.size(); j++) {
+        fprintf(outfile_, "vn %f %f %f\n", lnormals[j][0], lnormals[j][1], lnormals[j][2]);
       }
     }
     else {
+      UNUSED_VARS(lnormals);
       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]);
     }
@@ -352,7 +354,8 @@ void OBJWriter::write_poly_elements(const OBJMesh &obj_mesh_data)
     write_smooth_group(obj_mesh_data, i, last_face_smooth_group);
     write_vertex_group(obj_mesh_data, i, last_face_vertex_group);
     write_poly_material(obj_mesh_data, i, last_face_mat_nr);
-    (this->*func_vert_uv_normal_indices)(vertex_indices, obj_mesh_data.uv_indices(i), normal_indices, totloop);
+    (this->*func_vert_uv_normal_indices)(
+        vertex_indices, obj_mesh_data.uv_indices(i), normal_indices, totloop);
   }
 }
 
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
index 296dd97374b..847e32d1e6e 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
@@ -48,7 +48,7 @@ class OBJWriter {
   FILE *outfile_;
   const OBJExportParams &export_params_;
   /**
-   * Vertex offset, UV vertex offset, face normal offset respetively.
+   * Vertex offset, UV vertex offset, face/loop normal offset respetively.
    */
   uint index_offset_[3] = {0, 0, 0};
   /**
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 1eb7ab614bf..d5a66afa21e 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -204,6 +204,7 @@ int OBJMesh::ith_smooth_group(const int poly_index) const
 void OBJMesh::ensure_mesh_normals() const
 {
   BKE_mesh_ensure_normals(export_mesh_eval_);
+  BKE_mesh_calc_normals_split(export_mesh_eval_);
 }
 
 void OBJMesh::ensure_mesh_edges() const
@@ -378,16 +379,22 @@ float3 OBJMesh::calc_poly_normal(const uint poly_index) const
 }
 
 /**
- * Calculate vertex normal of a vertex at the given index.
+ * Calculate loop normal of a loop at the given index.
  *
- * Should be used when a mesh is shaded smooth.
+ * Should be used when a polygon is shaded smooth.
  */
-float3 OBJMesh::calc_vertex_normal(const uint vert_index) const
+void OBJMesh::calc_loop_normal(const uint poly_index, Vector<float3> &r_loop_normals) 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;
+  r_loop_normals.clear();
+  const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index];
+  const float(*lnors)[3] = (const float(*)[3])(
+      CustomData_get_layer(&export_mesh_eval_->ldata, CD_NORMAL));
+  for (int loop_of_poly = 0; loop_of_poly < mpoly.totloop; loop_of_poly++) {
+    float3 loop_normal;
+    copy_v3_v3(loop_normal, lnors[mpoly.loopstart + loop_of_poly]);
+    mul_mat3_m4_v3(world_and_axes_transform_, loop_normal);
+    r_loop_normals.append(loop_normal);
+  }
 }
 
 /**
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 b5fc8cf980a..20f3cbdab11 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
@@ -100,7 +100,7 @@ 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);
   float3 calc_poly_normal(const uint poly_index) const;
-  float3 calc_vertex_normal(const uint vert_index) const;
+  void calc_loop_normal(const uint poly_index, Vector<float3> &r_loop_normals) const;
   const char *get_poly_deform_group_name(const uint poly_index, short &r_last_vertex_group) const;
   std::optional<std::array<int, 2>> calc_edge_vert_indices(const uint edge_index) const;



More information about the Bf-blender-cvs mailing list