Subdiv: Add vertex deformation callback to multires/subsurf
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 11 Sep 2019 09:10:46 +0000 (11:10 +0200)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 19 Sep 2019 09:51:25 +0000 (11:51 +0200)
Currently unused but the intention is to use this to hook up these
modifiers to a generic deformed PBVH to make it easier to sculpt or
paint on a subdivided mesh.

source/blender/modifiers/intern/MOD_multires.c
source/blender/modifiers/intern/MOD_subsurf.c

index dd7c001931c6801a4115fa7bd30f15a84d2e3b6a..53bb579128af89473c72ad94b719f6714320cd36 100644 (file)
@@ -38,6 +38,7 @@
 #include "BKE_paint.h"
 #include "BKE_subdiv.h"
 #include "BKE_subdiv_ccg.h"
+#include "BKE_subdiv_deform.h"
 #include "BKE_subdiv_mesh.h"
 #include "BKE_subsurf.h"
 
@@ -227,6 +228,36 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes
   return result;
 }
 
+static void deformVerts(ModifierData *md,
+                        const ModifierEvalContext *UNUSED(ctx),
+                        Mesh *mesh,
+                        float (*vertex_cos)[3],
+                        int num_verts)
+{
+#if !defined(WITH_OPENSUBDIV)
+  modifier_setError(md, "Disabled, built without OpenSubdiv");
+  return;
+#endif
+  MultiresModifierData *mmd = (MultiresModifierData *)md;
+  SubdivSettings subdiv_settings;
+  BKE_multires_subdiv_settings_init(&subdiv_settings, mmd);
+  if (subdiv_settings.level == 0) {
+    return;
+  }
+  BKE_subdiv_settings_validate_for_mesh(&subdiv_settings, mesh);
+  MultiresRuntimeData *runtime_data = multires_ensure_runtime(mmd);
+  Subdiv *subdiv = subdiv_descriptor_ensure(mmd, &subdiv_settings, mesh);
+  if (subdiv == NULL) {
+    /* Happens on bad topology, ut also on empty input mesh. */
+    return;
+  }
+  BKE_subdiv_displacement_attach_from_multires(subdiv, mesh, mmd);
+  BKE_subdiv_deform_coarse_vertices(subdiv, mesh, vertex_cos, num_verts);
+  if (subdiv != runtime_data->subdiv) {
+    BKE_subdiv_free(subdiv);
+  }
+}
+
 ModifierTypeInfo modifierType_Multires = {
     /* name */ "Multires",
     /* structName */ "MultiresModifierData",
@@ -237,7 +268,7 @@ ModifierTypeInfo modifierType_Multires = {
 
     /* copyData */ copyData,
 
-    /* deformVerts */ NULL,
+    /* deformVerts */ deformVerts,
     /* deformMatrices */ NULL,
     /* deformVertsEM */ NULL,
     /* deformMatricesEM */ NULL,
index 08a884fa879e2494578f306bb3649a362a09c6cc..55df1b5ddda5f6cd74ce9d9e0cf3194718a58ef2 100644 (file)
@@ -35,6 +35,7 @@
 #include "BKE_scene.h"
 #include "BKE_subdiv.h"
 #include "BKE_subdiv_ccg.h"
+#include "BKE_subdiv_deform.h"
 #include "BKE_subdiv_mesh.h"
 #include "BKE_subsurf.h"
 
@@ -240,6 +241,35 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes
   return result;
 }
 
+static void deformVerts(ModifierData *md,
+                        const ModifierEvalContext *UNUSED(ctx),
+                        Mesh *mesh,
+                        float (*vertex_cos)[3],
+                        int num_verts)
+{
+#if !defined(WITH_OPENSUBDIV)
+  modifier_setError(md, "Disabled, built without OpenSubdiv");
+  return;
+#endif
+  SubsurfModifierData *smd = (SubsurfModifierData *)md;
+  SubdivSettings subdiv_settings;
+  subdiv_settings_init(&subdiv_settings, smd);
+  if (subdiv_settings.level == 0) {
+    return;
+  }
+  BKE_subdiv_settings_validate_for_mesh(&subdiv_settings, mesh);
+  SubsurfRuntimeData *runtime_data = subsurf_ensure_runtime(smd);
+  Subdiv *subdiv = subdiv_descriptor_ensure(smd, &subdiv_settings, mesh);
+  if (subdiv == NULL) {
+    /* Happens on bad topology, but also on empty input mesh. */
+    return;
+  }
+  BKE_subdiv_deform_coarse_vertices(subdiv, mesh, vertex_cos, num_verts);
+  if (subdiv != runtime_data->subdiv) {
+    BKE_subdiv_free(subdiv);
+  }
+}
+
 ModifierTypeInfo modifierType_Subsurf = {
     /* name */ "Subdivision",
     /* structName */ "SubsurfModifierData",
@@ -251,7 +281,7 @@ ModifierTypeInfo modifierType_Subsurf = {
 
     /* copyData */ copyData,
 
-    /* deformVerts */ NULL,
+    /* deformVerts */ deformVerts,
     /* deformMatrices */ NULL,
     /* deformVertsEM */ NULL,
     /* deformMatricesEM */ NULL,