[Bf-blender-cvs] [64c7bad3915] temp_bmesh_multires: New branch to fix multires topology editing. WIP

Joseph Eagar noreply at git.blender.org
Sun Oct 18 00:23:55 CEST 2020


Commit: 64c7bad391557b81070b112c33e22ae68b8697da
Author: Joseph Eagar
Date:   Sat Oct 17 15:11:02 2020 -0700
Branches: temp_bmesh_multires
https://developer.blender.org/rB64c7bad391557b81070b112c33e22ae68b8697da

New branch to fix multires topology editing. WIP

Changes:
* Brought back bmesh_mdisps_space_set, written from scratch using
  subdiv api, not ccg.
* Wrote a function to smooth multigres grids from within bmesh.  I might
  not need it; unless anyone thinks of a use for it I'll go ahead and
  delete it.

Todo:
* Purge code of all usages of CCG for multires.

This commit:
* Wrote a utility function to dump multires displacements into a (new)
  scene object.
* Consequently, I now know that the CCG_based multires code is not
  compatible with the OpenSubdiv based code.  The former produces gaps
  between grids when converting displacements to object space.

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

M	release/scripts/addons
M	release/scripts/addons_contrib
M	source/blender/blenkernel/BKE_multires.h
M	source/blender/blenkernel/intern/mesh_mirror.c
M	source/blender/blenkernel/intern/multires.c
M	source/blender/editors/mesh/editmesh_tools.c
M	source/blender/editors/mesh/mesh_intern.h
M	source/blender/editors/mesh/mesh_ops.c
M	source/blender/editors/uvedit/uvedit_unwrap_ops.c
M	source/blender/io/collada/collada_utils.cpp
M	source/blender/modifiers/intern/MOD_bevel.c
M	source/blender/modifiers/intern/MOD_decimate.c
M	source/blender/modifiers/intern/MOD_edgesplit.c
M	source/blender/modifiers/intern/MOD_triangulate.c
M	source/blender/modifiers/intern/MOD_wireframe.c
M	source/blender/python/bmesh/bmesh_py_types.c

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

diff --git a/release/scripts/addons b/release/scripts/addons
index 8ad9de7c1e1..a1f22920234 160000
--- a/release/scripts/addons
+++ b/release/scripts/addons
@@ -1 +1 @@
-Subproject commit 8ad9de7c1e1022dee907ddce78f4c357111fc09e
+Subproject commit a1f229202345c5e705f3035e2ff418bc3a9fe669
diff --git a/release/scripts/addons_contrib b/release/scripts/addons_contrib
index 26a8b2eadc7..eae381b6982 160000
--- a/release/scripts/addons_contrib
+++ b/release/scripts/addons_contrib
@@ -1 +1 @@
-Subproject commit 26a8b2eadc7abb2a30fac50eb5505aa24daf5785
+Subproject commit eae381b698248e70e4a3c62bdf239f9d5a0470a9
diff --git a/source/blender/blenkernel/BKE_multires.h b/source/blender/blenkernel/BKE_multires.h
index fbdfc5b76a7..3c20b29c42b 100644
--- a/source/blender/blenkernel/BKE_multires.h
+++ b/source/blender/blenkernel/BKE_multires.h
@@ -41,6 +41,7 @@ struct MultiresModifierData;
 struct Object;
 struct Scene;
 struct SubdivCCG;
+struct BMesh;
 
 struct MLoop;
 struct MLoopTri;
@@ -229,6 +230,8 @@ BLI_INLINE void BKE_multires_construct_tangent_matrix(float tangent_matrix[3][3]
                                                       const float dPdv[3],
                                                       const int corner);
 
+void BKE_multires_bmesh_space_set(struct Object *ob, struct BMesh *bm, int mode);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/intern/mesh_mirror.c b/source/blender/blenkernel/intern/mesh_mirror.c
index 54b219548f1..46764a56e60 100644
--- a/source/blender/blenkernel/intern/mesh_mirror.c
+++ b/source/blender/blenkernel/intern/mesh_mirror.c
@@ -59,7 +59,6 @@ Mesh *BKE_mesh_mirror_bisect_on_mirror_plane(MirrorModifierData *mmd,
   BMVert *v, *v_next;
 
   bm = BKE_mesh_to_bmesh_ex(mesh,
-                            NULL,
                             &(struct BMeshCreateParams){0},
                             &(struct BMeshFromMeshParams){
                                 .calc_face_normal = true,
diff --git a/source/blender/blenkernel/intern/multires.c b/source/blender/blenkernel/intern/multires.c
index 44a5febd833..09de551fec5 100644
--- a/source/blender/blenkernel/intern/multires.c
+++ b/source/blender/blenkernel/intern/multires.c
@@ -33,26 +33,32 @@
 
 #include "BLI_bitmap.h"
 #include "BLI_blenlib.h"
+#include "BLI_edgehash.h"
 #include "BLI_math.h"
 #include "BLI_task.h"
 #include "BLI_utildefines.h"
 
 #include "BKE_ccg.h"
+#include "BKE_collection.h"
+#include "BKE_layer.h"
 #include "BKE_cdderivedmesh.h"
+#include "BKE_context.h"
 #include "BKE_editmesh.h"
-#include "BKE_mesh.h"
 #include "BKE_global.h"
+#include "BKE_main.h"
+#include "BKE_mesh.h"
 #include "BKE_mesh_mapping.h"
 #include "BKE_mesh_runtime.h"
 #include "BKE_modifier.h"
 #include "BKE_multires.h"
+#include "BKE_object.h"
 #include "BKE_paint.h"
 #include "BKE_pbvh.h"
 #include "BKE_scene.h"
+#include "BKE_subdiv.h"
 #include "BKE_subdiv_ccg.h"
 #include "BKE_subdiv_eval.h"
 #include "BKE_subsurf.h"
-#include "BKE_subdiv.h"
 
 #include "BKE_object.h"
 
@@ -60,9 +66,9 @@
 
 #include "DEG_depsgraph_query.h"
 
-#include "multires_reshape.h"
-#include "multires_inline.h"
 #include "bmesh.h"
+#include "multires_inline.h"
+#include "multires_reshape.h"
 
 #include <math.h>
 #include <string.h>
@@ -962,20 +968,36 @@ void multiresModifier_subdivide_legacy(
 static void grid_tangent(const CCGKey *key, int x, int y, int axis, CCGElem *grid, float t[3])
 {
   if (axis == 0) {
-    if (x == 0) {
-      sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x+1, y), CCG_grid_elem_co(key, grid, x, y));
-    } else {
-      sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x-1, y));
+    if (x == key->grid_size - 1) {
+      if (y == key->grid_size - 1) {
+        sub_v3_v3v3(
+            t, CCG_grid_elem_co(key, grid, x, y - 1), CCG_grid_elem_co(key, grid, x - 1, y - 1));
+      }
+      else {
+        sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x - 1, y));
+      }
     }
-  } else if (axis == 1) {
-    if (y == 0) {
-        sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y+1), CCG_grid_elem_co(key, grid, x, y));
-    } else {
-      sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x, y-1));
+    else {
+      sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x + 1, y), CCG_grid_elem_co(key, grid, x, y));
+    }
+  }
+  else if (axis == 1) {
+    if (y == key->grid_size - 1) {
+      if (x == key->grid_size - 1) {
+        sub_v3_v3v3(
+            t, CCG_grid_elem_co(key, grid, x - 1, y), CCG_grid_elem_co(key, grid, x - 1, (y - 1)));
+      }
+      else {
+        sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x, (y - 1)));
+      }
+    }
+    else {
+      sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, (y + 1)), CCG_grid_elem_co(key, grid, x, y));
     }
   }
 }
 
+
 /* Construct 3x3 tangent-space matrix in 'mat' */
 static void grid_tangent_matrix(float mat[3][3], const CCGKey *key, int x, int y, CCGElem *grid)
 {
@@ -1006,9 +1028,206 @@ typedef struct MultiresThreadedData {
   float (*smat)[3];
 } MultiresThreadedData;
 
+Object *multires_dump_grids_bmesh(BMesh *bm)
+{
+  if (!CustomData_has_layer(&bm->ldata, CD_MDISPS)) {
+    printf("multires_dump_grids_bmesh: error: no multires grids\n");
+    return;
+  }
+
+  bool spaceset = false;
+
+  if (bm->multiresSpace != MULTIRES_SPACE_ABSOLUTE) {
+    BKE_multires_bmesh_space_set(NULL, bm, MULTIRES_SPACE_ABSOLUTE);
+    spaceset = true;
+  }
+
+  Main *bmain = G.main;
+  char *name = "multires_dump";
+
+  bContext *ctx = CTX_create();
+  CTX_data_main_set(ctx, bmain);
+  CTX_wm_manager_set(ctx, G.main->wm.first);
+  CTX_data_scene_set(ctx, G.main->scenes.first);
+
+  Scene *scene = CTX_data_scene(ctx);
+  ViewLayer *view_layer = CTX_data_view_layer(ctx);
+  Object *ob = BKE_object_add_only_object(bmain, OB_MESH, name);
+  Base *base;
+  LayerCollection *layer_collection;
+
+  ob->data = BKE_object_obdata_add_from_type(bmain, OB_MESH, name);
+  DEG_id_tag_update_ex(
+      bmain, &ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
+  //DEG_id_tag_update_ex(
+  //    bmain, &ob->data, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
+
+  layer_collection = BKE_layer_collection_get_active(view_layer);
+  BKE_collection_object_add(bmain, layer_collection->collection, ob);
+
+  DEG_id_type_tag(bmain, ID_OB);
+  DEG_relations_tag_update(bmain);
+  if (ob->data != NULL) {
+    DEG_id_tag_update_ex(bmain, (ID *)ob->data, ID_RECALC_EDITORS);
+  }
+
+  ob->modifiers.first = ob->modifiers.last = NULL;
+  zero_v3(ob->loc);
+
+  printf("users: %d\n", ob->id.us);
+
+  Mesh *me = ob->data;
+
+  BMIter iter;
+  BMVert *v;
+  BMEdge *e;
+  BMFace *f;
+
+  int cd_mdisp_off = CustomData_get_offset(&bm->ldata, CD_MDISPS);
+  int dimen = 0;
+
+  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
+    BMLoop *l = f->l_first;
+    MDisps *md = BM_ELEM_CD_GET_VOID_P(l, cd_mdisp_off);
+    dimen = (int)floor(sqrt(md->totdisp) + 0.00001);
+    break;
+  }
+
+  if (!dimen) {
+    printf("multires_dump_grids_bmesh: error: corrupted multires data\n");
+    return;
+  }
+
+  int totvert = bm->totloop * dimen * dimen;
+  int totface = bm->totloop * (dimen - 1) * (dimen - 1);
+  int totloop = totface * 4;
+
+  CustomData_free(&me->vdata, me->totvert);
+  CustomData_free(&me->edata, me->totedge);
+  CustomData_free(&me->fdata, me->totface);
+  CustomData_free(&me->ldata, me->totloop);
+  CustomData_free(&me->pdata, me->totpoly);
+
+  me->totvert = totvert;
+  me->totpoly = totface;
+  me->totloop = totloop;
+  me->totedge = totvert + totface;
+  me->totface = 0;
+  me->act_face = -1;
+
+  EdgeHash *eh = BLI_edgehash_new_ex("multires_dump_bmesh", me->totedge);
+
+  MVert *mvert = me->totvert ?
+                     MEM_callocN(sizeof(MVert) * me->totvert, "multires_dump_grids_bmesh.vert") :
+                     NULL;
+  MEdge *medge = me->totedge ?
+                     MEM_callocN(sizeof(MEdge) * me->totedge, "multires_dump_grids_bmesh.edge") :
+                     NULL;
+  MLoop *mloop = me->totloop ?
+                     MEM_callocN(sizeof(MLoop) * me->totloop, "multires_dump_grids_bmesh.loop") :
+                     NULL;
+  MPoly *mpoly = me->totpoly ?
+                     MEM_callocN(sizeof(MPoly) * me->totpoly, "multires_dump_grids_bmesh.poly") :
+                     NULL;
+
+  me->cd_flag = 0;
+
+  CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
+  CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
+  CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
+  CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
+
+  BKE_mesh_update_customdata_pointers(me, 0);
+
+  int loopi = 0;
+  int outli = 0;
+  int medi = 0;
+
+#define VINDEX(i, j) (loopi * dimen * dimen + ((j)*dimen + (i)))
+
+  // CustomData_daata_
+  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
+    BMLoop *l = f->l_first;
+    do {
+      MDisps *md = BM_ELEM_CD_GET_VOID_P(l, cd_mdisp_off);
+
+      for (int i = 0; i < dimen; i++) {
+        for (int j = 0; j < dimen; j++) {
+          int vidx = loopi * dimen * dimen + (j * dimen + i);
+          int idx = j * dimen + i;
+          float *co = md->disps[idx];
+
+          MVert *mv = mvert + vidx;
+          copy_v3_v3(mv->co, co);
+        }
+      }
+      
+      for (int i = 0; i < dimen-1; i++) {
+        for (int j = 0; j < dimen-1; j++) {
+          // do face
+          int fidx = loopi * (dimen - 1) * (dimen - 1) + (j * (dimen - 1) + i);
+          MPoly *mp = mpoly + fidx;
+
+          mp->totloop = 4;
+          mp->loopstart = outli;
+
+          MLoop *ml = mloop + outli;
+
+          ml[0].v = VINDEX(i, j);
+          ml[1].v = VINDEX(i, j + 1);
+          ml[2].v = VINDEX(i + 1, j + 1);
+          ml[3].v = VINDEX(i + 1, j);
+
+          for (int i2=0; i2<4; i2++) {
+            int a = ml[i2].v, b = ml[(i2+1) % 4].v;
+            int e;
+
+            if (!BLI_edgehash_haskey(eh, a, b)) {
+              BLI_edgehash_insert(eh, a, b, (void*)medi);
+              e = medi;
+
+              MEdge *med = medge + medi;
+
+              med->v1 = a;
+             

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list