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