- added modifiers_getVirtualModifierList, returns pointer to first modifier
[blender.git] / source / blender / blenkernel / intern / DerivedMesh.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2005 Blender Foundation.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include "PIL_time.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_effect_types.h"
42 #include "DNA_mesh_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
48 #include "BLI_arithb.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_editVert.h"
51
52 #include "BKE_utildefines.h"
53 #include "BKE_DerivedMesh.h"
54 #include "BKE_displist.h"
55 #include "BKE_effect.h"
56 #include "BKE_global.h"
57 #include "BKE_material.h"
58 #include "BKE_mesh.h"
59 #include "BKE_object.h"
60 #include "BKE_subsurf.h"
61 #include "BKE_deform.h"
62 #include "BKE_modifier.h"
63 #include "BKE_key.h"
64
65 #include "BIF_gl.h"
66 #include "BIF_glutil.h"
67
68 ///////////////////////////////////
69 ///////////////////////////////////
70
71 typedef struct {
72         DerivedMesh dm;
73
74         Object *ob;
75         Mesh *me;
76         MVert *verts;
77         float *nors;
78
79         int freeNors, freeVerts;
80 } MeshDerivedMesh;
81
82 static DispListMesh *meshDM_convertToDispListMesh(DerivedMesh *dm, int allowShared)
83 {
84         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
85         Mesh *me = mdm->me;
86         DispListMesh *dlm = MEM_callocN(sizeof(*dlm), "dlm");
87
88         dlm->totvert = me->totvert;
89         dlm->totedge = me->totedge;
90         dlm->totface = me->totface;
91         dlm->mvert = mdm->verts;
92         dlm->medge = me->medge;
93         dlm->mface = me->mface;
94         dlm->tface = me->tface;
95         dlm->mcol = me->mcol;
96         dlm->nors = mdm->nors;
97         dlm->dontFreeVerts = dlm->dontFreeOther = dlm->dontFreeNors = 1;
98
99         if (!allowShared) {
100                 dlm->mvert = MEM_dupallocN(dlm->mvert);
101                 if (dlm->medge) dlm->medge = MEM_dupallocN(dlm->medge);
102                 dlm->mface = MEM_dupallocN(dlm->mface);
103                 if (dlm->tface) dlm->tface = MEM_dupallocN(dlm->tface);
104                 if (dlm->mcol) dlm->mcol = MEM_dupallocN(dlm->mcol);
105                 if (dlm->nors) dlm->nors = MEM_dupallocN(dlm->nors);
106
107                 dlm->dontFreeVerts = dlm->dontFreeOther = dlm->dontFreeNors = 0;
108         }
109
110         return dlm;
111 }
112
113 static void meshDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
114 {
115         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
116         Mesh *me = mdm->me;
117         int i;
118
119         if (me->totvert) {
120                 for (i=0; i<me->totvert; i++) {
121                         DO_MINMAX(mdm->verts[i].co, min_r, max_r);
122                 }
123         } else {
124                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
125         }
126 }
127
128 static void meshDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
129 {
130         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
131         Mesh *me = mdm->me;
132         int i;
133
134         for (i=0; i<me->totvert; i++) {
135                 cos_r[i][0] = mdm->verts[i].co[0];
136                 cos_r[i][1] = mdm->verts[i].co[1];
137                 cos_r[i][2] = mdm->verts[i].co[2];
138         }
139 }
140
141 static void meshDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
142 {
143         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
144
145         VECCOPY(co_r, mdm->verts[index].co);
146 }
147
148 static void meshDM_getVertNo(DerivedMesh *dm, int index, float no_r[3])
149 {
150         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
151         short *no = mdm->verts[index].no;
152
153         no_r[0] = no[0]/32767.f;
154         no_r[1] = no[1]/32767.f;
155         no_r[2] = no[2]/32767.f;
156 }
157
158 static void meshDM_drawVerts(DerivedMesh *dm)
159 {
160         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
161         Mesh *me = mdm->me;
162         int a;
163
164         glBegin(GL_POINTS);
165         for(a=0; a<me->totvert; a++) {
166                 glVertex3fv(mdm->verts[ a].co);
167         }
168         glEnd();
169 }
170 static void meshDM_drawEdges(DerivedMesh *dm)
171 {
172         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
173         Mesh *me= mdm->me;
174         int a;
175         MFace *mface = me->mface;
176
177         if(me->medge) {
178                 MEdge *medge= me->medge;
179                 
180                 glBegin(GL_LINES);
181                 for(a=me->totedge; a>0; a--, medge++) {
182                         if(medge->flag & ME_EDGEDRAW) {
183                                 glVertex3fv(mdm->verts[ medge->v1].co);
184                                 glVertex3fv(mdm->verts[ medge->v2].co);
185                         }
186                 }
187                 glEnd();
188         }
189         else {
190                 glBegin(GL_LINES);
191                 for(a=0; a<me->totface; a++, mface++) {
192                         int test= mface->edcode;
193                         
194                         if(test) {
195                                 if(test&ME_V1V2){
196                                         glVertex3fv(mdm->verts[mface->v1].co);
197                                         glVertex3fv(mdm->verts[mface->v2].co);
198                                 }
199
200                                 if(mface->v3) {
201                                         if(test&ME_V2V3){
202                                                 glVertex3fv(mdm->verts[mface->v2].co);
203                                                 glVertex3fv(mdm->verts[mface->v3].co);
204                                         }
205
206                                         if (mface->v4) {
207                                                 if(test&ME_V3V4){
208                                                         glVertex3fv(mdm->verts[mface->v3].co);
209                                                         glVertex3fv(mdm->verts[mface->v4].co);
210                                                 }
211                                                 if(test&ME_V4V1){
212                                                         glVertex3fv(mdm->verts[mface->v4].co);
213                                                         glVertex3fv(mdm->verts[mface->v1].co);
214                                                 }
215                                         } else {
216                                                 if(test&ME_V3V1){
217                                                         glVertex3fv(mdm->verts[mface->v3].co);
218                                                         glVertex3fv(mdm->verts[mface->v1].co);
219                                                 }
220                                         }
221                                 }
222                         }
223                 }
224                 glEnd();
225         }
226 }
227 static void meshDM_drawLooseEdges(DerivedMesh *dm)
228 {
229         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
230         Mesh *me = mdm->me;
231         MFace *mface= me->mface;
232         int a;
233
234         glBegin(GL_LINES);
235         for(a=0; a<me->totface; a++, mface++) {
236                 if(!mface->v3) {
237                         glVertex3fv(mdm->verts[mface->v3].co);
238                         glVertex3fv(mdm->verts[mface->v4].co);
239                 } 
240         }
241         glEnd();
242 }
243 static void meshDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
244 {
245         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
246         Mesh *me = mdm->me;
247         MVert *mvert= mdm->verts;
248         MFace *mface= me->mface;
249         float *nors = mdm->nors;
250         int a;
251         int glmode=-1, shademodel=-1, matnr=-1, drawCurrentMat=1;
252
253 #define PASSVERT(index) {                                               \
254         if (shademodel==GL_SMOOTH) {                            \
255                 short *no = mvert[index].no;                    \
256                 glNormal3sv(no);                                                \
257         }                                                                                       \
258         glVertex3fv(mvert[index].co);   \
259 }
260
261         glBegin(glmode=GL_QUADS);
262         for(a=0; a<me->totface; a++, mface++, nors+=3) {
263                 if(mface->v3) {
264                         int new_glmode, new_matnr, new_shademodel;
265                                 
266                         new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
267                         new_matnr = mface->mat_nr+1;
268                         new_shademodel = (!(me->flag&ME_AUTOSMOOTH) && (mface->flag & ME_SMOOTH))?GL_SMOOTH:GL_FLAT;
269                         
270                         if (new_glmode!=glmode || new_matnr!=matnr || new_shademodel!=shademodel) {
271                                 glEnd();
272
273                                 drawCurrentMat = setMaterial(matnr=new_matnr);
274
275                                 glShadeModel(shademodel=new_shademodel);
276                                 glBegin(glmode=new_glmode);
277                         } 
278                         
279                         if (drawCurrentMat) {
280                                 if(shademodel==GL_FLAT) 
281                                         glNormal3fv(nors);
282
283                                 PASSVERT(mface->v1);
284                                 PASSVERT(mface->v2);
285                                 PASSVERT(mface->v3);
286                                 if (mface->v4) {
287                                         PASSVERT(mface->v4);
288                                 }
289                         }
290                 }
291         }
292         glEnd();
293
294         glShadeModel(GL_FLAT);
295 #undef PASSVERT
296 }
297
298 static void meshDM_drawFacesColored(DerivedMesh *dm, int useTwoSide, unsigned char *col1, unsigned char *col2)
299 {
300         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
301         Mesh *me= mdm->me;
302         MFace *mface= me->mface;
303         int a, glmode;
304         unsigned char *cp1, *cp2;
305
306         cp1= col1;
307         if(col2) {
308                 cp2= col2;
309         } else {
310                 cp2= NULL;
311                 useTwoSide= 0;
312         }
313
314         /* there's a conflict here... twosided colors versus culling...? */
315         /* defined by history, only texture faces have culling option */
316         /* we need that as mesh option builtin, next to double sided lighting */
317         if(col1 && col2)
318                 glEnable(GL_CULL_FACE);
319         
320         glShadeModel(GL_SMOOTH);
321         glBegin(glmode=GL_QUADS);
322         for(a=0; a<me->totface; a++, mface++, cp1+= 16) {
323                 if(mface->v3) {
324                         int new_glmode= mface->v4?GL_QUADS:GL_TRIANGLES;
325
326                         if (new_glmode!=glmode) {
327                                 glEnd();
328                                 glBegin(glmode= new_glmode);
329                         }
330                                 
331                         glColor3ub(cp1[3], cp1[2], cp1[1]);
332                         glVertex3fv( mdm->verts[mface->v1].co );
333                         glColor3ub(cp1[7], cp1[6], cp1[5]);
334                         glVertex3fv( mdm->verts[mface->v2].co );
335                         glColor3ub(cp1[11], cp1[10], cp1[9]);
336                         glVertex3fv( mdm->verts[mface->v3].co );
337                         if(mface->v4) {
338                                 glColor3ub(cp1[15], cp1[14], cp1[13]);
339                                 glVertex3fv( mdm->verts[mface->v4].co );
340                         }
341                                 
342                         if(useTwoSide) {
343                                 glColor3ub(cp2[11], cp2[10], cp2[9]);
344                                 glVertex3fv( mdm->verts[mface->v3].co );
345                                 glColor3ub(cp2[7], cp2[6], cp2[5]);
346                                 glVertex3fv( mdm->verts[mface->v2].co );
347                                 glColor3ub(cp2[3], cp2[2], cp2[1]);
348                                 glVertex3fv( mdm->verts[mface->v1].co );
349                                 if(mface->v4) {
350                                         glColor3ub(cp2[15], cp2[14], cp2[13]);
351                                         glVertex3fv( mdm->verts[mface->v4].co );
352                                 }
353                         }
354                 }
355                 if(col2) cp2+= 16;
356         }
357         glEnd();
358
359         glShadeModel(GL_FLAT);
360         glDisable(GL_CULL_FACE);
361 }
362
363 static void meshDM_drawFacesTex(DerivedMesh *dm, int (*setDrawParams)(TFace *tf, int matnr)) 
364 {
365         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
366         Mesh *me = mdm->me;
367         MVert *mvert= mdm->verts;
368         MFace *mface= me->mface;
369         TFace *tface = me->tface;
370         float *nors = mdm->nors;
371         int a;
372
373         for (a=0; a<me->totface; a++) {
374                 MFace *mf= &mface[a];
375                 TFace *tf = tface?&tface[a]:NULL;
376                 unsigned char *cp= NULL;
377                 
378                 if(mf->v3==0) continue;
379                 if(tf && ((tf->flag&TF_HIDE) || (tf->mode&TF_INVISIBLE))) continue;
380
381                 if (setDrawParams(tf, mf->mat_nr)) {
382                         if (tf) {
383                                 cp= (unsigned char *) tf->col;
384                         } else if (me->mcol) {
385                                 cp= (unsigned char *) &me->mcol[a*4];
386                         }
387                 }
388
389                 if (!(mf->flag&ME_SMOOTH)) {
390                         glNormal3fv(&nors[a*3]);
391                 }
392
393                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
394                 if (tf) glTexCoord2fv(tf->uv[0]);
395                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
396                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v1].no);
397                 glVertex3fv(mvert[mf->v1].co);
398                         
399                 if (tf) glTexCoord2fv(tf->uv[1]);
400                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
401                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v2].no);
402                 glVertex3fv(mvert[mf->v2].co);
403
404                 if (tf) glTexCoord2fv(tf->uv[2]);
405                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
406                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v3].no);
407                 glVertex3fv(mvert[mf->v3].co);
408
409                 if(mf->v4) {
410                         if (tf) glTexCoord2fv(tf->uv[3]);
411                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
412                         if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v4].no);
413                         glVertex3fv(mvert[mf->v4].co);
414                 }
415                 glEnd();
416         }
417 }
418 static int meshDM_getNumVerts(DerivedMesh *dm)
419 {
420         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
421         Mesh *me = mdm->me;
422
423         return me->totvert;
424 }
425 static int meshDM_getNumFaces(DerivedMesh *dm)
426 {
427         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
428         Mesh *me = mdm->me;
429
430         return me->totface;
431 }
432
433 static void meshDM_release(DerivedMesh *dm)
434 {
435         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
436
437         if (mdm->freeNors) MEM_freeN(mdm->nors);
438         if (mdm->freeVerts) MEM_freeN(mdm->verts);
439         MEM_freeN(mdm);
440 }
441
442 static DerivedMesh *getMeshDerivedMesh(Mesh *me, Object *ob, float (*vertCos)[3])
443 {
444         MeshDerivedMesh *mdm = MEM_callocN(sizeof(*mdm), "mdm");
445
446         mdm->dm.getMinMax = meshDM_getMinMax;
447
448         mdm->dm.convertToDispListMesh = meshDM_convertToDispListMesh;
449         mdm->dm.getNumVerts = meshDM_getNumVerts;
450         mdm->dm.getNumFaces = meshDM_getNumFaces;
451
452         mdm->dm.getVertCos = meshDM_getVertCos;
453         mdm->dm.getVertCo = meshDM_getVertCo;
454         mdm->dm.getVertNo = meshDM_getVertNo;
455
456         mdm->dm.drawVerts = meshDM_drawVerts;
457
458         mdm->dm.drawEdges = meshDM_drawEdges;
459         mdm->dm.drawMappedEdges = meshDM_drawEdges;
460         mdm->dm.drawLooseEdges = meshDM_drawLooseEdges;
461
462         mdm->dm.drawFacesSolid = meshDM_drawFacesSolid;
463         mdm->dm.drawFacesColored = meshDM_drawFacesColored;
464         mdm->dm.drawFacesTex = meshDM_drawFacesTex;
465
466         mdm->dm.release = meshDM_release;
467         
468         mdm->ob = ob;
469         mdm->me = me;
470         mdm->verts = me->mvert;
471         mdm->nors = NULL;
472         mdm->freeNors = 0;
473         mdm->freeVerts = 0;
474
475         if (vertCos) {
476                 int i;
477
478                 mdm->verts = MEM_mallocN(sizeof(*mdm->verts)*me->totvert, "deformedVerts");
479                 for (i=0; i<me->totvert; i++) {
480                         VECCOPY(mdm->verts[i].co, vertCos[i]);
481                 }
482                 mesh_calc_normals(mdm->verts, me->totvert, me->mface, me->totface, &mdm->nors);
483                 mdm->freeNors = 1;
484                 mdm->freeVerts = 1;
485         } else {
486                         // XXX this is kinda hacky because we shouldn't really be editing
487                         // the mesh here, however, we can't just call mesh_build_faceNormals(ob)
488                         // because in the case when a key is applied to a mesh the vertex normals
489                         // would never be correctly computed.
490                 mesh_calc_normals(mdm->verts, me->totvert, me->mface, me->totface, &mdm->nors);
491                 mdm->freeNors = 1;
492         }
493
494         return (DerivedMesh*) mdm;
495 }
496
497 ///
498
499 typedef struct {
500         DerivedMesh dm;
501
502         EditMesh *em;
503         float (*vertexCos)[3];
504         float (*vertexNos)[3];
505         float (*faceNos)[3];
506 } EditMeshDerivedMesh;
507
508 static void emDM_foreachMappedVertEM(DerivedMesh *dm, void (*func)(void *userData, EditVert *vert, float *co, float *no_f, short *no_s), void *userData)
509 {
510         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
511         EditVert *eve;
512
513         if (emdm->vertexCos) {
514                 int i;
515
516                 for (i=0,eve= emdm->em->verts.first; eve; i++,eve=eve->next) {
517                         func(userData, eve, emdm->vertexCos[i], emdm->vertexNos[i], NULL);
518                 }
519         } else {
520                 for (eve= emdm->em->verts.first; eve; eve=eve->next) {
521                         func(userData, eve, eve->co, eve->no, NULL);
522                 }
523         }
524 }
525 static void emDM_foreachMappedEdgeEM(DerivedMesh *dm, void (*func)(void *userData, EditEdge *eed, float *v0co, float *v1co), void *userData)
526 {
527         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
528         EditEdge *eed;
529
530         if (emdm->vertexCos) {
531                 EditVert *eve, *preveve;
532                 int i;
533
534                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
535                         eve->prev = (EditVert*) i++;
536                 for(eed= emdm->em->edges.first; eed; eed= eed->next)
537                         func(userData, eed, emdm->vertexCos[(int) eed->v1->prev], emdm->vertexCos[(int) eed->v2->prev]);
538                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
539                         eve->prev = preveve;
540         } else {
541                 for(eed= emdm->em->edges.first; eed; eed= eed->next)
542                         func(userData, eed, eed->v1->co, eed->v2->co);
543         }
544 }
545 static void emDM_drawMappedEdgesEM(DerivedMesh *dm, int (*setDrawOptions)(void *userData, EditEdge *edge), void *userData) 
546 {
547         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
548         EditEdge *eed;
549
550         if (emdm->vertexCos) {
551                 EditVert *eve, *preveve;
552                 int i;
553
554                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
555                         eve->prev = (EditVert*) i++;
556
557                 glBegin(GL_LINES);
558                 for(eed= emdm->em->edges.first; eed; eed= eed->next) {
559                         if(!setDrawOptions || setDrawOptions(userData, eed)) {
560                                 glVertex3fv(emdm->vertexCos[(int) eed->v1->prev]);
561                                 glVertex3fv(emdm->vertexCos[(int) eed->v2->prev]);
562                         }
563                 }
564                 glEnd();
565
566                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
567                         eve->prev = preveve;
568         } else {
569                 glBegin(GL_LINES);
570                 for(eed= emdm->em->edges.first; eed; eed= eed->next) {
571                         if(!setDrawOptions || setDrawOptions(userData, eed)) {
572                                 glVertex3fv(eed->v1->co);
573                                 glVertex3fv(eed->v2->co);
574                         }
575                 }
576                 glEnd();
577         }
578 }
579 static void emDM_drawEdges(DerivedMesh *dm)
580 {
581         emDM_drawMappedEdgesEM(dm, NULL, NULL);
582 }
583 static void emDM_drawMappedEdgesInterpEM(DerivedMesh *dm, int (*setDrawOptions)(void *userData, EditEdge *edge), void (*setDrawInterpOptions)(void *userData, EditEdge *edge, float t), void *userData) 
584 {
585         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
586         EditEdge *eed;
587
588         if (emdm->vertexCos) {
589                 EditVert *eve, *preveve;
590                 int i;
591
592                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
593                         eve->prev = (EditVert*) i++;
594
595                 glBegin(GL_LINES);
596                 for(eed= emdm->em->edges.first; eed; eed= eed->next) {
597                         if(!setDrawOptions || setDrawOptions(userData, eed)) {
598                                 setDrawInterpOptions(userData, eed, 0.0);
599                                 glVertex3fv(emdm->vertexCos[(int) eed->v1->prev]);
600                                 setDrawInterpOptions(userData, eed, 1.0);
601                                 glVertex3fv(emdm->vertexCos[(int) eed->v2->prev]);
602                         }
603                 }
604                 glEnd();
605
606                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
607                         eve->prev = preveve;
608         } else {
609                 glBegin(GL_LINES);
610                 for(eed= emdm->em->edges.first; eed; eed= eed->next) {
611                         if(!setDrawOptions || setDrawOptions(userData, eed)) {
612                                 setDrawInterpOptions(userData, eed, 0.0);
613                                 glVertex3fv(eed->v1->co);
614                                 setDrawInterpOptions(userData, eed, 1.0);
615                                 glVertex3fv(eed->v2->co);
616                         }
617                 }
618                 glEnd();
619         }
620 }
621 static void emDM__calcFaceCent(EditFace *efa, float cent[3], float (*vertexCos)[3])
622 {
623         if (vertexCos) {
624                 VECCOPY(cent, vertexCos[(int) efa->v1->prev]);
625                 VecAddf(cent, cent, vertexCos[(int) efa->v2->prev]);
626                 VecAddf(cent, cent, vertexCos[(int) efa->v3->prev]);
627                 if (efa->v4) VecAddf(cent, cent, vertexCos[(int) efa->v4->prev]);
628         } else {
629                 VECCOPY(cent, efa->v1->co);
630                 VecAddf(cent, cent, efa->v2->co);
631                 VecAddf(cent, cent, efa->v3->co);
632                 if (efa->v4) VecAddf(cent, cent, efa->v4->co);
633         }
634
635         if (efa->v4) {
636                 VecMulf(cent, 0.25f);
637         } else {
638                 VecMulf(cent, 0.33333333333f);
639         }
640 }
641 static void emDM_foreachMappedFaceCenterEM(DerivedMesh *dm, void (*func)(void *userData, EditFace *efa, float *co, float *no), void *userData)
642 {
643         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
644         EditVert *eve, *preveve;
645         EditFace *efa;
646         float cent[3];
647         int i;
648
649         if (emdm->vertexCos) {
650                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
651                         eve->prev = (EditVert*) i++;
652         }
653
654         for(efa= emdm->em->faces.first; efa; efa= efa->next) {
655                 emDM__calcFaceCent(efa, cent, emdm->vertexCos);
656                 func(userData, efa, cent, emdm->vertexCos?emdm->faceNos[i]:efa->n);
657         }
658
659         if (emdm->vertexCos) {
660                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
661                         eve->prev = preveve;
662         }
663 }
664 static void emDM_drawMappedFacesEM(DerivedMesh *dm, int (*setDrawOptions)(void *userData, EditFace *face), void *userData)
665 {
666         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
667         EditFace *efa;
668
669         if (emdm->vertexCos) {
670                 EditVert *eve, *preveve;
671                 int i;
672
673                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
674                         eve->prev = (EditVert*) i++;
675
676                 for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
677                         if(!setDrawOptions || setDrawOptions(userData, efa)) {
678                                 glNormal3fv(emdm->faceNos[i]);
679                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
680                                 glVertex3fv(emdm->vertexCos[(int) efa->v1->prev]);
681                                 glVertex3fv(emdm->vertexCos[(int) efa->v2->prev]);
682                                 glVertex3fv(emdm->vertexCos[(int) efa->v3->prev]);
683                                 if(efa->v4) glVertex3fv(emdm->vertexCos[(int) efa->v4->prev]);
684                                 glEnd();
685                         }
686                 }
687
688                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
689                         eve->prev = preveve;
690         } else {
691                 for (efa= emdm->em->faces.first; efa; efa= efa->next) {
692                         if(!setDrawOptions || setDrawOptions(userData, efa)) {
693                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
694                                 glVertex3fv(efa->v1->co);
695                                 glVertex3fv(efa->v2->co);
696                                 glVertex3fv(efa->v3->co);
697                                 if(efa->v4) glVertex3fv(efa->v4->co);
698                                 glEnd();
699                         }
700                 }
701         }
702 }
703 static void emDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
704 {
705         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
706         EditFace *efa;
707
708         if (emdm->vertexCos) {
709                 EditVert *eve, *preveve;
710                 int i;
711
712                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
713                         eve->prev = (EditVert*) i++;
714
715                 for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
716                         if(efa->h==0) {
717                                 if (setMaterial(efa->mat_nr+1)) {
718                                         glNormal3fv(emdm->faceNos[i]);
719                                         glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
720                                         glVertex3fv(emdm->vertexCos[(int) efa->v1->prev]);
721                                         glVertex3fv(emdm->vertexCos[(int) efa->v2->prev]);
722                                         glVertex3fv(emdm->vertexCos[(int) efa->v3->prev]);
723                                         if(efa->v4) glVertex3fv(emdm->vertexCos[(int) efa->v4->prev]);
724                                         glEnd();
725                                 }
726                         }
727                 }
728
729                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
730                         eve->prev = preveve;
731         } else {
732                 for (efa= emdm->em->faces.first; efa; efa= efa->next) {
733                         if(efa->h==0) {
734                                 if (setMaterial(efa->mat_nr+1)) {
735                                         glNormal3fv(efa->n);
736                                         glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
737                                         glVertex3fv(efa->v1->co);
738                                         glVertex3fv(efa->v2->co);
739                                         glVertex3fv(efa->v3->co);
740                                         if(efa->v4) glVertex3fv(efa->v4->co);
741                                         glEnd();
742                                 }
743                         }
744                 }
745         }
746 }
747
748 static void emDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
749 {
750         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
751         EditVert *eve;
752         int i;
753
754         if (emdm->em->verts.first) {
755                 for (i=0,eve= emdm->em->verts.first; eve; i++,eve= eve->next) {
756                         if (emdm->vertexCos) {
757                                 DO_MINMAX(emdm->vertexCos[i], min_r, max_r);
758                         } else {
759                                 DO_MINMAX(eve->co, min_r, max_r);
760                         }
761                 }
762         } else {
763                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
764         }
765 }
766 static int emDM_getNumVerts(DerivedMesh *dm)
767 {
768         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
769
770         return BLI_countlist(&emdm->em->verts);
771 }
772 static int emDM_getNumFaces(DerivedMesh *dm)
773 {
774         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
775
776         return BLI_countlist(&emdm->em->faces);
777 }
778
779 static void emDM_release(DerivedMesh *dm)
780 {
781         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
782
783         if (emdm->vertexCos) {
784                 MEM_freeN(emdm->vertexCos);
785                 MEM_freeN(emdm->vertexNos);
786                 MEM_freeN(emdm->faceNos);
787         }
788
789         MEM_freeN(emdm);
790 }
791
792 static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, float (*vertexCos)[3])
793 {
794         EditMeshDerivedMesh *emdm = MEM_callocN(sizeof(*emdm), "emdm");
795
796         emdm->dm.getMinMax = emDM_getMinMax;
797
798         emdm->dm.getNumVerts = emDM_getNumVerts;
799         emdm->dm.getNumFaces = emDM_getNumFaces;
800         emdm->dm.foreachMappedVertEM = emDM_foreachMappedVertEM;
801         emdm->dm.foreachMappedEdgeEM = emDM_foreachMappedEdgeEM;
802         emdm->dm.foreachMappedFaceCenterEM = emDM_foreachMappedFaceCenterEM;
803
804         emdm->dm.drawEdges = emDM_drawEdges;
805         emdm->dm.drawMappedEdgesEM = emDM_drawMappedEdgesEM;
806         emdm->dm.drawMappedEdgesInterpEM = emDM_drawMappedEdgesInterpEM;
807
808         emdm->dm.drawFacesSolid = emDM_drawFacesSolid;
809         emdm->dm.drawMappedFacesEM = emDM_drawMappedFacesEM;
810
811         emdm->dm.release = emDM_release;
812         
813         emdm->em = em;
814         emdm->vertexCos = vertexCos;
815
816         if (vertexCos) {
817                 EditVert *eve, *preveve;
818                 EditFace *efa;
819                 int totface = BLI_countlist(&em->faces);
820                 int i;
821
822                 for (i=0,eve=em->verts.first; eve; eve= eve->next)
823                         eve->prev = (EditVert*) i++;
824
825                 emdm->vertexNos = MEM_callocN(sizeof(*emdm->vertexNos)*i, "emdm_vno");
826                 emdm->faceNos = MEM_mallocN(sizeof(*emdm->faceNos)*totface, "emdm_vno");
827
828                 for(i=0, efa= em->faces.first; efa; i++, efa=efa->next) {
829                         float *v1 = vertexCos[(int) efa->v1->prev];
830                         float *v2 = vertexCos[(int) efa->v2->prev];
831                         float *v3 = vertexCos[(int) efa->v3->prev];
832                         float *no = emdm->faceNos[i];
833                         
834                         if(efa->v4) {
835                                 float *v4 = vertexCos[(int) efa->v3->prev];
836
837                                 CalcNormFloat4(v1, v2, v3, v4, no);
838                                 VecAddf(emdm->vertexNos[(int) efa->v4->prev], emdm->vertexNos[(int) efa->v4->prev], no);
839                         }
840                         else {
841                                 CalcNormFloat(v1, v2, v3, no);
842                         }
843
844                         VecAddf(emdm->vertexNos[(int) efa->v1->prev], emdm->vertexNos[(int) efa->v1->prev], no);
845                         VecAddf(emdm->vertexNos[(int) efa->v2->prev], emdm->vertexNos[(int) efa->v2->prev], no);
846                         VecAddf(emdm->vertexNos[(int) efa->v3->prev], emdm->vertexNos[(int) efa->v3->prev], no);
847                 }
848
849                 for(i=0, eve= em->verts.first; eve; i++, eve=eve->next) {
850                         float *no = emdm->vertexNos[i];
851
852                         if (Normalise(no)==0.0) {
853                                 VECCOPY(no, vertexCos[i]);
854                                 Normalise(no);
855                         }
856                 }
857
858                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
859                         eve->prev = preveve;
860         }
861
862         return (DerivedMesh*) emdm;
863 }
864
865 ///
866
867 typedef struct {
868         DerivedMesh dm;
869
870         DispListMesh *dlm;
871
872         EditVert **vertMap;
873         EditEdge **edgeMap;
874         EditFace **faceMap;
875 } SSDerivedMesh;
876
877 static void ssDM_foreachMappedVertEM(DerivedMesh *dm, void (*func)(void *userData, EditVert *vert, float *co, float *no_f, short *no_s), void *userData)
878 {
879         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
880         DispListMesh *dlm = ssdm->dlm;
881         int i;
882
883         if (ssdm->vertMap) {
884                 for (i=0; i<dlm->totvert; i++) {
885                         if (ssdm->vertMap[i]) {
886                                 func(userData, ssdm->vertMap[i], dlm->mvert[i].co, NULL, dlm->mvert[i].no);
887                         }
888                 }
889         }
890 }
891 static void ssDM_foreachMappedEdgeEM(DerivedMesh *dm, void (*func)(void *userData, EditEdge *eed, float *v0co, float *v1co), void *userData)
892 {
893         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
894         DispListMesh *dlm = ssdm->dlm;
895         int i;
896
897         if (ssdm->edgeMap) {
898                 for (i=0; i<dlm->totedge; i++) {
899                         if (ssdm->edgeMap[i]) {
900                                 MEdge *med = &dlm->medge[i];
901
902                                 func(userData, ssdm->edgeMap[i], dlm->mvert[med->v1].co, dlm->mvert[med->v2].co);
903                         }
904                 }
905         }
906 }
907 static void ssDM_drawMappedEdgesEM(DerivedMesh *dm, int (*setDrawOptions)(void *userData, EditEdge *edge), void *userData) 
908 {
909         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
910         DispListMesh *dlm = ssdm->dlm;
911         int i;
912
913         if (ssdm->edgeMap) {
914                 glBegin(GL_LINES);
915                 for(i=0; i<dlm->totedge; i++) {
916                         if(ssdm->edgeMap[i] && (!setDrawOptions || setDrawOptions(userData, ssdm->edgeMap[i]))) {
917                                 MEdge *med = &dlm->medge[i];
918
919                                 glVertex3fv(dlm->mvert[med->v1].co);
920                                 glVertex3fv(dlm->mvert[med->v2].co);
921                         }
922                 }
923                 glEnd();
924         }
925 }
926
927 static void ssDM_foreachMappedFaceCenterEM(DerivedMesh *dm, void (*func)(void *userData, EditFace *efa, float *co, float *no), void *userData)
928 {
929         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
930         DispListMesh *dlm = ssdm->dlm;
931         int i;
932
933         if (ssdm->faceMap) {
934                 for (i=0; i<dlm->totface; i++) {
935                         if(ssdm->faceMap[i]) {
936                                 MFace *mf = &dlm->mface[i];
937
938                                 if (mf->v3) {
939                                         float cent[3];
940                                         float no[3];
941
942                                         VECCOPY(cent, dlm->mvert[mf->v1].co);
943                                         VecAddf(cent, cent, dlm->mvert[mf->v2].co);
944                                         VecAddf(cent, cent, dlm->mvert[mf->v3].co);
945
946                                         if (mf->v4) {
947                                                 CalcNormFloat4(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, dlm->mvert[mf->v4].co, no);
948                                                 VecAddf(cent, cent, dlm->mvert[mf->v4].co);
949                                                 VecMulf(cent, 0.25f);
950                                         } else {
951                                                 CalcNormFloat(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, no);
952                                                 VecMulf(cent, 0.33333333333f);
953                                         }
954
955                                         func(userData, ssdm->faceMap[i], cent, no);
956                                 }
957                         }
958                 }
959         }
960 }
961 static void ssDM_drawMappedFacesEM(DerivedMesh *dm, int (*setDrawOptions)(void *userData, EditFace *face), void *userData)
962 {
963         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
964         DispListMesh *dlm = ssdm->dlm;
965         int i;
966
967         if (ssdm->faceMap) {            
968                 for (i=0; i<dlm->totface; i++) {
969                         if(ssdm->faceMap[i] && (!setDrawOptions || setDrawOptions(userData, ssdm->faceMap[i]))) {
970                                 MFace *mf = &dlm->mface[i];
971
972                                 if (mf->v3) {
973                                         glBegin(mf->v3?GL_QUADS:GL_TRIANGLES);
974                                         glVertex3fv(dlm->mvert[mf->v1].co);
975                                         glVertex3fv(dlm->mvert[mf->v2].co);
976                                         glVertex3fv(dlm->mvert[mf->v3].co);
977                                         if(mf->v4) glVertex3fv(dlm->mvert[mf->v4].co);
978                                         glEnd();
979                                 }
980                         }
981                 }
982         }
983 }
984
985 static void ssDM_drawMappedEdges(DerivedMesh *dm)
986 {
987         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
988         DispListMesh *dlm = ssdm->dlm;
989         MEdge *medge= dlm->medge;
990         MVert *mvert= dlm->mvert;
991         int a;
992         
993         glBegin(GL_LINES);
994         for (a=0; a<dlm->totedge; a++, medge++) {
995                 if (medge->flag&ME_EDGEDRAW) {
996                         glVertex3fv(mvert[medge->v1].co); 
997                         glVertex3fv(mvert[medge->v2].co);
998                 }
999         }
1000         glEnd();
1001 }
1002
1003 static void ssDM_drawLooseEdges(DerivedMesh *dm)
1004 {
1005         /* Can't implement currently */ 
1006 }
1007
1008 static void ssDM_drawVerts(DerivedMesh *dm)
1009 {
1010         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1011         DispListMesh *dlm = ssdm->dlm;
1012         MVert *mvert= dlm->mvert;
1013         int i;
1014
1015         bglBegin(GL_POINTS);
1016         for (i=0; i<dlm->totvert; i++) {
1017                 bglVertex3fv(mvert[i].co);
1018         }
1019         bglEnd();
1020 }
1021 static void ssDM_drawEdges(DerivedMesh *dm) 
1022 {
1023         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1024         DispListMesh *dlm = ssdm->dlm;
1025         MVert *mvert= dlm->mvert;
1026         int i;
1027
1028         if (dlm->medge) {
1029                 MEdge *medge= dlm->medge;
1030         
1031                 glBegin(GL_LINES);
1032                 for (i=0; i<dlm->totedge; i++, medge++) {
1033                         glVertex3fv(mvert[medge->v1].co); 
1034                         glVertex3fv(mvert[medge->v2].co);
1035                 }
1036                 glEnd();
1037         } else {
1038                 MFace *mface= dlm->mface;
1039
1040                 glBegin(GL_LINES);
1041                 for (i=0; i<dlm->totface; i++, mface++) {
1042                         glVertex3fv(mvert[mface->v1].co);
1043                         glVertex3fv(mvert[mface->v2].co);
1044
1045                         if (mface->v3) {
1046                                 glVertex3fv(mvert[mface->v2].co);
1047                                 glVertex3fv(mvert[mface->v3].co);
1048
1049                                 glVertex3fv(mvert[mface->v3].co);
1050                                 if (mface->v4) {
1051                                         glVertex3fv(mvert[mface->v4].co);
1052
1053                                         glVertex3fv(mvert[mface->v4].co);
1054                                 }
1055                                 glVertex3fv(mvert[mface->v1].co);
1056                         }
1057                 }
1058                 glEnd();
1059         }
1060 }
1061 static void ssDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
1062 {
1063         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1064         DispListMesh *dlm = ssdm->dlm;
1065         float *nors = dlm->nors;
1066         int glmode=-1, shademodel=-1, matnr=-1, drawCurrentMat=1;
1067         int i;
1068
1069 #define PASSVERT(ind) {                                         \
1070         if (shademodel==GL_SMOOTH)                              \
1071                 glNormal3sv(dlm->mvert[(ind)].no);      \
1072         glVertex3fv(dlm->mvert[(ind)].co);              \
1073 }
1074
1075         glBegin(glmode=GL_QUADS);
1076         for (i=0; i<dlm->totface; i++) {
1077                 MFace *mf= &dlm->mface[i];
1078                 
1079                 if (mf->v3) {
1080                         int new_glmode = mf->v4?GL_QUADS:GL_TRIANGLES;
1081                         int new_shademodel = (mf->flag&ME_SMOOTH)?GL_SMOOTH:GL_FLAT;
1082                         int new_matnr = mf->mat_nr+1;
1083                         
1084                         if(new_glmode!=glmode || new_shademodel!=shademodel || new_matnr!=matnr) {
1085                                 glEnd();
1086
1087                                 drawCurrentMat = setMaterial(matnr=new_matnr);
1088
1089                                 glShadeModel(shademodel=new_shademodel);
1090                                 glBegin(glmode=new_glmode);
1091                         }
1092                         
1093                         if (drawCurrentMat) {
1094                                 if (shademodel==GL_FLAT)
1095                                         glNormal3fv(&nors[i*3]);
1096                                         
1097                                 PASSVERT(mf->v1);
1098                                 PASSVERT(mf->v2);
1099                                 PASSVERT(mf->v3);
1100                                 if (mf->v4)
1101                                         PASSVERT(mf->v4);
1102                         }
1103                 }
1104         }
1105         glEnd();
1106         
1107 #undef PASSVERT
1108 }
1109 static void ssDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *vcols1, unsigned char *vcols2)
1110 {
1111         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1112         DispListMesh *dlm = ssdm->dlm;
1113         int i, lmode;
1114         
1115         glShadeModel(GL_SMOOTH);
1116         if (vcols2) {
1117                 glEnable(GL_CULL_FACE);
1118         } else {
1119                 useTwoSided = 0;
1120         }
1121                 
1122 #define PASSVERT(vidx, fidx) {                                  \
1123         unsigned char *col= &colbase[fidx*4];           \
1124         glColor3ub(col[3], col[2], col[1]);                     \
1125         glVertex3fv(dlm->mvert[(vidx)].co);                     \
1126 }
1127
1128         glBegin(lmode= GL_QUADS);
1129         for (i=0; i<dlm->totface; i++) {
1130                 MFace *mf= &dlm->mface[i];
1131                 
1132                 if (mf->v3) {
1133                         int nmode= mf->v4?GL_QUADS:GL_TRIANGLES;
1134                         unsigned char *colbase= &vcols1[i*16];
1135                         
1136                         if (nmode!=lmode) {
1137                                 glEnd();
1138                                 glBegin(lmode= nmode);
1139                         }
1140                         
1141                         PASSVERT(mf->v1, 0);
1142                         PASSVERT(mf->v2, 1);
1143                         PASSVERT(mf->v3, 2);
1144                         if (mf->v4)
1145                                 PASSVERT(mf->v4, 3);
1146                         
1147                         if (useTwoSided) {
1148                                 unsigned char *colbase= &vcols2[i*16];
1149
1150                                 if (mf->v4)
1151                                         PASSVERT(mf->v4, 3);
1152                                 PASSVERT(mf->v3, 2);
1153                                 PASSVERT(mf->v2, 1);
1154                                 PASSVERT(mf->v1, 0);
1155                         }
1156                 }
1157         }
1158         glEnd();
1159
1160         if (vcols2)
1161                 glDisable(GL_CULL_FACE);
1162         
1163 #undef PASSVERT
1164 }
1165 static void ssDM_drawFacesTex(DerivedMesh *dm, int (*setDrawParams)(TFace *tf, int matnr)) 
1166 {
1167         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1168         DispListMesh *dlm = ssdm->dlm;
1169         MVert *mvert= dlm->mvert;
1170         MFace *mface= dlm->mface;
1171         TFace *tface = dlm->tface;
1172         float *nors = dlm->nors;
1173         int a;
1174         
1175         for (a=0; a<dlm->totface; a++) {
1176                 MFace *mf= &mface[a];
1177                 TFace *tf = tface?&tface[a]:NULL;
1178                 unsigned char *cp= NULL;
1179                 
1180                 if(mf->v3==0) continue;
1181                 if(tf && ((tf->flag&TF_HIDE) || (tf->mode&TF_INVISIBLE))) continue;
1182
1183                 if (setDrawParams(tf, mf->mat_nr)) {
1184                         if (tf) {
1185                                 cp= (unsigned char*) tf->col;
1186                         } else if (dlm->mcol) {
1187                                 cp= (unsigned char*) &dlm->mcol[a*4];
1188                         }
1189                 }
1190
1191                 if (!(mf->flag&ME_SMOOTH)) {
1192                         glNormal3fv(&nors[a*3]);
1193                 }
1194
1195                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
1196                 if (tf) glTexCoord2fv(tf->uv[0]);
1197                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
1198                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v1].no);
1199                 glVertex3fv((mvert+mf->v1)->co);
1200                         
1201                 if (tf) glTexCoord2fv(tf->uv[1]);
1202                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
1203                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v2].no);
1204                 glVertex3fv((mvert+mf->v2)->co);
1205
1206                 if (tf) glTexCoord2fv(tf->uv[2]);
1207                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
1208                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v3].no);
1209                 glVertex3fv((mvert+mf->v3)->co);
1210
1211                 if(mf->v4) {
1212                         if (tf) glTexCoord2fv(tf->uv[3]);
1213                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
1214                         if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v4].no);
1215                         glVertex3fv((mvert+mf->v4)->co);
1216                 }
1217                 glEnd();
1218         }
1219 }
1220
1221 static void ssDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
1222 {
1223         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1224         int i;
1225
1226         if (ssdm->dlm->totvert) {
1227                 for (i=0; i<ssdm->dlm->totvert; i++) {
1228                         DO_MINMAX(ssdm->dlm->mvert[i].co, min_r, max_r);
1229                 }
1230         } else {
1231                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
1232         }
1233 }
1234
1235 static void ssDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
1236 {
1237         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1238         int i;
1239
1240         for (i=0; i<ssdm->dlm->totvert; i++) {
1241                 cos_r[i][0] = ssdm->dlm->mvert[i].co[0];
1242                 cos_r[i][1] = ssdm->dlm->mvert[i].co[1];
1243                 cos_r[i][2] = ssdm->dlm->mvert[i].co[2];
1244         }
1245 }
1246
1247 static int ssDM_getNumVerts(DerivedMesh *dm)
1248 {
1249         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1250
1251         return ssdm->dlm->totvert;
1252 }
1253 static int ssDM_getNumFaces(DerivedMesh *dm)
1254 {
1255         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1256
1257         return ssdm->dlm->totface;
1258 }
1259
1260 static DispListMesh *ssDM_convertToDispListMesh(DerivedMesh *dm, int allowShared)
1261 {
1262         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1263
1264         if (allowShared) {
1265                 return displistmesh_copyShared(ssdm->dlm);
1266         } else {
1267                 return displistmesh_copy(ssdm->dlm);
1268         }
1269 }
1270
1271 static DispListMesh *ssDM_convertToDispListMeshMapped(DerivedMesh *dm, int allowShared, EditVert ***vertMap_r, EditEdge ***edgeMap_r, EditFace ***faceMap_r)
1272 {
1273         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1274
1275                 // We should never get here if the appropriate ssdm fields weren't given.
1276
1277         *vertMap_r = MEM_dupallocN(ssdm->vertMap);
1278         *edgeMap_r = MEM_dupallocN(ssdm->edgeMap);
1279         *faceMap_r = MEM_dupallocN(ssdm->faceMap);
1280
1281         if (allowShared) {
1282                 return displistmesh_copyShared(ssdm->dlm);
1283         } else {
1284                 return displistmesh_copy(ssdm->dlm);
1285         }
1286 }
1287
1288 static void ssDM_release(DerivedMesh *dm)
1289 {
1290         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1291
1292         displistmesh_free(ssdm->dlm);
1293         if (ssdm->vertMap) {
1294                 MEM_freeN(ssdm->vertMap);
1295                 MEM_freeN(ssdm->edgeMap);
1296                 MEM_freeN(ssdm->faceMap);
1297         }
1298
1299         MEM_freeN(dm);
1300 }
1301
1302 DerivedMesh *derivedmesh_from_displistmesh(DispListMesh *dlm, float (*vertexCos)[3], EditVert **vertMap, EditEdge **edgeMap, EditFace **faceMap)
1303 {
1304         SSDerivedMesh *ssdm = MEM_callocN(sizeof(*ssdm), "ssdm");
1305
1306         ssdm->dm.getMinMax = ssDM_getMinMax;
1307
1308         ssdm->dm.getNumVerts = ssDM_getNumVerts;
1309         ssdm->dm.getNumFaces = ssDM_getNumFaces;
1310         ssdm->dm.convertToDispListMesh = ssDM_convertToDispListMesh;
1311         ssdm->dm.convertToDispListMeshMapped = ssDM_convertToDispListMeshMapped;
1312
1313         ssdm->dm.getVertCos = ssDM_getVertCos;
1314
1315         ssdm->dm.drawVerts = ssDM_drawVerts;
1316
1317         ssdm->dm.drawEdges = ssDM_drawEdges;
1318         ssdm->dm.drawMappedEdges = ssDM_drawMappedEdges;
1319         ssdm->dm.drawLooseEdges = ssDM_drawLooseEdges;
1320
1321         ssdm->dm.drawFacesSolid = ssDM_drawFacesSolid;
1322         ssdm->dm.drawFacesColored = ssDM_drawFacesColored;
1323         ssdm->dm.drawFacesTex = ssDM_drawFacesTex;
1324
1325                 /* EM functions */
1326         
1327         ssdm->dm.foreachMappedVertEM = ssDM_foreachMappedVertEM;
1328         ssdm->dm.foreachMappedEdgeEM = ssDM_foreachMappedEdgeEM;
1329         ssdm->dm.foreachMappedFaceCenterEM = ssDM_foreachMappedFaceCenterEM;
1330         
1331         ssdm->dm.drawMappedEdgesEM = ssDM_drawMappedEdgesEM;
1332         ssdm->dm.drawMappedEdgesInterpEM = NULL; // no way to implement this one
1333         
1334         ssdm->dm.drawMappedFacesEM = ssDM_drawMappedFacesEM;
1335
1336         ssdm->dm.release = ssDM_release;
1337         
1338         ssdm->dlm = dlm;
1339         ssdm->vertMap = vertMap;
1340         ssdm->edgeMap = edgeMap;
1341         ssdm->faceMap = faceMap;
1342
1343         if (vertexCos) {
1344                 int i;
1345
1346                 for (i=0; i<dlm->totvert; i++) {
1347                         VECCOPY(dlm->mvert[i].co, vertexCos[i]);
1348                 }
1349
1350                 if (dlm->nors && !dlm->dontFreeNors) {
1351                         MEM_freeN(dlm->nors);
1352                         dlm->nors = 0;
1353                 }
1354
1355                 mesh_calc_normals(dlm->mvert, dlm->totvert, dlm->mface, dlm->totface, &dlm->nors);
1356         }
1357
1358         return (DerivedMesh*) ssdm;
1359 }
1360
1361 /***/
1362
1363 typedef float vec3f[3];
1364
1365 DerivedMesh *mesh_create_derived_for_modifier(Object *ob, ModifierData *md)
1366 {
1367         Mesh *me = ob->data;
1368         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1369         DerivedMesh *dm;
1370
1371         if (!(md->mode&eModifierMode_Realtime)) return NULL;
1372         if (mti->isDisabled && mti->isDisabled(md)) return NULL;
1373
1374         if (mti->type==eModifierTypeType_OnlyDeform) {
1375                 int numVerts;
1376                 float (*deformedVerts)[3] = mesh_getVertexCos(me, &numVerts);
1377
1378                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts);
1379                 
1380                 dm = getMeshDerivedMesh(me, ob, deformedVerts);
1381                 MEM_freeN(deformedVerts);
1382         } else {
1383                 dm = mti->applyModifier(md, ob, NULL, NULL, 0, 0);
1384         }
1385
1386         return dm;
1387 }
1388
1389 static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], DerivedMesh **deform_r, DerivedMesh **final_r, int useRenderParams, int useDeform)
1390 {
1391         Mesh *me = ob->data;
1392         ModifierData *md= modifiers_getVirtualModifierList(ob);
1393         float (*deformedVerts)[3] = NULL;
1394         DerivedMesh *dm;
1395         int numVerts = me->totvert;
1396
1397         modifiers_clearErrors(ob);
1398
1399         if (deform_r) *deform_r = NULL;
1400         *final_r = NULL;
1401
1402         if (useDeform) {
1403                 do_mesh_key(me);
1404
1405                         /* Apply all leading deforming modifiers */
1406                 for (; md; md=md->next) {
1407                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1408
1409                         if (!(md->mode&(1<<useRenderParams))) continue;
1410                         if (mti->isDisabled && mti->isDisabled(md)) continue;
1411
1412                         if (mti->type==eModifierTypeType_OnlyDeform) {
1413                                 if (!deformedVerts) deformedVerts = mesh_getVertexCos(me, &numVerts);
1414                                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts);
1415                         } else {
1416                                 break;
1417                         }
1418                 }
1419
1420                         /* Result of all leading deforming modifiers is cached for
1421                          * places that wish to use the original mesh but with deformed
1422                          * coordinates (vpaint, etc.)
1423                          */
1424                 if (deform_r) *deform_r = getMeshDerivedMesh(me, ob, deformedVerts);
1425         } else {
1426                 deformedVerts = inputVertexCos;
1427         }
1428
1429                 /* Now apply all remaining modifiers. If useDeform is off then skip
1430                  * OnlyDeform ones. 
1431                  */
1432         dm = NULL;
1433         for (; md; md=md->next) {
1434                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1435
1436                 if (!(md->mode&(1<<useRenderParams))) continue;
1437                 if (mti->type==eModifierTypeType_OnlyDeform && !useDeform) continue;
1438                 if ((mti->flags&eModifierTypeFlag_RequiresOriginalData) && dm) {
1439                         modifier_setError(md, "Internal error, modifier requires original data (bad stack position).");
1440                         continue;
1441                 }
1442                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1443
1444                         /* How to apply modifier depends on (a) what we already have as
1445                          * a result of previous modifiers (could be a DerivedMesh or just
1446                          * deformed vertices) and (b) what type the modifier is.
1447                          */
1448
1449                 if (mti->type==eModifierTypeType_OnlyDeform) {
1450                                 /* No existing verts to deform, need to build them. */
1451                         if (!deformedVerts) {
1452                                 if (dm) {
1453                                                 /* Deforming a derived mesh, read the vertex locations out of the mesh and
1454                                                  * deform them. Once done with this run of deformers verts will be written back.
1455                                                  */
1456                                         numVerts = dm->getNumVerts(dm);
1457                                         deformedVerts = MEM_mallocN(sizeof(*deformedVerts)*numVerts, "dfmv");
1458                                         dm->getVertCos(dm, deformedVerts);
1459                                 } else {
1460                                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1461                                 }
1462                         }
1463
1464                         mti->deformVerts(md, ob, dm, deformedVerts, numVerts);
1465                 } else {
1466                                 /* There are 4 cases here (have deform? have dm?) but they all are handled
1467                                  * by the modifier apply function, which will also free the DerivedMesh if
1468                                  * it exists.
1469                                  */
1470                         DerivedMesh *ndm = mti->applyModifier(md, ob, dm, deformedVerts, useRenderParams, !inputVertexCos);
1471
1472                         if (ndm) {
1473                                 if (dm) dm->release(dm);
1474
1475                                 dm = ndm;
1476
1477                                 if (deformedVerts) {
1478                                         if (deformedVerts!=inputVertexCos) {
1479                                                 MEM_freeN(deformedVerts);
1480                                         }
1481                                         deformedVerts = NULL;
1482                                 }
1483                         } 
1484                 }
1485         }
1486
1487                 /* Yay, we are done. If we have a DerivedMesh and deformed vertices need to apply
1488                  * these back onto the DerivedMesh. If we have no DerivedMesh then we need to build
1489                  * one.
1490                  */
1491         if (dm && deformedVerts) {
1492                 DispListMesh *dlm = dm->convertToDispListMesh(dm, 0);
1493
1494                 dm->release(dm);
1495
1496                 *final_r = derivedmesh_from_displistmesh(dlm, deformedVerts, NULL, NULL, NULL);
1497         } else if (dm) {
1498                 *final_r = dm;
1499         } else {
1500                 *final_r = getMeshDerivedMesh(me, ob, deformedVerts);
1501         }
1502
1503         if (deformedVerts && deformedVerts!=inputVertexCos) {
1504                 MEM_freeN(deformedVerts);
1505         }
1506 }
1507
1508 static vec3f *editmesh_getVertexCos(EditMesh *em, int *numVerts_r)
1509 {
1510         int i, numVerts = *numVerts_r = BLI_countlist(&em->verts);
1511         float (*cos)[3];
1512         EditVert *eve;
1513
1514         cos = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos");
1515         for (i=0,eve=em->verts.first; i<numVerts; i++,eve=eve->next) {
1516                 VECCOPY(cos[i], eve->co);
1517         }
1518
1519         return cos;
1520 }
1521
1522 static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
1523 {
1524         Object *ob = G.obedit;
1525         EditMesh *em = G.editMesh;
1526         ModifierData *md;
1527         float (*deformedVerts)[3] = NULL;
1528         DerivedMesh *dm;
1529         int i, numVerts, cageIndex = modifiers_getCageIndex(ob, NULL);
1530
1531         modifiers_clearErrors(ob);
1532
1533         if (cage_r && cageIndex==-1) {
1534                 *cage_r = getEditMeshDerivedMesh(em, NULL);
1535         }
1536
1537         dm = NULL;
1538         for (i=0,md= ob->modifiers.first; md; i++,md=md->next) {
1539                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1540
1541                 if (!(md->mode&eModifierMode_Realtime)) continue;
1542                 if (!(md->mode&eModifierMode_Editmode)) continue;
1543                 if ((mti->flags&eModifierTypeFlag_RequiresOriginalData) && dm) {
1544                         modifier_setError(md, "Internal error, modifier requires original data (bad stack position).");
1545                         continue;
1546                 }
1547                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1548                 if (!(mti->flags&eModifierTypeFlag_SupportsEditmode)) continue;
1549
1550                         /* How to apply modifier depends on (a) what we already have as
1551                          * a result of previous modifiers (could be a DerivedMesh or just
1552                          * deformed vertices) and (b) what type the modifier is.
1553                          */
1554
1555                 if (mti->type==eModifierTypeType_OnlyDeform) {
1556                                 /* No existing verts to deform, need to build them. */
1557                         if (!deformedVerts) {
1558                                 if (dm) {
1559                                                 /* Deforming a derived mesh, read the vertex locations out of the mesh and
1560                                                  * deform them. Once done with this run of deformers verts will be written back.
1561                                                  */
1562                                         numVerts = dm->getNumVerts(dm);
1563                                         deformedVerts = MEM_mallocN(sizeof(*deformedVerts)*numVerts, "dfmv");
1564                                         dm->getVertCos(dm, deformedVerts);
1565                                 } else {
1566                                         deformedVerts = editmesh_getVertexCos(em, &numVerts);
1567                                 }
1568                         }
1569
1570                         mti->deformVertsEM(md, ob, em, dm, deformedVerts, numVerts);
1571                 } else {
1572                                 /* There are 4 cases here (have deform? have dm?) but they all are handled
1573                                  * by the modifier apply function, which will also free the DerivedMesh if
1574                                  * it exists.
1575                                  */
1576                         DerivedMesh *ndm = mti->applyModifierEM(md, ob, em, dm, deformedVerts);
1577
1578                         if (ndm) {
1579                                 if (dm && (!cage_r || dm!=*cage_r)) dm->release(dm);
1580
1581                                 dm = ndm;
1582
1583                                 if (deformedVerts) {
1584                                         MEM_freeN(deformedVerts);
1585                                         deformedVerts = NULL;
1586                                 }
1587                         }
1588                 }
1589
1590                 if (cage_r && i==cageIndex) {
1591                         if (dm && deformedVerts) {
1592                                 DispListMesh *dlm;
1593                                 EditVert **vertMap;
1594                                 EditEdge **edgeMap;
1595                                 EditFace **faceMap;
1596
1597                                 dlm = dm->convertToDispListMeshMapped(dm, 0, &vertMap, &edgeMap, &faceMap);
1598
1599                                 *cage_r = derivedmesh_from_displistmesh(dlm, deformedVerts, vertMap, edgeMap, faceMap);
1600                         } else if (dm) {
1601                                 *cage_r = dm;
1602                         } else {
1603                                 *cage_r = getEditMeshDerivedMesh(em, deformedVerts?MEM_dupallocN(deformedVerts):NULL);
1604                         }
1605                 }
1606         }
1607
1608                 /* Yay, we are done. If we have a DerivedMesh and deformed vertices need to apply
1609                  * these back onto the DerivedMesh. If we have no DerivedMesh then we need to build
1610                  * one.
1611                  */
1612         if (dm && deformedVerts) {
1613                 DispListMesh *dlm = dm->convertToDispListMesh(dm, 0);
1614
1615                 if (!cage_r || dm!=*cage_r) dm->release(dm);
1616
1617                 *final_r = derivedmesh_from_displistmesh(dlm, deformedVerts, NULL, NULL, NULL);
1618                 MEM_freeN(deformedVerts);
1619         } else if (dm) {
1620                 *final_r = dm;
1621         } else {
1622                 *final_r = getEditMeshDerivedMesh(em, deformedVerts);
1623         }
1624 }
1625
1626 /***/
1627
1628 static void clear_mesh_caches(Object *ob)
1629 {
1630         Mesh *me= ob->data;
1631
1632                 /* also serves as signal to remake texspace */
1633         if (me->bb) {
1634                 MEM_freeN(me->bb);
1635                 me->bb = NULL;
1636         }
1637
1638         freedisplist(&ob->disp);
1639
1640         if (ob->derivedFinal) {
1641                 ob->derivedFinal->release(ob->derivedFinal);
1642                 ob->derivedFinal= NULL;
1643         }
1644         if (ob->derivedDeform) {
1645                 ob->derivedDeform->release(ob->derivedDeform);
1646                 ob->derivedDeform= NULL;
1647         }
1648 }
1649
1650 static void mesh_build_data(Object *ob)
1651 {
1652         float min[3], max[3];
1653
1654         if(ob->flag&OB_FROMDUPLI) return;
1655
1656         clear_mesh_caches(ob);
1657
1658         mesh_calc_modifiers(ob, NULL, &ob->derivedDeform, &ob->derivedFinal, 0, 1);
1659
1660         INIT_MINMAX(min, max);
1661
1662         ob->derivedFinal->getMinMax(ob->derivedFinal, min, max);
1663
1664         boundbox_set_from_min_max(mesh_get_bb(ob->data), min, max);
1665 }
1666
1667 static void editmesh_build_data(void)
1668 {
1669         float min[3], max[3];
1670
1671         EditMesh *em = G.editMesh;
1672
1673         clear_mesh_caches(G.obedit);
1674
1675         if (em->derivedFinal) {
1676                 if (em->derivedFinal!=em->derivedCage) {
1677                         em->derivedFinal->release(em->derivedFinal);
1678                 }
1679                 em->derivedFinal = NULL;
1680         }
1681         if (em->derivedCage) {
1682                 em->derivedCage->release(em->derivedCage);
1683                 em->derivedCage = NULL;
1684         }
1685
1686         editmesh_calc_modifiers(&em->derivedCage, &em->derivedFinal);
1687
1688         INIT_MINMAX(min, max);
1689
1690         em->derivedFinal->getMinMax(em->derivedFinal, min, max);
1691
1692         boundbox_set_from_min_max(mesh_get_bb(G.obedit->data), min, max);
1693 }
1694
1695 void makeDispListMesh(Object *ob)
1696 {
1697         if (ob==G.obedit) {
1698                 editmesh_build_data();
1699         } else {
1700                 mesh_build_data(ob);
1701
1702                 build_particle_system(ob);
1703         }
1704 }
1705
1706 /***/
1707
1708 DerivedMesh *mesh_get_derived_final(Object *ob, int *needsFree_r)
1709 {
1710         if (!ob->derivedFinal) {
1711                 mesh_build_data(ob);
1712         }
1713
1714         *needsFree_r = 0;
1715         return ob->derivedFinal;
1716 }
1717
1718 DerivedMesh *mesh_get_derived_deform(Object *ob, int *needsFree_r)
1719 {
1720         if (!ob->derivedDeform) {
1721                 mesh_build_data(ob);
1722         } 
1723
1724         *needsFree_r = 0;
1725         return ob->derivedDeform;
1726 }
1727
1728 DerivedMesh *mesh_create_derived_render(Object *ob)
1729 {
1730         DerivedMesh *final;
1731
1732         mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1);
1733
1734         return final;
1735 }
1736
1737 DerivedMesh *mesh_create_derived_no_deform(Object *ob, float (*vertCos)[3])
1738 {
1739         DerivedMesh *final;
1740
1741         mesh_calc_modifiers(ob, vertCos, NULL, &final, 0, 0);
1742
1743         return final;
1744 }
1745
1746 DerivedMesh *mesh_create_derived_no_deform_render(Object *ob, float (*vertCos)[3])
1747 {
1748         DerivedMesh *final;
1749
1750         mesh_calc_modifiers(ob, vertCos, NULL, &final, 1, 0);
1751
1752         return final;
1753 }
1754
1755 /***/
1756
1757 DerivedMesh *editmesh_get_derived_cage_and_final(DerivedMesh **final_r, int *cageNeedsFree_r, int *finalNeedsFree_r)
1758 {
1759         *cageNeedsFree_r = *finalNeedsFree_r = 0;
1760
1761         if (!G.editMesh->derivedCage)
1762                 editmesh_build_data();
1763
1764         *final_r = G.editMesh->derivedFinal;
1765         return G.editMesh->derivedCage;
1766 }
1767
1768 DerivedMesh *editmesh_get_derived_cage(int *needsFree_r)
1769 {
1770         *needsFree_r = 0;
1771
1772         if (!G.editMesh->derivedCage)
1773                 editmesh_build_data();
1774
1775         return G.editMesh->derivedCage;
1776 }
1777
1778 DerivedMesh *editmesh_get_derived_base(void)
1779 {
1780         return getEditMeshDerivedMesh(G.editMesh, NULL);
1781 }