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