replace unhelpfully named `eck!` and `eek!` error prints, also some minor changes...
[blender.git] / source / blender / blenkernel / intern / DerivedMesh.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/DerivedMesh.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include "limits.h"
35
36 #include "MEM_guardedalloc.h"
37
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
44
45 #include "BLI_blenlib.h"
46 #include "BLI_editVert.h"
47 #include "BLI_math.h"
48 #include "BLI_memarena.h"
49 #include "BLI_array.h"
50 #include "BLI_pbvh.h"
51 #include "BLI_utildefines.h"
52 #include "BLI_linklist.h"
53
54 #include "BKE_cdderivedmesh.h"
55 #include "BKE_displist.h"
56 #include "BKE_key.h"
57 #include "BKE_modifier.h"
58 #include "BKE_mesh.h"
59 #include "BKE_object.h"
60 #include "BKE_paint.h"
61 #include "BKE_texture.h"
62 #include "BKE_multires.h"
63 #include "BKE_armature.h"
64 #include "BKE_particle.h"
65 #include "BKE_tessmesh.h"
66 #include "BKE_bvhutils.h"
67
68 #ifdef WITH_GAMEENGINE
69 #include "BKE_navmesh_conversion.h"
70 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm);
71 #endif
72
73 #include "BLO_sys_types.h" // for intptr_t support
74
75 #include "GL/glew.h"
76
77 #include "GPU_buffers.h"
78 #include "GPU_draw.h"
79 #include "GPU_extensions.h"
80 #include "GPU_material.h"
81
82 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
83
84 static void add_shapekey_layers(DerivedMesh *dm, Mesh *me, Object *ob);
85 static void shapekey_layers_to_keyblocks(DerivedMesh *dm, Mesh *me, int actshape_uid);
86
87 ///////////////////////////////////
88 ///////////////////////////////////
89
90 static MVert *dm_getVertArray(DerivedMesh *dm)
91 {
92         MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
93
94         if (!mvert) {
95                 mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL,
96                         dm->getNumVerts(dm));
97                 CustomData_set_layer_flag(&dm->vertData, CD_MVERT, CD_FLAG_TEMPORARY);
98                 dm->copyVertArray(dm, mvert);
99         }
100
101         return mvert;
102 }
103
104 static MEdge *dm_getEdgeArray(DerivedMesh *dm)
105 {
106         MEdge *medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
107
108         if (!medge) {
109                 medge = CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL,
110                         dm->getNumEdges(dm));
111                 CustomData_set_layer_flag(&dm->edgeData, CD_MEDGE, CD_FLAG_TEMPORARY);
112                 dm->copyEdgeArray(dm, medge);
113         }
114
115         return medge;
116 }
117
118 static MFace *dm_getFaceArray(DerivedMesh *dm)
119 {
120         MFace *mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
121
122         if (!mface) {
123                 mface = CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL,
124                         dm->getNumTessFaces(dm));
125                 CustomData_set_layer_flag(&dm->faceData, CD_MFACE, CD_FLAG_TEMPORARY);
126                 dm->copyTessFaceArray(dm, mface);
127         }
128
129         return mface;
130 }
131
132 static MLoop *dm_getLoopArray(DerivedMesh *dm)
133 {
134         MLoop *mloop = CustomData_get_layer(&dm->loopData, CD_MLOOP);
135
136         if (!mloop) {
137                 mloop = CustomData_add_layer(&dm->loopData, CD_MLOOP, CD_CALLOC, NULL,
138                         dm->numLoopData);
139                 CustomData_set_layer_flag(&dm->loopData, CD_MLOOP, CD_FLAG_TEMPORARY);
140                 dm->copyLoopArray(dm, mloop);
141         }
142
143         return mloop;
144 }
145
146 static MPoly *dm_getPolyArray(DerivedMesh *dm)
147 {
148         MPoly *mpoly = CustomData_get_layer(&dm->polyData, CD_MPOLY);
149
150         if (!mpoly) {
151                 mpoly = CustomData_add_layer(&dm->polyData, CD_MPOLY, CD_CALLOC, NULL,
152                         dm->getNumFaces(dm));
153                 CustomData_set_layer_flag(&dm->polyData, CD_MPOLY, CD_FLAG_TEMPORARY);
154                 dm->copyPolyArray(dm, mpoly);
155         }
156
157         return mpoly;
158 }
159
160 static MVert *dm_dupVertArray(DerivedMesh *dm)
161 {
162         MVert *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumVerts(dm),
163                                                          "dm_dupVertArray tmp");
164
165         if(tmp) dm->copyVertArray(dm, tmp);
166
167         return tmp;
168 }
169
170 static MEdge *dm_dupEdgeArray(DerivedMesh *dm)
171 {
172         MEdge *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumEdges(dm),
173                                                          "dm_dupEdgeArray tmp");
174
175         if(tmp) dm->copyEdgeArray(dm, tmp);
176
177         return tmp;
178 }
179
180 static MFace *dm_dupFaceArray(DerivedMesh *dm)
181 {
182         MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumTessFaces(dm),
183                                                          "dm_dupFaceArray tmp");
184
185         if(tmp) dm->copyTessFaceArray(dm, tmp);
186
187         return tmp;
188 }
189
190 static MLoop *dm_dupLoopArray(DerivedMesh *dm)
191 {
192         MLoop *tmp = MEM_callocN(sizeof(*tmp) * dm->numLoopData,
193                                                          "dm_dupLoopArray tmp");
194
195         if(tmp) dm->copyLoopArray(dm, tmp);
196
197         return tmp;
198 }
199
200 static MPoly *dm_dupPolyArray(DerivedMesh *dm)
201 {
202         MPoly *tmp = MEM_callocN(sizeof(*tmp) * dm->numFaceData,
203                                                          "dm_dupPolyArray tmp");
204
205         if(tmp) dm->copyPolyArray(dm, tmp);
206
207         return tmp;
208 }
209
210 static CustomData *dm_getVertCData(DerivedMesh *dm)
211 {
212         return &dm->vertData;
213 }
214
215 static CustomData *dm_getEdgeCData(DerivedMesh *dm)
216 {
217         return &dm->edgeData;
218 }
219
220 static CustomData *dm_getFaceCData(DerivedMesh *dm)
221 {
222         return &dm->faceData;
223 }
224
225 static CustomData *dm_getLoopCData(DerivedMesh *dm)
226 {
227         return &dm->loopData;
228 }
229
230 static CustomData *dm_getPolyCData(DerivedMesh *dm)
231 {
232         return &dm->polyData;
233 }
234
235 void DM_init_funcs(DerivedMesh *dm)
236 {
237         /* default function implementations */
238         dm->getVertArray = dm_getVertArray;
239         dm->getEdgeArray = dm_getEdgeArray;
240         dm->getTessFaceArray = dm_getFaceArray;
241         dm->getLoopArray = dm_getLoopArray;
242         dm->getPolyArray = dm_getPolyArray;
243         dm->dupVertArray = dm_dupVertArray;
244         dm->dupEdgeArray = dm_dupEdgeArray;
245         dm->dupTessFaceArray = dm_dupFaceArray;
246         dm->dupLoopArray = dm_dupLoopArray;
247         dm->dupPolyArray = dm_dupPolyArray;
248
249         dm->getVertDataLayout = dm_getVertCData;
250         dm->getEdgeDataLayout = dm_getEdgeCData;
251         dm->getTessFaceDataLayout = dm_getFaceCData;
252         dm->getLoopDataLayout = dm_getLoopCData;
253         dm->getFaceDataLayout = dm_getPolyCData;
254
255         dm->getVertData = DM_get_vert_data;
256         dm->getEdgeData = DM_get_edge_data;
257         dm->getTessFaceData = DM_get_face_data;
258         dm->getVertDataArray = DM_get_vert_data_layer;
259         dm->getEdgeDataArray = DM_get_edge_data_layer;
260         dm->getTessFaceDataArray = DM_get_tessface_data_layer;
261
262         bvhcache_init(&dm->bvhCache);
263 }
264
265 void DM_init(DerivedMesh *dm, DerivedMeshType type, int numVerts, int numEdges,
266              int numFaces, int numLoops, int numPoly)
267 {
268         dm->type = type;
269         dm->numVertData = numVerts;
270         dm->numEdgeData = numEdges;
271         dm->numFaceData = numFaces;
272         dm->numLoopData = numLoops;
273         dm->numPolyData = numPoly;
274
275         DM_init_funcs(dm);
276         
277         dm->needsFree = 1;
278 }
279
280 void DM_from_template(DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type,
281                       int numVerts, int numEdges, int numFaces,
282                       int numLoops, int numPolys)
283 {
284         CustomData_copy(&source->vertData, &dm->vertData, CD_MASK_DERIVEDMESH,
285                                         CD_CALLOC, numVerts);
286         CustomData_copy(&source->edgeData, &dm->edgeData, CD_MASK_DERIVEDMESH,
287                                         CD_CALLOC, numEdges);
288         CustomData_copy(&source->faceData, &dm->faceData, CD_MASK_DERIVEDMESH,
289                                         CD_CALLOC, numFaces);
290         CustomData_copy(&source->loopData, &dm->loopData, CD_MASK_DERIVEDMESH,
291                         CD_CALLOC, numLoops);
292         CustomData_copy(&source->polyData, &dm->polyData, CD_MASK_DERIVEDMESH,
293                         CD_CALLOC, numPolys);
294
295         dm->type = type;
296         dm->numVertData = numVerts;
297         dm->numEdgeData = numEdges;
298         dm->numFaceData = numFaces;
299         dm->numLoopData = numLoops;
300         dm->numPolyData = numPolys;
301
302         DM_init_funcs(dm);
303
304         dm->needsFree = 1;
305 }
306
307 int DM_release(DerivedMesh *dm)
308 {
309         if (dm->needsFree) {
310                 bvhcache_free(&dm->bvhCache);
311                 GPU_drawobject_free( dm );
312                 CustomData_free(&dm->vertData, dm->numVertData);
313                 CustomData_free(&dm->edgeData, dm->numEdgeData);
314                 CustomData_free(&dm->faceData, dm->numFaceData);
315                 CustomData_free(&dm->loopData, dm->numLoopData);
316                 CustomData_free(&dm->polyData, dm->numPolyData);
317
318                 return 1;
319         }
320         else {
321                 CustomData_free_temporary(&dm->vertData, dm->numVertData);
322                 CustomData_free_temporary(&dm->edgeData, dm->numEdgeData);
323                 CustomData_free_temporary(&dm->faceData, dm->numFaceData);
324                 CustomData_free_temporary(&dm->loopData, dm->numLoopData);
325                 CustomData_free_temporary(&dm->polyData, dm->numPolyData);
326
327                 return 0;
328         }
329 }
330
331 void DM_DupPolys(DerivedMesh *source, DerivedMesh *target)
332 {
333         CustomData_free(&target->loopData, source->numLoopData);
334         CustomData_free(&target->polyData, source->numPolyData);
335
336         CustomData_copy(&source->loopData, &target->loopData, CD_MASK_DERIVEDMESH, CD_DUPLICATE, source->numLoopData);
337         CustomData_copy(&source->polyData, &target->polyData, CD_MASK_DERIVEDMESH, CD_DUPLICATE, source->numPolyData);
338
339         target->numLoopData = source->numLoopData;
340         target->numPolyData = source->numPolyData;
341
342         if (!CustomData_has_layer(&target->polyData, CD_MPOLY)) {
343                 MPoly *mpoly;
344                 MLoop *mloop;
345
346                 mloop = source->dupLoopArray(source);
347                 mpoly = source->dupPolyArray(source);
348                 CustomData_add_layer(&target->loopData, CD_MLOOP, CD_ASSIGN, mloop, source->numLoopData);
349                 CustomData_add_layer(&target->polyData, CD_MPOLY, CD_ASSIGN, mpoly, source->numPolyData);
350         }
351 }
352
353 void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob)
354 {
355         /* dm might depend on me, so we need to do everything with a local copy */
356         Mesh tmp = *me;
357         int totvert, totedge /*, totface */ /* UNUSED */, totloop, totpoly;
358         int did_shapekeys=0;
359         
360         memset(&tmp.vdata, 0, sizeof(tmp.vdata));
361         memset(&tmp.edata, 0, sizeof(tmp.edata));
362         memset(&tmp.fdata, 0, sizeof(tmp.fdata));
363         memset(&tmp.ldata, 0, sizeof(tmp.ldata));
364         memset(&tmp.pdata, 0, sizeof(tmp.pdata));
365
366         totvert = tmp.totvert = dm->getNumVerts(dm);
367         totedge = tmp.totedge = dm->getNumEdges(dm);
368         totpoly = tmp.totpoly = dm->getNumFaces(dm);
369         totloop = tmp.totloop = dm->numLoopData;
370
371         CustomData_copy(&dm->vertData, &tmp.vdata, CD_MASK_MESH, CD_DUPLICATE, totvert);
372         CustomData_copy(&dm->edgeData, &tmp.edata, CD_MASK_MESH, CD_DUPLICATE, totedge);
373         CustomData_copy(&dm->loopData, &tmp.ldata, CD_MASK_MESH, CD_DUPLICATE, totloop);
374         CustomData_copy(&dm->polyData, &tmp.pdata, CD_MASK_MESH, CD_DUPLICATE, totpoly);
375
376         if (CustomData_has_layer(&dm->vertData, CD_SHAPEKEY)) {
377                 KeyBlock *kb;
378                 int i=0;
379                 
380                 if (ob) {
381                         for (kb=me->key->block.first; kb; kb=kb->next, i++) {
382                                 if (i == ob->shapenr-1) {
383                                         i = kb->uid;
384                                         break;
385                                 }
386                         }
387                         
388                         if (!kb) {
389                                 printf("%s: error - could not find active shapekey %d!\n",
390                                        __func__, ob->shapenr-1);
391
392                                 i = INT_MAX;
393                         }
394                 } else {
395                         /*if no object, set to INT_MAX so we don't mess up any shapekey layers*/
396                         i = INT_MAX;
397                 }
398                 
399                 shapekey_layers_to_keyblocks(dm, me, i);
400                 did_shapekeys = 1;
401         }
402         
403         /* not all DerivedMeshes store their verts/edges/faces in CustomData, so
404            we set them here in case they are missing */
405         if(!CustomData_has_layer(&tmp.vdata, CD_MVERT))
406                 CustomData_add_layer(&tmp.vdata, CD_MVERT, CD_ASSIGN, dm->dupVertArray(dm), totvert);
407         if(!CustomData_has_layer(&tmp.edata, CD_MEDGE))
408                 CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, dm->dupEdgeArray(dm), totedge);
409         if(!CustomData_has_layer(&tmp.pdata, CD_MPOLY)) {
410                 tmp.mloop = dm->dupLoopArray(dm);
411                 tmp.mpoly = dm->dupPolyArray(dm);
412
413                 CustomData_add_layer(&tmp.ldata, CD_MLOOP, CD_ASSIGN, tmp.mloop, tmp.totloop);
414                 CustomData_add_layer(&tmp.pdata, CD_MPOLY, CD_ASSIGN, tmp.mpoly, tmp.totpoly);
415         }
416
417         /* object had got displacement layer, should copy this layer to save sculpted data */
418         /* NOTE: maybe some other layers should be copied? nazgul */
419         if(CustomData_has_layer(&me->ldata, CD_MDISPS)) {
420                 if (totloop == me->totloop) {
421                         MDisps *mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
422                         CustomData_add_layer(&tmp.ldata, CD_MDISPS, CD_DUPLICATE, mdisps, totloop);
423                 }
424         }
425
426         tmp.totface = mesh_recalcTesselation(&tmp.fdata, &tmp.ldata, &tmp.pdata, tmp.mvert, tmp.totface, tmp.totloop, tmp.totpoly);
427         mesh_update_customdata_pointers(&tmp);
428
429         CustomData_free(&me->vdata, me->totvert);
430         CustomData_free(&me->edata, me->totedge);
431         CustomData_free(&me->fdata, me->totface);
432         CustomData_free(&me->ldata, me->totloop);
433         CustomData_free(&me->pdata, me->totpoly);
434
435         /*  ok, this should now use new CD shapekey data,
436             which shouuld be fed through the modifier 
437                 stack*/
438         if(tmp.totvert != me->totvert && !did_shapekeys && me->key) {
439                 printf("YEEK! this should be recoded! Shape key loss!!!\n");
440                 if(tmp.key) tmp.key->id.us--;
441                 tmp.key = NULL;
442         }
443
444         *me = tmp;
445 }
446
447 void DM_to_meshkey(DerivedMesh *dm, Mesh *me, KeyBlock *kb)
448 {
449         int a, totvert = dm->getNumVerts(dm);
450         float *fp;
451         MVert *mvert;
452         
453         if(totvert==0 || me->totvert==0 || me->totvert!=totvert) return;
454         
455         if(kb->data) MEM_freeN(kb->data);
456         kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
457         kb->totelem= totvert;
458         
459         fp= kb->data;
460         mvert=dm->getVertDataArray(dm, CD_MVERT);
461         
462         for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
463                 copy_v3_v3(fp, mvert->co);
464         }
465 }
466
467 void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask)
468 {
469         CustomData_set_only_copy(&dm->vertData, mask);
470         CustomData_set_only_copy(&dm->edgeData, mask);
471         CustomData_set_only_copy(&dm->faceData, mask);
472 }
473
474 void DM_add_vert_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
475 {
476         CustomData_add_layer(&dm->vertData, type, alloctype, layer, dm->numVertData);
477 }
478
479 void DM_add_edge_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
480 {
481         CustomData_add_layer(&dm->edgeData, type, alloctype, layer, dm->numEdgeData);
482 }
483
484 void DM_add_tessface_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
485 {
486         CustomData_add_layer(&dm->faceData, type, alloctype, layer, dm->numFaceData);
487 }
488
489 void DM_add_loop_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
490 {
491         CustomData_add_layer(&dm->loopData, type, alloctype, layer, dm->numLoopData);
492 }
493
494 void DM_add_face_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
495 {
496         CustomData_add_layer(&dm->polyData, type, alloctype, layer, dm->numPolyData);
497 }
498
499 void *DM_get_vert_data(DerivedMesh *dm, int index, int type)
500 {
501         return CustomData_get(&dm->vertData, index, type);
502 }
503
504 void *DM_get_edge_data(DerivedMesh *dm, int index, int type)
505 {
506         return CustomData_get(&dm->edgeData, index, type);
507 }
508
509 void *DM_get_face_data(DerivedMesh *dm, int index, int type)
510 {
511         return CustomData_get(&dm->faceData, index, type);
512 }
513
514 void *DM_get_vert_data_layer(DerivedMesh *dm, int type)
515 {
516         if(type == CD_MVERT)
517                 return dm->getVertArray(dm);
518
519         return CustomData_get_layer(&dm->vertData, type);
520 }
521
522 void *DM_get_edge_data_layer(DerivedMesh *dm, int type)
523 {
524         if(type == CD_MEDGE)
525                 return dm->getEdgeArray(dm);
526
527         return CustomData_get_layer(&dm->edgeData, type);
528 }
529
530 void *DM_get_tessface_data_layer(DerivedMesh *dm, int type)
531 {
532         if (type == CD_MFACE)
533                 return dm->getTessFaceArray(dm);
534
535         return CustomData_get_layer(&dm->faceData, type);
536 }
537
538 void *DM_get_face_data_layer(DerivedMesh *dm, int type)
539 {
540         return CustomData_get_layer(&dm->polyData, type);
541 }
542
543 void DM_set_vert_data(DerivedMesh *dm, int index, int type, void *data)
544 {
545         CustomData_set(&dm->vertData, index, type, data);
546 }
547
548 void DM_set_edge_data(DerivedMesh *dm, int index, int type, void *data)
549 {
550         CustomData_set(&dm->edgeData, index, type, data);
551 }
552
553 void DM_set_face_data(DerivedMesh *dm, int index, int type, void *data)
554 {
555         CustomData_set(&dm->faceData, index, type, data);
556 }
557
558 void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest,
559                                            int source_index, int dest_index, int count)
560 {
561         CustomData_copy_data(&source->vertData, &dest->vertData,
562                                                  source_index, dest_index, count);
563 }
564
565 void DM_copy_edge_data(DerivedMesh *source, DerivedMesh *dest,
566                                            int source_index, int dest_index, int count)
567 {
568         CustomData_copy_data(&source->edgeData, &dest->edgeData,
569                                                  source_index, dest_index, count);
570 }
571
572 void DM_copy_tessface_data(DerivedMesh *source, DerivedMesh *dest,
573                                            int source_index, int dest_index, int count)
574 {
575         CustomData_copy_data(&source->faceData, &dest->faceData,
576                                                  source_index, dest_index, count);
577 }
578
579 void DM_copy_loop_data(DerivedMesh *source, DerivedMesh *dest,
580                        int source_index, int dest_index, int count)
581 {
582         CustomData_copy_data(&source->loopData, &dest->loopData,
583                              source_index, dest_index, count);
584 }
585
586 void DM_copy_face_data(DerivedMesh *source, DerivedMesh *dest,
587                        int source_index, int dest_index, int count)
588 {
589         CustomData_copy_data(&source->polyData, &dest->polyData,
590                              source_index, dest_index, count);
591 }
592
593 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count)
594 {
595         CustomData_free_elem(&dm->vertData, index, count);
596 }
597
598 void DM_free_edge_data(struct DerivedMesh *dm, int index, int count)
599 {
600         CustomData_free_elem(&dm->edgeData, index, count);
601 }
602
603 void DM_free_tessface_data(struct DerivedMesh *dm, int index, int count)
604 {
605         CustomData_free_elem(&dm->faceData, index, count);
606 }
607
608 void DM_free_loop_data(struct DerivedMesh *dm, int index, int count)
609 {
610         CustomData_free_elem(&dm->loopData, index, count);
611 }
612
613 void DM_free_face_data(struct DerivedMesh *dm, int index, int count)
614 {
615         CustomData_free_elem(&dm->polyData, index, count);
616 }
617
618 void DM_interp_vert_data(DerivedMesh *source, DerivedMesh *dest,
619                                                  int *src_indices, float *weights,
620                                                  int count, int dest_index)
621 {
622         CustomData_interp(&source->vertData, &dest->vertData, src_indices,
623                                           weights, NULL, count, dest_index);
624 }
625
626 void DM_interp_edge_data(DerivedMesh *source, DerivedMesh *dest,
627                                                  int *src_indices,
628                                                  float *weights, EdgeVertWeight *vert_weights,
629                                                  int count, int dest_index)
630 {
631         CustomData_interp(&source->edgeData, &dest->edgeData, src_indices,
632                                           weights, (float*)vert_weights, count, dest_index);
633 }
634
635 void DM_interp_tessface_data(DerivedMesh *source, DerivedMesh *dest,
636                                                  int *src_indices,
637                                                  float *weights, FaceVertWeight *vert_weights,
638                                                  int count, int dest_index)
639 {
640         CustomData_interp(&source->faceData, &dest->faceData, src_indices,
641                                           weights, (float*)vert_weights, count, dest_index);
642 }
643
644 void DM_swap_tessface_data(DerivedMesh *dm, int index, const int *corner_indices)
645 {
646         CustomData_swap(&dm->faceData, index, corner_indices);
647 }
648
649 void DM_interp_loop_data(DerivedMesh *source, DerivedMesh *dest,
650                          int *src_indices,
651                          float *weights, int count, int dest_index)
652 {
653         CustomData_interp(&source->loopData, &dest->loopData, src_indices,
654                           weights, NULL, count, dest_index);
655 }
656
657 void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
658                          int *src_indices,
659                          float *weights, int count, int dest_index)
660 {
661         CustomData_interp(&source->polyData, &dest->polyData, src_indices,
662                           weights, NULL, count, dest_index);
663 }
664
665 ///
666 DerivedMesh *mesh_create_derived(Mesh *me, Object *ob, float (*vertCos)[3])
667 {
668         DerivedMesh *dm = CDDM_from_mesh(me, ob);
669         
670         if(!dm)
671                 return NULL;
672         
673         if (vertCos)
674                 CDDM_apply_vert_coords(dm, vertCos);
675
676         CDDM_calc_normals(dm);
677
678         return dm;
679 }
680
681 /***/
682
683 DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, 
684         ModifierData *md, int build_shapekey_layers)
685 {
686         Mesh *me = ob->data;
687         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
688         DerivedMesh *dm;
689
690         md->scene= scene;
691         
692         if (!(md->mode&eModifierMode_Realtime)) return NULL;
693         if (mti->isDisabled && mti->isDisabled(md, 0)) return NULL;
694         
695         if (build_shapekey_layers && me->key && ob->shapenr <= BLI_countlist(&me->key->block)) {
696                 key_to_mesh(BLI_findlink(&me->key->block, ob->shapenr-1), me);
697         }
698         
699         if (mti->type==eModifierTypeType_OnlyDeform) {
700                 int numVerts;
701                 float (*deformedVerts)[3] = mesh_getVertexCos(me, &numVerts);
702
703                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, 0, 0);
704                 dm = mesh_create_derived(me, ob, deformedVerts);
705
706                 if (build_shapekey_layers)
707                         add_shapekey_layers(dm, me, ob);
708                 
709                 MEM_freeN(deformedVerts);
710         } else {
711                 DerivedMesh *tdm = mesh_create_derived(me, ob, NULL);
712
713                 if (build_shapekey_layers)
714                         add_shapekey_layers(tdm, me, ob);
715                 
716                 dm = mti->applyModifier(md, ob, tdm, 0, 0);
717
718                 if(tdm != dm) tdm->release(tdm);
719         }
720
721         return dm;
722 }
723
724 static float *get_editbmesh_orco_verts(BMEditMesh *em)
725 {
726         BMIter iter;
727         BMVert *eve;
728         float *orco;
729         int a, totvert;
730
731         /* these may not really be the orco's, but it's only for preview.
732          * could be solver better once, but isn't simple */
733
734         totvert= em->bm->totvert;
735         
736         orco = MEM_mallocN(sizeof(float)*3*totvert, "EditMesh Orco");
737
738         eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
739         for (a=0; eve; eve=BMIter_Step(&iter), a+=3) {
740                 copy_v3_v3(orco+a, eve->co);
741         }
742         
743         return orco;
744 }
745
746 /* orco custom data layer */
747 static void *get_orco_coords_dm(Object *ob, BMEditMesh *em, int layer, int *free)
748 {
749         *free= 0;
750
751         if(layer == CD_ORCO) {
752                 /* get original coordinates */
753                 *free= 1;
754
755                 if(em)
756                         return (float(*)[3])get_editbmesh_orco_verts(em);
757                 else
758                         return (float(*)[3])get_mesh_orco_verts(ob);
759         }
760         else if(layer == CD_CLOTH_ORCO) {
761                 /* apply shape key for cloth, this should really be solved
762                    by a more flexible customdata system, but not simple */
763                 if(!em) {
764                         ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
765                         if (clmd) {
766                                 KeyBlock *kb= key_get_keyblock(ob_get_key(ob), clmd->sim_parms->shapekey_rest);
767         
768                                 if(kb->data)
769                                         return kb->data;
770                         }
771                 }
772
773                 return NULL;
774         }
775
776         return NULL;
777 }
778
779 static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, BMEditMesh *em, int layer)
780 {
781         DerivedMesh *dm;
782         float (*orco)[3];
783         int free;
784
785         if(em) dm= CDDM_from_BMEditMesh(em, me, 0);
786         else dm= CDDM_from_mesh(me, ob);
787
788         orco= get_orco_coords_dm(ob, em, layer, &free);
789
790         if(orco) {
791                 CDDM_apply_vert_coords(dm, orco);
792                 if(free) MEM_freeN(orco);
793         }
794
795         CDDM_calc_normals(dm);
796
797         return dm;
798 }
799
800 static void add_orco_dm(Object *ob, BMEditMesh *em, DerivedMesh *dm,
801                                                 DerivedMesh *orcodm, int layer)
802 {
803         float (*orco)[3], (*layerorco)[3];
804         int totvert, free;
805
806         totvert= dm->getNumVerts(dm);
807
808         if(orcodm) {
809                 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
810                 free= 1;
811
812                 if(orcodm->getNumVerts(orcodm) == totvert)
813                         orcodm->getVertCos(orcodm, orco);
814                 else
815                         dm->getVertCos(dm, orco);
816         }
817         else
818                 orco= get_orco_coords_dm(ob, em, layer, &free);
819
820         if(orco) {
821                 if(layer == CD_ORCO)
822                         transform_mesh_orco_verts(ob->data, orco, totvert, 0);
823
824                 if(!(layerorco = DM_get_vert_data_layer(dm, layer))) {
825                         DM_add_vert_layer(dm, layer, CD_CALLOC, NULL);
826                         layerorco = DM_get_vert_data_layer(dm, layer);
827                 }
828
829                 memcpy(layerorco, orco, sizeof(float)*3*totvert);
830                 if(free) MEM_freeN(orco);
831         }
832 }
833
834 /* weight paint colors */
835
836 /* Something of a hack, at the moment deal with weightpaint
837  * by tucking into colors during modifier eval, only in
838  * wpaint mode. Works ok but need to make sure recalc
839  * happens on enter/exit wpaint.
840  */
841
842 void weight_to_rgb(float input, float *fr, float *fg, float *fb)
843 {
844         float blend;
845         
846         blend= ((input/2.0f)+0.5f);
847         
848         if (input<=0.25f){      // blue->cyan
849                 *fr= 0.0f;
850                 *fg= blend*input*4.0f;
851                 *fb= blend;
852         }
853         else if (input<=0.50f){ // cyan->green
854                 *fr= 0.0f;
855                 *fg= blend;
856                 *fb= blend*(1.0f-((input-0.25f)*4.0f)); 
857         }
858         else if (input <= 0.75f){       // green->yellow
859                 *fr= blend * ((input-0.50f)*4.0f);
860                 *fg= blend;
861                 *fb= 0.0f;
862         }
863         else if (input <= 1.0f){ // yellow->red
864                 *fr= blend;
865                 *fg= blend * (1.0f-((input-0.75f)*4.0f)); 
866                 *fb= 0.0f;
867         }
868 }
869
870 /* draw_flag's for calc_weightpaint_vert_color */
871 enum {
872         CALC_WP_MULTIPAINT= (1<<0),
873         CALC_WP_AUTO_NORMALIZE= (1<<1),
874 };
875
876 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)
877 {
878         Mesh *me = ob->data;
879         float colf[4], input = 0.0f;
880         int i;
881
882         
883         int make_black= FALSE;
884
885         if (me->dvert) {
886                 if ((selected > 1) && (draw_flag & CALC_WP_MULTIPAINT)) {
887                         
888                         int was_a_nonzero= FALSE;
889                         for (i=0; i<me->dvert[vert].totweight; i++) {
890                                 /* in multipaint, get the average if auto normalize is inactive
891                                  * get the sum if it is active */
892                                 if(dg_flags[me->dvert[vert].dw[i].def_nr]) {
893                                         if(me->dvert[vert].dw[i].weight) {
894                                                 input+= me->dvert[vert].dw[i].weight;
895                                                 was_a_nonzero= TRUE;
896                                         }
897                                 }
898                         }
899
900                         /* make it black if the selected groups have no weight on a vertex */
901                         if(was_a_nonzero == FALSE) {
902                                 make_black = TRUE;
903                         }
904                         else if ((draw_flag & CALC_WP_AUTO_NORMALIZE) == FALSE) {
905                                 input /= selected; /* get the average */
906                         }
907                 }
908                 else {
909                         /* default, non tricky behavior */
910                         for (i=0; i<me->dvert[vert].totweight; i++) {
911                                 if (me->dvert[vert].dw[i].def_nr==ob->actdef-1) {
912                                         input+=me->dvert[vert].dw[i].weight;
913                                 }
914                         }
915                 }
916         }
917         
918         if (make_black) {
919                 col[3] = 0;
920                 col[2] = 0;
921                 col[1] = 0;
922                 col[0] = 255;
923                 return;
924         }
925
926         CLAMP(input, 0.0f, 1.0f);       
927
928         if(coba)
929                 do_colorband(coba, input, colf);
930         else
931                 weight_to_rgb(input, colf, colf+1, colf+2);
932         
933         col[3] = (unsigned char)(colf[0] * 255.0f);
934         col[2] = (unsigned char)(colf[1] * 255.0f);
935         col[1] = (unsigned char)(colf[2] * 255.0f);
936         col[0] = 255;
937 }
938
939 static ColorBand *stored_cb= NULL;
940
941 void vDM_ColorBand_store(ColorBand *coba)
942 {
943         stored_cb= coba;
944 }
945
946 static void add_weight_mcol_dm(Object *ob, DerivedMesh *dm, int const draw_flag)
947 {
948         // Mesh *me = ob->data; // UNUSED
949         MFace *mf = dm->getTessFaceArray(dm);
950         MLoop *mloop = dm->getLoopArray(dm), *ml;
951         MPoly *mp = dm->getPolyArray(dm);
952         ColorBand *coba= stored_cb;     /* warning, not a local var */
953         unsigned char *wtcol;
954         unsigned char(*wlcol)[4] = NULL;
955         BLI_array_declare(wlcol);
956         int i, j, totface=dm->getNumTessFaces(dm), totloop;
957         int *origIndex = dm->getVertDataArray(dm, CD_ORIGINDEX);
958
959         int defbase_len = BLI_countlist(&ob->defbase);
960         char *defbase_sel = MEM_mallocN(defbase_len * sizeof(char), __func__);
961         int selected = get_selected_defgroups(ob, defbase_sel, defbase_len);
962         int unselected = defbase_len - selected;
963
964         wtcol = MEM_callocN (sizeof (unsigned char) * totface*4*4, "weightmap");
965         
966         /*first add colors to the tesselation faces*/
967         memset(wtcol, 0x55, sizeof (unsigned char) * totface*4*4);
968         for (i=0; i<totface; i++, mf++) {
969                 /*origindex being NULL means we're operating on original mesh data*/
970                 calc_weightpaint_vert_color(ob, coba, mf->v1, &wtcol[(i*4 + 0)*4], defbase_sel, selected, unselected, draw_flag);
971                 calc_weightpaint_vert_color(ob, coba, mf->v2, &wtcol[(i*4 + 1)*4], defbase_sel, selected, unselected, draw_flag);
972                 calc_weightpaint_vert_color(ob, coba, mf->v3, &wtcol[(i*4 + 2)*4], defbase_sel, selected, unselected, draw_flag);
973                 if (mf->v4)
974                         calc_weightpaint_vert_color(ob, coba, mf->v4, &wtcol[(i*4 + 3)*4], defbase_sel, selected, unselected, draw_flag);
975         }
976         
977         CustomData_add_layer(&dm->faceData, CD_WEIGHT_MCOL, CD_ASSIGN, wtcol, totface);
978
979         /*now add to loops, so the data can be passed through the modifier stack*/
980         totloop = 0;
981         for (i=0; i<dm->numPolyData; i++, mp++) {
982                 ml = mloop + mp->loopstart;
983
984                 for (j=0; j<mp->totloop; j++, ml++, totloop++) {
985                         BLI_array_growone(wlcol);
986
987                         calc_weightpaint_vert_color(ob, coba, origIndex ? origIndex[ml->v] : ml->v,
988                                                                                 (unsigned char *)&wlcol[totloop], defbase_sel, selected, unselected, draw_flag);
989                 }
990         }
991
992         MEM_freeN(defbase_sel);
993
994         CustomData_add_layer(&dm->loopData, CD_WEIGHT_MLOOPCOL, CD_ASSIGN, wlcol, totloop);
995 }
996
997
998 static void shapekey_layers_to_keyblocks(DerivedMesh *dm, Mesh *me, int actshape_uid)
999 {
1000         KeyBlock *kb;
1001         int i, j, tot;
1002         
1003         if (!me->key)
1004                 return; 
1005         
1006         tot = CustomData_number_of_layers(&dm->vertData, CD_SHAPEKEY);
1007         for (i=0; i<tot; i++) {
1008                 CustomDataLayer *layer = &dm->vertData.layers[CustomData_get_layer_index_n(&dm->vertData, CD_SHAPEKEY, i)];
1009                 float (*cos)[3], (*kbcos)[3];
1010                 
1011                 for (kb=me->key->block.first; kb; kb=kb->next) {
1012                         if (kb->uid == layer->uid)
1013                                 break;
1014                 }
1015                 
1016                 if (!kb) {
1017                         kb = add_keyblock(me->key, layer->name);
1018                         kb->uid = layer->uid;
1019                 }
1020                 
1021                 if (kb->data)
1022                         MEM_freeN(kb->data);
1023                 
1024                 cos = CustomData_get_layer_n(&dm->vertData, CD_SHAPEKEY, i);
1025                 kb->totelem = dm->numVertData;
1026
1027                 kb->data = kbcos = MEM_mallocN(sizeof(float)*3*kb->totelem, "kbcos DerivedMesh.c");
1028                 if (kb->uid == actshape_uid) {
1029                         MVert *mvert = dm->getVertArray(dm);
1030                         
1031                         for (j=0; j<dm->numVertData; j++, kbcos++, mvert++) {
1032                                 copy_v3_v3(*kbcos, mvert->co);
1033                         }
1034                 } else {
1035                         for (j=0; j<kb->totelem; j++, cos++, kbcos++) {
1036                                 copy_v3_v3(*kbcos, *cos);
1037                         }
1038                 }
1039         }
1040         
1041         for (kb=me->key->block.first; kb; kb=kb->next) {
1042                 if (kb->totelem != dm->numVertData) {
1043                         if (kb->data)
1044                                 MEM_freeN(kb->data);
1045                         
1046                         kb->totelem = dm->numVertData;
1047                         kb->data = MEM_callocN(sizeof(float)*3*kb->totelem, "kb->data derivedmesh.c");
1048                         fprintf(stderr, "%s: lost a shapekey layer! (bmesh internal error)\n", __func__);
1049                 }
1050         }
1051 }
1052
1053 static void add_shapekey_layers(DerivedMesh *dm, Mesh *me, Object *UNUSED(ob))
1054 {
1055         KeyBlock *kb;
1056         Key *key = me->key;
1057         int a, b;
1058         
1059         if (!me->key)
1060                 return;
1061         
1062         if (dm->numVertData != me->totvert) {
1063                 printf("error in add_shapekey_layers: dm isn't the same size as me\n");
1064                 return;
1065         }
1066                 
1067         for (a=0, kb=key->block.first; kb; kb=kb->next, a++) {
1068                 float (*cos)[3] = CustomData_add_layer_named(&dm->vertData, CD_SHAPEKEY, CD_CALLOC, NULL, dm->numVertData, kb->name);
1069                 int ci = CustomData_get_layer_index_n(&dm->vertData, CD_SHAPEKEY, a);
1070                 
1071                 dm->vertData.layers[ci].uid = kb->uid;
1072                 if (kb->totelem != dm->numVertData) {
1073                         printf("error in add_shapekey_layers: totelem and totvert don't match");
1074                         continue;
1075                 }
1076                 
1077                 for (b=0; b<kb->totelem; b++, cos++) {
1078                         copy_v3_v3((float *)cos, ((float*)kb->data)+b*3);
1079                 }
1080         }
1081 }
1082
1083 /* new value for useDeform -1  (hack for the gameengine):
1084  * - apply only the modifier stack of the object, skipping the virtual modifiers,
1085  * - don't apply the key
1086  * - apply deform modifiers and input vertexco
1087  */
1088 static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos)[3],
1089                                                                 DerivedMesh **deform_r, DerivedMesh **final_r,
1090                                                                 int useRenderParams, int useDeform,
1091                                                                 int needMapping, CustomDataMask dataMask, 
1092                                                                 int index, int useCache, int build_shapekey_layers)
1093 {
1094         Mesh *me = ob->data;
1095         ModifierData *firstmd, *md;
1096         LinkNode *datamasks, *curr;
1097         CustomDataMask mask, nextmask, append_mask = 0;
1098         float (*deformedVerts)[3] = NULL;
1099         DerivedMesh *dm=NULL, *orcodm, *clothorcodm, *finaldm;
1100         int numVerts = me->totvert;
1101         int required_mode;
1102         int isPrevDeform= FALSE;
1103         int skipVirtualArmature = (useDeform < 0);
1104         MultiresModifierData *mmd= get_multires_modifier(scene, ob, 0);
1105         int has_multires = mmd != NULL, multires_applied = 0;
1106         int sculpt_mode = ob->mode & OB_MODE_SCULPT && ob->sculpt;
1107
1108         int draw_flag= ((scene->toolsettings->multipaint ? CALC_WP_MULTIPAINT : 0) |
1109                         (scene->toolsettings->auto_normalize ? CALC_WP_AUTO_NORMALIZE : 0));
1110
1111         if(mmd && !mmd->sculptlvl)
1112                 has_multires = 0;
1113
1114         if(!skipVirtualArmature) {
1115                 firstmd = modifiers_getVirtualModifierList(ob);
1116         }
1117         else {
1118                 /* game engine exception */
1119                 firstmd = ob->modifiers.first;
1120                 if(firstmd && firstmd->type == eModifierType_Armature)
1121                         firstmd = firstmd->next;
1122         }
1123
1124         md = firstmd;
1125
1126         modifiers_clearErrors(ob);
1127
1128         if(useRenderParams) required_mode = eModifierMode_Render;
1129         else required_mode = eModifierMode_Realtime;
1130
1131         datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
1132         curr = datamasks;
1133
1134         if(deform_r) *deform_r = NULL;
1135         *final_r = NULL;
1136
1137         if(useDeform) {
1138                 if(inputVertexCos)
1139                         deformedVerts = inputVertexCos;
1140                 
1141                 /* Apply all leading deforming modifiers */
1142                 for(;md; md = md->next, curr = curr->next) {
1143                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1144
1145                         md->scene= scene;
1146                         
1147                         if(!modifier_isEnabled(scene, md, required_mode)) continue;
1148                         if(useDeform < 0 && mti->dependsOnTime && mti->dependsOnTime(md)) continue;
1149
1150                         if(mti->type == eModifierTypeType_OnlyDeform) {
1151                                 if(!deformedVerts)
1152                                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1153
1154                                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, useRenderParams, useDeform);
1155                         } else {
1156                                 break;
1157                         }
1158                         
1159                         /* grab modifiers until index i */
1160                         if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
1161                                 break;
1162                 }
1163
1164                 /* Result of all leading deforming modifiers is cached for
1165                  * places that wish to use the original mesh but with deformed
1166                  * coordinates (vpaint, etc.)
1167                  */
1168                 if (deform_r) {
1169                         *deform_r = CDDM_from_mesh(me, ob);
1170                         
1171                         if (build_shapekey_layers)
1172                                 add_shapekey_layers(dm, me, ob);
1173                         
1174                         if(deformedVerts) {
1175                                 CDDM_apply_vert_coords(*deform_r, deformedVerts);
1176                                 CDDM_calc_normals(*deform_r);
1177                         }
1178                 }
1179         } else {
1180                 /* default behaviour for meshes */
1181                 if(inputVertexCos)
1182                         deformedVerts = inputVertexCos;
1183                 else
1184                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1185         }
1186
1187
1188         /* Now apply all remaining modifiers. If useDeform is off then skip
1189          * OnlyDeform ones. 
1190          */
1191         dm = NULL;
1192         orcodm = NULL;
1193         clothorcodm = NULL;
1194
1195         for(;md; md = md->next, curr = curr->next) {
1196                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1197
1198                 md->scene= scene;
1199
1200                 if(!modifier_isEnabled(scene, md, required_mode)) continue;
1201                 if(mti->type == eModifierTypeType_OnlyDeform && !useDeform) continue;
1202                 if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
1203                         modifier_setError(md, "Modifier requires original data, bad stack position.");
1204                         continue;
1205                 }
1206                 if(sculpt_mode && (!has_multires || multires_applied)) {
1207                         int unsupported= 0;
1208
1209                         if(scene->toolsettings->sculpt->flags & SCULPT_ONLY_DEFORM)
1210                                 unsupported|= mti->type != eModifierTypeType_OnlyDeform;
1211
1212                         unsupported|= md->type == eModifierType_Multires && ((MultiresModifierData*)md)->sculptlvl==0;
1213                         unsupported|= multires_applied;
1214
1215                         if(unsupported) {
1216                                 modifier_setError(md, "Not supported in sculpt mode.");
1217                                 continue;
1218                         }
1219                 }
1220                 if(needMapping && !modifier_supportsMapping(md)) continue;
1221                 if(useDeform < 0 && mti->dependsOnTime && mti->dependsOnTime(md)) continue;
1222
1223                 /* add an orco layer if needed by this modifier */
1224                 if(mti->requiredDataMask)
1225                         mask = mti->requiredDataMask(ob, md);
1226                 else
1227                         mask = 0;
1228
1229                 if(dm && (mask & CD_MASK_ORCO))
1230                         add_orco_dm(ob, NULL, dm, orcodm, CD_ORCO);
1231
1232                 /* How to apply modifier depends on (a) what we already have as
1233                  * a result of previous modifiers (could be a DerivedMesh or just
1234                  * deformed vertices) and (b) what type the modifier is.
1235                  */
1236
1237                 if(mti->type == eModifierTypeType_OnlyDeform) {
1238                         /* No existing verts to deform, need to build them. */
1239                         if(!deformedVerts) {
1240                                 if(dm) {
1241                                         /* Deforming a derived mesh, read the vertex locations
1242                                          * out of the mesh and deform them. Once done with this
1243                                          * run of deformers verts will be written back.
1244                                          */
1245                                         numVerts = dm->getNumVerts(dm);
1246                                         deformedVerts =
1247                                                 MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
1248                                         dm->getVertCos(dm, deformedVerts);
1249                                 } else {
1250                                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1251                                 }
1252                         }
1253
1254                         /* if this is not the last modifier in the stack then recalculate the normals
1255                          * to avoid giving bogus normals to the next modifier see: [#23673] */
1256                         if(dm && isPrevDeform &&  mti->dependsOnNormals && mti->dependsOnNormals(md)) {
1257                                 /* XXX, this covers bug #23673, but we may need normal calc for other types */
1258                                 if(dm && dm->type == DM_TYPE_CDDM) {
1259                                         CDDM_apply_vert_coords(dm, deformedVerts);
1260                                         CDDM_calc_normals(dm);
1261                                 }
1262                         }
1263
1264                         mti->deformVerts(md, ob, dm, deformedVerts, numVerts, useRenderParams, useDeform);
1265                 } else {
1266                         DerivedMesh *ndm;
1267
1268                         /* determine which data layers are needed by following modifiers */
1269                         if(curr->next)
1270                                 nextmask= (CustomDataMask)GET_INT_FROM_POINTER(curr->next->link);
1271                         else
1272                                 nextmask= dataMask;
1273
1274                         /* apply vertex coordinates or build a DerivedMesh as necessary */
1275                         if(dm) {
1276                                 if(deformedVerts) {
1277                                         DerivedMesh *tdm = CDDM_copy(dm, 0);
1278                                         dm->release(dm);
1279                                         dm = tdm;
1280
1281                                         CDDM_apply_vert_coords(dm, deformedVerts);
1282                                         CDDM_calc_normals(dm);
1283                                 }
1284                         } else {
1285                                 dm = CDDM_from_mesh(me, ob);
1286
1287                                 if (build_shapekey_layers)
1288                                         add_shapekey_layers(dm, me, ob);
1289
1290                                 if(deformedVerts) {
1291                                         CDDM_apply_vert_coords(dm, deformedVerts);
1292                                         CDDM_calc_normals(dm);
1293                                 }
1294
1295                                 /* Constructive modifiers need to have an origindex
1296                                  * otherwise they wont have anywhere to copy the data from.
1297                                  *
1298                                  * Also create ORIGINDEX data if any of the following modifiers
1299                                  * requests it, this way Mirror, Solidify etc will keep ORIGINDEX
1300                                  * data by using generic DM_copy_vert_data() functions.
1301                                  */
1302                                 if(needMapping || (nextmask & CD_MASK_ORIGINDEX)) {
1303                                         /* calc */
1304                                         DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
1305                                         DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
1306                                         DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
1307
1308                                         range_vn_i(DM_get_vert_data_layer(dm, CD_ORIGINDEX), dm->numVertData, 0);
1309                                         range_vn_i(DM_get_edge_data_layer(dm, CD_ORIGINDEX), dm->numEdgeData, 0);
1310                                         range_vn_i(DM_get_face_data_layer(dm, CD_ORIGINDEX), dm->numPolyData, 0);
1311                                 }
1312
1313                                 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
1314                                         add_weight_mcol_dm(ob, dm, draw_flag);
1315
1316                         }
1317
1318                         
1319                         /* set the DerivedMesh to only copy needed data */
1320                         mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
1321                         /* needMapping check here fixes bug [#28112], otherwise its
1322                          * possible that it wont be copied */
1323                         mask |= append_mask;
1324                         DM_set_only_copy(dm, mask | (needMapping ? CD_MASK_ORIGINDEX : 0));
1325                         
1326                         /* add cloth rest shape key if need */
1327                         if(mask & CD_MASK_CLOTH_ORCO)
1328                                 add_orco_dm(ob, NULL, dm, clothorcodm, CD_CLOTH_ORCO);
1329
1330                         /* add an origspace layer if needed */
1331                         if(((CustomDataMask)GET_INT_FROM_POINTER(curr->link)) & CD_MASK_ORIGSPACE)
1332                                 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
1333                                         DM_add_tessface_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
1334
1335                         ndm = mti->applyModifier(md, ob, dm, useRenderParams, useCache);
1336
1337                         if(ndm) {
1338                                 /* if the modifier returned a new dm, release the old one */
1339                                 if(dm && dm != ndm) dm->release(dm);
1340
1341                                 dm = ndm;
1342
1343                                 if(deformedVerts) {
1344                                         if(deformedVerts != inputVertexCos)
1345                                                 MEM_freeN(deformedVerts);
1346
1347                                         deformedVerts = NULL;
1348                                 }
1349                         } 
1350
1351                         /* create an orco derivedmesh in parallel */
1352                         if(nextmask & CD_MASK_ORCO) {
1353                                 if(!orcodm)
1354                                         orcodm= create_orco_dm(ob, me, NULL, CD_ORCO);
1355
1356                                 nextmask &= ~CD_MASK_ORCO;
1357                                 DM_set_only_copy(orcodm, nextmask | CD_MASK_ORIGINDEX);
1358                                 ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, 0);
1359
1360                                 if(ndm) {
1361                                         /* if the modifier returned a new dm, release the old one */
1362                                         if(orcodm && orcodm != ndm) orcodm->release(orcodm);
1363                                         orcodm = ndm;
1364                                 }
1365                         }
1366
1367                         /* create cloth orco derivedmesh in parallel */
1368                         if(nextmask & CD_MASK_CLOTH_ORCO) {
1369                                 if(!clothorcodm)
1370                                         clothorcodm= create_orco_dm(ob, me, NULL, CD_CLOTH_ORCO);
1371
1372                                 nextmask &= ~CD_MASK_CLOTH_ORCO;
1373                                 DM_set_only_copy(clothorcodm, nextmask | CD_MASK_ORIGINDEX);
1374                                 ndm = mti->applyModifier(md, ob, clothorcodm, useRenderParams, 0);
1375
1376                                 if(ndm) {
1377                                         /* if the modifier returned a new dm, release the old one */
1378                                         if(clothorcodm && clothorcodm != ndm) clothorcodm->release(clothorcodm);
1379                                         clothorcodm = ndm;
1380                                 }
1381                         }
1382
1383                         /* in case of dynamic paint, make sure preview mask remains for following modifiers */
1384                         if (md->type == eModifierType_DynamicPaint)
1385                                 append_mask |= CD_MASK_WEIGHT_MCOL;
1386                 }
1387
1388                 isPrevDeform= (mti->type == eModifierTypeType_OnlyDeform);
1389
1390                 /* grab modifiers until index i */
1391                 if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
1392                         break;
1393
1394                 if(sculpt_mode && md->type == eModifierType_Multires)
1395                         multires_applied = 1;
1396         }
1397
1398         for(md=firstmd; md; md=md->next)
1399                 modifier_freeTemporaryData(md);
1400
1401         /* Yay, we are done. If we have a DerivedMesh and deformed vertices
1402          * need to apply these back onto the DerivedMesh. If we have no
1403          * DerivedMesh then we need to build one.
1404          */
1405         if(dm && deformedVerts) {
1406                 finaldm = CDDM_copy(dm, 0);
1407
1408                 dm->release(dm);
1409
1410                 CDDM_apply_vert_coords(finaldm, deformedVerts);
1411                 CDDM_calc_normals(finaldm);
1412
1413                 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
1414                         add_weight_mcol_dm(ob, finaldm, draw_flag);
1415         } else if(dm) {
1416                 finaldm = dm;
1417         } else {
1418                 int recalc_normals= 0;
1419
1420                 finaldm = CDDM_from_mesh(me, ob);
1421                 
1422                 if(build_shapekey_layers) {
1423                         add_shapekey_layers(finaldm, me, ob);
1424                         recalc_normals= 1;
1425                 }
1426                 
1427                 if(deformedVerts) {
1428                         CDDM_apply_vert_coords(finaldm, deformedVerts);
1429                         recalc_normals= 1;
1430                 }
1431
1432                 if(recalc_normals)
1433                         CDDM_calc_normals(finaldm);
1434                 
1435                 if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
1436                         add_weight_mcol_dm(ob, finaldm, draw_flag);
1437         }
1438
1439         /* add an orco layer if needed */
1440         if(dataMask & CD_MASK_ORCO) {
1441                 add_orco_dm(ob, NULL, finaldm, orcodm, CD_ORCO);
1442
1443                 if(deform_r && *deform_r)
1444                         add_orco_dm(ob, NULL, *deform_r, NULL, CD_ORCO);
1445         }
1446
1447 #ifdef WITH_GAMEENGINE
1448         /* NavMesh - this is a hack but saves having a NavMesh modifier */
1449         if ((ob->gameflag & OB_NAVMESH) && (finaldm->type == DM_TYPE_CDDM)) {
1450                 DerivedMesh *tdm;
1451                 tdm= navmesh_dm_createNavMeshForVisualization(finaldm);
1452                 if (finaldm != tdm) {
1453                         finaldm->release(finaldm);
1454                         finaldm= tdm;
1455                 }
1456         }
1457 #endif /* WITH_GAMEENGINE */
1458
1459         /* Re-tesselation is necessary to push render data (uvs, textures, colors)
1460            from loops and polys onto the tessfaces. This may be currently be redundant
1461            in cases where the render mode doesn't use these inputs, but ideally
1462            eventually tesselation would happen on-demand, and this is one of the primary
1463            places it would be needed. */
1464         finaldm->recalcTesselation(finaldm);
1465         finaldm->calcNormals(finaldm);
1466
1467         *final_r = finaldm;
1468
1469         if(orcodm)
1470                 orcodm->release(orcodm);
1471         if(clothorcodm)
1472                 clothorcodm->release(clothorcodm);
1473
1474         if(deformedVerts && deformedVerts != inputVertexCos)
1475                 MEM_freeN(deformedVerts);
1476
1477         BLI_linklist_free(datamasks, NULL);
1478 }
1479
1480 float (*editbmesh_get_vertex_cos(BMEditMesh *em, int *numVerts_r))[3]
1481 {
1482         int i, numVerts = *numVerts_r = em->bm->totvert;
1483         float (*cos)[3];
1484         BMIter iter;
1485         BMVert *eve;
1486
1487         cos = MEM_mallocN(sizeof(float)*3*numVerts, "vertexcos");
1488
1489         eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
1490         for (i=0; eve; eve=BMIter_Step(&iter), i++) {
1491                 copy_v3_v3(cos[i], eve->co);
1492         }
1493
1494         return cos;
1495 }
1496
1497 int editbmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
1498 {
1499         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1500         int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1501
1502         if(!modifier_isEnabled(scene, md, required_mode)) return 0;
1503         if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
1504                 modifier_setError(md, "Modifier requires original data, bad stack position.");
1505                 return 0;
1506         }
1507         
1508         return 1;
1509 }
1510
1511 static void editbmesh_calc_modifiers(Scene *scene, Object *ob, BMEditMesh *em, DerivedMesh **cage_r,
1512                                                                         DerivedMesh **final_r,
1513                                                                         CustomDataMask dataMask)
1514 {
1515         ModifierData *md;
1516         float (*deformedVerts)[3] = NULL;
1517         CustomDataMask mask;
1518         DerivedMesh *dm = NULL, *orcodm = NULL, *finaldm = NULL;
1519         int i, numVerts = 0, cageIndex = modifiers_getCageIndex(scene, ob, NULL, 1);
1520         LinkNode *datamasks, *curr;
1521         int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1522
1523         modifiers_clearErrors(ob);
1524
1525         if(cage_r && cageIndex == -1) {
1526                 *cage_r = getEditDerivedBMesh(em, ob, NULL);
1527         }
1528
1529         md = modifiers_getVirtualModifierList(ob);
1530
1531         datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
1532
1533         curr = datamasks;
1534         for(i = 0; md; i++, md = md->next, curr = curr->next) {
1535                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1536
1537                 md->scene= scene;
1538                 
1539                 if(!editbmesh_modifier_is_enabled(scene, md, dm))
1540                         continue;
1541
1542                 /* add an orco layer if needed by this modifier */
1543                 if(dm && mti->requiredDataMask) {
1544                         mask = mti->requiredDataMask(ob, md);
1545                         if(mask & CD_MASK_ORCO)
1546                                 add_orco_dm(ob, em, dm, orcodm, CD_ORCO);
1547                 }
1548
1549                 /* How to apply modifier depends on (a) what we already have as
1550                  * a result of previous modifiers (could be a DerivedMesh or just
1551                  * deformed vertices) and (b) what type the modifier is.
1552                  */
1553
1554                 if(mti->type == eModifierTypeType_OnlyDeform) {
1555                         /* No existing verts to deform, need to build them. */
1556                         if(!deformedVerts) {
1557                                 if(dm) {
1558                                         /* Deforming a derived mesh, read the vertex locations
1559                                          * out of the mesh and deform them. Once done with this
1560                                          * run of deformers verts will be written back.
1561                                          */
1562                                         numVerts = dm->getNumVerts(dm);
1563                                         deformedVerts =
1564                                                 MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
1565                                         dm->getVertCos(dm, deformedVerts);
1566                                 } else {
1567                                         deformedVerts = editbmesh_get_vertex_cos(em, &numVerts);
1568                                 }
1569                         }
1570
1571                         if (mti->deformVertsEM)
1572                                 mti->deformVertsEM(md, ob, em, dm, deformedVerts, numVerts);
1573                         else mti->deformVerts(md, ob, dm, deformedVerts, numVerts, 0, 0);
1574                 } else {
1575                         DerivedMesh *ndm;
1576
1577                         /* apply vertex coordinates or build a DerivedMesh as necessary */
1578                         if(dm) {
1579                                 if(deformedVerts) {
1580                                         DerivedMesh *tdm = CDDM_copy(dm, 0);
1581                                         if(!(cage_r && dm == *cage_r)) dm->release(dm);
1582                                         dm = tdm;
1583
1584                                         CDDM_apply_vert_coords(dm, deformedVerts);
1585                                         CDDM_calc_normals(dm);
1586                                 } else if(cage_r && dm == *cage_r) {
1587                                         /* dm may be changed by this modifier, so we need to copy it
1588                                          */
1589                                         dm = CDDM_copy(dm, 0);
1590                                 }
1591
1592                         } else {
1593                                 dm = CDDM_from_BMEditMesh(em, ob->data, 0);
1594
1595                                 if(deformedVerts) {
1596                                         CDDM_apply_vert_coords(dm, deformedVerts);
1597                                         CDDM_calc_normals(dm);
1598                                 }
1599                         }
1600
1601                         /* create an orco derivedmesh in parallel */
1602                         mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
1603                         if(mask & CD_MASK_ORCO) {
1604                                 if(!orcodm)
1605                                         orcodm= create_orco_dm(ob, ob->data, em, CD_ORCO);
1606
1607                                 mask &= ~CD_MASK_ORCO;
1608                                 DM_set_only_copy(orcodm, mask | CD_MASK_ORIGINDEX);
1609
1610                                 if (mti->applyModifierEM)
1611                                         ndm = mti->applyModifierEM(md, ob, em, orcodm);
1612                                 else
1613                                         ndm = mti->applyModifier(md, ob, orcodm, 0, 0);
1614
1615                                 if(ndm) {
1616                                         /* if the modifier returned a new dm, release the old one */
1617                                         if(orcodm && orcodm != ndm) orcodm->release(orcodm);
1618                                         orcodm = ndm;
1619                                 }
1620                         }
1621
1622                         /* set the DerivedMesh to only copy needed data */
1623                         mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link); /* CD_MASK_ORCO may have been cleared above */
1624
1625                         DM_set_only_copy(dm, mask | CD_MASK_ORIGINDEX);
1626
1627                         if(mask & CD_MASK_ORIGSPACE)
1628                                 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
1629                                         DM_add_tessface_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
1630                         
1631                         if (mti->applyModifierEM)
1632                                 ndm = mti->applyModifierEM(md, ob, em, dm);
1633                         else
1634                                 ndm = mti->applyModifier(md, ob, dm, 0, 0);
1635
1636                         if (ndm) {
1637                                 if(dm && dm != ndm)
1638                                         dm->release(dm);
1639
1640                                 dm = ndm;
1641
1642                                 if (deformedVerts) {
1643                                         MEM_freeN(deformedVerts);
1644                                         deformedVerts = NULL;
1645                                 }
1646                         }
1647                 }
1648
1649                 if(cage_r && i == cageIndex) {
1650                         if(dm && deformedVerts) {
1651                                 *cage_r = CDDM_copy(dm, 0);
1652                                 CDDM_apply_vert_coords(*cage_r, deformedVerts);
1653                         } else if(dm) {
1654                                 *cage_r = dm;
1655                         } else {
1656                                 *cage_r =
1657                                         getEditDerivedBMesh(em, ob,
1658                                                 deformedVerts ? MEM_dupallocN(deformedVerts) : NULL);
1659                         }
1660                 }
1661         }
1662
1663         BLI_linklist_free(datamasks, NULL);
1664
1665         /* Yay, we are done. If we have a DerivedMesh and deformed vertices need
1666          * to apply these back onto the DerivedMesh. If we have no DerivedMesh
1667          * then we need to build one.
1668          */
1669         if(dm && deformedVerts) {
1670                 finaldm = CDDM_copy(dm, 0);
1671
1672                 if(!(cage_r && dm == *cage_r)) dm->release(dm);
1673
1674                 CDDM_apply_vert_coords(*final_r, deformedVerts);
1675         } else if (dm) {
1676                 finaldm = dm;
1677         } else if (!deformedVerts && cage_r && *cage_r) {
1678                 finaldm = *cage_r;
1679         } else {
1680                 finaldm = getEditDerivedBMesh(em, ob, deformedVerts);
1681                 deformedVerts = NULL;
1682         }
1683
1684         finaldm->calcNormals(finaldm);
1685
1686         *final_r = finaldm;
1687
1688         /* add an orco layer if needed */
1689         if(dataMask & CD_MASK_ORCO)
1690                 add_orco_dm(ob, em, *final_r, orcodm, CD_ORCO);
1691
1692         if(orcodm)
1693                 orcodm->release(orcodm);
1694
1695         if(deformedVerts)
1696                 MEM_freeN(deformedVerts);
1697 }
1698
1699 static void clear_mesh_caches(Object *ob)
1700 {
1701         Mesh *me= ob->data;
1702
1703                 /* also serves as signal to remake texspace */
1704         if (ob->bb) {
1705                 MEM_freeN(ob->bb);
1706                 ob->bb = NULL;
1707         }
1708         if (me->bb) {
1709                 MEM_freeN(me->bb);
1710                 me->bb = NULL;
1711         }
1712
1713         freedisplist(&ob->disp);
1714
1715         if (ob->derivedFinal) {
1716                 ob->derivedFinal->needsFree = 1;
1717                 ob->derivedFinal->release(ob->derivedFinal);
1718                 ob->derivedFinal= NULL;
1719         }
1720         if (ob->derivedDeform) {
1721                 ob->derivedDeform->needsFree = 1;
1722                 ob->derivedDeform->release(ob->derivedDeform);
1723                 ob->derivedDeform= NULL;
1724         }
1725
1726         if(ob->sculpt) {
1727                 object_sculpt_modifiers_changed(ob);
1728         }
1729 }
1730
1731 static void mesh_build_data(Scene *scene, Object *ob, CustomDataMask dataMask,
1732         int build_shapekey_layers)
1733 {
1734         Object *obact = scene->basact?scene->basact->object:NULL;
1735         int editing = paint_facesel_test(ob);
1736         /* weight paint and face select need original indices because of selection buffer drawing */
1737         int needMapping = (ob==obact) && (editing || (ob->mode & (OB_MODE_WEIGHT_PAINT|OB_MODE_VERTEX_PAINT|OB_MODE_TEXTURE_PAINT)));
1738
1739         clear_mesh_caches(ob);
1740
1741         mesh_calc_modifiers(scene, ob, NULL, &ob->derivedDeform,
1742                                                 &ob->derivedFinal, 0, 1,
1743                                                 needMapping, dataMask, -1, 1, build_shapekey_layers);
1744
1745         DM_set_object_boundbox (ob, ob->derivedFinal);
1746
1747         ob->derivedFinal->needsFree = 0;
1748         ob->derivedDeform->needsFree = 0;
1749         ob->lastDataMask = dataMask;
1750 }
1751
1752 static void editbmesh_build_data(Scene *scene, Object *obedit, BMEditMesh *em, CustomDataMask dataMask)
1753 {
1754         clear_mesh_caches(obedit);
1755
1756         if (em->derivedFinal) {
1757                 if (em->derivedFinal!=em->derivedCage) {
1758                         em->derivedFinal->needsFree = 1;
1759                         em->derivedFinal->release(em->derivedFinal);
1760                 }
1761                 em->derivedFinal = NULL;
1762         }
1763         if (em->derivedCage) {
1764                 em->derivedCage->needsFree = 1;
1765                 em->derivedCage->release(em->derivedCage);
1766                 em->derivedCage = NULL;
1767         }
1768
1769         editbmesh_calc_modifiers(scene, obedit, em, &em->derivedCage, &em->derivedFinal, dataMask);
1770         DM_set_object_boundbox (obedit, em->derivedFinal);
1771
1772         em->lastDataMask = dataMask;
1773         em->derivedFinal->needsFree = 0;
1774         em->derivedCage->needsFree = 0;
1775 }
1776
1777 void makeDerivedMesh(Scene *scene, Object *ob, BMEditMesh *em,
1778         CustomDataMask dataMask, int build_shapekey_layers)
1779 {
1780         if (em) {
1781                 editbmesh_build_data(scene, ob, em, dataMask);
1782         } else {
1783                 mesh_build_data(scene, ob, dataMask, build_shapekey_layers);
1784         }
1785 }
1786
1787 /***/
1788
1789 DerivedMesh *mesh_get_derived_final(Scene *scene, Object *ob, CustomDataMask dataMask)
1790 {
1791         /* if there's no derived mesh or the last data mask used doesn't include
1792          * the data we need, rebuild the derived mesh
1793          */
1794         if(!ob->derivedFinal || (dataMask & ob->lastDataMask) != dataMask)
1795                 mesh_build_data(scene, ob, dataMask, 0);
1796
1797         return ob->derivedFinal;
1798 }
1799
1800 DerivedMesh *mesh_get_derived_deform(Scene *scene, Object *ob, CustomDataMask dataMask)
1801 {
1802         /* if there's no derived mesh or the last data mask used doesn't include
1803          * the data we need, rebuild the derived mesh
1804          */
1805         if(!ob->derivedDeform || (dataMask & ob->lastDataMask) != dataMask)
1806                 mesh_build_data(scene, ob, dataMask, 0);
1807
1808         return ob->derivedDeform;
1809 }
1810
1811 DerivedMesh *mesh_create_derived_render(Scene *scene, Object *ob, CustomDataMask dataMask)
1812 {
1813         DerivedMesh *final;
1814         
1815         mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, -1, 0, 0);
1816
1817         return final;
1818 }
1819
1820 DerivedMesh *mesh_create_derived_index_render(Scene *scene, Object *ob, CustomDataMask dataMask, int index)
1821 {
1822         DerivedMesh *final;
1823         
1824         mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, index, 0, 0);
1825
1826         return final;
1827 }
1828
1829 DerivedMesh *mesh_create_derived_view(Scene *scene, Object *ob, CustomDataMask dataMask)
1830 {
1831         DerivedMesh *final;
1832
1833         mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 0, 1, 0, dataMask, -1, 0, 0);
1834
1835         return final;
1836 }
1837
1838 DerivedMesh *mesh_create_derived_no_deform(Scene *scene, Object *ob, float (*vertCos)[3],
1839                                                                                    CustomDataMask dataMask)
1840 {
1841         DerivedMesh *final;
1842         
1843         mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, 0, 0, dataMask, -1, 0, 0);
1844
1845         return final;
1846 }
1847
1848 DerivedMesh *mesh_create_derived_no_virtual(Scene *scene, Object *ob, float (*vertCos)[3],
1849                                                                                         CustomDataMask dataMask)
1850 {
1851         DerivedMesh *final;
1852         
1853         mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 0, dataMask, -1, 0, 0);
1854
1855         return final;
1856 }
1857
1858 DerivedMesh *mesh_create_derived_physics(Scene *scene, Object *ob, float (*vertCos)[3],
1859                                                                                         CustomDataMask dataMask)
1860 {
1861         DerivedMesh *final;
1862         
1863         mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 1, dataMask, -1, 0, 0);
1864
1865         return final;
1866 }
1867
1868 DerivedMesh *mesh_create_derived_no_deform_render(Scene *scene, Object *ob,
1869                                                                                                   float (*vertCos)[3],
1870                                                                                                   CustomDataMask dataMask)
1871 {
1872         DerivedMesh *final;
1873
1874         mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 1, 0, 0, dataMask, -1, 0, 0);
1875
1876         return final;
1877 }
1878
1879 /***/
1880
1881 DerivedMesh *editbmesh_get_derived_cage_and_final(Scene *scene, Object *obedit, BMEditMesh *em, DerivedMesh **final_r,
1882                                                                                                  CustomDataMask dataMask)
1883 {
1884         /* if there's no derived mesh or the last data mask used doesn't include
1885          * the data we need, rebuild the derived mesh
1886          */
1887         if(!em->derivedCage ||
1888            (em->lastDataMask & dataMask) != dataMask)
1889                 editbmesh_build_data(scene, obedit, em, dataMask);
1890
1891         *final_r = em->derivedFinal;
1892         return em->derivedCage;
1893 }
1894
1895 DerivedMesh *editbmesh_get_derived_cage(Scene *scene, Object *obedit, BMEditMesh *em, CustomDataMask dataMask)
1896 {
1897         /* if there's no derived mesh or the last data mask used doesn't include
1898          * the data we need, rebuild the derived mesh
1899          */
1900         if(!em->derivedCage ||
1901            (em->lastDataMask & dataMask) != dataMask)
1902                 editbmesh_build_data(scene, obedit, em, dataMask);
1903
1904         return em->derivedCage;
1905 }
1906
1907 DerivedMesh *editbmesh_get_derived_base(Object *obedit, BMEditMesh *em)
1908 {
1909         return getEditDerivedBMesh(em, obedit, NULL);
1910 }
1911
1912
1913 /* ********* For those who don't grasp derived stuff! (ton) :) *************** */
1914
1915 static void make_vertexcosnos__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
1916 {
1917         float *vec = userData;
1918         
1919         vec+= 6*index;
1920
1921         /* check if we've been here before (normal should not be 0) */
1922         if(vec[3] || vec[4] || vec[5]) return;
1923
1924         copy_v3_v3(vec, co);
1925         vec+= 3;
1926         if(no_f) {
1927                 copy_v3_v3(vec, no_f);
1928         }
1929         else {
1930                 normal_short_to_float_v3(vec, no_s);
1931         }
1932 }
1933
1934 /* always returns original amount me->totvert of vertices and normals, but fully deformed and subsurfered */
1935 /* this is needed for all code using vertexgroups (no subsurf support) */
1936 /* it stores the normals as floats, but they can still be scaled as shorts (32767 = unit) */
1937 /* in use now by vertex/weight paint and particle generating */
1938
1939 float *mesh_get_mapped_verts_nors(Scene *scene, Object *ob)
1940 {
1941         Mesh *me= ob->data;
1942         DerivedMesh *dm;
1943         float *vertexcosnos;
1944         
1945         /* lets prevent crashing... */
1946         if(ob->type!=OB_MESH || me->totvert==0)
1947                 return NULL;
1948         
1949         dm= mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH|CD_MASK_ORIGINDEX);
1950         vertexcosnos= MEM_callocN(6*sizeof(float)*me->totvert, "vertexcosnos map");
1951         
1952         if(dm->foreachMappedVert) {
1953                 dm->foreachMappedVert(dm, make_vertexcosnos__mapFunc, vertexcosnos);
1954         }
1955         else {
1956                 float *fp= vertexcosnos;
1957                 int a;
1958                 
1959                 for(a=0; a< me->totvert; a++, fp+=6) {
1960                         dm->getVertCo(dm, a, fp);
1961                         dm->getVertNo(dm, a, fp+3);
1962                 }
1963         }
1964         
1965         dm->release(dm);
1966         return vertexcosnos;
1967 }
1968
1969 /* ******************* GLSL ******************** */
1970
1971 typedef struct
1972 {
1973         float * precomputedFaceNormals;
1974         MTFace * mtface;        // texture coordinates
1975         MFace * mface;          // indices
1976         MVert * mvert;          // vertices & normals
1977         float (*orco)[3];
1978         float (*tangent)[4];    // destination
1979         int numFaces;
1980
1981 } SGLSLMeshToTangent;
1982
1983 // interface
1984 #include "mikktspace.h"
1985
1986 static int GetNumFaces(const SMikkTSpaceContext * pContext)
1987 {
1988         SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1989         return pMesh->numFaces;
1990 }
1991
1992 static int GetNumVertsOfFace(const SMikkTSpaceContext * pContext, const int face_num)
1993 {
1994         SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
1995         return pMesh->mface[face_num].v4!=0 ? 4 : 3;
1996 }
1997
1998 static void GetPosition(const SMikkTSpaceContext * pContext, float fPos[], const int face_num, const int vert_index)
1999 {
2000         //assert(vert_index>=0 && vert_index<4);
2001         SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
2002         const float *co= pMesh->mvert[(&pMesh->mface[face_num].v1)[vert_index]].co;
2003         copy_v3_v3(fPos, co);
2004 }
2005
2006 static void GetTextureCoordinate(const SMikkTSpaceContext * pContext, float fUV[], const int face_num, const int vert_index)
2007 {
2008         //assert(vert_index>=0 && vert_index<4);
2009         SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
2010
2011         if(pMesh->mtface!=NULL) {
2012                 float * uv = pMesh->mtface[face_num].uv[vert_index];
2013                 fUV[0]=uv[0]; fUV[1]=uv[1];
2014         }
2015         else {
2016                 const float *orco= pMesh->orco[(&pMesh->mface[face_num].v1)[vert_index]];
2017                 map_to_sphere( &fUV[0], &fUV[1], orco[0], orco[1], orco[2]);
2018         }
2019 }
2020
2021 static void GetNormal(const SMikkTSpaceContext * pContext, float fNorm[], const int face_num, const int vert_index)
2022 {
2023         //assert(vert_index>=0 && vert_index<4);
2024         SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
2025
2026         const int smoothnormal = (pMesh->mface[face_num].flag & ME_SMOOTH);
2027         if(!smoothnormal) {     // flat
2028                 if(pMesh->precomputedFaceNormals) {
2029                         copy_v3_v3(fNorm, &pMesh->precomputedFaceNormals[3*face_num]);
2030                 }
2031                 else {
2032                         MFace *mf= &pMesh->mface[face_num];
2033                         float *p0= pMesh->mvert[mf->v1].co;
2034                         float *p1= pMesh->mvert[mf->v2].co;
2035                         float *p2= pMesh->mvert[mf->v3].co;
2036
2037                         if(mf->v4) {
2038                                 float *p3 = pMesh->mvert[mf->v4].co;
2039                                 normal_quad_v3(fNorm, p0, p1, p2, p3);
2040                         }
2041                         else {
2042                                 normal_tri_v3(fNorm, p0, p1, p2);
2043                         }
2044                 }
2045         }
2046         else {
2047                 const short *no= pMesh->mvert[(&pMesh->mface[face_num].v1)[vert_index]].no;
2048                 normal_short_to_float_v3(fNorm, no);
2049         }
2050 }
2051 static void SetTSpace(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int face_num, const int iVert)
2052 {
2053         //assert(vert_index>=0 && vert_index<4);
2054         SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
2055         float * pRes = pMesh->tangent[4*face_num+iVert];
2056         copy_v3_v3(pRes, fvTangent);
2057         pRes[3]=fSign;
2058 }
2059
2060
2061 void DM_add_tangent_layer(DerivedMesh *dm)
2062 {
2063         /* mesh vars */
2064         MTFace *mtface, *tf;
2065         MFace *mface, *mf;
2066         MVert *mvert, *v1, *v2, *v3, *v4;
2067         MemArena *arena= NULL;
2068         VertexTangent **vtangents= NULL;
2069         float (*orco)[3]= NULL, (*tangent)[4];
2070         float *uv1, *uv2, *uv3, *uv4, *vtang;
2071         float fno[3], tang[3], uv[4][2];
2072         int i, j, len, mf_vi[4], totvert, totface, iCalcNewMethod;
2073         float *nors;
2074
2075         if(CustomData_get_layer_index(&dm->faceData, CD_TANGENT) != -1)
2076                 return;
2077
2078         nors = dm->getTessFaceDataArray(dm, CD_NORMAL);
2079
2080         /* check we have all the needed layers */
2081         totvert= dm->getNumVerts(dm);
2082         totface= dm->getNumTessFaces(dm);
2083
2084         mvert= dm->getVertArray(dm);
2085         mface= dm->getTessFaceArray(dm);
2086         mtface= dm->getTessFaceDataArray(dm, CD_MTFACE);
2087
2088         if(!mtface) {
2089                 orco= dm->getVertDataArray(dm, CD_ORCO);
2090                 if(!orco)
2091                         return;
2092         }
2093         
2094         /* create tangent layer */
2095         DM_add_tessface_layer(dm, CD_TANGENT, CD_CALLOC, NULL);
2096         tangent= DM_get_tessface_data_layer(dm, CD_TANGENT);
2097         
2098         /* allocate some space */
2099         arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "tangent layer arena");
2100         BLI_memarena_use_calloc(arena);
2101         vtangents= MEM_callocN(sizeof(VertexTangent*)*totvert, "VertexTangent");
2102
2103         // new computation method
2104         iCalcNewMethod = 1;
2105         if(iCalcNewMethod != 0) {
2106                 SGLSLMeshToTangent mesh2tangent= {0};
2107                 SMikkTSpaceContext sContext= {0};
2108                 SMikkTSpaceInterface sInterface= {0};
2109
2110                 mesh2tangent.precomputedFaceNormals = nors;
2111                 mesh2tangent.mtface = mtface;
2112                 mesh2tangent.mface = mface;
2113                 mesh2tangent.mvert = mvert;
2114                 mesh2tangent.orco = orco;
2115                 mesh2tangent.tangent = tangent;
2116                 mesh2tangent.numFaces = totface;
2117
2118                 sContext.m_pUserData = &mesh2tangent;
2119                 sContext.m_pInterface = &sInterface;
2120                 sInterface.m_getNumFaces = GetNumFaces;
2121                 sInterface.m_getNumVerticesOfFace = GetNumVertsOfFace;
2122                 sInterface.m_getPosition = GetPosition;
2123                 sInterface.m_getTexCoord = GetTextureCoordinate;
2124                 sInterface.m_getNormal = GetNormal;
2125                 sInterface.m_setTSpaceBasic = SetTSpace;
2126
2127                 // 0 if failed
2128                 iCalcNewMethod = genTangSpaceDefault(&sContext);
2129         }
2130
2131         if(!iCalcNewMethod) {
2132                 /* sum tangents at connected vertices */
2133                 for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++) {
2134                         v1= &mvert[mf->v1];
2135                         v2= &mvert[mf->v2];
2136                         v3= &mvert[mf->v3];
2137
2138                         if (mf->v4) {
2139                                 v4= &mvert[mf->v4];
2140                                 normal_quad_v3( fno,v4->co, v3->co, v2->co, v1->co);
2141                         }
2142                         else {
2143                                 v4= NULL;
2144                                 normal_tri_v3( fno,v3->co, v2->co, v1->co);
2145                         }
2146                 
2147                         if(mtface) {
2148                                 uv1= tf->uv[0];
2149                                 uv2= tf->uv[1];
2150                                 uv3= tf->uv[2];
2151                                 uv4= tf->uv[3];
2152                         }
2153                         else {
2154                                 uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
2155                                 map_to_sphere( &uv[0][0], &uv[0][1],orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2]);
2156                                 map_to_sphere( &uv[1][0], &uv[1][1],orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2]);
2157                                 map_to_sphere( &uv[2][0], &uv[2][1],orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2]);
2158                                 if(v4)
2159                                         map_to_sphere( &uv[3][0], &uv[3][1],orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2]);
2160                         }
2161                 
2162                         tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, fno, tang);
2163                         sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
2164                         sum_or_add_vertex_tangent(arena, &vtangents[mf->v2], tang, uv2);
2165                         sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
2166                 
2167                         if(mf->v4) {
2168                                 v4= &mvert[mf->v4];
2169                         
2170                                 tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, fno, tang);
2171                                 sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
2172                                 sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
2173                                 sum_or_add_vertex_tangent(arena, &vtangents[mf->v4], tang, uv4);
2174                         }
2175                 }
2176         
2177                 /* write tangent to layer */
2178                 for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++, tangent+=4) {
2179                         len= (mf->v4)? 4 : 3; 
2180
2181                         if(mtface == NULL) {
2182                                 map_to_sphere( &uv[0][0], &uv[0][1],orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2]);
2183                                 map_to_sphere( &uv[1][0], &uv[1][1],orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2]);
2184                                 map_to_sphere( &uv[2][0], &uv[2][1],orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2]);
2185                                 if(len==4)
2186                                         map_to_sphere( &uv[3][0], &uv[3][1],orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2]);
2187                         }
2188                 
2189                         mf_vi[0]= mf->v1;
2190                         mf_vi[1]= mf->v2;
2191                         mf_vi[2]= mf->v3;
2192                         mf_vi[3]= mf->v4;
2193                 
2194                         for(j=0; j<len; j++) {
2195                                 vtang= find_vertex_tangent(vtangents[mf_vi[j]], mtface ? tf->uv[j] : uv[j]);
2196                                 normalize_v3_v3(tangent[j], vtang);
2197                                 ((float *) tangent[j])[3]=1.0f;
2198                         }
2199                 }
2200         }
2201         
2202         BLI_memarena_free(arena);
2203         MEM_freeN(vtangents);
2204 }
2205
2206 void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs, DMVertexAttribs *attribs)
2207 {
2208         CustomData *vdata, *fdata, *tfdata = NULL;
2209         int a, b, layer;
2210
2211         /* From the layers requested by the GLSL shader, figure out which ones are
2212          * actually available for this derivedmesh, and retrieve the pointers */
2213
2214         memset(attribs, 0, sizeof(DMVertexAttribs));
2215
2216         vdata = &dm->vertData;
2217         fdata = tfdata = dm->getTessFaceDataLayout(dm);
2218         
2219         /* add a tangent layer if necessary */
2220         for(b = 0; b < gattribs->totlayer; b++)
2221                 if(gattribs->layer[b].type == CD_TANGENT)
2222                         if(CustomData_get_layer_index(fdata, CD_TANGENT) == -1)
2223                                 DM_add_tangent_layer(dm);
2224
2225         for(b = 0; b < gattribs->totlayer; b++) {
2226                 if(gattribs->layer[b].type == CD_MTFACE) {
2227                         /* uv coordinates */
2228                         if(gattribs->layer[b].name[0])
2229                                 layer = CustomData_get_named_layer_index(tfdata, CD_MTFACE,
2230                                         gattribs->layer[b].name);
2231                         else
2232                                 layer = CustomData_get_active_layer_index(tfdata, CD_MTFACE);
2233
2234                         if(layer != -1) {
2235                                 a = attribs->tottface++;
2236
2237                                 attribs->tface[a].array = tfdata->layers[layer].data;
2238                                 attribs->tface[a].emOffset = tfdata->layers[layer].offset;
2239                                 attribs->tface[a].glIndex = gattribs->layer[b].glindex;
2240                         } /*else {
2241                                 int player;
2242                                 CustomData *pdata = dm->getFaceDataLayout(dm);
2243                                 
2244                                 if(gattribs->layer[b].name[0])
2245                                         player = CustomData_get_named_layer_index(pdata, CD_MTEXPOLY,
2246                                                 gattribs->layer[b].name);
2247                                 else
2248                                         player = CustomData_get_active_layer_index(pdata, CD_MTEXPOLY);
2249                                 
2250                                 if (player != -1) {
2251                                         a = attribs->tottface++;
2252         
2253                                         attribs->tface[a].array = NULL;
2254                                         attribs->tface[a].emOffset = pdata->layers[layer].offset;
2255                                         attribs->tface[a].glIndex = gattribs->layer[b].glindex;
2256                                         attribs->tface[a].glTexco = gattribs->layer[b].gltexco;
2257                                         
2258                                 }
2259                         }*/
2260                 }
2261                 else if(gattribs->layer[b].type == CD_MCOL) {
2262                         /* vertex colors */
2263                         if(gattribs->layer[b].name[0])
2264                                 layer = CustomData_get_named_layer_index(tfdata, CD_MCOL,
2265                                         gattribs->layer[b].name);
2266                         else
2267                                 layer = CustomData_get_active_layer_index(tfdata, CD_MCOL);
2268
2269                         if(layer != -1) {
2270                                 a = attribs->totmcol++;
2271
2272                                 attribs->mcol[a].array = tfdata->layers[layer].data;
2273                                 attribs->mcol[a].emOffset = tfdata->layers[layer].offset;
2274                                 attribs->mcol[a].glIndex = gattribs->layer[b].glindex;
2275                         }
2276                 }
2277                 else if(gattribs->layer[b].type == CD_TANGENT) {
2278                         /* tangents */
2279                         layer = CustomData_get_layer_index(fdata, CD_TANGENT);
2280
2281                         if(layer != -1) {
2282                                 attribs->tottang = 1;
2283
2284                                 attribs->tang.array = fdata->layers[layer].data;
2285                                 attribs->tang.emOffset = fdata->layers[layer].offset;
2286                                 attribs->tang.glIndex = gattribs->layer[b].glindex;
2287                         }
2288                 }
2289                 else if(gattribs->layer[b].type == CD_ORCO) {
2290                         /* original coordinates */
2291                         layer = CustomData_get_layer_index(vdata, CD_ORCO);
2292
2293                         if(layer != -1) {
2294                                 attribs->totorco = 1;
2295
2296                                 attribs->orco.array = vdata->layers[layer].data;
2297                                 attribs->orco.emOffset = vdata->layers[layer].offset;
2298                                 attribs->orco.glIndex = gattribs->layer[b].glindex;
2299                                 attribs->orco.glTexco = gattribs->layer[b].gltexco;
2300                         }
2301                 }
2302         }
2303 }
2304
2305 /* Set object's bounding box based on DerivedMesh min/max data */
2306 void DM_set_object_boundbox(Object *ob, DerivedMesh *dm)
2307 {
2308         float min[3], max[3];
2309
2310         INIT_MINMAX(min, max);
2311
2312         dm->getMinMax(dm, min, max);
2313
2314         if(!ob->bb)
2315                 ob->bb= MEM_callocN(sizeof(BoundBox), "DM-BoundBox");
2316
2317         boundbox_set_from_min_max(ob->bb, min, max);
2318 }
2319
2320 /* --- NAVMESH (begin) --- */
2321 #ifdef WITH_GAMEENGINE
2322
2323 BM_INLINE int navmesh_bit(int a, int b)
2324 {
2325         return (a & (1 << b)) >> b;
2326 }
2327
2328 BM_INLINE void navmesh_intToCol(int i, float col[3])
2329 {
2330         int     r = navmesh_bit(i, 0) + navmesh_bit(i, 3) * 2 + 1;
2331         int     g = navmesh_bit(i, 1) + navmesh_bit(i, 4) * 2 + 1;
2332         int     b = navmesh_bit(i, 2) + navmesh_bit(i, 5) * 2 + 1;
2333         col[0] = 1 - r*63.0f/255.0f;
2334         col[1] = 1 - g*63.0f/255.0f;
2335         col[2] = 1 - b*63.0f/255.0f;
2336 }
2337
2338 static void navmesh_drawColored(DerivedMesh *dm)
2339 {
2340         int a, glmode;
2341         MVert *mvert = (MVert *)CustomData_get_layer(&dm->vertData, CD_MVERT);
2342         MFace *mface = (MFace *)CustomData_get_layer(&dm->faceData, CD_MFACE);
2343         int *polygonIdx = (int *)CustomData_get_layer(&dm->faceData, CD_RECAST);
2344         float col[3];
2345
2346         if (!polygonIdx)
2347                 return;
2348
2349         /*
2350         //UI_ThemeColor(TH_WIRE);
2351         glDisable(GL_LIGHTING);
2352         glLineWidth(2.0);
2353         dm->drawEdges(dm, 0, 1);
2354         glLineWidth(1.0);
2355         glEnable(GL_LIGHTING);*/
2356
2357         glDisable(GL_LIGHTING);
2358         /*  if(GPU_buffer_legacy(dm) ) */ { /* TODO - VBO draw code, not high priority - campbell */
2359                 DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" );
2360                 //glShadeModel(GL_SMOOTH);
2361                 glBegin(glmode = GL_QUADS);
2362                 for(a = 0; a < dm->numFaceData; a++, mface++) {
2363                         int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
2364                         int pi = polygonIdx[a];
2365                         if (pi <= 0) {
2366                                 zero_v3(col);
2367                         }
2368                         else {
2369                                 navmesh_intToCol(pi, col);
2370                         }
2371
2372                         if(new_glmode != glmode) {
2373                                 glEnd();
2374                                 glBegin(glmode = new_glmode);
2375                         }
2376                         glColor3fv(col);
2377                         glVertex3fv(mvert[mface->v1].co);
2378                         glVertex3fv(mvert[mface->v2].co);
2379                         glVertex3fv(mvert[mface->v3].co);
2380                         if(mface->v4) {
2381                                 glVertex3fv(mvert[mface->v4].co);
2382                         }
2383                 }
2384                 glEnd();
2385         }
2386         glEnable(GL_LIGHTING);
2387 }
2388
2389 static void navmesh_DM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr))
2390 {
2391         (void) setDrawOptions;
2392
2393         navmesh_drawColored(dm);
2394 }
2395
2396 static void navmesh_DM_drawFacesSolid(DerivedMesh *dm,
2397                                       float (*partial_redraw_planes)[4],
2398                                       int UNUSED(fast), int (*setMaterial)(int, void *attribs))
2399 {
2400         (void) partial_redraw_planes;
2401         (void) setMaterial;
2402
2403         //drawFacesSolid_original(dm, partial_redraw_planes, fast, setMaterial);
2404         navmesh_drawColored(dm);
2405 }
2406
2407 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm)
2408 {
2409         DerivedMesh *result;
2410         int maxFaces = dm->getNumFaces(dm);
2411         int *recastData;
2412         int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0;
2413         float* verts=NULL;
2414         unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL;
2415         int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
2416         int res;
2417
2418         result = CDDM_copy(dm, 0);
2419         if (!CustomData_has_layer(&result->faceData, CD_RECAST)) {
2420                 int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
2421                 if (sourceRecastData) {
2422                         CustomData_add_layer_named(&result->faceData, CD_RECAST, CD_DUPLICATE,
2423                                                    sourceRecastData, maxFaces, "recastData");
2424                 }
2425         }
2426         recastData = (int*)CustomData_get_layer(&result->faceData, CD_RECAST);
2427
2428         /* note: This is not good design! - really should not be doing this */
2429         result->drawFacesTex =  navmesh_DM_drawFacesTex;
2430         result->drawFacesSolid = navmesh_DM_drawFacesSolid;
2431
2432
2433         /* process mesh */
2434         res  = buildNavMeshDataByDerivedMesh(dm, &vertsPerPoly, &nverts, &verts, &ndtris, &dtris,
2435                                              &npolys, &dmeshes, &polys, &dtrisToPolysMap, &dtrisToTrisMap,
2436                                              &trisToFacesMap);
2437         if (res) {
2438                 size_t polyIdx;
2439
2440                 /* invalidate concave polygon */
2441                 for (polyIdx=0; polyIdx<(size_t)npolys; polyIdx++) {
2442                         unsigned short* poly = &polys[polyIdx*2*vertsPerPoly];
2443                         if (!polyIsConvex(poly, vertsPerPoly, verts)) {
2444                                 /* set negative polygon idx to all faces */
2445                                 unsigned short *dmesh = &dmeshes[4*polyIdx];
2446                                 unsigned short tbase = dmesh[2];
2447                                 unsigned short tnum = dmesh[3];
2448                                 unsigned short ti;
2449
2450                                 for (ti=0; ti<tnum; ti++) {
2451                                         unsigned short triidx = dtrisToTrisMap[tbase+ti];
2452                                         unsigned short faceidx = trisToFacesMap[triidx];
2453                                         if (recastData[faceidx] > 0) {
2454                                                 recastData[faceidx] = -recastData[faceidx];
2455                                         }
2456                                 }
2457                         }
2458                 }
2459         }
2460         else {
2461                 printf("Error during creation polygon infos\n");
2462         }
2463
2464         /* clean up */
2465         if (verts!=NULL)
2466                 MEM_freeN(verts);
2467         if (dtris!=NULL)
2468                 MEM_freeN(dtris);
2469         if (dmeshes!=NULL)
2470                 MEM_freeN(dmeshes);
2471         if (polys!=NULL)
2472                 MEM_freeN(polys);
2473         if (dtrisToPolysMap!=NULL)
2474                 MEM_freeN(dtrisToPolysMap);
2475         if (dtrisToTrisMap!=NULL)
2476                 MEM_freeN(dtrisToTrisMap);
2477         if (trisToFacesMap!=NULL)
2478                 MEM_freeN(trisToFacesMap);
2479
2480         return result;
2481 }
2482
2483 #endif /* WITH_GAMEENGINE */
2484
2485 /* --- NAVMESH (end) --- */