[Bf-blender-cvs] [fe3a359fb1f] soc-2020-io-performance: Support multiple smooth groups and bitflags

Ankit Meel noreply at git.blender.org
Sat Jul 4 10:36:35 CEST 2020


Commit: fe3a359fb1fff0226e8dc4afc0ff96f1e62b9856
Author: Ankit Meel
Date:   Sat Jul 4 14:06:27 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBfe3a359fb1fff0226e8dc4afc0ff96f1e62b9856

Support multiple smooth groups and bitflags

For uniformity, removed `const MPoly &mpoly` from `write_vertex_groups`
and `write_poly_material`, and replaced it with poly_index. And use
`get_ith_poly` to get the polygon needed.

Removed `is_shaded_smooth`. It was wrong to compare an object level
flag with a polygon level flag. Replaced its usage with
`tot_smooth_groups()`.

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

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/wavefront_obj_exporter_mesh.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_nurbs.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh

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

diff --git a/source/blender/editors/io/io_obj.c b/source/blender/editors/io/io_obj.c
index 2e93d6e4673..c6e7eda74f5 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -117,7 +117,8 @@ static int wm_obj_export_exec(bContext *C, wmOperator *op)
   export_params.export_object_groups = RNA_boolean_get(op->ptr, "export_object_groups");
   export_params.export_material_groups = RNA_boolean_get(op->ptr, "export_material_groups");
   export_params.export_vertex_groups = RNA_boolean_get(op->ptr, "export_vertex_groups");
-  export_params.export_smooth_group = RNA_boolean_get(op->ptr, "export_smooth_group");
+  export_params.export_smooth_groups = RNA_boolean_get(op->ptr, "export_smooth_groups");
+  export_params.smooth_groups_bitflags = RNA_boolean_get(op->ptr, "smooth_group_bitflags");
 
   OBJ_export(C, &export_params);
 
@@ -129,6 +130,7 @@ static void ui_obj_export_settings(uiLayout *layout, PointerRNA *imfptr)
   uiLayout *box;
   uiLayout *row;
   bool export_animation = RNA_boolean_get(imfptr, "export_animation");
+  bool export_smooth_groups = RNA_boolean_get(imfptr, "export_smooth_groups");
 
   box = uiLayoutBox(layout);
   row = uiLayoutRow(box, false);
@@ -197,7 +199,11 @@ static void ui_obj_export_settings(uiLayout *layout, PointerRNA *imfptr)
   uiItemR(row, imfptr, "export_vertex_groups", 0, NULL, ICON_NONE);
 
   row = uiLayoutRow(box, false);
-  uiItemR(row, imfptr, "export_smooth_group", 0, NULL, ICON_NONE);
+  uiItemR(row, imfptr, "export_smooth_groups", 0, NULL, ICON_NONE);
+
+  row = uiLayoutRow(box, false);
+  uiItemR(row, imfptr, "smooth_group_bitflags", 0, NULL, ICON_NONE);
+  uiLayoutSetEnabled(row, export_smooth_groups);
 }
 
 static void wm_obj_export_draw(bContext *UNUSED(C), wmOperator *op)
@@ -352,11 +358,17 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
       "If checked, writes 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,
-                  "export_smooth_group",
+                  "export_smooth_groups",
                   false,
                   "Export smooth groups",
                   "If checked, writes per-vertex normal instead of per-face normal if the mesh "
                   "is shaded smooth");
+  RNA_def_boolean(ot->srna,
+                  "smooth_group_bitflags",
+                  false,
+                  "Generate bitflags for smooth groups",
+                  "If true, generate bitflags for smooth groups' IDs. Generates upto 32 but "
+                  "usually much less");
 }
 
 static int wm_obj_import_invoke(bContext *C, wmOperator *op, const wmEvent *event)
diff --git a/source/blender/io/wavefront_obj/IO_wavefront_obj.h b/source/blender/io/wavefront_obj/IO_wavefront_obj.h
index 956f3c2806a..937498a9067 100644
--- a/source/blender/io/wavefront_obj/IO_wavefront_obj.h
+++ b/source/blender/io/wavefront_obj/IO_wavefront_obj.h
@@ -81,7 +81,11 @@ struct OBJExportParams {
    * Export vertex normals instead of face normals if mesh is shaded smooth and this option is
    * true.
    */
-  bool export_smooth_group;
+  bool export_smooth_groups;
+  /**
+   * If true, generate bitflags for smooth groups' IDs. Generates upto 32 but usually much less.
+   */
+  bool smooth_groups_bitflags;
 };
 
 struct OBJImportParams {
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
index d03bc7bf3c0..d5880e7f6a0 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
@@ -179,11 +179,11 @@ const uint OBJMesh::tot_col()
 }
 
 /**
- * Return true if the object is shaded smooth.
+ * Total smooth groups in the object to export.
  */
-const bool OBJMesh::is_shaded_smooth()
+const uint OBJMesh::tot_smooth_groups()
 {
-  return ((export_mesh_eval_->flag & ME_SMOOTH) == 0);
+  return tot_smooth_groups_;
 }
 
 void OBJMesh::ensure_mesh_normals()
@@ -332,7 +332,7 @@ void OBJMesh::calc_vertex_normal(float r_vertex_normal[3], uint vert_index)
 void OBJMesh::calc_poly_normal_indices(Vector<uint> &r_normal_indices, uint poly_index)
 {
   r_normal_indices.resize(export_mesh_eval_->mpoly[poly_index].totloop);
-  if (export_params_.export_smooth_group && this->is_shaded_smooth()) {
+  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 (uint i = 0; i < r_normal_indices.size(); i++) {
@@ -346,6 +346,30 @@ 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_exporter_mesh.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
index e910b1ccedf..2dcfc575831 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
@@ -72,6 +72,10 @@ class OBJMesh : NonMovable, NonCopyable {
    * Only for curve converted to meshes: total edges in a mesh.
    */
   uint tot_edges_;
+  /**
+   * Total smooth groups in an object.
+   */
+  uint tot_smooth_groups_ = 0;
 
  public:
   OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Object *export_object);
@@ -82,7 +86,7 @@ class OBJMesh : NonMovable, NonCopyable {
   const uint tot_uv_vertices();
   const uint tot_edges();
   const uint tot_col();
-  const bool is_shaded_smooth();
+  const uint tot_smooth_groups();
 
   void ensure_mesh_normals();
   Material *get_object_material(short mat_nr);
@@ -99,6 +103,7 @@ 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);
 
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_nurbs.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_nurbs.hh
index 55422ddd99e..74ffff30a31 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_nurbs.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_nurbs.hh
@@ -33,14 +33,14 @@
 
 namespace blender::io::obj {
 class OBJNurbs : NonMovable, NonCopyable {
-private:
+ private:
   Depsgraph *depsgraph_;
   Object *export_object_eval_;
   Curve *export_curve_;
 
  public:
   OBJNurbs(Depsgraph *depsgraph, Object *export_object);
-  
+
   const char *get_curve_name();
   const ListBase *curve_nurbs();
   void calc_point_coords(float r_coords[3], int point_index, const Nurb *nurb);
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
index 699ad00a812..0cae82f7204 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
@@ -186,7 +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();
-  if (export_params_.export_smooth_group && obj_mesh_data.is_shaded_smooth()) {
+  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++) {
       obj_mesh_data.calc_vertex_normal(vertex_normal, i);
@@ -203,17 +203,36 @@ void OBJWriter::write_poly_normals(OBJMesh &obj_mesh_data)
 }
 
 /**
- * Write smooth group if the object is shaded smooth and export settings specify so.
+ * Write smooth group if the polygon at given index is shaded smooth and export settings specify
+ * so. If the polygon is not shaded smooth, write "off".
  */
-void OBJWriter::write_smooth_group(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_group) {
-    if (obj_mesh_data.is_shaded_smooth()) {
-      fprintf(outfile_, "s 1\n");
+  if (!export_params_.export_smooth_groups) {
+    return;
+  }
+  if ((obj_mesh_data.get_ith_poly(poly_index).flag & ME_SMOOTH) == true) {
+    int curr_group = poly_smooth_groups[poly_index];
+    if (curr_group == r_last_face_smooth_group) {
+      return;
     }
-    else {
+    if (curr_group == 0) {
       fprintf(outfile_, "s off\n");
+      r_last_face_smooth_group = curr_group;
+     

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list