From a2438373b317005c19288d183af4b08f54f80428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 17 May 2018 15:26:59 +0200 Subject: =?UTF-8?q?Modifiers:=20ported=20Collision=20DerivedMesh=20?= =?UTF-8?q?=E2=86=92=20Mesh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/blender/modifiers/intern/MOD_collision.c | 59 ++++++++++++++++--------- 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'source/blender/modifiers/intern/MOD_collision.c') diff --git a/source/blender/modifiers/intern/MOD_collision.c b/source/blender/modifiers/intern/MOD_collision.c index c9037074ecc..036954a2774 100644 --- a/source/blender/modifiers/intern/MOD_collision.c +++ b/source/blender/modifiers/intern/MOD_collision.c @@ -33,6 +33,7 @@ */ #include "DNA_object_types.h" +#include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" #include "MEM_guardedalloc.h" @@ -41,13 +42,17 @@ #include "BLI_utildefines.h" #include "BKE_collision.h" -#include "BKE_cdderivedmesh.h" #include "BKE_global.h" +#include "BKE_library.h" +#include "BKE_mesh.h" #include "BKE_modifier.h" #include "BKE_pointcache.h" #include "BKE_scene.h" #include "MOD_modifiertypes.h" +#include "MOD_util.h" + +#include "DEG_depsgraph_query.h" static void initData(ModifierData *md) { @@ -97,37 +102,48 @@ static bool dependsOnTime(ModifierData *UNUSED(md)) static void deformVerts( ModifierData *md, const ModifierEvalContext *ctx, - DerivedMesh *derivedData, + Mesh *mesh, float (*vertexCos)[3], int UNUSED(numVerts)) { CollisionModifierData *collmd = (CollisionModifierData *) md; - DerivedMesh *dm = NULL; + Mesh *mesh_src; MVert *tempVert = NULL; Object *ob = ctx->object; - /* if possible use/create DerivedMesh */ - if (derivedData) dm = CDDM_copy(derivedData); - else if (ob->type == OB_MESH) dm = CDDM_from_mesh(ob->data); - + if (mesh == NULL) { + mesh_src = get_mesh(ob, NULL, NULL, NULL, false, false); + } + else { + /* Not possible to use get_mesh() in this case as we'll modify its vertices + * and get_mesh() would return 'mesh' directly. */ + BKE_id_copy_ex( + NULL, (ID *)mesh, (ID **)&mesh_src, + LIB_ID_CREATE_NO_MAIN | + LIB_ID_CREATE_NO_USER_REFCOUNT | + LIB_ID_CREATE_NO_DEG_TAG | + LIB_ID_COPY_NO_PREVIEW, + false); + } + if (!ob->pd) { printf("CollisionModifier deformVerts: Should not happen!\n"); return; } - if (dm) { + if (mesh_src) { float current_time = 0; unsigned int mvert_num = 0; - CDDM_apply_vert_coords(dm, vertexCos); - CDDM_calc_normals(dm); + BKE_mesh_apply_vert_coords(mesh_src, vertexCos); + BKE_mesh_calc_normals(mesh_src); current_time = DEG_get_ctime(ctx->depsgraph); if (G.debug_value > 0) printf("current_time %f, collmd->time_xnew %f\n", current_time, collmd->time_xnew); - mvert_num = dm->getNumVerts(dm); + mvert_num = mesh_src->totvert; if (current_time > collmd->time_xnew) { unsigned int i; @@ -138,7 +154,7 @@ static void deformVerts( if (collmd->time_xnew == -1000) { /* first time */ - collmd->x = dm->dupVertArray(dm); /* frame start position */ + collmd->x = MEM_dupallocN(mesh_src->mvert); /* frame start position */ for (i = 0; i < mvert_num; i++) { /* we save global positions */ @@ -152,12 +168,12 @@ static void deformVerts( collmd->mvert_num = mvert_num; - collmd->tri_num = dm->getNumLoopTri(dm); { - const MLoop *mloop = dm->getLoopArray(dm); - const MLoopTri *looptri = dm->getLoopTriArray(dm); + const MLoop *mloop = mesh_src->mloop; + const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(mesh_src); + collmd->tri_num = BKE_mesh_runtime_looptri_len(mesh_src); MVertTri *tri = MEM_malloc_arrayN(collmd->tri_num, sizeof(*tri), __func__); - DM_verttri_from_looptri(tri, mloop, looptri, collmd->tri_num); + BKE_mesh_runtime_verttri_from_looptri(tri, mloop, looptri, collmd->tri_num); collmd->tri = tri; } @@ -177,7 +193,7 @@ static void deformVerts( collmd->xnew = tempVert; collmd->time_x = collmd->time_xnew; - memcpy(collmd->xnew, dm->getVertArray(dm), mvert_num * sizeof(MVert)); + memcpy(collmd->xnew, mesh_src->mvert, mvert_num * sizeof(MVert)); bool is_static = true; @@ -238,8 +254,9 @@ static void deformVerts( } } - if (dm) - dm->release(dm); + if (mesh_src != mesh) { + BKE_id_free(NULL, mesh_src); + } } @@ -253,14 +270,14 @@ ModifierTypeInfo modifierType_Collision = { /* copyData */ NULL, - /* deformVerts_DM */ deformVerts, + /* deformVerts_DM */ NULL, /* deformMatrices_DM */ NULL, /* deformVertsEM_DM */ NULL, /* deformMatricesEM_DM*/NULL, /* applyModifier_DM */ NULL, /* applyModifierEM_DM */NULL, - /* deformVerts */ NULL, + /* deformVerts */ deformVerts, /* deformMatrices */ NULL, /* deformVertsEM */ NULL, /* deformMatricesEM */ NULL, -- cgit v1.2.3