Modifiers: Simple Deform & Build, DerivedMesh → Mesh

This commit introduces `EditMeshData`. The fields in this struct are
extracted from `EditDerivedBMesh` into their own struct `EditMeshData`,
which can then also be used by the `Mesh` struct. This allows passing
deformed vertices efficiently to the draw routines.

The modifier code constructs a new Mesh instead of writing to ob->data;
even when ob->data is a CoW copy, it can still be used by different
objects and thus shouldn't be modified by a modifier.
This commit is contained in:
2018-04-19 11:03:58 +02:00
parent be4df85919
commit 7efc75c709
11 changed files with 374 additions and 183 deletions

View File

@@ -93,6 +93,14 @@ struct Mesh *BKE_mesh_copy(struct Main *bmain, const struct Mesh *me);
void BKE_mesh_update_customdata_pointers(struct Mesh *me, const bool do_ensure_tess_cd); void BKE_mesh_update_customdata_pointers(struct Mesh *me, const bool do_ensure_tess_cd);
void BKE_mesh_ensure_skin_customdata(struct Mesh *me); void BKE_mesh_ensure_skin_customdata(struct Mesh *me);
struct Mesh * BKE_mesh_from_template(
struct Mesh *me_src,
int numVerts, int numEdges, int numTessFaces,
int numLoops, int numPolys);
bool BKE_mesh_ensure_edit_data(struct Mesh *me);
bool BKE_mesh_clear_edit_data(struct Mesh *me);
bool BKE_mesh_ensure_facemap_customdata(struct Mesh *me); bool BKE_mesh_ensure_facemap_customdata(struct Mesh *me);
bool BKE_mesh_clear_facemap_customdata(struct Mesh *me); bool BKE_mesh_clear_facemap_customdata(struct Mesh *me);

View File

@@ -1162,7 +1162,10 @@ DerivedMesh *mesh_create_derived_for_modifier(
if (mti->type == eModifierTypeType_OnlyDeform) { if (mti->type == eModifierTypeType_OnlyDeform) {
int numVerts; int numVerts;
float (*deformedVerts)[3] = BKE_mesh_vertexCos_get(me, &numVerts); /* Always get the vertex coordinates from the original mesh. Otherwise
* there is the risk of deforming already-deformed coordinates. */
Mesh *mesh_orig_id = (Mesh *)DEG_get_original_id(&me->id);
float (*deformedVerts)[3] = BKE_mesh_vertexCos_get(mesh_orig_id, &numVerts);
modwrap_deformVerts(md, depsgraph, ob, NULL, deformedVerts, numVerts, 0); modwrap_deformVerts(md, depsgraph, ob, NULL, deformedVerts, numVerts, 0);
dm = mesh_create_derived(me, deformedVerts); dm = mesh_create_derived(me, deformedVerts);
@@ -1748,6 +1751,9 @@ static void mesh_calc_modifiers(
DerivedMesh **r_deform, DerivedMesh **r_final) DerivedMesh **r_deform, DerivedMesh **r_final)
{ {
Mesh *me = ob->data; Mesh *me = ob->data;
/* Always get the vertex coordinates from the original mesh. Otherwise
* there is the risk of deforming already-deformed coordinates. */
Mesh *mesh_orig_id = (Mesh *)DEG_get_original_id(&me->id);
ModifierData *firstmd, *md, *previewmd = NULL; ModifierData *firstmd, *md, *previewmd = NULL;
CDMaskLink *datamasks, *curr; CDMaskLink *datamasks, *curr;
/* XXX Always copying POLYINDEX, else tessellated data are no more valid! */ /* XXX Always copying POLYINDEX, else tessellated data are no more valid! */
@@ -1837,7 +1843,7 @@ static void mesh_calc_modifiers(
if (mti->type == eModifierTypeType_OnlyDeform && !sculpt_dyntopo) { if (mti->type == eModifierTypeType_OnlyDeform && !sculpt_dyntopo) {
if (!deformedVerts) if (!deformedVerts)
deformedVerts = BKE_mesh_vertexCos_get(me, &numVerts); deformedVerts = BKE_mesh_vertexCos_get(mesh_orig_id, &numVerts);
modwrap_deformVerts(md, depsgraph, ob, NULL, deformedVerts, numVerts, deform_app_flags); modwrap_deformVerts(md, depsgraph, ob, NULL, deformedVerts, numVerts, deform_app_flags);
} }
@@ -1870,7 +1876,7 @@ static void mesh_calc_modifiers(
if (inputVertexCos) if (inputVertexCos)
deformedVerts = inputVertexCos; deformedVerts = inputVertexCos;
else else
deformedVerts = BKE_mesh_vertexCos_get(me, &numVerts); deformedVerts = BKE_mesh_vertexCos_get(mesh_orig_id, &numVerts);
} }
@@ -1967,7 +1973,7 @@ static void mesh_calc_modifiers(
dm->getVertCos(dm, deformedVerts); dm->getVertCos(dm, deformedVerts);
} }
else { else {
deformedVerts = BKE_mesh_vertexCos_get(me, &numVerts); deformedVerts = BKE_mesh_vertexCos_get(mesh_orig_id, &numVerts);
} }
} }
@@ -2467,6 +2473,11 @@ static void editbmesh_calc_modifiers(
*r_cage = dm; *r_cage = dm;
} }
else { else {
struct Mesh *mesh = ob->data;
if (mesh->id.tag & LIB_TAG_COPY_ON_WRITE) {
BKE_mesh_ensure_edit_data(mesh);
mesh->emd->vertexCos = MEM_dupallocN(deformedVerts);
}
*r_cage = getEditDerivedBMesh( *r_cage = getEditDerivedBMesh(
em, ob, mask, em, ob, mask,
deformedVerts ? MEM_dupallocN(deformedVerts) : NULL); deformedVerts ? MEM_dupallocN(deformedVerts) : NULL);
@@ -2504,6 +2515,13 @@ static void editbmesh_calc_modifiers(
} }
else { else {
/* this is just a copy of the editmesh, no need to calc normals */ /* this is just a copy of the editmesh, no need to calc normals */
struct Mesh *mesh = ob->data;
if (mesh->id.tag & LIB_TAG_COPY_ON_WRITE) {
BKE_mesh_ensure_edit_data(mesh);
if (mesh->emd->vertexCos != NULL)
MEM_freeN((void *)mesh->emd->vertexCos);
mesh->emd->vertexCos = MEM_dupallocN(deformedVerts);
}
*r_final = getEditDerivedBMesh(em, ob, dataMask, deformedVerts); *r_final = getEditDerivedBMesh(em, ob, dataMask, deformedVerts);
deformedVerts = NULL; deformedVerts = NULL;

View File

@@ -56,6 +56,7 @@
#include "DNA_scene_types.h" #include "DNA_scene_types.h"
#include "DNA_object_types.h" #include "DNA_object_types.h"
#include "DNA_mesh_types.h"
#include "MEM_guardedalloc.h" #include "MEM_guardedalloc.h"
@@ -64,14 +65,7 @@ typedef struct EditDerivedBMesh {
BMEditMesh *em; BMEditMesh *em;
/** when set, \a vertexNos, polyNos are lazy initialized */ EditMeshData emd;
const float (*vertexCos)[3];
/** lazy initialize (when \a vertexCos is set) */
float const (*vertexNos)[3];
float const (*polyNos)[3];
/** also lazy init but dont depend on \a vertexCos */
const float (*polyCos)[3];
} EditDerivedBMesh; } EditDerivedBMesh;
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
@@ -81,7 +75,7 @@ static void emDM_ensurePolyNormals(EditDerivedBMesh *bmdm);
static void emDM_ensureVertNormals(EditDerivedBMesh *bmdm) static void emDM_ensureVertNormals(EditDerivedBMesh *bmdm)
{ {
if (bmdm->vertexCos && (bmdm->vertexNos == NULL)) { if (bmdm->emd.vertexCos && (bmdm->emd.vertexNos == NULL)) {
BMesh *bm = bmdm->em->bm; BMesh *bm = bmdm->em->bm;
const float (*vertexCos)[3], (*polyNos)[3]; const float (*vertexCos)[3], (*polyNos)[3];
@@ -92,19 +86,19 @@ static void emDM_ensureVertNormals(EditDerivedBMesh *bmdm)
BM_mesh_elem_index_ensure(bm, BM_FACE); BM_mesh_elem_index_ensure(bm, BM_FACE);
polyNos = bmdm->polyNos; polyNos = bmdm->emd.polyNos;
vertexCos = bmdm->vertexCos; vertexCos = bmdm->emd.vertexCos;
vertexNos = MEM_callocN(sizeof(*vertexNos) * bm->totvert, __func__); vertexNos = MEM_callocN(sizeof(*vertexNos) * bm->totvert, __func__);
BM_verts_calc_normal_vcos(bm, polyNos, vertexCos, vertexNos); BM_verts_calc_normal_vcos(bm, polyNos, vertexCos, vertexNos);
bmdm->vertexNos = (const float (*)[3])vertexNos; bmdm->emd.vertexNos = (const float (*)[3])vertexNos;
} }
} }
static void emDM_ensurePolyNormals(EditDerivedBMesh *bmdm) static void emDM_ensurePolyNormals(EditDerivedBMesh *bmdm)
{ {
if (bmdm->vertexCos && (bmdm->polyNos == NULL)) { if (bmdm->emd.vertexCos && (bmdm->emd.polyNos == NULL)) {
BMesh *bm = bmdm->em->bm; BMesh *bm = bmdm->em->bm;
const float (*vertexCos)[3]; const float (*vertexCos)[3];
float (*polyNos)[3]; float (*polyNos)[3];
@@ -117,7 +111,7 @@ static void emDM_ensurePolyNormals(EditDerivedBMesh *bmdm)
polyNos = MEM_mallocN(sizeof(*polyNos) * bm->totface, __func__); polyNos = MEM_mallocN(sizeof(*polyNos) * bm->totface, __func__);
vertexCos = bmdm->vertexCos; vertexCos = bmdm->emd.vertexCos;
BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) { BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
BM_elem_index_set(efa, i); /* set_inline */ BM_elem_index_set(efa, i); /* set_inline */
@@ -125,13 +119,13 @@ static void emDM_ensurePolyNormals(EditDerivedBMesh *bmdm)
} }
bm->elem_index_dirty &= ~BM_FACE; bm->elem_index_dirty &= ~BM_FACE;
bmdm->polyNos = (const float (*)[3])polyNos; bmdm->emd.polyNos = (const float (*)[3])polyNos;
} }
} }
static void emDM_ensurePolyCenters(EditDerivedBMesh *bmdm) static void emDM_ensurePolyCenters(EditDerivedBMesh *bmdm)
{ {
if (bmdm->polyCos == NULL) { if (bmdm->emd.polyCos == NULL) {
BMesh *bm = bmdm->em->bm; BMesh *bm = bmdm->em->bm;
float (*polyCos)[3]; float (*polyCos)[3];
@@ -141,9 +135,9 @@ static void emDM_ensurePolyCenters(EditDerivedBMesh *bmdm)
polyCos = MEM_mallocN(sizeof(*polyCos) * bm->totface, __func__); polyCos = MEM_mallocN(sizeof(*polyCos) * bm->totface, __func__);
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
const float (*vertexCos)[3]; const float (*vertexCos)[3];
vertexCos = bmdm->vertexCos; vertexCos = bmdm->emd.vertexCos;
BM_mesh_elem_index_ensure(bm, BM_VERT); BM_mesh_elem_index_ensure(bm, BM_VERT);
@@ -157,7 +151,7 @@ static void emDM_ensurePolyCenters(EditDerivedBMesh *bmdm)
} }
} }
bmdm->polyCos = (const float (*)[3])polyCos; bmdm->emd.polyCos = (const float (*)[3])polyCos;
} }
} }
@@ -193,9 +187,9 @@ static void emDM_calcLoopNormalsSpaceArray(
emDM_ensurePolyNormals(bmdm); emDM_ensurePolyNormals(bmdm);
dm->dirty &= ~DM_DIRTY_NORMALS; dm->dirty &= ~DM_DIRTY_NORMALS;
vertexCos = bmdm->vertexCos; vertexCos = bmdm->emd.vertexCos;
vertexNos = bmdm->vertexNos; vertexNos = bmdm->emd.vertexNos;
polyNos = bmdm->polyNos; polyNos = bmdm->emd.polyNos;
loopNos = dm->getLoopDataArray(dm, CD_NORMAL); loopNos = dm->getLoopDataArray(dm, CD_NORMAL);
if (!loopNos) { if (!loopNos) {
@@ -247,7 +241,7 @@ static void emDM_calc_loop_tangents(
return; return;
} }
const float (*poly_normals)[3] = bmdm->polyNos; const float (*poly_normals)[3] = bmdm->emd.polyNos;
const float (*loop_normals)[3] = CustomData_get_layer(&dm->loopData, CD_NORMAL); const float (*loop_normals)[3] = CustomData_get_layer(&dm->loopData, CD_NORMAL);
const float (*vert_orco)[3] = dm->getVertDataArray(dm, CD_ORCO); /* can be NULL */ const float (*vert_orco)[3] = dm->getVertDataArray(dm, CD_ORCO); /* can be NULL */
BKE_editmesh_loop_tangent_calc( BKE_editmesh_loop_tangent_calc(
@@ -311,13 +305,13 @@ static void emDM_foreachMappedVert(
BMIter iter; BMIter iter;
int i; int i;
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
const float (*vertexCos)[3] = bmdm->vertexCos; const float (*vertexCos)[3] = bmdm->emd.vertexCos;
const float (*vertexNos)[3]; const float (*vertexNos)[3];
if (flag & DM_FOREACH_USE_NORMAL) { if (flag & DM_FOREACH_USE_NORMAL) {
emDM_ensureVertNormals(bmdm); emDM_ensureVertNormals(bmdm);
vertexNos = bmdm->vertexNos; vertexNos = bmdm->emd.vertexNos;
} }
else { else {
vertexNos = NULL; vertexNos = NULL;
@@ -346,14 +340,14 @@ static void emDM_foreachMappedEdge(
BMIter iter; BMIter iter;
int i; int i;
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
BM_mesh_elem_index_ensure(bm, BM_VERT); BM_mesh_elem_index_ensure(bm, BM_VERT);
BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) { BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
func(userData, i, func(userData, i,
bmdm->vertexCos[BM_elem_index_get(eed->v1)], bmdm->emd.vertexCos[BM_elem_index_get(eed->v1)],
bmdm->vertexCos[BM_elem_index_get(eed->v2)]); bmdm->emd.vertexCos[BM_elem_index_get(eed->v2)]);
} }
} }
else { else {
@@ -378,7 +372,7 @@ static void emDM_foreachMappedLoop(
BMFace *efa; BMFace *efa;
BMIter iter; BMIter iter;
const float (*vertexCos)[3] = bmdm->vertexCos; const float (*vertexCos)[3] = bmdm->emd.vertexCos;
int f_idx; int f_idx;
BM_mesh_elem_index_ensure(bm, BM_VERT); BM_mesh_elem_index_ensure(bm, BM_VERT);
@@ -411,11 +405,11 @@ static void emDM_foreachMappedFaceCenter(
int i; int i;
emDM_ensurePolyCenters(bmdm); emDM_ensurePolyCenters(bmdm);
polyCos = bmdm->polyCos; /* always set */ polyCos = bmdm->emd.polyCos; /* always set */
if (flag & DM_FOREACH_USE_NORMAL) { if (flag & DM_FOREACH_USE_NORMAL) {
emDM_ensurePolyNormals(bmdm); emDM_ensurePolyNormals(bmdm);
polyNos = bmdm->polyNos; /* maybe NULL */ polyNos = bmdm->emd.polyNos; /* maybe NULL */
} }
else { else {
polyNos = NULL; polyNos = NULL;
@@ -444,9 +438,9 @@ static void emDM_getMinMax(DerivedMesh *dm, float r_min[3], float r_max[3])
int i; int i;
if (bm->totvert) { if (bm->totvert) {
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) { BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
minmax_v3v3_v3(r_min, r_max, bmdm->vertexCos[i]); minmax_v3v3_v3(r_min, r_max, bmdm->emd.vertexCos[i]);
} }
} }
else { else {
@@ -526,8 +520,8 @@ static void emDM_getVert(DerivedMesh *dm, int index, MVert *r_vert)
// ev = BM_vert_at_index(bm, index); /* warning, does list loop, _not_ ideal */ // ev = BM_vert_at_index(bm, index); /* warning, does list loop, _not_ ideal */
bmvert_to_mvert(bm, ev, r_vert); bmvert_to_mvert(bm, ev, r_vert);
if (bmdm->vertexCos) if (bmdm->emd.vertexCos)
copy_v3_v3(r_vert->co, bmdm->vertexCos[index]); copy_v3_v3(r_vert->co, bmdm->emd.vertexCos[index]);
} }
static void emDM_getVertCo(DerivedMesh *dm, int index, float r_co[3]) static void emDM_getVertCo(DerivedMesh *dm, int index, float r_co[3])
@@ -540,8 +534,8 @@ static void emDM_getVertCo(DerivedMesh *dm, int index, float r_co[3])
return; return;
} }
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
copy_v3_v3(r_co, bmdm->vertexCos[index]); copy_v3_v3(r_co, bmdm->emd.vertexCos[index]);
} }
else { else {
BMVert *ev; BMVert *ev;
@@ -564,9 +558,9 @@ static void emDM_getVertNo(DerivedMesh *dm, int index, float r_no[3])
} }
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
emDM_ensureVertNormals(bmdm); emDM_ensureVertNormals(bmdm);
copy_v3_v3(r_no, bmdm->vertexNos[index]); copy_v3_v3(r_no, bmdm->emd.vertexNos[index]);
} }
else { else {
BMVert *ev; BMVert *ev;
@@ -588,9 +582,9 @@ static void emDM_getPolyNo(DerivedMesh *dm, int index, float r_no[3])
return; return;
} }
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
emDM_ensurePolyNormals(bmdm); emDM_ensurePolyNormals(bmdm);
copy_v3_v3(r_no, bmdm->polyNos[index]); copy_v3_v3(r_no, bmdm->emd.polyNos[index]);
} }
else { else {
BMFace *efa; BMFace *efa;
@@ -665,11 +659,11 @@ static void emDM_copyVertArray(DerivedMesh *dm, MVert *r_vert)
BMIter iter; BMIter iter;
const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT); const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
int i; int i;
BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) { BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
copy_v3_v3(r_vert->co, bmdm->vertexCos[i]); copy_v3_v3(r_vert->co, bmdm->emd.vertexCos[i]);
normal_float_to_short_v3(r_vert->no, eve->no); normal_float_to_short_v3(r_vert->no, eve->no);
r_vert->flag = BM_vert_flag_to_mflag(eve); r_vert->flag = BM_vert_flag_to_mflag(eve);
@@ -873,9 +867,9 @@ static void emDM_getVertCos(DerivedMesh *dm, float (*r_cos)[3])
BMIter iter; BMIter iter;
int i; int i;
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) { BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
copy_v3_v3(r_cos[i], bmdm->vertexCos[i]); copy_v3_v3(r_cos[i], bmdm->emd.vertexCos[i]);
} }
} }
else { else {
@@ -890,18 +884,18 @@ static void emDM_release(DerivedMesh *dm)
EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm; EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
if (DM_release(dm)) { if (DM_release(dm)) {
if (bmdm->vertexCos) { if (bmdm->emd.vertexCos) {
MEM_freeN((void *)bmdm->vertexCos); MEM_freeN((void *)bmdm->emd.vertexCos);
if (bmdm->vertexNos) { if (bmdm->emd.vertexNos) {
MEM_freeN((void *)bmdm->vertexNos); MEM_freeN((void *)bmdm->emd.vertexNos);
} }
if (bmdm->polyNos) { if (bmdm->emd.polyNos) {
MEM_freeN((void *)bmdm->polyNos); MEM_freeN((void *)bmdm->emd.polyNos);
} }
} }
if (bmdm->polyCos) { if (bmdm->emd.polyCos) {
MEM_freeN((void *)bmdm->polyCos); MEM_freeN((void *)bmdm->emd.polyCos);
} }
MEM_freeN(bmdm); MEM_freeN(bmdm);
@@ -1006,7 +1000,7 @@ DerivedMesh *getEditDerivedBMesh(
bmdm->dm.release = emDM_release; bmdm->dm.release = emDM_release;
bmdm->vertexCos = (const float (*)[3])vertexCos; bmdm->emd.vertexCos = (const float (*)[3])vertexCos;
bmdm->dm.deformedOnly = (vertexCos != NULL); bmdm->dm.deformedOnly = (vertexCos != NULL);
const int cd_dvert_offset = (data_mask & CD_MASK_MDEFORMVERT) ? const int cd_dvert_offset = (data_mask & CD_MASK_MDEFORMVERT) ?
@@ -1434,7 +1428,7 @@ void BKE_editmesh_statvis_calc(
{ {
BKE_editmesh_color_ensure(em, BM_FACE); BKE_editmesh_color_ensure(em, BM_FACE);
statvis_calc_overhang( statvis_calc_overhang(
em, bmdm ? bmdm->polyNos : NULL, em, bmdm ? bmdm->emd.polyNos : NULL,
statvis->overhang_min / (float)M_PI, statvis->overhang_min / (float)M_PI,
statvis->overhang_max / (float)M_PI, statvis->overhang_max / (float)M_PI,
statvis->overhang_axis, statvis->overhang_axis,
@@ -1446,7 +1440,7 @@ void BKE_editmesh_statvis_calc(
const float scale = 1.0f / mat4_to_scale(em->ob->obmat); const float scale = 1.0f / mat4_to_scale(em->ob->obmat);
BKE_editmesh_color_ensure(em, BM_FACE); BKE_editmesh_color_ensure(em, BM_FACE);
statvis_calc_thickness( statvis_calc_thickness(
em, bmdm ? bmdm->vertexCos : NULL, em, bmdm ? bmdm->emd.vertexCos : NULL,
statvis->thickness_min * scale, statvis->thickness_min * scale,
statvis->thickness_max * scale, statvis->thickness_max * scale,
statvis->thickness_samples, statvis->thickness_samples,
@@ -1457,7 +1451,7 @@ void BKE_editmesh_statvis_calc(
{ {
BKE_editmesh_color_ensure(em, BM_FACE); BKE_editmesh_color_ensure(em, BM_FACE);
statvis_calc_intersect( statvis_calc_intersect(
em, bmdm ? bmdm->vertexCos : NULL, em, bmdm ? bmdm->emd.vertexCos : NULL,
em->derivedFaceColor); em->derivedFaceColor);
break; break;
} }
@@ -1469,7 +1463,7 @@ void BKE_editmesh_statvis_calc(
emDM_ensurePolyNormals(bmdm); emDM_ensurePolyNormals(bmdm);
statvis_calc_distort( statvis_calc_distort(
em, bmdm ? bmdm->vertexCos : NULL, bmdm ? bmdm->polyNos : NULL, em, bmdm ? bmdm->emd.vertexCos : NULL, bmdm ? bmdm->emd.polyNos : NULL,
statvis->distort_min, statvis->distort_min,
statvis->distort_max, statvis->distort_max,
em->derivedFaceColor); em->derivedFaceColor);
@@ -1479,7 +1473,7 @@ void BKE_editmesh_statvis_calc(
{ {
BKE_editmesh_color_ensure(em, BM_VERT); BKE_editmesh_color_ensure(em, BM_VERT);
statvis_calc_sharp( statvis_calc_sharp(
em, bmdm ? bmdm->vertexCos : NULL, em, bmdm ? bmdm->emd.vertexCos : NULL,
statvis->sharp_min, statvis->sharp_min,
statvis->sharp_max, statvis->sharp_max,
/* in this case they are vertex colors */ /* in this case they are vertex colors */

View File

@@ -388,6 +388,36 @@ void BKE_mesh_ensure_skin_customdata(Mesh *me)
} }
} }
bool BKE_mesh_ensure_edit_data(struct Mesh *me)
{
if (me->emd != NULL) {
return false;
}
me->emd = MEM_callocN(sizeof(EditMeshData), "EditMeshData");
return true;
}
bool BKE_mesh_clear_edit_data(struct Mesh *me)
{
if (me->emd == NULL) {
return false;
}
if (me->emd->polyCos != NULL)
MEM_freeN((void *)me->emd->polyCos);
if (me->emd->polyNos != NULL)
MEM_freeN((void *)me->emd->polyNos);
if (me->emd->vertexCos != NULL)
MEM_freeN((void *)me->emd->vertexCos);
if (me->emd->vertexNos != NULL)
MEM_freeN((void *)me->emd->vertexNos);
MEM_SAFE_FREE(me->emd);
return true;
}
bool BKE_mesh_ensure_facemap_customdata(struct Mesh *me) bool BKE_mesh_ensure_facemap_customdata(struct Mesh *me)
{ {
BMesh *bm = me->edit_btmesh ? me->edit_btmesh->bm : NULL; BMesh *bm = me->edit_btmesh ? me->edit_btmesh->bm : NULL;
@@ -481,6 +511,7 @@ void BKE_mesh_free(Mesh *me)
BKE_animdata_free(&me->id, false); BKE_animdata_free(&me->id, false);
BKE_mesh_batch_cache_free(me); BKE_mesh_batch_cache_free(me);
BKE_mesh_clear_edit_data(me);
CustomData_free(&me->vdata, me->totvert); CustomData_free(&me->vdata, me->totvert);
CustomData_free(&me->edata, me->totedge); CustomData_free(&me->edata, me->totedge);
@@ -580,6 +611,52 @@ void BKE_mesh_copy_data(Main *bmain, Mesh *me_dst, const Mesh *me_src, const int
} }
} }
static Mesh *mesh_from_template_ex(
Mesh *me_src,
int numVerts, int numEdges, int numTessFaces,
int numLoops, int numPolys,
CustomDataMask mask)
{
const bool do_tessface = ((me_src->totface != 0) && (me_src->totpoly == 0)); /* only do tessface if we have no polys */
Mesh *me_dst = MEM_callocN(sizeof(struct Mesh), "Mesh");
BKE_mesh_init(me_dst);
me_dst->mat = MEM_dupallocN(me_src->mat);
me_dst->mselect = MEM_dupallocN(me_dst->mselect);
me_dst->totvert = numVerts;
me_dst->totedge = numEdges;
me_dst->totloop = numLoops;
me_dst->totpoly = numPolys;
CustomData_copy(&me_src->vdata, &me_dst->vdata, mask, CD_CALLOC, numVerts);
CustomData_copy(&me_src->edata, &me_dst->edata, mask, CD_CALLOC, numEdges);
CustomData_copy(&me_src->ldata, &me_dst->ldata, mask, CD_CALLOC, numLoops);
CustomData_copy(&me_src->pdata, &me_dst->pdata, mask, CD_CALLOC, numPolys);
if (do_tessface) {
CustomData_copy(&me_src->fdata, &me_dst->fdata, mask, CD_CALLOC, numTessFaces);
}
else {
mesh_tessface_clear_intern(me_dst, false);
}
BKE_mesh_update_customdata_pointers(me_dst, false);
return me_dst;
}
Mesh * BKE_mesh_from_template(Mesh *me_src,
int numVerts, int numEdges, int numTessFaces,
int numLoops, int numPolys)
{
return mesh_from_template_ex(
me_src,
numVerts, numEdges, numTessFaces,
numLoops, numPolys,
CD_MASK_EVERYTHING);
}
Mesh *BKE_mesh_copy(Main *bmain, const Mesh *me) Mesh *BKE_mesh_copy(Main *bmain, const Mesh *me)
{ {
Mesh *me_copy; Mesh *me_copy;

View File

@@ -863,11 +863,16 @@ void modifier_deformVerts(struct ModifierData *md, struct Depsgraph *depsgraph,
mti->deformVerts(md, depsgraph, ob, mesh, vertexCos, numVerts, flag); mti->deformVerts(md, depsgraph, ob, mesh, vertexCos, numVerts, flag);
} }
else { else {
DerivedMesh *dm = CDDM_from_mesh(mesh); DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformVerts_DM(md, depsgraph, ob, dm, vertexCos, numVerts, flag); mti->deformVerts_DM(md, depsgraph, ob, dm, vertexCos, numVerts, flag);
dm->release(dm); if (dm) {
dm->release(dm);
}
} }
} }
@@ -881,11 +886,16 @@ void modifier_deformMatrices(struct ModifierData *md, struct Depsgraph *depsgrap
mti->deformMatrices(md, depsgraph, ob, mesh, vertexCos, defMats, numVerts); mti->deformMatrices(md, depsgraph, ob, mesh, vertexCos, defMats, numVerts);
} }
else { else {
DerivedMesh *dm = CDDM_from_mesh(mesh); DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformMatrices_DM(md, depsgraph, ob, dm, vertexCos, defMats, numVerts); mti->deformMatrices_DM(md, depsgraph, ob, dm, vertexCos, defMats, numVerts);
dm->release(dm); if (dm) {
dm->release(dm);
}
} }
} }
@@ -899,11 +909,16 @@ void modifier_deformVertsEM(struct ModifierData *md, struct Depsgraph *depsgraph
mti->deformVertsEM(md, depsgraph, ob, editData, mesh, vertexCos, numVerts); mti->deformVertsEM(md, depsgraph, ob, editData, mesh, vertexCos, numVerts);
} }
else { else {
DerivedMesh *dm = CDDM_from_mesh(mesh); DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformVertsEM_DM(md, depsgraph, ob, editData, dm, vertexCos, numVerts); mti->deformVertsEM_DM(md, depsgraph, ob, editData, dm, vertexCos, numVerts);
dm->release(dm); if (dm) {
dm->release(dm);
}
} }
} }
@@ -917,11 +932,16 @@ void modifier_deformMatricesEM(struct ModifierData *md, struct Depsgraph *depsgr
mti->deformMatricesEM(md, depsgraph, ob, editData, mesh, vertexCos, defMats, numVerts); mti->deformMatricesEM(md, depsgraph, ob, editData, mesh, vertexCos, defMats, numVerts);
} }
else { else {
DerivedMesh *dm = CDDM_from_mesh(mesh); DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformMatricesEM_DM(md, depsgraph, ob, editData, dm, vertexCos, defMats, numVerts); mti->deformMatricesEM_DM(md, depsgraph, ob, editData, dm, vertexCos, defMats, numVerts);
dm->release(dm); if (dm) {
dm->release(dm);
}
} }
} }
@@ -985,14 +1005,20 @@ void modifier_deformVerts_DM_deprecated(struct ModifierData *md, struct Depsgrap
mti->deformVerts_DM(md, depsgraph, ob, dm, vertexCos, numVerts, flag); mti->deformVerts_DM(md, depsgraph, ob, dm, vertexCos, numVerts, flag);
} }
else { else {
struct Mesh mesh; /* TODO(sybren): deduplicate all the copies of this code in this file. */
BKE_mesh_init(&mesh); Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_libblock_alloc_notest(ID_ME);
BKE_mesh_init(mesh);
DM_to_mesh(dm, mesh, ob, CD_MASK_EVERYTHING, false);
}
DM_to_mesh(dm, &mesh, ob, CD_MASK_EVERYTHING, false); mti->deformVerts(md, depsgraph, ob, mesh, vertexCos, numVerts, flag);
mti->deformVerts(md, depsgraph, ob, &mesh, vertexCos, numVerts, flag); if (mesh != NULL) {
BKE_mesh_free(mesh);
BKE_mesh_free(&mesh); MEM_freeN(mesh);
}
} }
} }
@@ -1028,14 +1054,19 @@ void modifier_deformVertsEM_DM_deprecated(struct ModifierData *md, struct Depsgr
mti->deformVertsEM_DM(md, depsgraph, ob, editData, dm, vertexCos, numVerts); mti->deformVertsEM_DM(md, depsgraph, ob, editData, dm, vertexCos, numVerts);
} }
else { else {
struct Mesh mesh; Mesh *mesh = NULL;
BKE_mesh_init(&mesh); if (dm != NULL) {
mesh = BKE_libblock_alloc_notest(ID_ME);
BKE_mesh_init(mesh);
DM_to_mesh(dm, mesh, ob, CD_MASK_EVERYTHING, false);
}
DM_to_mesh(dm, &mesh, ob, CD_MASK_EVERYTHING, false); mti->deformVertsEM(md, depsgraph, ob, editData, mesh, vertexCos, numVerts);
mti->deformVertsEM(md, depsgraph, ob, editData, &mesh, vertexCos, numVerts); if (mesh != NULL) {
BKE_mesh_free(mesh);
BKE_mesh_free(&mesh); MEM_freeN(mesh);
}
} }
} }
@@ -1069,19 +1100,30 @@ struct DerivedMesh *modifier_applyModifier_DM_deprecated(struct ModifierData *md
return mti->applyModifier_DM(md, depsgraph, ob, dm, flag); return mti->applyModifier_DM(md, depsgraph, ob, dm, flag);
} }
else { else {
struct Mesh mesh; /* TODO(sybren): deduplicate all the copies of this code in this file. */
BKE_mesh_init(&mesh); Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_libblock_alloc_notest(ID_ME);
BKE_mesh_init(mesh);
DM_to_mesh(dm, mesh, ob, CD_MASK_EVERYTHING, false);
}
DM_to_mesh(dm, &mesh, ob, CD_MASK_EVERYTHING, false); struct Mesh *new_mesh = mti->applyModifier(md, depsgraph, ob, mesh, flag);
struct Mesh *new_mesh = mti->applyModifier(md, depsgraph, ob, &mesh, flag);
DerivedMesh *ndm = CDDM_from_mesh(new_mesh); DerivedMesh *ndm = CDDM_from_mesh(new_mesh);
if(new_mesh != mesh) {
/* Make a DM that doesn't reference new_mesh so we can free the latter. */
/* TODO(sybren): create CDDM_from_mesh_ex() that creates a copy directly. */
DerivedMesh *nonref_dm = CDDM_copy(ndm);
ndm->release(ndm);
ndm = nonref_dm;
if(new_mesh != &mesh) { BKE_mesh_free(new_mesh);
BKE_mesh_free(&mesh); MEM_freeN(new_mesh);
}
/* XXX free new_mesh? */ if (mesh != NULL) {
BKE_mesh_free(mesh);
MEM_freeN(mesh);
} }
return ndm; return ndm;

View File

@@ -128,6 +128,8 @@ typedef struct MeshRenderData {
int loose_edge_len; int loose_edge_len;
BMEditMesh *edit_bmesh; BMEditMesh *edit_bmesh;
struct EditMeshData *edit_data;
MVert *mvert; MVert *mvert;
MEdge *medge; MEdge *medge;
MLoop *mloop; MLoop *mloop;
@@ -389,6 +391,7 @@ static MeshRenderData *mesh_render_data_create_ex(
BMesh *bm = embm->bm; BMesh *bm = embm->bm;
rdata->edit_bmesh = embm; rdata->edit_bmesh = embm;
rdata->edit_data = me->emd;
int bm_ensure_types = 0; int bm_ensure_types = 0;
if (types & (MR_DATATYPE_VERT)) { if (types & (MR_DATATYPE_VERT)) {
@@ -426,6 +429,9 @@ static MeshRenderData *mesh_render_data_create_ex(
if (types & (MR_DATATYPE_DVERT)) { if (types & (MR_DATATYPE_DVERT)) {
bm_ensure_types |= BM_VERT; bm_ensure_types |= BM_VERT;
} }
if (rdata->edit_data != NULL) {
bm_ensure_types |= BM_VERT;
}
BM_mesh_elem_index_ensure(bm, bm_ensure_types); BM_mesh_elem_index_ensure(bm, bm_ensure_types);
BM_mesh_elem_table_ensure(bm, bm_ensure_types & ~BM_LOOP); BM_mesh_elem_table_ensure(bm, bm_ensure_types & ~BM_LOOP);
@@ -1359,9 +1365,19 @@ static void add_overlay_tri(
unsigned char vflag; unsigned char vflag;
if (vbo_pos) { if (vbo_pos) {
for (uint i = 0; i < 3; i++) { /* TODO(sybren): deduplicate this and all the other places it's pasted to in this file. */
const float *pos = bm_looptri[i]->v->co; if (rdata->edit_data && rdata->edit_data->vertexCos) {
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx + i, pos); for (uint i = 0; i < 3; i++) {
int vidx = BM_elem_index_get(bm_looptri[i]->v);
const float *pos = rdata->edit_data->vertexCos[vidx];
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx + i, pos);
}
}
else {
for (uint i = 0; i < 3; i++) {
const float *pos = bm_looptri[i]->v->co;
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx + i, pos);
}
} }
} }
@@ -1399,9 +1415,19 @@ static void add_overlay_loose_edge(
const BMEdge *eed, const int base_vert_idx) const BMEdge *eed, const int base_vert_idx)
{ {
if (vbo_pos) { if (vbo_pos) {
for (int i = 0; i < 2; ++i) { /* TODO(sybren): deduplicate this and all the other places it's pasted to in this file. */
const float *pos = (&eed->v1)[i]->co; if (rdata->edit_data && rdata->edit_data->vertexCos) {
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx + i, pos); for (uint i = 0; i < 2; i++) {
int vidx = BM_elem_index_get((&eed->v1)[i]);
const float *pos = rdata->edit_data->vertexCos[vidx];
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx + i, pos);
}
}
else {
for (int i = 0; i < 2; ++i) {
const float *pos = (&eed->v1)[i]->co;
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx + i, pos);
}
} }
} }
@@ -1428,8 +1454,16 @@ static void add_overlay_loose_vert(
const BMVert *eve, const int base_vert_idx) const BMVert *eve, const int base_vert_idx)
{ {
if (vbo_pos) { if (vbo_pos) {
const float *pos = eve->co; /* TODO(sybren): deduplicate this and all the other places it's pasted to in this file. */
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx, pos); if (rdata->edit_data && rdata->edit_data->vertexCos) {
int vidx = BM_elem_index_get(eve);
const float *pos = rdata->edit_data->vertexCos[vidx];
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx, pos);
}
else {
const float *pos = eve->co;
GWN_vertbuf_attr_set(vbo_pos, pos_id, base_vert_idx, pos);
}
} }
if (vbo_nor) { if (vbo_nor) {
@@ -2095,8 +2129,18 @@ static Gwn_VertBuf *mesh_batch_cache_get_tri_pos_and_normals_ex(
} }
} }
for (uint t = 0; t < 3; t++) { /* TODO(sybren): deduplicate this and all the other places it's pasted to in this file. */
copy_v3_v3(GWN_vertbuf_raw_step(&pos_step), bm_looptri[t]->v->co); if (rdata->edit_data && rdata->edit_data->vertexCos) {
for (uint t = 0; t < 3; t++) {
int vidx = BM_elem_index_get(bm_looptri[t]->v);
const float *pos = rdata->edit_data->vertexCos[vidx];
copy_v3_v3(GWN_vertbuf_raw_step(&pos_step), pos);
}
}
else {
for (uint t = 0; t < 3; t++) {
copy_v3_v3(GWN_vertbuf_raw_step(&pos_step), bm_looptri[t]->v->co);
}
} }
} }
} }

View File

@@ -52,6 +52,19 @@ struct Material;
struct Mesh; struct Mesh;
struct Multires; struct Multires;
#
#
typedef struct EditMeshData {
/** when set, \a vertexNos, polyNos are lazy initialized */
const float (*vertexCos)[3];
/** lazy initialize (when \a vertexCos is set) */
float const (*vertexNos)[3];
float const (*polyNos)[3];
/** also lazy init but dont depend on \a vertexCos */
const float (*polyCos)[3];
} EditMeshData;
typedef struct Mesh { typedef struct Mesh {
ID id; ID id;
struct AnimData *adt; /* animation data (must be immediately after id for utilities to use it) */ struct AnimData *adt; /* animation data (must be immediately after id for utilities to use it) */
@@ -87,6 +100,7 @@ typedef struct Mesh {
/* When the object is available, the preferred access method is: BKE_editmesh_from_object(ob) */ /* When the object is available, the preferred access method is: BKE_editmesh_from_object(ob) */
struct BMEditMesh *edit_btmesh; /* not saved in file! */ struct BMEditMesh *edit_btmesh; /* not saved in file! */
struct EditMeshData *emd; /* not saved in file! */
struct CustomData vdata, edata, fdata; struct CustomData vdata, edata, fdata;

View File

@@ -41,8 +41,13 @@
#include "BLI_ghash.h" #include "BLI_ghash.h"
#include "DNA_meshdata_types.h" #include "DNA_meshdata_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DEG_depsgraph_query.h"
#include "BKE_cdderivedmesh.h" #include "BKE_cdderivedmesh.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h" #include "BKE_modifier.h"
#include "BKE_particle.h" #include "BKE_particle.h"
#include "BKE_scene.h" #include "BKE_scene.h"
@@ -75,12 +80,11 @@ static bool dependsOnTime(ModifierData *UNUSED(md))
return true; return true;
} }
static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(depsgraph), static Mesh *applyModifier(ModifierData *md, struct Depsgraph *depsgraph,
Object *UNUSED(ob), DerivedMesh *derivedData, Object *UNUSED(ob), struct Mesh *mesh,
ModifierApplyFlag UNUSED(flag)) ModifierApplyFlag UNUSED(flag))
{ {
DerivedMesh *dm = derivedData; Mesh *result;
DerivedMesh *result;
BuildModifierData *bmd = (BuildModifierData *) md; BuildModifierData *bmd = (BuildModifierData *) md;
int i, j, k; int i, j, k;
int numFaces_dst, numEdges_dst, numLoops_dst = 0; int numFaces_dst, numEdges_dst, numLoops_dst = 0;
@@ -93,16 +97,16 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
GHash *vertHash = BLI_ghash_int_new("build ve apply gh"); GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
/* maps edge indices in new mesh to indices in old mesh */ /* maps edge indices in new mesh to indices in old mesh */
GHash *edgeHash = BLI_ghash_int_new("build ed apply gh"); GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
/* maps edge indices in old mesh to indices in new mesh */
GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh"); GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
const int numVert_src = dm->getNumVerts(dm); const int numVert_src = mesh->totvert;
const int numEdge_src = dm->getNumEdges(dm); const int numEdge_src = mesh->totedge;
const int numPoly_src = dm->getNumPolys(dm); const int numPoly_src = mesh->totpoly;
MPoly *mpoly_src = dm->getPolyArray(dm); MPoly *mpoly_src = mesh->mpoly;
MLoop *mloop_src = dm->getLoopArray(dm); MLoop *mloop_src = mesh->mloop;
MEdge *medge_src = dm->getEdgeArray(dm); MEdge *medge_src = mesh->medge;
MVert *mvert_src = dm->getVertArray(dm); MVert *mvert_src = mesh->mvert;
vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap"); vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap");
edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap"); edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap");
@@ -112,13 +116,13 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
range_vn_i(edgeMap, numEdge_src, 0); range_vn_i(edgeMap, numEdge_src, 0);
range_vn_i(faceMap, numPoly_src, 0); range_vn_i(faceMap, numPoly_src, 0);
frac = (BKE_scene_frame_get(md->scene) - bmd->start) / bmd->length; struct Scene *scene = DEG_get_input_scene(depsgraph);
frac = (BKE_scene_frame_get(scene) - bmd->start) / bmd->length;
CLAMP(frac, 0.0f, 1.0f); CLAMP(frac, 0.0f, 1.0f);
if (bmd->flag & MOD_BUILD_FLAG_REVERSE) { if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
frac = 1.0f - frac; frac = 1.0f - frac;
} }
numFaces_dst = numPoly_src * frac; numFaces_dst = numPoly_src * frac;
numEdges_dst = numEdge_src * frac; numEdges_dst = numEdge_src * frac;
@@ -126,7 +130,6 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
if (numFaces_dst) { if (numFaces_dst) {
MPoly *mpoly, *mp; MPoly *mpoly, *mp;
MLoop *ml, *mloop; MLoop *ml, *mloop;
MEdge *medge;
uintptr_t hash_num, hash_num_alt; uintptr_t hash_num, hash_num_alt;
if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) { if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
@@ -159,11 +162,10 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
/* get the set of edges that will be in the new mesh (i.e. all edges /* get the set of edges that will be in the new mesh (i.e. all edges
* that have both verts in the new mesh) * that have both verts in the new mesh)
*/ */
medge = medge_src;
hash_num = 0; hash_num = 0;
hash_num_alt = 0; hash_num_alt = 0;
for (i = 0; i < numEdge_src; i++, hash_num_alt++) { for (i = 0; i < numEdge_src; i++, hash_num_alt++) {
MEdge *me = medge + i; MEdge *me = medge_src + i;
if (BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v1)) && if (BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v1)) &&
BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v2))) BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v2)))
@@ -173,6 +175,7 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
hash_num++; hash_num++;
} }
} }
BLI_assert(hash_num == BLI_ghash_len(edgeHash));
} }
else if (numEdges_dst) { else if (numEdges_dst) {
MEdge *medge, *me; MEdge *medge, *me;
@@ -206,7 +209,7 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
/* get the set of edges that will be in the new mesh */ /* get the set of edges that will be in the new mesh */
for (i = 0; i < numEdges_dst; i++) { for (i = 0; i < numEdges_dst; i++) {
j = BLI_ghash_len(edgeHash); j = BLI_ghash_len(edgeHash);
BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(j), BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(j),
SET_INT_IN_POINTER(edgeMap[i])); SET_INT_IN_POINTER(edgeMap[i]));
BLI_ghash_insert(edgeHash2, SET_INT_IN_POINTER(edgeMap[i]), BLI_ghash_insert(edgeHash2, SET_INT_IN_POINTER(edgeMap[i]),
@@ -229,11 +232,9 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
} }
} }
/* now we know the number of verts, edges and faces, we can create /* now we know the number of verts, edges and faces, we can create the mesh. */
* the mesh result = BKE_mesh_from_template(mesh, BLI_ghash_len(vertHash), BLI_ghash_len(edgeHash),
*/ 0, numLoops_dst, numFaces_dst);
result = CDDM_from_template(dm, BLI_ghash_len(vertHash),
BLI_ghash_len(edgeHash), 0, numLoops_dst, numFaces_dst);
/* copy the vertices across */ /* copy the vertices across */
GHASH_ITER (gh_iter, vertHash) { GHASH_ITER (gh_iter, vertHash) {
@@ -243,45 +244,44 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter)); int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter));
source = mvert_src[oldIndex]; source = mvert_src[oldIndex];
dest = CDDM_get_vert(result, newIndex); dest = &result->mvert[newIndex];
DM_copy_vert_data(dm, result, oldIndex, newIndex, 1); CustomData_copy_data(&mesh->vdata, &result->vdata, oldIndex, newIndex, 1);
*dest = source; *dest = source;
} }
/* copy the edges across, remapping indices */ /* copy the edges across, remapping indices */
for (i = 0; i < BLI_ghash_len(edgeHash); i++) { for (i = 0; i < BLI_ghash_len(edgeHash); i++) {
MEdge source; MEdge source;
MEdge *dest; MEdge *dest;
int oldIndex = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash, SET_INT_IN_POINTER(i))); int oldIndex = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash, SET_INT_IN_POINTER(i)));
source = medge_src[oldIndex]; source = medge_src[oldIndex];
dest = CDDM_get_edge(result, i); dest = &result->medge[i];
source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1))); source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2))); source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
DM_copy_edge_data(dm, result, oldIndex, i, 1); CustomData_copy_data(&mesh->edata, &result->edata, oldIndex, i, 1);
*dest = source; *dest = source;
} }
mpoly_dst = CDDM_get_polys(result); mpoly_dst = result->mpoly;
/* mloop_dst = */ ml_dst = CDDM_get_loops(result); ml_dst = result->mloop;
/* copy the faces across, remapping indices */ /* copy the faces across, remapping indices */
k = 0; k = 0;
for (i = 0; i < numFaces_dst; i++) { for (i = 0; i < numFaces_dst; i++) {
MPoly *source; MPoly *source;
MPoly *dest; MPoly *dest;
source = mpoly_src + faceMap[i]; source = mpoly_src + faceMap[i];
dest = mpoly_dst + i; dest = mpoly_dst + i;
DM_copy_poly_data(dm, result, faceMap[i], i, 1); CustomData_copy_data(&mesh->pdata, &result->pdata, faceMap[i], i, 1);
*dest = *source; *dest = *source;
dest->loopstart = k; dest->loopstart = k;
CustomData_copy_data(&mesh->ldata, &result->ldata, source->loopstart, dest->loopstart, dest->totloop);
DM_copy_loop_data(dm, result, source->loopstart, dest->loopstart, dest->totloop);
ml_src = mloop_src + source->loopstart; ml_src = mloop_src + source->loopstart;
for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) { for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) {
@@ -298,10 +298,7 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
MEM_freeN(edgeMap); MEM_freeN(edgeMap);
MEM_freeN(faceMap); MEM_freeN(faceMap);
if (dm->dirty & DM_DIRTY_NORMALS) { /* TODO(sybren): also copy flags & tags? */
result->dirty |= DM_DIRTY_NORMALS;
}
return result; return result;
} }
@@ -319,14 +316,14 @@ ModifierTypeInfo modifierType_Build = {
/* deformMatrices_DM */ NULL, /* deformMatrices_DM */ NULL,
/* deformVertsEM_DM */ NULL, /* deformVertsEM_DM */ NULL,
/* deformMatricesEM_DM*/NULL, /* deformMatricesEM_DM*/NULL,
/* applyModifier_DM */ applyModifier, /* applyModifier_DM */ NULL,
/* applyModifierEM_DM */NULL, /* applyModifierEM_DM */NULL,
/* deformVerts */ NULL, /* deformVerts */ NULL,
/* deformMatrices */ NULL, /* deformMatrices */ NULL,
/* deformVertsEM */ NULL, /* deformVertsEM */ NULL,
/* deformMatricesEM */ NULL, /* deformMatricesEM */ NULL,
/* applyModifier */ NULL, /* applyModifier */ applyModifier,
/* applyModifierEM */ NULL, /* applyModifierEM */ NULL,
/* initData */ initData, /* initData */ initData,

View File

@@ -32,6 +32,7 @@
* \ingroup modifiers * \ingroup modifiers
*/ */
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h" #include "DNA_meshdata_types.h"
#include "DNA_object_types.h" #include "DNA_object_types.h"
@@ -39,6 +40,7 @@
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "BKE_cdderivedmesh.h" #include "BKE_cdderivedmesh.h"
#include "BKE_mesh.h"
#include "BKE_library_query.h" #include "BKE_library_query.h"
#include "BKE_modifier.h" #include "BKE_modifier.h"
#include "BKE_deform.h" #include "BKE_deform.h"
@@ -180,11 +182,10 @@ static void simpleDeform_bend(const float factor, const int axis, const float dc
/* simple deform modifier */ /* simple deform modifier */
static void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct DerivedMesh *dm, static void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct Mesh *mesh,
float (*vertexCos)[3], int numVerts) float (*vertexCos)[3], int numVerts)
{ {
const float base_limit[2] = {0.0f, 0.0f}; const float base_limit[2] = {0.0f, 0.0f};
int i; int i;
float smd_limit[2], smd_factor; float smd_limit[2], smd_factor;
SpaceTransform *transf = NULL, tmp_transf; SpaceTransform *transf = NULL, tmp_transf;
@@ -281,7 +282,7 @@ static void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object
} }
} }
modifier_get_vgroup(ob, dm, smd->vgroup_name, &dvert, &vgroup); modifier_get_vgroup_mesh(ob, mesh, smd->vgroup_name, &dvert, &vgroup);
const bool invert_vgroup = (smd->flag & MOD_SIMPLEDEFORM_FLAG_INVERT_VGROUP) != 0; const bool invert_vgroup = (smd->flag & MOD_SIMPLEDEFORM_FLAG_INVERT_VGROUP) != 0;
const uint *axis_map = axis_map_table[(smd->mode != MOD_SIMPLEDEFORM_MODE_BEND) ? deform_axis : 2]; const uint *axis_map = axis_map_table[(smd->mode != MOD_SIMPLEDEFORM_MODE_BEND) ? deform_axis : 2];
@@ -384,43 +385,21 @@ static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphConte
} }
static void deformVerts(ModifierData *md, struct Depsgraph *UNUSED(depsgraph), static void deformVerts(ModifierData *md, struct Depsgraph *UNUSED(depsgraph),
Object *ob, DerivedMesh *derivedData, Object *ob, struct Mesh *mesh,
float (*vertexCos)[3], float (*vertexCos)[3],
int numVerts, int numVerts,
ModifierApplyFlag UNUSED(flag)) ModifierApplyFlag UNUSED(flag))
{ {
DerivedMesh *dm = derivedData; SimpleDeformModifier_do((SimpleDeformModifierData *)md, ob, mesh, vertexCos, numVerts);
CustomDataMask dataMask = requiredDataMask(ob, md);
/* we implement requiredDataMask but thats not really useful since
* mesh_calc_modifiers pass a NULL derivedData */
if (dataMask)
dm = get_dm(ob, NULL, dm, NULL, false, false);
SimpleDeformModifier_do((SimpleDeformModifierData *)md, ob, dm, vertexCos, numVerts);
if (dm != derivedData)
dm->release(dm);
} }
static void deformVertsEM(ModifierData *md, struct Depsgraph *UNUSED(depsgraph), static void deformVertsEM(ModifierData *md, struct Depsgraph *UNUSED(depsgraph),
Object *ob, struct BMEditMesh *editData, Object *ob, struct BMEditMesh *UNUSED(editData),
DerivedMesh *derivedData, struct Mesh *mesh,
float (*vertexCos)[3], float (*vertexCos)[3],
int numVerts) int numVerts)
{ {
DerivedMesh *dm = derivedData; SimpleDeformModifier_do((SimpleDeformModifierData *)md, ob, mesh, vertexCos, numVerts);
CustomDataMask dataMask = requiredDataMask(ob, md);
/* we implement requiredDataMask but thats not really useful since
* mesh_calc_modifiers pass a NULL derivedData */
if (dataMask)
dm = get_dm(ob, editData, dm, NULL, false, false);
SimpleDeformModifier_do((SimpleDeformModifierData *)md, ob, dm, vertexCos, numVerts);
if (dm != derivedData)
dm->release(dm);
} }
@@ -438,16 +417,16 @@ ModifierTypeInfo modifierType_SimpleDeform = {
/* copyData */ copyData, /* copyData */ copyData,
/* deformVerts_DM */ deformVerts, /* deformVerts_DM */ NULL,
/* deformMatrices_DM */ NULL, /* deformMatrices_DM */ NULL,
/* deformVertsEM_DM */ deformVertsEM, /* deformVertsEM_DM */ NULL,
/* deformMatricesEM_DM*/NULL, /* deformMatricesEM_DM*/NULL,
/* applyModifier_DM */ NULL, /* applyModifier_DM */ NULL,
/* applyModifierEM_DM */NULL, /* applyModifierEM_DM */NULL,
/* deformVerts */ NULL, /* deformVerts */ deformVerts,
/* deformMatrices */ NULL, /* deformMatrices */ NULL,
/* deformVertsEM */ NULL, /* deformVertsEM */ deformVertsEM,
/* deformMatricesEM */ NULL, /* deformMatricesEM */ NULL,
/* applyModifier */ NULL, /* applyModifier */ NULL,
/* applyModifierEM */ NULL, /* applyModifierEM */ NULL,

View File

@@ -34,9 +34,11 @@
#include "DNA_image_types.h" #include "DNA_image_types.h"
#include "DNA_meshdata_types.h" #include "DNA_meshdata_types.h"
#include "DNA_mesh_types.h"
#include "DNA_modifier_types.h" #include "DNA_modifier_types.h"
#include "DNA_object_types.h" #include "DNA_object_types.h"
#include "DNA_scene_types.h" #include "DNA_scene_types.h"
#include "DNA_scene_types.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "BLI_math_vector.h" #include "BLI_math_vector.h"
@@ -229,6 +231,20 @@ void modifier_get_vgroup(Object *ob, DerivedMesh *dm, const char *name, MDeformV
} }
} }
/* TODO(sybren): replace the above function with this one, once we got rid of DerivedMesh for modifiers. */
void modifier_get_vgroup_mesh(Object *ob, struct Mesh *mesh, const char *name, MDeformVert **dvert, int *defgrp_index)
{
*defgrp_index = defgroup_name_index(ob, name);
*dvert = NULL;
if (*defgrp_index != -1) {
if (ob->type == OB_LATTICE)
*dvert = BKE_lattice_deform_verts_get(ob);
else if (mesh)
*dvert = mesh->dvert;
}
}
/* only called by BKE_modifier.h/modifier.c */ /* only called by BKE_modifier.h/modifier.c */
void modifier_type_init(ModifierTypeInfo *types[]) void modifier_type_init(ModifierTypeInfo *types[])

View File

@@ -51,5 +51,7 @@ struct DerivedMesh *get_dm(struct Object *ob, struct BMEditMesh *em, struct Deri
struct DerivedMesh *get_dm_for_modifier(struct Object *ob, ModifierApplyFlag flag); struct DerivedMesh *get_dm_for_modifier(struct Object *ob, ModifierApplyFlag flag);
void modifier_get_vgroup(struct Object *ob, struct DerivedMesh *dm, void modifier_get_vgroup(struct Object *ob, struct DerivedMesh *dm,
const char *name, struct MDeformVert **dvert, int *defgrp_index); const char *name, struct MDeformVert **dvert, int *defgrp_index);
void modifier_get_vgroup_mesh(struct Object *ob, struct Mesh *mesh,
const char *name, struct MDeformVert **dvert, int *defgrp_index);
#endif /* __MOD_UTIL_H__ */ #endif /* __MOD_UTIL_H__ */