9538deadd5bf0aa3535b2ffc109ec81536a693f2
[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                                         nors += 3;
284                                 }
285                                 else {
286                                         /* TODO make this better (cache facenormals as layer?) */
287                                         float nor[3];
288                                         if(mface->v4) {
289                                                 CalcNormFloat4(mvert[mface->v1].co, mvert[mface->v2].co,
290                                                                            mvert[mface->v3].co, mvert[mface->v4].co,
291                                                                            nor);
292                                         } else {
293                                                 CalcNormFloat(mvert[mface->v1].co, mvert[mface->v2].co,
294                                                                           mvert[mface->v3].co, nor);
295                                         }
296                                         glNormal3fv(nor);
297                                 }
298                         }
299
300                         PASSVERT(mface->v1);
301                         PASSVERT(mface->v2);
302                         PASSVERT(mface->v3);
303                         if(mface->v4) {
304                                 PASSVERT(mface->v4);
305                         }
306                 }
307         }
308         glEnd();
309
310         glShadeModel(GL_FLAT);
311 #undef PASSVERT
312 }
313
314 static void cdDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2)
315 {
316         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
317         int a, glmode;
318         unsigned char *cp1, *cp2;
319         MVert *mvert = cddm->mvert;
320         MFace *mface = cddm->mface;
321
322         cp1 = col1;
323         if(col2) {
324                 cp2 = col2;
325         } else {
326                 cp2 = NULL;
327                 useTwoSided = 0;
328         }
329
330         /* there's a conflict here... twosided colors versus culling...? */
331         /* defined by history, only texture faces have culling option */
332         /* we need that as mesh option builtin, next to double sided lighting */
333         if(col1 && col2)
334                 glEnable(GL_CULL_FACE);
335         
336         glShadeModel(GL_SMOOTH);
337         glBegin(glmode = GL_QUADS);
338         for(a = 0; a < dm->numFaceData; a++, mface++, cp1 += 16) {
339                 int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
340
341                 if(new_glmode != glmode) {
342                         glEnd();
343                         glBegin(glmode = new_glmode);
344                 }
345                         
346                 glColor3ub(cp1[0], cp1[1], cp1[2]);
347                 glVertex3fv(mvert[mface->v1].co);
348                 glColor3ub(cp1[4], cp1[5], cp1[6]);
349                 glVertex3fv(mvert[mface->v2].co);
350                 glColor3ub(cp1[8], cp1[9], cp1[10]);
351                 glVertex3fv(mvert[mface->v3].co);
352                 if(mface->v4) {
353                         glColor3ub(cp1[12], cp1[13], cp1[14]);
354                         glVertex3fv(mvert[mface->v4].co);
355                 }
356                         
357                 if(useTwoSided) {
358                         glColor3ub(cp2[8], cp2[9], cp2[10]);
359                         glVertex3fv(mvert[mface->v3].co );
360                         glColor3ub(cp2[4], cp2[5], cp2[6]);
361                         glVertex3fv(mvert[mface->v2].co );
362                         glColor3ub(cp2[0], cp2[1], cp2[2]);
363                         glVertex3fv(mvert[mface->v1].co );
364                         if(mface->v4) {
365                                 glColor3ub(cp2[12], cp2[13], cp2[14]);
366                                 glVertex3fv(mvert[mface->v4].co );
367                         }
368                 }
369                 if(col2) cp2 += 16;
370         }
371         glEnd();
372
373         glShadeModel(GL_FLAT);
374         glDisable(GL_CULL_FACE);
375 }
376
377 static void cdDM_drawFacesTex_common(DerivedMesh *dm,
378                int (*drawParams)(MTFace *tface, MCol *mcol, int matnr),
379                int (*drawParamsMapped)(void *userData, int index),
380                void *userData) 
381 {
382         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
383         MVert *mv = cddm->mvert;
384         MFace *mf = cddm->mface;
385         MCol *mcol = dm->getFaceDataArray(dm, CD_MCOL);
386         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
387         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
388         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
389
390         for(i = 0; i < dm->numFaceData; i++, mf++) {
391                 MVert *mvert;
392                 int flag;
393                 unsigned char *cp = NULL;
394
395                 if(drawParams) {
396                         flag = drawParams(tf? &tf[i]: NULL, mcol? &mcol[i*4]: NULL, mf->mat_nr);
397                 }
398                 else {
399                         if(index) {
400                                 orig = *index++;
401                                 if(orig == ORIGINDEX_NONE) continue;
402                                 flag = drawParamsMapped(userData, orig);
403                         }
404                         else
405                                 flag = drawParamsMapped(userData, i);
406                 }
407
408                 if(flag == 0)
409                         continue;
410                 else if (flag==1 && mcol)
411                         cp= (unsigned char*) &mcol[i*4];
412
413                 if(!(mf->flag&ME_SMOOTH)) {
414                         if (nors) {
415                                 glNormal3fv(nors);
416                                 nors += 3;
417                         }
418                         else {
419                                 /* TODO make this better (cache facenormals as layer?) */
420                                 float nor[3];
421                                 if(mf->v4) {
422                                         CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
423                                                                    mv[mf->v3].co, mv[mf->v4].co,
424                                                                    nor);
425                                 } else {
426                                         CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
427                                                                   mv[mf->v3].co, nor);
428                                 }
429                                 glNormal3fv(nor);
430                         }
431                 }
432
433                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
434                 if(tf) glTexCoord2fv(tf[i].uv[0]);
435                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
436                 mvert = &mv[mf->v1];
437                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
438                 glVertex3fv(mvert->co);
439                         
440                 if(tf) glTexCoord2fv(tf[i].uv[1]);
441                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
442                 mvert = &mv[mf->v2];
443                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
444                 glVertex3fv(mvert->co);
445
446                 if(tf) glTexCoord2fv(tf[i].uv[2]);
447                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
448                 mvert = &mv[mf->v3];
449                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
450                 glVertex3fv(mvert->co);
451
452                 if(mf->v4) {
453                         if(tf) glTexCoord2fv(tf[i].uv[3]);
454                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
455                         mvert = &mv[mf->v4];
456                         if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
457                         glVertex3fv(mvert->co);
458                 }
459                 glEnd();
460         }
461 }
462
463 static void cdDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
464 {
465         cdDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
466 }
467
468 static void cdDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors)
469 {
470         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
471         MVert *mv = cddm->mvert;
472         MFace *mf = cddm->mface;
473         MCol *mc = DM_get_face_data_layer(dm, CD_MCOL);
474         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
475         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
476
477         for(i = 0; i < dm->numFaceData; i++, mf++) {
478                 int drawSmooth = (mf->flag & ME_SMOOTH);
479
480                 if(index) {
481                         orig = *index++;
482                         if(setDrawOptions && orig == ORIGINDEX_NONE) continue;
483                 }
484                 else
485                         orig = i;
486
487                 if(!setDrawOptions || setDrawOptions(userData, orig, &drawSmooth)) {
488                         unsigned char *cp = NULL;
489
490                         if(useColors && mc)
491                                 cp = (unsigned char *)&mc[i * 4];
492
493                         glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
494                         glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
495
496                         if (!drawSmooth) {
497                                 if (nors) {
498                                         glNormal3fv(nors);
499                                         nors += 3;
500                                 }
501                                 else {
502                                         /* TODO make this better (cache facenormals as layer?) */
503                                         float nor[3];
504                                         if(mf->v4) {
505                                                 CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
506                                                                            mv[mf->v3].co, mv[mf->v4].co,
507                                                                            nor);
508                                         } else {
509                                                 CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
510                                                                           mv[mf->v3].co, nor);
511                                         }
512                                         glNormal3fv(nor);
513                                 }
514
515                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
516                                 glVertex3fv(mv[mf->v1].co);
517                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
518                                 glVertex3fv(mv[mf->v2].co);
519                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
520                                 glVertex3fv(mv[mf->v3].co);
521                                 if(mf->v4) {
522                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
523                                         glVertex3fv(mv[mf->v4].co);
524                                 }
525                         } else {
526                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
527                                 glNormal3sv(mv[mf->v1].no);
528                                 glVertex3fv(mv[mf->v1].co);
529                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
530                                 glNormal3sv(mv[mf->v2].no);
531                                 glVertex3fv(mv[mf->v2].co);
532                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
533                                 glNormal3sv(mv[mf->v3].no);
534                                 glVertex3fv(mv[mf->v3].co);
535                                 if(mf->v4) {
536                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
537                                         glNormal3sv(mv[mf->v4].no);
538                                         glVertex3fv(mv[mf->v4].co);
539                                 }
540                         }
541
542                         glEnd();
543                 }
544         }
545 }
546
547 static void cdDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
548 {
549         cdDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
550 }
551
552 static void cdDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
553 {
554         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
555         MVert *vert = cddm->mvert;
556         MEdge *edge = cddm->medge;
557         int i, orig, *index = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
558
559         glBegin(GL_LINES);
560         for(i = 0; i < dm->numEdgeData; i++, edge++) {
561                 if(index) {
562                         orig = *index++;
563                         if(setDrawOptions && orig == ORIGINDEX_NONE) continue;
564                 }
565                 else
566                         orig = i;
567
568                 if(!setDrawOptions || setDrawOptions(userData, orig)) {
569                         glVertex3fv(vert[edge->v1].co);
570                         glVertex3fv(vert[edge->v2].co);
571                 }
572         }
573         glEnd();
574 }
575
576 static void cdDM_foreachMappedVert(
577                            DerivedMesh *dm,
578                            void (*func)(void *userData, int index, float *co,
579                                         float *no_f, short *no_s),
580                            void *userData)
581 {
582         MVert *mv = CDDM_get_verts(dm);
583         int i, orig, *index = DM_get_vert_data_layer(dm, CD_ORIGINDEX);
584
585         for(i = 0; i < dm->numVertData; i++, mv++) {
586                 if(index) {
587                         orig = *index++;
588                         if(orig == ORIGINDEX_NONE) continue;
589                         func(userData, orig, mv->co, NULL, mv->no);
590                 }
591                 else
592                         func(userData, i, mv->co, NULL, mv->no);
593         }
594 }
595
596 static void cdDM_foreachMappedEdge(
597                            DerivedMesh *dm,
598                            void (*func)(void *userData, int index,
599                                         float *v0co, float *v1co),
600                            void *userData)
601 {
602         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
603         MVert *mv = cddm->mvert;
604         MEdge *med = cddm->medge;
605         int i, orig, *index = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
606
607         for(i = 0; i < dm->numEdgeData; i++, med++) {
608                 if (index) {
609                         orig = *index++;
610                         if(orig == ORIGINDEX_NONE) continue;
611                         func(userData, orig, mv[med->v1].co, mv[med->v2].co);
612                 }
613                 else
614                         func(userData, i, mv[med->v1].co, mv[med->v2].co);
615         }
616 }
617
618 static void cdDM_foreachMappedFaceCenter(
619                            DerivedMesh *dm,
620                            void (*func)(void *userData, int index,
621                                         float *cent, float *no),
622                            void *userData)
623 {
624         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
625         MVert *mv = cddm->mvert;
626         MFace *mf = cddm->mface;
627         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
628
629         for(i = 0; i < dm->numFaceData; i++, mf++) {
630                 float cent[3];
631                 float no[3];
632
633                 if (index) {
634                         orig = *index++;
635                         if(orig == ORIGINDEX_NONE) continue;
636                 }
637                 else
638                         orig = i;
639
640                 VECCOPY(cent, mv[mf->v1].co);
641                 VecAddf(cent, cent, mv[mf->v2].co);
642                 VecAddf(cent, cent, mv[mf->v3].co);
643
644                 if (mf->v4) {
645                         CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
646                                        mv[mf->v3].co, mv[mf->v4].co, no);
647                         VecAddf(cent, cent, mv[mf->v4].co);
648                         VecMulf(cent, 0.25f);
649                 } else {
650                         CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
651                                       mv[mf->v3].co, no);
652                         VecMulf(cent, 0.33333333333f);
653                 }
654
655                 func(userData, orig, cent, no);
656         }
657 }
658
659 static void cdDM_release(DerivedMesh *dm)
660 {
661         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
662
663         if (DM_release(dm))
664                 MEM_freeN(cddm);
665 }
666
667 /**************** CDDM interface functions ****************/
668 static CDDerivedMesh *cdDM_create(const char *desc)
669 {
670         CDDerivedMesh *cddm;
671         DerivedMesh *dm;
672
673         cddm = MEM_callocN(sizeof(*cddm), desc);
674         dm = &cddm->dm;
675
676         dm->getMinMax = cdDM_getMinMax;
677
678         dm->getNumVerts = cdDM_getNumVerts;
679         dm->getNumFaces = cdDM_getNumFaces;
680         dm->getNumEdges = cdDM_getNumEdges;
681
682         dm->getVert = cdDM_getVert;
683         dm->getEdge = cdDM_getEdge;
684         dm->getFace = cdDM_getFace;
685         dm->copyVertArray = cdDM_copyVertArray;
686         dm->copyEdgeArray = cdDM_copyEdgeArray;
687         dm->copyFaceArray = cdDM_copyFaceArray;
688         dm->getVertData = DM_get_vert_data;
689         dm->getEdgeData = DM_get_edge_data;
690         dm->getFaceData = DM_get_face_data;
691         dm->getVertDataArray = DM_get_vert_data_layer;
692         dm->getEdgeDataArray = DM_get_edge_data_layer;
693         dm->getFaceDataArray = DM_get_face_data_layer;
694
695         dm->getVertCos = cdDM_getVertCos;
696         dm->getVertCo = cdDM_getVertCo;
697         dm->getVertNo = cdDM_getVertNo;
698
699         dm->drawVerts = cdDM_drawVerts;
700
701         dm->drawUVEdges = cdDM_drawUVEdges;
702         dm->drawEdges = cdDM_drawEdges;
703         dm->drawLooseEdges = cdDM_drawLooseEdges;
704         dm->drawMappedEdges = cdDM_drawMappedEdges;
705
706         dm->drawFacesSolid = cdDM_drawFacesSolid;
707         dm->drawFacesColored = cdDM_drawFacesColored;
708         dm->drawFacesTex = cdDM_drawFacesTex;
709         dm->drawMappedFaces = cdDM_drawMappedFaces;
710         dm->drawMappedFacesTex = cdDM_drawMappedFacesTex;
711
712         dm->foreachMappedVert = cdDM_foreachMappedVert;
713         dm->foreachMappedEdge = cdDM_foreachMappedEdge;
714         dm->foreachMappedFaceCenter = cdDM_foreachMappedFaceCenter;
715
716         dm->release = cdDM_release;
717
718         return cddm;
719 }
720
721 DerivedMesh *CDDM_new(int numVerts, int numEdges, int numFaces)
722 {
723         CDDerivedMesh *cddm = cdDM_create("CDDM_new dm");
724         DerivedMesh *dm = &cddm->dm;
725
726         DM_init(dm, numVerts, numEdges, numFaces);
727
728         cddm->mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, 0, 0, numVerts);
729         cddm->medge = CustomData_add_layer(&dm->edgeData, CD_MEDGE, 0, 0, numEdges);
730         cddm->mface = CustomData_add_layer(&dm->faceData, CD_MFACE, 0, 0, numFaces);
731
732         return dm;
733 }
734
735 DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob)
736 {
737         CDDerivedMesh *cddm = cdDM_create("CDDM_from_mesh dm");
738         DerivedMesh *dm = &cddm->dm;
739         int i, *index;
740
741         /* this does a referenced copy, the only new layers being ORIGINDEX */
742
743         DM_init(dm, mesh->totvert, mesh->totedge, mesh->totface);
744
745         CustomData_merge(&mesh->vdata, &dm->vertData, CD_MASK_MESH, CD_REFERENCE,
746                          mesh->totvert);
747         CustomData_merge(&mesh->edata, &dm->edgeData, CD_MASK_MESH, CD_REFERENCE,
748                          mesh->totedge);
749         CustomData_merge(&mesh->fdata, &dm->faceData, CD_MASK_MESH, CD_REFERENCE,
750                          mesh->totface);
751
752         cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
753         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
754         cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
755
756         index = CustomData_get_layer(&dm->vertData, CD_ORIGINDEX);
757         for(i = 0; i < mesh->totvert; ++i, ++index)
758                 *index = i;
759
760         index = CustomData_get_layer(&dm->edgeData, CD_ORIGINDEX);
761         for(i = 0; i < mesh->totedge; ++i, ++index)
762                 *index = i;
763
764         index = CustomData_get_layer(&dm->faceData, CD_ORIGINDEX);
765         for(i = 0; i < mesh->totface; ++i, ++index)
766                 *index = i;
767         
768         /* works in conjunction with hack during modifier calc, where mcol is
769            temporarily replaced by weight paint colors */
770         if ((G.f & G_WEIGHTPAINT) &&
771                 (ob && ob==(G.scene->basact?G.scene->basact->object:NULL)))
772                 CustomData_duplicate_referenced_layer(&dm->faceData, CD_MCOL);
773
774         return dm;
775 }
776
777 DerivedMesh *CDDM_from_editmesh(EditMesh *em, Mesh *me)
778 {
779         DerivedMesh *dm = CDDM_new(BLI_countlist(&em->verts),
780                                    BLI_countlist(&em->edges),
781                                    BLI_countlist(&em->faces));
782         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
783         EditVert *eve;
784         EditEdge *eed;
785         EditFace *efa;
786         MVert *mvert = cddm->mvert;
787         MEdge *medge = cddm->medge;
788         MFace *mface = cddm->mface;
789         int i, *index;
790
791         CustomData_merge(&em->vdata, &dm->vertData, CD_MASK_DERIVEDMESH,
792                          CD_CALLOC, dm->numVertData);
793         /* CustomData_merge(&em->edata, &dm->edgeData, CD_MASK_DERIVEDMESH,
794                          CD_CALLOC, dm->numEdgeData); */
795         CustomData_merge(&em->fdata, &dm->faceData, CD_MASK_DERIVEDMESH,
796                          CD_CALLOC, dm->numFaceData);
797
798         /* set eve->hash to vert index */
799         for(i = 0, eve = em->verts.first; eve; eve = eve->next, ++i)
800                 eve->tmp.l = i;
801
802         /* Need to be able to mark loose edges */
803         for(eed = em->edges.first; eed; eed = eed->next) {
804                 eed->f2 = 0;
805         }
806         for(efa = em->faces.first; efa; efa = efa->next) {
807                 efa->e1->f2 = 1;
808                 efa->e2->f2 = 1;
809                 efa->e3->f2 = 1;
810                 if(efa->e4) efa->e4->f2 = 1;
811         }
812
813         index = dm->getVertDataArray(dm, CD_ORIGINDEX);
814         for(i = 0, eve = em->verts.first; i < dm->numVertData;
815             i++, eve = eve->next, index++) {
816                 MVert *mv = &mvert[i];
817
818                 VECCOPY(mv->co, eve->co);
819
820                 mv->no[0] = eve->no[0] * 32767.0;
821                 mv->no[1] = eve->no[1] * 32767.0;
822                 mv->no[2] = eve->no[2] * 32767.0;
823
824                 mv->mat_nr = 0;
825                 mv->flag = 0;
826
827                 *index = i;
828
829                 CustomData_from_em_block(&em->vdata, &dm->vertData, eve->data, i);
830         }
831
832         index = dm->getEdgeDataArray(dm, CD_ORIGINDEX);
833         for(i = 0, eed = em->edges.first; i < dm->numEdgeData;
834             i++, eed = eed->next, index++) {
835                 MEdge *med = &medge[i];
836
837                 med->v1 = eed->v1->tmp.l;
838                 med->v2 = eed->v2->tmp.l;
839                 med->crease = (unsigned char) (eed->crease * 255.0f);
840                 med->flag = ME_EDGEDRAW|ME_EDGERENDER;
841                 
842                 if(eed->seam) med->flag |= ME_SEAM;
843                 if(eed->sharp) med->flag |= ME_SHARP;
844                 if(!eed->f2) med->flag |= ME_LOOSEEDGE;
845
846                 *index = i;
847
848                 /* CustomData_from_em_block(&em->edata, &dm->edgeData, eed->data, i); */
849         }
850
851         index = dm->getFaceDataArray(dm, CD_ORIGINDEX);
852         for(i = 0, efa = em->faces.first; i < dm->numFaceData;
853             i++, efa = efa->next, index++) {
854                 MFace *mf = &mface[i];
855
856                 mf->v1 = efa->v1->tmp.l;
857                 mf->v2 = efa->v2->tmp.l;
858                 mf->v3 = efa->v3->tmp.l;
859                 mf->v4 = efa->v4 ? efa->v4->tmp.l : 0;
860                 mf->mat_nr = efa->mat_nr;
861                 mf->flag = efa->flag;
862
863                 *index = i;
864
865                 CustomData_from_em_block(&em->fdata, &dm->faceData, efa->data, i);
866                 test_index_face(mf, &dm->faceData, i, efa->v4?4:3);
867         }
868
869         return dm;
870 }
871
872 DerivedMesh *CDDM_copy(DerivedMesh *source)
873 {
874         CDDerivedMesh *cddm = cdDM_create("CDDM_copy cddm");
875         DerivedMesh *dm = &cddm->dm;
876         int numVerts = source->numVertData;
877         int numEdges = source->numEdgeData;
878         int numFaces = source->numFaceData;
879
880         /* this initializes dm, and copies all non mvert/medge/mface layers */
881         DM_from_template(dm, source, numVerts, numEdges, numFaces);
882
883         CustomData_copy_data(&source->vertData, &dm->vertData, 0, 0, numVerts);
884         CustomData_copy_data(&source->edgeData, &dm->edgeData, 0, 0, numEdges);
885         CustomData_copy_data(&source->faceData, &dm->faceData, 0, 0, numFaces);
886
887         /* now add mvert/medge/mface layers */
888         cddm->mvert = source->dupVertArray(source);
889         cddm->medge = source->dupEdgeArray(source);
890         cddm->mface = source->dupFaceArray(source);
891
892         CustomData_add_layer(&dm->vertData, CD_MVERT, 0, cddm->mvert, numVerts);
893         CustomData_add_layer(&dm->edgeData, CD_MEDGE, 0, cddm->medge, numEdges);
894         CustomData_add_layer(&dm->faceData, CD_MFACE, 0, cddm->mface, numFaces);
895
896         return dm;
897 }
898
899 DerivedMesh *CDDM_from_template(DerivedMesh *source,
900                                 int numVerts, int numEdges, int numFaces)
901 {
902         CDDerivedMesh *cddm = cdDM_create("CDDM_from_template dest");
903         DerivedMesh *dm = &cddm->dm;
904
905         /* this does a copy of all non mvert/medge/mface layers */
906         DM_from_template(dm, source, numVerts, numEdges, numFaces);
907
908         /* now add mvert/medge/mface layers */
909         cddm->mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, 0, 0, numVerts);
910         cddm->medge = CustomData_add_layer(&dm->edgeData, CD_MEDGE, 0, 0, numEdges);
911         cddm->mface = CustomData_add_layer(&dm->faceData, CD_MFACE, 0, 0, numFaces);
912
913         return dm;
914 }
915
916 void CDDM_apply_vert_coords(DerivedMesh *dm, float (*vertCoords)[3])
917 {
918         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
919         MVert *vert;
920         int i;
921
922         /* this will just return the pointer if it wasn't a referenced layer */
923         vert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
924         cddm->mvert = vert;
925
926         for(i = 0; i < dm->numVertData; ++i, ++vert)
927                 VECCOPY(vert->co, vertCoords[i]);
928 }
929
930 void CDDM_apply_vert_normals(DerivedMesh *dm, short (*vertNormals)[3])
931 {
932         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
933         MVert *vert;
934         int i;
935
936         /* this will just return the pointer if it wasn't a referenced layer */
937         vert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
938         cddm->mvert = vert;
939
940         for(i = 0; i < dm->numVertData; ++i, ++vert)
941                 VECCOPY(vert->no, vertNormals[i]);
942 }
943
944 /* adapted from mesh_calc_normals */
945 void CDDM_calc_normals(DerivedMesh *dm)
946 {
947         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
948         float (*temp_nors)[3];
949         float (*face_nors)[3];
950         int i;
951         int numVerts = dm->numVertData;
952         int numFaces = dm->numFaceData;
953         MFace *mf;
954         MVert *mv;
955
956         if(numVerts == 0) return;
957
958         temp_nors = MEM_callocN(numVerts * sizeof(*temp_nors),
959                                 "CDDM_calc_normals temp_nors");
960
961         /* we don't want to overwrite any referenced layers */
962         mv = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
963         cddm->mvert = mv;
964
965         /* make a face normal layer if not present */
966         face_nors = CustomData_get_layer(&dm->faceData, CD_NORMAL);
967         if(!face_nors)
968                 face_nors = CustomData_add_layer(&dm->faceData, CD_NORMAL, 0, NULL,
969                                                  dm->numFaceData);
970
971         /* calculate face normals and add to vertex normals */
972         mf = CDDM_get_faces(dm);
973         for(i = 0; i < numFaces; i++, mf++) {
974                 float *f_no = face_nors[i];
975
976                 if(mf->v4)
977                         CalcNormFloat4(mv[mf->v1].co, mv[mf->v2].co,
978                                        mv[mf->v3].co, mv[mf->v4].co, f_no);
979                 else
980                         CalcNormFloat(mv[mf->v1].co, mv[mf->v2].co,
981                                       mv[mf->v3].co, f_no);
982                 
983                 VecAddf(temp_nors[mf->v1], temp_nors[mf->v1], f_no);
984                 VecAddf(temp_nors[mf->v2], temp_nors[mf->v2], f_no);
985                 VecAddf(temp_nors[mf->v3], temp_nors[mf->v3], f_no);
986                 if(mf->v4)
987                         VecAddf(temp_nors[mf->v4], temp_nors[mf->v4], f_no);
988         }
989
990         /* normalize vertex normals and assign */
991         for(i = 0; i < numVerts; i++, mv++) {
992                 float *no = temp_nors[i];
993                 
994                 if (Normalise(no) == 0.0) {
995                         VECCOPY(no, mv->co);
996                         Normalise(no);
997                 }
998
999                 mv->no[0] = (short)(no[0] * 32767.0);
1000                 mv->no[1] = (short)(no[1] * 32767.0);
1001                 mv->no[2] = (short)(no[2] * 32767.0);
1002         }
1003         
1004         MEM_freeN(temp_nors);
1005 }
1006
1007 void CDDM_calc_edges(DerivedMesh *dm)
1008 {
1009         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1010         CustomData edgeData;
1011         EdgeHashIterator *ehi;
1012         MFace *mf = cddm->mface;
1013         MEdge *med;
1014         EdgeHash *eh = BLI_edgehash_new();
1015         int i, *index, numEdges, maxFaces = dm->numFaceData;
1016
1017         for (i = 0; i < maxFaces; i++, mf++) {
1018                 if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
1019                         BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
1020                 if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
1021                         BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
1022                 
1023                 if (mf->v4) {
1024                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
1025                                 BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
1026                         if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
1027                                 BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
1028                 } else {
1029                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
1030                                 BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
1031                 }
1032         }
1033
1034         numEdges = BLI_edgehash_size(eh);
1035
1036         /* write new edges into a temporary CustomData */
1037         memset(&edgeData, 0, sizeof(edgeData));
1038         CustomData_add_layer(&edgeData, CD_MEDGE, 0, NULL, numEdges);
1039         index = CustomData_add_layer(&edgeData, CD_ORIGINDEX, 0, NULL, numEdges);
1040
1041         ehi = BLI_edgehashIterator_new(eh);
1042         med = CustomData_get_layer(&edgeData, CD_MEDGE);
1043         for(i = 0; !BLI_edgehashIterator_isDone(ehi);
1044             BLI_edgehashIterator_step(ehi), ++i, ++med, ++index) {
1045                 BLI_edgehashIterator_getKey(ehi, (int*)&med->v1, (int*)&med->v2);
1046
1047                 med->flag = ME_EDGEDRAW|ME_EDGERENDER;
1048                 *index = ORIGINDEX_NONE;
1049         }
1050         BLI_edgehashIterator_free(ehi);
1051
1052         /* free old CustomData and assign new one */
1053         CustomData_free(&dm->edgeData, dm->numVertData);
1054         dm->edgeData = edgeData;
1055         dm->numEdgeData = numEdges;
1056
1057         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
1058
1059         BLI_edgehash_free(eh, NULL);
1060 }
1061
1062 void CDDM_lower_num_verts(DerivedMesh *dm, int numVerts)
1063 {
1064         if (numVerts < dm->numVertData)
1065                 CustomData_free_elem(&dm->vertData, numVerts, dm->numVertData - numVerts);
1066
1067         dm->numVertData = numVerts;
1068 }
1069
1070 void CDDM_lower_num_edges(DerivedMesh *dm, int numEdges)
1071 {
1072         if (numEdges < dm->numEdgeData)
1073                 CustomData_free_elem(&dm->edgeData, numEdges, dm->numEdgeData - numEdges);
1074
1075         dm->numEdgeData = numEdges;
1076 }
1077
1078 void CDDM_lower_num_faces(DerivedMesh *dm, int numFaces)
1079 {
1080         if (numFaces < dm->numFaceData)
1081                 CustomData_free_elem(&dm->faceData, numFaces, dm->numFaceData - numFaces);
1082
1083         dm->numFaceData = numFaces;
1084 }
1085
1086 MVert *CDDM_get_vert(DerivedMesh *dm, int index)
1087 {
1088         return &((CDDerivedMesh*)dm)->mvert[index];
1089 }
1090
1091 MEdge *CDDM_get_edge(DerivedMesh *dm, int index)
1092 {
1093         return &((CDDerivedMesh*)dm)->medge[index];
1094 }
1095
1096 MFace *CDDM_get_face(DerivedMesh *dm, int index)
1097 {
1098         return &((CDDerivedMesh*)dm)->mface[index];
1099 }
1100
1101 MVert *CDDM_get_verts(DerivedMesh *dm)
1102 {
1103         return ((CDDerivedMesh*)dm)->mvert;
1104 }
1105
1106 MEdge *CDDM_get_edges(DerivedMesh *dm)
1107 {
1108         return ((CDDerivedMesh*)dm)->medge;
1109 }
1110
1111 MFace *CDDM_get_faces(DerivedMesh *dm)
1112 {
1113         return ((CDDerivedMesh*)dm)->mface;
1114 }
1115