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"
63 #include "BKE_deform.h"
65 #ifdef WITH_GAMEENGINE
66 #include "BKE_navmesh_conversion.h"
67 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm);
70 #include "BLO_sys_types.h" // for intptr_t support
74 #include "GPU_buffers.h"
76 #include "GPU_extensions.h"
77 #include "GPU_material.h"
79 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
81 ///////////////////////////////////
82 ///////////////////////////////////
84 static MVert *dm_getVertArray(DerivedMesh *dm)
86 MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
89 mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL,
91 CustomData_set_layer_flag(&dm->vertData, CD_MVERT, CD_FLAG_TEMPORARY);
92 dm->copyVertArray(dm, mvert);
98 static MEdge *dm_getEdgeArray(DerivedMesh *dm)
100 MEdge *medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
103 medge = CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL,
104 dm->getNumEdges(dm));
105 CustomData_set_layer_flag(&dm->edgeData, CD_MEDGE, CD_FLAG_TEMPORARY);
106 dm->copyEdgeArray(dm, medge);
112 static MFace *dm_getFaceArray(DerivedMesh *dm)
114 MFace *mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
117 mface = CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL,
118 dm->getNumFaces(dm));
119 CustomData_set_layer_flag(&dm->faceData, CD_MFACE, CD_FLAG_TEMPORARY);
120 dm->copyFaceArray(dm, mface);
126 static MVert *dm_dupVertArray(DerivedMesh *dm)
128 MVert *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumVerts(dm),
129 "dm_dupVertArray tmp");
131 if(tmp) dm->copyVertArray(dm, tmp);
136 static MEdge *dm_dupEdgeArray(DerivedMesh *dm)
138 MEdge *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumEdges(dm),
139 "dm_dupEdgeArray tmp");
141 if(tmp) dm->copyEdgeArray(dm, tmp);
146 static MFace *dm_dupFaceArray(DerivedMesh *dm)
148 MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumFaces(dm),
149 "dm_dupFaceArray tmp");
151 if(tmp) dm->copyFaceArray(dm, tmp);
156 void DM_init_funcs(DerivedMesh *dm)
158 /* default function implementations */
159 dm->getVertArray = dm_getVertArray;
160 dm->getEdgeArray = dm_getEdgeArray;
161 dm->getFaceArray = dm_getFaceArray;
162 dm->dupVertArray = dm_dupVertArray;
163 dm->dupEdgeArray = dm_dupEdgeArray;
164 dm->dupFaceArray = dm_dupFaceArray;
166 dm->getVertData = DM_get_vert_data;
167 dm->getEdgeData = DM_get_edge_data;
168 dm->getFaceData = DM_get_face_data;
169 dm->getVertDataArray = DM_get_vert_data_layer;
170 dm->getEdgeDataArray = DM_get_edge_data_layer;
171 dm->getFaceDataArray = DM_get_face_data_layer;
173 bvhcache_init(&dm->bvhCache);
176 void DM_init(DerivedMesh *dm, DerivedMeshType type,
177 int numVerts, int numEdges, int numFaces)
180 dm->numVertData = numVerts;
181 dm->numEdgeData = numEdges;
182 dm->numFaceData = numFaces;
187 dm->auto_bump_scale = -1.0f;
190 void DM_from_template(DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type,
191 int numVerts, int numEdges, int numFaces)
193 CustomData_copy(&source->vertData, &dm->vertData, CD_MASK_DERIVEDMESH,
194 CD_CALLOC, numVerts);
195 CustomData_copy(&source->edgeData, &dm->edgeData, CD_MASK_DERIVEDMESH,
196 CD_CALLOC, numEdges);
197 CustomData_copy(&source->faceData, &dm->faceData, CD_MASK_DERIVEDMESH,
198 CD_CALLOC, numFaces);
201 dm->numVertData = numVerts;
202 dm->numEdgeData = numEdges;
203 dm->numFaceData = numFaces;
210 int DM_release(DerivedMesh *dm)
213 bvhcache_free(&dm->bvhCache);
214 GPU_drawobject_free( dm );
215 CustomData_free(&dm->vertData, dm->numVertData);
216 CustomData_free(&dm->edgeData, dm->numEdgeData);
217 CustomData_free(&dm->faceData, dm->numFaceData);
222 CustomData_free_temporary(&dm->vertData, dm->numVertData);
223 CustomData_free_temporary(&dm->edgeData, dm->numEdgeData);
224 CustomData_free_temporary(&dm->faceData, dm->numFaceData);
230 void DM_to_mesh(DerivedMesh *dm, Mesh *me)
232 /* dm might depend on me, so we need to do everything with a local copy */
234 int totvert, totedge, totface;
236 memset(&tmp.vdata, 0, sizeof(tmp.vdata));
237 memset(&tmp.edata, 0, sizeof(tmp.edata));
238 memset(&tmp.fdata, 0, sizeof(tmp.fdata));
240 totvert = tmp.totvert = dm->getNumVerts(dm);
241 totedge = tmp.totedge = dm->getNumEdges(dm);
242 totface = tmp.totface = dm->getNumFaces(dm);
244 CustomData_copy(&dm->vertData, &tmp.vdata, CD_MASK_MESH, CD_DUPLICATE, totvert);
245 CustomData_copy(&dm->edgeData, &tmp.edata, CD_MASK_MESH, CD_DUPLICATE, totedge);
246 CustomData_copy(&dm->faceData, &tmp.fdata, CD_MASK_MESH, CD_DUPLICATE, totface);
248 /* not all DerivedMeshes store their verts/edges/faces in CustomData, so
249 we set them here in case they are missing */
250 if(!CustomData_has_layer(&tmp.vdata, CD_MVERT))
251 CustomData_add_layer(&tmp.vdata, CD_MVERT, CD_ASSIGN, dm->dupVertArray(dm), totvert);
252 if(!CustomData_has_layer(&tmp.edata, CD_MEDGE))
253 CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, dm->dupEdgeArray(dm), totedge);
254 if(!CustomData_has_layer(&tmp.fdata, CD_MFACE))
255 CustomData_add_layer(&tmp.fdata, CD_MFACE, CD_ASSIGN, dm->dupFaceArray(dm), totface);
257 /* object had got displacement layer, should copy this layer to save sculpted data */
258 /* NOTE: maybe some other layers should be copied? nazgul */
259 if(CustomData_has_layer(&me->fdata, CD_MDISPS)) {
260 if (totface == me->totface) {
261 MDisps *mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS);
262 CustomData_add_layer(&tmp.fdata, CD_MDISPS, CD_DUPLICATE, mdisps, totface);
266 mesh_update_customdata_pointers(&tmp);
268 CustomData_free(&me->vdata, me->totvert);
269 CustomData_free(&me->edata, me->totedge);
270 CustomData_free(&me->fdata, me->totface);
272 /* if the number of verts has changed, remove invalid data */
273 if(tmp.totvert != me->totvert) {
274 if(tmp.key) tmp.key->id.us--;
281 void DM_to_meshkey(DerivedMesh *dm, Mesh *me, KeyBlock *kb)
283 int a, totvert = dm->getNumVerts(dm);
287 if(totvert==0 || me->totvert==0 || me->totvert!=totvert) return;
289 if(kb->data) MEM_freeN(kb->data);
290 kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
291 kb->totelem= totvert;
294 mvert=dm->getVertDataArray(dm, CD_MVERT);
296 for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
297 copy_v3_v3(fp, mvert->co);
301 void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask)
303 CustomData_set_only_copy(&dm->vertData, mask);
304 CustomData_set_only_copy(&dm->edgeData, mask);
305 CustomData_set_only_copy(&dm->faceData, mask);
308 void DM_add_vert_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
310 CustomData_add_layer(&dm->vertData, type, alloctype, layer, dm->numVertData);
313 void DM_add_edge_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
315 CustomData_add_layer(&dm->edgeData, type, alloctype, layer, dm->numEdgeData);
318 void DM_add_face_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
320 CustomData_add_layer(&dm->faceData, type, alloctype, layer, dm->numFaceData);
323 void *DM_get_vert_data(DerivedMesh *dm, int index, int type)
325 return CustomData_get(&dm->vertData, index, type);
328 void *DM_get_edge_data(DerivedMesh *dm, int index, int type)
330 return CustomData_get(&dm->edgeData, index, type);
333 void *DM_get_face_data(DerivedMesh *dm, int index, int type)
335 return CustomData_get(&dm->faceData, index, type);
338 void *DM_get_vert_data_layer(DerivedMesh *dm, int type)
341 return dm->getVertArray(dm);
343 return CustomData_get_layer(&dm->vertData, type);
346 void *DM_get_edge_data_layer(DerivedMesh *dm, int type)
349 return dm->getEdgeArray(dm);
351 return CustomData_get_layer(&dm->edgeData, type);
354 void *DM_get_face_data_layer(DerivedMesh *dm, int type)
357 return dm->getFaceArray(dm);
359 return CustomData_get_layer(&dm->faceData, type);
362 void DM_set_vert_data(DerivedMesh *dm, int index, int type, void *data)
364 CustomData_set(&dm->vertData, index, type, data);
367 void DM_set_edge_data(DerivedMesh *dm, int index, int type, void *data)
369 CustomData_set(&dm->edgeData, index, type, data);
372 void DM_set_face_data(DerivedMesh *dm, int index, int type, void *data)
374 CustomData_set(&dm->faceData, index, type, data);
377 void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest,
378 int source_index, int dest_index, int count)
380 CustomData_copy_data(&source->vertData, &dest->vertData,
381 source_index, dest_index, count);
384 void DM_copy_edge_data(DerivedMesh *source, DerivedMesh *dest,
385 int source_index, int dest_index, int count)
387 CustomData_copy_data(&source->edgeData, &dest->edgeData,
388 source_index, dest_index, count);
391 void DM_copy_face_data(DerivedMesh *source, DerivedMesh *dest,
392 int source_index, int dest_index, int count)
394 CustomData_copy_data(&source->faceData, &dest->faceData,
395 source_index, dest_index, count);
398 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count)
400 CustomData_free_elem(&dm->vertData, index, count);
403 void DM_free_edge_data(struct DerivedMesh *dm, int index, int count)
405 CustomData_free_elem(&dm->edgeData, index, count);
408 void DM_free_face_data(struct DerivedMesh *dm, int index, int count)
410 CustomData_free_elem(&dm->faceData, index, count);
413 void DM_interp_vert_data(DerivedMesh *source, DerivedMesh *dest,
414 int *src_indices, float *weights,
415 int count, int dest_index)
417 CustomData_interp(&source->vertData, &dest->vertData, src_indices,
418 weights, NULL, count, dest_index);
421 void DM_interp_edge_data(DerivedMesh *source, DerivedMesh *dest,
423 float *weights, EdgeVertWeight *vert_weights,
424 int count, int dest_index)
426 CustomData_interp(&source->edgeData, &dest->edgeData, src_indices,
427 weights, (float*)vert_weights, count, dest_index);
430 void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
432 float *weights, FaceVertWeight *vert_weights,
433 int count, int dest_index)
435 CustomData_interp(&source->faceData, &dest->faceData, src_indices,
436 weights, (float*)vert_weights, count, dest_index);
439 void DM_swap_face_data(DerivedMesh *dm, int index, const int *corner_indices)
441 CustomData_swap(&dm->faceData, index, corner_indices);
446 DerivedMesh *mesh_create_derived(Mesh *me, Object *ob, float (*vertCos)[3])
448 DerivedMesh *dm = CDDM_from_mesh(me, ob);
454 CDDM_apply_vert_coords(dm, vertCos);
456 CDDM_calc_normals(dm);
461 DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, ModifierData *md)
464 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
469 if (!(md->mode&eModifierMode_Realtime)) return NULL;
470 if (mti->isDisabled && mti->isDisabled(md, 0)) return NULL;
472 if (mti->type==eModifierTypeType_OnlyDeform) {
474 float (*deformedVerts)[3] = mesh_getVertexCos(me, &numVerts);
476 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, 0, 0);
477 dm = mesh_create_derived(me, ob, deformedVerts);
479 MEM_freeN(deformedVerts);
481 DerivedMesh *tdm = mesh_create_derived(me, ob, NULL);
482 dm = mti->applyModifier(md, ob, tdm, 0, 0);
484 if(tdm != dm) tdm->release(tdm);
490 static float *get_editmesh_orco_verts(EditMesh *em)
496 /* these may not really be the orco's, but it's only for preview.
497 * could be solver better once, but isn't simple */
500 for(eve=em->verts.first; eve; eve=eve->next)
503 orco = MEM_mallocN(sizeof(float)*3*totvert, "EditMesh Orco");
505 for(a=0, eve=em->verts.first; eve; eve=eve->next, a+=3) {
506 copy_v3_v3(orco+a, eve->co);
512 /* orco custom data layer */
514 static void *get_orco_coords_dm(Object *ob, EditMesh *em, int layer, int *free)
518 if(layer == CD_ORCO) {
519 /* get original coordinates */
523 return (float(*)[3])get_editmesh_orco_verts(em);
525 return (float(*)[3])get_mesh_orco_verts(ob);
527 else if(layer == CD_CLOTH_ORCO) {
528 /* apply shape key for cloth, this should really be solved
529 by a more flexible customdata system, but not simple */
531 ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
532 KeyBlock *kb= key_get_keyblock(ob_get_key(ob), clmd->sim_parms->shapekey_rest);
544 static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, EditMesh *em, int layer)
550 if(em) dm= CDDM_from_editmesh(em, me);
551 else dm= CDDM_from_mesh(me, ob);
553 orco= get_orco_coords_dm(ob, em, layer, &free);
556 CDDM_apply_vert_coords(dm, orco);
557 if(free) MEM_freeN(orco);
560 CDDM_calc_normals(dm);
565 static void add_orco_dm(Object *ob, EditMesh *em, DerivedMesh *dm, DerivedMesh *orcodm, int layer)
567 float (*orco)[3], (*layerorco)[3];
570 totvert= dm->getNumVerts(dm);
573 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
576 if(orcodm->getNumVerts(orcodm) == totvert)
577 orcodm->getVertCos(orcodm, orco);
579 dm->getVertCos(dm, orco);
582 orco= get_orco_coords_dm(ob, em, layer, &free);
586 transform_mesh_orco_verts(ob->data, orco, totvert, 0);
588 if(!(layerorco = DM_get_vert_data_layer(dm, layer))) {
589 DM_add_vert_layer(dm, layer, CD_CALLOC, NULL);
590 layerorco = DM_get_vert_data_layer(dm, layer);
593 memcpy(layerorco, orco, sizeof(float)*3*totvert);
594 if(free) MEM_freeN(orco);
598 /* weight paint colors */
600 /* Something of a hack, at the moment deal with weightpaint
601 * by tucking into colors during modifier eval, only in
602 * wpaint mode. Works ok but need to make sure recalc
603 * happens on enter/exit wpaint.
606 void weight_to_rgb(float r_rgb[3], const float weight)
608 const float blend= ((weight/2.0f)+0.5f);
610 if (weight<=0.25f){ // blue->cyan
612 r_rgb[1]= blend*weight*4.0f;
615 else if (weight<=0.50f){ // cyan->green
618 r_rgb[2]= blend*(1.0f-((weight-0.25f)*4.0f));
620 else if (weight <= 0.75f){ // green->yellow
621 r_rgb[0]= blend * ((weight-0.50f)*4.0f);
625 else if (weight <= 1.0f){ // yellow->red
627 r_rgb[1]= blend * (1.0f-((weight-0.75f)*4.0f));
631 /* exceptional value, unclamped or nan,
632 * avoid uninitialized memory use */
639 /* draw_flag's for calc_weightpaint_vert_color */
641 CALC_WP_MULTIPAINT= (1<<0),
642 CALC_WP_AUTO_NORMALIZE= (1<<1)
645 static void calc_weightpaint_vert_color(
646 Object *ob, const int defbase_tot, ColorBand *coba, int vert, unsigned char *col,
647 const char *dg_flags, int selected, int UNUSED(unselected), const int draw_flag)
652 int make_black= FALSE;
655 MDeformVert *dvert= &me->dvert[vert];
657 if ((selected > 1) && (draw_flag & CALC_WP_MULTIPAINT)) {
658 int was_a_nonzero= FALSE;
661 MDeformWeight *dw= dvert->dw;
662 for (i = dvert->totweight; i > 0; i--, dw++) {
663 /* in multipaint, get the average if auto normalize is inactive
664 * get the sum if it is active */
665 if (dw->def_nr < defbase_tot) {
666 if (dg_flags[dw->def_nr]) {
675 /* make it black if the selected groups have no weight on a vertex */
676 if (was_a_nonzero == FALSE) {
679 else if ((draw_flag & CALC_WP_AUTO_NORMALIZE) == FALSE) {
680 input /= selected; /* get the average */
684 /* default, non tricky behavior */
685 input= defvert_find_weight(dvert, ob->actdef-1);
697 CLAMP(input, 0.0f, 1.0f);
699 if(coba) do_colorband(coba, input, colf);
700 else weight_to_rgb(colf, input);
702 col[3] = (unsigned char)(colf[0] * 255.0f);
703 col[2] = (unsigned char)(colf[1] * 255.0f);
704 col[1] = (unsigned char)(colf[2] * 255.0f);
709 static ColorBand *stored_cb= NULL;
711 void vDM_ColorBand_store(ColorBand *coba)
716 static void add_weight_mcol_dm(Object *ob, DerivedMesh *dm, int const draw_flag)
719 MFace *mf = me->mface;
720 ColorBand *coba= stored_cb; /* warning, not a local var */
721 unsigned char *wtcol;
724 int defbase_tot = BLI_countlist(&ob->defbase);
725 char *defbase_sel = MEM_mallocN(defbase_tot * sizeof(char), __func__);
726 int selected = get_selected_defgroups(ob, defbase_sel, defbase_tot);
727 int unselected = defbase_tot - selected;
729 wtcol = MEM_callocN (sizeof (unsigned char) * me->totface*4*4, "weightmap");
731 memset(wtcol, 0x55, sizeof (unsigned char) * me->totface*4*4);
732 for (i=0; i<me->totface; i++, mf++) {
733 calc_weightpaint_vert_color(ob, defbase_tot, coba, mf->v1, &wtcol[(i*4 + 0)*4], defbase_sel, selected, unselected, draw_flag);
734 calc_weightpaint_vert_color(ob, defbase_tot, coba, mf->v2, &wtcol[(i*4 + 1)*4], defbase_sel, selected, unselected, draw_flag);
735 calc_weightpaint_vert_color(ob, defbase_tot, coba, mf->v3, &wtcol[(i*4 + 2)*4], defbase_sel, selected, unselected, draw_flag);
737 calc_weightpaint_vert_color(ob, defbase_tot, coba, mf->v4, &wtcol[(i*4 + 3)*4], defbase_sel, selected, unselected, draw_flag);
740 MEM_freeN(defbase_sel);
742 CustomData_add_layer(&dm->faceData, CD_WEIGHT_MCOL, CD_ASSIGN, wtcol, dm->numFaceData);
745 /* new value for useDeform -1 (hack for the gameengine):
746 * - apply only the modifier stack of the object, skipping the virtual modifiers,
747 * - don't apply the key
748 * - apply deform modifiers and input vertexco
750 static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos)[3],
751 DerivedMesh **deform_r, DerivedMesh **final_r,
752 int useRenderParams, int useDeform,
753 int needMapping, CustomDataMask dataMask, int index, int useCache)
756 ModifierData *firstmd, *md;
757 LinkNode *datamasks, *curr;
758 CustomDataMask mask, nextmask, append_mask = 0;
759 float (*deformedVerts)[3] = NULL;
760 DerivedMesh *dm, *orcodm, *clothorcodm, *finaldm;
761 int numVerts = me->totvert;
763 int isPrevDeform= FALSE;
764 int skipVirtualArmature = (useDeform < 0);
765 MultiresModifierData *mmd= get_multires_modifier(scene, ob, 0);
766 int has_multires = mmd != NULL, multires_applied = 0;
767 int sculpt_mode = ob->mode & OB_MODE_SCULPT && ob->sculpt;
769 int draw_flag= ((scene->toolsettings->multipaint ? CALC_WP_MULTIPAINT : 0) |
770 (scene->toolsettings->auto_normalize ? CALC_WP_AUTO_NORMALIZE : 0));
772 if(mmd && !mmd->sculptlvl)
775 if(!skipVirtualArmature) {
776 firstmd = modifiers_getVirtualModifierList(ob);
779 /* game engine exception */
780 firstmd = ob->modifiers.first;
781 if(firstmd && firstmd->type == eModifierType_Armature)
782 firstmd = firstmd->next;
787 modifiers_clearErrors(ob);
789 if(useRenderParams) required_mode = eModifierMode_Render;
790 else required_mode = eModifierMode_Realtime;
792 datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
795 if(deform_r) *deform_r = NULL;
800 deformedVerts = inputVertexCos;
802 /* Apply all leading deforming modifiers */
803 for(;md; md = md->next, curr = curr->next) {
804 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
808 if(!modifier_isEnabled(scene, md, required_mode)) continue;
809 if(useDeform < 0 && mti->dependsOnTime && mti->dependsOnTime(md)) continue;
811 if(mti->type == eModifierTypeType_OnlyDeform) {
813 deformedVerts = mesh_getVertexCos(me, &numVerts);
815 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, useRenderParams, useDeform);
820 /* grab modifiers until index i */
821 if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
825 /* Result of all leading deforming modifiers is cached for
826 * places that wish to use the original mesh but with deformed
827 * coordinates (vpaint, etc.)
830 *deform_r = CDDM_from_mesh(me, ob);
833 CDDM_apply_vert_coords(*deform_r, deformedVerts);
834 CDDM_calc_normals(*deform_r);
838 /* default behaviour for meshes */
840 deformedVerts = inputVertexCos;
842 deformedVerts = mesh_getVertexCos(me, &numVerts);
846 /* Now apply all remaining modifiers. If useDeform is off then skip
853 for(;md; md = md->next, curr = curr->next) {
854 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
858 if(!modifier_isEnabled(scene, md, required_mode)) continue;
859 if(mti->type == eModifierTypeType_OnlyDeform && !useDeform) continue;
860 if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
861 modifier_setError(md, "Modifier requires original data, bad stack position.");
864 if(sculpt_mode && (!has_multires || multires_applied)) {
867 if(scene->toolsettings->sculpt->flags & SCULPT_ONLY_DEFORM)
868 unsupported|= mti->type != eModifierTypeType_OnlyDeform;
870 unsupported|= md->type == eModifierType_Multires && ((MultiresModifierData*)md)->sculptlvl==0;
871 unsupported|= multires_applied;
874 modifier_setError(md, "Not supported in sculpt mode.");
878 if(needMapping && !modifier_supportsMapping(md)) continue;
879 if(useDeform < 0 && mti->dependsOnTime && mti->dependsOnTime(md)) continue;
881 /* add an orco layer if needed by this modifier */
882 if(mti->requiredDataMask)
883 mask = mti->requiredDataMask(ob, md);
887 if(dm && (mask & CD_MASK_ORCO))
888 add_orco_dm(ob, NULL, dm, orcodm, CD_ORCO);
890 /* How to apply modifier depends on (a) what we already have as
891 * a result of previous modifiers (could be a DerivedMesh or just
892 * deformed vertices) and (b) what type the modifier is.
895 if(mti->type == eModifierTypeType_OnlyDeform) {
896 /* No existing verts to deform, need to build them. */
899 /* Deforming a derived mesh, read the vertex locations
900 * out of the mesh and deform them. Once done with this
901 * run of deformers verts will be written back.
903 numVerts = dm->getNumVerts(dm);
905 MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
906 dm->getVertCos(dm, deformedVerts);
908 deformedVerts = mesh_getVertexCos(me, &numVerts);
912 /* if this is not the last modifier in the stack then recalculate the normals
913 * to avoid giving bogus normals to the next modifier see: [#23673] */
914 if(isPrevDeform && mti->dependsOnNormals && mti->dependsOnNormals(md)) {
915 /* XXX, this covers bug #23673, but we may need normal calc for other types */
916 if(dm && dm->type == DM_TYPE_CDDM) {
917 CDDM_apply_vert_coords(dm, deformedVerts);
918 CDDM_calc_normals(dm);
922 mti->deformVerts(md, ob, dm, deformedVerts, numVerts, useRenderParams, useDeform);
926 /* determine which data layers are needed by following modifiers */
928 nextmask= (CustomDataMask)GET_INT_FROM_POINTER(curr->next->link);
932 /* apply vertex coordinates or build a DerivedMesh as necessary */
935 DerivedMesh *tdm = CDDM_copy(dm);
939 CDDM_apply_vert_coords(dm, deformedVerts);
940 CDDM_calc_normals(dm);
943 dm = CDDM_from_mesh(me, ob);
946 CDDM_apply_vert_coords(dm, deformedVerts);
947 CDDM_calc_normals(dm);
950 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
951 add_weight_mcol_dm(ob, dm, draw_flag);
953 /* Constructive modifiers need to have an origindex
954 * otherwise they wont have anywhere to copy the data from.
956 * Also create ORIGINDEX data if any of the following modifiers
957 * requests it, this way Mirror, Solidify etc will keep ORIGINDEX
958 * data by using generic DM_copy_vert_data() functions.
960 if(needMapping || (nextmask & CD_MASK_ORIGINDEX)) {
962 DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
963 DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
964 DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
966 range_vn_i(DM_get_vert_data_layer(dm, CD_ORIGINDEX), dm->numVertData, 0);
967 range_vn_i(DM_get_edge_data_layer(dm, CD_ORIGINDEX), dm->numEdgeData, 0);
968 range_vn_i(DM_get_face_data_layer(dm, CD_ORIGINDEX), dm->numFaceData, 0);
973 /* set the DerivedMesh to only copy needed data */
974 mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
975 /* needMapping check here fixes bug [#28112], otherwise its
976 * possible that it wont be copied */
978 DM_set_only_copy(dm, mask | (needMapping ? CD_MASK_ORIGINDEX : 0));
980 /* add cloth rest shape key if need */
981 if(mask & CD_MASK_CLOTH_ORCO)
982 add_orco_dm(ob, NULL, dm, clothorcodm, CD_CLOTH_ORCO);
984 /* add an origspace layer if needed */
985 if(((CustomDataMask)GET_INT_FROM_POINTER(curr->link)) & CD_MASK_ORIGSPACE)
986 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
987 DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
989 ndm = mti->applyModifier(md, ob, dm, useRenderParams, useCache);
992 /* if the modifier returned a new dm, release the old one */
993 if(dm && dm != ndm) dm->release(dm);
998 if(deformedVerts != inputVertexCos)
999 MEM_freeN(deformedVerts);
1001 deformedVerts = NULL;
1005 /* create an orco derivedmesh in parallel */
1006 if(nextmask & CD_MASK_ORCO) {
1008 orcodm= create_orco_dm(ob, me, NULL, CD_ORCO);
1010 nextmask &= ~CD_MASK_ORCO;
1011 DM_set_only_copy(orcodm, nextmask | CD_MASK_ORIGINDEX);
1012 ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, 0);
1015 /* if the modifier returned a new dm, release the old one */
1016 if(orcodm && orcodm != ndm) orcodm->release(orcodm);
1021 /* create cloth orco derivedmesh in parallel */
1022 if(nextmask & CD_MASK_CLOTH_ORCO) {
1024 clothorcodm= create_orco_dm(ob, me, NULL, CD_CLOTH_ORCO);
1026 nextmask &= ~CD_MASK_CLOTH_ORCO;
1027 DM_set_only_copy(clothorcodm, nextmask | CD_MASK_ORIGINDEX);
1028 ndm = mti->applyModifier(md, ob, clothorcodm, useRenderParams, 0);
1031 /* if the modifier returned a new dm, release the old one */
1032 if(clothorcodm && clothorcodm != ndm) clothorcodm->release(clothorcodm);
1037 /* in case of dynamic paint, make sure preview mask remains for following modifiers */
1038 if (md->type == eModifierType_DynamicPaint)
1039 append_mask |= CD_MASK_WEIGHT_MCOL;
1042 isPrevDeform= (mti->type == eModifierTypeType_OnlyDeform);
1044 /* grab modifiers until index i */
1045 if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
1048 if(sculpt_mode && md->type == eModifierType_Multires)
1049 multires_applied = 1;
1052 for(md=firstmd; md; md=md->next)
1053 modifier_freeTemporaryData(md);
1055 /* Yay, we are done. If we have a DerivedMesh and deformed vertices
1056 * need to apply these back onto the DerivedMesh. If we have no
1057 * DerivedMesh then we need to build one.
1059 if(dm && deformedVerts) {
1060 finaldm = CDDM_copy(dm);
1064 CDDM_apply_vert_coords(finaldm, deformedVerts);
1065 CDDM_calc_normals(finaldm);
1067 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
1068 add_weight_mcol_dm(ob, finaldm, draw_flag);
1072 finaldm = CDDM_from_mesh(me, ob);
1075 CDDM_apply_vert_coords(finaldm, deformedVerts);
1076 CDDM_calc_normals(finaldm);
1079 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
1080 add_weight_mcol_dm(ob, finaldm, draw_flag);
1083 /* add an orco layer if needed */
1084 if(dataMask & CD_MASK_ORCO) {
1085 add_orco_dm(ob, NULL, finaldm, orcodm, CD_ORCO);
1087 if(deform_r && *deform_r)
1088 add_orco_dm(ob, NULL, *deform_r, NULL, CD_ORCO);
1091 #ifdef WITH_GAMEENGINE
1092 /* NavMesh - this is a hack but saves having a NavMesh modifier */
1093 if ((ob->gameflag & OB_NAVMESH) && (finaldm->type == DM_TYPE_CDDM)) {
1095 tdm= navmesh_dm_createNavMeshForVisualization(finaldm);
1096 if (finaldm != tdm) {
1097 finaldm->release(finaldm);
1101 #endif /* WITH_GAMEENGINE */
1106 orcodm->release(orcodm);
1108 clothorcodm->release(clothorcodm);
1110 if(deformedVerts && deformedVerts != inputVertexCos)
1111 MEM_freeN(deformedVerts);
1113 BLI_linklist_free(datamasks, NULL);
1116 float (*editmesh_get_vertex_cos(EditMesh *em, int *numVerts_r))[3]
1118 int i, numVerts = *numVerts_r = BLI_countlist(&em->verts);
1122 cos = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos");
1123 for (i=0,eve=em->verts.first; i<numVerts; i++,eve=eve->next) {
1124 copy_v3_v3(cos[i], eve->co);
1130 int editmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
1132 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1133 int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1135 if(!modifier_isEnabled(scene, md, required_mode)) return 0;
1136 if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
1137 modifier_setError(md, "Modifier requires original data, bad stack position.");
1144 static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, DerivedMesh **cage_r,
1145 DerivedMesh **final_r,
1146 CustomDataMask dataMask)
1149 float (*deformedVerts)[3] = NULL;
1150 CustomDataMask mask;
1151 DerivedMesh *dm, *orcodm = NULL;
1152 int i, numVerts = 0, cageIndex = modifiers_getCageIndex(scene, ob, NULL, 1);
1153 LinkNode *datamasks, *curr;
1154 int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1156 modifiers_clearErrors(ob);
1158 if(cage_r && cageIndex == -1) {
1159 *cage_r = editmesh_get_derived(em, NULL);
1163 md = modifiers_getVirtualModifierList(ob);
1165 datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
1168 for(i = 0; md; i++, md = md->next, curr = curr->next) {
1169 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1173 if(!editmesh_modifier_is_enabled(scene, md, dm))
1176 /* add an orco layer if needed by this modifier */
1177 if(dm && mti->requiredDataMask) {
1178 mask = mti->requiredDataMask(ob, md);
1179 if(mask & CD_MASK_ORCO)
1180 add_orco_dm(ob, em, dm, orcodm, CD_ORCO);
1183 /* How to apply modifier depends on (a) what we already have as
1184 * a result of previous modifiers (could be a DerivedMesh or just
1185 * deformed vertices) and (b) what type the modifier is.
1188 if(mti->type == eModifierTypeType_OnlyDeform) {
1189 /* No existing verts to deform, need to build them. */
1190 if(!deformedVerts) {
1192 /* Deforming a derived mesh, read the vertex locations
1193 * out of the mesh and deform them. Once done with this
1194 * run of deformers verts will be written back.
1196 numVerts = dm->getNumVerts(dm);
1198 MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
1199 dm->getVertCos(dm, deformedVerts);
1201 deformedVerts = editmesh_get_vertex_cos(em, &numVerts);
1205 if (mti->deformVertsEM)
1206 mti->deformVertsEM(md, ob, em, dm, deformedVerts, numVerts);
1207 else mti->deformVerts(md, ob, dm, deformedVerts, numVerts, 0, 0);
1211 /* apply vertex coordinates or build a DerivedMesh as necessary */
1214 DerivedMesh *tdm = CDDM_copy(dm);
1215 if(!(cage_r && dm == *cage_r)) dm->release(dm);
1218 CDDM_apply_vert_coords(dm, deformedVerts);
1219 CDDM_calc_normals(dm);
1220 } else if(cage_r && dm == *cage_r) {
1221 /* dm may be changed by this modifier, so we need to copy it
1227 dm = CDDM_from_editmesh(em, ob->data);
1230 CDDM_apply_vert_coords(dm, deformedVerts);
1231 CDDM_calc_normals(dm);
1235 /* create an orco derivedmesh in parallel */
1236 mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
1237 if(mask & CD_MASK_ORCO) {
1239 orcodm= create_orco_dm(ob, ob->data, em, CD_ORCO);
1241 mask &= ~CD_MASK_ORCO;
1242 DM_set_only_copy(orcodm, mask | CD_MASK_ORIGINDEX);
1244 if (mti->applyModifierEM)
1245 ndm = mti->applyModifierEM(md, ob, em, orcodm);
1247 ndm = mti->applyModifier(md, ob, orcodm, 0, 0);
1250 /* if the modifier returned a new dm, release the old one */
1251 if(orcodm && orcodm != ndm) orcodm->release(orcodm);
1256 /* set the DerivedMesh to only copy needed data */
1257 mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link); /* CD_MASK_ORCO may have been cleared above */
1259 DM_set_only_copy(dm, mask | CD_MASK_ORIGINDEX);
1261 if(mask & CD_MASK_ORIGSPACE)
1262 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
1263 DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
1265 if (mti->applyModifierEM)
1266 ndm = mti->applyModifierEM(md, ob, em, dm);
1268 ndm = mti->applyModifier(md, ob, dm, 0, 0);
1276 if (deformedVerts) {
1277 MEM_freeN(deformedVerts);
1278 deformedVerts = NULL;
1283 if(cage_r && i == cageIndex) {
1284 if(dm && deformedVerts) {
1285 *cage_r = CDDM_copy(dm);
1286 CDDM_apply_vert_coords(*cage_r, deformedVerts);
1291 editmesh_get_derived(em,
1292 deformedVerts ? MEM_dupallocN(deformedVerts) : NULL);
1297 BLI_linklist_free(datamasks, NULL);
1299 /* Yay, we are done. If we have a DerivedMesh and deformed vertices need
1300 * to apply these back onto the DerivedMesh. If we have no DerivedMesh
1301 * then we need to build one.
1303 if(dm && deformedVerts) {
1304 *final_r = CDDM_copy(dm);
1306 if(!(cage_r && dm == *cage_r)) dm->release(dm);
1308 CDDM_apply_vert_coords(*final_r, deformedVerts);
1309 CDDM_calc_normals(*final_r);
1312 } else if (!deformedVerts && cage_r && *cage_r) {
1315 *final_r = editmesh_get_derived(em, deformedVerts);
1316 deformedVerts = NULL;
1319 /* add an orco layer if needed */
1320 if(dataMask & CD_MASK_ORCO)
1321 add_orco_dm(ob, em, *final_r, orcodm, CD_ORCO);
1324 orcodm->release(orcodm);
1327 MEM_freeN(deformedVerts);
1330 static void clear_mesh_caches(Object *ob)
1334 /* also serves as signal to remake texspace */
1344 freedisplist(&ob->disp);
1346 if (ob->derivedFinal) {
1347 ob->derivedFinal->needsFree = 1;
1348 ob->derivedFinal->release(ob->derivedFinal);
1349 ob->derivedFinal= NULL;
1351 if (ob->derivedDeform) {
1352 ob->derivedDeform->needsFree = 1;
1353 ob->derivedDeform->release(ob->derivedDeform);
1354 ob->derivedDeform= NULL;
1358 object_sculpt_modifiers_changed(ob);
1362 static void mesh_build_data(Scene *scene, Object *ob, CustomDataMask dataMask)
1364 Object *obact = scene->basact?scene->basact->object:NULL;
1365 int editing = paint_facesel_test(ob) || paint_vertsel_test(ob);/* paint_vertsel_test */
1366 /* weight paint and face select need original indices because of selection buffer drawing */
1367 int needMapping = (ob==obact) && (editing || (ob->mode & (OB_MODE_WEIGHT_PAINT|OB_MODE_VERTEX_PAINT)));
1369 clear_mesh_caches(ob);
1371 mesh_calc_modifiers(scene, ob, NULL, &ob->derivedDeform,
1372 &ob->derivedFinal, 0, 1,
1373 needMapping, dataMask, -1, 1);
1375 DM_set_object_boundbox (ob, ob->derivedFinal);
1377 ob->derivedFinal->needsFree = 0;
1378 ob->derivedDeform->needsFree = 0;
1379 ob->lastDataMask = dataMask;
1382 static void editmesh_build_data(Scene *scene, Object *obedit, EditMesh *em, CustomDataMask dataMask)
1384 clear_mesh_caches(obedit);
1386 if (em->derivedFinal) {
1387 if (em->derivedFinal!=em->derivedCage) {
1388 em->derivedFinal->needsFree = 1;
1389 em->derivedFinal->release(em->derivedFinal);
1391 em->derivedFinal = NULL;
1393 if (em->derivedCage) {
1394 em->derivedCage->needsFree = 1;
1395 em->derivedCage->release(em->derivedCage);
1396 em->derivedCage = NULL;
1399 editmesh_calc_modifiers(scene, obedit, em, &em->derivedCage, &em->derivedFinal, dataMask);
1400 DM_set_object_boundbox (obedit, em->derivedFinal);
1402 em->lastDataMask = dataMask;
1403 em->derivedFinal->needsFree = 0;
1404 em->derivedCage->needsFree = 0;
1407 void makeDerivedMesh(Scene *scene, Object *ob, EditMesh *em, CustomDataMask dataMask)
1410 editmesh_build_data(scene, ob, em, dataMask);
1412 mesh_build_data(scene, ob, dataMask);
1418 DerivedMesh *mesh_get_derived_final(Scene *scene, Object *ob, CustomDataMask dataMask)
1420 /* if there's no derived mesh or the last data mask used doesn't include
1421 * the data we need, rebuild the derived mesh
1423 if(!ob->derivedFinal || (dataMask & ob->lastDataMask) != dataMask)
1424 mesh_build_data(scene, ob, dataMask);
1426 return ob->derivedFinal;
1429 DerivedMesh *mesh_get_derived_deform(Scene *scene, Object *ob, CustomDataMask dataMask)
1431 /* if there's no derived mesh or the last data mask used doesn't include
1432 * the data we need, rebuild the derived mesh
1434 if(!ob->derivedDeform || (dataMask & ob->lastDataMask) != dataMask)
1435 mesh_build_data(scene, ob, dataMask);
1437 return ob->derivedDeform;
1440 DerivedMesh *mesh_create_derived_render(Scene *scene, Object *ob, CustomDataMask dataMask)
1444 mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, -1, 0);
1449 DerivedMesh *mesh_create_derived_index_render(Scene *scene, Object *ob, CustomDataMask dataMask, int index)
1453 mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, index, 0);
1458 DerivedMesh *mesh_create_derived_view(Scene *scene, Object *ob, CustomDataMask dataMask)
1462 mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 0, 1, 0, dataMask, -1, 0);
1467 DerivedMesh *mesh_create_derived_no_deform(Scene *scene, Object *ob, float (*vertCos)[3],
1468 CustomDataMask dataMask)
1472 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, 0, 0, dataMask, -1, 0);
1477 DerivedMesh *mesh_create_derived_no_virtual(Scene *scene, Object *ob, float (*vertCos)[3],
1478 CustomDataMask dataMask)
1482 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 0, dataMask, -1, 0);
1487 DerivedMesh *mesh_create_derived_physics(Scene *scene, Object *ob, float (*vertCos)[3],
1488 CustomDataMask dataMask)
1492 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 1, dataMask, -1, 0);
1497 DerivedMesh *mesh_create_derived_no_deform_render(Scene *scene, Object *ob,
1498 float (*vertCos)[3],
1499 CustomDataMask dataMask)
1503 mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 1, 0, 0, dataMask, -1, 0);
1510 DerivedMesh *editmesh_get_derived_cage_and_final(Scene *scene, Object *obedit, EditMesh *em, DerivedMesh **final_r,
1511 CustomDataMask dataMask)
1513 /* if there's no derived mesh or the last data mask used doesn't include
1514 * the data we need, rebuild the derived mesh
1516 if(!em->derivedCage ||
1517 (em->lastDataMask & dataMask) != dataMask)
1518 editmesh_build_data(scene, obedit, em, dataMask);
1520 *final_r = em->derivedFinal;
1521 return em->derivedCage;
1524 DerivedMesh *editmesh_get_derived_cage(Scene *scene, Object *obedit, EditMesh *em, CustomDataMask dataMask)
1526 /* if there's no derived mesh or the last data mask used doesn't include
1527 * the data we need, rebuild the derived mesh
1529 if(!em->derivedCage ||
1530 (em->lastDataMask & dataMask) != dataMask)
1531 editmesh_build_data(scene, obedit, em, dataMask);
1533 return em->derivedCage;
1536 DerivedMesh *editmesh_get_derived_base(Object *UNUSED(obedit), EditMesh *em)
1538 return editmesh_get_derived(em, NULL);
1542 /* ********* For those who don't grasp derived stuff! (ton) :) *************** */
1544 static void make_vertexcosnos__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
1546 float *vec = userData;
1550 /* check if we've been here before (normal should not be 0) */
1551 if(vec[3] || vec[4] || vec[5]) return;
1553 copy_v3_v3(vec, co);
1556 copy_v3_v3(vec, no_f);
1559 normal_short_to_float_v3(vec, no_s);
1563 /* always returns original amount me->totvert of vertices and normals, but fully deformed and subsurfered */
1564 /* this is needed for all code using vertexgroups (no subsurf support) */
1565 /* it stores the normals as floats, but they can still be scaled as shorts (32767 = unit) */
1566 /* in use now by vertex/weight paint and particle generating */
1568 float *mesh_get_mapped_verts_nors(Scene *scene, Object *ob)
1572 float *vertexcosnos;
1574 /* lets prevent crashing... */
1575 if(ob->type!=OB_MESH || me->totvert==0)
1578 dm= mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
1579 vertexcosnos= MEM_callocN(6*sizeof(float)*me->totvert, "vertexcosnos map");
1581 if(dm->foreachMappedVert) {
1582 dm->foreachMappedVert(dm, make_vertexcosnos__mapFunc, vertexcosnos);
1585 float *fp= vertexcosnos;
1588 for(a=0; a< me->totvert; a++, fp+=6) {
1589 dm->getVertCo(dm, a, fp);
1590 dm->getVertNo(dm, a, fp+3);
1595 return vertexcosnos;
1598 /* ******************* GLSL ******************** */
1602 float * precomputedFaceNormals;
1603 MTFace * mtface; // texture coordinates
1604 MFace * mface; // indices
1605 MVert * mvert; // vertices & normals
1607 float (*tangent)[4]; // destination
1610 } SGLSLMeshToTangent;
1613 #include "mikktspace.h"
1615 static int GetNumFaces(const SMikkTSpaceContext * pContext)
1617 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1618 return pMesh->numFaces;
1621 static int GetNumVertsOfFace(const SMikkTSpaceContext * pContext, const int face_num)
1623 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1624 return pMesh->mface[face_num].v4!=0 ? 4 : 3;
1627 static void GetPosition(const SMikkTSpaceContext * pContext, float fPos[], const int face_num, const int vert_index)
1629 //assert(vert_index>=0 && vert_index<4);
1630 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1631 const float *co= pMesh->mvert[(&pMesh->mface[face_num].v1)[vert_index]].co;
1632 copy_v3_v3(fPos, co);
1635 static void GetTextureCoordinate(const SMikkTSpaceContext * pContext, float fUV[], const int face_num, const int vert_index)
1637 //assert(vert_index>=0 && vert_index<4);
1638 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1640 if(pMesh->mtface!=NULL) {
1641 float * uv = pMesh->mtface[face_num].uv[vert_index];
1642 fUV[0]=uv[0]; fUV[1]=uv[1];
1645 const float *orco= pMesh->orco[(&pMesh->mface[face_num].v1)[vert_index]];
1646 map_to_sphere( &fUV[0], &fUV[1], orco[0], orco[1], orco[2]);
1650 static void GetNormal(const SMikkTSpaceContext * pContext, float fNorm[], const int face_num, const int vert_index)
1652 //assert(vert_index>=0 && vert_index<4);
1653 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1655 const int smoothnormal = (pMesh->mface[face_num].flag & ME_SMOOTH);
1656 if(!smoothnormal) { // flat
1657 if(pMesh->precomputedFaceNormals) {
1658 copy_v3_v3(fNorm, &pMesh->precomputedFaceNormals[3*face_num]);
1661 MFace *mf= &pMesh->mface[face_num];
1662 float *p0= pMesh->mvert[mf->v1].co;
1663 float *p1= pMesh->mvert[mf->v2].co;
1664 float *p2= pMesh->mvert[mf->v3].co;
1667 float *p3 = pMesh->mvert[mf->v4].co;
1668 normal_quad_v3(fNorm, p0, p1, p2, p3);
1671 normal_tri_v3(fNorm, p0, p1, p2);
1676 const short *no= pMesh->mvert[(&pMesh->mface[face_num].v1)[vert_index]].no;
1677 normal_short_to_float_v3(fNorm, no);
1680 static void SetTSpace(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int face_num, const int iVert)
1682 //assert(vert_index>=0 && vert_index<4);
1683 SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1684 float * pRes = pMesh->tangent[4*face_num+iVert];
1685 copy_v3_v3(pRes, fvTangent);
1690 void DM_add_tangent_layer(DerivedMesh *dm)
1693 MTFace *mtface, *tf;
1695 MVert *mvert, *v1, *v2, *v3, *v4;
1696 MemArena *arena= NULL;
1697 VertexTangent **vtangents= NULL;
1698 float (*orco)[3]= NULL, (*tangent)[4];
1699 float *uv1, *uv2, *uv3, *uv4, *vtang;
1700 float fno[3], tang[3], uv[4][2];
1701 int i, j, len, mf_vi[4], totvert, totface, iCalcNewMethod;
1704 if(CustomData_get_layer_index(&dm->faceData, CD_TANGENT) != -1)
1707 nors = dm->getFaceDataArray(dm, CD_NORMAL);
1709 /* check we have all the needed layers */
1710 totvert= dm->getNumVerts(dm);
1711 totface= dm->getNumFaces(dm);
1713 mvert= dm->getVertArray(dm);
1714 mface= dm->getFaceArray(dm);
1715 mtface= dm->getFaceDataArray(dm, CD_MTFACE);
1718 orco= dm->getVertDataArray(dm, CD_ORCO);
1723 /* create tangent layer */
1724 DM_add_face_layer(dm, CD_TANGENT, CD_CALLOC, NULL);
1725 tangent= DM_get_face_data_layer(dm, CD_TANGENT);
1727 /* allocate some space */
1728 arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "tangent layer arena");
1729 BLI_memarena_use_calloc(arena);
1730 vtangents= MEM_callocN(sizeof(VertexTangent*)*totvert, "VertexTangent");
1732 // new computation method
1734 if(iCalcNewMethod != 0) {
1735 SGLSLMeshToTangent mesh2tangent= {0};
1736 SMikkTSpaceContext sContext= {0};
1737 SMikkTSpaceInterface sInterface= {0};
1739 mesh2tangent.precomputedFaceNormals = nors;
1740 mesh2tangent.mtface = mtface;
1741 mesh2tangent.mface = mface;
1742 mesh2tangent.mvert = mvert;
1743 mesh2tangent.orco = orco;
1744 mesh2tangent.tangent = tangent;
1745 mesh2tangent.numFaces = totface;
1747 sContext.m_pUserData = &mesh2tangent;
1748 sContext.m_pInterface = &sInterface;
1749 sInterface.m_getNumFaces = GetNumFaces;
1750 sInterface.m_getNumVerticesOfFace = GetNumVertsOfFace;
1751 sInterface.m_getPosition = GetPosition;
1752 sInterface.m_getTexCoord = GetTextureCoordinate;
1753 sInterface.m_getNormal = GetNormal;
1754 sInterface.m_setTSpaceBasic = SetTSpace;
1757 iCalcNewMethod = genTangSpaceDefault(&sContext);
1760 if(!iCalcNewMethod) {
1761 /* sum tangents at connected vertices */
1762 for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++) {
1769 normal_quad_v3( fno,v4->co, v3->co, v2->co, v1->co);
1773 normal_tri_v3( fno,v3->co, v2->co, v1->co);
1783 uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
1784 map_to_sphere( &uv[0][0], &uv[0][1],orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2]);
1785 map_to_sphere( &uv[1][0], &uv[1][1],orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2]);
1786 map_to_sphere( &uv[2][0], &uv[2][1],orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2]);
1788 map_to_sphere( &uv[3][0], &uv[3][1],orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2]);
1791 tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, fno, tang);
1792 sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
1793 sum_or_add_vertex_tangent(arena, &vtangents[mf->v2], tang, uv2);
1794 sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
1799 tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, fno, tang);
1800 sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
1801 sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
1802 sum_or_add_vertex_tangent(arena, &vtangents[mf->v4], tang, uv4);
1806 /* write tangent to layer */
1807 for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++, tangent+=4) {
1808 len= (mf->v4)? 4 : 3;
1810 if(mtface == NULL) {
1811 map_to_sphere( &uv[0][0], &uv[0][1],orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2]);
1812 map_to_sphere( &uv[1][0], &uv[1][1],orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2]);
1813 map_to_sphere( &uv[2][0], &uv[2][1],orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2]);
1815 map_to_sphere( &uv[3][0], &uv[3][1],orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2]);
1823 for(j=0; j<len; j++) {
1824 vtang= find_vertex_tangent(vtangents[mf_vi[j]], mtface ? tf->uv[j] : uv[j]);
1825 normalize_v3_v3(tangent[j], vtang);
1826 ((float *) tangent[j])[3]=1.0f;
1831 BLI_memarena_free(arena);
1832 MEM_freeN(vtangents);
1835 void DM_calc_auto_bump_scale(DerivedMesh *dm)
1837 /* int totvert= dm->getNumVerts(dm); */ /* UNUSED */
1838 int totface= dm->getNumFaces(dm);
1840 MVert * mvert = dm->getVertArray(dm);
1841 MFace * mface = dm->getFaceArray(dm);
1842 MTFace * mtface = dm->getFaceDataArray(dm, CD_MTFACE);
1847 int nr_accumulated = 0;
1850 for ( f=0; f<totface; f++ )
1853 float * verts[4], * tex_coords[4];
1854 const int nr_verts = mface[f].v4!=0 ? 4 : 3;
1855 int i, is_degenerate;
1857 verts[0]=mvert[mface[f].v1].co; verts[1]=mvert[mface[f].v2].co; verts[2]=mvert[mface[f].v3].co;
1858 tex_coords[0]=mtface[f].uv[0]; tex_coords[1]=mtface[f].uv[1]; tex_coords[2]=mtface[f].uv[2];
1861 verts[3]=mvert[mface[f].v4].co;
1862 tex_coords[3]=mtface[f].uv[3];
1865 // discard degenerate faces
1867 if( equals_v3v3(verts[0], verts[1]) || equals_v3v3(verts[0], verts[2]) || equals_v3v3(verts[1], verts[2]) ||
1868 equals_v2v2(tex_coords[0], tex_coords[1]) || equals_v2v2(tex_coords[0], tex_coords[2]) || equals_v2v2(tex_coords[1], tex_coords[2]) )
1873 // verify last vertex as well if this is a quad
1874 if ( is_degenerate==0 && nr_verts==4 )
1876 if( equals_v3v3(verts[3], verts[0]) || equals_v3v3(verts[3], verts[1]) || equals_v3v3(verts[3], verts[2]) ||
1877 equals_v2v2(tex_coords[3], tex_coords[0]) || equals_v2v2(tex_coords[3], tex_coords[1]) || equals_v2v2(tex_coords[3], tex_coords[2]) )
1882 // verify the winding is consistent
1883 if ( is_degenerate==0 )
1887 sub_v2_v2v2(prev_edge, tex_coords[0], tex_coords[3]);
1890 while ( is_degenerate==0 && i<4 )
1892 float cur_edge[2], signed_area;
1893 sub_v2_v2v2(cur_edge, tex_coords[(i+1)&0x3], tex_coords[i]);
1894 signed_area = prev_edge[0]*cur_edge[1] - prev_edge[1]*cur_edge[0];
1895 if ( i==0 ) is_signed = signed_area<0.0f ? 1 : 0;
1896 else if((is_signed!=0)!=(signed_area<0.0f)) is_degenerate=1;
1898 if ( is_degenerate==0 )
1900 copy_v2_v2(prev_edge, cur_edge);
1907 // proceed if not a degenerate face
1908 if ( is_degenerate==0 )
1910 int nr_tris_to_pile=0;
1911 // quads split at shortest diagonal
1912 int offs = 0; // initial triangulation is 0,1,2 and 0, 2, 3
1915 float pos_len_diag0, pos_len_diag1;
1917 sub_v3_v3v3(vtmp, verts[2], verts[0]);
1918 pos_len_diag0 = dot_v3v3(vtmp, vtmp);
1919 sub_v3_v3v3(vtmp, verts[3], verts[1]);
1920 pos_len_diag1 = dot_v3v3(vtmp, vtmp);
1922 if(pos_len_diag1<pos_len_diag0)
1923 offs=1; // alter split
1924 else if(pos_len_diag0==pos_len_diag1) // do UV check instead
1926 float tex_len_diag0, tex_len_diag1;
1928 sub_v2_v2v2(vtmp, tex_coords[2], tex_coords[0]);
1929 tex_len_diag0 = dot_v2v2(vtmp, vtmp);
1930 sub_v2_v2v2(vtmp, tex_coords[3], tex_coords[1]);
1931 tex_len_diag1 = dot_v2v2(vtmp, vtmp);
1933 if(tex_len_diag1<tex_len_diag0)
1935 offs=1; // alter split
1939 nr_tris_to_pile = nr_verts-2 ;
1940 if ( nr_tris_to_pile==1 || nr_tris_to_pile==2 )
1942 const int indices[] = {offs+0, offs+1, offs+2, offs+0, offs+2, (offs+3)&0x3 };
1944 for ( t=0; t<nr_tris_to_pile; t++ )
1947 float * p0 = verts[indices[t*3+0]];
1948 float * p1 = verts[indices[t*3+1]];
1949 float * p2 = verts[indices[t*3+2]];
1951 float edge_t0[2], edge_t1[2];
1952 sub_v2_v2v2(edge_t0, tex_coords[indices[t*3+1]], tex_coords[indices[t*3+0]]);
1953 sub_v2_v2v2(edge_t1, tex_coords[indices[t*3+2]], tex_coords[indices[t*3+0]]);
1955 f2x_area_uv = fabsf(edge_t0[0]*edge_t1[1] - edge_t0[1]*edge_t1[0]);
1956 if ( f2x_area_uv>FLT_EPSILON )
1958 float norm[3], v0[3], v1[3], f2x_surf_area, fsurf_ratio;
1959 sub_v3_v3v3(v0, p1, p0);
1960 sub_v3_v3v3(v1, p2, p0);
1961 cross_v3_v3v3(norm, v0, v1);
1963 f2x_surf_area = len_v3(norm);
1964 fsurf_ratio = f2x_surf_area/f2x_area_uv; // tri area divided by texture area
1967 dsum += (double)(fsurf_ratio);
1977 const float avg_area_ratio = (nr_accumulated>0) ? ((float)(dsum / nr_accumulated)) : 1.0f;
1978 const float use_as_render_bump_scale = sqrtf(avg_area_ratio); // use width of average surface ratio as your bump scale
1979 dm->auto_bump_scale = use_as_render_bump_scale;
1984 dm->auto_bump_scale = 1.0f;
1988 void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs, DMVertexAttribs *attribs)
1990 CustomData *vdata, *fdata, *tfdata = NULL;
1993 /* From the layers requested by the GLSL shader, figure out which ones are
1994 * actually available for this derivedmesh, and retrieve the pointers */
1996 memset(attribs, 0, sizeof(DMVertexAttribs));
1998 vdata = &dm->vertData;
1999 fdata = &dm->faceData;
2001 /* ugly hack, editmesh derivedmesh doesn't copy face data, this way we
2002 * can use offsets instead */
2003 if(dm->type == DM_TYPE_EDITMESH)
2004 tfdata = &((EditMeshDerivedMesh*)dm)->em->fdata;
2008 /* calc auto bump scale if necessary */
2010 if(dm->auto_bump_scale<=0.0f)
2011 DM_calc_auto_bump_scale(dm);
2013 dm->auto_bump_scale = 1.0f; // will revert this after release
2017 /* add a tangent layer if necessary */
2018 for(b = 0; b < gattribs->totlayer; b++)
2019 if(gattribs->layer[b].type == CD_TANGENT)
2020 if(CustomData_get_layer_index(fdata, CD_TANGENT) == -1)
2021 DM_add_tangent_layer(dm);
2023 for(b = 0; b < gattribs->totlayer; b++) {
2024 if(gattribs->layer[b].type == CD_MTFACE) {
2025 /* uv coordinates */
2026 if(gattribs->layer[b].name[0])
2027 layer = CustomData_get_named_layer_index(tfdata, CD_MTFACE,
2028 gattribs->layer[b].name);
2030 layer = CustomData_get_active_layer_index(tfdata, CD_MTFACE);
2033 a = attribs->tottface++;
2035 attribs->tface[a].array = tfdata->layers[layer].data;
2036 attribs->tface[a].emOffset = tfdata->layers[layer].offset;
2037 attribs->tface[a].glIndex = gattribs->layer[b].glindex;
2038 attribs->tface[a].glTexco = gattribs->layer[b].gltexco;
2041 else if(gattribs->layer[b].type == CD_MCOL) {
2043 if(gattribs->layer[b].name[0])
2044 layer = CustomData_get_named_layer_index(tfdata, CD_MCOL,
2045 gattribs->layer[b].name);
2047 layer = CustomData_get_active_layer_index(tfdata, CD_MCOL);
2050 a = attribs->totmcol++;
2052 attribs->mcol[a].array = tfdata->layers[layer].data;
2053 attribs->mcol[a].emOffset = tfdata->layers[layer].offset;
2054 attribs->mcol[a].glIndex = gattribs->layer[b].glindex;
2057 else if(gattribs->layer[b].type == CD_TANGENT) {
2059 layer = CustomData_get_layer_index(fdata, CD_TANGENT);
2062 attribs->tottang = 1;
2064 attribs->tang.array = fdata->layers[layer].data;
2065 attribs->tang.emOffset = fdata->layers[layer].offset;
2066 attribs->tang.glIndex = gattribs->layer[b].glindex;
2069 else if(gattribs->layer[b].type == CD_ORCO) {
2070 /* original coordinates */
2071 layer = CustomData_get_layer_index(vdata, CD_ORCO);
2074 attribs->totorco = 1;
2076 attribs->orco.array = vdata->layers[layer].data;
2077 attribs->orco.emOffset = vdata->layers[layer].offset;
2078 attribs->orco.glIndex = gattribs->layer[b].glindex;
2079 attribs->orco.glTexco = gattribs->layer[b].gltexco;
2085 /* Set object's bounding box based on DerivedMesh min/max data */
2086 void DM_set_object_boundbox(Object *ob, DerivedMesh *dm)
2088 float min[3], max[3];
2090 INIT_MINMAX(min, max);
2092 dm->getMinMax(dm, min, max);
2095 ob->bb= MEM_callocN(sizeof(BoundBox), "DM-BoundBox");
2097 boundbox_set_from_min_max(ob->bb, min, max);
2100 /* --- NAVMESH (begin) --- */
2101 #ifdef WITH_GAMEENGINE
2103 BM_INLINE int navmesh_bit(int a, int b)
2105 return (a & (1 << b)) >> b;
2108 BM_INLINE void navmesh_intToCol(int i, float col[3])
2110 int r = navmesh_bit(i, 0) + navmesh_bit(i, 3) * 2 + 1;
2111 int g = navmesh_bit(i, 1) + navmesh_bit(i, 4) * 2 + 1;
2112 int b = navmesh_bit(i, 2) + navmesh_bit(i, 5) * 2 + 1;
2113 col[0] = 1 - r*63.0f/255.0f;
2114 col[1] = 1 - g*63.0f/255.0f;
2115 col[2] = 1 - b*63.0f/255.0f;
2118 static void navmesh_drawColored(DerivedMesh *dm)
2121 MVert *mvert = (MVert *)CustomData_get_layer(&dm->vertData, CD_MVERT);
2122 MFace *mface = (MFace *)CustomData_get_layer(&dm->faceData, CD_MFACE);
2123 int *polygonIdx = (int *)CustomData_get_layer(&dm->faceData, CD_RECAST);
2130 //UI_ThemeColor(TH_WIRE);
2131 glDisable(GL_LIGHTING);
2133 dm->drawEdges(dm, 0, 1);
2135 glEnable(GL_LIGHTING);*/
2137 glDisable(GL_LIGHTING);
2138 /* if(GPU_buffer_legacy(dm) ) */ { /* TODO - VBO draw code, not high priority - campbell */
2139 DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" );
2140 //glShadeModel(GL_SMOOTH);
2141 glBegin(glmode = GL_QUADS);
2142 for(a = 0; a < dm->numFaceData; a++, mface++) {
2143 int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
2144 int pi = polygonIdx[a];
2149 navmesh_intToCol(pi, col);
2152 if(new_glmode != glmode) {
2154 glBegin(glmode = new_glmode);
2157 glVertex3fv(mvert[mface->v1].co);
2158 glVertex3fv(mvert[mface->v2].co);
2159 glVertex3fv(mvert[mface->v3].co);
2161 glVertex3fv(mvert[mface->v4].co);
2166 glEnable(GL_LIGHTING);
2169 static void navmesh_DM_drawFacesTex(DerivedMesh *dm,
2170 int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr),
2171 int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
2174 (void) setDrawOptions;
2175 (void) compareDrawOptions;
2178 navmesh_drawColored(dm);
2181 static void navmesh_DM_drawFacesSolid(DerivedMesh *dm,
2182 float (*partial_redraw_planes)[4],
2183 int UNUSED(fast), int (*setMaterial)(int, void *attribs))
2185 (void) partial_redraw_planes;
2188 //drawFacesSolid_original(dm, partial_redraw_planes, fast, setMaterial);
2189 navmesh_drawColored(dm);
2192 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm)
2194 DerivedMesh *result;
2195 int maxFaces = dm->getNumFaces(dm);
2197 int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0;
2199 unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL;
2200 int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
2203 result = CDDM_copy(dm);
2204 if (!CustomData_has_layer(&result->faceData, CD_RECAST)) {
2205 int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
2206 if (sourceRecastData) {
2207 CustomData_add_layer_named(&result->faceData, CD_RECAST, CD_DUPLICATE,
2208 sourceRecastData, maxFaces, "recastData");
2211 recastData = (int*)CustomData_get_layer(&result->faceData, CD_RECAST);
2213 /* note: This is not good design! - really should not be doing this */
2214 result->drawFacesTex = navmesh_DM_drawFacesTex;
2215 result->drawFacesSolid = navmesh_DM_drawFacesSolid;
2219 res = buildNavMeshDataByDerivedMesh(dm, &vertsPerPoly, &nverts, &verts, &ndtris, &dtris,
2220 &npolys, &dmeshes, &polys, &dtrisToPolysMap, &dtrisToTrisMap,
2225 /* invalidate concave polygon */
2226 for (polyIdx=0; polyIdx<(size_t)npolys; polyIdx++) {
2227 unsigned short* poly = &polys[polyIdx*2*vertsPerPoly];
2228 if (!polyIsConvex(poly, vertsPerPoly, verts)) {
2229 /* set negative polygon idx to all faces */
2230 unsigned short *dmesh = &dmeshes[4*polyIdx];
2231 unsigned short tbase = dmesh[2];
2232 unsigned short tnum = dmesh[3];
2235 for (ti=0; ti<tnum; ti++) {
2236 unsigned short triidx = dtrisToTrisMap[tbase+ti];
2237 unsigned short faceidx = trisToFacesMap[triidx];
2238 if (recastData[faceidx] > 0) {
2239 recastData[faceidx] = -recastData[faceidx];
2246 printf("Error during creation polygon infos\n");
2258 if (dtrisToPolysMap!=NULL)
2259 MEM_freeN(dtrisToPolysMap);
2260 if (dtrisToTrisMap!=NULL)
2261 MEM_freeN(dtrisToTrisMap);
2262 if (trisToFacesMap!=NULL)
2263 MEM_freeN(trisToFacesMap);
2268 #endif /* WITH_GAMEENGINE */
2270 /* --- NAVMESH (end) --- */