Removing an extra semi-colon that caused some grief
[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 #include <string.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <zlib.h>
40
41 #include "PIL_time.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_effect_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_modifier_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_object_force.h"
51 #include "DNA_object_fluidsim.h" // N_T
52 #include "DNA_scene_types.h" // N_T
53
54 #include "BLI_arithb.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_editVert.h"
57
58 #include "BKE_utildefines.h"
59 #include "BKE_DerivedMesh.h"
60 #include "BKE_displist.h"
61 #include "BKE_effect.h"
62 #include "BKE_global.h"
63 #include "BKE_material.h"
64 #include "BKE_mesh.h"
65 #include "BKE_object.h"
66 #include "BKE_subsurf.h"
67 #include "LBM_fluidsim.h"
68 #include "BKE_deform.h"
69 #include "BKE_modifier.h"
70 #include "BKE_key.h"
71
72 #include "BIF_gl.h"
73 #include "BIF_glutil.h"
74
75 ///////////////////////////////////
76 ///////////////////////////////////
77
78 typedef struct {
79         DerivedMesh dm;
80
81         Object *ob;
82         Mesh *me;
83         MVert *verts;
84         float *nors;
85         MCol *wpaintMCol;
86
87         int freeNors, freeVerts;
88 } MeshDerivedMesh;
89
90 static DispListMesh *meshDM_convertToDispListMesh(DerivedMesh *dm, int allowShared)
91 {
92         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
93         Mesh *me = mdm->me;
94         DispListMesh *dlm = MEM_callocN(sizeof(*dlm), "dlm");
95
96         dlm->totvert = me->totvert;
97         dlm->totedge = me->totedge;
98         dlm->totface = me->totface;
99         dlm->mvert = mdm->verts;
100         dlm->medge = me->medge;
101         dlm->mface = me->mface;
102         dlm->tface = me->tface;
103         dlm->mcol = me->mcol;
104         dlm->nors = mdm->nors;
105         dlm->dontFreeVerts = dlm->dontFreeOther = dlm->dontFreeNors = 1;
106
107         if (!allowShared) {
108                 dlm->mvert = MEM_dupallocN(dlm->mvert);
109                 if (dlm->nors) dlm->nors = MEM_dupallocN(dlm->nors);
110
111                 dlm->dontFreeVerts = dlm->dontFreeNors = 0;
112         }
113
114         return dlm;
115 }
116
117 static void meshDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
118 {
119         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
120         Mesh *me = mdm->me;
121         int i;
122
123         if (me->totvert) {
124                 for (i=0; i<me->totvert; i++) {
125                         DO_MINMAX(mdm->verts[i].co, min_r, max_r);
126                 }
127         } else {
128                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
129         }
130 }
131
132 static void meshDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
133 {
134         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
135         Mesh *me = mdm->me;
136         int i;
137
138         for (i=0; i<me->totvert; i++) {
139                 cos_r[i][0] = mdm->verts[i].co[0];
140                 cos_r[i][1] = mdm->verts[i].co[1];
141                 cos_r[i][2] = mdm->verts[i].co[2];
142         }
143 }
144
145 static void meshDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
146 {
147         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
148
149         VECCOPY(co_r, mdm->verts[index].co);
150 }
151
152 static void meshDM_getVertNo(DerivedMesh *dm, int index, float no_r[3])
153 {
154         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
155         short *no = mdm->verts[index].no;
156
157         no_r[0] = no[0]/32767.f;
158         no_r[1] = no[1]/32767.f;
159         no_r[2] = no[2]/32767.f;
160 }
161
162 static void meshDM_drawVerts(DerivedMesh *dm)
163 {
164         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
165         Mesh *me = mdm->me;
166         int i;
167
168         glBegin(GL_POINTS);
169         for(i=0; i<me->totvert; i++) {
170                 glVertex3fv(mdm->verts[i].co);
171         }
172         glEnd();
173 }
174 static void meshDM_drawUVEdges(DerivedMesh *dm)
175 {
176         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
177         Mesh *me = mdm->me;
178         int i;
179
180         if (me->tface) {
181                 glBegin(GL_LINES);
182                 for (i=0; i<me->totface; i++) {
183                         TFace *tf = &me->tface[i];
184
185                         if (!(tf->flag&TF_HIDE)) {
186                                 glVertex2fv(tf->uv[0]);
187                                 glVertex2fv(tf->uv[1]);
188
189                                 glVertex2fv(tf->uv[1]);
190                                 glVertex2fv(tf->uv[2]);
191
192                                 if (!me->mface[i].v4) {
193                                         glVertex2fv(tf->uv[2]);
194                                         glVertex2fv(tf->uv[0]);
195                                 } else {
196                                         glVertex2fv(tf->uv[2]);
197                                         glVertex2fv(tf->uv[3]);
198
199                                         glVertex2fv(tf->uv[3]);
200                                         glVertex2fv(tf->uv[0]);
201                                 }
202                         }
203                 }
204                 glEnd();
205         }
206 }
207 static void meshDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
208 {
209         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
210         Mesh *me= mdm->me;
211         MEdge *medge= me->medge;
212         int i;
213                 
214         glBegin(GL_LINES);
215         for(i=0; i<me->totedge; i++, medge++) {
216                 if ((medge->flag&ME_EDGEDRAW) && (drawLooseEdges || !(medge->flag&ME_LOOSEEDGE))) {
217                         glVertex3fv(mdm->verts[medge->v1].co);
218                         glVertex3fv(mdm->verts[medge->v2].co);
219                 }
220         }
221         glEnd();
222 }
223 static void meshDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
224 {
225         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
226         Mesh *me= mdm->me;
227         int i;
228                 
229         glBegin(GL_LINES);
230         for (i=0; i<me->totedge; i++) {
231                 if (!setDrawOptions || setDrawOptions(userData, i)) {
232                         glVertex3fv(mdm->verts[me->medge[i].v1].co);
233                         glVertex3fv(mdm->verts[me->medge[i].v2].co);
234                 }
235         }
236         glEnd();
237 }
238 static void meshDM_drawLooseEdges(DerivedMesh *dm)
239 {
240         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
241         Mesh *me= mdm->me;
242         MEdge *medge= me->medge;
243         int i;
244
245         glBegin(GL_LINES);
246         for (i=0; i<me->totedge; i++, medge++) {
247                 if (medge->flag&ME_LOOSEEDGE) {
248                         glVertex3fv(mdm->verts[medge->v1].co);
249                         glVertex3fv(mdm->verts[medge->v2].co);
250                 }
251         }
252         glEnd();
253 }
254 static void meshDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
255 {
256         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
257         Mesh *me = mdm->me;
258         MVert *mvert= mdm->verts;
259         MFace *mface= me->mface;
260         float *nors = mdm->nors;
261         int a;
262         int glmode=-1, shademodel=-1, matnr=-1, drawCurrentMat=1;
263
264 #define PASSVERT(index) {                                               \
265         if (shademodel==GL_SMOOTH) {                            \
266                 short *no = mvert[index].no;                    \
267                 glNormal3sv(no);                                                \
268         }                                                                                       \
269         glVertex3fv(mvert[index].co);   \
270 }
271
272         glBegin(glmode=GL_QUADS);
273         for(a=0; a<me->totface; a++, mface++, nors+=3) {
274                 int new_glmode, new_matnr, new_shademodel;
275                         
276                 new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
277                 new_matnr = mface->mat_nr+1;
278                 new_shademodel = (mface->flag & ME_SMOOTH)?GL_SMOOTH:GL_FLAT;
279                 
280                 if (new_glmode!=glmode || new_matnr!=matnr || new_shademodel!=shademodel) {
281                         glEnd();
282
283                         drawCurrentMat = setMaterial(matnr=new_matnr);
284
285                         glShadeModel(shademodel=new_shademodel);
286                         glBegin(glmode=new_glmode);
287                 } 
288                 
289                 if (drawCurrentMat) {
290                         if(shademodel==GL_FLAT) 
291                                 glNormal3fv(nors);
292
293                         PASSVERT(mface->v1);
294                         PASSVERT(mface->v2);
295                         PASSVERT(mface->v3);
296                         if (mface->v4) {
297                                 PASSVERT(mface->v4);
298                         }
299                 }
300         }
301         glEnd();
302
303         glShadeModel(GL_FLAT);
304 #undef PASSVERT
305 }
306
307 static void meshDM_drawFacesColored(DerivedMesh *dm, int useTwoSide, unsigned char *col1, unsigned char *col2)
308 {
309         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
310         Mesh *me= mdm->me;
311         MFace *mface= me->mface;
312         int a, glmode;
313         unsigned char *cp1, *cp2;
314
315         cp1= col1;
316         if(col2) {
317                 cp2= col2;
318         } else {
319                 cp2= NULL;
320                 useTwoSide= 0;
321         }
322
323         /* there's a conflict here... twosided colors versus culling...? */
324         /* defined by history, only texture faces have culling option */
325         /* we need that as mesh option builtin, next to double sided lighting */
326         if(col1 && col2)
327                 glEnable(GL_CULL_FACE);
328         
329         glShadeModel(GL_SMOOTH);
330         glBegin(glmode=GL_QUADS);
331         for(a=0; a<me->totface; a++, mface++, cp1+= 16) {
332                 int new_glmode= mface->v4?GL_QUADS:GL_TRIANGLES;
333
334                 if (new_glmode!=glmode) {
335                         glEnd();
336                         glBegin(glmode= new_glmode);
337                 }
338                         
339                 glColor3ub(cp1[3], cp1[2], cp1[1]);
340                 glVertex3fv( mdm->verts[mface->v1].co );
341                 glColor3ub(cp1[7], cp1[6], cp1[5]);
342                 glVertex3fv( mdm->verts[mface->v2].co );
343                 glColor3ub(cp1[11], cp1[10], cp1[9]);
344                 glVertex3fv( mdm->verts[mface->v3].co );
345                 if(mface->v4) {
346                         glColor3ub(cp1[15], cp1[14], cp1[13]);
347                         glVertex3fv( mdm->verts[mface->v4].co );
348                 }
349                         
350                 if(useTwoSide) {
351                         glColor3ub(cp2[11], cp2[10], cp2[9]);
352                         glVertex3fv( mdm->verts[mface->v3].co );
353                         glColor3ub(cp2[7], cp2[6], cp2[5]);
354                         glVertex3fv( mdm->verts[mface->v2].co );
355                         glColor3ub(cp2[3], cp2[2], cp2[1]);
356                         glVertex3fv( mdm->verts[mface->v1].co );
357                         if(mface->v4) {
358                                 glColor3ub(cp2[15], cp2[14], cp2[13]);
359                                 glVertex3fv( mdm->verts[mface->v4].co );
360                         }
361                 }
362                 if(col2) cp2+= 16;
363         }
364         glEnd();
365
366         glShadeModel(GL_FLAT);
367         glDisable(GL_CULL_FACE);
368 }
369 static void meshDM_drawFacesTex(DerivedMesh *dm, int (*setDrawParams)(TFace *tf, int matnr)) 
370 {
371         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
372         Mesh *me = mdm->me;
373         MVert *mvert= mdm->verts;
374         MFace *mface= me->mface;
375         TFace *tface = me->tface;
376         float *nors = mdm->nors;
377         int i;
378
379         for (i=0; i<me->totface; i++) {
380                 MFace *mf= &mface[i];
381                 TFace *tf = tface?&tface[i]:NULL;
382                 int flag;
383                 unsigned char *cp= NULL;
384                 
385                 flag = setDrawParams(tf, mf->mat_nr);
386
387                 if (flag==0) {
388                         continue;
389                 } else if (flag==1) {
390                         if (mdm->wpaintMCol) {
391                                 cp= (unsigned char*) &mdm->wpaintMCol[i*4];
392                         } else if (tf) {
393                                 cp= (unsigned char*) tf->col;
394                         } else if (me->mcol) {
395                                 cp= (unsigned char*) &me->mcol[i*4];
396                         }
397                 }
398
399                 if (!(mf->flag&ME_SMOOTH)) {
400                         glNormal3fv(&nors[i*3]);
401                 }
402
403                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
404                 if (tf) glTexCoord2fv(tf->uv[0]);
405                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
406                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v1].no);
407                 glVertex3fv(mvert[mf->v1].co);
408                         
409                 if (tf) glTexCoord2fv(tf->uv[1]);
410                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
411                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v2].no);
412                 glVertex3fv(mvert[mf->v2].co);
413
414                 if (tf) glTexCoord2fv(tf->uv[2]);
415                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
416                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v3].no);
417                 glVertex3fv(mvert[mf->v3].co);
418
419                 if(mf->v4) {
420                         if (tf) glTexCoord2fv(tf->uv[3]);
421                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
422                         if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v4].no);
423                         glVertex3fv(mvert[mf->v4].co);
424                 }
425                 glEnd();
426         }
427 }
428 static void meshDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors) 
429 {
430         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
431         Mesh *me = mdm->me;
432         MVert *mvert= mdm->verts;
433         MFace *mface= me->mface;
434         float *nors= mdm->nors;
435         int i;
436
437         for (i=0; i<me->totface; i++) {
438                 MFace *mf= &mface[i];
439                 int drawSmooth = 1;
440
441                 if (!setDrawOptions || setDrawOptions(userData, i, &drawSmooth)) {
442                         unsigned char *cp = NULL;
443
444                         if (useColors) {
445                                 if (mdm->wpaintMCol) {
446                                         cp= (unsigned char*) &mdm->wpaintMCol[i*4];
447                                 } else if (me->tface) {
448                                         cp= (unsigned char*) me->tface[i].col;
449                                 } else if (me->mcol) {
450                                         cp= (unsigned char*) &me->mcol[i*4];
451                                 }
452                         }
453
454                         glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
455                         glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
456
457                         if (!drawSmooth) {
458                                 glNormal3fv(&nors[i*3]);
459
460                                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
461                                 glVertex3fv(mvert[mf->v1].co);
462                                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
463                                 glVertex3fv(mvert[mf->v2].co);
464                                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
465                                 glVertex3fv(mvert[mf->v3].co);
466                                 if(mf->v4) {
467                                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
468                                         glVertex3fv(mvert[mf->v4].co);
469                                 }
470                         } else {
471                                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
472                                 glNormal3sv(mvert[mf->v1].no);
473                                 glVertex3fv(mvert[mf->v1].co);
474                                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
475                                 glNormal3sv(mvert[mf->v2].no);
476                                 glVertex3fv(mvert[mf->v2].co);
477                                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
478                                 glNormal3sv(mvert[mf->v3].no);
479                                 glVertex3fv(mvert[mf->v3].co);
480                                 if(mf->v4) {
481                                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
482                                         glNormal3sv(mvert[mf->v4].no);
483                                         glVertex3fv(mvert[mf->v4].co);
484                                 }
485                         }
486
487                         glEnd();
488                 }
489         }
490 }
491 static int meshDM_getNumVerts(DerivedMesh *dm)
492 {
493         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
494         Mesh *me = mdm->me;
495
496         return me->totvert;
497 }
498 static int meshDM_getNumFaces(DerivedMesh *dm)
499 {
500         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
501         Mesh *me = mdm->me;
502
503         return me->totface;
504 }
505
506 static void meshDM_release(DerivedMesh *dm)
507 {
508         MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
509
510         if (mdm->wpaintMCol) MEM_freeN(mdm->wpaintMCol);
511         if (mdm->freeNors) MEM_freeN(mdm->nors);
512         if (mdm->freeVerts) MEM_freeN(mdm->verts);
513         MEM_freeN(mdm);
514 }
515
516 static DerivedMesh *getMeshDerivedMesh(Mesh *me, Object *ob, float (*vertCos)[3])
517 {
518         MeshDerivedMesh *mdm = MEM_callocN(sizeof(*mdm), "mdm");
519
520         mdm->dm.getMinMax = meshDM_getMinMax;
521
522         mdm->dm.convertToDispListMesh = meshDM_convertToDispListMesh;
523         mdm->dm.getNumVerts = meshDM_getNumVerts;
524         mdm->dm.getNumFaces = meshDM_getNumFaces;
525
526         mdm->dm.getVertCos = meshDM_getVertCos;
527         mdm->dm.getVertCo = meshDM_getVertCo;
528         mdm->dm.getVertNo = meshDM_getVertNo;
529
530         mdm->dm.drawVerts = meshDM_drawVerts;
531
532         mdm->dm.drawUVEdges = meshDM_drawUVEdges;
533         mdm->dm.drawEdges = meshDM_drawEdges;
534         mdm->dm.drawLooseEdges = meshDM_drawLooseEdges;
535         
536         mdm->dm.drawFacesSolid = meshDM_drawFacesSolid;
537         mdm->dm.drawFacesColored = meshDM_drawFacesColored;
538         mdm->dm.drawFacesTex = meshDM_drawFacesTex;
539         mdm->dm.drawMappedFaces = meshDM_drawMappedFaces;
540
541         mdm->dm.drawMappedEdges = meshDM_drawMappedEdges;
542         mdm->dm.drawMappedFaces = meshDM_drawMappedFaces;
543
544         mdm->dm.release = meshDM_release;
545
546                 /* Works in conjunction with hack during modifier calc */
547         if ((G.f & G_WEIGHTPAINT) && ob==(G.scene->basact?G.scene->basact->object:NULL)) {
548                 mdm->wpaintMCol = MEM_dupallocN(me->mcol);
549         }
550
551         mdm->ob = ob;
552         mdm->me = me;
553         mdm->verts = me->mvert;
554         mdm->nors = NULL;
555         mdm->freeNors = 0;
556         mdm->freeVerts = 0;
557
558         if (vertCos) {
559                 int i;
560
561                 /* copy the original verts to preserve flag settings; if this is too
562                  * costly, must at least use MEM_callocN to clear flags */
563                 mdm->verts = MEM_dupallocN( me->mvert );
564                 for (i=0; i<me->totvert; i++) {
565                         VECCOPY(mdm->verts[i].co, vertCos[i]);
566                 }
567                 mesh_calc_normals(mdm->verts, me->totvert, me->mface, me->totface, &mdm->nors);
568                 mdm->freeNors = 1;
569                 mdm->freeVerts = 1;
570         } else {
571                         // XXX this is kinda hacky because we shouldn't really be editing
572                         // the mesh here, however, we can't just call mesh_build_faceNormals(ob)
573                         // because in the case when a key is applied to a mesh the vertex normals
574                         // would never be correctly computed.
575                 mesh_calc_normals(mdm->verts, me->totvert, me->mface, me->totface, &mdm->nors);
576                 mdm->freeNors = 1;
577         }
578
579         return (DerivedMesh*) mdm;
580 }
581
582 ///
583
584 typedef struct {
585         DerivedMesh dm;
586
587         EditMesh *em;
588         float (*vertexCos)[3];
589         float (*vertexNos)[3];
590         float (*faceNos)[3];
591 } EditMeshDerivedMesh;
592
593 static void emDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData)
594 {
595         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
596         EditVert *eve;
597         int i;
598
599         for (i=0,eve= emdm->em->verts.first; eve; i++,eve=eve->next) {
600                 if (emdm->vertexCos) {
601                         func(userData, i, emdm->vertexCos[i], emdm->vertexNos[i], NULL);
602                 } else {
603                         func(userData, i, eve->co, eve->no, NULL);
604                 }
605         }
606 }
607 static void emDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData)
608 {
609         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
610         EditEdge *eed;
611         int i;
612
613         if (emdm->vertexCos) {
614                 EditVert *eve, *preveve;
615
616                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
617                         eve->prev = (EditVert*) i++;
618                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
619                         func(userData, i, emdm->vertexCos[(int) eed->v1->prev], emdm->vertexCos[(int) eed->v2->prev]);
620                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
621                         eve->prev = preveve;
622         } else {
623                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
624                         func(userData, i, eed->v1->co, eed->v2->co);
625         }
626 }
627 static void emDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) 
628 {
629         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
630         EditEdge *eed;
631         int i;
632
633         if (emdm->vertexCos) {
634                 EditVert *eve, *preveve;
635
636                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
637                         eve->prev = (EditVert*) i++;
638
639                 glBegin(GL_LINES);
640                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
641                         if(!setDrawOptions || setDrawOptions(userData, i)) {
642                                 glVertex3fv(emdm->vertexCos[(int) eed->v1->prev]);
643                                 glVertex3fv(emdm->vertexCos[(int) eed->v2->prev]);
644                         }
645                 }
646                 glEnd();
647
648                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
649                         eve->prev = preveve;
650         } else {
651                 glBegin(GL_LINES);
652                 for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
653                         if(!setDrawOptions || setDrawOptions(userData, i)) {
654                                 glVertex3fv(eed->v1->co);
655                                 glVertex3fv(eed->v2->co);
656                         }
657                 }
658                 glEnd();
659         }
660 }
661 static void emDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
662 {
663         emDM_drawMappedEdges(dm, NULL, NULL);
664 }
665 static void emDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void (*setDrawInterpOptions)(void *userData, int index, float t), void *userData) 
666 {
667         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
668         EditEdge *eed;
669         int i;
670
671         if (emdm->vertexCos) {
672                 EditVert *eve, *preveve;
673
674                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
675                         eve->prev = (EditVert*) i++;
676
677                 glBegin(GL_LINES);
678                 for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
679                         if(!setDrawOptions || setDrawOptions(userData, i)) {
680                                 setDrawInterpOptions(userData, i, 0.0);
681                                 glVertex3fv(emdm->vertexCos[(int) eed->v1->prev]);
682                                 setDrawInterpOptions(userData, i, 1.0);
683                                 glVertex3fv(emdm->vertexCos[(int) eed->v2->prev]);
684                         }
685                 }
686                 glEnd();
687
688                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
689                         eve->prev = preveve;
690         } else {
691                 glBegin(GL_LINES);
692                 for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
693                         if(!setDrawOptions || setDrawOptions(userData, i)) {
694                                 setDrawInterpOptions(userData, i, 0.0);
695                                 glVertex3fv(eed->v1->co);
696                                 setDrawInterpOptions(userData, i, 1.0);
697                                 glVertex3fv(eed->v2->co);
698                         }
699                 }
700                 glEnd();
701         }
702 }
703 static void emDM__calcFaceCent(EditFace *efa, float cent[3], float (*vertexCos)[3])
704 {
705         if (vertexCos) {
706                 VECCOPY(cent, vertexCos[(int) efa->v1->prev]);
707                 VecAddf(cent, cent, vertexCos[(int) efa->v2->prev]);
708                 VecAddf(cent, cent, vertexCos[(int) efa->v3->prev]);
709                 if (efa->v4) VecAddf(cent, cent, vertexCos[(int) efa->v4->prev]);
710         } else {
711                 VECCOPY(cent, efa->v1->co);
712                 VecAddf(cent, cent, efa->v2->co);
713                 VecAddf(cent, cent, efa->v3->co);
714                 if (efa->v4) VecAddf(cent, cent, efa->v4->co);
715         }
716
717         if (efa->v4) {
718                 VecMulf(cent, 0.25f);
719         } else {
720                 VecMulf(cent, 0.33333333333f);
721         }
722 }
723 static void emDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData)
724 {
725         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
726         EditVert *eve, *preveve;
727         EditFace *efa;
728         float cent[3];
729         int i;
730
731         if (emdm->vertexCos) {
732                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
733                         eve->prev = (EditVert*) i++;
734         }
735
736         for(i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
737                 emDM__calcFaceCent(efa, cent, emdm->vertexCos);
738                 func(userData, i, cent, emdm->vertexCos?emdm->faceNos[i]:efa->n);
739         }
740
741         if (emdm->vertexCos) {
742                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
743                         eve->prev = preveve;
744         }
745 }
746 static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors)
747 {
748         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
749         EditFace *efa;
750         int i;
751
752         if (emdm->vertexCos) {
753                 EditVert *eve, *preveve;
754                 int drawSmooth = 1;
755
756                 for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
757                         eve->prev = (EditVert*) i++;
758
759                 for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
760                         if(!setDrawOptions || setDrawOptions(userData, i, &drawSmooth)) {
761                                 glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
762
763                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
764                                 if (!drawSmooth) {
765                                         glNormal3fv(emdm->faceNos[i]);
766                                         glVertex3fv(emdm->vertexCos[(int) efa->v1->prev]);
767                                         glVertex3fv(emdm->vertexCos[(int) efa->v2->prev]);
768                                         glVertex3fv(emdm->vertexCos[(int) efa->v3->prev]);
769                                         if(efa->v4) glVertex3fv(emdm->vertexCos[(int) efa->v4->prev]);
770                                 } else {
771                                         glNormal3fv(emdm->vertexNos[(int) efa->v1->prev]);
772                                         glVertex3fv(emdm->vertexCos[(int) efa->v1->prev]);
773                                         glNormal3fv(emdm->vertexNos[(int) efa->v2->prev]);
774                                         glVertex3fv(emdm->vertexCos[(int) efa->v2->prev]);
775                                         glNormal3fv(emdm->vertexNos[(int) efa->v3->prev]);
776                                         glVertex3fv(emdm->vertexCos[(int) efa->v3->prev]);
777                                         if(efa->v4) {
778                                                 glNormal3fv(emdm->vertexNos[(int) efa->v4->prev]);
779                                                 glVertex3fv(emdm->vertexCos[(int) efa->v4->prev]);
780                                         }
781                                 }
782                                 glEnd();
783                         }
784                 }
785
786                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
787                         eve->prev = preveve;
788         } else {
789                 int drawSmooth = 1;
790
791                 for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
792                         if(!setDrawOptions || setDrawOptions(userData, i, &drawSmooth)) {
793                                 glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
794
795                                 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
796                                 if (!drawSmooth) {
797                                         glNormal3fv(efa->n);
798                                         glVertex3fv(efa->v1->co);
799                                         glVertex3fv(efa->v2->co);
800                                         glVertex3fv(efa->v3->co);
801                                         if(efa->v4) glVertex3fv(efa->v4->co);
802                                 } else {
803                                         glNormal3fv(efa->v1->no);
804                                         glVertex3fv(efa->v1->co);
805                                         glNormal3fv(efa->v2->no);
806                                         glVertex3fv(efa->v2->co);
807                                         glNormal3fv(efa->v3->no);
808                                         glVertex3fv(efa->v3->co);
809                                         if(efa->v4) {
810                                                 glNormal3fv(efa->v4->no);
811                                                 glVertex3fv(efa->v4->co);
812                                         }
813                                 }
814                                 glEnd();
815                         }
816                 }
817         }
818 }
819
820 static void emDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
821 {
822         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
823         EditVert *eve;
824         int i;
825
826         if (emdm->em->verts.first) {
827                 for (i=0,eve= emdm->em->verts.first; eve; i++,eve= eve->next) {
828                         if (emdm->vertexCos) {
829                                 DO_MINMAX(emdm->vertexCos[i], min_r, max_r);
830                         } else {
831                                 DO_MINMAX(eve->co, min_r, max_r);
832                         }
833                 }
834         } else {
835                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
836         }
837 }
838 static int emDM_getNumVerts(DerivedMesh *dm)
839 {
840         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
841
842         return BLI_countlist(&emdm->em->verts);
843 }
844 static int emDM_getNumFaces(DerivedMesh *dm)
845 {
846         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
847
848         return BLI_countlist(&emdm->em->faces);
849 }
850
851 static void emDM_release(DerivedMesh *dm)
852 {
853         EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
854
855         if (emdm->vertexCos) {
856                 MEM_freeN(emdm->vertexCos);
857                 MEM_freeN(emdm->vertexNos);
858                 MEM_freeN(emdm->faceNos);
859         }
860
861         MEM_freeN(emdm);
862 }
863
864 static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, float (*vertexCos)[3])
865 {
866         EditMeshDerivedMesh *emdm = MEM_callocN(sizeof(*emdm), "emdm");
867
868         emdm->dm.getMinMax = emDM_getMinMax;
869
870         emdm->dm.getNumVerts = emDM_getNumVerts;
871         emdm->dm.getNumFaces = emDM_getNumFaces;
872         emdm->dm.foreachMappedVert = emDM_foreachMappedVert;
873         emdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
874         emdm->dm.foreachMappedFaceCenter = emDM_foreachMappedFaceCenter;
875
876         emdm->dm.drawEdges = emDM_drawEdges;
877         emdm->dm.drawMappedEdges = emDM_drawMappedEdges;
878         emdm->dm.drawMappedEdgesInterp = emDM_drawMappedEdgesInterp;
879         emdm->dm.drawMappedFaces = emDM_drawMappedFaces;
880
881         emdm->dm.release = emDM_release;
882         
883         emdm->em = em;
884         emdm->vertexCos = vertexCos;
885
886         if (vertexCos) {
887                 EditVert *eve, *preveve;
888                 EditFace *efa;
889                 int totface = BLI_countlist(&em->faces);
890                 int i;
891
892                 for (i=0,eve=em->verts.first; eve; eve= eve->next)
893                         eve->prev = (EditVert*) i++;
894
895                 emdm->vertexNos = MEM_callocN(sizeof(*emdm->vertexNos)*i, "emdm_vno");
896                 emdm->faceNos = MEM_mallocN(sizeof(*emdm->faceNos)*totface, "emdm_vno");
897
898                 for(i=0, efa= em->faces.first; efa; i++, efa=efa->next) {
899                         float *v1 = vertexCos[(int) efa->v1->prev];
900                         float *v2 = vertexCos[(int) efa->v2->prev];
901                         float *v3 = vertexCos[(int) efa->v3->prev];
902                         float *no = emdm->faceNos[i];
903                         
904                         if(efa->v4) {
905                                 float *v4 = vertexCos[(int) efa->v3->prev];
906
907                                 CalcNormFloat4(v1, v2, v3, v4, no);
908                                 VecAddf(emdm->vertexNos[(int) efa->v4->prev], emdm->vertexNos[(int) efa->v4->prev], no);
909                         }
910                         else {
911                                 CalcNormFloat(v1, v2, v3, no);
912                         }
913
914                         VecAddf(emdm->vertexNos[(int) efa->v1->prev], emdm->vertexNos[(int) efa->v1->prev], no);
915                         VecAddf(emdm->vertexNos[(int) efa->v2->prev], emdm->vertexNos[(int) efa->v2->prev], no);
916                         VecAddf(emdm->vertexNos[(int) efa->v3->prev], emdm->vertexNos[(int) efa->v3->prev], no);
917                 }
918
919                 for(i=0, eve= em->verts.first; eve; i++, eve=eve->next) {
920                         float *no = emdm->vertexNos[i];
921
922                         if (Normalise(no)==0.0) {
923                                 VECCOPY(no, vertexCos[i]);
924                                 Normalise(no);
925                         }
926                 }
927
928                 for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
929                         eve->prev = preveve;
930         }
931
932         return (DerivedMesh*) emdm;
933 }
934
935 ///
936
937 typedef struct {
938         DerivedMesh dm;
939
940         DispListMesh *dlm;
941 } SSDerivedMesh;
942
943 static void ssDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData)
944 {
945         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
946         DispListMesh *dlm = ssdm->dlm;
947         int i, index=-1;
948
949         for (i=0; i<dlm->totvert; i++) {
950                 MVert *mv = &dlm->mvert[i];
951
952                 if (mv->flag&ME_VERT_STEPINDEX) index++;
953
954                 if (index!=-1) {
955                         func(userData, index, mv->co, NULL, mv->no);
956                 }
957         }
958 }
959 static void ssDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData)
960 {
961         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
962         DispListMesh *dlm = ssdm->dlm;
963         int i, index=-1;
964
965         for (i=0; i<dlm->totedge; i++) {
966                 MEdge *med = &dlm->medge[i];
967
968                 if (med->flag&ME_EDGE_STEPINDEX) index++;
969
970                 if (index!=-1) {
971                         func(userData, index, dlm->mvert[med->v1].co, dlm->mvert[med->v2].co);
972                 }
973         }
974 }
975 static void ssDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) 
976 {
977         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
978         DispListMesh *dlm = ssdm->dlm;
979         int i, index=-1;
980
981         glBegin(GL_LINES);
982         for(i=0; i<dlm->totedge; i++) {
983                 MEdge *med = &dlm->medge[i];
984
985                 if (med->flag&ME_EDGE_STEPINDEX) index++;
986
987                 if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) {
988                         glVertex3fv(dlm->mvert[med->v1].co);
989                         glVertex3fv(dlm->mvert[med->v2].co);
990                 }
991         }
992         glEnd();
993 }
994
995 static void ssDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData)
996 {
997         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
998         DispListMesh *dlm = ssdm->dlm;
999         int i, index=-1;
1000
1001         for (i=0; i<dlm->totface; i++) {
1002                 MFace *mf = &dlm->mface[i];
1003
1004                 if (mf->flag&ME_FACE_STEPINDEX) index++;
1005
1006                 if(index!=-1) {
1007                         float cent[3];
1008                         float no[3];
1009
1010                         VECCOPY(cent, dlm->mvert[mf->v1].co);
1011                         VecAddf(cent, cent, dlm->mvert[mf->v2].co);
1012                         VecAddf(cent, cent, dlm->mvert[mf->v3].co);
1013
1014                         if (mf->v4) {
1015                                 CalcNormFloat4(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, dlm->mvert[mf->v4].co, no);
1016                                 VecAddf(cent, cent, dlm->mvert[mf->v4].co);
1017                                 VecMulf(cent, 0.25f);
1018                         } else {
1019                                 CalcNormFloat(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, no);
1020                                 VecMulf(cent, 0.33333333333f);
1021                         }
1022
1023                         func(userData, index, cent, no);
1024                 }
1025         }
1026 }
1027 static void ssDM_drawVerts(DerivedMesh *dm)
1028 {
1029         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1030         DispListMesh *dlm = ssdm->dlm;
1031         MVert *mvert= dlm->mvert;
1032         int i;
1033
1034         bglBegin(GL_POINTS);
1035         for (i=0; i<dlm->totvert; i++) {
1036                 bglVertex3fv(mvert[i].co);
1037         }
1038         bglEnd();
1039 }
1040 static void ssDM_drawUVEdges(DerivedMesh *dm)
1041 {
1042         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1043         DispListMesh *dlm = ssdm->dlm;
1044         int i;
1045
1046         if (dlm->tface) {
1047                 glBegin(GL_LINES);
1048                 for (i=0; i<dlm->totface; i++) {
1049                         TFace *tf = &dlm->tface[i];
1050
1051                         if (!(tf->flag&TF_HIDE)) {
1052                                 glVertex2fv(tf->uv[0]);
1053                                 glVertex2fv(tf->uv[1]);
1054
1055                                 glVertex2fv(tf->uv[1]);
1056                                 glVertex2fv(tf->uv[2]);
1057
1058                                 if (!dlm->mface[i].v4) {
1059                                         glVertex2fv(tf->uv[2]);
1060                                         glVertex2fv(tf->uv[0]);
1061                                 } else {
1062                                         glVertex2fv(tf->uv[2]);
1063                                         glVertex2fv(tf->uv[3]);
1064
1065                                         glVertex2fv(tf->uv[3]);
1066                                         glVertex2fv(tf->uv[0]);
1067                                 }
1068                         }
1069                 }
1070                 glEnd();
1071         }
1072 }
1073 static void ssDM_drawLooseEdges(DerivedMesh *dm) 
1074 {
1075         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1076         DispListMesh *dlm = ssdm->dlm;
1077         MVert *mvert = dlm->mvert;
1078         MEdge *medge= dlm->medge;
1079         int i;
1080
1081         glBegin(GL_LINES);
1082         for (i=0; i<dlm->totedge; i++, medge++) {
1083                 if (medge->flag&ME_LOOSEEDGE) {
1084                         glVertex3fv(mvert[medge->v1].co); 
1085                         glVertex3fv(mvert[medge->v2].co);
1086                 }
1087         }
1088         glEnd();
1089 }
1090 static void ssDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) 
1091 {
1092         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1093         DispListMesh *dlm = ssdm->dlm;
1094         MVert *mvert= dlm->mvert;
1095         MEdge *medge= dlm->medge;
1096         int i;
1097         
1098         glBegin(GL_LINES);
1099         for (i=0; i<dlm->totedge; i++, medge++) {
1100                 if ((medge->flag&ME_EDGEDRAW) && (drawLooseEdges || !(medge->flag&ME_LOOSEEDGE))) {
1101                         glVertex3fv(mvert[medge->v1].co); 
1102                         glVertex3fv(mvert[medge->v2].co);
1103                 }
1104         }
1105         glEnd();
1106 }
1107 static void ssDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
1108 {
1109         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1110         DispListMesh *dlm = ssdm->dlm;
1111         float *nors = dlm->nors;
1112         int glmode=-1, shademodel=-1, matnr=-1, drawCurrentMat=1;
1113         int i;
1114
1115 #define PASSVERT(ind) {                                         \
1116         if (shademodel==GL_SMOOTH)                              \
1117                 glNormal3sv(dlm->mvert[(ind)].no);      \
1118         glVertex3fv(dlm->mvert[(ind)].co);              \
1119 }
1120
1121         glBegin(glmode=GL_QUADS);
1122         for (i=0; i<dlm->totface; i++) {
1123                 MFace *mf= &dlm->mface[i];
1124                 int new_glmode = mf->v4?GL_QUADS:GL_TRIANGLES;
1125                 int new_shademodel = (mf->flag&ME_SMOOTH)?GL_SMOOTH:GL_FLAT;
1126                 int new_matnr = mf->mat_nr+1;
1127                 
1128                 if(new_glmode!=glmode || new_shademodel!=shademodel || new_matnr!=matnr) {
1129                         glEnd();
1130
1131                         drawCurrentMat = setMaterial(matnr=new_matnr);
1132
1133                         glShadeModel(shademodel=new_shademodel);
1134                         glBegin(glmode=new_glmode);
1135                 }
1136                 
1137                 if (drawCurrentMat) {
1138                         if (shademodel==GL_FLAT)
1139                                 glNormal3fv(&nors[i*3]);
1140                                 
1141                         PASSVERT(mf->v1);
1142                         PASSVERT(mf->v2);
1143                         PASSVERT(mf->v3);
1144                         if (mf->v4)
1145                                 PASSVERT(mf->v4);
1146                 }
1147         }
1148         glEnd();
1149         
1150 #undef PASSVERT
1151 }
1152 static void ssDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *vcols1, unsigned char *vcols2)
1153 {
1154         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1155         DispListMesh *dlm = ssdm->dlm;
1156         int i, lmode;
1157         
1158         glShadeModel(GL_SMOOTH);
1159         if (vcols2) {
1160                 glEnable(GL_CULL_FACE);
1161         } else {
1162                 useTwoSided = 0;
1163         }
1164                 
1165 #define PASSVERT(vidx, fidx) {                                  \
1166         unsigned char *col= &colbase[fidx*4];           \
1167         glColor3ub(col[3], col[2], col[1]);                     \
1168         glVertex3fv(dlm->mvert[(vidx)].co);                     \
1169 }
1170
1171         glBegin(lmode= GL_QUADS);
1172         for (i=0; i<dlm->totface; i++) {
1173                 MFace *mf= &dlm->mface[i];
1174                 int nmode= mf->v4?GL_QUADS:GL_TRIANGLES;
1175                 unsigned char *colbase= &vcols1[i*16];
1176                 
1177                 if (nmode!=lmode) {
1178                         glEnd();
1179                         glBegin(lmode= nmode);
1180                 }
1181                 
1182                 PASSVERT(mf->v1, 0);
1183                 PASSVERT(mf->v2, 1);
1184                 PASSVERT(mf->v3, 2);
1185                 if (mf->v4)
1186                         PASSVERT(mf->v4, 3);
1187                 
1188                 if (useTwoSided) {
1189                         unsigned char *colbase= &vcols2[i*16];
1190
1191                         if (mf->v4)
1192                                 PASSVERT(mf->v4, 3);
1193                         PASSVERT(mf->v3, 2);
1194                         PASSVERT(mf->v2, 1);
1195                         PASSVERT(mf->v1, 0);
1196                 }
1197         }
1198         glEnd();
1199
1200         if (vcols2)
1201                 glDisable(GL_CULL_FACE);
1202         
1203 #undef PASSVERT
1204 }
1205 static void ssDM_drawFacesTex(DerivedMesh *dm, int (*setDrawParams)(TFace *tf, int matnr)) 
1206 {
1207         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1208         DispListMesh *dlm = ssdm->dlm;
1209         MVert *mvert= dlm->mvert;
1210         MFace *mface= dlm->mface;
1211         TFace *tface = dlm->tface;
1212         float *nors = dlm->nors;
1213         int a;
1214         
1215         for (a=0; a<dlm->totface; a++) {
1216                 MFace *mf= &mface[a];
1217                 TFace *tf = tface?&tface[a]:NULL;
1218                 int flag;
1219                 unsigned char *cp= NULL;
1220                 
1221                 flag = setDrawParams(tf, mf->mat_nr);
1222
1223                 if (flag==0) {
1224                         continue;
1225                 } else if (flag==1) {
1226                         if (tf) {
1227                                 cp= (unsigned char*) tf->col;
1228                         } else if (dlm->mcol) {
1229                                 cp= (unsigned char*) &dlm->mcol[a*4];
1230                         }
1231                 }
1232
1233                 if (!(mf->flag&ME_SMOOTH)) {
1234                         glNormal3fv(&nors[a*3]);
1235                 }
1236
1237                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
1238                 if (tf) glTexCoord2fv(tf->uv[0]);
1239                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
1240                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v1].no);
1241                 glVertex3fv((mvert+mf->v1)->co);
1242                         
1243                 if (tf) glTexCoord2fv(tf->uv[1]);
1244                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
1245                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v2].no);
1246                 glVertex3fv((mvert+mf->v2)->co);
1247
1248                 if (tf) glTexCoord2fv(tf->uv[2]);
1249                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
1250                 if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v3].no);
1251                 glVertex3fv((mvert+mf->v3)->co);
1252
1253                 if(mf->v4) {
1254                         if (tf) glTexCoord2fv(tf->uv[3]);
1255                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
1256                         if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v4].no);
1257                         glVertex3fv((mvert+mf->v4)->co);
1258                 }
1259                 glEnd();
1260         }
1261 }
1262 static void ssDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors) 
1263 {
1264         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1265         DispListMesh *dlm = ssdm->dlm;
1266         MVert *mvert= dlm->mvert;
1267         MFace *mface= dlm->mface;
1268         float *nors = dlm->nors;
1269         int i, index=-1;
1270
1271         for (i=0; i<dlm->totface; i++) {
1272                 MFace *mf = &mface[i];
1273                 int drawSmooth = 1;
1274
1275                 if (mf->flag&ME_FACE_STEPINDEX) index++;
1276
1277                 if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index, &drawSmooth))) {
1278                         unsigned char *cp = NULL;
1279
1280                         if (useColors) {
1281                                 if (dlm->tface) {
1282                                         cp= (unsigned char*) dlm->tface[i].col;
1283                                 } else if (dlm->mcol) {
1284                                         cp= (unsigned char*) &dlm->mcol[i*4];
1285                                 }
1286                         }
1287
1288                         glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
1289                         glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
1290
1291                         if (!drawSmooth) {
1292                                 glNormal3fv(&nors[i*3]);
1293
1294                                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
1295                                 glVertex3fv(mvert[mf->v1].co);
1296                                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
1297                                 glVertex3fv(mvert[mf->v2].co);
1298                                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
1299                                 glVertex3fv(mvert[mf->v3].co);
1300                                 if(mf->v4) {
1301                                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
1302                                         glVertex3fv(mvert[mf->v4].co);
1303                                 }
1304                         } else {
1305                                 if (cp) glColor3ub(cp[3], cp[2], cp[1]);
1306                                 glNormal3sv(mvert[mf->v1].no);
1307                                 glVertex3fv(mvert[mf->v1].co);
1308                                 if (cp) glColor3ub(cp[7], cp[6], cp[5]);
1309                                 glNormal3sv(mvert[mf->v2].no);
1310                                 glVertex3fv(mvert[mf->v2].co);
1311                                 if (cp) glColor3ub(cp[11], cp[10], cp[9]);
1312                                 glNormal3sv(mvert[mf->v3].no);
1313                                 glVertex3fv(mvert[mf->v3].co);
1314                                 if(mf->v4) {
1315                                         if (cp) glColor3ub(cp[15], cp[14], cp[13]);
1316                                         glNormal3sv(mvert[mf->v4].no);
1317                                         glVertex3fv(mvert[mf->v4].co);
1318                                 }
1319                         }
1320
1321                         glEnd();
1322                 }
1323         }
1324 }
1325 static void ssDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
1326 {
1327         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1328         int i;
1329
1330         if (ssdm->dlm->totvert) {
1331                 for (i=0; i<ssdm->dlm->totvert; i++) {
1332                         DO_MINMAX(ssdm->dlm->mvert[i].co, min_r, max_r);
1333                 }
1334         } else {
1335                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
1336         }
1337 }
1338
1339 static void ssDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
1340 {
1341         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1342         int i;
1343
1344         for (i=0; i<ssdm->dlm->totvert; i++) {
1345                 cos_r[i][0] = ssdm->dlm->mvert[i].co[0];
1346                 cos_r[i][1] = ssdm->dlm->mvert[i].co[1];
1347                 cos_r[i][2] = ssdm->dlm->mvert[i].co[2];
1348         }
1349 }
1350
1351 static int ssDM_getNumVerts(DerivedMesh *dm)
1352 {
1353         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1354
1355         return ssdm->dlm->totvert;
1356 }
1357 static int ssDM_getNumFaces(DerivedMesh *dm)
1358 {
1359         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1360
1361         return ssdm->dlm->totface;
1362 }
1363
1364 static DispListMesh *ssDM_convertToDispListMesh(DerivedMesh *dm, int allowShared)
1365 {
1366         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1367
1368         if (allowShared) {
1369                 return displistmesh_copyShared(ssdm->dlm);
1370         } else {
1371                 return displistmesh_copy(ssdm->dlm);
1372         }
1373 }
1374
1375 static void ssDM_release(DerivedMesh *dm)
1376 {
1377         SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
1378
1379         displistmesh_free(ssdm->dlm);
1380
1381         MEM_freeN(dm);
1382 }
1383
1384 DerivedMesh *derivedmesh_from_displistmesh(DispListMesh *dlm, float (*vertexCos)[3])
1385 {
1386         SSDerivedMesh *ssdm = MEM_callocN(sizeof(*ssdm), "ssdm");
1387
1388         ssdm->dm.getMinMax = ssDM_getMinMax;
1389
1390         ssdm->dm.getNumVerts = ssDM_getNumVerts;
1391         ssdm->dm.getNumFaces = ssDM_getNumFaces;
1392         ssdm->dm.convertToDispListMesh = ssDM_convertToDispListMesh;
1393
1394         ssdm->dm.getVertCos = ssDM_getVertCos;
1395
1396         ssdm->dm.drawVerts = ssDM_drawVerts;
1397
1398         ssdm->dm.drawUVEdges = ssDM_drawUVEdges;
1399         ssdm->dm.drawEdges = ssDM_drawEdges;
1400         ssdm->dm.drawLooseEdges = ssDM_drawLooseEdges;
1401         
1402         ssdm->dm.drawFacesSolid = ssDM_drawFacesSolid;
1403         ssdm->dm.drawFacesColored = ssDM_drawFacesColored;
1404         ssdm->dm.drawFacesTex = ssDM_drawFacesTex;
1405         ssdm->dm.drawMappedFaces = ssDM_drawMappedFaces;
1406
1407                 /* EM functions */
1408         
1409         ssdm->dm.foreachMappedVert = ssDM_foreachMappedVert;
1410         ssdm->dm.foreachMappedEdge = ssDM_foreachMappedEdge;
1411         ssdm->dm.foreachMappedFaceCenter = ssDM_foreachMappedFaceCenter;
1412         
1413         ssdm->dm.drawMappedEdges = ssDM_drawMappedEdges;
1414         ssdm->dm.drawMappedEdgesInterp = NULL; // no way to implement this one
1415         
1416         ssdm->dm.release = ssDM_release;
1417         
1418         ssdm->dlm = dlm;
1419
1420         if (vertexCos) {
1421                 int i;
1422
1423                 for (i=0; i<dlm->totvert; i++) {
1424                         VECCOPY(dlm->mvert[i].co, vertexCos[i]);
1425                 }
1426
1427                 if (dlm->nors && !dlm->dontFreeNors) {
1428                         MEM_freeN(dlm->nors);
1429                         dlm->nors = 0;
1430                 }
1431
1432                 mesh_calc_normals(dlm->mvert, dlm->totvert, dlm->mface, dlm->totface, &dlm->nors);
1433         }
1434
1435         return (DerivedMesh*) ssdm;
1436 }
1437
1438 /***/
1439
1440 DerivedMesh *mesh_create_derived_for_modifier(Object *ob, ModifierData *md)
1441 {
1442         Mesh *me = ob->data;
1443         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1444         DerivedMesh *dm;
1445
1446         if (!(md->mode&eModifierMode_Realtime)) return NULL;
1447         if (mti->isDisabled && mti->isDisabled(md)) return NULL;
1448
1449         if (mti->type==eModifierTypeType_OnlyDeform) {
1450                 int numVerts;
1451                 float (*deformedVerts)[3] = mesh_getVertexCos(me, &numVerts);
1452
1453                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts);
1454                 
1455                 dm = getMeshDerivedMesh(me, ob, deformedVerts);
1456                 MEM_freeN(deformedVerts);
1457         } else {
1458                 dm = mti->applyModifier(md, ob, NULL, NULL, 0, 0);
1459         }
1460
1461         return dm;
1462 }
1463
1464 static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], DerivedMesh **deform_r, DerivedMesh **final_r, int useRenderParams, int useDeform)
1465 {
1466         Mesh *me = ob->data;
1467         ModifierData *md= modifiers_getVirtualModifierList(ob);
1468         float (*deformedVerts)[3] = NULL;
1469         DerivedMesh *dm;
1470         int numVerts = me->totvert;
1471
1472         modifiers_clearErrors(ob);
1473
1474         if (deform_r) *deform_r = NULL;
1475         *final_r = NULL;
1476
1477         if (useDeform) {
1478                 if(do_ob_key(ob))       /* shape key makes deform verts */
1479                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1480                 
1481                         /* Apply all leading deforming modifiers */
1482                 for (; md; md=md->next) {
1483                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1484
1485                         if (!(md->mode&(1<<useRenderParams))) continue;
1486                         if (mti->isDisabled && mti->isDisabled(md)) continue;
1487
1488                         if (mti->type==eModifierTypeType_OnlyDeform) {
1489                                 if (!deformedVerts) deformedVerts = mesh_getVertexCos(me, &numVerts);
1490                                 mti->deformVerts(md, ob, NULL, deformedVerts, numVerts);
1491                         } else {
1492                                 break;
1493                         }
1494                 }
1495
1496                         /* Result of all leading deforming modifiers is cached for
1497                          * places that wish to use the original mesh but with deformed
1498                          * coordinates (vpaint, etc.)
1499                          */
1500                 if (deform_r) *deform_r = getMeshDerivedMesh(me, ob, deformedVerts);
1501         } else {
1502                 deformedVerts = inputVertexCos;
1503         }
1504
1505         /* N_T 
1506          * i dont know why, but somehow the if(useDeform) part
1507          * is necessary to get anything displayed
1508          */ 
1509         if((G.obedit!=ob) && (ob->fluidsimFlag & OB_FLUIDSIM_ENABLE)) {
1510                 if(ob->fluidsimSettings->type & OB_FLUIDSIM_DOMAIN) {
1511                         *final_r = getFluidsimDerivedMesh(ob,useRenderParams, NULL,NULL);                                       
1512                         if(*final_r) return;
1513                 }
1514         }
1515
1516                 /* Now apply all remaining modifiers. If useDeform is off then skip
1517                  * OnlyDeform ones. 
1518                  */
1519         dm = NULL;
1520         for (; md; md=md->next) {
1521                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1522
1523                 if (!(md->mode&(1<<useRenderParams))) continue;
1524                 if (mti->type==eModifierTypeType_OnlyDeform && !useDeform) continue;
1525                 if ((mti->flags&eModifierTypeFlag_RequiresOriginalData) && dm) {
1526                         modifier_setError(md, "Internal error, modifier requires original data (bad stack position).");
1527                         continue;
1528                 }
1529                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1530
1531                         /* How to apply modifier depends on (a) what we already have as
1532                          * a result of previous modifiers (could be a DerivedMesh or just
1533                          * deformed vertices) and (b) what type the modifier is.
1534                          */
1535
1536                 if (mti->type==eModifierTypeType_OnlyDeform) {
1537                                 /* No existing verts to deform, need to build them. */
1538                         if (!deformedVerts) {
1539                                 if (dm) {
1540                                                 /* Deforming a derived mesh, read the vertex locations out of the mesh and
1541                                                  * deform them. Once done with this run of deformers verts will be written back.
1542                                                  */
1543                                         numVerts = dm->getNumVerts(dm);
1544                                         deformedVerts = MEM_mallocN(sizeof(*deformedVerts)*numVerts, "dfmv");
1545                                         dm->getVertCos(dm, deformedVerts);
1546                                 } else {
1547                                         deformedVerts = mesh_getVertexCos(me, &numVerts);
1548                                 }
1549                         }
1550
1551                         mti->deformVerts(md, ob, dm, deformedVerts, numVerts);
1552                 } else {
1553                                 /* There are 4 cases here (have deform? have dm?) but they all are handled
1554                                  * by the modifier apply function, which will also free the DerivedMesh if
1555                                  * it exists.
1556                                  */
1557                         DerivedMesh *ndm = mti->applyModifier(md, ob, dm, deformedVerts, useRenderParams, !inputVertexCos);
1558
1559                         if (ndm) {
1560                                 if (dm) dm->release(dm);
1561
1562                                 dm = ndm;
1563
1564                                 if (deformedVerts) {
1565                                         if (deformedVerts!=inputVertexCos) {
1566                                                 MEM_freeN(deformedVerts);
1567                                         }
1568                                         deformedVerts = NULL;
1569                                 }
1570                         } 
1571                 }
1572         }
1573
1574                 /* Yay, we are done. If we have a DerivedMesh and deformed vertices need to apply
1575                  * these back onto the DerivedMesh. If we have no DerivedMesh then we need to build
1576                  * one.
1577                  */
1578         if (dm && deformedVerts) {
1579                 DispListMesh *dlm = dm->convertToDispListMesh(dm, 0);
1580
1581                 dm->release(dm);
1582
1583                 *final_r = derivedmesh_from_displistmesh(dlm, deformedVerts);
1584         } else if (dm) {
1585                 *final_r = dm;
1586         } else {
1587                 *final_r = getMeshDerivedMesh(me, ob, deformedVerts);
1588         }
1589
1590         if (deformedVerts && deformedVerts!=inputVertexCos) {
1591                 MEM_freeN(deformedVerts);
1592         }
1593 }
1594
1595 static float (*editmesh_getVertexCos(EditMesh *em, int *numVerts_r))[3]
1596 {
1597         int i, numVerts = *numVerts_r = BLI_countlist(&em->verts);
1598         float (*cos)[3];
1599         EditVert *eve;
1600
1601         cos = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos");
1602         for (i=0,eve=em->verts.first; i<numVerts; i++,eve=eve->next) {
1603                 VECCOPY(cos[i], eve->co);
1604         }
1605
1606         return cos;
1607 }
1608
1609 static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
1610 {
1611         Object *ob = G.obedit;
1612         EditMesh *em = G.editMesh;
1613         ModifierData *md;
1614         float (*deformedVerts)[3] = NULL;
1615         DerivedMesh *dm;
1616         int i, numVerts, cageIndex = modifiers_getCageIndex(ob, NULL);
1617
1618         modifiers_clearErrors(ob);
1619
1620         if (cage_r && cageIndex==-1) {
1621                 *cage_r = getEditMeshDerivedMesh(em, NULL);
1622         }
1623
1624         dm = NULL;
1625         for (i=0,md= ob->modifiers.first; md; i++,md=md->next) {
1626                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1627
1628                 if (!(md->mode&eModifierMode_Realtime)) continue;
1629                 if (!(md->mode&eModifierMode_Editmode)) continue;
1630                 if ((mti->flags&eModifierTypeFlag_RequiresOriginalData) && dm) {
1631                         modifier_setError(md, "Internal error, modifier requires original data (bad stack position).");
1632                         continue;
1633                 }
1634                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1635                 if (!(mti->flags&eModifierTypeFlag_SupportsEditmode)) continue;
1636
1637                         /* How to apply modifier depends on (a) what we already have as
1638                          * a result of previous modifiers (could be a DerivedMesh or just
1639                          * deformed vertices) and (b) what type the modifier is.
1640                          */
1641
1642                 if (mti->type==eModifierTypeType_OnlyDeform) {
1643                                 /* No existing verts to deform, need to build them. */
1644                         if (!deformedVerts) {
1645                                 if (dm) {
1646                                                 /* Deforming a derived mesh, read the vertex locations out of the mesh and
1647                                                  * deform them. Once done with this run of deformers verts will be written back.
1648                                                  */
1649                                         numVerts = dm->getNumVerts(dm);
1650                                         deformedVerts = MEM_mallocN(sizeof(*deformedVerts)*numVerts, "dfmv");
1651                                         dm->getVertCos(dm, deformedVerts);
1652                                 } else {
1653                                         deformedVerts = editmesh_getVertexCos(em, &numVerts);
1654                                 }
1655                         }
1656
1657                         mti->deformVertsEM(md, ob, em, dm, deformedVerts, numVerts);
1658                 } else {
1659                                 /* There are 4 cases here (have deform? have dm?) but they all are handled
1660                                  * by the modifier apply function, which will also free the DerivedMesh if
1661                                  * it exists.
1662                                  */
1663                         DerivedMesh *ndm = mti->applyModifierEM(md, ob, em, dm, deformedVerts);
1664
1665                         if (ndm) {
1666                                 if (dm && (!cage_r || dm!=*cage_r)) dm->release(dm);
1667
1668                                 dm = ndm;
1669
1670                                 if (deformedVerts) {
1671                                         MEM_freeN(deformedVerts);
1672                                         deformedVerts = NULL;
1673                                 }
1674                         }
1675                 }
1676
1677                 if (cage_r && i==cageIndex) {
1678                         if (dm && deformedVerts) {
1679                                 DispListMesh *dlm;
1680
1681                                 dlm = dm->convertToDispListMesh(dm, 0);
1682
1683                                 *cage_r = derivedmesh_from_displistmesh(dlm, deformedVerts);
1684                         } else if (dm) {
1685                                 *cage_r = dm;
1686                         } else {
1687                                 *cage_r = getEditMeshDerivedMesh(em, deformedVerts?MEM_dupallocN(deformedVerts):NULL);
1688                         }
1689                 }
1690         }
1691
1692                 /* Yay, we are done. If we have a DerivedMesh and deformed vertices need to apply
1693                  * these back onto the DerivedMesh. If we have no DerivedMesh then we need to build
1694                  * one.
1695                  */
1696         if (dm && deformedVerts) {
1697                 DispListMesh *dlm = dm->convertToDispListMesh(dm, 0);
1698
1699                 if (!cage_r || dm!=*cage_r) dm->release(dm);
1700
1701                 *final_r = derivedmesh_from_displistmesh(dlm, deformedVerts);
1702                 MEM_freeN(deformedVerts);
1703         } else if (dm) {
1704                 *final_r = dm;
1705         } else {
1706                 *final_r = getEditMeshDerivedMesh(em, deformedVerts);
1707         }
1708 }
1709
1710 /***/
1711
1712
1713         /* Something of a hack, at the moment deal with weightpaint
1714          * by tucking into colors during modifier eval, only in
1715          * wpaint mode. Works ok but need to make sure recalc
1716          * happens on enter/exit wpaint.
1717          */
1718
1719 static void weight_to_rgb(float input, float *fr, float *fg, float *fb)
1720 {
1721         float blend;
1722         
1723         blend= ((input/2.0f)+0.5f);
1724         
1725         if (input<=0.25f){      // blue->cyan
1726                 *fr= 0.0f;
1727                 *fg= blend*input*4.0f;
1728                 *fb= blend;
1729         }
1730         else if (input<=0.50f){ // cyan->green
1731                 *fr= 0.0f;
1732                 *fg= blend;
1733                 *fb= blend*(1.0f-((input-0.25f)*4.0f)); 
1734         }
1735         else if (input<=0.75){  // green->yellow
1736                 *fr= blend * ((input-0.50f)*4.0f);
1737                 *fg= blend;
1738                 *fb= 0.0f;
1739         }
1740         else if (input<=1.0){ // yellow->red
1741                 *fr= blend;
1742                 *fg= blend * (1.0f-((input-0.75f)*4.0f)); 
1743                 *fb= 0.0f;
1744         }
1745 }
1746 static void calc_weightpaint_vert_color(Object *ob, int vert, unsigned char *col)
1747 {
1748         Mesh *me = ob->data;
1749         float fr, fg, fb, input = 0.0f;
1750         int i;
1751
1752         if (me->dvert) {
1753                 for (i=0; i<me->dvert[vert].totweight; i++)
1754                         if (me->dvert[vert].dw[i].def_nr==ob->actdef-1)
1755                                 input+=me->dvert[vert].dw[i].weight;            
1756         }
1757
1758         CLAMP(input, 0.0f, 1.0f);
1759         
1760         weight_to_rgb(input, &fr, &fg, &fb);
1761         
1762         col[3] = (unsigned char)(fr * 255.0f);
1763         col[2] = (unsigned char)(fg * 255.0f);
1764         col[1] = (unsigned char)(fb * 255.0f);
1765         col[0] = 255;
1766 }
1767 static unsigned char *calc_weightpaint_colors(Object *ob) 
1768 {
1769         Mesh *me = ob->data;
1770         MFace *mf = me->mface;
1771         unsigned char *wtcol;
1772         int i;
1773         
1774         wtcol = MEM_callocN (sizeof (unsigned char) * me->totface*4*4, "weightmap");
1775         
1776         memset(wtcol, 0x55, sizeof (unsigned char) * me->totface*4*4);
1777         for (i=0; i<me->totface; i++, mf++){
1778                 calc_weightpaint_vert_color(ob, mf->v1, &wtcol[(i*4 + 0)*4]); 
1779                 calc_weightpaint_vert_color(ob, mf->v2, &wtcol[(i*4 + 1)*4]); 
1780                 calc_weightpaint_vert_color(ob, mf->v3, &wtcol[(i*4 + 2)*4]); 
1781                 if (mf->v4)
1782                         calc_weightpaint_vert_color(ob, mf->v4, &wtcol[(i*4 + 3)*4]); 
1783         }
1784         
1785         return wtcol;
1786 }
1787
1788 static void clear_mesh_caches(Object *ob)
1789 {
1790         Mesh *me= ob->data;
1791
1792                 /* also serves as signal to remake texspace */
1793         if (me->bb) {
1794                 MEM_freeN(me->bb);
1795                 me->bb = NULL;
1796         }
1797
1798         freedisplist(&ob->disp);
1799
1800         if (ob->derivedFinal) {
1801                 ob->derivedFinal->release(ob->derivedFinal);
1802                 ob->derivedFinal= NULL;
1803         }
1804         if (ob->derivedDeform) {
1805                 ob->derivedDeform->release(ob->derivedDeform);
1806                 ob->derivedDeform= NULL;
1807         }
1808 }
1809
1810 static void mesh_build_data(Object *ob)
1811 {
1812         Mesh *me = ob->data;
1813         float min[3], max[3];
1814
1815         if(ob->flag&OB_FROMDUPLI) return;
1816
1817         clear_mesh_caches(ob);
1818
1819         if(ob!=G.obedit) {
1820                 if( (G.f & G_WEIGHTPAINT) && ob==(G.scene->basact?G.scene->basact->object:NULL)) {
1821                         MCol *mcol = me->mcol;
1822                         TFace *tface =  me->tface;
1823
1824                         me->tface = NULL;
1825                         me->mcol = (MCol*) calc_weightpaint_colors(ob);
1826
1827                         mesh_calc_modifiers(ob, NULL, &ob->derivedDeform, &ob->derivedFinal, 0, 1);
1828
1829                         MEM_freeN(me->mcol);
1830                         me->mcol = mcol;
1831                         me->tface = tface;
1832                 } else {
1833                         mesh_calc_modifiers(ob, NULL, &ob->derivedDeform, &ob->derivedFinal, 0, 1);
1834                 }
1835
1836                 INIT_MINMAX(min, max);
1837
1838                 ob->derivedFinal->getMinMax(ob->derivedFinal, min, max);
1839
1840                 boundbox_set_from_min_max(mesh_get_bb(ob->data), min, max);
1841         }
1842 }
1843
1844 static void editmesh_build_data(void)
1845 {
1846         float min[3], max[3];
1847
1848         EditMesh *em = G.editMesh;
1849
1850         clear_mesh_caches(G.obedit);
1851
1852         if (em->derivedFinal) {
1853                 if (em->derivedFinal!=em->derivedCage) {
1854                         em->derivedFinal->release(em->derivedFinal);
1855                 }
1856                 em->derivedFinal = NULL;
1857         }
1858         if (em->derivedCage) {
1859                 em->derivedCage->release(em->derivedCage);
1860                 em->derivedCage = NULL;
1861         }
1862
1863         editmesh_calc_modifiers(&em->derivedCage, &em->derivedFinal);
1864
1865         INIT_MINMAX(min, max);
1866
1867         em->derivedFinal->getMinMax(em->derivedFinal, min, max);
1868
1869         boundbox_set_from_min_max(mesh_get_bb(G.obedit->data), min, max);
1870 }
1871
1872 void makeDispListMesh(Object *ob)
1873 {
1874         if (ob==G.obedit) {
1875                 editmesh_build_data();
1876         } else {
1877                 mesh_build_data(ob);
1878
1879                 build_particle_system(ob);
1880         }
1881 }
1882
1883 /***/
1884
1885 DerivedMesh *mesh_get_derived_final(Object *ob, int *needsFree_r)
1886 {
1887         if (!ob->derivedFinal) {
1888                 mesh_build_data(ob);
1889         }
1890
1891         *needsFree_r = 0;
1892         return ob->derivedFinal;
1893 }
1894
1895 DerivedMesh *mesh_get_derived_deform(Object *ob, int *needsFree_r)
1896 {
1897         if (!ob->derivedDeform) {
1898                 mesh_build_data(ob);
1899         } 
1900
1901         *needsFree_r = 0;
1902         return ob->derivedDeform;
1903 }
1904
1905 DerivedMesh *mesh_create_derived_render(Object *ob)
1906 {
1907         DerivedMesh *final;
1908
1909         mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1);
1910
1911         return final;
1912 }
1913
1914 DerivedMesh *mesh_create_derived_no_deform(Object *ob, float (*vertCos)[3])
1915 {
1916         DerivedMesh *final;
1917
1918         mesh_calc_modifiers(ob, vertCos, NULL, &final, 0, 0);
1919
1920         return final;
1921 }
1922
1923 DerivedMesh *mesh_create_derived_no_deform_render(Object *ob, float (*vertCos)[3])
1924 {
1925         DerivedMesh *final;
1926
1927         mesh_calc_modifiers(ob, vertCos, NULL, &final, 1, 0);
1928
1929         return final;
1930 }
1931
1932 /***/
1933
1934 DerivedMesh *editmesh_get_derived_cage_and_final(DerivedMesh **final_r, int *cageNeedsFree_r, int *finalNeedsFree_r)
1935 {
1936         *cageNeedsFree_r = *finalNeedsFree_r = 0;
1937
1938         if (!G.editMesh->derivedCage)
1939                 editmesh_build_data();
1940
1941         *final_r = G.editMesh->derivedFinal;
1942         return G.editMesh->derivedCage;
1943 }
1944
1945 DerivedMesh *editmesh_get_derived_cage(int *needsFree_r)
1946 {
1947         *needsFree_r = 0;
1948
1949         if (!G.editMesh->derivedCage)
1950                 editmesh_build_data();
1951
1952         return G.editMesh->derivedCage;
1953 }
1954
1955 DerivedMesh *editmesh_get_derived_base(void)
1956 {
1957         return getEditMeshDerivedMesh(G.editMesh, NULL);
1958 }
1959
1960
1961 /* ********* For those who don't grasp derived stuff! (ton) :) *************** */
1962
1963 static void make_vertexcosnos__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
1964 {
1965         float *vec = userData;
1966         
1967         vec+= 6*index;
1968         VECCOPY(vec, co);
1969         vec+= 3;
1970         if(no_f) {
1971                 VECCOPY(vec, no_f);
1972         }
1973         else {
1974                 VECCOPY(vec, no_s);
1975         }
1976 }
1977
1978 /* always returns original amount me->totvert of vertices and normals, but fully deformed and subsurfered */
1979 /* this is needed for all code using vertexgroups (no subsurf support) */
1980 /* it stores the normals as floats, but they can still be scaled as shorts (32767 = unit) */
1981 /* in use now by vertex/weight paint and particle generating */
1982
1983 float *mesh_get_mapped_verts_nors(Object *ob)
1984 {
1985         Mesh *me= ob->data;
1986         DerivedMesh *dm;
1987         float *vertexcosnos;
1988         int needsFree;
1989         
1990         /* lets prevent crashing... */
1991         if(ob->type!=OB_MESH || me->totvert==0)
1992                 return NULL;
1993         
1994         dm= mesh_get_derived_final(ob, &needsFree);
1995         vertexcosnos= MEM_mallocN(6*sizeof(float)*me->totvert, "vertexcosnos map");
1996         
1997         if(dm->foreachMappedVert)
1998                 dm->foreachMappedVert(dm, make_vertexcosnos__mapFunc, vertexcosnos);
1999         else {
2000                 float *fp= vertexcosnos;
2001                 int a;
2002                 
2003                 for(a=0; a< me->totvert; a++, fp+=6) {
2004                         dm->getVertCo(dm, a, fp);
2005                         dm->getVertNo(dm, a, fp+3);
2006                 }
2007         }
2008         
2009         if (needsFree) dm->release(dm);
2010         return vertexcosnos;
2011 }
2012
2013
2014 /* ************************* fluidsim bobj file handling **************************** */
2015
2016 #ifdef WIN32
2017 #ifndef snprintf
2018 #define snprintf _snprintf
2019 #endif
2020 #endif
2021
2022 /* write .bobj.gz file for a mesh object */
2023 void writeBobjgz(char *filename, struct Object *ob) 
2024 {
2025         // const int debugBobjWrite = 0; // now handled by global debug level
2026         char debugStrBuffer[256];
2027         int wri,i,j;
2028         float wrf;
2029         gzFile gzf;
2030         DispListMesh *dlm = NULL;
2031         DerivedMesh *dm;
2032         float vec[3];
2033         float rotmat[3][3];
2034         MFace *mface = NULL;
2035
2036         if(!ob->data || (ob->type!=OB_MESH)) {
2037                 snprintf(debugStrBuffer,256,"Writing GZ_BOBJ Invalid object %s ...\n", ob->id.name); 
2038                 elbeemDebugOut(debugStrBuffer);
2039                 return;
2040         }
2041         if((ob->size[0]<0.0) || (ob->size[0]<0.0) || (ob->size[0]<0.0) ) {
2042                 snprintf(debugStrBuffer,256,"\nfluidSim::writeBobjgz:: Warning object %s has negative scaling - check triangle ordering...?\n\n", ob->id.name); 
2043                 elbeemDebugOut(debugStrBuffer);
2044         }
2045
2046         snprintf(debugStrBuffer,256,"Writing GZ_BOBJ '%s' ... ",filename); elbeemDebugOut(debugStrBuffer); 
2047         gzf = gzopen(filename, "wb9");
2048         if (!gzf) {
2049                 snprintf(debugStrBuffer,256,"writeBobjgz::error - Unable to open file for writing '%s'\n", filename);
2050                 elbeemDebugOut(debugStrBuffer);
2051                 return;
2052         }
2053
2054         dm = mesh_create_derived_render(ob);
2055         dlm = dm->convertToDispListMesh(dm, 1);
2056         mface = dlm->mface;
2057
2058         if(sizeof(wri)!=4) { snprintf(debugStrBuffer,256,"Writing GZ_BOBJ, Invalid int size %d...\n", wri); elbeemDebugOut(debugStrBuffer); return; } // paranoia check
2059         wri = dlm->totvert;
2060         gzwrite(gzf, &wri, sizeof(wri));
2061         for(i=0; i<wri;i++) {
2062                 VECCOPY(vec, dlm->mvert[i].co); /* get transformed point */
2063                 Mat4MulVecfl(ob->obmat, vec);
2064                 for(j=0; j<3; j++) {
2065                         wrf = vec[j]; 
2066                         gzwrite(gzf, &wrf, sizeof( wrf )); 
2067                 }
2068         }
2069
2070         // should be the same as Vertices.size
2071         wri = dlm->totvert;
2072         gzwrite(gzf, &wri, sizeof(wri));
2073         EulToMat3(ob->rot, rotmat);
2074         for(i=0; i<wri;i++) {
2075                 VECCOPY(vec, dlm->mvert[i].no);
2076                 // FIXME divide? mv->no[0]= (short)(no[0]*32767.0);
2077                 Mat3MulVecfl(rotmat, vec); 
2078                 Normalise(vec);
2079                 for(j=0; j<3; j++) {
2080                         wrf = vec[j];
2081                         gzwrite(gzf, &wrf, sizeof( wrf )); 
2082                 }
2083         }
2084
2085         
2086         /* compute no. of triangles */
2087         wri = 0;
2088         for(i=0; i<dlm->totface; i++) {
2089                 wri++;
2090                 if(mface[i].v4) { wri++; }
2091         }
2092         gzwrite(gzf, &wri, sizeof(wri));
2093         for(i=0; i<dlm->totface; i++) {
2094
2095                 int face[4];
2096                 face[0] = mface[i].v1;
2097                 face[1] = mface[i].v2;
2098                 face[2] = mface[i].v3;
2099                 face[3] = mface[i].v4;
2100                 //snprintf(debugStrBuffer,256,"F %s %d = %d,%d,%d,%d \n",ob->id.name, i, face[0],face[1],face[2],face[3] ); elbeemDebugOut(debugStrBuffer);
2101
2102                 gzwrite(gzf, &(face[0]), sizeof( face[0] )); 
2103                 gzwrite(gzf, &(face[1]), sizeof( face[1] )); 
2104                 gzwrite(gzf, &(face[2]), sizeof( face[2] )); 
2105                 if(face[3]) { 
2106                         gzwrite(gzf, &(face[0]), sizeof( face[0] )); 
2107                         gzwrite(gzf, &(face[2]), sizeof( face[2] )); 
2108                         gzwrite(gzf, &(face[3]), sizeof( face[3] )); 
2109                 }
2110         }
2111
2112         snprintf(debugStrBuffer,256,"Done. #Vertices: %d, #Triangles: %d\n", dlm->totvert, dlm->totface ); 
2113         elbeemDebugOut(debugStrBuffer);
2114         
2115         gzclose( gzf );
2116         if(dlm) displistmesh_free(dlm);
2117         dm->release(dm);
2118 }
2119
2120 /* read .bobj.gz file into a fluidsimDerivedMesh struct */
2121 Mesh* readBobjgz(char *filename, Mesh *orgmesh) //, fluidsimDerivedMesh *fsdm)
2122 {
2123         int wri,i,j;
2124         char debugStrBuffer[256];
2125         float wrf;
2126         Mesh *newmesh; 
2127         const int debugBobjRead = 1;
2128         // init data from old mesh (materials,flags)
2129         MFace *origMFace = &((MFace*) orgmesh->mface)[0];
2130         int mat_nr = -1;
2131         int flag = -1;
2132         MFace *fsface = NULL;
2133         int gotBytes;
2134         gzFile gzf;
2135
2136         if(!orgmesh) return NULL;
2137         if(!origMFace) return NULL;
2138         mat_nr = origMFace->mat_nr;
2139         flag = origMFace->flag;
2140
2141         // similar to copy_mesh
2142         newmesh = MEM_dupallocN(orgmesh);
2143         newmesh->mat= orgmesh->mat;
2144
2145         newmesh->mvert= NULL;
2146         newmesh->medge= NULL;
2147         newmesh->mface= NULL;
2148         newmesh->tface= NULL;
2149         newmesh->dface= NULL;
2150
2151         newmesh->dvert = NULL;
2152
2153         newmesh->mcol= NULL;
2154         newmesh->msticky= NULL;
2155         newmesh->texcomesh= NULL;
2156
2157         newmesh->key= NULL;
2158         newmesh->totface = 0;
2159         newmesh->totvert = 0;
2160         newmesh->totedge = 0;
2161         newmesh->medge = NULL;
2162
2163
2164         snprintf(debugStrBuffer,256,"Reading '%s' GZ_BOBJ... ",filename); elbeemDebugOut(debugStrBuffer); 
2165         gzf = gzopen(filename, "rb");
2166         // gzf = fopen(filename, "rb");
2167         // debug: fread(b,c,1,a) = gzread(a,b,c)
2168         if (!gzf) {
2169                 //snprintf(debugStrBuffer,256,"readBobjgz::error - Unable to open file for reading '%s'\n", filename); // DEBUG
2170                 MEM_freeN(newmesh);
2171                 return NULL;
2172         }
2173
2174         //if(sizeof(wri)!=4) { snprintf(debugStrBuffer,256,"Reading GZ_BOBJ, Invalid int size %d...\n", wri); return NULL; } // paranoia check
2175         gotBytes = gzread(gzf, &wri, sizeof(wri));
2176         newmesh->totvert = wri;
2177         newmesh->mvert = MEM_callocN(sizeof(MVert)*newmesh->totvert, "fluidsimDerivedMesh_bobjvertices");
2178         if(debugBobjRead){ snprintf(debugStrBuffer,256,"#vertices %d ", newmesh->totvert); elbeemDebugOut(debugStrBuffer); } //DEBUG
2179         for(i=0; i<newmesh->totvert;i++) {
2180                 //if(debugBobjRead) snprintf(debugStrBuffer,256,"V %d = ",i);
2181                 for(j=0; j<3; j++) {
2182                         gotBytes = gzread(gzf, &wrf, sizeof( wrf )); 
2183                         newmesh->mvert[i].co[j] = wrf;
2184                         //if(debugBobjRead) snprintf(debugStrBuffer,256,"%25.20f ", wrf);
2185                 }
2186                 //if(debugBobjRead) snprintf(debugStrBuffer,256,"\n");
2187         }
2188
2189         // should be the same as Vertices.size
2190         gotBytes = gzread(gzf, &wri, sizeof(wri));
2191         if(wri != newmesh->totvert) {
2192                 // complain #vertices has to be equal to #normals, reset&abort
2193                 MEM_freeN(newmesh->mvert);
2194                 MEM_freeN(newmesh);
2195                 snprintf(debugStrBuffer,256,"Reading GZ_BOBJ, #normals=%d, #vertices=%d, aborting...\n", wri,newmesh->totvert );
2196                 return NULL;
2197         }
2198         for(i=0; i<newmesh->totvert;i++) {
2199                 for(j=0; j<3; j++) {
2200                         gotBytes = gzread(gzf, &wrf, sizeof( wrf )); 
2201                         newmesh->mvert[i].no[j] = wrf*32767.0;
2202                 }
2203         }
2204
2205         
2206         /* compute no. of triangles */
2207         gotBytes = gzread(gzf, &wri, sizeof(wri));
2208         newmesh->totface = wri;
2209         newmesh->mface = MEM_callocN(sizeof(MFace)*newmesh->totface, "fluidsimDerivedMesh_bobjfaces");
2210         if(debugBobjRead){ snprintf(debugStrBuffer,256,"#faces %d ", newmesh->totface); elbeemDebugOut(debugStrBuffer); } //DEBUG
2211         fsface = newmesh->mface;
2212         for(i=0; i<newmesh->totface; i++) {
2213                 int face[4];
2214
2215                 gotBytes = gzread(gzf, &(face[0]), sizeof( face[0] )); 
2216                 gotBytes = gzread(gzf, &(face[1]), sizeof( face[1] )); 
2217                 gotBytes = gzread(gzf, &(face[2]), sizeof( face[2] )); 
2218                 face[3] = 0;
2219
2220                 fsface[i].v1 = face[0];
2221                 fsface[i].v2 = face[1];
2222                 fsface[i].v3 = face[2];
2223                 fsface[i].v4 = face[3];
2224         }
2225
2226         // correct triangles with v3==0 for blender, cycle verts
2227         for(i=0; i<newmesh->totface; i++) {
2228                 if(!fsface[i].v3) {
2229                         int temp = fsface[i].v1;
2230                         fsface[i].v1 = fsface[i].v2;
2231                         fsface[i].v2 = fsface[i].v3;
2232                         fsface[i].v3 = temp;
2233                 }
2234         }
2235         
2236         gzclose( gzf );
2237         for(i=0;i<newmesh->totface;i++) { 
2238                 fsface[i].mat_nr = mat_nr;
2239                 fsface[i].flag = flag;
2240                 fsface[i].edcode = ME_V1V2 | ME_V2V3 | ME_V3V1;
2241                 //snprintf(debugStrBuffer,256,"%d : %d,%d,%d\n", i,fsface[i].mat_nr, fsface[i].flag, fsface[i].edcode );
2242         }
2243
2244         snprintf(debugStrBuffer,256," (%d,%d) done\n", newmesh->totvert,newmesh->totface); elbeemDebugOut(debugStrBuffer); //DEBUG
2245         return newmesh;
2246 }
2247
2248
2249 /* ***************************** fluidsim derived mesh ***************************** */
2250
2251 typedef struct {
2252         MeshDerivedMesh mdm;
2253
2254         /* release whole mesh? */
2255         char freeMesh;
2256 } FluidsimDerivedMesh;
2257
2258 static void fluidsimDM_release(DerivedMesh *dm)
2259 {
2260         FluidsimDerivedMesh *fsdm = (FluidsimDerivedMesh*) dm;
2261         if(fsdm->freeMesh) {
2262                 // similar to free_mesh(fsdm->mdm.me) , but no things like unlink...
2263                 if(fsdm->mdm.me->mvert) MEM_freeN(fsdm->mdm.me->mvert);
2264                 if(fsdm->mdm.me->medge) MEM_freeN(fsdm->mdm.me->medge);
2265                 if(fsdm->mdm.me->mface) MEM_freeN(fsdm->mdm.me->mface);
2266                 MEM_freeN(fsdm->mdm.me);
2267         }
2268
2269         if (fsdm->mdm.freeNors) MEM_freeN(fsdm->mdm.nors);
2270         if (fsdm->mdm.freeVerts) MEM_freeN(fsdm->mdm.verts);
2271         MEM_freeN(fsdm);
2272 }
2273
2274 DerivedMesh *getFluidsimDerivedMesh(Object *srcob, int useRenderParams, float *extverts, float *nors) 
2275 {
2276         Mesh *mesh = NULL;
2277         FluidsimDerivedMesh *fsdm;
2278         MeshDerivedMesh *mdm = NULL;
2279         float (*vertCos)[3];
2280         int displaymode = 0;
2281         int curFrame = G.scene->r.cfra - 1; /* start with 0 */
2282         char targetDir[FILE_MAXFILE+FILE_MAXDIR], targetFile[FILE_MAXFILE+FILE_MAXDIR];
2283         char debugStrBuffer[256];
2284         //snprintf(debugStrBuffer,256,"getFluidsimDerivedMesh call (obid '%s', rp %d)\n", srcob->id.name, useRenderParams); // debug
2285
2286         if((!srcob)||(!srcob->fluidsimSettings)) {
2287                 fprintf(stderr,"??? DEBUG, strange getFluidsimDerivedMesh call!\n\n"); return NULL;
2288         }
2289         
2290         if(!useRenderParams) {
2291                 displaymode = srcob->fluidsimSettings->guiDisplayMode;
2292         } else {
2293                 displaymode = srcob->fluidsimSettings->renderDisplayMode;
2294         }
2295         
2296         snprintf(debugStrBuffer,256,"getFluidsimDerivedMesh call (obid '%s', rp %d, dm %d)\n", srcob->id.name, useRenderParams, displaymode); // debug
2297         elbeemDebugOut(debugStrBuffer); // debug
2298
2299         strncpy(targetDir, srcob->fluidsimSettings->surfdataPath, FILE_MAXDIR);
2300         // use preview or final mesh?
2301         if(displaymode==2) {
2302                 strcat(targetDir,"fluidsurface_preview_#");
2303         } else {
2304                 strcat(targetDir,"fluidsurface_final_#");
2305         }
2306         BLI_convertstringcode(targetDir, G.sce, curFrame); // fixed #frame-no 
2307         strcpy(targetFile,targetDir);
2308         strcat(targetFile, ".bobj.gz");
2309         //fprintf(stderr,"getFluidsimDerivedMesh call (obid '', rp %d, dm %d) '%s' \n", useRenderParams, displaymode, targetFile);  // debug
2310
2311         snprintf(debugStrBuffer,256,"getFluidsimDerivedMesh call (obid '%s', rp %d, dm %d) '%s' \n", srcob->id.name, useRenderParams, displaymode, targetFile);  // debug
2312         elbeemDebugOut(debugStrBuffer); // debug
2313
2314         mesh = readBobjgz(targetFile, (Mesh*)(srcob->data) );
2315         if(!mesh) {
2316                 // display org. object upon failure
2317                 mesh = srcob->data;                     
2318                 return getMeshDerivedMesh(mesh , srcob, NULL);
2319         }
2320
2321         if((mesh)&&(mesh->totvert>0)) {
2322                 make_edges(mesh, 0);    // 0 = make all edges draw
2323         }
2324
2325         // WARNING copied from getMeshDerivedMesh
2326         fsdm = MEM_callocN(sizeof(*fsdm), "getFluidsimDerivedMesh_fsdm");
2327         fsdm->freeMesh = 1;
2328         mdm = &fsdm->mdm;
2329         vertCos = NULL;
2330
2331         mdm->dm.getMinMax = meshDM_getMinMax;
2332         mdm->dm.convertToDispListMesh = meshDM_convertToDispListMesh;
2333         mdm->dm.getNumVerts = meshDM_getNumVerts;
2334         mdm->dm.getNumFaces = meshDM_getNumFaces;
2335         mdm->dm.getVertCos = meshDM_getVertCos;
2336         mdm->dm.getVertCo = meshDM_getVertCo;
2337         mdm->dm.getVertNo = meshDM_getVertNo;
2338         mdm->dm.drawVerts = meshDM_drawVerts;
2339         mdm->dm.drawUVEdges = meshDM_drawUVEdges;
2340         mdm->dm.drawEdges = meshDM_drawEdges;
2341         mdm->dm.drawLooseEdges = meshDM_drawLooseEdges;
2342         mdm->dm.drawFacesSolid = meshDM_drawFacesSolid;
2343         mdm->dm.drawFacesColored = meshDM_drawFacesColored;
2344         mdm->dm.drawFacesTex = meshDM_drawFacesTex;
2345         mdm->dm.drawMappedFaces = meshDM_drawMappedFaces;
2346         mdm->dm.drawMappedEdges = meshDM_drawMappedEdges;
2347         mdm->dm.drawMappedFaces = meshDM_drawMappedFaces;
2348
2349         // use own release function
2350         mdm->dm.release = fluidsimDM_release;
2351         
2352         mdm->ob = srcob;
2353         mdm->me = mesh;
2354         mdm->verts = mesh->mvert;
2355         mdm->nors = NULL;
2356         mdm->freeNors = 0;
2357         mdm->freeVerts = 0;
2358         
2359         // if (vertCos) { not needed for fluid meshes... 
2360         // this is kinda ... see getMeshDerivedMesh
2361         mesh_calc_normals(mdm->verts, mdm->me->totvert, mdm->me->mface, mdm->me->totface, &mdm->nors);
2362         mdm->freeNors = 1;
2363         return (DerivedMesh*) mdm;
2364 }
2365
2366