- support for fake verse edges (subsurf modifier should work now)
[blender.git] / source / blender / blenkernel / intern / cdderivedmesh.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * The Original Code is Copyright (C) 2006 Blender Foundation.
21 * All rights reserved.
22 *
23 * The Original Code is: all of this file.
24 *
25 * Contributor(s): Ben Batt <benbatt@gmail.com>
26 *
27 * ***** END GPL LICENSE BLOCK *****
28 *
29 * Implementation of CDDerivedMesh.
30 *
31 * BKE_cdderivedmesh.h contains the function prototypes for this file.
32 *
33 */ 
34
35 /* TODO maybe BIF_gl.h should include string.h? */
36 #include <string.h>
37 #include "BIF_gl.h"
38
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_customdata.h"
41 #include "BKE_DerivedMesh.h"
42 #include "BKE_displist.h"
43 #include "BKE_global.h"
44 #include "BKE_mesh.h"
45 #include "BKE_utildefines.h"
46
47 #include "BLI_arithb.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_edgehash.h"
50 #include "BLI_editVert.h"
51 #include "BLI_ghash.h"
52
53 #include "DNA_mesh_types.h"
54 #include "DNA_meshdata_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_scene_types.h"
57
58 #include "MEM_guardedalloc.h"
59
60 #include <string.h>
61 #include <limits.h>
62
63 typedef struct {
64         DerivedMesh dm;
65
66         /* these point to data in the DerivedMesh custom data layers,
67            they are only here for efficiency and convenience **/
68         MVert *mvert;
69         MEdge *medge;
70         MFace *mface;
71 } CDDerivedMesh;
72
73 /**************** DerivedMesh interface functions ****************/
74 static int cdDM_getNumVerts(DerivedMesh *dm)
75 {
76         return dm->numVertData;
77 }
78
79 static int cdDM_getNumEdges(DerivedMesh *dm)
80 {
81         return dm->numEdgeData;
82 }
83
84 static int cdDM_getNumFaces(DerivedMesh *dm)
85 {
86         return dm->numFaceData;
87 }
88
89 static void cdDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
90 {
91         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
92         *vert_r = cddm->mvert[index];
93 }
94
95 static void cdDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
96 {
97         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
98         *edge_r = cddm->medge[index];
99 }
100
101 static void cdDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
102 {
103         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
104         *face_r = cddm->mface[index];
105 }
106
107 static void cdDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
108 {
109         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
110         memcpy(vert_r, cddm->mvert, sizeof(*vert_r) * dm->numVertData);
111 }
112
113 static void cdDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
114 {
115         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
116         memcpy(edge_r, cddm->medge, sizeof(*edge_r) * dm->numEdgeData);
117 }
118
119 void cdDM_copyFaceArray(DerivedMesh *dm, MFace *face_r)
120 {
121         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
122         memcpy(face_r, cddm->mface, sizeof(*face_r) * dm->numFaceData);
123 }
124
125 static void cdDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
126 {
127         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
128         int i;
129
130         if (dm->numVertData) {
131                 for (i=0; i<dm->numVertData; i++) {
132                         DO_MINMAX(cddm->mvert[i].co, min_r, max_r);
133                 }
134         } else {
135                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
136         }
137 }
138
139 static void cdDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
140 {
141         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
142
143         VECCOPY(co_r, cddm->mvert[index].co);
144 }
145
146 static void cdDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
147 {
148         MVert *mv = CDDM_get_verts(dm);
149         int i;
150
151         for(i = 0; i < dm->numVertData; i++, mv++)
152                 VECCOPY(cos_r[i], mv->co);
153 }
154
155 static void cdDM_getVertNo(DerivedMesh *dm, int index, float no_r[3])
156 {
157         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
158         short *no = cddm->mvert[index].no;
159
160         no_r[0] = no[0]/32767.f;
161         no_r[1] = no[1]/32767.f;
162         no_r[2] = no[2]/32767.f;
163 }
164
165 static void cdDM_drawVerts(DerivedMesh *dm)
166 {
167         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
168         MVert *mv = cddm->mvert;
169         int i;
170
171         glBegin(GL_POINTS);
172         for(i = 0; i < dm->numVertData; i++, mv++)
173                 glVertex3fv(mv->co);
174         glEnd();
175 }
176
177 static void cdDM_drawUVEdges(DerivedMesh *dm)
178 {
179         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
180         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
181         MFace *mf = cddm->mface;
182         int i;
183
184         if(tf) {
185                 glBegin(GL_LINES);
186                 for(i = 0; i < dm->numFaceData; i++, tf++, mf++) {
187                         if(!(tf->flag&TF_HIDE)) {
188                                 glVertex2fv(tf->uv[0]);
189                                 glVertex2fv(tf->uv[1]);
190
191                                 glVertex2fv(tf->uv[1]);
192                                 glVertex2fv(tf->uv[2]);
193
194                                 if(!mf->v4) {
195                                         glVertex2fv(tf->uv[2]);
196                                         glVertex2fv(tf->uv[0]);
197                                 } else {
198                                         glVertex2fv(tf->uv[2]);
199                                         glVertex2fv(tf->uv[3]);
200
201                                         glVertex2fv(tf->uv[3]);
202                                         glVertex2fv(tf->uv[0]);
203                                 }
204                         }
205                 }
206                 glEnd();
207         }
208 }
209
210 static void cdDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
211 {
212         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
213         MVert *mvert = cddm->mvert;
214         MEdge *medge = cddm->medge;
215         int i;
216                 
217         glBegin(GL_LINES);
218         for(i = 0; i < dm->numEdgeData; i++, medge++) {
219                 if((medge->flag&ME_EDGEDRAW)
220                    && (drawLooseEdges || !(medge->flag&ME_LOOSEEDGE))) {
221                         glVertex3fv(mvert[medge->v1].co);
222                         glVertex3fv(mvert[medge->v2].co);
223                 }
224         }
225         glEnd();
226 }
227
228 static void cdDM_drawLooseEdges(DerivedMesh *dm)
229 {
230         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
231         MVert *mvert = cddm->mvert;
232         MEdge *medge = cddm->medge;
233         int i;
234
235         glBegin(GL_LINES);
236         for(i = 0; i < dm->numEdgeData; i++, medge++) {
237                 if(medge->flag&ME_LOOSEEDGE) {
238                         glVertex3fv(mvert[medge->v1].co);
239                         glVertex3fv(mvert[medge->v2].co);
240                 }
241         }
242         glEnd();
243 }
244
245 static void cdDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
246 {
247         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
248         MVert *mvert = cddm->mvert;
249         MFace *mface = cddm->mface;
250         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
251         int a, glmode = -1, shademodel = -1, matnr = -1, drawCurrentMat = 1;
252
253 #define PASSVERT(index) {                                               \
254         if(shademodel == GL_SMOOTH) {                           \
255                 short *no = mvert[index].no;                    \
256                 glNormal3sv(no);                                                \
257         }                                                                                       \
258         glVertex3fv(mvert[index].co);   \
259 }
260
261         glBegin(glmode = GL_QUADS);
262         for(a = 0; a < dm->numFaceData; a++, mface++) {
263                 int new_glmode, new_matnr, new_shademodel;
264
265                 new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
266                 new_matnr = mface->mat_nr + 1;
267                 new_shademodel = (mface->flag & ME_SMOOTH)?GL_SMOOTH:GL_FLAT;
268                 
269                 if(new_glmode != glmode || new_matnr != matnr
270                    || new_shademodel != shademodel) {
271                         glEnd();
272
273                         drawCurrentMat = setMaterial(matnr = new_matnr);
274
275                         glShadeModel(shademodel = new_shademodel);
276                         glBegin(glmode = new_glmode);
277                 } 
278                 
279                 if(drawCurrentMat) {
280                         if(shademodel == GL_FLAT) {
281                                 if (nors) {
282                                         glNormal3fv(nors);
283                                 }
284                                 else {
285                                         /* TODO make this better (cache facenormals as layer?) */
286                                         float nor[3];
287                                         if(mface->v4) {
288                                                 CalcNormFloat4(mvert[mface->v1].co, mvert[mface->v2].co,
289                                                                            mvert[mface->v3].co, mvert[mface->v4].co,
290                                                                            nor);
291                                         } else {
292                                                 CalcNormFloat(mvert[mface->v1].co, mvert[mface->v2].co,
293                                                                           mvert[mface->v3].co, nor);
294                                         }
295                                         glNormal3fv(nor);
296                                 }
297                         }
298
299                         PASSVERT(mface->v1);
300                         PASSVERT(mface->v2);
301                         PASSVERT(mface->v3);
302                         if(mface->v4) {
303                                 PASSVERT(mface->v4);
304                         }
305                 }
306
307                 if(nors) nors += 3;
308         }
309         glEnd();
310
311         glShadeModel(GL_FLAT);
312 #undef PASSVERT
313 }
314
315 static void cdDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2)
316 {
317         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
318         int a, glmode;
319         unsigned char *cp1, *cp2;
320         MVert *mvert = cddm->mvert;
321         MFace *mface = cddm->mface;
322
323         cp1 = col1;
324         if(col2) {
325                 cp2 = col2;
326         } else {
327                 cp2 = NULL;
328                 useTwoSided = 0;
329         }
330
331         /* there's a conflict here... twosided colors versus culling...? */
332         /* defined by history, only texture faces have culling option */
333         /* we need that as mesh option builtin, next to double sided lighting */
334         if(col1 && col2)
335                 glEnable(GL_CULL_FACE);
336         
337         glShadeModel(GL_SMOOTH);
338         glBegin(glmode = GL_QUADS);
339         for(a = 0; a < dm->numFaceData; a++, mface++, cp1 += 16) {
340                 int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
341
342                 if(new_glmode != glmode) {
343                         glEnd();
344                         glBegin(glmode = new_glmode);
345                 }
346                         
347                 glColor3ub(cp1[0], cp1[1], cp1[2]);
348                 glVertex3fv(mvert[mface->v1].co);
349                 glColor3ub(cp1[4], cp1[5], cp1[6]);
350                 glVertex3fv(mvert[mface->v2].co);
351                 glColor3ub(cp1[8], cp1[9], cp1[10]);
352                 glVertex3fv(mvert[mface->v3].co);
353                 if(mface->v4) {
354                         glColor3ub(cp1[12], cp1[13], cp1[14]);
355                         glVertex3fv(mvert[mface->v4].co);
356                 }
357                         
358                 if(useTwoSided) {
359                         glColor3ub(cp2[8], cp2[9], cp2[10]);
360                         glVertex3fv(mvert[mface->v3].co );
361                         glColor3ub(cp2[4], cp2[5], cp2[6]);
362                         glVertex3fv(mvert[mface->v2].co );
363                         glColor3ub(cp2[0], cp2[1], cp2[2]);
364                         glVertex3fv(mvert[mface->v1].co );
365                         if(mface->v4) {
366                                 glColor3ub(cp2[12], cp2[13], cp2[14]);
367                                 glVertex3fv(mvert[mface->v4].co );
368                         }
369                 }
370                 if(col2) cp2 += 16;
371         }
372         glEnd();
373
374         glShadeModel(GL_FLAT);
375         glDisable(GL_CULL_FACE);
376 }
377
378 static void cdDM_drawFacesTex_common(DerivedMesh *dm,
379                int (*drawParams)(MTFace *tface, MCol *mcol, int matnr),
380                int (*drawParamsMapped)(void *userData, int index),
381                void *userData) 
382 {
383         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
384         MVert *mv = cddm->mvert;
385         MFace *mf = cddm->mface;
386         MCol *mcol = dm->getFaceDataArray(dm, CD_MCOL);
387         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
388         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
389         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
390
391         for(i = 0; i < dm->numFaceData; i++, mf++) {
392                 MVert *mvert;
393                 int flag;
394                 unsigned char *cp = NULL;
395
396                 if(drawParams) {
397                         flag = drawParams(tf? &tf[i]: NULL, mcol? &mcol[i*4]: NULL, mf->mat_nr);
398                 }
399                 else {
400                         if(index) {
401                                 orig = *index++;
402                                 if(orig == ORIGINDEX_NONE) continue;
403                                 if(drawParamsMapped) flag = drawParamsMapped(userData, orig);
404                                 else continue;
405                         }
406                         else
407                                 if(drawParamsMapped) flag = drawParamsMapped(userData, i);
408                                 else continue;
409                 }
410
411                 if(flag == 0)
412                         continue;
413                 else if (flag==1 && mcol)
414                         cp= (unsigned char*) &mcol[i*4];
415
416                 if(!(mf->flag&ME_SMOOTH)) {
417                         if (nors) {
418                                 glNormal3fv(nors);
419                         }
420                         else {
421                                 /* TODO make this better (cache facenormals as layer?) */
422                                 float nor[3];
423                                 if(mf->v4) {
424                                         CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
425                                                                    mv[mf->v3].co, mv[mf->v4].co,
426                                                                    nor);
427                                 } else {
428                                         CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
429                                                                   mv[mf->v3].co, nor);
430                                 }
431                                 glNormal3fv(nor);
432                         }
433                 }
434
435                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
436                 if(tf) glTexCoord2fv(tf[i].uv[0]);
437                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
438                 mvert = &mv[mf->v1];
439                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
440                 glVertex3fv(mvert->co);
441                         
442                 if(tf) glTexCoord2fv(tf[i].uv[1]);
443                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
444                 mvert = &mv[mf->v2];
445                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
446                 glVertex3fv(mvert->co);
447
448                 if(tf) glTexCoord2fv(tf[i].uv[2]);
449                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
450                 mvert = &mv[mf->v3];
451                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
452                 glVertex3fv(mvert->co);
453
454                 if(mf->v4) {
455                         if(tf) glTexCoord2fv(tf[i].uv[3]);
456                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
457                         mvert = &mv[mf->v4];
458                         if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
459                         glVertex3fv(mvert->co);
460                 }
461                 glEnd();
462
463                 if(nors) nors += 3;
464         }
465 }
466
467 static void cdDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
468 {
469         cdDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
470 }
471
472 static void cdDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors)
473 {
474         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
475         MVert *mv = cddm->mvert;
476         MFace *mf = cddm->mface;
477         MCol *mc = DM_get_face_data_layer(dm, CD_MCOL);
478         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
479         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
480
481         for(i = 0; i < dm->numFaceData; i++, mf++) {
482                 int drawSmooth = (mf->flag & ME_SMOOTH);
483
484                 if(index) {
485                         orig = *index++;
486                         if(setDrawOptions && orig == ORIGINDEX_NONE) continue;
487                 }
488                 else
489                         orig = i;
490
491                 if(!setDrawOptions || setDrawOptions(userData, orig, &drawSmooth)) {
492                         unsigned char *cp = NULL;
493
494                         if(useColors && mc)
495                                 cp = (unsigned char *)&mc[i * 4];
496
497                         glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
498                         glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
499
500                         if (!drawSmooth) {
501                                 if (nors) {
502                                         glNormal3fv(nors);
503                                 }
504                                 else {
505                                         /* TODO make this better (cache facenormals as layer?) */
506                                         float nor[3];
507                                         if(mf->v4) {
508                                                 CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
509                                                                            mv[mf->v3].co, mv[mf->v4].co,
510                                                                            nor);
511                                         } else {
512                                                 CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
513                                                                           mv[mf->v3].co, nor);
514                                         }
515                                         glNormal3fv(nor);
516                                 }
517
518                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
519                                 glVertex3fv(mv[mf->v1].co);
520                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
521                                 glVertex3fv(mv[mf->v2].co);
522                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
523                                 glVertex3fv(mv[mf->v3].co);
524                                 if(mf->v4) {
525                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
526                                         glVertex3fv(mv[mf->v4].co);
527                                 }
528                         } else {
529                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
530                                 glNormal3sv(mv[mf->v1].no);
531                                 glVertex3fv(mv[mf->v1].co);
532                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
533                                 glNormal3sv(mv[mf->v2].no);
534                                 glVertex3fv(mv[mf->v2].co);
535                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
536                                 glNormal3sv(mv[mf->v3].no);
537                                 glVertex3fv(mv[mf->v3].co);
538                                 if(mf->v4) {
539                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
540                                         glNormal3sv(mv[mf->v4].no);
541                                         glVertex3fv(mv[mf->v4].co);
542                                 }
543                         }
544
545                         glEnd();
546                 }
547                 
548                 if (nors) nors += 3;
549         }
550 }
551
552 static void cdDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
553 {
554         cdDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
555 }
556
557 static void cdDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
558 {
559         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
560         MVert *vert = cddm->mvert;
561         MEdge *edge = cddm->medge;
562         int i, orig, *index = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
563
564         glBegin(GL_LINES);
565         for(i = 0; i < dm->numEdgeData; i++, edge++) {
566                 if(index) {
567                         orig = *index++;
568                         if(setDrawOptions && orig == ORIGINDEX_NONE) continue;
569                 }
570                 else
571                         orig = i;
572
573                 if(!setDrawOptions || setDrawOptions(userData, orig)) {
574                         glVertex3fv(vert[edge->v1].co);
575                         glVertex3fv(vert[edge->v2].co);
576                 }
577         }
578         glEnd();
579 }
580
581 static void cdDM_foreachMappedVert(
582                            DerivedMesh *dm,
583                            void (*func)(void *userData, int index, float *co,
584                                         float *no_f, short *no_s),
585                            void *userData)
586 {
587         MVert *mv = CDDM_get_verts(dm);
588         int i, orig, *index = DM_get_vert_data_layer(dm, CD_ORIGINDEX);
589
590         for(i = 0; i < dm->numVertData; i++, mv++) {
591                 if(index) {
592                         orig = *index++;
593                         if(orig == ORIGINDEX_NONE) continue;
594                         func(userData, orig, mv->co, NULL, mv->no);
595                 }
596                 else
597                         func(userData, i, mv->co, NULL, mv->no);
598         }
599 }
600
601 static void cdDM_foreachMappedEdge(
602                            DerivedMesh *dm,
603                            void (*func)(void *userData, int index,
604                                         float *v0co, float *v1co),
605                            void *userData)
606 {
607         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
608         MVert *mv = cddm->mvert;
609         MEdge *med = cddm->medge;
610         int i, orig, *index = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
611
612         for(i = 0; i < dm->numEdgeData; i++, med++) {
613                 if (index) {
614                         orig = *index++;
615                         if(orig == ORIGINDEX_NONE) continue;
616                         func(userData, orig, mv[med->v1].co, mv[med->v2].co);
617                 }
618                 else
619                         func(userData, i, mv[med->v1].co, mv[med->v2].co);
620         }
621 }
622
623 static void cdDM_foreachMappedFaceCenter(
624                            DerivedMesh *dm,
625                            void (*func)(void *userData, int index,
626                                         float *cent, float *no),
627                            void *userData)
628 {
629         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
630         MVert *mv = cddm->mvert;
631         MFace *mf = cddm->mface;
632         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
633
634         for(i = 0; i < dm->numFaceData; i++, mf++) {
635                 float cent[3];
636                 float no[3];
637
638                 if (index) {
639                         orig = *index++;
640                         if(orig == ORIGINDEX_NONE) continue;
641                 }
642                 else
643                         orig = i;
644
645                 VECCOPY(cent, mv[mf->v1].co);
646                 VecAddf(cent, cent, mv[mf->v2].co);
647                 VecAddf(cent, cent, mv[mf->v3].co);
648
649                 if (mf->v4) {
650                         CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
651                                        mv[mf->v3].co, mv[mf->v4].co, no);
652                         VecAddf(cent, cent, mv[mf->v4].co);
653                         VecMulf(cent, 0.25f);
654                 } else {
655                         CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
656                                       mv[mf->v3].co, no);
657                         VecMulf(cent, 0.33333333333f);
658                 }
659
660                 func(userData, orig, cent, no);
661         }
662 }
663
664 static void cdDM_release(DerivedMesh *dm)
665 {
666         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
667
668         if (DM_release(dm))
669                 MEM_freeN(cddm);
670 }
671
672 /**************** CDDM interface functions ****************/
673 static CDDerivedMesh *cdDM_create(const char *desc)
674 {
675         CDDerivedMesh *cddm;
676         DerivedMesh *dm;
677
678         cddm = MEM_callocN(sizeof(*cddm), desc);
679         dm = &cddm->dm;
680
681         dm->getMinMax = cdDM_getMinMax;
682
683         dm->getNumVerts = cdDM_getNumVerts;
684         dm->getNumFaces = cdDM_getNumFaces;
685         dm->getNumEdges = cdDM_getNumEdges;
686
687         dm->getVert = cdDM_getVert;
688         dm->getEdge = cdDM_getEdge;
689         dm->getFace = cdDM_getFace;
690         dm->copyVertArray = cdDM_copyVertArray;
691         dm->copyEdgeArray = cdDM_copyEdgeArray;
692         dm->copyFaceArray = cdDM_copyFaceArray;
693         dm->getVertData = DM_get_vert_data;
694         dm->getEdgeData = DM_get_edge_data;
695         dm->getFaceData = DM_get_face_data;
696         dm->getVertDataArray = DM_get_vert_data_layer;
697         dm->getEdgeDataArray = DM_get_edge_data_layer;
698         dm->getFaceDataArray = DM_get_face_data_layer;
699
700         dm->getVertCos = cdDM_getVertCos;
701         dm->getVertCo = cdDM_getVertCo;
702         dm->getVertNo = cdDM_getVertNo;
703
704         dm->drawVerts = cdDM_drawVerts;
705
706         dm->drawUVEdges = cdDM_drawUVEdges;
707         dm->drawEdges = cdDM_drawEdges;
708         dm->drawLooseEdges = cdDM_drawLooseEdges;
709         dm->drawMappedEdges = cdDM_drawMappedEdges;
710
711         dm->drawFacesSolid = cdDM_drawFacesSolid;
712         dm->drawFacesColored = cdDM_drawFacesColored;
713         dm->drawFacesTex = cdDM_drawFacesTex;
714         dm->drawMappedFaces = cdDM_drawMappedFaces;
715         dm->drawMappedFacesTex = cdDM_drawMappedFacesTex;
716
717         dm->foreachMappedVert = cdDM_foreachMappedVert;
718         dm->foreachMappedEdge = cdDM_foreachMappedEdge;
719         dm->foreachMappedFaceCenter = cdDM_foreachMappedFaceCenter;
720
721         dm->release = cdDM_release;
722
723         return cddm;
724 }
725
726 DerivedMesh *CDDM_new(int numVerts, int numEdges, int numFaces)
727 {
728         CDDerivedMesh *cddm = cdDM_create("CDDM_new dm");
729         DerivedMesh *dm = &cddm->dm;
730
731         DM_init(dm, numVerts, numEdges, numFaces);
732
733         CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL, numVerts);
734         CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges);
735         CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL, numFaces);
736
737         cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
738         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
739         cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
740
741         return dm;
742 }
743
744 DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob)
745 {
746         CDDerivedMesh *cddm = cdDM_create("CDDM_from_mesh dm");
747         DerivedMesh *dm = &cddm->dm;
748         int i, *index;
749
750         /* this does a referenced copy, the only new layers being ORIGINDEX */
751
752         DM_init(dm, mesh->totvert, mesh->totedge, mesh->totface);
753
754         CustomData_merge(&mesh->vdata, &dm->vertData, CD_MASK_MESH, CD_REFERENCE,
755                          mesh->totvert);
756         CustomData_merge(&mesh->edata, &dm->edgeData, CD_MASK_MESH, CD_REFERENCE,
757                          mesh->totedge);
758         CustomData_merge(&mesh->fdata, &dm->faceData, CD_MASK_MESH, CD_REFERENCE,
759                          mesh->totface);
760
761         cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
762         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
763         cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
764
765         index = CustomData_get_layer(&dm->vertData, CD_ORIGINDEX);
766         for(i = 0; i < mesh->totvert; ++i, ++index)
767                 *index = i;
768
769         index = CustomData_get_layer(&dm->edgeData, CD_ORIGINDEX);
770         for(i = 0; i < mesh->totedge; ++i, ++index)
771                 *index = i;
772
773         index = CustomData_get_layer(&dm->faceData, CD_ORIGINDEX);
774         for(i = 0; i < mesh->totface; ++i, ++index)
775                 *index = i;
776         
777         /* works in conjunction with hack during modifier calc, where active mcol
778            layer with weight paint colors is temporarily added */
779         if ((G.f & G_WEIGHTPAINT) &&
780                 (ob && ob==(G.scene->basact?G.scene->basact->object:NULL)))
781                 CustomData_duplicate_referenced_layer(&dm->faceData, CD_MCOL);
782
783         return dm;
784 }
785
786 DerivedMesh *CDDM_from_editmesh(EditMesh *em, Mesh *me)
787 {
788         DerivedMesh *dm = CDDM_new(BLI_countlist(&em->verts),
789                                    BLI_countlist(&em->edges),
790                                    BLI_countlist(&em->faces));
791         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
792         EditVert *eve;
793         EditEdge *eed;
794         EditFace *efa;
795         MVert *mvert = cddm->mvert;
796         MEdge *medge = cddm->medge;
797         MFace *mface = cddm->mface;
798         int i, *index;
799
800         CustomData_merge(&em->vdata, &dm->vertData, CD_MASK_DERIVEDMESH,
801                          CD_CALLOC, dm->numVertData);
802         /* CustomData_merge(&em->edata, &dm->edgeData, CD_MASK_DERIVEDMESH,
803                          CD_CALLOC, dm->numEdgeData); */
804         CustomData_merge(&em->fdata, &dm->faceData, CD_MASK_DERIVEDMESH,
805                          CD_CALLOC, dm->numFaceData);
806
807         /* set eve->hash to vert index */
808         for(i = 0, eve = em->verts.first; eve; eve = eve->next, ++i)
809                 eve->tmp.l = i;
810
811         /* Need to be able to mark loose edges */
812         for(eed = em->edges.first; eed; eed = eed->next) {
813                 eed->f2 = 0;
814         }
815         for(efa = em->faces.first; efa; efa = efa->next) {
816                 efa->e1->f2 = 1;
817                 efa->e2->f2 = 1;
818                 efa->e3->f2 = 1;
819                 if(efa->e4) efa->e4->f2 = 1;
820         }
821
822         index = dm->getVertDataArray(dm, CD_ORIGINDEX);
823         for(i = 0, eve = em->verts.first; i < dm->numVertData;
824             i++, eve = eve->next, index++) {
825                 MVert *mv = &mvert[i];
826
827                 VECCOPY(mv->co, eve->co);
828
829                 mv->no[0] = eve->no[0] * 32767.0;
830                 mv->no[1] = eve->no[1] * 32767.0;
831                 mv->no[2] = eve->no[2] * 32767.0;
832
833                 mv->mat_nr = 0;
834                 mv->flag = 0;
835
836                 *index = i;
837
838                 CustomData_from_em_block(&em->vdata, &dm->vertData, eve->data, i);
839         }
840
841         index = dm->getEdgeDataArray(dm, CD_ORIGINDEX);
842         for(i = 0, eed = em->edges.first; i < dm->numEdgeData;
843             i++, eed = eed->next, index++) {
844                 MEdge *med = &medge[i];
845
846                 med->v1 = eed->v1->tmp.l;
847                 med->v2 = eed->v2->tmp.l;
848                 med->crease = (unsigned char) (eed->crease * 255.0f);
849                 med->flag = ME_EDGEDRAW|ME_EDGERENDER;
850                 
851                 if(eed->seam) med->flag |= ME_SEAM;
852                 if(eed->sharp) med->flag |= ME_SHARP;
853                 if(!eed->f2) med->flag |= ME_LOOSEEDGE;
854
855                 *index = i;
856
857                 /* CustomData_from_em_block(&em->edata, &dm->edgeData, eed->data, i); */
858         }
859
860         index = dm->getFaceDataArray(dm, CD_ORIGINDEX);
861         for(i = 0, efa = em->faces.first; i < dm->numFaceData;
862             i++, efa = efa->next, index++) {
863                 MFace *mf = &mface[i];
864
865                 mf->v1 = efa->v1->tmp.l;
866                 mf->v2 = efa->v2->tmp.l;
867                 mf->v3 = efa->v3->tmp.l;
868                 mf->v4 = efa->v4 ? efa->v4->tmp.l : 0;
869                 mf->mat_nr = efa->mat_nr;
870                 mf->flag = efa->flag;
871
872                 *index = i;
873
874                 CustomData_from_em_block(&em->fdata, &dm->faceData, efa->data, i);
875                 test_index_face(mf, &dm->faceData, i, efa->v4?4:3);
876         }
877
878         return dm;
879 }
880
881 DerivedMesh *CDDM_copy(DerivedMesh *source)
882 {
883         CDDerivedMesh *cddm = cdDM_create("CDDM_copy cddm");
884         DerivedMesh *dm = &cddm->dm;
885         int numVerts = source->numVertData;
886         int numEdges = source->numEdgeData;
887         int numFaces = source->numFaceData;
888
889         /* this initializes dm, and copies all non mvert/medge/mface layers */
890         DM_from_template(dm, source, numVerts, numEdges, numFaces);
891
892         CustomData_copy_data(&source->vertData, &dm->vertData, 0, 0, numVerts);
893         CustomData_copy_data(&source->edgeData, &dm->edgeData, 0, 0, numEdges);
894         CustomData_copy_data(&source->faceData, &dm->faceData, 0, 0, numFaces);
895
896         /* now add mvert/medge/mface layers */
897         cddm->mvert = source->dupVertArray(source);
898         cddm->medge = source->dupEdgeArray(source);
899         cddm->mface = source->dupFaceArray(source);
900
901         CustomData_add_layer(&dm->vertData, CD_MVERT, CD_ASSIGN, cddm->mvert, numVerts);
902         CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_ASSIGN, cddm->medge, numEdges);
903         CustomData_add_layer(&dm->faceData, CD_MFACE, CD_ASSIGN, cddm->mface, numFaces);
904
905         return dm;
906 }
907
908 DerivedMesh *CDDM_from_template(DerivedMesh *source,
909                                 int numVerts, int numEdges, int numFaces)
910 {
911         CDDerivedMesh *cddm = cdDM_create("CDDM_from_template dest");
912         DerivedMesh *dm = &cddm->dm;
913
914         /* this does a copy of all non mvert/medge/mface layers */
915         DM_from_template(dm, source, numVerts, numEdges, numFaces);
916
917         /* now add mvert/medge/mface layers */
918         CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL, numVerts);
919         CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges);
920         CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL, numFaces);
921
922         cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
923         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
924         cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
925
926         return dm;
927 }
928
929 void CDDM_apply_vert_coords(DerivedMesh *dm, float (*vertCoords)[3])
930 {
931         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
932         MVert *vert;
933         int i;
934
935         /* this will just return the pointer if it wasn't a referenced layer */
936         vert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
937         cddm->mvert = vert;
938
939         for(i = 0; i < dm->numVertData; ++i, ++vert)
940                 VECCOPY(vert->co, vertCoords[i]);
941 }
942
943 void CDDM_apply_vert_normals(DerivedMesh *dm, short (*vertNormals)[3])
944 {
945         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
946         MVert *vert;
947         int i;
948
949         /* this will just return the pointer if it wasn't a referenced layer */
950         vert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
951         cddm->mvert = vert;
952
953         for(i = 0; i < dm->numVertData; ++i, ++vert)
954                 VECCOPY(vert->no, vertNormals[i]);
955 }
956
957 /* adapted from mesh_calc_normals */
958 void CDDM_calc_normals(DerivedMesh *dm)
959 {
960         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
961         float (*temp_nors)[3];
962         float (*face_nors)[3];
963         int i;
964         int numVerts = dm->numVertData;
965         int numFaces = dm->numFaceData;
966         MFace *mf;
967         MVert *mv;
968
969         if(numVerts == 0) return;
970
971         temp_nors = MEM_callocN(numVerts * sizeof(*temp_nors),
972                                 "CDDM_calc_normals temp_nors");
973
974         /* we don't want to overwrite any referenced layers */
975         mv = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
976         cddm->mvert = mv;
977
978         /* make a face normal layer if not present */
979         face_nors = CustomData_get_layer(&dm->faceData, CD_NORMAL);
980         if(!face_nors)
981                 face_nors = CustomData_add_layer(&dm->faceData, CD_NORMAL, CD_CALLOC,
982                                                  NULL, dm->numFaceData);
983
984         /* calculate face normals and add to vertex normals */
985         mf = CDDM_get_faces(dm);
986         for(i = 0; i < numFaces; i++, mf++) {
987                 float *f_no = face_nors[i];
988
989                 if(mf->v4)
990                         CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
991                                        mv[mf->v3].co, mv[mf->v4].co, f_no);
992                 else
993                         CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
994                                       mv[mf->v3].co, f_no);
995                 
996                 VecAddf(temp_nors[mf->v1], temp_nors[mf->v1], f_no);
997                 VecAddf(temp_nors[mf->v2], temp_nors[mf->v2], f_no);
998                 VecAddf(temp_nors[mf->v3], temp_nors[mf->v3], f_no);
999                 if(mf->v4)
1000                         VecAddf(temp_nors[mf->v4], temp_nors[mf->v4], f_no);
1001         }
1002
1003         /* normalize vertex normals and assign */
1004         for(i = 0; i < numVerts; i++, mv++) {
1005                 float *no = temp_nors[i];
1006                 
1007                 if (Normalize(no) == 0.0) {
1008                         VECCOPY(no, mv->co);
1009                         Normalize(no);
1010                 }
1011
1012                 mv->no[0] = (short)(no[0] * 32767.0);
1013                 mv->no[1] = (short)(no[1] * 32767.0);
1014                 mv->no[2] = (short)(no[2] * 32767.0);
1015         }
1016         
1017         MEM_freeN(temp_nors);
1018 }
1019
1020 void CDDM_calc_edges(DerivedMesh *dm)
1021 {
1022         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1023         CustomData edgeData;
1024         EdgeHashIterator *ehi;
1025         MFace *mf = cddm->mface;
1026         MEdge *med;
1027         EdgeHash *eh = BLI_edgehash_new();
1028         int i, *index, numEdges, maxFaces = dm->numFaceData;
1029
1030         for (i = 0; i < maxFaces; i++, mf++) {
1031                 if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
1032                         BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
1033                 if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
1034                         BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
1035                 
1036                 if (mf->v4) {
1037                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
1038                                 BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
1039                         if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
1040                                 BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
1041                 } else {
1042                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
1043                                 BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
1044                 }
1045         }
1046
1047         numEdges = BLI_edgehash_size(eh);
1048
1049         /* write new edges into a temporary CustomData */
1050         memset(&edgeData, 0, sizeof(edgeData));
1051         CustomData_add_layer(&edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges);
1052         CustomData_add_layer(&edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges);
1053
1054         ehi = BLI_edgehashIterator_new(eh);
1055         med = CustomData_get_layer(&edgeData, CD_MEDGE);
1056         index = CustomData_get_layer(&edgeData, CD_ORIGINDEX);
1057         for(i = 0; !BLI_edgehashIterator_isDone(ehi);
1058             BLI_edgehashIterator_step(ehi), ++i, ++med, ++index) {
1059                 BLI_edgehashIterator_getKey(ehi, (int*)&med->v1, (int*)&med->v2);
1060
1061                 med->flag = ME_EDGEDRAW|ME_EDGERENDER;
1062                 *index = ORIGINDEX_NONE;
1063         }
1064         BLI_edgehashIterator_free(ehi);
1065
1066         /* free old CustomData and assign new one */
1067         CustomData_free(&dm->edgeData, dm->numVertData);
1068         dm->edgeData = edgeData;
1069         dm->numEdgeData = numEdges;
1070
1071         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
1072
1073         BLI_edgehash_free(eh, NULL);
1074 }
1075
1076 void CDDM_lower_num_verts(DerivedMesh *dm, int numVerts)
1077 {
1078         if (numVerts < dm->numVertData)
1079                 CustomData_free_elem(&dm->vertData, numVerts, dm->numVertData-numVerts);
1080
1081         dm->numVertData = numVerts;
1082 }
1083
1084 void CDDM_lower_num_edges(DerivedMesh *dm, int numEdges)
1085 {
1086         if (numEdges < dm->numEdgeData)
1087                 CustomData_free_elem(&dm->edgeData, numEdges, dm->numEdgeData-numEdges);
1088
1089         dm->numEdgeData = numEdges;
1090 }
1091
1092 void CDDM_lower_num_faces(DerivedMesh *dm, int numFaces)
1093 {
1094         if (numFaces < dm->numFaceData)
1095                 CustomData_free_elem(&dm->faceData, numFaces, dm->numFaceData-numFaces);
1096
1097         dm->numFaceData = numFaces;
1098 }
1099
1100 MVert *CDDM_get_vert(DerivedMesh *dm, int index)
1101 {
1102         return &((CDDerivedMesh*)dm)->mvert[index];
1103 }
1104
1105 MEdge *CDDM_get_edge(DerivedMesh *dm, int index)
1106 {
1107         return &((CDDerivedMesh*)dm)->medge[index];
1108 }
1109
1110 MFace *CDDM_get_face(DerivedMesh *dm, int index)
1111 {
1112         return &((CDDerivedMesh*)dm)->mface[index];
1113 }
1114
1115 MVert *CDDM_get_verts(DerivedMesh *dm)
1116 {
1117         return ((CDDerivedMesh*)dm)->mvert;
1118 }
1119
1120 MEdge *CDDM_get_edges(DerivedMesh *dm)
1121 {
1122         return ((CDDerivedMesh*)dm)->medge;
1123 }
1124
1125 MFace *CDDM_get_faces(DerivedMesh *dm)
1126 {
1127         return ((CDDerivedMesh*)dm)->mface;
1128 }
1129