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