Modifiers: ported Collision DerivedMesh → Mesh

This commit is contained in:
Sybren A. Stüvel 2018-05-17 15:26:59 +02:00
parent c48a3afb16
commit a2438373b3
3 changed files with 57 additions and 21 deletions

View File

@ -54,6 +54,7 @@ struct MLoop;
struct MFace;
struct MEdge;
struct MVert;
struct MVertTri;
struct MDeformVert;
struct MDisps;
struct Object;
@ -201,6 +202,11 @@ bool BKE_mesh_runtime_clear_edit_data(struct Mesh *mesh);
void BKE_mesh_runtime_clear_geometry(struct Mesh *mesh);
void BKE_mesh_runtime_clear_cache(struct Mesh *mesh);
void BKE_mesh_runtime_verttri_from_looptri(
struct MVertTri *r_verttri,
const struct MLoop *mloop, const struct MLoopTri *looptri, int looptri_num);
/* *** mesh_evaluate.c *** */
void BKE_mesh_calc_normals_mapping(

View File

@ -144,6 +144,19 @@ const MLoopTri *BKE_mesh_runtime_looptri_ensure(Mesh *mesh)
return looptri;
}
/* This is a copy of DM_verttri_from_looptri(). */
void BKE_mesh_runtime_verttri_from_looptri(MVertTri *r_verttri, const MLoop *mloop,
const MLoopTri *looptri, int looptri_num)
{
int i;
for (i = 0; i < looptri_num; i++) {
r_verttri[i].tri[0] = mloop[looptri[i].tri[0]].v;
r_verttri[i].tri[1] = mloop[looptri[i].tri[1]].v;
r_verttri[i].tri[2] = mloop[looptri[i].tri[2]].v;
}
}
bool BKE_mesh_runtime_ensure_edit_data(struct Mesh *mesh)
{
if (mesh->runtime.edit_data != NULL) {

View File

@ -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,