2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2005 Blender Foundation.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/DerivedMesh.c
36 #include "MEM_guardedalloc.h"
38 #include "DNA_cloth_types.h"
39 #include "DNA_key_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h" // N_T
45 #include "BLI_blenlib.h"
46 #include "BLI_editVert.h"
48 #include "BLI_memarena.h"
50 #include "BLI_utildefines.h"
51 #include "BLI_linklist.h"
53 #include "BKE_cdderivedmesh.h"
54 #include "BKE_displist.h"
56 #include "BKE_modifier.h"
58 #include "BKE_object.h"
59 #include "BKE_paint.h"
60 #include "BKE_texture.h"
61 #include "BKE_multires.h"
62 #include "BKE_armature.h"
64 #ifdef WITH_GAMEENGINE
65 #include "BKE_navmesh_conversion.h"
66 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm);
69 #include "BLO_sys_types.h" // for intptr_t support
73 #include "GPU_buffers.h"
75 #include "GPU_extensions.h"
76 #include "GPU_material.h"
78 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
80 ///////////////////////////////////
81 ///////////////////////////////////
83 static MVert *dm_getVertArray(DerivedMesh *dm)
85 MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
88 mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL,
90 CustomData_set_layer_flag(&dm->vertData, CD_MVERT, CD_FLAG_TEMPORARY);
91 dm->copyVertArray(dm, mvert);
97 static MEdge *dm_getEdgeArray(DerivedMesh *dm)
99 MEdge *medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
102 medge = CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL,
103 dm->getNumEdges(dm));
104 CustomData_set_layer_flag(&dm->edgeData, CD_MEDGE, CD_FLAG_TEMPORARY);
105 dm->copyEdgeArray(dm, medge);
111 static MFace *dm_getFaceArray(DerivedMesh *dm)
113 MFace *mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
116 mface = CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL,
117 dm->getNumFaces(dm));
118 CustomData_set_layer_flag(&dm->faceData, CD_MFACE, CD_FLAG_TEMPORARY);
119 dm->copyFaceArray(dm, mface);
125 static MVert *dm_dupVertArray(DerivedMesh *dm)
127 MVert *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumVerts(dm),
128 "dm_dupVertArray tmp");
130 if(tmp) dm->copyVertArray(dm, tmp);
135 static MEdge *dm_dupEdgeArray(DerivedMesh *dm)
137 MEdge *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumEdges(dm),
138 "dm_dupEdgeArray tmp");
140 if(tmp) dm->copyEdgeArray(dm, tmp);
145 static MFace *dm_dupFaceArray(DerivedMesh *dm)
147 MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumFaces(dm),
148 "dm_dupFaceArray tmp");
150 if(tmp) dm->copyFaceArray(dm, tmp);
155 void DM_init_funcs(DerivedMesh *dm)
157 /* default function implementations */
158 dm->getVertArray = dm_getVertArray;
159 dm->getEdgeArray = dm_getEdgeArray;
160 dm->getFaceArray = dm_getFaceArray;
161 dm->dupVertArray = dm_dupVertArray;
162 dm->dupEdgeArray = dm_dupEdgeArray;
163 dm->dupFaceArray = dm_dupFaceArray;
165 dm->getVertData = DM_get_vert_data;
166 dm->getEdgeData = DM_get_edge_data;
167 dm->getFaceData = DM_get_face_data;
168 dm->getVertDataArray = DM_get_vert_data_layer;
169 dm->getEdgeDataArray = DM_get_edge_data_layer;
170 dm->getFaceDataArray = DM_get_face_data_layer;
172 bvhcache_init(&dm->bvhCache);
175 void DM_init(DerivedMesh *dm, DerivedMeshType type,
176 int numVerts, int numEdges, int numFaces)
179 dm->numVertData = numVerts;
180 dm->numEdgeData = numEdges;
181 dm->numFaceData = numFaces;
188 void DM_from_template(DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type,
189 int numVerts, int numEdges, int numFaces)
191 CustomData_copy(&source->vertData, &dm->vertData, CD_MASK_DERIVEDMESH,
192 CD_CALLOC, numVerts);
193 CustomData_copy(&source->edgeData, &dm->edgeData, CD_MASK_DERIVEDMESH,
194 CD_CALLOC, numEdges);
195 CustomData_copy(&source->faceData, &dm->faceData, CD_MASK_DERIVEDMESH,
196 CD_CALLOC, numFaces);
199 dm->numVertData = numVerts;
200 dm->numEdgeData = numEdges;
201 dm->numFaceData = numFaces;
208 int DM_release(DerivedMesh *dm)
211 bvhcache_free(&dm->bvhCache);
212 GPU_drawobject_free( dm );
213 CustomData_free(&dm->vertData, dm->numVertData);
214 CustomData_free(&dm->edgeData, dm->numEdgeData);
215 CustomData_free(&dm->faceData, dm->numFaceData);
220 CustomData_free_temporary(&dm->vertData, dm->numVertData);
221 CustomData_free_temporary(&dm->edgeData, dm->numEdgeData);
222 CustomData_free_temporary(&dm->faceData, dm->numFaceData);
228 void DM_to_mesh(DerivedMesh *dm, Mesh *me)
230 /* dm might depend on me, so we need to do everything with a local copy */
232 int totvert, totedge, totface;
234 memset(&tmp.vdata, 0, sizeof(tmp.vdata));
235 memset(&tmp.edata, 0, sizeof(tmp.edata));
236 memset(&tmp.fdata, 0, sizeof(tmp.fdata));
238 totvert = tmp.totvert = dm->getNumVerts(dm);
239 totedge = tmp.totedge = dm->getNumEdges(dm);
240 totface = tmp.totface = dm->getNumFaces(dm);
242 CustomData_copy(&dm->vertData, &tmp.vdata, CD_MASK_MESH, CD_DUPLICATE, totvert);
243 CustomData_copy(&dm->edgeData, &tmp.edata, CD_MASK_MESH, CD_DUPLICATE, totedge);
244 CustomData_copy(&dm->faceData, &tmp.fdata, CD_MASK_MESH, CD_DUPLICATE, totface);
246 /* not all DerivedMeshes store their verts/edges/faces in CustomData, so
247 we set them here in case they are missing */
248 if(!CustomData_has_layer(&tmp.vdata, CD_MVERT))
249 CustomData_add_layer(&tmp.vdata, CD_MVERT, CD_ASSIGN, dm->dupVertArray(dm), totvert);
250 if(!CustomData_has_layer(&tmp.edata, CD_MEDGE))
251 CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, dm->dupEdgeArray(dm), totedge);
252 if(!CustomData_has_layer(&tmp.fdata, CD_MFACE))
253 CustomData_add_layer(&tmp.fdata, CD_MFACE, CD_ASSIGN, dm->dupFaceArray(dm), totface);
255 /* object had got displacement layer, should copy this layer to save sculpted data */
256 /* NOTE: maybe some other layers should be copied? nazgul */
257 if(CustomData_has_layer(&me->fdata, CD_MDISPS)) {
258 if (totface == me->totface) {
259 MDisps *mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS);
260 CustomData_add_layer(&tmp.fdata, CD_MDISPS, CD_DUPLICATE, mdisps, totface);
264 mesh_update_customdata_pointers(&tmp);
266 CustomData_free(&me->vdata, me->totvert);
267 CustomData_free(&me->edata, me->totedge);
268 CustomData_free(&me->fdata, me->totface);
270 /* if the number of verts has changed, remove invalid data */
271 if(tmp.totvert != me->totvert) {
272 if(tmp.key) tmp.key->id.us--;
279 void DM_to_meshkey(DerivedMesh *dm, Mesh *me, KeyBlock *kb)
281 int a, totvert = dm->getNumVerts(dm);
285 if(totvert==0 || me->totvert==0 || me->totvert!=totvert) return;
287 if(kb->data) MEM_freeN(kb->data);
288 kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
289 kb->totelem= totvert;
292 mvert=dm->getVertDataArray(dm, CD_MVERT);
294 for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
295 copy_v3_v3(fp, mvert->co);
299 void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask)
301 CustomData_set_only_copy(&dm->vertData, mask);
302 CustomData_set_only_copy(&dm->edgeData, mask);
303 CustomData_set_only_copy(&dm->faceData, mask);
306 void DM_add_vert_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
308 CustomData_add_layer(&dm->vertData, type, alloctype, layer, dm->numVertData);
311 void DM_add_edge_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
313 CustomData_add_layer(&dm->edgeData, type, alloctype, layer, dm->numEdgeData);
316 void DM_add_face_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
318 CustomData_add_layer(&dm->faceData, type, alloctype, layer, dm->numFaceData);
321 void *DM_get_vert_data(DerivedMesh *dm, int index, int type)
323 return CustomData_get(&dm->vertData, index, type);
326 void *DM_get_edge_data(DerivedMesh *dm, int index, int type)
328 return CustomData_get(&dm->edgeData, index, type);
331 void *DM_get_face_data(DerivedMesh *dm, int index, int type)
333 return CustomData_get(&dm->faceData, index, type);
336 void *DM_get_vert_data_layer(DerivedMesh *dm, int type)
339 return dm->getVertArray(dm);
341 return CustomData_get_layer(&dm->vertData, type);
344 void *DM_get_edge_data_layer(DerivedMesh *dm, int type)
347 return dm->getEdgeArray(dm);
349 return CustomData_get_layer(&dm->edgeData, type);
352 void *DM_get_face_data_layer(DerivedMesh *dm, int type)
355 return dm->getFaceArray(dm);
357 return CustomData_get_layer(&dm->faceData, type);
360 void DM_set_vert_data(DerivedMesh *dm, int index, int type, void *data)
362 CustomData_set(&dm->vertData, index, type, data);
365 void DM_set_edge_data(DerivedMesh *dm, int index, int type, void *data)
367 CustomData_set(&dm->edgeData, index, type, data);
370 void DM_set_face_data(DerivedMesh *dm, int index, int type, void *data)
372 CustomData_set(&dm->faceData, index, type, data);
375 void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest,
376 int source_index, int dest_index, int count)
378 CustomData_copy_data(&source->vertData, &dest->vertData,
379 source_index, dest_index, count);
382 void DM_copy_edge_data(DerivedMesh *source, DerivedMesh *dest,
383 int source_index, int dest_index, int count)
385 CustomData_copy_data(&source->edgeData, &dest->edgeData,
386 source_index, dest_index, count);
389 void DM_copy_face_data(DerivedMesh *source, DerivedMesh *dest,
390 int source_index, int dest_index, int count)
392 CustomData_copy_data(&source->faceData, &dest->faceData,
393 source_index, dest_index, count);
396 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count)
398 CustomData_free_elem(&dm->vertData, index, count);
401 void DM_free_edge_data(struct DerivedMesh *dm, int index, int count)
403 CustomData_free_elem(&dm->edgeData, index, count);
406 void DM_free_face_data(struct DerivedMesh *dm, int index, int count)
408 CustomData_free_elem(&dm->faceData, index, count);
411 void DM_interp_vert_data(DerivedMesh *source, DerivedMesh *dest,
412 int *src_indices, float *weights,
413 int count, int dest_index)
415 CustomData_interp(&source->vertData, &dest->vertData, src_indices,
416 weights, NULL, count, dest_index);
419 void DM_interp_edge_data(DerivedMesh *source, DerivedMesh *dest,
421 float *weights, EdgeVertWeight *vert_weights,
422 int count, int dest_index)
424 CustomData_interp(&source->edgeData, &dest->edgeData, src_indices,
425 weights, (float*)vert_weights, count, dest_index);
428 void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
430 float *weights, FaceVertWeight *vert_weights,
431 int count, int dest_index)
433 CustomData_interp(&source->faceData, &dest->faceData, src_indices,
434 weights, (float*)vert_weights, count, dest_index);
437 void DM_swap_face_data(DerivedMesh *dm, int index, const int *corner_indices)
439 CustomData_swap(&dm->faceData, index, corner_indices);
444 DerivedMesh *mesh_create_derived(Mesh *me, Object *ob, float (*vertCos)[3])
446 DerivedMesh *dm = CDDM_from_mesh(me, ob);
452 CDDM_apply_vert_coords(dm, vertCos);
454 CDDM_calc_normals(dm);
459 DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, ModifierData *md)
462 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
467 if (!(md->mode&eModifierMode_Realtime)) return NULL;
468 if (mti->isDisabled && mti->isDisabled(md, 0)) return NULL;
470 if (mti->type==eModifierTypeType_OnlyDeform) {
472 float (*deformedVerts)[3] = mesh_getVertexCos(me, &numVerts);
474 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, 0, 0);
475 dm = mesh_create_derived(me, ob, deformedVerts);
477 MEM_freeN(deformedVerts);
479 DerivedMesh *tdm = mesh_create_derived(me, ob, NULL);
480 dm = mti->applyModifier(md, ob, tdm, 0, 0);
482 if(tdm != dm) tdm->release(tdm);
488 static float *get_editmesh_orco_verts(EditMesh *em)
494 /* these may not really be the orco's, but it's only for preview.
495 * could be solver better once, but isn't simple */
498 for(eve=em->verts.first; eve; eve=eve->next)
501 orco = MEM_mallocN(sizeof(float)*3*totvert, "EditMesh Orco");
503 for(a=0, eve=em->verts.first; eve; eve=eve->next, a+=3) {
504 copy_v3_v3(orco+a, eve->co);
510 /* orco custom data layer */
512 static void *get_orco_coords_dm(Object *ob, EditMesh *em, int layer, int *free)
516 if(layer == CD_ORCO) {
517 /* get original coordinates */
521 return (float(*)[3])get_editmesh_orco_verts(em);
523 return (float(*)[3])get_mesh_orco_verts(ob);
525 else if(layer == CD_CLOTH_ORCO) {
526 /* apply shape key for cloth, this should really be solved
527 by a more flexible customdata system, but not simple */
529 ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
530 KeyBlock *kb= key_get_keyblock(ob_get_key(ob), clmd->sim_parms->shapekey_rest);
542 static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, EditMesh *em, int layer)
548 if(em) dm= CDDM_from_editmesh(em, me);
549 else dm= CDDM_from_mesh(me, ob);
551 orco= get_orco_coords_dm(ob, em, layer, &free);
554 CDDM_apply_vert_coords(dm, orco);
555 if(free) MEM_freeN(orco);
558 CDDM_calc_normals(dm);
563 static void add_orco_dm(Object *ob, EditMesh *em, DerivedMesh *dm, DerivedMesh *orcodm, int layer)
565 float (*orco)[3], (*layerorco)[3];
568 totvert= dm->getNumVerts(dm);
571 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
574 if(orcodm->getNumVerts(orcodm) == totvert)
575 orcodm->getVertCos(orcodm, orco);
577 dm->getVertCos(dm, orco);
580 orco= get_orco_coords_dm(ob, em, layer, &free);
584 transform_mesh_orco_verts(ob->data, orco, totvert, 0);
586 if(!(layerorco = DM_get_vert_data_layer(dm, layer))) {
587 DM_add_vert_layer(dm, layer, CD_CALLOC, NULL);
588 layerorco = DM_get_vert_data_layer(dm, layer);
591 memcpy(layerorco, orco, sizeof(float)*3*totvert);
592 if(free) MEM_freeN(orco);
596 /* weight paint colors */
598 /* Something of a hack, at the moment deal with weightpaint
599 * by tucking into colors during modifier eval, only in
600 * wpaint mode. Works ok but need to make sure recalc
601 * happens on enter/exit wpaint.
604 void weight_to_rgb(float r_rgb[3], const float weight)
606 const float blend= ((weight/2.0f)+0.5f);
608 if (weight<=0.25f){ // blue->cyan
610 r_rgb[1]= blend*weight*4.0f;
613 else if (weight<=0.50f){ // cyan->green
616 r_rgb[2]= blend*(1.0f-((weight-0.25f)*4.0f));
618 else if (weight <= 0.75f){ // green->yellow
619 r_rgb[0]= blend * ((weight-0.50f)*4.0f);
623 else if (weight <= 1.0f){ // yellow->red
625 r_rgb[1]= blend * (1.0f-((weight-0.75f)*4.0f));
630 /* draw_flag's for calc_weightpaint_vert_color */
632 CALC_WP_MULTIPAINT= (1<<0),
633 CALC_WP_AUTO_NORMALIZE= (1<<1),
636 static void calc_weightpaint_vert_color(Object *ob, ColorBand *coba, int vert, unsigned char *col, char *dg_flags, int selected, int UNUSED(unselected), const int draw_flag)
639 float colf[4], input = 0.0f;
643 int make_black= FALSE;
646 if ((selected > 1) && (draw_flag & CALC_WP_MULTIPAINT)) {
648 int was_a_nonzero= FALSE;
649 for (i=0; i<me->dvert[vert].totweight; i++) {
650 /* in multipaint, get the average if auto normalize is inactive
651 * get the sum if it is active */
652 if(dg_flags[me->dvert[vert].dw[i].def_nr]) {
653 if(me->dvert[vert].dw[i].weight) {
654 input+= me->dvert[vert].dw[i].weight;
660 /* make it black if the selected groups have no weight on a vertex */
661 if(was_a_nonzero == FALSE) {
664 else if ((draw_flag & CALC_WP_AUTO_NORMALIZE) == FALSE) {
665 input /= selected; /* get the average */
669 /* default, non tricky behavior */
670 for (i=0; i<me->dvert[vert].totweight; i++) {
671 if (me->dvert[vert].dw[i].def_nr==ob->actdef-1) {
672 input+=me->dvert[vert].dw[i].weight;
686 CLAMP(input, 0.0f, 1.0f);
689 do_colorband(coba, input, colf);
691 weight_to_rgb(colf, input);
693 col[3] = (unsigned char)(colf[0] * 255.0f);
694 col[2] = (unsigned char)(colf[1] * 255.0f);
695 col[1] = (unsigned char)(colf[2] * 255.0f);
699 static ColorBand *stored_cb= NULL;
701 void vDM_ColorBand_store(ColorBand *coba)
706 static void add_weight_mcol_dm(Object *ob, DerivedMesh *dm, int const draw_flag)
709 MFace *mf = me->mface;
710 ColorBand *coba= stored_cb; /* warning, not a local var */
711 unsigned char *wtcol;
714 int defbase_len = BLI_countlist(&ob->defbase);
715 char *defbase_sel = MEM_mallocN(defbase_len * sizeof(char), __func__);
716 int selected = get_selected_defgroups(ob, defbase_sel, defbase_len);
717 int unselected = defbase_len - selected;
719 wtcol = MEM_callocN (sizeof (unsigned char) * me->totface*4*4, "weightmap");
721 memset(wtcol, 0x55, sizeof (unsigned char) * me->totface*4*4);
722 for (i=0; i<me->totface; i++, mf++) {
723 calc_weightpaint_vert_color(ob, coba, mf->v1, &wtcol[(i*4 + 0)*4], defbase_sel, selected, unselected, draw_flag);
724 calc_weightpaint_vert_color(ob, coba, mf->v2, &wtcol[(i*4 + 1)*4], defbase_sel, selected, unselected, draw_flag);
725 calc_weightpaint_vert_color(ob, coba, mf->v3, &wtcol[(i*4 + 2)*4], defbase_sel, selected, unselected, draw_flag);
727 calc_weightpaint_vert_color(ob, coba, mf->v4, &wtcol[(i*4 + 3)*4], defbase_sel, selected, unselected, draw_flag);
730 MEM_freeN(defbase_sel);
732 CustomData_add_layer(&dm->faceData, CD_WEIGHT_MCOL, CD_ASSIGN, wtcol, dm->numFaceData);
735 /* new value for useDeform -1 (hack for the gameengine):
736 * - apply only the modifier stack of the object, skipping the virtual modifiers,
737 * - don't apply the key
738 * - apply deform modifiers and input vertexco
740 static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos)[3],
741 DerivedMesh **deform_r, DerivedMesh **final_r,
742 int useRenderParams, int useDeform,
743 int needMapping, CustomDataMask dataMask, int index, int useCache)
746 ModifierData *firstmd, *md;
747 LinkNode *datamasks, *curr;
748 CustomDataMask mask, nextmask, append_mask = 0;
749 float (*deformedVerts)[3] = NULL;
750 DerivedMesh *dm, *orcodm, *clothorcodm, *finaldm;
751 int numVerts = me->totvert;
753 int isPrevDeform= FALSE;
754 int skipVirtualArmature = (useDeform < 0);
755 MultiresModifierData *mmd= get_multires_modifier(scene, ob, 0);
756 int has_multires = mmd != NULL, multires_applied = 0;
757 int sculpt_mode = ob->mode & OB_MODE_SCULPT && ob->sculpt;
759 int draw_flag= ((scene->toolsettings->multipaint ? CALC_WP_MULTIPAINT : 0) |
760 (scene->toolsettings->auto_normalize ? CALC_WP_AUTO_NORMALIZE : 0));
762 if(mmd && !mmd->sculptlvl)
765 if(!skipVirtualArmature) {
766 firstmd = modifiers_getVirtualModifierList(ob);
769 /* game engine exception */
770 firstmd = ob->modifiers.first;
771 if(firstmd && firstmd->type == eModifierType_Armature)
772 firstmd = firstmd->next;
777 modifiers_clearErrors(ob);
779 if(useRenderParams) required_mode = eModifierMode_Render;
780 else required_mode = eModifierMode_Realtime;
782 datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
785 if(deform_r) *deform_r = NULL;
790 deformedVerts = inputVertexCos;
792 /* Apply all leading deforming modifiers */
793 for(;md; md = md->next, curr = curr->next) {
794 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
798 if(!modifier_isEnabled(scene, md, required_mode)) continue;
799 if(useDeform < 0 && mti->dependsOnTime && mti->dependsOnTime(md)) continue;
801 if(mti->type == eModifierTypeType_OnlyDeform) {
803 deformedVerts = mesh_getVertexCos(me, &numVerts);
805 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, useRenderParams, useDeform);
810 /* grab modifiers until index i */
811 if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
815 /* Result of all leading deforming modifiers is cached for
816 * places that wish to use the original mesh but with deformed
817 * coordinates (vpaint, etc.)
820 *deform_r = CDDM_from_mesh(me, ob);
823 CDDM_apply_vert_coords(*deform_r, deformedVerts);
824 CDDM_calc_normals(*deform_r);
828 /* default behaviour for meshes */
830 deformedVerts = inputVertexCos;
832 deformedVerts = mesh_getVertexCos(me, &numVerts);
836 /* Now apply all remaining modifiers. If useDeform is off then skip
843 for(;md; md = md->next, curr = curr->next) {
844 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
848 if(!modifier_isEnabled(scene, md, required_mode)) continue;
849 if(mti->type == eModifierTypeType_OnlyDeform && !useDeform) continue;
850 if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
851 modifier_setError(md, "Modifier requires original data, bad stack position.");
854 if(sculpt_mode && (!has_multires || multires_applied)) {
857 if(scene->toolsettings->sculpt->flags & SCULPT_ONLY_DEFORM)
858 unsupported|= mti->type != eModifierTypeType_OnlyDeform;
860 unsupported|= md->type == eModifierType_Multires && ((MultiresModifierData*)md)->sculptlvl==0;
861 unsupported|= multires_applied;
864 modifier_setError(md, "Not supported in sculpt mode.");
868 if(needMapping && !modifier_supportsMapping(md)) continue;
869 if(useDeform < 0 && mti->dependsOnTime && mti->dependsOnTime(md)) continue;
871 /* add an orco layer if needed by this modifier */
872 if(mti->requiredDataMask)
873 mask = mti->requiredDataMask(ob, md);
877 if(dm && (mask & CD_MASK_ORCO))
878 add_orco_dm(ob, NULL, dm, orcodm, CD_ORCO);
880 /* How to apply modifier depends on (a) what we already have as
881 * a result of previous modifiers (could be a DerivedMesh or just
882 * deformed vertices) and (b) what type the modifier is.
885 if(mti->type == eModifierTypeType_OnlyDeform) {
886 /* No existing verts to deform, need to build them. */
889 /* Deforming a derived mesh, read the vertex locations
890 * out of the mesh and deform them. Once done with this
891 * run of deformers verts will be written back.
893 numVerts = dm->getNumVerts(dm);
895 MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
896 dm->getVertCos(dm, deformedVerts);
898 deformedVerts = mesh_getVertexCos(me, &numVerts);
902 /* if this is not the last modifier in the stack then recalculate the normals
903 * to avoid giving bogus normals to the next modifier see: [#23673] */
904 if(isPrevDeform && mti->dependsOnNormals && mti->dependsOnNormals(md)) {
905 /* XXX, this covers bug #23673, but we may need normal calc for other types */
906 if(dm && dm->type == DM_TYPE_CDDM) {
907 CDDM_apply_vert_coords(dm, deformedVerts);
908 CDDM_calc_normals(dm);
912 mti->deformVerts(md, ob, dm, deformedVerts, numVerts, useRenderParams, useDeform);
916 /* determine which data layers are needed by following modifiers */
918 nextmask= (CustomDataMask)GET_INT_FROM_POINTER(curr->next->link);
922 /* apply vertex coordinates or build a DerivedMesh as necessary */
925 DerivedMesh *tdm = CDDM_copy(dm);
929 CDDM_apply_vert_coords(dm, deformedVerts);
930 CDDM_calc_normals(dm);
933 dm = CDDM_from_mesh(me, ob);
936 CDDM_apply_vert_coords(dm, deformedVerts);
937 CDDM_calc_normals(dm);
940 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
941 add_weight_mcol_dm(ob, dm, draw_flag);
943 /* Constructive modifiers need to have an origindex
944 * otherwise they wont have anywhere to copy the data from.
946 * Also create ORIGINDEX data if any of the following modifiers
947 * requests it, this way Mirror, Solidify etc will keep ORIGINDEX
948 * data by using generic DM_copy_vert_data() functions.
950 if(needMapping || (nextmask & CD_MASK_ORIGINDEX)) {
952 DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
953 DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
954 DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
956 range_vn_i(DM_get_vert_data_layer(dm, CD_ORIGINDEX), dm->numVertData, 0);
957 range_vn_i(DM_get_edge_data_layer(dm, CD_ORIGINDEX), dm->numEdgeData, 0);
958 range_vn_i(DM_get_face_data_layer(dm, CD_ORIGINDEX), dm->numFaceData, 0);
963 /* set the DerivedMesh to only copy needed data */
964 mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
965 /* needMapping check here fixes bug [#28112], otherwise its
966 * possible that it wont be copied */
968 DM_set_only_copy(dm, mask | (needMapping ? CD_MASK_ORIGINDEX : 0));
970 /* add cloth rest shape key if need */
971 if(mask & CD_MASK_CLOTH_ORCO)
972 add_orco_dm(ob, NULL, dm, clothorcodm, CD_CLOTH_ORCO);
974 /* add an origspace layer if needed */
975 if(((CustomDataMask)GET_INT_FROM_POINTER(curr->link)) & CD_MASK_ORIGSPACE)
976 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
977 DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
979 ndm = mti->applyModifier(md, ob, dm, useRenderParams, useCache);
982 /* if the modifier returned a new dm, release the old one */
983 if(dm && dm != ndm) dm->release(dm);
988 if(deformedVerts != inputVertexCos)
989 MEM_freeN(deformedVerts);
991 deformedVerts = NULL;
995 /* create an orco derivedmesh in parallel */
996 if(nextmask & CD_MASK_ORCO) {
998 orcodm= create_orco_dm(ob, me, NULL, CD_ORCO);
1000 nextmask &= ~CD_MASK_ORCO;
1001 DM_set_only_copy(orcodm, nextmask | CD_MASK_ORIGINDEX);
1002 ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, 0);
1005 /* if the modifier returned a new dm, release the old one */
1006 if(orcodm && orcodm != ndm) orcodm->release(orcodm);
1011 /* create cloth orco derivedmesh in parallel */
1012 if(nextmask & CD_MASK_CLOTH_ORCO) {
1014 clothorcodm= create_orco_dm(ob, me, NULL, CD_CLOTH_ORCO);
1016 nextmask &= ~CD_MASK_CLOTH_ORCO;
1017 DM_set_only_copy(clothorcodm, nextmask | CD_MASK_ORIGINDEX);
1018 ndm = mti->applyModifier(md, ob, clothorcodm, useRenderParams, 0);
1021 /* if the modifier returned a new dm, release the old one */
1022 if(clothorcodm && clothorcodm != ndm) clothorcodm->release(clothorcodm);
1027 /* in case of dynamic paint, make sure preview mask remains for following modifiers */
1028 if (md->type == eModifierType_DynamicPaint)
1029 append_mask |= CD_MASK_WEIGHT_MCOL;
1032 isPrevDeform= (mti->type == eModifierTypeType_OnlyDeform);
1034 /* grab modifiers until index i */
1035 if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
1038 if(sculpt_mode && md->type == eModifierType_Multires)
1039 multires_applied = 1;
1042 for(md=firstmd; md; md=md->next)
1043 modifier_freeTemporaryData(md);
1045 /* Yay, we are done. If we have a DerivedMesh and deformed vertices
1046 * need to apply these back onto the DerivedMesh. If we have no
1047 * DerivedMesh then we need to build one.
1049 if(dm && deformedVerts) {
1050 finaldm = CDDM_copy(dm);
1054 CDDM_apply_vert_coords(finaldm, deformedVerts);
1055 CDDM_calc_normals(finaldm);
1057 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
1058 add_weight_mcol_dm(ob, finaldm, draw_flag);
1062 finaldm = CDDM_from_mesh(me, ob);
1065 CDDM_apply_vert_coords(finaldm, deformedVerts);
1066 CDDM_calc_normals(finaldm);
1069 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
1070 add_weight_mcol_dm(ob, finaldm, draw_flag);
1073 /* add an orco layer if needed */
1074 if(dataMask & CD_MASK_ORCO) {
1075 add_orco_dm(ob, NULL, finaldm, orcodm, CD_ORCO);
1077 if(deform_r && *deform_r)
1078 add_orco_dm(ob, NULL, *deform_r, NULL, CD_ORCO);
1081 #ifdef WITH_GAMEENGINE
1082 /* NavMesh - this is a hack but saves having a NavMesh modifier */
1083 if ((ob->gameflag & OB_NAVMESH) && (finaldm->type == DM_TYPE_CDDM)) {
1085 tdm= navmesh_dm_createNavMeshForVisualization(finaldm);
1086 if (finaldm != tdm) {
1087 finaldm->release(finaldm);
1091 #endif /* WITH_GAMEENGINE */
1096 orcodm->release(orcodm);
1098 clothorcodm->release(clothorcodm);
1100 if(deformedVerts && deformedVerts != inputVertexCos)
1101 MEM_freeN(deformedVerts);
1103 BLI_linklist_free(datamasks, NULL);
1106 float (*editmesh_get_vertex_cos(EditMesh *em, int *numVerts_r))[3]
1108 int i, numVerts = *numVerts_r = BLI_countlist(&em->verts);
1112 cos = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos");
1113 for (i=0,eve=em->verts.first; i<numVerts; i++,eve=eve->next) {
1114 copy_v3_v3(cos[i], eve->co);
1120 int editmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
1122 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1123 int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1125 if(!modifier_isEnabled(scene, md, required_mode)) return 0;
1126 if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
1127 modifier_setError(md, "Modifier requires original data, bad stack position.");
1134 static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, DerivedMesh **cage_r,
1135 DerivedMesh **final_r,
1136 CustomDataMask dataMask)
1139 float (*deformedVerts)[3] = NULL;
1140 CustomDataMask mask;
1141 DerivedMesh *dm, *orcodm = NULL;
1142 int i, numVerts = 0, cageIndex = modifiers_getCageIndex(scene, ob, NULL, 1);
1143 LinkNode *datamasks, *curr;
1144 int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1146 modifiers_clearErrors(ob);
1148 if(cage_r && cageIndex == -1) {
1149 *cage_r = editmesh_get_derived(em, NULL);
1153 md = modifiers_getVirtualModifierList(ob);
1155 datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
1158 for(i = 0; md; i++, md = md->next, curr = curr->next) {
1159 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1163 if(!editmesh_modifier_is_enabled(scene, md, dm))
1166 /* add an orco layer if needed by this modifier */
1167 if(dm && mti->requiredDataMask) {
1168 mask = mti->requiredDataMask(ob, md);
1169 if(mask & CD_MASK_ORCO)
1170 add_orco_dm(ob, em, dm, orcodm, CD_ORCO);
1173 /* How to apply modifier depends on (a) what we already have as
1174 * a result of previous modifiers (could be a DerivedMesh or just
1175 * deformed vertices) and (b) what type the modifier is.
1178 if(mti->type == eModifierTypeType_OnlyDeform) {
1179 /* No existing verts to deform, need to build them. */
1180 if(!deformedVerts) {
1182 /* Deforming a derived mesh, read the vertex locations
1183 * out of the mesh and deform them. Once done with this
1184 * run of deformers verts will be written back.
1186 numVerts = dm->getNumVerts(dm);
1188 MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
1189 dm->getVertCos(dm, deformedVerts);
1191 deformedVerts = editmesh_get_vertex_cos(em, &numVerts);
1195 if (mti->deformVertsEM)
1196 mti->deformVertsEM(md, ob, em, dm, deformedVerts, numVerts);
1197 else mti->deformVerts(md, ob, dm, deformedVerts, numVerts, 0, 0);
1201 /* apply vertex coordinates or build a DerivedMesh as necessary */
1204 DerivedMesh *tdm = CDDM_copy(dm);
1205 if(!(cage_r && dm == *cage_r)) dm->release(dm);
1208 CDDM_apply_vert_coords(dm, deformedVerts);
1209 CDDM_calc_normals(dm);
1210 } else if(cage_r && dm == *cage_r) {
1211 /* dm may be changed by this modifier, so we need to copy it
1217 dm = CDDM_from_editmesh(em, ob->data);
1220 CDDM_apply_vert_coords(dm, deformedVerts);
1221 CDDM_calc_normals(dm);
1225 /* create an orco derivedmesh in parallel */
1226 mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
1227 if(mask & CD_MASK_ORCO) {
1229 orcodm= create_orco_dm(ob, ob->data, em, CD_ORCO);
1231 mask &= ~CD_MASK_ORCO;
1232 DM_set_only_copy(orcodm, mask | CD_MASK_ORIGINDEX);
1234 if (mti->applyModifierEM)
1235 ndm = mti->applyModifierEM(md, ob, em, orcodm);
1237 ndm = mti->applyModifier(md, ob, orcodm, 0, 0);
1240 /* if the modifier returned a new dm, release the old one */
1241 if(orcodm && orcodm != ndm) orcodm->release(orcodm);
1246 /* set the DerivedMesh to only copy needed data */
1247 mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link); /* CD_MASK_ORCO may have been cleared above */
1249 DM_set_only_copy(dm, mask | CD_MASK_ORIGINDEX);
1251 if(mask & CD_MASK_ORIGSPACE)
1252 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
1253 DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
1255 if (mti->applyModifierEM)
1256 ndm = mti->applyModifierEM(md, ob, em, dm);
1258 ndm = mti->applyModifier(md, ob, dm, 0, 0);
1266 if (deformedVerts) {
1267 MEM_freeN(deformedVerts);
1268 deformedVerts = NULL;
1273 if(cage_r && i == cageIndex) {
1274 if(dm && deformedVerts) {
1275 *cage_r = CDDM_copy(dm);
1276 CDDM_apply_vert_coords(*cage_r, deformedVerts);
1281 editmesh_get_derived(em,
1282 deformedVerts ? MEM_dupallocN(deformedVerts) : NULL);
1287 BLI_linklist_free(datamasks, NULL);
1289 /* Yay, we are done. If we have a DerivedMesh and deformed vertices need
1290 * to apply these back onto the DerivedMesh. If we have no DerivedMesh
1291 * then we need to build one.
1293 if(dm && deformedVerts) {
1294 *final_r = CDDM_copy(dm);
1296 if(!(cage_r && dm == *cage_r)) dm->release(dm);
1298 CDDM_apply_vert_coords(*final_r, deformedVerts);
1299 CDDM_calc_normals(*final_r);
1302 } else if (!deformedVerts && cage_r && *cage_r) {
1305 *final_r = editmesh_get_derived(em, deformedVerts);
1306 deformedVerts = NULL;
1309 /* add an orco layer if needed */
1310 if(dataMask & CD_MASK_ORCO)
1311 add_orco_dm(ob, em, *final_r, orcodm, CD_ORCO);
1314 orcodm->release(orcodm);
1317 MEM_freeN(deformedVerts);
1320 static void clear_mesh_caches(Object *ob)
1324 /* also serves as signal to remake texspace */
1334 freedisplist(&ob->disp);
1336 if (ob->derivedFinal) {
1337 ob->derivedFinal->needsFree = 1;
1338 ob->derivedFinal->release(ob->derivedFinal);
1339 ob->derivedFinal= NULL;
1341 if (ob->derivedDeform) {
1342 ob->derivedDeform->needsFree = 1;
1343 ob->derivedDeform->release(ob->derivedDeform);
1344 ob->derivedDeform= NULL;
1348 object_sculpt_modifiers_changed(ob);
1352 static void mesh_build_data(Scene *scene, Object *ob, CustomDataMask dataMask)
1354 Object *obact = scene->basact?scene->basact->object:NULL;
1355 int editing = paint_facesel_test(ob) || paint_vertsel_test(ob);/* paint_vertsel_test */
1356 /* weight paint and face select need original indices because of selection buffer drawing */
1357 int needMapping = (ob==obact) && (editing || (ob->mode & (OB_MODE_WEIGHT_PAINT|OB_MODE_VERTEX_PAINT)));
1359 clear_mesh_caches(ob);
1361 mesh_calc_modifiers(scene, ob, NULL, &ob->derivedDeform,
1362 &ob->derivedFinal, 0, 1,
1363 needMapping, dataMask, -1, 1);
1365 DM_set_object_boundbox (ob, ob->derivedFinal);
1367 ob->derivedFinal->needsFree = 0;
1368 ob->derivedDeform->needsFree = 0;
1369 ob->lastDataMask = dataMask;
1372 static void editmesh_build_data(Scene *scene, Object *obedit, EditMesh *em, CustomDataMask dataMask)
1374 clear_mesh_caches(obedit);
1376 if (em->derivedFinal) {
1377 if (em->derivedFinal!=em->derivedCage) {
1378 em->derivedFinal->needsFree = 1;
1379 em->derivedFinal->release(em->derivedFinal);
1381 em->derivedFinal = NULL;
1383 if (em->derivedCage) {
1384 em->derivedCage->needsFree = 1;
1385 em->derivedCage->release(em->derivedCage);
1386 em->derivedCage = NULL;
1389 editmesh_calc_modifiers(scene, obedit, em, &em->derivedCage, &em->derivedFinal, dataMask);
1390 DM_set_object_boundbox (obedit, em->derivedFinal);
1392 em->lastDataMask = dataMask;
1393 em->derivedFinal->needsFree = 0;
1394 em->derivedCage->needsFree = 0;
1397 void makeDerivedMesh(Scene *scene, Object *ob, EditMesh *em, CustomDataMask dataMask)
1400 editmesh_build_data(scene, ob, em, dataMask);
1402 mesh_build_data(scene, ob, dataMask);
1408 DerivedMesh *mesh_get_derived_final(Scene *scene, Object *ob, CustomDataMask dataMask)
1410 /* if there's no derived mesh or the last data mask used doesn't include
1411 * the data we need, rebuild the derived mesh
1413 if(!ob->derivedFinal || (dataMask & ob->lastDataMask) != dataMask)
1414 mesh_build_data(scene, ob, dataMask);
1416 return ob->derivedFinal;
1419 DerivedMesh *mesh_get_derived_deform(Scene *scene, Object *ob, CustomDataMask dataMask)
1421 /* if there's no derived mesh or the last data mask used doesn't include
1422 * the data we need, rebuild the derived mesh
1424 if(!ob->derivedDeform || (dataMask & ob->lastDataMask) != dataMask)
1425 mesh_build_data(scene, ob, dataMask);
1427 return ob->derivedDeform;
1430 DerivedMesh *mesh_create_derived_render(Scene *scene, Object *ob, CustomDataMask dataMask)
1434 mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, -1, 0);
1439 DerivedMesh *mesh_create_derived_index_render(Scene *scene, Object *ob, CustomDataMask dataMask, int index)
1443 mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, index, 0);
1448 DerivedMesh *mesh_create_derived_view(Scene *scene, Object *ob, CustomDataMask dataMask)
1452 mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 0, 1, 0, dataMask, -1, 0);
1457 DerivedMesh *mesh_create_derived_no_deform(Scene *scene, Object *ob, float (*vertCos)[3],
1458 CustomDataMask dataMask)
1462 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, 0, 0, dataMask, -1, 0);
1467 DerivedMesh *mesh_create_derived_no_virtual(Scene *scene, Object *ob, float (*vertCos)[3],
1468 CustomDataMask dataMask)
1472 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 0, dataMask, -1, 0);
1477 DerivedMesh *mesh_create_derived_physics(Scene *scene, Object *ob, float (*vertCos)[3],
1478 CustomDataMask dataMask)
1482 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 1, dataMask, -1, 0);
1487 DerivedMesh *mesh_create_derived_no_deform_render(Scene *scene, Object *ob,
1488 float (*vertCos)[3],
1489 CustomDataMask dataMask)
1493 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 1, 0, 0, dataMask, -1, 0);
1500 DerivedMesh *editmesh_get_derived_cage_and_final(Scene *scene, Object *obedit, EditMesh *em, DerivedMesh **final_r,
1501 CustomDataMask dataMask)
1503 /* if there's no derived mesh or the last data mask used doesn't include
1504 * the data we need, rebuild the derived mesh
1506 if(!em->derivedCage ||
1507 (em->lastDataMask & dataMask) != dataMask)
1508 editmesh_build_data(scene, obedit, em, dataMask);
1510 *final_r = em->derivedFinal;
1511 return em->derivedCage;
1514 DerivedMesh *editmesh_get_derived_cage(Scene *scene, Object *obedit, EditMesh *em, CustomDataMask dataMask)
1516 /* if there's no derived mesh or the last data mask used doesn't include
1517 * the data we need, rebuild the derived mesh
1519 if(!em->derivedCage ||
1520 (em->lastDataMask & dataMask) != dataMask)
1521 editmesh_build_data(scene, obedit, em, dataMask);
1523 return em->derivedCage;
1526 DerivedMesh *editmesh_get_derived_base(Object *UNUSED(obedit), EditMesh *em)
1528 return editmesh_get_derived(em, NULL);
1532 /* ********* For those who don't grasp derived stuff! (ton) :) *************** */
1534 static void make_vertexcosnos__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
1536 float *vec = userData;
1540 /* check if we've been here before (normal should not be 0) */
1541 if(vec[3] || vec[4] || vec[5]) return;
1543 copy_v3_v3(vec, co);
1546 copy_v3_v3(vec, no_f);
1549 normal_short_to_float_v3(vec, no_s);
1553 /* always returns original amount me->totvert of vertices and normals, but fully deformed and subsurfered */
1554 /* this is needed for all code using vertexgroups (no subsurf support) */
1555 /* it stores the normals as floats, but they can still be scaled as shorts (32767 = unit) */
1556 /* in use now by vertex/weight paint and particle generating */
1558 float *mesh_get_mapped_verts_nors(Scene *scene, Object *ob)
1562 float *vertexcosnos;
1564 /* lets prevent crashing... */
1565 if(ob->type!=OB_MESH || me->totvert==0)
1568 dm= mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
1569 vertexcosnos= MEM_callocN(6*sizeof(float)*me->totvert, "vertexcosnos map");
1571 if(dm->foreachMappedVert) {
1572 dm->foreachMappedVert(dm, make_vertexcosnos__mapFunc, vertexcosnos);
1575 float *fp= vertexcosnos;
1578 for(a=0; a< me->totvert; a++, fp+=6) {
1579 dm->getVertCo(dm, a, fp);
1580 dm->getVertNo(dm, a, fp+3);
1585 return vertexcosnos;
1588 /* ******************* GLSL ******************** */
1592 float * precomputedFaceNormals;
1593 MTFace * mtface; // texture coordinates
1594 MFace * mface; // indices
1595 MVert * mvert; // vertices & normals
1597 float (*tangent)[4]; // destination
1600 } SGLSLMeshToTangent;
1603 #include "mikktspace.h"
1605 static int GetNumFaces(const SMikkTSpaceContext * pContext)
1607 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1608 return pMesh->numFaces;
1611 static int GetNumVertsOfFace(const SMikkTSpaceContext * pContext, const int face_num)
1613 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1614 return pMesh->mface[face_num].v4!=0 ? 4 : 3;
1617 static void GetPosition(const SMikkTSpaceContext * pContext, float fPos[], const int face_num, const int vert_index)
1619 //assert(vert_index>=0 && vert_index<4);
1620 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1621 const float *co= pMesh->mvert[(&pMesh->mface[face_num].v1)[vert_index]].co;
1622 copy_v3_v3(fPos, co);
1625 static void GetTextureCoordinate(const SMikkTSpaceContext * pContext, float fUV[], const int face_num, const int vert_index)
1627 //assert(vert_index>=0 && vert_index<4);
1628 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1630 if(pMesh->mtface!=NULL) {
1631 float * uv = pMesh->mtface[face_num].uv[vert_index];
1632 fUV[0]=uv[0]; fUV[1]=uv[1];
1635 const float *orco= pMesh->orco[(&pMesh->mface[face_num].v1)[vert_index]];
1636 map_to_sphere( &fUV[0], &fUV[1], orco[0], orco[1], orco[2]);
1640 static void GetNormal(const SMikkTSpaceContext * pContext, float fNorm[], const int face_num, const int vert_index)
1642 //assert(vert_index>=0 && vert_index<4);
1643 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1645 const int smoothnormal = (pMesh->mface[face_num].flag & ME_SMOOTH);
1646 if(!smoothnormal) { // flat
1647 if(pMesh->precomputedFaceNormals) {
1648 copy_v3_v3(fNorm, &pMesh->precomputedFaceNormals[3*face_num]);
1651 MFace *mf= &pMesh->mface[face_num];
1652 float *p0= pMesh->mvert[mf->v1].co;
1653 float *p1= pMesh->mvert[mf->v2].co;
1654 float *p2= pMesh->mvert[mf->v3].co;
1657 float *p3 = pMesh->mvert[mf->v4].co;
1658 normal_quad_v3(fNorm, p0, p1, p2, p3);
1661 normal_tri_v3(fNorm, p0, p1, p2);
1666 const short *no= pMesh->mvert[(&pMesh->mface[face_num].v1)[vert_index]].no;
1667 normal_short_to_float_v3(fNorm, no);
1670 static void SetTSpace(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int face_num, const int iVert)
1672 //assert(vert_index>=0 && vert_index<4);
1673 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1674 float * pRes = pMesh->tangent[4*face_num+iVert];
1675 copy_v3_v3(pRes, fvTangent);
1680 void DM_add_tangent_layer(DerivedMesh *dm)
1683 MTFace *mtface, *tf;
1685 MVert *mvert, *v1, *v2, *v3, *v4;
1686 MemArena *arena= NULL;
1687 VertexTangent **vtangents= NULL;
1688 float (*orco)[3]= NULL, (*tangent)[4];
1689 float *uv1, *uv2, *uv3, *uv4, *vtang;
1690 float fno[3], tang[3], uv[4][2];
1691 int i, j, len, mf_vi[4], totvert, totface, iCalcNewMethod;
1694 if(CustomData_get_layer_index(&dm->faceData, CD_TANGENT) != -1)
1697 nors = dm->getFaceDataArray(dm, CD_NORMAL);
1699 /* check we have all the needed layers */
1700 totvert= dm->getNumVerts(dm);
1701 totface= dm->getNumFaces(dm);
1703 mvert= dm->getVertArray(dm);
1704 mface= dm->getFaceArray(dm);
1705 mtface= dm->getFaceDataArray(dm, CD_MTFACE);
1708 orco= dm->getVertDataArray(dm, CD_ORCO);
1713 /* create tangent layer */
1714 DM_add_face_layer(dm, CD_TANGENT, CD_CALLOC, NULL);
1715 tangent= DM_get_face_data_layer(dm, CD_TANGENT);
1717 /* allocate some space */
1718 arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "tangent layer arena");
1719 BLI_memarena_use_calloc(arena);
1720 vtangents= MEM_callocN(sizeof(VertexTangent*)*totvert, "VertexTangent");
1722 // new computation method
1724 if(iCalcNewMethod != 0) {
1725 SGLSLMeshToTangent mesh2tangent= {0};
1726 SMikkTSpaceContext sContext= {0};
1727 SMikkTSpaceInterface sInterface= {0};
1729 mesh2tangent.precomputedFaceNormals = nors;
1730 mesh2tangent.mtface = mtface;
1731 mesh2tangent.mface = mface;
1732 mesh2tangent.mvert = mvert;
1733 mesh2tangent.orco = orco;
1734 mesh2tangent.tangent = tangent;
1735 mesh2tangent.numFaces = totface;
1737 sContext.m_pUserData = &mesh2tangent;
1738 sContext.m_pInterface = &sInterface;
1739 sInterface.m_getNumFaces = GetNumFaces;
1740 sInterface.m_getNumVerticesOfFace = GetNumVertsOfFace;
1741 sInterface.m_getPosition = GetPosition;
1742 sInterface.m_getTexCoord = GetTextureCoordinate;
1743 sInterface.m_getNormal = GetNormal;
1744 sInterface.m_setTSpaceBasic = SetTSpace;
1747 iCalcNewMethod = genTangSpaceDefault(&sContext);
1750 if(!iCalcNewMethod) {
1751 /* sum tangents at connected vertices */
1752 for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++) {
1759 normal_quad_v3( fno,v4->co, v3->co, v2->co, v1->co);
1763 normal_tri_v3( fno,v3->co, v2->co, v1->co);
1773 uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
1774 map_to_sphere( &uv[0][0], &uv[0][1],orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2]);
1775 map_to_sphere( &uv[1][0], &uv[1][1],orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2]);
1776 map_to_sphere( &uv[2][0], &uv[2][1],orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2]);
1778 map_to_sphere( &uv[3][0], &uv[3][1],orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2]);
1781 tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, fno, tang);
1782 sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
1783 sum_or_add_vertex_tangent(arena, &vtangents[mf->v2], tang, uv2);
1784 sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
1789 tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, fno, tang);
1790 sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
1791 sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
1792 sum_or_add_vertex_tangent(arena, &vtangents[mf->v4], tang, uv4);
1796 /* write tangent to layer */
1797 for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++, tangent+=4) {
1798 len= (mf->v4)? 4 : 3;
1800 if(mtface == NULL) {
1801 map_to_sphere( &uv[0][0], &uv[0][1],orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2]);
1802 map_to_sphere( &uv[1][0], &uv[1][1],orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2]);
1803 map_to_sphere( &uv[2][0], &uv[2][1],orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2]);
1805 map_to_sphere( &uv[3][0], &uv[3][1],orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2]);
1813 for(j=0; j<len; j++) {
1814 vtang= find_vertex_tangent(vtangents[mf_vi[j]], mtface ? tf->uv[j] : uv[j]);
1815 normalize_v3_v3(tangent[j], vtang);
1816 ((float *) tangent[j])[3]=1.0f;
1821 BLI_memarena_free(arena);
1822 MEM_freeN(vtangents);
1825 void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs, DMVertexAttribs *attribs)
1827 CustomData *vdata, *fdata, *tfdata = NULL;
1830 /* From the layers requested by the GLSL shader, figure out which ones are
1831 * actually available for this derivedmesh, and retrieve the pointers */
1833 memset(attribs, 0, sizeof(DMVertexAttribs));
1835 vdata = &dm->vertData;
1836 fdata = &dm->faceData;
1838 /* ugly hack, editmesh derivedmesh doesn't copy face data, this way we
1839 * can use offsets instead */
1840 if(dm->type == DM_TYPE_EDITMESH)
1841 tfdata = &((EditMeshDerivedMesh*)dm)->em->fdata;
1845 /* add a tangent layer if necessary */
1846 for(b = 0; b < gattribs->totlayer; b++)
1847 if(gattribs->layer[b].type == CD_TANGENT)
1848 if(CustomData_get_layer_index(fdata, CD_TANGENT) == -1)
1849 DM_add_tangent_layer(dm);
1851 for(b = 0; b < gattribs->totlayer; b++) {
1852 if(gattribs->layer[b].type == CD_MTFACE) {
1853 /* uv coordinates */
1854 if(gattribs->layer[b].name[0])
1855 layer = CustomData_get_named_layer_index(tfdata, CD_MTFACE,
1856 gattribs->layer[b].name);
1858 layer = CustomData_get_active_layer_index(tfdata, CD_MTFACE);
1861 a = attribs->tottface++;
1863 attribs->tface[a].array = tfdata->layers[layer].data;
1864 attribs->tface[a].emOffset = tfdata->layers[layer].offset;
1865 attribs->tface[a].glIndex = gattribs->layer[b].glindex;
1866 attribs->tface[a].glTexco = gattribs->layer[b].gltexco;
1869 else if(gattribs->layer[b].type == CD_MCOL) {
1871 if(gattribs->layer[b].name[0])
1872 layer = CustomData_get_named_layer_index(tfdata, CD_MCOL,
1873 gattribs->layer[b].name);
1875 layer = CustomData_get_active_layer_index(tfdata, CD_MCOL);
1878 a = attribs->totmcol++;
1880 attribs->mcol[a].array = tfdata->layers[layer].data;
1881 attribs->mcol[a].emOffset = tfdata->layers[layer].offset;
1882 attribs->mcol[a].glIndex = gattribs->layer[b].glindex;
1885 else if(gattribs->layer[b].type == CD_TANGENT) {
1887 layer = CustomData_get_layer_index(fdata, CD_TANGENT);
1890 attribs->tottang = 1;
1892 attribs->tang.array = fdata->layers[layer].data;
1893 attribs->tang.emOffset = fdata->layers[layer].offset;
1894 attribs->tang.glIndex = gattribs->layer[b].glindex;
1897 else if(gattribs->layer[b].type == CD_ORCO) {
1898 /* original coordinates */
1899 layer = CustomData_get_layer_index(vdata, CD_ORCO);
1902 attribs->totorco = 1;
1904 attribs->orco.array = vdata->layers[layer].data;
1905 attribs->orco.emOffset = vdata->layers[layer].offset;
1906 attribs->orco.glIndex = gattribs->layer[b].glindex;
1907 attribs->orco.glTexco = gattribs->layer[b].gltexco;
1913 /* Set object's bounding box based on DerivedMesh min/max data */
1914 void DM_set_object_boundbox(Object *ob, DerivedMesh *dm)
1916 float min[3], max[3];
1918 INIT_MINMAX(min, max);
1920 dm->getMinMax(dm, min, max);
1923 ob->bb= MEM_callocN(sizeof(BoundBox), "DM-BoundBox");
1925 boundbox_set_from_min_max(ob->bb, min, max);
1928 /* --- NAVMESH (begin) --- */
1929 #ifdef WITH_GAMEENGINE
1931 BM_INLINE int navmesh_bit(int a, int b)
1933 return (a & (1 << b)) >> b;
1936 BM_INLINE void navmesh_intToCol(int i, float col[3])
1938 int r = navmesh_bit(i, 0) + navmesh_bit(i, 3) * 2 + 1;
1939 int g = navmesh_bit(i, 1) + navmesh_bit(i, 4) * 2 + 1;
1940 int b = navmesh_bit(i, 2) + navmesh_bit(i, 5) * 2 + 1;
1941 col[0] = 1 - r*63.0f/255.0f;
1942 col[1] = 1 - g*63.0f/255.0f;
1943 col[2] = 1 - b*63.0f/255.0f;
1946 static void navmesh_drawColored(DerivedMesh *dm)
1949 MVert *mvert = (MVert *)CustomData_get_layer(&dm->vertData, CD_MVERT);
1950 MFace *mface = (MFace *)CustomData_get_layer(&dm->faceData, CD_MFACE);
1951 int *polygonIdx = (int *)CustomData_get_layer(&dm->faceData, CD_RECAST);
1958 //UI_ThemeColor(TH_WIRE);
1959 glDisable(GL_LIGHTING);
1961 dm->drawEdges(dm, 0, 1);
1963 glEnable(GL_LIGHTING);*/
1965 glDisable(GL_LIGHTING);
1966 /* if(GPU_buffer_legacy(dm) ) */ { /* TODO - VBO draw code, not high priority - campbell */
1967 DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" );
1968 //glShadeModel(GL_SMOOTH);
1969 glBegin(glmode = GL_QUADS);
1970 for(a = 0; a < dm->numFaceData; a++, mface++) {
1971 int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
1972 int pi = polygonIdx[a];
1977 navmesh_intToCol(pi, col);
1980 if(new_glmode != glmode) {
1982 glBegin(glmode = new_glmode);
1985 glVertex3fv(mvert[mface->v1].co);
1986 glVertex3fv(mvert[mface->v2].co);
1987 glVertex3fv(mvert[mface->v3].co);
1989 glVertex3fv(mvert[mface->v4].co);
1994 glEnable(GL_LIGHTING);
1997 static void navmesh_DM_drawFacesTex(DerivedMesh *dm,
1998 int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr),
1999 int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
2002 (void) setDrawOptions;
2003 (void) compareDrawOptions;
2006 navmesh_drawColored(dm);
2009 static void navmesh_DM_drawFacesSolid(DerivedMesh *dm,
2010 float (*partial_redraw_planes)[4],
2011 int UNUSED(fast), int (*setMaterial)(int, void *attribs))
2013 (void) partial_redraw_planes;
2016 //drawFacesSolid_original(dm, partial_redraw_planes, fast, setMaterial);
2017 navmesh_drawColored(dm);
2020 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm)
2022 DerivedMesh *result;
2023 int maxFaces = dm->getNumFaces(dm);
2025 int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0;
2027 unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL;
2028 int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
2031 result = CDDM_copy(dm);
2032 if (!CustomData_has_layer(&result->faceData, CD_RECAST)) {
2033 int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
2034 if (sourceRecastData) {
2035 CustomData_add_layer_named(&result->faceData, CD_RECAST, CD_DUPLICATE,
2036 sourceRecastData, maxFaces, "recastData");
2039 recastData = (int*)CustomData_get_layer(&result->faceData, CD_RECAST);
2041 /* note: This is not good design! - really should not be doing this */
2042 result->drawFacesTex = navmesh_DM_drawFacesTex;
2043 result->drawFacesSolid = navmesh_DM_drawFacesSolid;
2047 res = buildNavMeshDataByDerivedMesh(dm, &vertsPerPoly, &nverts, &verts, &ndtris, &dtris,
2048 &npolys, &dmeshes, &polys, &dtrisToPolysMap, &dtrisToTrisMap,
2053 /* invalidate concave polygon */
2054 for (polyIdx=0; polyIdx<(size_t)npolys; polyIdx++) {
2055 unsigned short* poly = &polys[polyIdx*2*vertsPerPoly];
2056 if (!polyIsConvex(poly, vertsPerPoly, verts)) {
2057 /* set negative polygon idx to all faces */
2058 unsigned short *dmesh = &dmeshes[4*polyIdx];
2059 unsigned short tbase = dmesh[2];
2060 unsigned short tnum = dmesh[3];
2063 for (ti=0; ti<tnum; ti++) {
2064 unsigned short triidx = dtrisToTrisMap[tbase+ti];
2065 unsigned short faceidx = trisToFacesMap[triidx];
2066 if (recastData[faceidx] > 0) {
2067 recastData[faceidx] = -recastData[faceidx];
2074 printf("Error during creation polygon infos\n");
2086 if (dtrisToPolysMap!=NULL)
2087 MEM_freeN(dtrisToPolysMap);
2088 if (dtrisToTrisMap!=NULL)
2089 MEM_freeN(dtrisToTrisMap);
2090 if (trisToFacesMap!=NULL)
2091 MEM_freeN(trisToFacesMap);
2096 #endif /* WITH_GAMEENGINE */
2098 /* --- NAVMESH (end) --- */