svn merge -r 16060:16222 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender-staging.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <string.h>
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "PIL_time.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_effect_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_object_fluidsim.h" // N_T
48 #include "DNA_scene_types.h" // N_T
49 #include "DNA_texture_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_particle_types.h"
54
55 #include "BLI_arithb.h"
56 #include "BLI_blenlib.h"
57 #include "BLI_edgehash.h"
58 #include "BLI_editVert.h"
59 #include "BLI_linklist.h"
60 #include "BLI_memarena.h"
61
62 #include "BKE_cdderivedmesh.h"
63 #include "BKE_customdata.h"
64 #include "BKE_DerivedMesh.h"
65 #include "BKE_deform.h"
66 #include "BKE_displist.h"
67 #include "BKE_effect.h"
68 #include "BKE_fluidsim.h"
69 #include "BKE_global.h"
70 #include "BKE_key.h"
71 #include "BKE_material.h"
72 #include "BKE_modifier.h"
73 #include "BKE_mesh.h"
74 #include "BKE_multires.h"
75 #include "BKE_object.h"
76 #include "BKE_subsurf.h"
77 #include "BKE_texture.h"
78 #include "BKE_utildefines.h"
79 #include "BKE_particle.h"
80
81 #include "BLO_sys_types.h" // for intptr_t support
82
83 #ifdef WITH_VERSE
84 #include "BKE_verse.h"
85 #endif
86
87 #include "BIF_gl.h"
88 #include "BIF_glutil.h"
89
90 ///////////////////////////////////
91 ///////////////////////////////////
92
93 MVert *dm_getVertArray(DerivedMesh *dm)
94 {
95         MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
96
97         if (!mvert) {
98                 mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL,
99                         dm->getNumVerts(dm));
100                 CustomData_set_layer_flag(&dm->vertData, CD_MVERT, CD_FLAG_TEMPORARY);
101                 dm->copyVertArray(dm, mvert);
102         }
103
104         return mvert;
105 }
106
107 MEdge *dm_getEdgeArray(DerivedMesh *dm)
108 {
109         MEdge *medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
110
111         if (!medge) {
112                 medge = CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL,
113                         dm->getNumEdges(dm));
114                 CustomData_set_layer_flag(&dm->edgeData, CD_MEDGE, CD_FLAG_TEMPORARY);
115                 dm->copyEdgeArray(dm, medge);
116         }
117
118         return medge;
119 }
120
121 MFace *dm_getFaceArray(DerivedMesh *dm)
122 {
123         MFace *mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
124
125         if (!mface) {
126                 mface = CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL,
127                         dm->getNumFaces(dm));
128                 CustomData_set_layer_flag(&dm->faceData, CD_MFACE, CD_FLAG_TEMPORARY);
129                 dm->copyFaceArray(dm, mface);
130         }
131
132         return mface;
133 }
134
135 MVert *dm_dupVertArray(DerivedMesh *dm)
136 {
137         MVert *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumVerts(dm),
138                                  "dm_dupVertArray tmp");
139
140         if(tmp) dm->copyVertArray(dm, tmp);
141
142         return tmp;
143 }
144
145 MEdge *dm_dupEdgeArray(DerivedMesh *dm)
146 {
147         MEdge *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumEdges(dm),
148                                  "dm_dupEdgeArray tmp");
149
150         if(tmp) dm->copyEdgeArray(dm, tmp);
151
152         return tmp;
153 }
154
155 MFace *dm_dupFaceArray(DerivedMesh *dm)
156 {
157         MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumFaces(dm),
158                                  "dm_dupFaceArray tmp");
159
160         if(tmp) dm->copyFaceArray(dm, tmp);
161
162         return tmp;
163 }
164
165 void DM_init_funcs(DerivedMesh *dm)
166 {
167         /* default function implementations */
168         dm->getVertArray = dm_getVertArray;
169         dm->getEdgeArray = dm_getEdgeArray;
170         dm->getFaceArray = dm_getFaceArray;
171         dm->dupVertArray = dm_dupVertArray;
172         dm->dupEdgeArray = dm_dupEdgeArray;
173         dm->dupFaceArray = dm_dupFaceArray;
174
175         dm->getVertData = DM_get_vert_data;
176         dm->getEdgeData = DM_get_edge_data;
177         dm->getFaceData = DM_get_face_data;
178         dm->getVertDataArray = DM_get_vert_data_layer;
179         dm->getEdgeDataArray = DM_get_edge_data_layer;
180         dm->getFaceDataArray = DM_get_face_data_layer;
181 }
182
183 void DM_init(DerivedMesh *dm,
184              int numVerts, int numEdges, int numFaces)
185 {
186         dm->numVertData = numVerts;
187         dm->numEdgeData = numEdges;
188         dm->numFaceData = numFaces;
189
190         DM_init_funcs(dm);
191         
192         dm->needsFree = 1;
193 }
194
195 void DM_from_template(DerivedMesh *dm, DerivedMesh *source,
196                       int numVerts, int numEdges, int numFaces)
197 {
198         CustomData_copy(&source->vertData, &dm->vertData, CD_MASK_DERIVEDMESH,
199                         CD_CALLOC, numVerts);
200         CustomData_copy(&source->edgeData, &dm->edgeData, CD_MASK_DERIVEDMESH,
201                         CD_CALLOC, numEdges);
202         CustomData_copy(&source->faceData, &dm->faceData, CD_MASK_DERIVEDMESH,
203                         CD_CALLOC, numFaces);
204
205         dm->numVertData = numVerts;
206         dm->numEdgeData = numEdges;
207         dm->numFaceData = numFaces;
208
209         DM_init_funcs(dm);
210
211         dm->needsFree = 1;
212 }
213
214 int DM_release(DerivedMesh *dm)
215 {
216         if (dm->needsFree) {
217                 CustomData_free(&dm->vertData, dm->numVertData);
218                 CustomData_free(&dm->edgeData, dm->numEdgeData);
219                 CustomData_free(&dm->faceData, dm->numFaceData);
220
221                 return 1;
222         }
223         else {
224                 CustomData_free_temporary(&dm->vertData, dm->numVertData);
225                 CustomData_free_temporary(&dm->edgeData, dm->numEdgeData);
226                 CustomData_free_temporary(&dm->faceData, dm->numFaceData);
227
228                 return 0;
229         }
230 }
231
232 void DM_to_mesh(DerivedMesh *dm, Mesh *me)
233 {
234         /* dm might depend on me, so we need to do everything with a local copy */
235         Mesh tmp = *me;
236         int totvert, totedge, totface;
237
238         memset(&tmp.vdata, 0, sizeof(tmp.vdata));
239         memset(&tmp.edata, 0, sizeof(tmp.edata));
240         memset(&tmp.fdata, 0, sizeof(tmp.fdata));
241
242         totvert = tmp.totvert = dm->getNumVerts(dm);
243         totedge = tmp.totedge = dm->getNumEdges(dm);
244         totface = tmp.totface = dm->getNumFaces(dm);
245
246         CustomData_copy(&dm->vertData, &tmp.vdata, CD_MASK_MESH, CD_DUPLICATE, totvert);
247         CustomData_copy(&dm->edgeData, &tmp.edata, CD_MASK_MESH, CD_DUPLICATE, totedge);
248         CustomData_copy(&dm->faceData, &tmp.fdata, CD_MASK_MESH, CD_DUPLICATE, totface);
249
250         /* not all DerivedMeshes store their verts/edges/faces in CustomData, so
251            we set them here in case they are missing */
252         if(!CustomData_has_layer(&tmp.vdata, CD_MVERT))
253                 CustomData_add_layer(&tmp.vdata, CD_MVERT, CD_ASSIGN, dm->dupVertArray(dm), totvert);
254         if(!CustomData_has_layer(&tmp.edata, CD_MEDGE))
255                 CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, dm->dupEdgeArray(dm), totedge);
256         if(!CustomData_has_layer(&tmp.fdata, CD_MFACE))
257                 CustomData_add_layer(&tmp.fdata, CD_MFACE, CD_ASSIGN, dm->dupFaceArray(dm), totface);
258
259         mesh_update_customdata_pointers(&tmp);
260
261         CustomData_free(&me->vdata, me->totvert);
262         CustomData_free(&me->edata, me->totedge);
263         CustomData_free(&me->fdata, me->totface);
264
265         /* if the number of verts has changed, remove invalid data */
266         if(tmp.totvert != me->totvert) {
267                 if(me->key) me->key->id.us--;
268                 me->key = NULL;
269         }
270
271         *me = tmp;
272 }
273
274 void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask)
275 {
276         CustomData_set_only_copy(&dm->vertData, mask);
277         CustomData_set_only_copy(&dm->edgeData, mask);
278         CustomData_set_only_copy(&dm->faceData, mask);
279 }
280
281 void DM_add_vert_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
282 {
283         CustomData_add_layer(&dm->vertData, type, alloctype, layer, dm->numVertData);
284 }
285
286 void DM_add_edge_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
287 {
288         CustomData_add_layer(&dm->edgeData, type, alloctype, layer, dm->numEdgeData);
289 }
290
291 void DM_add_face_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
292 {
293         CustomData_add_layer(&dm->faceData, type, alloctype, layer, dm->numFaceData);
294 }
295
296 void *DM_get_vert_data(DerivedMesh *dm, int index, int type)
297 {
298         return CustomData_get(&dm->vertData, index, type);
299 }
300
301 void *DM_get_edge_data(DerivedMesh *dm, int index, int type)
302 {
303         return CustomData_get(&dm->edgeData, index, type);
304 }
305
306 void *DM_get_face_data(DerivedMesh *dm, int index, int type)
307 {
308         return CustomData_get(&dm->faceData, index, type);
309 }
310
311 void *DM_get_vert_data_layer(DerivedMesh *dm, int type)
312 {
313         return CustomData_get_layer(&dm->vertData, type);
314 }
315
316 void *DM_get_edge_data_layer(DerivedMesh *dm, int type)
317 {
318         return CustomData_get_layer(&dm->edgeData, type);
319 }
320
321 void *DM_get_face_data_layer(DerivedMesh *dm, int type)
322 {
323         return CustomData_get_layer(&dm->faceData, type);
324 }
325
326 void DM_set_vert_data(DerivedMesh *dm, int index, int type, void *data)
327 {
328         CustomData_set(&dm->vertData, index, type, data);
329 }
330
331 void DM_set_edge_data(DerivedMesh *dm, int index, int type, void *data)
332 {
333         CustomData_set(&dm->edgeData, index, type, data);
334 }
335
336 void DM_set_face_data(DerivedMesh *dm, int index, int type, void *data)
337 {
338         CustomData_set(&dm->faceData, index, type, data);
339 }
340
341 void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest,
342                        int source_index, int dest_index, int count)
343 {
344         CustomData_copy_data(&source->vertData, &dest->vertData,
345                              source_index, dest_index, count);
346 }
347
348 void DM_copy_edge_data(DerivedMesh *source, DerivedMesh *dest,
349                        int source_index, int dest_index, int count)
350 {
351         CustomData_copy_data(&source->edgeData, &dest->edgeData,
352                              source_index, dest_index, count);
353 }
354
355 void DM_copy_face_data(DerivedMesh *source, DerivedMesh *dest,
356                        int source_index, int dest_index, int count)
357 {
358         CustomData_copy_data(&source->faceData, &dest->faceData,
359                              source_index, dest_index, count);
360 }
361
362 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count)
363 {
364         CustomData_free_elem(&dm->vertData, index, count);
365 }
366
367 void DM_free_edge_data(struct DerivedMesh *dm, int index, int count)
368 {
369         CustomData_free_elem(&dm->edgeData, index, count);
370 }
371
372 void DM_free_face_data(struct DerivedMesh *dm, int index, int count)
373 {
374         CustomData_free_elem(&dm->faceData, index, count);
375 }
376
377 void DM_interp_vert_data(DerivedMesh *source, DerivedMesh *dest,
378                          int *src_indices, float *weights,
379                          int count, int dest_index)
380 {
381         CustomData_interp(&source->vertData, &dest->vertData, src_indices,
382                           weights, NULL, count, dest_index);
383 }
384
385 void DM_interp_edge_data(DerivedMesh *source, DerivedMesh *dest,
386                          int *src_indices,
387                          float *weights, EdgeVertWeight *vert_weights,
388                          int count, int dest_index)
389 {
390         CustomData_interp(&source->edgeData, &dest->edgeData, src_indices,
391                           weights, (float*)vert_weights, count, dest_index);
392 }
393
394 void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
395                          int *src_indices,
396                          float *weights, FaceVertWeight *vert_weights,
397                          int count, int dest_index)
398 {
399         CustomData_interp(&source->faceData, &dest->faceData, src_indices,
400                           weights, (float*)vert_weights, count, dest_index);
401 }
402
403 void DM_swap_face_data(DerivedMesh *dm, int index, int *corner_indices)
404 {
405         CustomData_swap(&dm->faceData, index, corner_indices);
406 }
407
408 static DerivedMesh *getMeshDerivedMesh(Mesh *me, Object *ob, float (*vertCos)[3])
409 {
410         DerivedMesh *dm = CDDM_from_mesh(me, ob);
411         
412         if(!dm)
413                 return NULL;
414         
415         if (vertCos)
416                 CDDM_apply_vert_coords(dm, vertCos);
417
418         CDDM_calc_normals(dm);
419
420         return dm;
421 }
422
423 ///
424
425 typedef struct {
426         DerivedMesh dm;
427
428         EditMesh *em;
429         float (*vertexCos)[3];
430         float (*vertexNos)[3];
431         float (*faceNos)[3];
432 } EditMeshDerivedMesh;
433
434 static void emDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData)
435 {
436         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
437         EditVert *eve;
438         int i;
439
440         for (i=0,eve= emdm->em->verts.first; eve; i++,eve=eve->next) {
441                 if (emdm->vertexCos) {
442                         func(userData, i, emdm->vertexCos[i], emdm->vertexNos[i], NULL);
443                 } else {
444                         func(userData, i, eve->co, eve->no, NULL);
445                 }
446         }
447 }
448 static void emDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData)
449 {
450         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
451         EditEdge *eed;
452         int i;
453
454         if (emdm->vertexCos) {
455                 EditVert *eve;
456
457                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
458                         eve->tmp.l = (intptr_t) i++;
459                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
460                         func(userData, i, emdm->vertexCos[(int) eed->v1->tmp.l], emdm->vertexCos[(int) eed->v2->tmp.l]);
461         } else {
462                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
463                         func(userData, i, eed->v1->co, eed->v2->co);
464         }
465 }
466 static void emDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) 
467 {
468         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
469         EditEdge *eed;
470         int i;
471
472         if (emdm->vertexCos) {
473                 EditVert *eve;
474
475                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
476                         eve->tmp.l = (intptr_t) i++;
477
478                 glBegin(GL_LINES);
479                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
480                         if(!setDrawOptions || setDrawOptions(userData, i)) {
481                                 glVertex3fv(emdm->vertexCos[(int) eed->v1->tmp.l]);
482                                 glVertex3fv(emdm->vertexCos[(int) eed->v2->tmp.l]);
483                         }
484                 }
485                 glEnd();
486         } else {
487                 glBegin(GL_LINES);
488                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
489                         if(!setDrawOptions || setDrawOptions(userData, i)) {
490                                 glVertex3fv(eed->v1->co);
491                                 glVertex3fv(eed->v2->co);
492                         }
493                 }
494                 glEnd();
495         }
496 }
497 static void emDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
498 {
499         emDM_drawMappedEdges(dm, NULL, NULL);
500 }
501 static void emDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void (*setDrawInterpOptions)(void *userData, int index, float t), void *userData) 
502 {
503         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
504         EditEdge *eed;
505         int i;
506
507         if (emdm->vertexCos) {
508                 EditVert *eve;
509
510                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
511                         eve->tmp.l = (intptr_t) i++;
512
513                 glBegin(GL_LINES);
514                 for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
515                         if(!setDrawOptions || setDrawOptions(userData, i)) {
516                                 setDrawInterpOptions(userData, i, 0.0);
517                                 glVertex3fv(emdm->vertexCos[(int) eed->v1->tmp.l]);
518                                 setDrawInterpOptions(userData, i, 1.0);
519                                 glVertex3fv(emdm->vertexCos[(int) eed->v2->tmp.l]);
520                         }
521                 }
522                 glEnd();
523         } else {
524                 glBegin(GL_LINES);
525                 for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
526                         if(!setDrawOptions || setDrawOptions(userData, i)) {
527                                 setDrawInterpOptions(userData, i, 0.0);
528                                 glVertex3fv(eed->v1->co);
529                                 setDrawInterpOptions(userData, i, 1.0);
530                                 glVertex3fv(eed->v2->co);
531                         }
532                 }
533                 glEnd();
534         }
535 }
536
537 static void emDM_drawUVEdges(DerivedMesh *dm)
538 {
539         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
540         EditFace *efa;
541         MTFace *tf;
542
543         glBegin(GL_LINES);
544         for(efa= emdm->em->faces.first; efa; efa= efa->next) {
545                 tf = CustomData_em_get(&emdm->em->fdata, efa->data, CD_MTFACE);
546
547                 if(tf && !(efa->h)) {
548                         glVertex2fv(tf->uv[0]);
549                         glVertex2fv(tf->uv[1]);
550
551                         glVertex2fv(tf->uv[1]);
552                         glVertex2fv(tf->uv[2]);
553
554                         if (!efa->v4) {
555                                 glVertex2fv(tf->uv[2]);
556                                 glVertex2fv(tf->uv[0]);
557                         } else {
558                                 glVertex2fv(tf->uv[2]);
559                                 glVertex2fv(tf->uv[3]);
560                                 glVertex2fv(tf->uv[3]);
561                                 glVertex2fv(tf->uv[0]);
562                         }
563                 }
564         }
565         glEnd();
566 }
567
568 static void emDM__calcFaceCent(EditFace *efa, float cent[3], float (*vertexCos)[3])
569 {
570         if (vertexCos) {
571                 VECCOPY(cent, vertexCos[(int) efa->v1->tmp.l]);
572                 VecAddf(cent, cent, vertexCos[(int) efa->v2->tmp.l]);
573                 VecAddf(cent, cent, vertexCos[(int) efa->v3->tmp.l]);
574                 if (efa->v4) VecAddf(cent, cent, vertexCos[(int) efa->v4->tmp.l]);
575         } else {
576                 VECCOPY(cent, efa->v1->co);
577                 VecAddf(cent, cent, efa->v2->co);
578                 VecAddf(cent, cent, efa->v3->co);
579                 if (efa->v4) VecAddf(cent, cent, efa->v4->co);
580         }
581
582         if (efa->v4) {
583                 VecMulf(cent, 0.25f);
584         } else {
585                 VecMulf(cent, 0.33333333333f);
586         }
587 }
588 static void emDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData)
589 {
590         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
591         EditVert *eve;
592         EditFace *efa;
593         float cent[3];
594         int i;
595
596         if (emdm->vertexCos) {
597                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
598                         eve->tmp.l = (intptr_t) i++;
599         }
600
601         for(i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
602                 emDM__calcFaceCent(efa, cent, emdm->vertexCos);
603                 func(userData, i, cent, emdm->vertexCos?emdm->faceNos[i]:efa->n);
604         }
605 }
606 static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors)
607 {
608         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
609         EditFace *efa;
610         int i, draw;
611
612         if (emdm->vertexCos) {
613                 EditVert *eve;
614
615                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
616                         eve->tmp.l = (intptr_t) i++;
617
618                 for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
619                         int drawSmooth = (efa->flag & ME_SMOOTH);
620                         draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, i, &drawSmooth);
621                         if(draw) {
622                                 if (draw==2) { /* enabled with stipple */
623                                         glEnable(GL_POLYGON_STIPPLE);
624                                         glPolygonStipple(stipple_quarttone);
625                                 }
626                                 
627                                 glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
628
629                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
630                                 if (!drawSmooth) {
631                                         glNormal3fv(emdm->faceNos[i]);
632                                         glVertex3fv(emdm->vertexCos[(int) efa->v1->tmp.l]);
633                                         glVertex3fv(emdm->vertexCos[(int) efa->v2->tmp.l]);
634                                         glVertex3fv(emdm->vertexCos[(int) efa->v3->tmp.l]);
635                                         if(efa->v4) glVertex3fv(emdm->vertexCos[(int) efa->v4->tmp.l]);
636                                 } else {
637                                         glNormal3fv(emdm->vertexNos[(int) efa->v1->tmp.l]);
638                                         glVertex3fv(emdm->vertexCos[(int) efa->v1->tmp.l]);
639                                         glNormal3fv(emdm->vertexNos[(int) efa->v2->tmp.l]);
640                                         glVertex3fv(emdm->vertexCos[(int) efa->v2->tmp.l]);
641                                         glNormal3fv(emdm->vertexNos[(int) efa->v3->tmp.l]);
642                                         glVertex3fv(emdm->vertexCos[(int) efa->v3->tmp.l]);
643                                         if(efa->v4) {
644                                                 glNormal3fv(emdm->vertexNos[(int) efa->v4->tmp.l]);
645                                                 glVertex3fv(emdm->vertexCos[(int) efa->v4->tmp.l]);
646                                         }
647                                 }
648                                 glEnd();
649                                 
650                                 if (draw==2)
651                                         glDisable(GL_POLYGON_STIPPLE);
652                         }
653                 }
654         } else {
655                 for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
656                         int drawSmooth = (efa->flag & ME_SMOOTH);
657                         draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, i, &drawSmooth);
658                         if(draw) {
659                                 if (draw==2) { /* enabled with stipple */
660                                         glEnable(GL_POLYGON_STIPPLE);
661                                         glPolygonStipple(stipple_quarttone);
662                                 }
663                                 glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
664
665                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
666                                 if (!drawSmooth) {
667                                         glNormal3fv(efa->n);
668                                         glVertex3fv(efa->v1->co);
669                                         glVertex3fv(efa->v2->co);
670                                         glVertex3fv(efa->v3->co);
671                                         if(efa->v4) glVertex3fv(efa->v4->co);
672                                 } else {
673                                         glNormal3fv(efa->v1->no);
674                                         glVertex3fv(efa->v1->co);
675                                         glNormal3fv(efa->v2->no);
676                                         glVertex3fv(efa->v2->co);
677                                         glNormal3fv(efa->v3->no);
678                                         glVertex3fv(efa->v3->co);
679                                         if(efa->v4) {
680                                                 glNormal3fv(efa->v4->no);
681                                                 glVertex3fv(efa->v4->co);
682                                         }
683                                 }
684                                 glEnd();
685                                 
686                                 if (draw==2)
687                                         glDisable(GL_POLYGON_STIPPLE);
688                         }
689                 }
690         }
691 }
692
693 static void emDM_drawFacesTex_common(DerivedMesh *dm,
694                int (*drawParams)(MTFace *tface, MCol *mcol, int matnr),
695                int (*drawParamsMapped)(void *userData, int index),
696                void *userData) 
697 {
698         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
699         EditMesh *em= emdm->em;
700         float (*vertexCos)[3]= emdm->vertexCos;
701         float (*vertexNos)[3]= emdm->vertexNos;
702         EditFace *efa;
703         int i;
704
705         /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
706         glShadeModel(GL_SMOOTH);
707         
708         if (vertexCos) {
709                 EditVert *eve;
710
711                 for (i=0,eve=em->verts.first; eve; eve= eve->next)
712                         eve->tmp.l = (intptr_t) i++;
713
714                 for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) {
715                         MTFace *tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
716                         MCol *mcol= CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
717                         unsigned char *cp= NULL;
718                         int drawSmooth= (efa->flag & ME_SMOOTH);
719                         int flag;
720
721                         if(drawParams)
722                                 flag= drawParams(tf, mcol, efa->mat_nr);
723                         else if(drawParamsMapped)
724                                 flag= drawParamsMapped(userData, i);
725                         else
726                                 flag= 1;
727
728                         if(flag != 0) { /* flag 0 == the face is hidden or invisible */
729                                 
730                                 /* we always want smooth here since otherwise vertex colors dont interpolate */
731                                 if (mcol) {
732                                         if (flag==1) {
733                                                 cp= (unsigned char*)mcol;
734                                         }
735                                 } else {
736                                         glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
737                                 } 
738                                 
739                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
740                                 if (!drawSmooth) {
741                                         glNormal3fv(emdm->faceNos[i]);
742
743                                         if(tf) glTexCoord2fv(tf->uv[0]);
744                                         if(cp) glColor3ub(cp[3], cp[2], cp[1]);
745                                         glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
746
747                                         if(tf) glTexCoord2fv(tf->uv[1]);
748                                         if(cp) glColor3ub(cp[7], cp[6], cp[5]);
749                                         glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
750
751                                         if(tf) glTexCoord2fv(tf->uv[2]);
752                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
753                                         glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
754
755                                         if(efa->v4) {
756                                                 if(tf) glTexCoord2fv(tf->uv[3]);
757                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
758                                                 glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
759                                         }
760                                 } else {
761                                         if(tf) glTexCoord2fv(tf->uv[0]);
762                                         if(cp) glColor3ub(cp[3], cp[2], cp[1]);
763                                         glNormal3fv(vertexNos[(int) efa->v1->tmp.l]);
764                                         glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
765
766                                         if(tf) glTexCoord2fv(tf->uv[1]);
767                                         if(cp) glColor3ub(cp[7], cp[6], cp[5]);
768                                         glNormal3fv(vertexNos[(int) efa->v2->tmp.l]);
769                                         glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
770
771                                         if(tf) glTexCoord2fv(tf->uv[2]);
772                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
773                                         glNormal3fv(vertexNos[(int) efa->v3->tmp.l]);
774                                         glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
775
776                                         if(efa->v4) {
777                                                 if(tf) glTexCoord2fv(tf->uv[3]);
778                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
779                                                 glNormal3fv(vertexNos[(int) efa->v4->tmp.l]);
780                                                 glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
781                                         }
782                                 }
783                                 glEnd();
784                         }
785                 }
786         } else {
787                 for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) {
788                         MTFace *tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
789                         MCol *mcol= CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
790                         unsigned char *cp= NULL;
791                         int drawSmooth= (efa->flag & ME_SMOOTH);
792                         int flag;
793
794                         if(drawParams)
795                                 flag= drawParams(tf, mcol, efa->mat_nr);
796                         else if(drawParamsMapped)
797                                 flag= drawParamsMapped(userData, i);
798                         else
799                                 flag= 1;
800
801                         if(flag != 0) { /* flag 0 == the face is hidden or invisible */
802                                 /* we always want smooth here since otherwise vertex colors dont interpolate */
803                                 if (mcol) {
804                                         if (flag==1) {
805                                                 cp= (unsigned char*)mcol;
806                                         }
807                                 } else {
808                                         glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
809                                 } 
810
811                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
812                                 if (!drawSmooth) {
813                                         glNormal3fv(efa->n);
814
815                                         if(tf) glTexCoord2fv(tf->uv[0]);
816                                         if(cp) glColor3ub(cp[3], cp[2], cp[1]);
817                                         glVertex3fv(efa->v1->co);
818
819                                         if(tf) glTexCoord2fv(tf->uv[1]);
820                                         if(cp) glColor3ub(cp[7], cp[6], cp[5]);
821                                         glVertex3fv(efa->v2->co);
822
823                                         if(tf) glTexCoord2fv(tf->uv[2]);
824                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
825                                         glVertex3fv(efa->v3->co);
826
827                                         if(efa->v4) {
828                                                 if(tf) glTexCoord2fv(tf->uv[3]);
829                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
830                                                 glVertex3fv(efa->v4->co);
831                                         }
832                                 } else {
833                                         if(tf) glTexCoord2fv(tf->uv[0]);
834                                         if(cp) glColor3ub(cp[3], cp[2], cp[1]);
835                                         glNormal3fv(efa->v1->no);
836                                         glVertex3fv(efa->v1->co);
837
838                                         if(tf) glTexCoord2fv(tf->uv[1]);
839                                         if(cp) glColor3ub(cp[7], cp[6], cp[5]);
840                                         glNormal3fv(efa->v2->no);
841                                         glVertex3fv(efa->v2->co);
842
843                                         if(tf) glTexCoord2fv(tf->uv[2]);
844                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
845                                         glNormal3fv(efa->v3->no);
846                                         glVertex3fv(efa->v3->co);
847
848                                         if(efa->v4) {
849                                                 if(tf) glTexCoord2fv(tf->uv[3]);
850                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
851                                                 glNormal3fv(efa->v4->no);
852                                                 glVertex3fv(efa->v4->co);
853                                         }
854                                 }
855                                 glEnd();
856                         }
857                 }
858         }
859 }
860
861 static void emDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
862 {
863         emDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
864 }
865
866 static void emDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
867 {
868         emDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
869 }
870
871 static void emDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
872 {
873         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
874         EditVert *eve;
875         int i;
876
877         if (emdm->em->verts.first) {
878                 for (i=0,eve= emdm->em->verts.first; eve; i++,eve= eve->next) {
879                         if (emdm->vertexCos) {
880                                 DO_MINMAX(emdm->vertexCos[i], min_r, max_r);
881                         } else {
882                                 DO_MINMAX(eve->co, min_r, max_r);
883                         }
884                 }
885         } else {
886                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
887         }
888 }
889 static int emDM_getNumVerts(DerivedMesh *dm)
890 {
891         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
892
893         return BLI_countlist(&emdm->em->verts);
894 }
895
896 static int emDM_getNumEdges(DerivedMesh *dm)
897 {
898         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
899
900         return BLI_countlist(&emdm->em->edges);
901 }
902
903 static int emDM_getNumFaces(DerivedMesh *dm)
904 {
905         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
906
907         return BLI_countlist(&emdm->em->faces);
908 }
909
910 void emDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
911 {
912         EditVert *ev = ((EditMeshDerivedMesh *)dm)->em->verts.first;
913         int i;
914
915         for(i = 0; i < index; ++i) ev = ev->next;
916
917         VECCOPY(vert_r->co, ev->co);
918
919         vert_r->no[0] = ev->no[0] * 32767.0;
920         vert_r->no[1] = ev->no[1] * 32767.0;
921         vert_r->no[2] = ev->no[2] * 32767.0;
922
923         /* TODO what to do with vert_r->flag and vert_r->mat_nr? */
924         vert_r->mat_nr = 0;
925         vert_r->bweight = (unsigned char) (ev->bweight*255.0f);
926 }
927
928 void emDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
929 {
930         EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
931         EditEdge *ee = em->edges.first;
932         EditVert *ev, *v1, *v2;
933         int i;
934
935         for(i = 0; i < index; ++i) ee = ee->next;
936
937         edge_r->crease = (unsigned char) (ee->crease*255.0f);
938         edge_r->bweight = (unsigned char) (ee->bweight*255.0f);
939         /* TODO what to do with edge_r->flag? */
940         edge_r->flag = ME_EDGEDRAW|ME_EDGERENDER;
941         if (ee->seam) edge_r->flag |= ME_SEAM;
942         if (ee->sharp) edge_r->flag |= ME_SHARP;
943 #if 0
944         /* this needs setup of f2 field */
945         if (!ee->f2) edge_r->flag |= ME_LOOSEEDGE;
946 #endif
947
948         /* goddamn, we have to search all verts to find indices */
949         v1 = ee->v1;
950         v2 = ee->v2;
951         for(i = 0, ev = em->verts.first; v1 || v2; i++, ev = ev->next) {
952                 if(ev == v1) {
953                         edge_r->v1 = i;
954                         v1 = NULL;
955                 }
956                 if(ev == v2) {
957                         edge_r->v2 = i;
958                         v2 = NULL;
959                 }
960         }
961 }
962
963 void emDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
964 {
965         EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
966         EditFace *ef = em->faces.first;
967         EditVert *ev, *v1, *v2, *v3, *v4;
968         int i;
969
970         for(i = 0; i < index; ++i) ef = ef->next;
971
972         face_r->mat_nr = ef->mat_nr;
973         face_r->flag = ef->flag;
974
975         /* goddamn, we have to search all verts to find indices */
976         v1 = ef->v1;
977         v2 = ef->v2;
978         v3 = ef->v3;
979         v4 = ef->v4;
980         if(!v4) face_r->v4 = 0;
981
982         for(i = 0, ev = em->verts.first; v1 || v2 || v3 || v4;
983             i++, ev = ev->next) {
984                 if(ev == v1) {
985                         face_r->v1 = i;
986                         v1 = NULL;
987                 }
988                 if(ev == v2) {
989                         face_r->v2 = i;
990                         v2 = NULL;
991                 }
992                 if(ev == v3) {
993                         face_r->v3 = i;
994                         v3 = NULL;
995                 }
996                 if(ev == v4) {
997                         face_r->v4 = i;
998                         v4 = NULL;
999                 }
1000         }
1001
1002         test_index_face(face_r, NULL, 0, ef->v4?4:3);
1003 }
1004
1005 void emDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
1006 {
1007         EditVert *ev = ((EditMeshDerivedMesh *)dm)->em->verts.first;
1008
1009         for( ; ev; ev = ev->next, ++vert_r) {
1010                 VECCOPY(vert_r->co, ev->co);
1011
1012                 vert_r->no[0] = ev->no[0] * 32767.0;
1013                 vert_r->no[1] = ev->no[1] * 32767.0;
1014                 vert_r->no[2] = ev->no[2] * 32767.0;
1015
1016                 /* TODO what to do with vert_r->flag and vert_r->mat_nr? */
1017                 vert_r->mat_nr = 0;
1018                 vert_r->flag = 0;
1019                 vert_r->bweight = (unsigned char) (ev->bweight*255.0f);
1020         }
1021 }
1022
1023 void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
1024 {
1025         EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
1026         EditEdge *ee = em->edges.first;
1027         EditVert *ev;
1028         int i;
1029
1030         /* store vertex indices in tmp union */
1031         for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i)
1032                 ev->tmp.l = (intptr_t) i;
1033
1034         for( ; ee; ee = ee->next, ++edge_r) {
1035                 edge_r->crease = (unsigned char) (ee->crease*255.0f);
1036                 edge_r->bweight = (unsigned char) (ee->bweight*255.0f);
1037                 /* TODO what to do with edge_r->flag? */
1038                 edge_r->flag = ME_EDGEDRAW|ME_EDGERENDER;
1039                 if (ee->seam) edge_r->flag |= ME_SEAM;
1040                 if (ee->sharp) edge_r->flag |= ME_SHARP;
1041 #if 0
1042                 /* this needs setup of f2 field */
1043                 if (!ee->f2) edge_r->flag |= ME_LOOSEEDGE;
1044 #endif
1045
1046                 edge_r->v1 = (int)ee->v1->tmp.l;
1047                 edge_r->v2 = (int)ee->v2->tmp.l;
1048         }
1049 }
1050
1051 void emDM_copyFaceArray(DerivedMesh *dm, MFace *face_r)
1052 {
1053         EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
1054         EditFace *ef = em->faces.first;
1055         EditVert *ev;
1056         int i;
1057
1058         /* store vertexes indices in tmp union */
1059         for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i)
1060                 ev->tmp.l = (intptr_t) i;
1061
1062         for( ; ef; ef = ef->next, ++face_r) {
1063                 face_r->mat_nr = ef->mat_nr;
1064                 face_r->flag = ef->flag;
1065
1066                 face_r->v1 = (int)ef->v1->tmp.l;
1067                 face_r->v2 = (int)ef->v2->tmp.l;
1068                 face_r->v3 = (int)ef->v3->tmp.l;
1069                 if(ef->v4) face_r->v4 = (int)ef->v4->tmp.l;
1070                 else face_r->v4 = 0;
1071
1072                 test_index_face(face_r, NULL, 0, ef->v4?4:3);
1073         }
1074 }
1075
1076 static void emDM_release(DerivedMesh *dm)
1077 {
1078         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
1079
1080         if (DM_release(dm)) {
1081                 if (emdm->vertexCos) {
1082                         MEM_freeN(emdm->vertexCos);
1083                         MEM_freeN(emdm->vertexNos);
1084                         MEM_freeN(emdm->faceNos);
1085                 }
1086
1087                 MEM_freeN(emdm);
1088         }
1089 }
1090
1091 static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, Object *ob,
1092                                            float (*vertexCos)[3])
1093 {
1094         EditMeshDerivedMesh *emdm = MEM_callocN(sizeof(*emdm), "emdm");
1095
1096         DM_init(&emdm->dm, BLI_countlist(&em->verts),
1097                          BLI_countlist(&em->edges), BLI_countlist(&em->faces));
1098
1099         emdm->dm.getMinMax = emDM_getMinMax;
1100
1101         emdm->dm.getNumVerts = emDM_getNumVerts;
1102         emdm->dm.getNumEdges = emDM_getNumEdges;
1103         emdm->dm.getNumFaces = emDM_getNumFaces;
1104
1105         emdm->dm.getVert = emDM_getVert;
1106         emdm->dm.getEdge = emDM_getEdge;
1107         emdm->dm.getFace = emDM_getFace;
1108         emdm->dm.copyVertArray = emDM_copyVertArray;
1109         emdm->dm.copyEdgeArray = emDM_copyEdgeArray;
1110         emdm->dm.copyFaceArray = emDM_copyFaceArray;
1111
1112         emdm->dm.foreachMappedVert = emDM_foreachMappedVert;
1113         emdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
1114         emdm->dm.foreachMappedFaceCenter = emDM_foreachMappedFaceCenter;
1115
1116         emdm->dm.drawEdges = emDM_drawEdges;
1117         emdm->dm.drawMappedEdges = emDM_drawMappedEdges;
1118         emdm->dm.drawMappedEdgesInterp = emDM_drawMappedEdgesInterp;
1119         emdm->dm.drawMappedFaces = emDM_drawMappedFaces;
1120         emdm->dm.drawMappedFacesTex = emDM_drawMappedFacesTex;
1121         emdm->dm.drawFacesTex = emDM_drawFacesTex;
1122         emdm->dm.drawUVEdges = emDM_drawUVEdges;
1123
1124         emdm->dm.release = emDM_release;
1125         
1126         emdm->em = em;
1127         emdm->vertexCos = vertexCos;
1128
1129         if(CustomData_has_layer(&em->vdata, CD_MDEFORMVERT)) {
1130                 EditVert *eve;
1131                 int i;
1132
1133                 DM_add_vert_layer(&emdm->dm, CD_MDEFORMVERT, CD_CALLOC, NULL);
1134
1135                 for(eve = em->verts.first, i = 0; eve; eve = eve->next, ++i)
1136                         DM_set_vert_data(&emdm->dm, i, CD_MDEFORMVERT,
1137                                          CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT));
1138         }
1139
1140         if(vertexCos) {
1141                 EditVert *eve;
1142                 EditFace *efa;
1143                 int totface = BLI_countlist(&em->faces);
1144                 int i;
1145
1146                 for (i=0,eve=em->verts.first; eve; eve= eve->next)
1147                         eve->tmp.l = (intptr_t) i++;
1148
1149                 emdm->vertexNos = MEM_callocN(sizeof(*emdm->vertexNos)*i, "emdm_vno");
1150                 emdm->faceNos = MEM_mallocN(sizeof(*emdm->faceNos)*totface, "emdm_vno");
1151
1152                 for(i=0, efa= em->faces.first; efa; i++, efa=efa->next) {
1153                         float *v1 = vertexCos[(int) efa->v1->tmp.l];
1154                         float *v2 = vertexCos[(int) efa->v2->tmp.l];
1155                         float *v3 = vertexCos[(int) efa->v3->tmp.l];
1156                         float *no = emdm->faceNos[i];
1157                         
1158                         if(efa->v4) {
1159                                 float *v4 = vertexCos[(int) efa->v4->tmp.l];
1160
1161                                 CalcNormFloat4(v1, v2, v3, v4, no);
1162                                 VecAddf(emdm->vertexNos[(int) efa->v4->tmp.l], emdm->vertexNos[(int) efa->v4->tmp.l], no);
1163                         }
1164                         else {
1165                                 CalcNormFloat(v1, v2, v3, no);
1166                         }
1167
1168                         VecAddf(emdm->vertexNos[(int) efa->v1->tmp.l], emdm->vertexNos[(int) efa->v1->tmp.l], no);
1169                         VecAddf(emdm->vertexNos[(int) efa->v2->tmp.l], emdm->vertexNos[(int) efa->v2->tmp.l], no);
1170                         VecAddf(emdm->vertexNos[(int) efa->v3->tmp.l], emdm->vertexNos[(int) efa->v3->tmp.l], no);
1171                 }
1172
1173                 for(i=0, eve= em->verts.first; eve; i++, eve=eve->next) {
1174                         float *no = emdm->vertexNos[i];
1175                         /* following Mesh convention; we use vertex coordinate itself
1176                          * for normal in this case */
1177                         if (Normalize(no)==0.0) {
1178                                 VECCOPY(no, vertexCos[i]);
1179                                 Normalize(no);
1180                         }
1181                 }
1182         }
1183
1184         return (DerivedMesh*) emdm;
1185 }
1186
1187 #ifdef WITH_VERSE
1188
1189 /* verse derived mesh */
1190 typedef struct {
1191         struct DerivedMesh dm;
1192         struct VNode *vnode;
1193         struct VLayer *vertex_layer;
1194         struct VLayer *polygon_layer;
1195         struct ListBase *edges;
1196         float (*vertexCos)[3];
1197 } VDerivedMesh;
1198
1199 /* this function set up border points of verse mesh bounding box */
1200 static void vDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
1201 {
1202         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1203         struct VerseVert *vvert;
1204
1205         if(!vdm->vertex_layer) return;
1206
1207         vvert = (VerseVert*)vdm->vertex_layer->dl.lb.first;
1208
1209         if(vdm->vertex_layer->dl.da.count > 0) {
1210                 while(vvert) {
1211                         DO_MINMAX(vdm->vertexCos ? vvert->cos : vvert->co, min_r, max_r);
1212                         vvert = vvert->next;
1213                 }
1214         }
1215         else {
1216                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
1217         }
1218 }
1219
1220 /* this function return number of vertexes in vertex layer */
1221 static int vDM_getNumVerts(DerivedMesh *dm)
1222 {
1223         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1224
1225         if(!vdm->vertex_layer) return 0;
1226         else return vdm->vertex_layer->dl.da.count;
1227 }
1228
1229 /* this function return number of 'fake' edges */
1230 static int vDM_getNumEdges(DerivedMesh *dm)
1231 {
1232         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1233
1234         return BLI_countlist(vdm->edges);
1235 }
1236
1237 /* this function returns number of polygons in polygon layer */
1238 static int vDM_getNumFaces(DerivedMesh *dm)
1239 {
1240         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1241
1242         if(!vdm->polygon_layer) return 0;
1243         else return vdm->polygon_layer->dl.da.count;
1244 }
1245
1246 /* this function doesn't return vertex with index of access array,
1247  * but it return 'indexth' vertex of dynamic list */
1248 void vDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
1249 {
1250         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1251         struct VerseVert *vvert;
1252         int i;
1253
1254         if(!vdm->vertex_layer) return;
1255
1256         for(vvert = vdm->vertex_layer->dl.lb.first, i=0 ; i<index; i++) vvert = vvert->next;
1257
1258         if(vvert) {
1259                 VECCOPY(vert_r->co, vvert->co);
1260
1261                 vert_r->no[0] = vvert->no[0] * 32767.0;
1262                 vert_r->no[1] = vvert->no[1] * 32767.0;
1263                 vert_r->no[2] = vvert->no[2] * 32767.0;
1264
1265                 /* TODO what to do with vert_r->flag and vert_r->mat_nr? */
1266                 vert_r->mat_nr = 0;
1267                 vert_r->flag = 0;
1268         }
1269 }
1270
1271 /* this function returns fake verse edge */
1272 void vDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
1273 {
1274         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1275         struct VerseEdge *vedge;
1276         struct VLayer *vert_vlayer = vdm->vertex_layer;
1277         struct VerseVert *vvert;
1278         int j;
1279
1280         if(!vdm->vertex_layer || !vdm->edges) return;
1281
1282         if(vdm->edges->first) {
1283                 struct VerseVert *vvert1, *vvert2;
1284
1285                 /* store vert indices in tmp union */
1286                 for(vvert = vdm->vertex_layer->dl.lb.first, j = 0; vvert; vvert = vvert->next, j++)
1287                         vvert->tmp.index = j;
1288
1289                 for(vedge = vdm->edges->first; vedge; vedge = vedge->next) {
1290                         if(vedge->tmp.index==index) {
1291                                 vvert1 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v0);
1292                                 vvert2 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v1);
1293                                 
1294                                 if(vvert1 && vvert2) {
1295                                         edge_r->v1 = vvert1->tmp.index;
1296                                         edge_r->v2 = vvert2->tmp.index;
1297                                 }
1298                                 else {
1299                                         edge_r->v1 = 0;
1300                                         edge_r->v2 = 0;
1301                                 }
1302                                 /* not supported yet */
1303                                 edge_r->flag = 0;
1304                                 edge_r->crease = 0;
1305                                 edge_r->bweight = 0;
1306                                 break;
1307                         }
1308                 }
1309         }
1310 }
1311
1312 /* this function doesn't return face with index of access array,
1313  * but it returns 'indexth' vertex of dynamic list */
1314 void vDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
1315 {
1316         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1317         struct VerseFace *vface;
1318         struct VerseVert *vvert;
1319         struct VerseVert *vvert0, *vvert1, *vvert2, *vvert3;
1320         int i;
1321
1322         if(!vdm->vertex_layer || !vdm->polygon_layer) return;
1323
1324         for(vface = vdm->polygon_layer->dl.lb.first, i = 0; i < index; ++i) vface = vface->next;
1325
1326         face_r->mat_nr = 0;
1327         face_r->flag = 0;
1328
1329         /* goddamn, we have to search all verts to find indices */
1330         vvert0 = vface->vvert0;
1331         vvert1 = vface->vvert1;
1332         vvert2 = vface->vvert2;
1333         vvert3 = vface->vvert3;
1334         if(!vvert3) face_r->v4 = 0;
1335
1336         for(vvert = vdm->vertex_layer->dl.lb.first, i = 0; vvert0 || vvert1 || vvert2 || vvert3; i++, vvert = vvert->next) {
1337                 if(vvert == vvert0) {
1338                         face_r->v1 = i;
1339                         vvert0 = NULL;
1340                 }
1341                 if(vvert == vvert1) {
1342                         face_r->v2 = i;
1343                         vvert1 = NULL;
1344                 }
1345                 if(vvert == vvert2) {
1346                         face_r->v3 = i;
1347                         vvert2 = NULL;
1348                 }
1349                 if(vvert == vvert3) {
1350                         face_r->v4 = i;
1351                         vvert3 = NULL;
1352                 }
1353         }
1354
1355         test_index_face(face_r, NULL, 0, vface->vvert3?4:3);
1356 }
1357
1358 /* fill array of mvert */
1359 void vDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
1360 {
1361         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1362         struct VerseVert *vvert;
1363
1364         if(!vdm->vertex_layer) return;
1365
1366         for(vvert = vdm->vertex_layer->dl.lb.first ; vvert; vvert = vvert->next, ++vert_r) {
1367                 VECCOPY(vert_r->co, vvert->co);
1368
1369                 vert_r->no[0] = vvert->no[0] * 32767.0;
1370                 vert_r->no[1] = vvert->no[1] * 32767.0;
1371                 vert_r->no[2] = vvert->no[2] * 32767.0;
1372
1373                 vert_r->mat_nr = 0;
1374                 vert_r->flag = 0;
1375         }
1376 }
1377
1378 /* dummy function, edges arent supported in verse mesh */
1379 void vDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
1380 {
1381         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1382
1383         if(!vdm->vertex_layer || !vdm->edges) return;
1384
1385         if(vdm->edges->first) {
1386                 struct VerseEdge *vedge;
1387                 struct VLayer *vert_vlayer = vdm->vertex_layer;
1388                 struct VerseVert *vvert, *vvert1, *vvert2;
1389                 int j;
1390
1391                 /* store vert indices in tmp union */
1392                 for(vvert = vdm->vertex_layer->dl.lb.first, j = 0; vvert; vvert = vvert->next, ++j)
1393                         vvert->tmp.index = j;
1394
1395                 for(vedge = vdm->edges->first, j=0 ; vedge; vedge = vedge->next, ++edge_r, j++) {
1396                         /* create temporary edge index */
1397                         vedge->tmp.index = j;
1398                         vvert1 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v0);
1399                         vvert2 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v1);
1400                         if(vvert1 && vvert2) {
1401                                 edge_r->v1 = vvert1->tmp.index;
1402                                 edge_r->v2 = vvert2->tmp.index;
1403                         }
1404                         else {
1405                                 printf("error: vDM_copyEdgeArray: %d, %d\n", vedge->v0, vedge->v1);
1406                                 edge_r->v1 = 0;
1407                                 edge_r->v2 = 0;
1408                         }
1409                         /* not supported yet */
1410                         edge_r->flag = 0;
1411                         edge_r->crease = 0;
1412                         edge_r->bweight = 0;
1413                 }
1414         }
1415 }
1416
1417 /* fill array of mfaces */
1418 void vDM_copyFaceArray(DerivedMesh *dm, MFace *face_r)
1419 {
1420         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1421         struct VerseFace *vface;
1422         struct VerseVert *vvert;
1423         int i;
1424         
1425         if(!vdm->vertex_layer || !vdm->polygon_layer) return;
1426         
1427         /* store vertexes indices in tmp union */
1428         for(vvert = vdm->vertex_layer->dl.lb.first, i = 0; vvert; vvert = vvert->next, ++i)
1429                 vvert->tmp.index = i;
1430
1431         for(vface = vdm->polygon_layer->dl.lb.first; vface; vface = vface->next, ++face_r) {
1432                 face_r->mat_nr = 0;
1433                 face_r->flag = 0;
1434
1435                 face_r->v1 = vface->vvert0->tmp.index;
1436                 face_r->v2 = vface->vvert1->tmp.index;
1437                 face_r->v3 = vface->vvert2->tmp.index;
1438                 if(vface->vvert3) face_r->v4 = vface->vvert3->tmp.index;
1439                 else face_r->v4 = 0;
1440
1441                 test_index_face(face_r, NULL, 0, vface->vvert3?4:3);
1442         }
1443 }
1444
1445 /* return coordination of vertex with index */
1446 static void vDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
1447 {
1448         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1449         struct VerseVert *vvert = NULL;
1450
1451         if(!vdm->vertex_layer) return;
1452
1453         vvert = BLI_dlist_find_link(&(vdm->vertex_layer->dl), index);
1454         
1455         if(vvert) {
1456                 VECCOPY(co_r, vdm->vertexCos ? vvert->cos : vvert->co);
1457         }
1458         else {
1459                 co_r[0] = co_r[1] = co_r[2] = 0.0;
1460         }
1461 }
1462
1463 /* return array of vertex coordiantions */
1464 static void vDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
1465 {
1466         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1467         struct VerseVert *vvert;
1468         int i = 0;
1469
1470         if(!vdm->vertex_layer) return;
1471
1472         vvert = vdm->vertex_layer->dl.lb.first;
1473         while(vvert) {
1474                 VECCOPY(cos_r[i], vdm->vertexCos ? vvert->cos : vvert->co);
1475                 i++;
1476                 vvert = vvert->next;
1477         }
1478 }
1479
1480 /* return normal of vertex with index */
1481 static void vDM_getVertNo(DerivedMesh *dm, int index, float no_r[3])
1482 {
1483         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1484         struct VerseVert *vvert = NULL;
1485
1486         if(!vdm->vertex_layer) return;
1487
1488         vvert = BLI_dlist_find_link(&(vdm->vertex_layer->dl), index);
1489         if(vvert) {
1490                 VECCOPY(no_r, vvert->no);
1491         }
1492         else {
1493                 no_r[0] = no_r[1] = no_r[2] = 0.0;
1494         }
1495 }
1496
1497 /* draw all VerseVertexes */
1498 static void vDM_drawVerts(DerivedMesh *dm)
1499 {
1500         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1501         struct VerseVert *vvert;
1502
1503         if(!vdm->vertex_layer) return;
1504
1505         vvert = vdm->vertex_layer->dl.lb.first;
1506
1507         bglBegin(GL_POINTS);
1508         while(vvert) {
1509                 bglVertex3fv(vdm->vertexCos ? vvert->cos : vvert->co);
1510                 vvert = vvert->next;
1511         }
1512         bglEnd();
1513 }
1514
1515 /* draw all edges of VerseFaces ... it isn't optimal, because verse
1516  * specification doesn't support edges :-( ... bother eskil ;-)
1517  * ... some edges (most of edges) are drawn twice */
1518 static void vDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
1519 {
1520         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1521         struct VerseEdge *vedge;
1522         struct VLayer *vert_vlayer = vdm->vertex_layer;
1523
1524         if(vert_vlayer && vdm->edges && (BLI_countlist(vdm->edges) > 0)) {
1525                 struct VerseVert *vvert1, *vvert2;
1526
1527                 glBegin(GL_LINES);
1528                 for(vedge = vdm->edges->first; vedge; vedge = vedge->next) {
1529                         vvert1 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v0);
1530                         vvert2 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v1);
1531                         if(vvert1 && vvert2) {
1532                                 glVertex3fv(vdm->vertexCos ? vvert1->cos : vvert1->co);
1533                                 glVertex3fv(vdm->vertexCos ? vvert2->cos : vvert2->co);
1534                         }
1535                 }
1536                 glEnd();
1537         }
1538 }
1539
1540 /* verse spec doesn't support edges ... loose edges can't exist */
1541 void vDM_drawLooseEdges(DerivedMesh *dm)
1542 {
1543 }
1544
1545 /* draw uv edges, not supported yet */
1546 static void vDM_drawUVEdges(DerivedMesh *dm)
1547 {
1548 }
1549
1550 /* draw all VerseFaces */
1551 static void vDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
1552 {
1553         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1554         struct VerseFace *vface;
1555
1556         if(!vdm->polygon_layer) return;
1557
1558         vface = vdm->polygon_layer->dl.lb.first;
1559
1560         glShadeModel(GL_FLAT);
1561         while(vface) {
1562                 glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
1563                 glNormal3fv(vface->no);
1564                 glVertex3fv(vdm->vertexCos ? vface->vvert0->cos : vface->vvert0->co);
1565                 glVertex3fv(vdm->vertexCos ? vface->vvert1->cos : vface->vvert1->co);
1566                 glVertex3fv(vdm->vertexCos ? vface->vvert2->cos : vface->vvert2->co);
1567                 if(vface->vvert3)
1568                         glVertex3fv(vdm->vertexCos ? vface->vvert3->cos : vface->vvert3->co);
1569                 glEnd();
1570                 vface = vface->next;
1571         }
1572 }
1573
1574 /* this function should draw mesh with mapped texture, but it isn't supported yet */
1575 static void vDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
1576 {
1577         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1578         struct VerseFace *vface;
1579
1580         if(!vdm->polygon_layer) return;
1581
1582         vface = vdm->polygon_layer->dl.lb.first;
1583
1584         while(vface) {
1585                 glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
1586                 glVertex3fv(vdm->vertexCos ? vface->vvert0->cos : vface->vvert0->co);
1587                 glVertex3fv(vdm->vertexCos ? vface->vvert1->cos : vface->vvert1->co);
1588                 glVertex3fv(vdm->vertexCos ? vface->vvert2->cos : vface->vvert2->co);
1589                 if(vface->vvert3)
1590                         glVertex3fv(vdm->vertexCos ? vface->vvert3->cos : vface->vvert3->co);
1591                 glEnd();
1592
1593                 vface = vface->next;
1594         }
1595 }
1596
1597 /* this function should draw mesh with colored faces (weight paint, vertex
1598  * colors, etc.), but it isn't supported yet */
1599 static void vDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2)
1600 {
1601         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1602         struct VerseFace *vface;
1603
1604         if(!vdm->polygon_layer) return;
1605
1606         vface = vdm->polygon_layer->dl.lb.first;
1607
1608         while(vface) {
1609                 glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
1610                 glVertex3fv(vdm->vertexCos ? vface->vvert0->cos : vface->vvert0->co);
1611                 glVertex3fv(vdm->vertexCos ? vface->vvert1->cos : vface->vvert1->co);
1612                 glVertex3fv(vdm->vertexCos ? vface->vvert2->cos : vface->vvert2->co);
1613                 if(vface->vvert3)
1614                         glVertex3fv(vdm->vertexCos ? vface->vvert3->cos : vface->vvert3->co);
1615                 glEnd();
1616
1617                 vface = vface->next;
1618         }
1619 }
1620
1621 /**/
1622 static void vDM_foreachMappedVert(
1623                 DerivedMesh *dm,
1624                 void (*func)(void *userData, int index, float *co, float *no_f, short *no_s),
1625                 void *userData)
1626 {
1627 }
1628
1629 /**/
1630 static void vDM_foreachMappedEdge(
1631                 DerivedMesh *dm,
1632                 void (*func)(void *userData, int index, float *v0co, float *v1co),
1633                 void *userData)
1634 {
1635 }
1636
1637 /**/
1638 static void vDM_foreachMappedFaceCenter(
1639                 DerivedMesh *dm,
1640                 void (*func)(void *userData, int index, float *cent, float *no),
1641                 void *userData)
1642 {
1643 }
1644
1645 /**/
1646 static void vDM_drawMappedFacesTex(
1647                 DerivedMesh *dm,
1648                 int (*setDrawParams)(void *userData, int index),
1649                 void *userData)
1650 {
1651 }
1652
1653 /**/
1654 static void vDM_drawMappedFaces(
1655                 DerivedMesh *dm,
1656                 int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r),
1657                 void *userData,
1658                 int useColors)
1659 {
1660 }
1661
1662 /**/
1663 static void vDM_drawMappedEdges(
1664                 DerivedMesh *dm,
1665                 int (*setDrawOptions)(void *userData, int index),
1666                 void *userData)
1667 {
1668 }
1669
1670 /**/
1671 static void vDM_drawMappedEdgesInterp(
1672                 DerivedMesh *dm, 
1673                 int (*setDrawOptions)(void *userData, int index), 
1674                 void (*setDrawInterpOptions)(void *userData, int index, float t),
1675                 void *userData)
1676 {
1677 }
1678
1679 /* free all DerivedMesh data */
1680 static void vDM_release(DerivedMesh *dm)
1681 {
1682         VDerivedMesh *vdm = (VDerivedMesh*)dm;
1683
1684         if (DM_release(dm)) {
1685                 if(vdm->vertexCos) MEM_freeN(vdm->vertexCos);
1686                 MEM_freeN(vdm);
1687         }
1688 }
1689
1690 /* create derived mesh from verse mesh ... it is used in object mode, when some other client can
1691  * change shared data and want to see this changes in real time too */
1692 DerivedMesh *derivedmesh_from_versemesh(VNode *vnode, float (*vertexCos)[3])
1693 {
1694         VDerivedMesh *vdm = MEM_callocN(sizeof(*vdm), "vdm");
1695
1696         vdm->vnode = vnode;
1697         vdm->vertex_layer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1698         vdm->polygon_layer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1699         vdm->edges = &((VGeomData*)vnode->data)->edges;
1700
1701         /* vertex and polygon layer has to exist */
1702         if(vdm->vertex_layer && vdm->polygon_layer)
1703                 DM_init(&vdm->dm, vdm->vertex_layer->dl.da.count, BLI_countlist(vdm->edges), vdm->polygon_layer->dl.da.count);
1704         else
1705                 DM_init(&vdm->dm, 0, 0, 0);
1706         
1707         vdm->dm.getMinMax = vDM_getMinMax;
1708
1709         vdm->dm.getNumVerts = vDM_getNumVerts;
1710         vdm->dm.getNumEdges = vDM_getNumEdges;
1711         vdm->dm.getNumFaces = vDM_getNumFaces;
1712
1713         vdm->dm.getVert = vDM_getVert;
1714         vdm->dm.getEdge = vDM_getEdge;
1715         vdm->dm.getFace = vDM_getFace;
1716         vdm->dm.copyVertArray = vDM_copyVertArray;
1717         vdm->dm.copyEdgeArray = vDM_copyEdgeArray;
1718         vdm->dm.copyFaceArray = vDM_copyFaceArray;
1719         
1720         vdm->dm.foreachMappedVert = vDM_foreachMappedVert;
1721         vdm->dm.foreachMappedEdge = vDM_foreachMappedEdge;
1722         vdm->dm.foreachMappedFaceCenter = vDM_foreachMappedFaceCenter;
1723
1724         vdm->dm.getVertCos = vDM_getVertCos;
1725         vdm->dm.getVertCo = vDM_getVertCo;
1726         vdm->dm.getVertNo = vDM_getVertNo;
1727
1728         vdm->dm.drawVerts = vDM_drawVerts;
1729
1730         vdm->dm.drawEdges = vDM_drawEdges;
1731         vdm->dm.drawLooseEdges = vDM_drawLooseEdges;
1732         vdm->dm.drawUVEdges = vDM_drawUVEdges;
1733
1734         vdm->dm.drawFacesSolid = vDM_drawFacesSolid;
1735         vdm->dm.drawFacesTex = vDM_drawFacesTex;
1736         vdm->dm.drawFacesColored = vDM_drawFacesColored;
1737
1738         vdm->dm.drawMappedFacesTex = vDM_drawMappedFacesTex;
1739         vdm->dm.drawMappedFaces = vDM_drawMappedFaces;
1740         vdm->dm.drawMappedEdges = vDM_drawMappedEdges;
1741         vdm->dm.drawMappedEdgesInterp = vDM_drawMappedEdgesInterp;
1742
1743         vdm->dm.release = vDM_release;
1744
1745         vdm->vertexCos = vertexCos;
1746
1747         return (DerivedMesh*) vdm;
1748 }
1749
1750 #endif
1751
1752 /***/
1753
1754 DerivedMesh *mesh_create_derived_for_modifier(Object *ob, ModifierData *md)
1755 {
1756         Mesh *me = ob->data;
1757         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1758         DerivedMesh *dm;
1759
1760         if (!(md->mode&eModifierMode_Realtime)) return NULL;
1761         if (mti->isDisabled && mti->isDisabled(md)) return NULL;
1762
1763         if (mti->type==eModifierTypeType_OnlyDeform) {
1764                 int numVerts;
1765                 float (*deformedVerts)[3] = mesh_getVertexCos(me, &numVerts);
1766
1767                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts);
1768 #ifdef WITH_VERSE
1769                 if(me->vnode) dm = derivedmesh_from_versemesh(me->vnode, deformedVerts);
1770                 else dm = getMeshDerivedMesh(me, ob, deformedVerts);
1771 #else
1772                 dm = getMeshDerivedMesh(me, ob, deformedVerts);
1773 #endif
1774
1775                 MEM_freeN(deformedVerts);
1776         } else {
1777                 DerivedMesh *tdm = getMeshDerivedMesh(me, ob, NULL);
1778                 dm = mti->applyModifier(md, ob, tdm, 0, 0);
1779
1780                 if(tdm != dm) tdm->release(tdm);
1781         }
1782
1783         return dm;
1784 }
1785
1786 CustomDataMask get_viewedit_datamask()
1787 {
1788         CustomDataMask mask = CD_MASK_BAREMESH;
1789         ScrArea *sa;
1790
1791         /* check if we need tfaces & mcols due to face select or texture paint */
1792         if(FACESEL_PAINT_TEST || G.f & G_TEXTUREPAINT) {
1793                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1794         } else {
1795                 /* check if we need tfaces & mcols due to view mode */
1796                 for(sa = G.curscreen->areabase.first; sa; sa = sa->next) {
1797                         if(sa->spacetype == SPACE_VIEW3D) {
1798                                 View3D *view = sa->spacedata.first;
1799                                 if(view->drawtype == OB_SHADED) {
1800                                         /* this includes normals for mesh_create_shadedColors */
1801                                         mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO;
1802                                 }
1803                                 if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) {
1804                                         mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1805                                 }
1806                         }
1807                 }
1808         }
1809
1810         /* check if we need mcols due to vertex paint or weightpaint */
1811         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT)
1812                 mask |= CD_MASK_MCOL;
1813
1814         return mask;
1815 }
1816
1817 static DerivedMesh *create_orco_dm(Object *ob, Mesh *me)
1818 {
1819         DerivedMesh *dm;
1820         float (*orco)[3];
1821
1822         dm= CDDM_from_mesh(me, ob);
1823         orco= (float(*)[3])get_mesh_orco_verts(ob);
1824         CDDM_apply_vert_coords(dm, orco);
1825         CDDM_calc_normals(dm);
1826         MEM_freeN(orco);
1827
1828         return dm;
1829 }
1830
1831 static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1832 {
1833         float (*orco)[3], (*layerorco)[3];
1834         int totvert;
1835
1836         totvert= dm->getNumVerts(dm);
1837
1838         if(orcodm) {
1839                 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
1840
1841                 if(orcodm->getNumVerts(orcodm) == totvert)
1842                         orcodm->getVertCos(orcodm, orco);
1843                 else
1844                         dm->getVertCos(dm, orco);
1845         }
1846         else
1847                 orco= (float(*)[3])get_mesh_orco_verts(ob);
1848
1849         transform_mesh_orco_verts(ob->data, orco, totvert, 0);
1850
1851         if((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1852                 memcpy(layerorco, orco, sizeof(float)*totvert);
1853                 MEM_freeN(orco);
1854         }
1855         else
1856                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1857 }
1858
1859 static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
1860                                 DerivedMesh **deform_r, DerivedMesh **final_r,
1861                                 int useRenderParams, int useDeform,
1862                                 int needMapping, CustomDataMask dataMask, int index)
1863 {
1864         Mesh *me = ob->data;
1865         ModifierData *firstmd, *md;
1866         LinkNode *datamasks, *curr;
1867         CustomDataMask mask;
1868         float (*deformedVerts)[3] = NULL;
1869         DerivedMesh *dm, *orcodm, *finaldm;
1870         int numVerts = me->totvert;
1871         int required_mode;
1872
1873         md = firstmd = modifiers_getVirtualModifierList(ob);
1874
1875         modifiers_clearErrors(ob);
1876
1877         /* we always want to keep original indices */
1878         dataMask |= CD_MASK_ORIGINDEX;
1879
1880         datamasks = modifiers_calcDataMasks(md, dataMask);
1881         curr = datamasks;
1882
1883         if(deform_r) *deform_r = NULL;
1884         *final_r = NULL;
1885
1886         if(useRenderParams) required_mode = eModifierMode_Render;
1887         else required_mode = eModifierMode_Realtime;
1888
1889         if(useDeform) {
1890                 if(do_ob_key(ob)) /* shape key makes deform verts */
1891                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1892                 
1893                 /* Apply all leading deforming modifiers */
1894                 for(;md; md = md->next, curr = curr->next) {
1895                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1896
1897                         if((md->mode & required_mode) != required_mode) continue;
1898                         if(mti->isDisabled && mti->isDisabled(md)) continue;
1899
1900                         if(mti->type == eModifierTypeType_OnlyDeform) {
1901                                 if(!deformedVerts)
1902                                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1903
1904                                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts);
1905                         } else {
1906                                 break;
1907                         }
1908                         
1909                         /* grab modifiers until index i */
1910                         if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
1911                                 break;
1912                 }
1913
1914                 /* Result of all leading deforming modifiers is cached for
1915                  * places that wish to use the original mesh but with deformed
1916                  * coordinates (vpaint, etc.)
1917                  */
1918                 if (deform_r) {
1919 #ifdef WITH_VERSE
1920                         if(me->vnode) *deform_r = derivedmesh_from_versemesh(me->vnode, deformedVerts);
1921                         else {
1922                                 *deform_r = CDDM_from_mesh(me, ob);
1923                                 if(deformedVerts) {
1924                                         CDDM_apply_vert_coords(*deform_r, deformedVerts);
1925                                         CDDM_calc_normals(*deform_r);
1926                                 }
1927                         }
1928 #else
1929                         *deform_r = CDDM_from_mesh(me, ob);
1930                         if(deformedVerts) {
1931                                 CDDM_apply_vert_coords(*deform_r, deformedVerts);
1932                                 CDDM_calc_normals(*deform_r);
1933                         }
1934 #endif
1935                 }
1936         } else {
1937                 /* default behaviour for meshes */
1938                 if(inputVertexCos)
1939                         deformedVerts = inputVertexCos;
1940                 else
1941                         deformedVerts = mesh_getRefKeyCos(me, &numVerts);
1942         }
1943
1944
1945         /* Now apply all remaining modifiers. If useDeform is off then skip
1946          * OnlyDeform ones. 
1947          */
1948         dm = NULL;
1949         orcodm = NULL;
1950
1951 #ifdef WITH_VERSE
1952         /* hack to make sure modifiers don't try to use mesh data from a verse
1953          * node
1954          */
1955         if(me->vnode) dm = derivedmesh_from_versemesh(me->vnode, deformedVerts);
1956 #endif
1957
1958         for(;md; md = md->next, curr = curr->next) {
1959                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1960
1961                 if((md->mode & required_mode) != required_mode) continue;
1962                 if(mti->type == eModifierTypeType_OnlyDeform && !useDeform) continue;
1963                 if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
1964                         modifier_setError(md, "Modifier requires original data, bad stack position.");
1965                         continue;
1966                 }
1967                 if(mti->isDisabled && mti->isDisabled(md)) continue;
1968                 if(needMapping && !modifier_supportsMapping(md)) continue;
1969
1970                 /* add an orco layer if needed by this modifier */
1971                 if(dm && mti->requiredDataMask) {
1972                         mask = mti->requiredDataMask(md);
1973                         if(mask & CD_MASK_ORCO)
1974                                 add_orco_dm(ob, dm, orcodm);
1975                 }
1976
1977                 /* How to apply modifier depends on (a) what we already have as
1978                  * a result of previous modifiers (could be a DerivedMesh or just
1979                  * deformed vertices) and (b) what type the modifier is.
1980                  */
1981
1982                 if(mti->type == eModifierTypeType_OnlyDeform) {
1983                         
1984                         /* No existing verts to deform, need to build them. */
1985                         if(!deformedVerts) {
1986                                 if(dm) {
1987                                         /* Deforming a derived mesh, read the vertex locations
1988                                          * out of the mesh and deform them. Once done with this
1989                                          * run of deformers verts will be written back.
1990                                          */
1991                                         numVerts = dm->getNumVerts(dm);
1992                                         deformedVerts =
1993                                             MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
1994                                         dm->getVertCos(dm, deformedVerts);
1995                                 } else {
1996                                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1997                                 }
1998                         }
1999
2000                         mti->deformVerts(md, ob, dm, deformedVerts, numVerts);
2001                 } else {
2002                         DerivedMesh *ndm;
2003
2004                         /* apply vertex coordinates or build a DerivedMesh as necessary */
2005                         if(dm) {
2006                                 if(deformedVerts) {
2007                                         DerivedMesh *tdm = CDDM_copy(dm);
2008                                         dm->release(dm);
2009                                         dm = tdm;
2010
2011                                         CDDM_apply_vert_coords(dm, deformedVerts);
2012                                         CDDM_calc_normals(dm);
2013                                 }
2014                         } else {
2015                                 dm = CDDM_from_mesh(me, ob);
2016
2017                                 if(deformedVerts) {
2018                                         CDDM_apply_vert_coords(dm, deformedVerts);
2019                                         CDDM_calc_normals(dm);
2020                                 }
2021                         }
2022
2023                         /* create an orco derivedmesh in parallel */
2024                         mask= (CustomDataMask)curr->link;
2025                         if(mask & CD_MASK_ORCO) {
2026                                 if(!orcodm)
2027                                         orcodm= create_orco_dm(ob, me);
2028
2029                                 mask &= ~CD_MASK_ORCO;
2030                                 DM_set_only_copy(orcodm, mask);
2031                                 ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, !inputVertexCos);
2032
2033                                 if(ndm) {
2034                                         /* if the modifier returned a new dm, release the old one */
2035                                         if(orcodm && orcodm != ndm) orcodm->release(orcodm);
2036                                         orcodm = ndm;
2037                                 }
2038                         }
2039
2040                         /* set the DerivedMesh to only copy needed data */
2041                         DM_set_only_copy(dm, mask);
2042                         
2043                         /* add an origspace layer if needed */
2044                         if(((CustomDataMask)curr->link) & CD_MASK_ORIGSPACE)
2045                                 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
2046                                         DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
2047
2048                         ndm = mti->applyModifier(md, ob, dm, useRenderParams, !inputVertexCos);
2049
2050                         if(ndm) {
2051                                 /* if the modifier returned a new dm, release the old one */
2052                                 if(dm && dm != ndm) dm->release(dm);
2053
2054                                 dm = ndm;
2055
2056                                 if(deformedVerts) {
2057                                         if(deformedVerts != inputVertexCos)
2058                                                 MEM_freeN(deformedVerts);
2059
2060                                         deformedVerts = NULL;
2061                                 }
2062                         } 
2063                 }
2064                 
2065                 /* grab modifiers until index i */
2066                 if((index >= 0) && (modifiers_indexInObject(ob, md) >= index))
2067                         break;
2068         }
2069
2070         for(md=firstmd; md; md=md->next)
2071                 modifier_freeTemporaryData(md);
2072
2073         /* Yay, we are done. If we have a DerivedMesh and deformed vertices
2074          * need to apply these back onto the DerivedMesh. If we have no
2075          * DerivedMesh then we need to build one.
2076          */
2077         if(dm && deformedVerts) {
2078                 finaldm = CDDM_copy(dm);
2079
2080                 dm->release(dm);
2081
2082                 CDDM_apply_vert_coords(finaldm, deformedVerts);
2083                 CDDM_calc_normals(finaldm);
2084         } else if(dm) {
2085                 finaldm = dm;
2086         } else {
2087 #ifdef WITH_VERSE
2088                 if(me->vnode)
2089                         finaldm = derivedmesh_from_versemesh(me->vnode, deformedVerts);
2090                 else {
2091                         finaldm = CDDM_from_mesh(me, ob);
2092                         if(deformedVerts) {
2093                                 CDDM_apply_vert_coords(finaldm, deformedVerts);
2094                                 CDDM_calc_normals(finaldm);
2095                         }
2096                 }
2097 #else
2098                 finaldm = CDDM_from_mesh(me, ob);
2099                 if(deformedVerts) {
2100                         CDDM_apply_vert_coords(finaldm, deformedVerts);
2101                         CDDM_calc_normals(finaldm);
2102                 }
2103 #endif
2104         }
2105
2106         /* add an orco layer if needed */
2107         if(dataMask & CD_MASK_ORCO) {
2108                 add_orco_dm(ob, finaldm, orcodm);
2109
2110                 if(deform_r && *deform_r)
2111                         add_orco_dm(ob, *deform_r, NULL);
2112         }
2113
2114         *final_r = finaldm;
2115
2116         if(orcodm)
2117                 orcodm->release(orcodm);
2118
2119         if(deformedVerts && deformedVerts != inputVertexCos)
2120                 MEM_freeN(deformedVerts);
2121
2122         BLI_linklist_free(datamasks, NULL);
2123 }
2124
2125 static float (*editmesh_getVertexCos(EditMesh *em, int *numVerts_r))[3]
2126 {
2127         int i, numVerts = *numVerts_r = BLI_countlist(&em->verts);
2128         float (*cos)[3];
2129         EditVert *eve;
2130
2131         cos = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos");
2132         for (i=0,eve=em->verts.first; i<numVerts; i++,eve=eve->next) {
2133                 VECCOPY(cos[i], eve->co);
2134         }
2135
2136         return cos;
2137 }
2138
2139 static int editmesh_modifier_is_enabled(ModifierData *md, DerivedMesh *dm)
2140 {
2141         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
2142         int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
2143
2144         if((md->mode & required_mode) != required_mode) return 0;
2145         if((mti->flags & eModifierTypeFlag_RequiresOriginalData) && dm) {
2146                 modifier_setError(md, "Modifier requires original data, bad stack position.");
2147                 return 0;
2148         }
2149         if(mti->isDisabled && mti->isDisabled(md)) return 0;
2150         if(!(mti->flags & eModifierTypeFlag_SupportsEditmode)) return 0;
2151         if(md->mode & eModifierMode_DisableTemporary) return 0;
2152         
2153         return 1;
2154 }
2155
2156 static void editmesh_calc_modifiers(DerivedMesh **cage_r,
2157                                     DerivedMesh **final_r,
2158                                     CustomDataMask dataMask)
2159 {
2160         Object *ob = G.obedit;
2161         EditMesh *em = G.editMesh;
2162         ModifierData *md;
2163         float (*deformedVerts)[3] = NULL;
2164         DerivedMesh *dm;
2165         int i, numVerts = 0, cageIndex = modifiers_getCageIndex(ob, NULL);
2166         LinkNode *datamasks, *curr;
2167
2168         modifiers_clearErrors(ob);
2169
2170         if(cage_r && cageIndex == -1) {
2171                 *cage_r = getEditMeshDerivedMesh(em, ob, NULL);
2172         }
2173
2174         dm = NULL;
2175         md = ob->modifiers.first;
2176
2177         /* we always want to keep original indices */
2178         dataMask |= CD_MASK_ORIGINDEX;
2179
2180         datamasks = modifiers_calcDataMasks(md, dataMask);
2181
2182         curr = datamasks;
2183         for(i = 0; md; i++, md = md->next, curr = curr->next) {
2184                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
2185
2186                 if(!editmesh_modifier_is_enabled(md, dm))
2187                         continue;
2188
2189                 /* How to apply modifier depends on (a) what we already have as
2190                  * a result of previous modifiers (could be a DerivedMesh or just
2191                  * deformed vertices) and (b) what type the modifier is.
2192                  */
2193
2194                 if(mti->type == eModifierTypeType_OnlyDeform) {
2195                         /* No existing verts to deform, need to build them. */
2196                         if(!deformedVerts) {
2197                                 if(dm) {
2198                                         /* Deforming a derived mesh, read the vertex locations
2199                                          * out of the mesh and deform them. Once done with this
2200                                          * run of deformers verts will be written back.
2201                                          */
2202                                         numVerts = dm->getNumVerts(dm);
2203                                         deformedVerts =
2204                                             MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
2205                                         dm->getVertCos(dm, deformedVerts);
2206                                 } else {
2207                                         deformedVerts = editmesh_getVertexCos(em, &numVerts);
2208                                 }
2209                         }
2210
2211                         mti->deformVertsEM(md, ob, em, dm, deformedVerts, numVerts);
2212                 } else {
2213                         DerivedMesh *ndm;
2214
2215                         /* apply vertex coordinates or build a DerivedMesh as necessary */
2216                         if(dm) {
2217                                 if(deformedVerts) {
2218                                         DerivedMesh *tdm = CDDM_copy(dm);
2219                                         if(!(cage_r && dm == *cage_r)) dm->release(dm);
2220                                         dm = tdm;
2221
2222                                         CDDM_apply_vert_coords(dm, deformedVerts);
2223                                         CDDM_calc_normals(dm);
2224                                 } else if(cage_r && dm == *cage_r) {
2225                                         /* dm may be changed by this modifier, so we need to copy it
2226                                          */
2227                                         dm = CDDM_copy(dm);
2228                                 }
2229
2230                         } else {
2231                                 dm = CDDM_from_editmesh(em, ob->data);
2232
2233                                 if(deformedVerts) {
2234                                         CDDM_apply_vert_coords(dm, deformedVerts);
2235                                         CDDM_calc_normals(dm);
2236                                 }
2237                         }
2238
2239                         /* set the DerivedMesh to only copy needed data */
2240                         DM_set_only_copy(dm, (CustomDataMask)curr->link);
2241
2242                         if(((CustomDataMask)curr->link) & CD_MASK_ORIGSPACE)
2243                                 if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
2244                                         DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
2245                         
2246                         ndm = mti->applyModifierEM(md, ob, em, dm);
2247
2248                         if (ndm) {
2249                                 if(dm && dm != ndm)
2250                                         dm->release(dm);
2251
2252                                 dm = ndm;
2253
2254                                 if (deformedVerts) {
2255                                         MEM_freeN(deformedVerts);
2256                                         deformedVerts = NULL;
2257                                 }
2258                         }
2259                 }
2260
2261                 if(cage_r && i == cageIndex) {
2262                         if(dm && deformedVerts) {
2263                                 *cage_r = CDDM_copy(dm);
2264                                 CDDM_apply_vert_coords(*cage_r, deformedVerts);
2265                         } else if(dm) {
2266                                 *cage_r = dm;
2267                         } else {
2268                                 *cage_r =
2269                                     getEditMeshDerivedMesh(em, ob,
2270                                         deformedVerts ? MEM_dupallocN(deformedVerts) : NULL);
2271                         }
2272                 }
2273         }
2274
2275         BLI_linklist_free(datamasks, NULL);
2276
2277         /* Yay, we are done. If we have a DerivedMesh and deformed vertices need
2278          * to apply these back onto the DerivedMesh. If we have no DerivedMesh
2279          * then we need to build one.
2280          */
2281         if(dm && deformedVerts) {
2282                 *final_r = CDDM_copy(dm);
2283
2284                 if(!(cage_r && dm == *cage_r)) dm->release(dm);
2285
2286                 CDDM_apply_vert_coords(*final_r, deformedVerts);
2287                 CDDM_calc_normals(*final_r);
2288         } else if (dm) {
2289                 *final_r = dm;
2290         } else if (!deformedVerts && cage_r && *cage_r) {
2291                 *final_r = *cage_r;
2292         } else {
2293                 *final_r = getEditMeshDerivedMesh(em, ob, deformedVerts);
2294                 deformedVerts = NULL;
2295         }
2296
2297         if(deformedVerts)
2298                 MEM_freeN(deformedVerts);
2299 }
2300
2301 /***/
2302
2303
2304         /* Something of a hack, at the moment deal with weightpaint
2305          * by tucking into colors during modifier eval, only in
2306          * wpaint mode. Works ok but need to make sure recalc
2307          * happens on enter/exit wpaint.
2308          */
2309
2310 void weight_to_rgb(float input, float *fr, float *fg, float *fb)
2311 {
2312         float blend;
2313         
2314         blend= ((input/2.0f)+0.5f);
2315         
2316         if (input<=0.25f){      // blue->cyan
2317                 *fr= 0.0f;
2318                 *fg= blend*input*4.0f;
2319                 *fb= blend;
2320         }
2321         else if (input<=0.50f){ // cyan->green
2322                 *fr= 0.0f;
2323                 *fg= blend;
2324                 *fb= blend*(1.0f-((input-0.25f)*4.0f)); 
2325         }
2326         else if (input<=0.75){  // green->yellow
2327                 *fr= blend * ((input-0.50f)*4.0f);
2328                 *fg= blend;
2329                 *fb= 0.0f;
2330         }
2331         else if (input<=1.0){ // yellow->red
2332                 *fr= blend;
2333                 *fg= blend * (1.0f-((input-0.75f)*4.0f)); 
2334                 *fb= 0.0f;
2335         }
2336 }
2337 static void calc_weightpaint_vert_color(Object *ob, ColorBand *coba, int vert, unsigned char *col)
2338 {
2339         Mesh *me = ob->data;
2340         float colf[4], input = 0.0f;
2341         int i;
2342
2343         if (me->dvert) {
2344                 for (i=0; i<me->dvert[vert].totweight; i++)
2345                         if (me->dvert[vert].dw[i].def_nr==ob->actdef-1)
2346                                 input+=me->dvert[vert].dw[i].weight;            
2347         }
2348
2349         CLAMP(input, 0.0f, 1.0f);
2350         
2351         if(coba)
2352                 do_colorband(coba, input, colf);
2353         else
2354                 weight_to_rgb(input, colf, colf+1, colf+2);
2355         
2356         col[3] = (unsigned char)(colf[0] * 255.0f);
2357         col[2] = (unsigned char)(colf[1] * 255.0f);
2358         col[1] = (unsigned char)(colf[2] * 255.0f);
2359         col[0] = 255;
2360 }
2361
2362 static ColorBand *stored_cb= NULL;
2363
2364 void vDM_ColorBand_store(ColorBand *coba)
2365 {
2366         stored_cb= coba;
2367 }
2368
2369 static unsigned char *calc_weightpaint_colors(Object *ob) 
2370 {
2371         Mesh *me = ob->data;
2372         MFace *mf = me->mface;
2373         ColorBand *coba= stored_cb;     /* warning, not a local var */
2374         unsigned char *wtcol;
2375         int i;
2376         
2377         wtcol = MEM_callocN (sizeof (unsigned char) * me->totface*4*4, "weightmap");
2378         
2379         memset(wtcol, 0x55, sizeof (unsigned char) * me->totface*4*4);
2380         for (i=0; i<me->totface; i++, mf++) {
2381                 calc_weightpaint_vert_color(ob, coba, mf->v1, &wtcol[(i*4 + 0)*4]); 
2382                 calc_weightpaint_vert_color(ob, coba, mf->v2, &wtcol[(i*4 + 1)*4]); 
2383                 calc_weightpaint_vert_color(ob, coba, mf->v3, &wtcol[(i*4 + 2)*4]); 
2384                 if (mf->v4)
2385                         calc_weightpaint_vert_color(ob, coba, mf->v4, &wtcol[(i*4 + 3)*4]); 
2386         }
2387         
2388         return wtcol;
2389 }
2390
2391 static void clear_mesh_caches(Object *ob)
2392 {
2393         Mesh *me= ob->data;
2394
2395                 /* also serves as signal to remake texspace */
2396         if (ob->bb) {
2397                 MEM_freeN(ob->bb);
2398                 ob->bb = NULL;
2399         }
2400         if (me->bb) {
2401                 MEM_freeN(me->bb);
2402                 me->bb = NULL;
2403         }
2404
2405         freedisplist(&ob->disp);
2406
2407         if (ob->derivedFinal) {
2408                 ob->derivedFinal->needsFree = 1;
2409                 ob->derivedFinal->release(ob->derivedFinal);
2410                 ob->derivedFinal= NULL;
2411         }
2412         if (ob->derivedDeform) {
2413                 ob->derivedDeform->needsFree = 1;
2414                 ob->derivedDeform->release(ob->derivedDeform);
2415                 ob->derivedDeform= NULL;
2416         }
2417 }
2418
2419 static void mesh_build_data(Object *ob, CustomDataMask dataMask)
2420 {
2421         Mesh *me = ob->data;
2422         float min[3], max[3];
2423
2424         clear_mesh_caches(ob);
2425
2426         if(ob!=G.obedit) {
2427                 Object *obact = G.scene->basact?G.scene->basact->object:NULL;
2428                 int editing = (FACESEL_PAINT_TEST)|(G.f & G_PARTICLEEDIT);
2429                 int needMapping = editing && (ob==obact);
2430
2431                 if( (G.f & G_WEIGHTPAINT) && ob==obact ) {
2432                         MCol *wpcol = (MCol*)calc_weightpaint_colors(ob);
2433                         int layernum = CustomData_number_of_layers(&me->fdata, CD_MCOL);
2434
2435                         /* ugly hack here, we temporarily add a new active mcol layer with
2436                            weightpaint colors in it, that is then duplicated in CDDM_from_mesh */
2437                         CustomData_add_layer(&me->fdata, CD_MCOL, CD_ASSIGN, wpcol, me->totface);
2438                         CustomData_set_layer_active(&me->fdata, CD_MCOL, layernum);
2439
2440                         mesh_calc_modifiers(ob, NULL, &ob->derivedDeform,
2441                                             &ob->derivedFinal, 0, 1,
2442                                             needMapping, dataMask, -1);
2443
2444                         CustomData_free_layer_active(&me->fdata, CD_MCOL, me->totface);
2445                 } else {
2446                         mesh_calc_modifiers(ob, NULL, &ob->derivedDeform,
2447                                             &ob->derivedFinal, G.rendering, 1,
2448                                             needMapping, dataMask, -1);
2449                 }
2450
2451                 INIT_MINMAX(min, max);
2452
2453                 ob->derivedFinal->getMinMax(ob->derivedFinal, min, max);
2454
2455                 if(!ob->bb)
2456                         ob->bb= MEM_callocN(sizeof(BoundBox), "bb");
2457                 boundbox_set_from_min_max(ob->bb, min, max);
2458
2459                 ob->derivedFinal->needsFree = 0;
2460                 ob->derivedDeform->needsFree = 0;
2461                 ob->lastDataMask = dataMask;
2462         }
2463 }
2464
2465 static void editmesh_build_data(CustomDataMask dataMask)
2466 {
2467         float min[3], max[3];
2468
2469         EditMesh *em = G.editMesh;
2470
2471         clear_mesh_caches(G.obedit);
2472
2473         if (em->derivedFinal) {
2474                 if (em->derivedFinal!=em->derivedCage) {
2475                         em->derivedFinal->needsFree = 1;
2476                         em->derivedFinal->release(em->derivedFinal);
2477                 }
2478                 em->derivedFinal = NULL;
2479         }
2480         if (em->derivedCage) {
2481                 em->derivedCage->needsFree = 1;
2482                 em->derivedCage->release(em->derivedCage);
2483                 em->derivedCage = NULL;
2484         }
2485
2486         editmesh_calc_modifiers(&em->derivedCage, &em->derivedFinal, dataMask);
2487         em->lastDataMask = dataMask;
2488
2489         INIT_MINMAX(min, max);
2490
2491         em->derivedFinal->getMinMax(em->derivedFinal, min, max);
2492
2493         if(!G.obedit->bb)
2494                 G.obedit->bb= MEM_callocN(sizeof(BoundBox), "bb");
2495         boundbox_set_from_min_max(G.obedit->bb, min, max);
2496
2497         em->derivedFinal->needsFree = 0;
2498         em->derivedCage->needsFree = 0;
2499 }
2500
2501 void makeDerivedMesh(Object *ob, CustomDataMask dataMask)
2502 {
2503         if (ob==G.obedit) {
2504                 editmesh_build_data(dataMask);
2505         } else {
2506                 mesh_build_data(ob, dataMask);
2507         }
2508 }
2509
2510 /***/
2511
2512 DerivedMesh *mesh_get_derived_final(Object *ob, CustomDataMask dataMask)
2513 {
2514         /* if there's no derived mesh or the last data mask used doesn't include
2515          * the data we need, rebuild the derived mesh
2516          */
2517         if(!ob->derivedFinal || (dataMask & ob->lastDataMask) != dataMask)
2518                 mesh_build_data(ob, dataMask);
2519
2520         return ob->derivedFinal;
2521 }
2522
2523 DerivedMesh *mesh_get_derived_deform(Object *ob, CustomDataMask dataMask)
2524 {
2525         /* if there's no derived mesh or the last data mask used doesn't include
2526          * the data we need, rebuild the derived mesh
2527          */
2528         if(!ob->derivedDeform || (dataMask & ob->lastDataMask) != dataMask)
2529                 mesh_build_data(ob, dataMask);
2530
2531         return ob->derivedDeform;
2532 }
2533
2534 /* Move to multires Pin level, returns a copy of the original vertex coords. */
2535 float *multires_render_pin(Object *ob, Mesh *me, int *orig_lvl)
2536 {
2537         float *vert_copy= NULL;
2538
2539         if(me->mr && !(me->mr->flag & MULTIRES_NO_RENDER)) {
2540                 MultiresLevel *lvl= NULL;
2541                 int i;
2542                 
2543                 /* Make sure all mesh edits are properly stored in the multires data*/
2544                 multires_update_levels(me, 1);
2545         
2546                 /* Copy the highest level of multires verts */
2547                 *orig_lvl= me->mr->current;
2548                 lvl= multires_level_n(me->mr, BLI_countlist(&me->mr->levels));
2549                 vert_copy= MEM_callocN(sizeof(float)*3*lvl->totvert, "multires vert_copy");
2550                 for(i=0; i<lvl->totvert; ++i)
2551                         VecCopyf(&vert_copy[i*3], me->mr->verts[i].co);
2552         
2553                 /* Goto the pin level for multires */
2554                 me->mr->newlvl= me->mr->pinlvl;
2555                 multires_set_level(ob, me, 1);
2556         }
2557         
2558         return vert_copy;
2559 }
2560
2561 /* Propagate the changes to render level - fails if mesh topology changed */
2562 void multires_render_final(Object *ob, Mesh *me, DerivedMesh **dm, float *vert_copy,
2563                            const int orig_lvl, CustomDataMask dataMask)
2564 {
2565         if(me->mr && !(me->mr->flag & MULTIRES_NO_RENDER)) {
2566                 if((*dm)->getNumVerts(*dm) == me->totvert &&
2567                    (*dm)->getNumFaces(*dm) == me->totface) {
2568                         MultiresLevel *lvl= multires_level_n(me->mr, BLI_countlist(&me->mr->levels));
2569                         DerivedMesh *old= NULL;
2570                         MVert *vertdup= NULL;
2571                         int i;
2572
2573                         /* Copy the verts into the mesh */
2574                         vertdup= (*dm)->dupVertArray(*dm);
2575                         (*dm)->release(*dm);
2576                         for(i=0; i<me->totvert; ++i)
2577                                 me->mvert[i]= vertdup[i];
2578                         /* Free vertdup after use*/
2579                         MEM_freeN(vertdup);
2580                         /* Go to the render level */
2581                         me->mr->newlvl= me->mr->renderlvl;
2582                         multires_set_level(ob, me, 1);
2583                         (*dm)= getMeshDerivedMesh(me, ob, NULL);
2584
2585                         /* Some of the data in dm is referenced externally, so make a copy */
2586                         old= *dm;
2587                         (*dm)= CDDM_copy(old);
2588                         old->release(old);
2589
2590                         if(dataMask & CD_MASK_ORCO)
2591                                 add_orco_dm(ob, *dm, NULL);
2592
2593                         /* Restore the original verts */
2594                         me->mr->newlvl= BLI_countlist(&me->mr->levels);
2595                         multires_set_level(ob, me, 1);
2596                         for(i=0; i<lvl->totvert; ++i)
2597                                 VecCopyf(me->mvert[i].co, &vert_copy[i*3]);
2598                 }
2599                 
2600                 if(vert_copy)
2601                         MEM_freeN(vert_copy);
2602                         
2603                 me->mr->newlvl= orig_lvl;
2604                 multires_set_level(ob, me, 1);
2605         }
2606 }
2607
2608 /* Multires note - if mesh has multires enabled, mesh is first set to the Pin level,
2609    where all modifiers are applied, then if the topology hasn't changed, the changes
2610    from modifiers are propagated up to the Render level. */
2611 DerivedMesh *mesh_create_derived_render(Object *ob, CustomDataMask dataMask)
2612 {
2613         DerivedMesh *final;
2614         Mesh *me= get_mesh(ob);
2615         float *vert_copy= NULL;
2616         int orig_lvl= 0;
2617         
2618         vert_copy= multires_render_pin(ob, me, &orig_lvl);
2619         mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1, 0, dataMask, -1);
2620         multires_render_final(ob, me, &final, vert_copy, orig_lvl, dataMask);
2621
2622         return final;
2623 }
2624
2625 DerivedMesh *mesh_create_derived_index_render(Object *ob, CustomDataMask dataMask, int index)
2626 {
2627         DerivedMesh *final;
2628         Mesh *me= get_mesh(ob);
2629         float *vert_copy= NULL;
2630         int orig_lvl= 0;
2631         
2632         vert_copy= multires_render_pin(ob, me, &orig_lvl);
2633         mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1, 0, dataMask, index);
2634         multires_render_final(ob, me, &final, vert_copy, orig_lvl, dataMask);
2635
2636         return final;
2637 }
2638
2639 DerivedMesh *mesh_create_derived_view(Object *ob, CustomDataMask dataMask)
2640 {
2641         DerivedMesh *final;
2642
2643         mesh_calc_modifiers(ob, NULL, NULL, &final, 0, 1, 0, dataMask, -1);
2644
2645         return final;
2646 }
2647
2648 DerivedMesh *mesh_create_derived_no_deform(Object *ob, float (*vertCos)[3],
2649                                            CustomDataMask dataMask)
2650 {
2651         DerivedMesh *final;
2652         
2653         mesh_calc_modifiers(ob, vertCos, NULL, &final, 0, 0, 0, dataMask, -1);
2654
2655         return final;
2656 }
2657
2658 DerivedMesh *mesh_create_derived_no_deform_render(Object *ob,
2659                                                   float (*vertCos)[3],
2660                                                   CustomDataMask dataMask)
2661 {
2662         DerivedMesh *final;
2663         Mesh *me= get_mesh(ob);
2664         float *vert_copy= NULL;
2665         int orig_lvl= 0;
2666
2667         vert_copy= multires_render_pin(ob, me, &orig_lvl);
2668         mesh_calc_modifiers(ob, vertCos, NULL, &final, 1, 0, 0, dataMask, -1);
2669         multires_render_final(ob, me, &final, vert_copy, orig_lvl, dataMask);
2670
2671         return final;
2672 }
2673
2674 /***/
2675
2676 DerivedMesh *editmesh_get_derived_cage_and_final(DerivedMesh **final_r,
2677                                                  CustomDataMask dataMask)
2678 {
2679         /* if there's no derived mesh or the last data mask used doesn't include
2680          * the data we need, rebuild the derived mesh
2681          */
2682         if(!G.editMesh->derivedCage ||
2683            (G.editMesh->lastDataMask & dataMask) != dataMask)
2684                 editmesh_build_data(dataMask);
2685
2686         *final_r = G.editMesh->derivedFinal;
2687         return G.editMesh->derivedCage;
2688 }
2689
2690 DerivedMesh *editmesh_get_derived_cage(CustomDataMask dataMask)
2691 {
2692         /* if there's no derived mesh or the last data mask used doesn't include
2693          * the data we need, rebuild the derived mesh
2694          */
2695         if(!G.editMesh->derivedCage ||
2696            (G.editMesh->lastDataMask & dataMask) != dataMask)
2697                 editmesh_build_data(dataMask);
2698
2699         return G.editMesh->derivedCage;
2700 }
2701
2702 DerivedMesh *editmesh_get_derived_base(void)
2703 {
2704         return getEditMeshDerivedMesh(G.editMesh, G.obedit, NULL);
2705 }
2706
2707
2708 /* ********* For those who don't grasp derived stuff! (ton) :) *************** */
2709
2710 static void make_vertexcosnos__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
2711 {
2712         float *vec = userData;
2713         
2714         vec+= 6*index;
2715
2716         /* check if we've been here before (normal should not be 0) */
2717         if(vec[3] || vec[4] || vec[5]) return;
2718
2719         VECCOPY(vec, co);
2720         vec+= 3;
2721         if(no_f) {
2722                 VECCOPY(vec, no_f);
2723         }
2724         else {
2725                 VECCOPY(vec, no_s);
2726         }
2727 }
2728
2729 /* always returns original amount me->totvert of vertices and normals, but fully deformed and subsurfered */
2730 /* this is needed for all code using vertexgroups (no subsurf support) */
2731 /* it stores the normals as floats, but they can still be scaled as shorts (32767 = unit) */
2732 /* in use now by vertex/weight paint and particle generating */
2733
2734 float *mesh_get_mapped_verts_nors(Object *ob)
2735 {
2736         Mesh *me= ob->data;
2737         DerivedMesh *dm;
2738         float *vertexcosnos;
2739         
2740         /* lets prevent crashing... */
2741         if(ob->type!=OB_MESH || me->totvert==0)
2742                 return NULL;
2743         
2744         dm= mesh_get_derived_final(ob, CD_MASK_BAREMESH);
2745         vertexcosnos= MEM_callocN(6*sizeof(float)*me->totvert, "vertexcosnos map");
2746         
2747         if(dm->foreachMappedVert) {
2748                 dm->foreachMappedVert(dm, make_vertexcosnos__mapFunc, vertexcosnos);
2749         }
2750         else {
2751                 float *fp= vertexcosnos;
2752                 int a;
2753                 
2754                 for(a=0; a< me->totvert; a++, fp+=6) {
2755                         dm->getVertCo(dm, a, fp);
2756                         dm->getVertNo(dm, a, fp+3);
2757                 }
2758         }
2759         
2760         dm->release(dm);
2761         return vertexcosnos;
2762 }
2763
2764 /* ********* crazyspace *************** */
2765
2766 int editmesh_get_first_deform_matrices(float (**deformmats)[3][3], float (**deformcos)[3])
2767 {
2768         Object *ob = G.obedit;
2769         EditMesh *em = G.editMesh;
2770         ModifierData *md;
2771         DerivedMesh *dm;
2772         int i, a, numleft = 0, numVerts = 0;
2773         int cageIndex = modifiers_getCageIndex(ob, NULL);
2774         float (*defmats)[3][3] = NULL, (*deformedVerts)[3] = NULL;
2775
2776         modifiers_clearErrors(ob);
2777
2778         dm = NULL;
2779         md = ob->modifiers.first;
2780
2781         /* compute the deformation matrices and coordinates for the first
2782            modifiers with on cage editing that are enabled and support computing
2783            deform matrices */
2784         for(i = 0; md && i <= cageIndex; i++, md = md->next) {
2785                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
2786
2787                 if(!editmesh_modifier_is_enabled(md, dm))
2788                         continue;
2789
2790                 if(mti->type==eModifierTypeType_OnlyDeform && mti->deformMatricesEM) {
2791                         if(!defmats) {
2792                                 dm= getEditMeshDerivedMesh(em, ob, NULL);
2793                                 deformedVerts= editmesh_getVertexCos(em, &numVerts);
2794                                 defmats= MEM_callocN(sizeof(*defmats)*numVerts, "defmats");
2795
2796                                 for(a=0; a<numVerts; a++)
2797                                         Mat3One(defmats[a]);
2798                         }
2799
2800                         mti->deformMatricesEM(md, ob, em, dm, deformedVerts, defmats,
2801                                 numVerts);
2802                 }
2803                 else
2804                         break;
2805         }
2806
2807         for(; md && i <= cageIndex; md = md->next, i++)
2808                 if(editmesh_modifier_is_enabled(md, dm) && modifier_isDeformer(md))
2809                         numleft++;
2810
2811         if(dm)
2812                 dm->release(dm);
2813         
2814         *deformmats= defmats;
2815         *deformcos= deformedVerts;
2816
2817         return numleft;
2818 }
2819
2820 void DM_add_tangent_layer(DerivedMesh *dm)
2821 {
2822         /* mesh vars */
2823         MTFace *mtface, *tf;
2824         MFace *mface, *mf;
2825         MVert *mvert, *v1, *v2, *v3, *v4;
2826         MemArena *arena= NULL;
2827         VertexTangent **vtangents= NULL;
2828         float (*orco)[3]= NULL, (*tangent)[3];
2829         float *uv1, *uv2, *uv3, *uv4, *vtang;
2830         float fno[3], tang[3], uv[4][2];
2831         int i, j, len, mf_vi[4], totvert, totface;
2832
2833         if(CustomData_get_layer_index(&dm->faceData, CD_TANGENT) != -1)
2834                 return;
2835
2836         /* check we have all the needed layers */
2837         totvert= dm->getNumVerts(dm);
2838         totface= dm->getNumFaces(dm);
2839
2840         mvert= dm->getVertArray(dm);
2841         mface= dm->getFaceArray(dm);
2842         mtface= dm->getFaceDataArray(dm, CD_MTFACE);
2843
2844         if(!mtface) {
2845                 orco= dm->getVertDataArray(dm, CD_ORCO);
2846                 if(!orco)
2847                         return;
2848         }
2849         
2850         /* create tangent layer */
2851         DM_add_face_layer(dm, CD_TANGENT, CD_CALLOC, NULL);
2852         tangent= DM_get_face_data_layer(dm, CD_TANGENT);
2853         
2854         /* allocate some space */
2855         arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
2856         BLI_memarena_use_calloc(arena);
2857         vtangents= MEM_callocN(sizeof(VertexTangent*)*totvert, "VertexTangent");
2858         
2859         /* sum tangents at connected vertices */
2860         for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++) {
2861                 v1= &mvert[mf->v1];
2862                 v2= &mvert[mf->v2];
2863                 v3= &mvert[mf->v3];
2864
2865                 if (mf->v4) {
2866                         v4= &mvert[mf->v4];
2867                         CalcNormFloat4(v4->co, v3->co, v2->co, v1->co, fno);
2868                 }
2869                 else {
2870                         v4= NULL;
2871                         CalcNormFloat(v3->co, v2->co, v1->co, fno);
2872                 }
2873                 
2874                 if(mtface) {
2875                         uv1= tf->uv[0];
2876                         uv2= tf->uv[1];
2877                         uv3= tf->uv[2];
2878                         uv4= tf->uv[3];
2879                 }
2880                 else {
2881                         uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
2882                         spheremap(orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2], &uv[0][0], &uv[0][1]);
2883                         spheremap(orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2], &uv[1][0], &uv[1][1]);
2884                         spheremap(orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2], &uv[2][0], &uv[2][1]);
2885                         if(v4)
2886                                 spheremap(orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2], &uv[3][0], &uv[3][1]);
2887                 }
2888                 
2889                 tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, fno, tang);
2890                 sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
2891                 sum_or_add_vertex_tangent(arena, &vtangents[mf->v2], tang, uv2);
2892                 sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
2893                 
2894                 if(mf->v4) {
2895                         v4= &mvert[mf->v4];
2896                         
2897                         tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, fno, tang);
2898                         sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1);
2899                         sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3);
2900                         sum_or_add_vertex_tangent(arena, &vtangents[mf->v4], tang, uv4);
2901                 }
2902         }
2903         
2904         /* write tangent to layer */
2905         for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++, tangent+=4) {
2906                 len= (mf->v4)? 4 : 3; 
2907                 
2908                 if(mtface) {
2909                         uv1= tf->uv[0];
2910                         uv2= tf->uv[1];
2911                         uv3= tf->uv[2];
2912                         uv4= tf->uv[3];
2913                 }
2914                 else {
2915                         uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
2916                         spheremap(orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2], &uv[0][0], &uv[0][1]);
2917                         spheremap(orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2], &uv[1][0], &uv[1][1]);
2918                         spheremap(orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2], &uv[2][0], &uv[2][1]);
2919                         if(len==4)
2920                                 spheremap(orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2], &uv[3][0], &uv[3][1]);
2921                 }
2922                 
2923                 mf_vi[0]= mf->v1;
2924                 mf_vi[1]= mf->v2;
2925                 mf_vi[2]= mf->v3;
2926                 mf_vi[3]= mf->v4;
2927                 
2928                 for(j=0; j<len; j++) {
2929                         vtang= find_vertex_tangent(vtangents[mf_vi[j]], mtface ? tf->uv[j] : uv[j]);
2930
2931                         VECCOPY(tangent[j], vtang);
2932                         Normalize(tangent[j]);
2933                 }
2934         }
2935         
2936         BLI_memarena_free(arena);
2937         MEM_freeN(vtangents);
2938 }
2939
2940