Fix #20857: shrinkwrap doesn't use vertex group, broken recently with
[blender.git] / source / blender / blenkernel / intern / subsurf_ccg.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) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <math.h>
34 #include <float.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_modifier_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43
44 #include "BKE_cdderivedmesh.h"
45 #include "BKE_customdata.h"
46 #include "BKE_DerivedMesh.h"
47 #include "BKE_displist.h"
48 #include "BKE_utildefines.h"
49 #include "BKE_global.h"
50 #include "BKE_mesh.h"
51 #include "BKE_multires.h"
52 #include "BKE_scene.h"
53 #include "BKE_subsurf.h"
54
55 #include "BLI_blenlib.h"
56 #include "BLI_edgehash.h"
57 #include "BLI_editVert.h"
58 #include "BLI_linklist.h"
59 #include "BLI_math.h"
60 #include "BLI_memarena.h"
61 #include "BLI_pbvh.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h"
65
66 #include "GPU_draw.h"
67 #include "GPU_extensions.h"
68 #include "GPU_material.h"
69
70 #include "CCGSubSurf.h"
71
72 static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v);
73 static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e);
74 static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f);
75
76 ///
77
78 static void *arena_alloc(CCGAllocatorHDL a, int numBytes) {
79         return BLI_memarena_alloc(a, numBytes);
80 }
81 static void *arena_realloc(CCGAllocatorHDL a, void *ptr, int newSize, int oldSize) {
82         void *p2 = BLI_memarena_alloc(a, newSize);
83         if (ptr) {
84                 memcpy(p2, ptr, oldSize);
85         }
86         return p2;
87 }
88 static void arena_free(CCGAllocatorHDL a, void *ptr) {
89 }
90 static void arena_release(CCGAllocatorHDL a) {
91         BLI_memarena_free(a);
92 }
93
94 static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels, int useAging, int useArena, int useFlatSubdiv) {
95         CCGMeshIFC ifc;
96         CCGSubSurf *ccgSS;
97
98                 /* subdivLevels==0 is not allowed */
99         subdivLevels = MAX2(subdivLevels, 1);
100
101         if (prevSS) {
102                 int oldUseAging;
103
104                 useAging = !!useAging;
105                 ccgSubSurf_getUseAgeCounts(prevSS, &oldUseAging, NULL, NULL, NULL);
106
107                 if (oldUseAging!=useAging) {
108                         ccgSubSurf_free(prevSS);
109                 } else {
110                         ccgSubSurf_setSubdivisionLevels(prevSS, subdivLevels);
111
112                         return prevSS;
113                 }
114         }
115
116         if (useAging) {
117                 ifc.vertUserSize = ifc.edgeUserSize = ifc.faceUserSize = 12;
118         } else {
119                 ifc.vertUserSize = ifc.edgeUserSize = ifc.faceUserSize = 8;
120         }
121         ifc.vertDataSize = sizeof(DMGridData);
122
123         if (useArena) {
124                 CCGAllocatorIFC allocatorIFC;
125                 CCGAllocatorHDL allocator = BLI_memarena_new((1<<16));
126
127                 allocatorIFC.alloc = arena_alloc;
128                 allocatorIFC.realloc = arena_realloc;
129                 allocatorIFC.free = arena_free;
130                 allocatorIFC.release = arena_release;
131
132                 ccgSS = ccgSubSurf_new(&ifc, subdivLevels, &allocatorIFC, allocator);
133         } else {
134                 ccgSS = ccgSubSurf_new(&ifc, subdivLevels, NULL, NULL);
135         }
136
137         if (useAging) {
138                 ccgSubSurf_setUseAgeCounts(ccgSS, 1, 8, 8, 8);
139         }
140
141         ccgSubSurf_setCalcVertexNormals(ccgSS, 1, BLI_STRUCT_OFFSET(DMGridData, no));
142
143         return ccgSS;
144 }
145
146 static int getEdgeIndex(CCGSubSurf *ss, CCGEdge *e, int x, int edgeSize) {
147         CCGVert *v0 = ccgSubSurf_getEdgeVert0(e);
148         CCGVert *v1 = ccgSubSurf_getEdgeVert1(e);
149         int v0idx = *((int*) ccgSubSurf_getVertUserData(ss, v0));
150         int v1idx = *((int*) ccgSubSurf_getVertUserData(ss, v1));
151         int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e));
152
153         if (x==0) {
154                 return v0idx;
155         } else if (x==edgeSize-1) {
156                 return v1idx;
157         } else {
158                 return edgeBase + x-1;
159         }
160 }
161 static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edgeSize, int gridSize) {
162         int faceBase = *((int*) ccgSubSurf_getFaceUserData(ss, f));
163         int numVerts = ccgSubSurf_getFaceNumVerts(f);
164
165         if (x==gridSize-1 && y==gridSize-1) {
166                 CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
167                 return *((int*) ccgSubSurf_getVertUserData(ss, v));
168         } else if (x==gridSize-1) {
169                 CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
170                 CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, S);
171                 int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e));
172                 if (v==ccgSubSurf_getEdgeVert0(e)) {
173                         return edgeBase + (gridSize-1-y)-1;
174                 } else {
175                         return edgeBase + (edgeSize-2-1)-((gridSize-1-y)-1);
176                 }
177         } else if (y==gridSize-1) {
178                 CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
179                 CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, (S+numVerts-1)%numVerts);
180                 int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e));
181                 if (v==ccgSubSurf_getEdgeVert0(e)) {
182                         return edgeBase + (gridSize-1-x)-1;
183                 } else {
184                         return edgeBase + (edgeSize-2-1)-((gridSize-1-x)-1);
185                 }
186         } else if (x==0 && y==0) {
187                 return faceBase;
188         } else if (x==0) {
189                 S = (S+numVerts-1)%numVerts;
190                 return faceBase + 1 + (gridSize-2)*S + (y-1);
191         } else if (y==0) {
192                 return faceBase + 1 + (gridSize-2)*S + (x-1);
193         } else {
194                 return faceBase + 1 + (gridSize-2)*numVerts + S*(gridSize-2)*(gridSize-2) + (y-1)*(gridSize-2) + (x-1);
195         }
196 }
197
198 static void get_face_uv_map_vert(UvVertMap *vmap, struct MFace *mf, int fi, CCGVertHDL *fverts) {
199         unsigned int *fv = &mf->v1;
200         UvMapVert *v, *nv;
201         int j, nverts= mf->v4? 4: 3;
202
203         for (j=0; j<nverts; j++, fv++) {
204                 for (nv=v=get_uv_map_vert(vmap, *fv); v; v=v->next) {
205                         if (v->separate)
206                                 nv= v;
207                         if (v->f == fi)
208                                 break;
209                 }
210
211                 fverts[j]= SET_INT_IN_POINTER(nv->f*4 + nv->tfindex);
212         }
213 }
214
215 static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm, MTFace *tface) {
216         MFace *mface = dm->getFaceArray(dm);
217         MVert *mvert = dm->getVertArray(dm);
218         int totvert = dm->getNumVerts(dm);
219         int totface = dm->getNumFaces(dm);
220         int i, j, seam;
221         UvMapVert *v;
222         UvVertMap *vmap;
223         float limit[2];
224         CCGVertHDL fverts[4];
225         EdgeHash *ehash;
226         float creaseFactor = (float)ccgSubSurf_getSubdivisionLevels(ss);
227
228         limit[0]= limit[1]= STD_UV_CONNECT_LIMIT;
229         vmap= make_uv_vert_map(mface, tface, totface, totvert, 0, limit);
230         if (!vmap)
231                 return 0;
232         
233         ccgSubSurf_initFullSync(ss);
234
235         /* create vertices */
236         for (i=0; i<totvert; i++) {
237                 if (!get_uv_map_vert(vmap, i))
238                         continue;
239
240                 for (v=get_uv_map_vert(vmap, i)->next; v; v=v->next)
241                         if (v->separate)
242                                 break;
243
244                 seam = (v != NULL) || ((mvert+i)->flag & ME_VERT_MERGED);
245
246                 for (v=get_uv_map_vert(vmap, i); v; v=v->next) {
247                         if (v->separate) {
248                                 CCGVert *ssv;
249                                 CCGVertHDL vhdl = SET_INT_IN_POINTER(v->f*4 + v->tfindex);
250                                 float uv[3];
251
252                                 uv[0]= (tface+v->f)->uv[v->tfindex][0];
253                                 uv[1]= (tface+v->f)->uv[v->tfindex][1];
254                                 uv[2]= 0.0f;
255
256                                 ccgSubSurf_syncVert(ss, vhdl, uv, seam, &ssv);
257                         }
258                 }
259         }
260
261         /* create edges */
262         ehash = BLI_edgehash_new();
263
264         for (i=0; i<totface; i++) {
265                 MFace *mf = &((MFace*) mface)[i];
266                 int nverts= mf->v4? 4: 3;
267                 CCGFace *origf= ccgSubSurf_getFace(origss, SET_INT_IN_POINTER(i));
268                 unsigned int *fv = &mf->v1;
269
270                 get_face_uv_map_vert(vmap, mf, i, fverts);
271
272                 for (j=0; j<nverts; j++) {
273                         int v0 = GET_INT_FROM_POINTER(fverts[j]);
274                         int v1 = GET_INT_FROM_POINTER(fverts[(j+1)%nverts]);
275                         MVert *mv0 = mvert + *(fv+j);
276                         MVert *mv1 = mvert + *(fv+((j+1)%nverts));
277
278                         if (!BLI_edgehash_haskey(ehash, v0, v1)) {
279                                 CCGEdge *e, *orige= ccgSubSurf_getFaceEdge(origss, origf, j);
280                                 CCGEdgeHDL ehdl= SET_INT_IN_POINTER(i*4 + j);
281                                 float crease;
282
283                                 if ((mv0->flag&mv1->flag) & ME_VERT_MERGED)
284                                         crease = creaseFactor;
285                                 else
286                                         crease = ccgSubSurf_getEdgeCrease(orige);
287
288                                 ccgSubSurf_syncEdge(ss, ehdl, fverts[j], fverts[(j+1)%nverts], crease, &e);
289                                 BLI_edgehash_insert(ehash, v0, v1, NULL);
290                         }
291                 }
292         }
293
294         BLI_edgehash_free(ehash, NULL);
295
296         /* create faces */
297         for (i=0; i<totface; i++) {
298                 MFace *mf = &((MFace*) mface)[i];
299                 int nverts= mf->v4? 4: 3;
300                 CCGFace *f;
301
302                 get_face_uv_map_vert(vmap, mf, i, fverts);
303                 ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), nverts, fverts, &f);
304         }
305
306         free_uv_vert_map(vmap);
307         ccgSubSurf_processSync(ss);
308
309         return 1;
310 }
311
312 static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, int n)
313 {
314         CCGSubSurf *uvss;
315         CCGFace **faceMap;
316         MTFace *tf;
317         CCGFaceIterator *fi;
318         int index, gridSize, gridFaces, edgeSize, totface, x, y, S;
319         MTFace *dmtface = CustomData_get_layer_n(&dm->faceData, CD_MTFACE, n);
320         MTFace *tface = CustomData_get_layer_n(&result->faceData, CD_MTFACE, n);
321
322         if(!dmtface || !tface)
323                 return;
324
325         /* create a CCGSubSurf from uv's */
326         uvss = _getSubSurf(NULL, ccgSubSurf_getSubdivisionLevels(ss), 0, 1, 0);
327
328         if(!ss_sync_from_uv(uvss, ss, dm, dmtface)) {
329                 ccgSubSurf_free(uvss);
330                 return;
331         }
332
333         /* get some info from CCGSubSurf */
334         totface = ccgSubSurf_getNumFaces(uvss);
335         edgeSize = ccgSubSurf_getEdgeSize(uvss);
336         gridSize = ccgSubSurf_getGridSize(uvss);
337         gridFaces = gridSize - 1;
338
339         /* make a map from original faces to CCGFaces */
340         faceMap = MEM_mallocN(totface*sizeof(*faceMap), "facemapuv");
341
342         fi = ccgSubSurf_getFaceIterator(uvss);
343         for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
344                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
345                 faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(uvss, f))] = f;
346         }
347         ccgFaceIterator_free(fi);
348
349         /* load coordinates from uvss into tface */
350         tf= tface;
351
352         for(index = 0; index < totface; index++) {
353                 CCGFace *f = faceMap[index];
354                 int numVerts = ccgSubSurf_getFaceNumVerts(f);
355
356                 for (S=0; S<numVerts; S++) {
357                         DMGridData *faceGridData= ccgSubSurf_getFaceGridDataArray(uvss, f, S);
358
359                         for(y = 0; y < gridFaces; y++) {
360                                 for(x = 0; x < gridFaces; x++) {
361                                         float *a = faceGridData[(y + 0)*gridSize + x + 0].co;
362                                         float *b = faceGridData[(y + 0)*gridSize + x + 1].co;
363                                         float *c = faceGridData[(y + 1)*gridSize + x + 1].co;
364                                         float *d = faceGridData[(y + 1)*gridSize + x + 0].co;
365
366                                         tf->uv[0][0] = a[0]; tf->uv[0][1] = a[1];
367                                         tf->uv[1][0] = d[0]; tf->uv[1][1] = d[1];
368                                         tf->uv[2][0] = c[0]; tf->uv[2][1] = c[1];
369                                         tf->uv[3][0] = b[0]; tf->uv[3][1] = b[1];
370
371                                         tf++;
372                                 }
373                         }
374                 }
375         }
376
377         ccgSubSurf_free(uvss);
378         MEM_freeN(faceMap);
379 }
380
381 /* face weighting */
382 static void calc_ss_weights(int gridFaces,
383                             FaceVertWeight **qweight, FaceVertWeight **tweight)
384 {
385         FaceVertWeight *qw, *tw;
386         int x, y, j;
387         int numWeights = gridFaces * gridFaces;
388
389         *tweight = MEM_mallocN(sizeof(**tweight) * numWeights, "ssTriWeight");
390         *qweight = MEM_mallocN(sizeof(**qweight) * numWeights, "ssQuadWeight");
391
392         qw = *qweight;
393         tw = *tweight;
394
395         for (y = 0; y < gridFaces; y++) {
396                 for (x = 0; x < gridFaces; x++) {
397                         for (j = 0; j < 4; j++) {
398                                 int fx = x + (j == 2 || j == 3);
399                                 int fy = y + (j == 1 || j == 2);
400                                 float x_v = (float) fx / gridFaces;
401                                 float y_v = (float) fy / gridFaces;
402                                 float tx_v = (1.0f - x_v), ty_v = (1.0f - y_v);
403                                 float center = (1.0f / 3.0f) * tx_v * ty_v;
404
405                                 (*tw)[j][0] = center + 0.5f * tx_v * y_v;
406                                 (*tw)[j][2] = center + 0.5f * x_v * ty_v;
407                                 (*tw)[j][1] = 1.0f - (*tw)[j][0] - (*tw)[j][2];
408                                 (*tw)[j][3] = 0.0f;
409
410                                 tx_v *= 0.5f;
411                                 ty_v *= 0.5f;
412
413                                 (*qw)[j][3] = tx_v * ty_v;
414                                 (*qw)[j][0] = (*qw)[j][3] + tx_v * y_v;
415                                 (*qw)[j][2] = (*qw)[j][3] + x_v * ty_v;
416                                 (*qw)[j][1] = 1.0f - (*qw)[j][0] - (*qw)[j][2] - (*qw)[j][3];
417
418                         }
419                         tw++;
420                         qw++;
421                 }
422         }
423 }
424
425 static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
426                                      float (*vertexCos)[3], int useFlatSubdiv)
427 {
428         float creaseFactor = (float) ccgSubSurf_getSubdivisionLevels(ss);
429         CCGVertHDL fVerts[4];
430         int totvert = dm->getNumVerts(dm);
431         int totedge = dm->getNumEdges(dm);
432         int totface = dm->getNumFaces(dm);
433         int i;
434         int *index;
435         MVert *mvert = dm->getVertArray(dm);
436         MEdge *medge = dm->getEdgeArray(dm);
437         MFace *mface = dm->getFaceArray(dm);
438         MVert *mv;
439         MEdge *me;
440         MFace *mf;
441
442         ccgSubSurf_initFullSync(ss);
443
444         mv = mvert;
445         index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
446         for(i = 0; i < totvert; i++, mv++) {
447                 CCGVert *v;
448
449                 if(vertexCos) {
450                         ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), vertexCos[i], 0, &v);
451                 } else {
452                         ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), mv->co, 0, &v);
453                 }
454
455                 ((int*)ccgSubSurf_getVertUserData(ss, v))[1] = (index)? *index++: i;
456         }
457
458         me = medge;
459         index = (int *)dm->getEdgeDataArray(dm, CD_ORIGINDEX);
460         for(i = 0; i < totedge; i++, me++) {
461                 CCGEdge *e;
462                 float crease;
463
464                 crease = useFlatSubdiv ? creaseFactor :
465                                          me->crease * creaseFactor / 255.0f;
466
467                 ccgSubSurf_syncEdge(ss, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(me->v1),
468                                     SET_INT_IN_POINTER(me->v2), crease, &e);
469
470                 ((int*)ccgSubSurf_getEdgeUserData(ss, e))[1] = (index)? *index++: i;
471         }
472
473         mf = mface;
474         index = (int *)dm->getFaceDataArray(dm, CD_ORIGINDEX);
475         for (i = 0; i < totface; i++, mf++) {
476                 CCGFace *f;
477
478                 fVerts[0] = SET_INT_IN_POINTER(mf->v1);
479                 fVerts[1] = SET_INT_IN_POINTER(mf->v2);
480                 fVerts[2] = SET_INT_IN_POINTER(mf->v3);
481                 fVerts[3] = SET_INT_IN_POINTER(mf->v4);
482
483                 // this is very bad, means mesh is internally consistent.
484                 // it is not really possible to continue without modifying
485                 // other parts of code significantly to handle missing faces.
486                 // since this really shouldn't even be possible we just bail.
487                 if(ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), fVerts[3] ? 4 : 3,
488                                        fVerts, &f) == eCCGError_InvalidValue) {
489                         static int hasGivenError = 0;
490
491                         if(!hasGivenError) {
492                                 //XXX error("Unrecoverable error in SubSurf calculation,"
493                                 //      " mesh is inconsistent.");
494
495                                 hasGivenError = 1;
496                         }
497
498                         return;
499                 }
500
501                 ((int*)ccgSubSurf_getFaceUserData(ss, f))[1] = (index)? *index++: i;
502         }
503
504         ccgSubSurf_processSync(ss);
505 }
506
507 /***/
508
509 static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v) {
510         return ((int*) ccgSubSurf_getVertUserData(ss, v))[1];
511 }
512
513 static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e) {
514         return ((int*) ccgSubSurf_getEdgeUserData(ss, e))[1];
515 }
516
517 static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f) {
518         return ((int*) ccgSubSurf_getFaceUserData(ss, f))[1];
519 }
520
521 static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) {
522         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
523         CCGSubSurf *ss = ccgdm->ss;
524         CCGVertIterator *vi = ccgSubSurf_getVertIterator(ss);
525         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
526         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
527         int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
528         int gridSize = ccgSubSurf_getGridSize(ss);
529
530         if (!ccgSubSurf_getNumVerts(ss))
531                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
532
533         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
534                 CCGVert *v = ccgVertIterator_getCurrent(vi);
535                 float *co = ccgSubSurf_getVertData(ss, v);
536
537                 DO_MINMAX(co, min_r, max_r);
538         }
539
540         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
541                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
542                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
543
544                 for (i=0; i<edgeSize; i++)
545                         DO_MINMAX(edgeData[i].co, min_r, max_r);
546         }
547
548         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
549                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
550                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
551
552                 for (S=0; S<numVerts; S++) {
553                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
554
555                         for (y=0; y<gridSize; y++)
556                                 for (x=0; x<gridSize; x++)
557                                         DO_MINMAX(faceGridData[y*gridSize + x].co, min_r, max_r);
558                 }
559         }
560
561         ccgFaceIterator_free(fi);
562         ccgEdgeIterator_free(ei);
563         ccgVertIterator_free(vi);
564 }
565 static int ccgDM_getNumVerts(DerivedMesh *dm) {
566         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
567
568         return ccgSubSurf_getNumFinalVerts(ccgdm->ss);
569 }
570 static int ccgDM_getNumEdges(DerivedMesh *dm) {
571         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
572
573         return ccgSubSurf_getNumFinalEdges(ccgdm->ss);
574 }
575 static int ccgDM_getNumFaces(DerivedMesh *dm) {
576         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
577
578         return ccgSubSurf_getNumFinalFaces(ccgdm->ss);
579 }
580
581 static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
582 {
583         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
584         CCGSubSurf *ss = ccgdm->ss;
585         int i;
586
587         memset(mv, 0, sizeof(*mv));
588
589         if((vertNum < ccgdm->edgeMap[0].startVert) && (ccgSubSurf_getNumFaces(ss) > 0)) {
590                 /* this vert comes from face data */
591                 int lastface = ccgSubSurf_getNumFaces(ss) - 1;
592                 CCGFace *f;
593                 int x, y, grid, numVerts;
594                 int offset;
595                 int gridSize = ccgSubSurf_getGridSize(ss);
596                 int gridSideVerts;
597                 int gridInternalVerts;
598                 int gridSideEnd;
599                 int gridInternalEnd;
600
601                 i = 0;
602                 while(i < lastface && vertNum >= ccgdm->faceMap[i + 1].startVert)
603                         ++i;
604
605                 f = ccgdm->faceMap[i].face;
606                 numVerts = ccgSubSurf_getFaceNumVerts(f);
607
608                 gridSideVerts = gridSize - 2;
609                 gridInternalVerts = gridSideVerts * gridSideVerts;
610
611                 gridSideEnd = 1 + numVerts * gridSideVerts;
612                 gridInternalEnd = gridSideEnd + numVerts * gridInternalVerts;
613
614                 offset = vertNum - ccgdm->faceMap[i].startVert;
615                 if(offset < 1) {
616                         copy_v3_v3(mv->co, ccgSubSurf_getFaceCenterData(f));
617                 } else if(offset < gridSideEnd) {
618                         offset -= 1;
619                         grid = offset / gridSideVerts;
620                         x = offset % gridSideVerts + 1;
621                         copy_v3_v3(mv->co, ccgSubSurf_getFaceGridEdgeData(ss, f, grid, x));
622                 } else if(offset < gridInternalEnd) {
623                         offset -= gridSideEnd;
624                         grid = offset / gridInternalVerts;
625                         offset %= gridInternalVerts;
626                         y = offset / gridSideVerts + 1;
627                         x = offset % gridSideVerts + 1;
628                         copy_v3_v3(mv->co, ccgSubSurf_getFaceGridData(ss, f, grid, x, y));
629                 }
630         } else if((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) {
631                 /* this vert comes from edge data */
632                 CCGEdge *e;
633                 int lastedge = ccgSubSurf_getNumEdges(ss) - 1;
634                 int x;
635
636                 i = 0;
637                 while(i < lastedge && vertNum >= ccgdm->edgeMap[i + 1].startVert)
638                         ++i;
639
640                 e = ccgdm->edgeMap[i].edge;
641
642                 x = vertNum - ccgdm->edgeMap[i].startVert + 1;
643                 copy_v3_v3(mv->co, ccgSubSurf_getEdgeData(ss, e, x));
644         } else {
645                 /* this vert comes from vert data */
646                 CCGVert *v;
647                 i = vertNum - ccgdm->vertMap[0].startVert;
648
649                 v = ccgdm->vertMap[i].vert;
650                 copy_v3_v3(mv->co, ccgSubSurf_getVertData(ss, v));
651         }
652 }
653
654 static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med)
655 {
656         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
657         CCGSubSurf *ss = ccgdm->ss;
658         int i;
659
660         memset(med, 0, sizeof(*med));
661
662         if(edgeNum < ccgdm->edgeMap[0].startEdge) {
663                 /* this edge comes from face data */
664                 int lastface = ccgSubSurf_getNumFaces(ss) - 1;
665                 CCGFace *f;
666                 int x, y, grid, numVerts;
667                 int offset;
668                 int gridSize = ccgSubSurf_getGridSize(ss);
669                 int edgeSize = ccgSubSurf_getEdgeSize(ss);
670                 int gridSideEdges;
671                 int gridInternalEdges;
672
673                 i = 0;
674                 while(i < lastface && edgeNum >= ccgdm->faceMap[i + 1].startEdge)
675                         ++i;
676
677                 f = ccgdm->faceMap[i].face;
678                 numVerts = ccgSubSurf_getFaceNumVerts(f);
679
680                 gridSideEdges = gridSize - 1;
681                 gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2; 
682
683                 offset = edgeNum - ccgdm->faceMap[i].startEdge;
684                 grid = offset / (gridSideEdges + gridInternalEdges);
685                 offset %= (gridSideEdges + gridInternalEdges);
686
687                 if(offset < gridSideEdges) {
688                         x = offset;
689                         med->v1 = getFaceIndex(ss, f, grid, x, 0, edgeSize, gridSize);
690                         med->v2 = getFaceIndex(ss, f, grid, x+1, 0, edgeSize, gridSize);
691                 } else {
692                         offset -= gridSideEdges;
693                         x = (offset / 2) / gridSideEdges + 1;
694                         y = (offset / 2) % gridSideEdges;
695                         if(offset % 2 == 0) {
696                                 med->v1 = getFaceIndex(ss, f, grid, x, y, edgeSize, gridSize);
697                                 med->v2 = getFaceIndex(ss, f, grid, x, y+1, edgeSize, gridSize);
698                         } else {
699                                 med->v1 = getFaceIndex(ss, f, grid, y, x, edgeSize, gridSize);
700                                 med->v2 = getFaceIndex(ss, f, grid, y+1, x, edgeSize, gridSize);
701                         }
702                 }
703         } else {
704                 /* this vert comes from edge data */
705                 CCGEdge *e;
706                 int edgeSize = ccgSubSurf_getEdgeSize(ss);
707                 int x;
708                 short *edgeFlag;
709                 unsigned int flags = 0;
710
711                 i = (edgeNum - ccgdm->edgeMap[0].startEdge) / (edgeSize - 1);
712
713                 e = ccgdm->edgeMap[i].edge;
714
715                 if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
716
717                 x = edgeNum - ccgdm->edgeMap[i].startEdge;
718
719                 med->v1 = getEdgeIndex(ss, e, x, edgeSize);
720                 med->v2 = getEdgeIndex(ss, e, x+1, edgeSize);
721
722                 edgeFlag = (ccgdm->edgeFlags)? &ccgdm->edgeFlags[i]: NULL;
723                 if(edgeFlag)
724                         flags |= (*edgeFlag & (ME_SEAM | ME_SHARP))
725                                          | ME_EDGEDRAW | ME_EDGERENDER;
726                 else
727                         flags |= ME_EDGEDRAW | ME_EDGERENDER;
728
729                 med->flag = flags;
730         }
731 }
732
733 static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf)
734 {
735         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
736         CCGSubSurf *ss = ccgdm->ss;
737         int gridSize = ccgSubSurf_getGridSize(ss);
738         int edgeSize = ccgSubSurf_getEdgeSize(ss);
739         int gridSideEdges = gridSize - 1;
740         int gridFaces = gridSideEdges * gridSideEdges;
741         int i;
742         CCGFace *f;
743         int numVerts;
744         int offset;
745         int grid;
746         int x, y;
747         int lastface = ccgSubSurf_getNumFaces(ss) - 1;
748         char *faceFlags = ccgdm->faceFlags;
749
750         memset(mf, 0, sizeof(*mf));
751
752         i = 0;
753         while(i < lastface && faceNum >= ccgdm->faceMap[i + 1].startFace)
754                 ++i;
755
756         f = ccgdm->faceMap[i].face;
757         numVerts = ccgSubSurf_getFaceNumVerts(f);
758
759         offset = faceNum - ccgdm->faceMap[i].startFace;
760         grid = offset / gridFaces;
761         offset %= gridFaces;
762         y = offset / gridSideEdges;
763         x = offset % gridSideEdges;
764
765         mf->v1 = getFaceIndex(ss, f, grid, x+0, y+0, edgeSize, gridSize);
766         mf->v2 = getFaceIndex(ss, f, grid, x+0, y+1, edgeSize, gridSize);
767         mf->v3 = getFaceIndex(ss, f, grid, x+1, y+1, edgeSize, gridSize);
768         mf->v4 = getFaceIndex(ss, f, grid, x+1, y+0, edgeSize, gridSize);
769
770         if(faceFlags) mf->flag = faceFlags[i*2];
771         else mf->flag = ME_SMOOTH;
772 }
773
774 static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
775 {
776         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
777         CCGSubSurf *ss = ccgdm->ss;
778         DMGridData *vd;
779         int index;
780         int totvert, totedge, totface;
781         int gridSize = ccgSubSurf_getGridSize(ss);
782         int edgeSize = ccgSubSurf_getEdgeSize(ss);
783         int i = 0;
784
785         totface = ccgSubSurf_getNumFaces(ss);
786         for(index = 0; index < totface; index++) {
787                 CCGFace *f = ccgdm->faceMap[index].face;
788                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
789
790                 vd= ccgSubSurf_getFaceCenterData(f);
791                 copy_v3_v3(mvert[i].co, vd->co);
792                 normal_float_to_short_v3(mvert[i].no, vd->no);
793                 i++;
794                 
795                 for(S = 0; S < numVerts; S++) {
796                         for(x = 1; x < gridSize - 1; x++, i++) {
797                                 vd= ccgSubSurf_getFaceGridEdgeData(ss, f, S, x);
798                                 copy_v3_v3(mvert[i].co, vd->co);
799                                 normal_float_to_short_v3(mvert[i].no, vd->no);
800                         }
801                 }
802
803                 for(S = 0; S < numVerts; S++) {
804                         for(y = 1; y < gridSize - 1; y++) {
805                                 for(x = 1; x < gridSize - 1; x++, i++) {
806                                         vd= ccgSubSurf_getFaceGridData(ss, f, S, x, y);
807                                         copy_v3_v3(mvert[i].co, vd->co);
808                                         normal_float_to_short_v3(mvert[i].no, vd->no);
809                                 }
810                         }
811                 }
812         }
813
814         totedge = ccgSubSurf_getNumEdges(ss);
815         for(index = 0; index < totedge; index++) {
816                 CCGEdge *e = ccgdm->edgeMap[index].edge;
817                 int x;
818
819                 for(x = 1; x < edgeSize - 1; x++, i++) {
820                         vd= ccgSubSurf_getEdgeData(ss, e, x);
821                         copy_v3_v3(mvert[i].co, vd->co);
822                         /* TODO CCGSubsurf does not set these */
823                         normal_float_to_short_v3(mvert[i].no, vd->no);
824                 }
825         }
826
827         totvert = ccgSubSurf_getNumVerts(ss);
828         for(index = 0; index < totvert; index++) {
829                 CCGVert *v = ccgdm->vertMap[index].vert;
830
831                 vd= ccgSubSurf_getVertData(ss, v);
832                 copy_v3_v3(mvert[i].co, vd->co);
833                 normal_float_to_short_v3(mvert[i].no, vd->no);
834                 i++;
835         }
836 }
837
838 static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
839 {
840         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
841         CCGSubSurf *ss = ccgdm->ss;
842         int index;
843         int totedge, totface;
844         int gridSize = ccgSubSurf_getGridSize(ss);
845         int edgeSize = ccgSubSurf_getEdgeSize(ss);
846         int i = 0;
847         short *edgeFlags = ccgdm->edgeFlags;
848
849         totface = ccgSubSurf_getNumFaces(ss);
850         for(index = 0; index < totface; index++) {
851                 CCGFace *f = ccgdm->faceMap[index].face;
852                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
853
854                 for(S = 0; S < numVerts; S++) {
855                         for(x = 0; x < gridSize - 1; x++) {
856                                 MEdge *med = &medge[i];
857
858                                 if(ccgdm->drawInteriorEdges)
859                                     med->flag = ME_EDGEDRAW | ME_EDGERENDER;
860                                 med->v1 = getFaceIndex(ss, f, S, x, 0, edgeSize, gridSize);
861                                 med->v2 = getFaceIndex(ss, f, S, x + 1, 0, edgeSize, gridSize);
862                                 i++;
863                         }
864
865                         for(x = 1; x < gridSize - 1; x++) {
866                                 for(y = 0; y < gridSize - 1; y++) {
867                                         MEdge *med;
868
869                                         med = &medge[i];
870                                         if(ccgdm->drawInteriorEdges)
871                                             med->flag = ME_EDGEDRAW | ME_EDGERENDER;
872                                         med->v1 = getFaceIndex(ss, f, S, x, y,
873                                                                edgeSize, gridSize);
874                                         med->v2 = getFaceIndex(ss, f, S, x, y + 1,
875                                                                edgeSize, gridSize);
876                                         i++;
877
878                                         med = &medge[i];
879                                         if(ccgdm->drawInteriorEdges)
880                                             med->flag = ME_EDGEDRAW | ME_EDGERENDER;
881                                         med->v1 = getFaceIndex(ss, f, S, y, x,
882                                                                edgeSize, gridSize);
883                                         med->v2 = getFaceIndex(ss, f, S, y + 1, x,
884                                                                edgeSize, gridSize);
885                                         i++;
886                                 }
887                         }
888                 }
889         }
890
891         totedge = ccgSubSurf_getNumEdges(ss);
892         for(index = 0; index < totedge; index++) {
893                 CCGEdge *e = ccgdm->edgeMap[index].edge;
894                 unsigned int flags = 0;
895                 int x;
896                 int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
897
898                 if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
899
900                 if(edgeFlags) {
901                         if(edgeIdx != -1) {
902                                 flags |= (edgeFlags[index] & (ME_SEAM | ME_SHARP))
903                                          | ME_EDGEDRAW | ME_EDGERENDER;
904                         }
905                 } else {
906                         flags |= ME_EDGEDRAW | ME_EDGERENDER;
907                 }
908
909                 for(x = 0; x < edgeSize - 1; x++) {
910                         MEdge *med = &medge[i];
911                         med->v1 = getEdgeIndex(ss, e, x, edgeSize);
912                         med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
913                         med->flag = flags;
914                         i++;
915                 }
916         }
917 }
918
919 static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface)
920 {
921         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
922         CCGSubSurf *ss = ccgdm->ss;
923         int index;
924         int totface;
925         int gridSize = ccgSubSurf_getGridSize(ss);
926         int edgeSize = ccgSubSurf_getEdgeSize(ss);
927         int i = 0;
928         char *faceFlags = ccgdm->faceFlags;
929
930         totface = ccgSubSurf_getNumFaces(ss);
931         for(index = 0; index < totface; index++) {
932                 CCGFace *f = ccgdm->faceMap[index].face;
933                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
934                 int flag = (faceFlags)? faceFlags[index*2]: ME_SMOOTH;
935                 int mat_nr = (faceFlags)? faceFlags[index*2+1]: 0;
936
937                 for(S = 0; S < numVerts; S++) {
938                         for(y = 0; y < gridSize - 1; y++) {
939                                 for(x = 0; x < gridSize - 1; x++) {
940                                         MFace *mf = &mface[i];
941                                         mf->v1 = getFaceIndex(ss, f, S, x + 0, y + 0,
942                                                               edgeSize, gridSize);
943                                         mf->v2 = getFaceIndex(ss, f, S, x + 0, y + 1,
944                                                               edgeSize, gridSize);
945                                         mf->v3 = getFaceIndex(ss, f, S, x + 1, y + 1,
946                                                               edgeSize, gridSize);
947                                         mf->v4 = getFaceIndex(ss, f, S, x + 1, y + 0,
948                                                               edgeSize, gridSize);
949                                         mf->mat_nr = mat_nr;
950                                         mf->flag = flag;
951
952                                         i++;
953                                 }
954                         }
955                 }
956         }
957 }
958
959 static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) {
960         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
961         CCGSubSurf *ss = ccgdm->ss;
962         int edgeSize = ccgSubSurf_getEdgeSize(ss);
963         int gridSize = ccgSubSurf_getGridSize(ss);
964         int i;
965         CCGVertIterator *vi;
966         CCGEdgeIterator *ei;
967         CCGFaceIterator *fi;
968         CCGFace **faceMap2;
969         CCGEdge **edgeMap2;
970         CCGVert **vertMap2;
971         int index, totvert, totedge, totface;
972         
973         totvert = ccgSubSurf_getNumVerts(ss);
974         vertMap2 = MEM_mallocN(totvert*sizeof(*vertMap2), "vertmap");
975         vi = ccgSubSurf_getVertIterator(ss);
976         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
977                 CCGVert *v = ccgVertIterator_getCurrent(vi);
978
979                 vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v;
980         }
981         ccgVertIterator_free(vi);
982
983         totedge = ccgSubSurf_getNumEdges(ss);
984         edgeMap2 = MEM_mallocN(totedge*sizeof(*edgeMap2), "edgemap");
985         ei = ccgSubSurf_getEdgeIterator(ss);
986         for (i=0; !ccgEdgeIterator_isStopped(ei); i++,ccgEdgeIterator_next(ei)) {
987                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
988
989                 edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e;
990         }
991
992         totface = ccgSubSurf_getNumFaces(ss);
993         faceMap2 = MEM_mallocN(totface*sizeof(*faceMap2), "facemap");
994         fi = ccgSubSurf_getFaceIterator(ss);
995         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
996                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
997
998                 faceMap2[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f))] = f;
999         }
1000         ccgFaceIterator_free(fi);
1001
1002         i = 0;
1003         for (index=0; index<totface; index++) {
1004                 CCGFace *f = faceMap2[index];
1005                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
1006
1007                 copy_v3_v3(cos[i++], ccgSubSurf_getFaceCenterData(f));
1008                 
1009                 for (S=0; S<numVerts; S++) {
1010                         for (x=1; x<gridSize-1; x++) {
1011                                 copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
1012                         }
1013                 }
1014
1015                 for (S=0; S<numVerts; S++) {
1016                         for (y=1; y<gridSize-1; y++) {
1017                                 for (x=1; x<gridSize-1; x++) {
1018                                         copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridData(ss, f, S, x, y));
1019                                 }
1020                         }
1021                 }
1022         }
1023
1024         for (index=0; index<totedge; index++) {
1025                 CCGEdge *e= edgeMap2[index];
1026                 int x;
1027
1028                 for (x=1; x<edgeSize-1; x++) {
1029                         copy_v3_v3(cos[i++], ccgSubSurf_getEdgeData(ss, e, x));
1030                 }
1031         }
1032
1033         for (index=0; index<totvert; index++) {
1034                 CCGVert *v = vertMap2[index];
1035                 copy_v3_v3(cos[i++], ccgSubSurf_getVertData(ss, v));
1036         }
1037
1038         MEM_freeN(vertMap2);
1039         MEM_freeN(edgeMap2);
1040         MEM_freeN(faceMap2);
1041 }
1042 static void ccgDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData) {
1043         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1044         CCGVertIterator *vi = ccgSubSurf_getVertIterator(ccgdm->ss);
1045
1046         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
1047                 CCGVert *v = ccgVertIterator_getCurrent(vi);
1048                 DMGridData *vd = ccgSubSurf_getVertData(ccgdm->ss, v);
1049                 int index = ccgDM_getVertMapIndex(ccgdm->ss, v);
1050
1051                 if (index!=-1)
1052                         func(userData, index, vd->co, vd->no, NULL);
1053         }
1054
1055         ccgVertIterator_free(vi);
1056 }
1057 static void ccgDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData) {
1058         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1059         CCGSubSurf *ss = ccgdm->ss;
1060         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1061         int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
1062
1063         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1064                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1065                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1066                 int index = ccgDM_getEdgeMapIndex(ss, e);
1067
1068                 if (index!=-1) {
1069                         for (i=0; i<edgeSize-1; i++)
1070                                 func(userData, index, edgeData[i].co, edgeData[i+1].co);
1071                 }
1072         }
1073
1074         ccgEdgeIterator_free(ei);
1075 }
1076
1077 static void ccgDM_drawVerts(DerivedMesh *dm) {
1078         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1079         CCGSubSurf *ss = ccgdm->ss;
1080         int edgeSize = ccgSubSurf_getEdgeSize(ss);
1081         int gridSize = ccgSubSurf_getGridSize(ss);
1082         CCGVertIterator *vi;
1083         CCGEdgeIterator *ei;
1084         CCGFaceIterator *fi;
1085
1086         glBegin(GL_POINTS);
1087         vi = ccgSubSurf_getVertIterator(ss);
1088         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
1089                 CCGVert *v = ccgVertIterator_getCurrent(vi);
1090                 glVertex3fv(ccgSubSurf_getVertData(ss, v));
1091         }
1092         ccgVertIterator_free(vi);
1093
1094         ei = ccgSubSurf_getEdgeIterator(ss);
1095         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1096                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1097                 int x;
1098
1099                 for (x=1; x<edgeSize-1; x++)
1100                         glVertex3fv(ccgSubSurf_getEdgeData(ss, e, x));
1101         }
1102         ccgEdgeIterator_free(ei);
1103
1104         fi = ccgSubSurf_getFaceIterator(ss);
1105         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1106                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1107                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
1108
1109                 glVertex3fv(ccgSubSurf_getFaceCenterData(f));
1110                 for (S=0; S<numVerts; S++)
1111                         for (x=1; x<gridSize-1; x++)
1112                                 glVertex3fv(ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
1113                 for (S=0; S<numVerts; S++)
1114                         for (y=1; y<gridSize-1; y++)
1115                                 for (x=1; x<gridSize-1; x++)
1116                                         glVertex3fv(ccgSubSurf_getFaceGridData(ss, f, S, x, y));
1117         }
1118         ccgFaceIterator_free(fi);
1119         glEnd();
1120 }
1121 static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) {
1122         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1123         CCGSubSurf *ss = ccgdm->ss;
1124         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1125         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1126         int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
1127         int gridSize = ccgSubSurf_getGridSize(ss);
1128         int useAging;
1129
1130         ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
1131
1132         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1133                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1134                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1135
1136                 if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e))
1137                         continue;
1138
1139                 if (useAging && !(G.f&G_BACKBUFSEL)) {
1140                         int ageCol = 255-ccgSubSurf_getEdgeAge(ss, e)*4;
1141                         glColor3ub(0, ageCol>0?ageCol:0, 0);
1142                 }
1143
1144                 glBegin(GL_LINE_STRIP);
1145                 for (i=0; i<edgeSize-1; i++) {
1146                         glVertex3fv(edgeData[i].co);
1147                         glVertex3fv(edgeData[i+1].co);
1148                 }
1149                 glEnd();
1150         }
1151
1152         if (useAging && !(G.f&G_BACKBUFSEL)) {
1153                 glColor3ub(0, 0, 0);
1154         }
1155
1156         if (ccgdm->drawInteriorEdges) {
1157                 for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1158                         CCGFace *f = ccgFaceIterator_getCurrent(fi);
1159                         int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1160
1161                         for (S=0; S<numVerts; S++) {
1162                                 DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1163
1164                                 glBegin(GL_LINE_STRIP);
1165                                 for (x=0; x<gridSize; x++)
1166                                         glVertex3fv(faceGridData[x].co);
1167                                 glEnd();
1168                                 for (y=1; y<gridSize-1; y++) {
1169                                         glBegin(GL_LINE_STRIP);
1170                                         for (x=0; x<gridSize; x++)
1171                                                 glVertex3fv(faceGridData[y*gridSize + x].co);
1172                                         glEnd();
1173                                 }
1174                                 for (x=1; x<gridSize-1; x++) {
1175                                         glBegin(GL_LINE_STRIP);
1176                                         for (y=0; y<gridSize; y++)
1177                                                 glVertex3fv(faceGridData[y*gridSize + x].co);
1178                                         glEnd();
1179                                 }
1180                         }
1181                 }
1182         }
1183
1184         ccgFaceIterator_free(fi);
1185         ccgEdgeIterator_free(ei);
1186 }
1187 static void ccgDM_drawLooseEdges(DerivedMesh *dm) {
1188         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1189         CCGSubSurf *ss = ccgdm->ss;
1190         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1191         int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
1192
1193         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1194                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1195                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1196
1197                 if (!ccgSubSurf_getEdgeNumFaces(e)) {
1198                         glBegin(GL_LINE_STRIP);
1199                         for (i=0; i<edgeSize-1; i++) {
1200                                 glVertex3fv(edgeData[i].co);
1201                                 glVertex3fv(edgeData[i+1].co);
1202                         }
1203                         glEnd();
1204                 }
1205         }
1206
1207         ccgEdgeIterator_free(ei);
1208 }
1209
1210 static void ccgDM_glNormalFast(float *a, float *b, float *c, float *d)
1211 {
1212         float a_cX = c[0]-a[0], a_cY = c[1]-a[1], a_cZ = c[2]-a[2];
1213         float b_dX = d[0]-b[0], b_dY = d[1]-b[1], b_dZ = d[2]-b[2];
1214         float no[3];
1215
1216         no[0] = b_dY*a_cZ - b_dZ*a_cY;
1217         no[1] = b_dZ*a_cX - b_dX*a_cZ;
1218         no[2] = b_dX*a_cY - b_dY*a_cX;
1219
1220         /* don't normalize, GL_NORMALIZE is be enabled */
1221         glNormal3fv(no);
1222 }
1223
1224         /* Only used by non-editmesh types */
1225 static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes)[4], int fast, int (*setMaterial)(int, void *attribs)) {
1226         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1227         CCGSubSurf *ss = ccgdm->ss;
1228         CCGFaceIterator *fi;
1229         int gridSize = ccgSubSurf_getGridSize(ss);
1230         char *faceFlags = ccgdm->faceFlags;
1231         int step = (fast)? gridSize-1: 1;
1232
1233         if(ccgdm->pbvh && ccgdm->multires.mmd && !fast) {
1234                 CCGFace **faces;
1235                 int totface;
1236
1237                 BLI_pbvh_get_grid_updates(ccgdm->pbvh, 1, (void***)&faces, &totface);
1238                 if(totface) {
1239                         ccgSubSurf_updateFromFaces(ss, 0, faces, totface);
1240                         ccgSubSurf_updateNormals(ss, faces, totface);
1241                         MEM_freeN(faces);
1242                 }
1243
1244                 /* should be per face */
1245                 if(faceFlags && faceFlags[0] & ME_SMOOTH)
1246                         glShadeModel(GL_SMOOTH);
1247
1248                 BLI_pbvh_draw(ccgdm->pbvh, partial_redraw_planes, NULL);
1249
1250                 glShadeModel(GL_FLAT);
1251
1252                 return;
1253         }
1254
1255         fi = ccgSubSurf_getFaceIterator(ss);
1256         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1257                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1258                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1259                 int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1260                 int drawSmooth, mat_nr;
1261
1262                 if(faceFlags) {
1263                         drawSmooth = (faceFlags[index*2] & ME_SMOOTH);
1264                         mat_nr= faceFlags[index*2 + 1];
1265                 }
1266                 else {
1267                         drawSmooth = 1;
1268                         mat_nr= 0;
1269                 }
1270                 
1271                 if (!setMaterial(mat_nr+1, NULL))
1272                         continue;
1273
1274                 glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT);
1275                 for (S=0; S<numVerts; S++) {
1276                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1277
1278                         if (drawSmooth) {
1279                                 for (y=0; y<gridSize-1; y+=step) {
1280                                         glBegin(GL_QUAD_STRIP);
1281                                         for (x=0; x<gridSize; x+=step) {
1282                                                 DMGridData *a = &faceGridData[(y+0)*gridSize + x];
1283                                                 DMGridData *b = &faceGridData[(y+step)*gridSize + x];
1284
1285                                                 glNormal3fv(a->no);
1286                                                 glVertex3fv(a->co);
1287                                                 glNormal3fv(b->no);
1288                                                 glVertex3fv(b->co);
1289                                         }
1290                                         glEnd();
1291                                 }
1292                         } else {
1293                                 glBegin(GL_QUADS);
1294                                 for (y=0; y<gridSize-1; y+=step) {
1295                                         for (x=0; x<gridSize-1; x+=step) {
1296                                                 float *a = faceGridData[(y+0)*gridSize + x].co;
1297                                                 float *b = faceGridData[(y+0)*gridSize + x + step].co;
1298                                                 float *c = faceGridData[(y+step)*gridSize + x + step].co;
1299                                                 float *d = faceGridData[(y+step)*gridSize + x].co;
1300
1301                                                 ccgDM_glNormalFast(a, b, c, d);
1302
1303                                                 glVertex3fv(d);
1304                                                 glVertex3fv(c);
1305                                                 glVertex3fv(b);
1306                                                 glVertex3fv(a);
1307                                         }
1308                                 }
1309                                 glEnd();
1310                         }
1311                 }
1312         }
1313
1314         ccgFaceIterator_free(fi);
1315 }
1316
1317         /* Only used by non-editmesh types */
1318 static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs), int (*setDrawOptions)(void *userData, int index), void *userData) {
1319         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1320         CCGSubSurf *ss = ccgdm->ss;
1321         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1322         GPUVertexAttribs gattribs;
1323         DMVertexAttribs attribs;
1324         MTFace *tf = dm->getFaceDataArray(dm, CD_MTFACE);
1325         int gridSize = ccgSubSurf_getGridSize(ss);
1326         int gridFaces = gridSize - 1;
1327         int edgeSize = ccgSubSurf_getEdgeSize(ss);
1328         int transp, orig_transp, new_transp;
1329         char *faceFlags = ccgdm->faceFlags;
1330         int a, b, i, doDraw, numVerts, matnr, new_matnr, totface;
1331
1332         doDraw = 0;
1333         numVerts = 0;
1334         matnr = -1;
1335         transp = GPU_get_material_blend_mode();
1336         orig_transp = transp;
1337
1338         memset(&attribs, 0, sizeof(attribs));
1339
1340 #define PASSATTRIB(dx, dy, vert) {                                                                                              \
1341         if(attribs.totorco) {                                                                                                           \
1342                 index = getFaceIndex(ss, f, S, x+dx, y+dy, edgeSize, gridSize);                 \
1343                 glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]);  \
1344         }                                                                                                                                                       \
1345         for(b = 0; b < attribs.tottface; b++) {                                                                         \
1346                 MTFace *tf = &attribs.tface[b].array[a];                                                                \
1347                 glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]);                   \
1348         }                                                                                                                                                       \
1349         for(b = 0; b < attribs.totmcol; b++) {                                                                          \
1350                 MCol *cp = &attribs.mcol[b].array[a*4 + vert];                                                  \
1351                 GLubyte col[4];                                                                                                                 \
1352                 col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;                             \
1353                 glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col);                                    \
1354         }                                                                                                                                                       \
1355         if(attribs.tottang) {                                                                                                           \
1356                 float *tang = attribs.tang.array[a*4 + vert];                                                   \
1357                 glVertexAttrib3fvARB(attribs.tang.glIndex, tang);                                               \
1358         }                                                                                                                                                       \
1359 }
1360
1361         totface = ccgSubSurf_getNumFaces(ss);
1362         for(a = 0, i = 0; i < totface; i++) {
1363                 CCGFace *f = ccgdm->faceMap[i].face;
1364                 int S, x, y, drawSmooth;
1365                 int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1366                 int origIndex = ccgDM_getFaceMapIndex(ss, f);
1367                 
1368                 numVerts = ccgSubSurf_getFaceNumVerts(f);
1369
1370                 if(faceFlags) {
1371                         drawSmooth = (faceFlags[index*2] & ME_SMOOTH);
1372                         new_matnr= faceFlags[index*2 + 1] + 1;
1373                 }
1374                 else {
1375                         drawSmooth = 1;
1376                         new_matnr= 1;
1377                 }
1378
1379                 if(new_matnr != matnr) {
1380                         doDraw = setMaterial(matnr = new_matnr, &gattribs);
1381                         if(doDraw)
1382                                 DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
1383                 }
1384
1385                 if(!doDraw || (setDrawOptions && !setDrawOptions(userData, origIndex))) {
1386                         a += gridFaces*gridFaces*numVerts;
1387                         continue;
1388                 }
1389
1390                 if(tf) {
1391                         new_transp = tf[i].transp;
1392
1393                         if(new_transp != transp) {
1394                                 if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID)
1395                                         GPU_set_material_blend_mode(orig_transp);
1396                                 else
1397                                         GPU_set_material_blend_mode(new_transp);
1398                                 transp = new_transp;
1399                         }
1400                 }
1401
1402                 glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT);
1403                 for (S=0; S<numVerts; S++) {
1404                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1405                         DMGridData *vda, *vdb;
1406
1407                         if (drawSmooth) {
1408                                 for (y=0; y<gridFaces; y++) {
1409                                         glBegin(GL_QUAD_STRIP);
1410                                         for (x=0; x<gridFaces; x++) {
1411                                                 vda = &faceGridData[(y+0)*gridSize + x];
1412                                                 vdb = &faceGridData[(y+1)*gridSize + x];
1413                                                 
1414                                                 PASSATTRIB(0, 0, 0);
1415                                                 glNormal3fv(vda->no);
1416                                                 glVertex3fv(vda->co);
1417
1418                                                 PASSATTRIB(0, 1, 1);
1419                                                 glNormal3fv(vdb->no);
1420                                                 glVertex3fv(vdb->co);
1421
1422                                                 if(x != gridFaces-1)
1423                                                         a++;
1424                                         }
1425
1426                                         vda = &faceGridData[(y+0)*gridSize + x];
1427                                         vdb = &faceGridData[(y+1)*gridSize + x];
1428
1429                                         PASSATTRIB(0, 0, 3);
1430                                         glNormal3fv(vda->no);
1431                                         glVertex3fv(vda->co);
1432
1433                                         PASSATTRIB(0, 1, 2);
1434                                         glNormal3fv(vdb->no);
1435                                         glVertex3fv(vdb->co);
1436
1437                                         glEnd();
1438
1439                                         a++;
1440                                 }
1441                         } else {
1442                                 glBegin(GL_QUADS);
1443                                 for (y=0; y<gridFaces; y++) {
1444                                         for (x=0; x<gridFaces; x++) {
1445                                                 float *aco = faceGridData[(y+0)*gridSize + x].co;
1446                                                 float *bco = faceGridData[(y+0)*gridSize + x + 1].co;
1447                                                 float *cco = faceGridData[(y+1)*gridSize + x + 1].co;
1448                                                 float *dco = faceGridData[(y+1)*gridSize + x].co;
1449
1450                                                 ccgDM_glNormalFast(aco, bco, cco, dco);
1451
1452                                                 PASSATTRIB(0, 1, 1);
1453                                                 glVertex3fv(dco);
1454                                                 PASSATTRIB(1, 1, 2);
1455                                                 glVertex3fv(cco);
1456                                                 PASSATTRIB(1, 0, 3);
1457                                                 glVertex3fv(bco);
1458                                                 PASSATTRIB(0, 0, 0);
1459                                                 glVertex3fv(aco);
1460                                                 
1461                                                 a++;
1462                                         }
1463                                 }
1464                                 glEnd();
1465                         }
1466                 }
1467         }
1468
1469 #undef PASSATTRIB
1470
1471         ccgFaceIterator_free(fi);
1472 }
1473
1474 static void ccgDM_drawFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) {
1475         dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL);
1476 }
1477
1478 static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2) {
1479         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1480         CCGSubSurf *ss = ccgdm->ss;
1481         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1482         int gridSize = ccgSubSurf_getGridSize(ss);
1483         unsigned char *cp1, *cp2;
1484         int useTwoSide=1;
1485
1486         cp1= col1;
1487         if(col2) {
1488                 cp2= col2;
1489         } else {
1490                 cp2= NULL;
1491                 useTwoSide= 0;
1492         }
1493
1494         glShadeModel(GL_SMOOTH);
1495         if(col1 && col2)
1496                 glEnable(GL_CULL_FACE);
1497
1498         glBegin(GL_QUADS);
1499         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1500                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1501                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1502
1503                 for (S=0; S<numVerts; S++) {
1504                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1505                         for (y=0; y<gridSize-1; y++) {
1506                                 for (x=0; x<gridSize-1; x++) {
1507                                         float *a = faceGridData[(y+0)*gridSize + x].co;
1508                                         float *b = faceGridData[(y+0)*gridSize + x + 1].co;
1509                                         float *c = faceGridData[(y+1)*gridSize + x + 1].co;
1510                                         float *d = faceGridData[(y+1)*gridSize + x].co;
1511
1512                                         glColor3ub(cp1[3], cp1[2], cp1[1]);
1513                                         glVertex3fv(d);
1514                                         glColor3ub(cp1[7], cp1[6], cp1[5]);
1515                                         glVertex3fv(c);
1516                                         glColor3ub(cp1[11], cp1[10], cp1[9]);
1517                                         glVertex3fv(b);
1518                                         glColor3ub(cp1[15], cp1[14], cp1[13]);
1519                                         glVertex3fv(a);
1520
1521                                         if (useTwoSide) {
1522                                                 glColor3ub(cp2[15], cp2[14], cp2[13]);
1523                                                 glVertex3fv(a);
1524                                                 glColor3ub(cp2[11], cp2[10], cp2[9]);
1525                                                 glVertex3fv(b);
1526                                                 glColor3ub(cp2[7], cp2[6], cp2[5]);
1527                                                 glVertex3fv(c);
1528                                                 glColor3ub(cp2[3], cp2[2], cp2[1]);
1529                                                 glVertex3fv(d);
1530                                         }
1531
1532                                         if (cp2) cp2+=16;
1533                                         cp1+=16;
1534                                 }
1535                         }
1536                 }
1537         }
1538         glEnd();
1539
1540         ccgFaceIterator_free(fi);
1541 }
1542
1543 static void ccgDM_drawFacesTex_common(DerivedMesh *dm,
1544         int (*drawParams)(MTFace *tface, MCol *mcol, int matnr),
1545         int (*drawParamsMapped)(void *userData, int index),
1546         void *userData) 
1547 {
1548         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1549         CCGSubSurf *ss = ccgdm->ss;
1550         MCol *mcol = dm->getFaceDataArray(dm, CD_WEIGHT_MCOL);
1551         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
1552         char *faceFlags = ccgdm->faceFlags;
1553         int i, totface, flag, gridSize = ccgSubSurf_getGridSize(ss);
1554         int gridFaces = gridSize - 1;
1555
1556         if(!mcol)
1557                 mcol = dm->getFaceDataArray(dm, CD_MCOL);
1558
1559         totface = ccgSubSurf_getNumFaces(ss);
1560         for(i = 0; i < totface; i++) {
1561                 CCGFace *f = ccgdm->faceMap[i].face;
1562                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1563                 int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
1564                 int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1565                 unsigned char *cp= NULL;
1566                 int mat_nr;
1567
1568                 if(faceFlags) {
1569                         drawSmooth = (faceFlags[origIndex*2] & ME_SMOOTH);
1570                         mat_nr= faceFlags[origIndex*2 + 1];
1571                 }
1572                 else {
1573                         drawSmooth = 1;
1574                         mat_nr= 0;
1575                 }
1576
1577                 if(drawParams)
1578                         flag = drawParams(tf, mcol, mat_nr);
1579                 else
1580                         flag= (drawParamsMapped)? drawParamsMapped(userData, index): 1;
1581                 
1582                 if (flag == 0) { /* flag 0 == the face is hidden or invisible */
1583                         if(tf) tf += gridFaces*gridFaces*numVerts;
1584                         if(mcol) mcol += gridFaces*gridFaces*numVerts*4;
1585                         continue;
1586                 }
1587
1588                 /* flag 1 == use vertex colors */
1589                 if(mcol) {
1590                         if(flag==1) cp= (unsigned char*)mcol;
1591                         mcol += gridFaces*gridFaces*numVerts*4;
1592                 }
1593
1594                 for (S=0; S<numVerts; S++) {
1595                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1596                         DMGridData *a, *b;
1597
1598                         if (drawSmooth) {
1599                                 glShadeModel(GL_SMOOTH);
1600                                 for (y=0; y<gridFaces; y++) {
1601                                         glBegin(GL_QUAD_STRIP);
1602                                         for (x=0; x<gridFaces; x++) {
1603                                                 a = &faceGridData[(y+0)*gridSize + x];
1604                                                 b = &faceGridData[(y+1)*gridSize + x];
1605
1606                                                 if(tf) glTexCoord2fv(tf->uv[0]);
1607                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1608                                                 glNormal3fv(a->no);
1609                                                 glVertex3fv(a->co);
1610
1611                                                 if(tf) glTexCoord2fv(tf->uv[1]);
1612                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1613                                                 glNormal3fv(b->no);
1614                                                 glVertex3fv(b->co);
1615                                                 
1616                                                 if(x != gridFaces-1) {
1617                                                         if(tf) tf++;
1618                                                         if(cp) cp += 16;
1619                                                 }
1620                                         }
1621
1622                                         a = &faceGridData[(y+0)*gridSize + x];
1623                                         b = &faceGridData[(y+1)*gridSize + x];
1624
1625                                         if(tf) glTexCoord2fv(tf->uv[3]);
1626                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1627                                         glNormal3fv(a->no);
1628                                         glVertex3fv(a->co);
1629
1630                                         if(tf) glTexCoord2fv(tf->uv[2]);
1631                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1632                                         glNormal3fv(b->no);
1633                                         glVertex3fv(b->co);
1634
1635                                         if(tf) tf++;
1636                                         if(cp) cp += 16;
1637
1638                                         glEnd();
1639                                 }
1640                         } else {
1641                                 glShadeModel(GL_FLAT);
1642                                 glBegin(GL_QUADS);
1643                                 for (y=0; y<gridFaces; y++) {
1644                                         for (x=0; x<gridFaces; x++) {
1645                                                 float *a_co = faceGridData[(y+0)*gridSize + x].co;
1646                                                 float *b_co = faceGridData[(y+0)*gridSize + x + 1].co;
1647                                                 float *c_co = faceGridData[(y+1)*gridSize + x + 1].co;
1648                                                 float *d_co = faceGridData[(y+1)*gridSize + x].co;
1649
1650                                                 ccgDM_glNormalFast(a_co, b_co, c_co, d_co);
1651
1652                                                 if(tf) glTexCoord2fv(tf->uv[1]);
1653                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1654                                                 glVertex3fv(d_co);
1655
1656                                                 if(tf) glTexCoord2fv(tf->uv[2]);
1657                                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1658                                                 glVertex3fv(c_co);
1659
1660                                                 if(tf) glTexCoord2fv(tf->uv[3]);
1661                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1662                                                 glVertex3fv(b_co);
1663
1664                                                 if(tf) glTexCoord2fv(tf->uv[0]);
1665                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1666                                                 glVertex3fv(a_co);
1667
1668                                                 if(tf) tf++;
1669                                                 if(cp) cp += 16;
1670                                         }
1671                                 }
1672                                 glEnd();
1673                         }
1674                 }
1675         }
1676 }
1677
1678 static void ccgDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
1679 {
1680         ccgDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
1681 }
1682
1683 static void ccgDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
1684 {
1685         ccgDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
1686 }
1687
1688 static void ccgDM_drawUVEdges(DerivedMesh *dm)
1689 {
1690
1691         MFace *mf = dm->getFaceArray(dm);
1692         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
1693         int i;
1694         
1695         if (tf) {
1696                 glBegin(GL_LINES);
1697                 for(i = 0; i < dm->numFaceData; i++, mf++, tf++) {
1698                         if(!(mf->flag&ME_HIDE)) {
1699                                 glVertex2fv(tf->uv[0]);
1700                                 glVertex2fv(tf->uv[1]);
1701         
1702                                 glVertex2fv(tf->uv[1]);
1703                                 glVertex2fv(tf->uv[2]);
1704         
1705                                 if(!mf->v4) {
1706                                         glVertex2fv(tf->uv[2]);
1707                                         glVertex2fv(tf->uv[0]);
1708                                 } else {
1709                                         glVertex2fv(tf->uv[2]);
1710                                         glVertex2fv(tf->uv[3]);
1711         
1712                                         glVertex2fv(tf->uv[3]);
1713                                         glVertex2fv(tf->uv[0]);
1714                                 }
1715                         }
1716                 }
1717                 glEnd();
1718         }
1719 }
1720
1721 static void ccgDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors) {
1722         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1723         CCGSubSurf *ss = ccgdm->ss;
1724         MCol *mcol= NULL;
1725         int i, gridSize = ccgSubSurf_getGridSize(ss);
1726         char *faceFlags = ccgdm->faceFlags;
1727         int gridFaces = gridSize - 1, totface;
1728
1729         if(useColors) {
1730                 mcol = dm->getFaceDataArray(dm, CD_WEIGHT_MCOL);
1731                 if(!mcol)
1732                         mcol = dm->getFaceDataArray(dm, CD_MCOL);
1733         }
1734
1735         totface = ccgSubSurf_getNumFaces(ss);
1736         for(i = 0; i < totface; i++) {
1737                 CCGFace *f = ccgdm->faceMap[i].face;
1738                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1739                 int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
1740                 int origIndex;
1741                 unsigned char *cp= NULL;
1742
1743                 origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1744
1745                 if(faceFlags) drawSmooth = (faceFlags[origIndex*2] & ME_SMOOTH);
1746                 else drawSmooth = 1;
1747
1748                 if(mcol) {
1749                         cp= (unsigned char*)mcol;
1750                         mcol += gridFaces*gridFaces*numVerts*4;
1751                 }
1752
1753                 if (index!=-1) {
1754                         int draw;
1755                         draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, index, &drawSmooth);
1756                         
1757                         if (draw) {
1758                                 if (draw==2) {
1759                                         glEnable(GL_POLYGON_STIPPLE);
1760                                         glPolygonStipple(stipple_quarttone);
1761                                 }
1762                                 
1763                                 for (S=0; S<numVerts; S++) {
1764                                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1765                                         if (drawSmooth) {
1766                                                 glShadeModel(GL_SMOOTH);
1767                                                 for (y=0; y<gridFaces; y++) {
1768                                                         DMGridData *a, *b;
1769                                                         glBegin(GL_QUAD_STRIP);
1770                                                         for (x=0; x<gridFaces; x++) {
1771                                                                 a = &faceGridData[(y+0)*gridSize + x];
1772                                                                 b = &faceGridData[(y+1)*gridSize + x];
1773         
1774                                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1775                                                                 glNormal3fv(a->no);
1776                                                                 glVertex3fv(a->co);
1777                                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1778                                                                 glNormal3fv(b->no);
1779                                                                 glVertex3fv(b->co);
1780
1781                                                                 if(x != gridFaces-1) {
1782                                                                         if(cp) cp += 16;
1783                                                                 }
1784                                                         }
1785
1786                                                         a = &faceGridData[(y+0)*gridSize + x];
1787                                                         b = &faceGridData[(y+1)*gridSize + x];
1788
1789                                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1790                                                         glNormal3fv(a->no);
1791                                                         glVertex3fv(a->co);
1792                                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1793                                                         glNormal3fv(b->no);
1794                                                         glVertex3fv(b->co);
1795
1796                                                         if(cp) cp += 16;
1797
1798                                                         glEnd();
1799                                                 }
1800                                         } else {
1801                                                 glShadeModel(GL_FLAT);
1802                                                 glBegin(GL_QUADS);
1803                                                 for (y=0; y<gridFaces; y++) {
1804                                                         for (x=0; x<gridFaces; x++) {
1805                                                                 float *a = faceGridData[(y+0)*gridSize + x].co;
1806                                                                 float *b = faceGridData[(y+0)*gridSize + x + 1].co;
1807                                                                 float *c = faceGridData[(y+1)*gridSize + x + 1].co;
1808                                                                 float *d = faceGridData[(y+1)*gridSize + x].co;
1809
1810                                                                 ccgDM_glNormalFast(a, b, c, d);
1811         
1812                                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1813                                                                 glVertex3fv(d);
1814                                                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1815                                                                 glVertex3fv(c);
1816                                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1817                                                                 glVertex3fv(b);
1818                                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1819                                                                 glVertex3fv(a);
1820
1821                                                                 if(cp) cp += 16;
1822                                                         }
1823                                                 }
1824                                                 glEnd();
1825                                         }
1826                                 }
1827                                 if (draw==2)
1828                                         glDisable(GL_POLYGON_STIPPLE);
1829                         }
1830                 }
1831         }
1832 }
1833 static void ccgDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) {
1834         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1835         CCGSubSurf *ss = ccgdm->ss;
1836         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1837         int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss);
1838
1839         ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
1840
1841         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1842                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1843                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1844                 int index = ccgDM_getEdgeMapIndex(ss, e);
1845
1846                 glBegin(GL_LINE_STRIP);
1847                 if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) {
1848                         if (useAging && !(G.f&G_BACKBUFSEL)) {
1849                                 int ageCol = 255-ccgSubSurf_getEdgeAge(ss, e)*4;
1850                                 glColor3ub(0, ageCol>0?ageCol:0, 0);
1851                         }
1852
1853                         for (i=0; i<edgeSize-1; i++) {
1854                                 glVertex3fv(edgeData[i].co);
1855                                 glVertex3fv(edgeData[i+1].co);
1856                         }
1857                 }
1858                 glEnd();
1859         }
1860
1861         ccgEdgeIterator_free(ei);
1862 }
1863 static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void (*setDrawInterpOptions)(void *userData, int index, float t), void *userData) {
1864         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1865         CCGSubSurf *ss = ccgdm->ss;
1866         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1867         int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss);
1868
1869         ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
1870
1871         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1872                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1873                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1874                 int index = ccgDM_getEdgeMapIndex(ss, e);
1875
1876                 glBegin(GL_LINE_STRIP);
1877                 if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) {
1878                         for (i=0; i<edgeSize; i++) {
1879                                 setDrawInterpOptions(userData, index, (float) i/(edgeSize-1));
1880
1881                                 if (useAging && !(G.f&G_BACKBUFSEL)) {
1882                                         int ageCol = 255-ccgSubSurf_getEdgeAge(ss, e)*4;
1883                                         glColor3ub(0, ageCol>0?ageCol:0, 0);
1884                                 }
1885
1886                                 glVertex3fv(edgeData[i].co);
1887                         }
1888                 }
1889                 glEnd();
1890         }
1891
1892         ccgEdgeIterator_free(ei);
1893 }
1894 static void ccgDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData) {
1895         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1896         CCGSubSurf *ss = ccgdm->ss;
1897         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1898
1899         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1900                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1901                 int index = ccgDM_getFaceMapIndex(ss, f);
1902
1903                 if (index!=-1) {
1904                                 /* Face center data normal isn't updated atm. */
1905                         DMGridData *vd = ccgSubSurf_getFaceGridData(ss, f, 0, 0, 0);
1906
1907                         func(userData, index, vd->co, vd->no);
1908                 }
1909         }
1910
1911         ccgFaceIterator_free(fi);
1912 }
1913
1914 static void ccgDM_release(DerivedMesh *dm) {
1915         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1916
1917         if (DM_release(dm)) {
1918                 /* Before freeing, need to update the displacement map */
1919                 if(ccgdm->multires.modified) {
1920                         /* Check that mmd still exists */
1921                         if(!ccgdm->multires.local_mmd && BLI_findindex(&ccgdm->multires.ob->modifiers, ccgdm->multires.mmd) < 0)
1922                                 ccgdm->multires.mmd = NULL;
1923                         if(ccgdm->multires.mmd)
1924                                 ccgdm->multires.update(dm);
1925                 }
1926
1927                 if(ccgdm->pbvh) BLI_pbvh_free(ccgdm->pbvh);
1928                 if(ccgdm->gridFaces) MEM_freeN(ccgdm->gridFaces);
1929                 if(ccgdm->gridData) MEM_freeN(ccgdm->gridData);
1930                 if(ccgdm->gridAdjacency) MEM_freeN(ccgdm->gridAdjacency);
1931                 if(ccgdm->gridOffset) MEM_freeN(ccgdm->gridOffset);
1932                 if(ccgdm->freeSS) ccgSubSurf_free(ccgdm->ss);
1933                 MEM_freeN(ccgdm->edgeFlags);
1934                 MEM_freeN(ccgdm->faceFlags);
1935                 MEM_freeN(ccgdm->vertMap);
1936                 MEM_freeN(ccgdm->edgeMap);
1937                 MEM_freeN(ccgdm->faceMap);
1938                 MEM_freeN(ccgdm);
1939         }
1940 }
1941
1942 static void *ccgDM_get_vert_data_layer(DerivedMesh *dm, int type)
1943 {
1944         if(type == CD_ORIGINDEX) {
1945                 /* create origindex on demand to save memory */
1946                 CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
1947                 CCGSubSurf *ss= ccgdm->ss;
1948                 int *origindex;
1949                 int a, index, totnone, totorig;
1950
1951                 DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
1952                 origindex= DM_get_vert_data_layer(dm, CD_ORIGINDEX);
1953
1954                 totorig = ccgSubSurf_getNumVerts(ss);
1955                 totnone= dm->numVertData - totorig;
1956
1957                 /* original vertices are at the end */
1958                 for(a=0; a<totnone; a++)
1959                         origindex[a]= ORIGINDEX_NONE;
1960
1961                 for(index=0; index<totorig; index++, a++) {
1962                         CCGVert *v = ccgdm->vertMap[index].vert;
1963                         origindex[a] = ccgDM_getVertMapIndex(ccgdm->ss, v);
1964                 }
1965
1966                 return origindex;
1967         }
1968
1969         return DM_get_vert_data_layer(dm, type);
1970 }
1971
1972 static void *ccgDM_get_edge_data_layer(DerivedMesh *dm, int type)
1973 {
1974         if(type == CD_ORIGINDEX) {
1975                 /* create origindex on demand to save memory */
1976                 CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
1977                 CCGSubSurf *ss= ccgdm->ss;
1978                 int *origindex;
1979                 int a, i, index, totnone, totorig, totedge;
1980                 int edgeSize= ccgSubSurf_getEdgeSize(ss);
1981
1982                 DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
1983                 origindex= DM_get_edge_data_layer(dm, CD_ORIGINDEX);
1984
1985                 totedge= ccgSubSurf_getNumEdges(ss);
1986                 totorig= totedge*(edgeSize - 1);
1987                 totnone= dm->numEdgeData - totorig;
1988
1989                 /* original edges are at the end */
1990                 for(a=0; a<totnone; a++)
1991                         origindex[a]= ORIGINDEX_NONE;
1992
1993                 for(index=0; index<totedge; index++) {
1994                         CCGEdge *e= ccgdm->edgeMap[index].edge;
1995                         int mapIndex= ccgDM_getEdgeMapIndex(ss, e);
1996
1997                         for(i = 0; i < edgeSize - 1; i++, a++)
1998                                 origindex[a]= mapIndex;
1999                 }
2000
2001                 return origindex;
2002         }
2003
2004         return DM_get_edge_data_layer(dm, type);
2005 }
2006
2007 static void *ccgDM_get_face_data_layer(DerivedMesh *dm, int type)
2008 {
2009         if(type == CD_ORIGINDEX) {
2010                 /* create origindex on demand to save memory */
2011                 CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2012                 CCGSubSurf *ss= ccgdm->ss;
2013                 int *origindex;
2014                 int a, i, index, totface;
2015                 int gridFaces = ccgSubSurf_getGridSize(ss) - 1;
2016
2017                 DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
2018                 origindex= DM_get_face_data_layer(dm, CD_ORIGINDEX);
2019
2020                 totface= ccgSubSurf_getNumFaces(ss);
2021
2022                 for(a=0, index=0; index<totface; index++) {
2023                         CCGFace *f = ccgdm->faceMap[index].face;
2024                         int numVerts = ccgSubSurf_getFaceNumVerts(f);
2025                         int mapIndex = ccgDM_getFaceMapIndex(ss, f);
2026
2027                         for(i=0; i<gridFaces*gridFaces*numVerts; i++, a++)
2028                                 origindex[a]= mapIndex;
2029                 }
2030
2031                 return origindex;
2032         }
2033
2034         return DM_get_face_data_layer(dm, type);
2035 }
2036
2037 static int ccgDM_getNumGrids(DerivedMesh *dm)
2038 {
2039         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2040         int index, numFaces, numGrids;
2041
2042         numFaces= ccgSubSurf_getNumFaces(ccgdm->ss);
2043         numGrids= 0;
2044
2045         for(index=0; index<numFaces; index++) {
2046                 CCGFace *f = ccgdm->faceMap[index].face;
2047                 numGrids += ccgSubSurf_getFaceNumVerts(f);
2048         }
2049
2050         return numGrids;
2051 }
2052
2053 static int ccgDM_getGridSize(DerivedMesh *dm)
2054 {
2055         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2056         return ccgSubSurf_getGridSize(ccgdm->ss);
2057 }
2058
2059 static int ccgdm_adjacent_grid(CCGSubSurf *ss, int *gridOffset, CCGFace *f, int S, int offset)
2060 {
2061         CCGFace *adjf;
2062         CCGEdge *e;
2063         int i, j= 0, numFaces, fIndex, numEdges= 0;
2064
2065         e = ccgSubSurf_getFaceEdge(ss, f, S);
2066         numFaces = ccgSubSurf_getEdgeNumFaces(e);
2067
2068         if(numFaces != 2)
2069                 return -1;
2070
2071         for(i = 0; i < numFaces; i++) {
2072                 adjf = ccgSubSurf_getEdgeFace(e, i);
2073
2074                 if(adjf != f) {
2075                         numEdges = ccgSubSurf_getFaceNumVerts(adjf);
2076                         for(j = 0; j < numEdges; j++)
2077                                 if(ccgSubSurf_getFaceEdge(ss, adjf, j) == e)
2078                                         break;
2079
2080                         if(j != numEdges)
2081                                 break;
2082                 }
2083         }
2084         
2085         fIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, adjf));
2086
2087         return gridOffset[fIndex] + (j + offset)%numEdges;
2088 }
2089
2090 static void ccgdm_create_grids(DerivedMesh *dm)
2091 {
2092         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2093         CCGSubSurf *ss= ccgdm->ss;
2094         DMGridData **gridData;
2095         DMGridAdjacency *gridAdjacency, *adj;
2096         CCGFace **gridFaces;
2097         int *gridOffset;
2098         int index, numFaces, numGrids, S, gIndex, gridSize;
2099
2100         if(ccgdm->gridData)
2101                 return;
2102         
2103         numGrids = ccgDM_getNumGrids(dm);
2104         numFaces = ccgSubSurf_getNumFaces(ss);
2105         gridSize = ccgDM_getGridSize(dm);
2106
2107         /* compute offset into grid array for each face */
2108         gridOffset = MEM_mallocN(sizeof(int)*numFaces, "ccgdm.gridOffset");
2109
2110         for(gIndex = 0, index = 0; index < numFaces; index++) {
2111                 CCGFace *f = ccgdm->faceMap[index].face;
2112                 int numVerts = ccgSubSurf_getFaceNumVerts(f);
2113
2114                 gridOffset[index] = gIndex;
2115                 gIndex += numVerts;
2116         }
2117
2118         /* compute grid data */
2119         gridData = MEM_mallocN(sizeof(DMGridData*)*numGrids, "ccgdm.gridData");
2120         gridAdjacency = MEM_mallocN(sizeof(DMGridAdjacency)*numGrids, "ccgdm.gridAdjacency");
2121         gridFaces = MEM_mallocN(sizeof(CCGFace*)*numGrids, "ccgdm.gridFaces");
2122
2123         for(gIndex = 0, index = 0; index < numFaces; index++) {
2124                 CCGFace *f = ccgdm->faceMap[index].face;
2125                 int numVerts = ccgSubSurf_getFaceNumVerts(f);
2126
2127                 for(S = 0; S < numVerts; S++, gIndex++) {
2128                         int prevS = (S - 1 + numVerts) % numVerts;
2129                         int nextS = (S + 1 + numVerts) % numVerts;
2130
2131                         gridData[gIndex] = ccgSubSurf_getFaceGridDataArray(ss, f, S);
2132                         gridFaces[gIndex] = f;
2133
2134                         adj = &gridAdjacency[gIndex];
2135
2136                         adj->index[0] = gIndex - S + nextS;
2137                         adj->rotation[0] = 3;
2138                         adj->index[1] = ccgdm_adjacent_grid(ss, gridOffset, f, prevS, 0);
2139                         adj->rotation[1] = 1;
2140                         adj->index[2] = ccgdm_adjacent_grid(ss, gridOffset, f, S, 1);
2141                         adj->rotation[2] = 3;
2142                         adj->index[3] = gIndex - S + prevS;
2143                         adj->rotation[3] = 1;
2144                 }
2145         }
2146
2147         ccgdm->gridData = gridData;
2148         ccgdm->gridFaces = gridFaces;
2149         ccgdm->gridAdjacency = gridAdjacency;
2150         ccgdm->gridOffset = gridOffset;
2151 }
2152
2153 static DMGridData **ccgDM_getGridData(DerivedMesh *dm)
2154 {
2155         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2156
2157         ccgdm_create_grids(dm);
2158         return ccgdm->gridData;
2159 }
2160
2161 static DMGridAdjacency *ccgDM_getGridAdjacency(DerivedMesh *dm)
2162 {
2163         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2164
2165         ccgdm_create_grids(dm);
2166         return ccgdm->gridAdjacency;
2167 }
2168
2169 static int *ccgDM_getGridOffset(DerivedMesh *dm)
2170 {
2171         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2172
2173         ccgdm_create_grids(dm);
2174         return ccgdm->gridOffset;
2175 }
2176
2177 static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm)
2178 {
2179         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2180         int gridSize, numGrids;
2181
2182         if(ccgdm->pbvh)
2183                 return ccgdm->pbvh;
2184
2185         if(ccgdm->multires.mmd) {
2186                 ccgdm_create_grids(dm);
2187
2188                 gridSize = ccgDM_getGridSize(dm);
2189                 numGrids = ccgDM_getNumGrids(dm);
2190
2191                 ccgdm->pbvh = BLI_pbvh_new();
2192                 BLI_pbvh_build_grids(ccgdm->pbvh, ccgdm->gridData, ccgdm->gridAdjacency,
2193                         numGrids, gridSize, (void**)ccgdm->gridFaces);
2194         }
2195         else if(ob->type == OB_MESH) {
2196                 Mesh *me= ob->data;
2197
2198                 ccgdm->pbvh = BLI_pbvh_new();
2199                 BLI_pbvh_build_mesh(ccgdm->pbvh, me->mface, me->mvert,
2200                                me->totface, me->totvert);
2201         }
2202
2203         return ccgdm->pbvh;
2204 }
2205
2206 static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
2207                                          int drawInteriorEdges,
2208                                          int useSubsurfUv,
2209                                          DerivedMesh *dm)
2210 {
2211         CCGDerivedMesh *ccgdm = MEM_callocN(sizeof(*ccgdm), "ccgdm");
2212         CCGVertIterator *vi;
2213         CCGEdgeIterator *ei;
2214         CCGFaceIterator *fi;
2215         int index, totvert, totedge, totface;
2216         int i;
2217         int vertNum, edgeNum, faceNum;
2218         short *edgeFlags;
2219         char *faceFlags;
2220         int edgeSize;
2221         int gridSize;
2222         int gridFaces;
2223         int gridSideVerts;
2224         int gridSideEdges;
2225         int gridInternalEdges;
2226         MEdge *medge = NULL;
2227         MFace *mface = NULL;
2228         FaceVertWeight *qweight, *tweight;
2229
2230         DM_from_template(&ccgdm->dm, dm, DM_TYPE_CCGDM,
2231                                          ccgSubSurf_getNumFinalVerts(ss),
2232                                          ccgSubSurf_getNumFinalEdges(ss),
2233                                          ccgSubSurf_getNumFinalFaces(ss));
2234
2235         ccgdm->dm.getMinMax = ccgDM_getMinMax;
2236         ccgdm->dm.getNumVerts = ccgDM_getNumVerts;
2237         ccgdm->dm.getNumFaces = ccgDM_getNumFaces;
2238
2239         ccgdm->dm.getNumEdges = ccgDM_getNumEdges;
2240         ccgdm->dm.getVert = ccgDM_getFinalVert;
2241         ccgdm->dm.getEdge = ccgDM_getFinalEdge;
2242         ccgdm->dm.getFace = ccgDM_getFinalFace;
2243         ccgdm->dm.copyVertArray = ccgDM_copyFinalVertArray;
2244         ccgdm->dm.copyEdgeArray = ccgDM_copyFinalEdgeArray;
2245         ccgdm->dm.copyFaceArray = ccgDM_copyFinalFaceArray;
2246         ccgdm->dm.getVertData = DM_get_vert_data;
2247         ccgdm->dm.getEdgeData = DM_get_edge_data;
2248         ccgdm->dm.getFaceData = DM_get_face_data;
2249         ccgdm->dm.getVertDataArray = ccgDM_get_vert_data_layer;
2250         ccgdm->dm.getEdgeDataArray = ccgDM_get_edge_data_layer;
2251         ccgdm->dm.getFaceDataArray = ccgDM_get_face_data_layer;
2252         ccgdm->dm.getNumGrids = ccgDM_getNumGrids;
2253         ccgdm->dm.getGridSize = ccgDM_getGridSize;
2254         ccgdm->dm.getGridData = ccgDM_getGridData;
2255         ccgdm->dm.getGridAdjacency = ccgDM_getGridAdjacency;
2256         ccgdm->dm.getGridOffset = ccgDM_getGridOffset;
2257         ccgdm->dm.getPBVH = ccgDM_getPBVH;
2258
2259         ccgdm->dm.getVertCos = ccgdm_getVertCos;
2260         ccgdm->dm.foreachMappedVert = ccgDM_foreachMappedVert;
2261         ccgdm->dm.foreachMappedEdge = ccgDM_foreachMappedEdge;
2262         ccgdm->dm.foreachMappedFaceCenter = ccgDM_foreachMappedFaceCenter;
2263         
2264         ccgdm->dm.drawVerts = ccgDM_drawVerts;
2265         ccgdm->dm.drawEdges = ccgDM_drawEdges;
2266         ccgdm->dm.drawLooseEdges = ccgDM_drawLooseEdges;
2267         ccgdm->dm.drawFacesSolid = ccgDM_drawFacesSolid;
2268         ccgdm->dm.drawFacesColored = ccgDM_drawFacesColored;
2269         ccgdm->dm.drawFacesTex = ccgDM_drawFacesTex;
2270         ccgdm->dm.drawFacesGLSL = ccgDM_drawFacesGLSL;
2271         ccgdm->dm.drawMappedFaces = ccgDM_drawMappedFaces;
2272         ccgdm->dm.drawMappedFacesTex = ccgDM_drawMappedFacesTex;
2273         ccgdm->dm.drawMappedFacesGLSL = ccgDM_drawMappedFacesGLSL;
2274         ccgdm->dm.drawUVEdges = ccgDM_drawUVEdges;
2275
2276         ccgdm->dm.drawMappedEdgesInterp = ccgDM_drawMappedEdgesInterp;
2277         ccgdm->dm.drawMappedEdges = ccgDM_drawMappedEdges;
2278         
2279         ccgdm->dm.release = ccgDM_release;
2280         
2281         ccgdm->ss = ss;
2282         ccgdm->drawInteriorEdges = drawInteriorEdges;
2283         ccgdm->useSubsurfUv = useSubsurfUv;
2284
2285         totvert = ccgSubSurf_getNumVerts(ss);
2286         ccgdm->vertMap = MEM_mallocN(totvert * sizeof(*ccgdm->vertMap), "vertMap");
2287         vi = ccgSubSurf_getVertIterator(ss);
2288         for(; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
2289                 CCGVert *v = ccgVertIterator_getCurrent(vi);
2290
2291                 ccgdm->vertMap[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))].vert = v;
2292         }
2293         ccgVertIterator_free(vi);
2294
2295         totedge = ccgSubSurf_getNumEdges(ss);
2296         ccgdm->edgeMap = MEM_mallocN(totedge * sizeof(*ccgdm->edgeMap), "edgeMap");
2297         ei = ccgSubSurf_getEdgeIterator(ss);
2298         for(; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
2299                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
2300
2301                 ccgdm->edgeMap[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))].edge = e;
2302         }
2303
2304         totface = ccgSubSurf_getNumFaces(ss);
2305         ccgdm->faceMap = MEM_mallocN(totface * sizeof(*ccgdm->faceMap), "faceMap");
2306         fi = ccgSubSurf_getFaceIterator(ss);
2307         for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
2308                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
2309
2310                 ccgdm->faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f))].face = f;
2311         }
2312         ccgFaceIterator_free(fi);
2313
2314         edgeSize = ccgSubSurf_getEdgeSize(ss);
2315         gridSize = ccgSubSurf_getGridSize(ss);
2316         gridFaces = gridSize - 1;
2317         gridSideVerts = gridSize - 2;
2318         /*gridInternalVerts = gridSideVerts * gridSideVerts; - as yet, unused */
2319         gridSideEdges = gridSize - 1;
2320         gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2; 
2321
2322         calc_ss_weights(gridFaces, &qweight, &tweight);
2323
2324         vertNum = 0;
2325         edgeNum = 0;
2326         faceNum = 0;
2327
2328         /* mvert = dm->getVertArray(dm); - as yet unused */
2329         medge = dm->getEdgeArray(dm);
2330         mface = dm->getFaceArray(dm);
2331
2332         faceFlags = ccgdm->faceFlags = MEM_callocN(sizeof(char)*2*totface, "faceFlags");
2333
2334         for(index = 0; index < totface; ++index) {
2335                 CCGFace *f = ccgdm->faceMap[index].face;
2336                 int numVerts = ccgSubSurf_getFaceNumVerts(f);
2337                 int numFinalEdges = numVerts * (gridSideEdges + gridInternalEdges);
2338                 int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
2339                 FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight;
2340                 int S, x, y;
2341                 int vertIdx[4];
2342
2343                 ccgdm->faceMap[index].startVert = vertNum;
2344                 ccgdm->faceMap[index].startEdge = edgeNum;
2345                 ccgdm->faceMap[index].startFace = faceNum;
2346
2347                 /* set the face base vert */
2348                 *((int*)ccgSubSurf_getFaceUserData(ss, f)) = vertNum;
2349
2350                 for(S = 0; S < numVerts; S++) {
2351                         CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
2352
2353                         vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2354                 }
2355
2356                 DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, weight[0][0],
2357                                     numVerts, vertNum);
2358                 ++vertNum;
2359
2360                 for(S = 0; S < numVerts; S++) {
2361                         int prevS = (S - 1 + numVerts) % numVerts;
2362                         int nextS = (S + 1) % numVerts;
2363                         int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
2364                         for(x = 1; x < gridFaces; x++) {
2365                                 float w[4];
2366                                 w[prevS]  = weight[x][0][0];
2367                                 w[S]      = weight[x][0][1];
2368                                 w[nextS]  = weight[x][0][2];
2369                                 w[otherS] = weight[x][0][3];
2370                                 DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w,
2371                                                     numVerts, vertNum);
2372                                 ++vertNum;
2373                         }
2374                 }
2375
2376                 for(S = 0; S < numVerts; S++) {
2377                         int prevS = (S - 1 + numVerts) % numVerts;
2378                         int nextS = (S + 1) % numVerts;
2379                         int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
2380                         for(y = 1; y < gridFaces; y++) {
2381                                 for(x = 1; x < gridFaces; x++) {
2382                                         float w[4];
2383                                         w[prevS]  = weight[y * gridFaces + x][0][0];
2384                                         w[S]      = weight[y * gridFaces + x][0][1];
2385                                         w[nextS]  = weight[y * gridFaces + x][0][2];
2386                                         w[otherS] = weight[y * gridFaces + x][0][3];
2387                                         DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w,
2388                                                             numVerts, vertNum);
2389                                         ++vertNum;
2390                                 }
2391                         }
2392                 }
2393
2394                 for(S = 0; S < numVerts; S++) {
2395                         int prevS = (S - 1 + numVerts) % numVerts;
2396                         int nextS = (S + 1) % numVerts;
2397                         int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
2398
2399                         weight = (numVerts == 4) ? qweight : tweight;
2400
2401                         for(y = 0; y < gridFaces; y++) {
2402                                 for(x = 0; x < gridFaces; x++) {
2403                                         FaceVertWeight w;
2404                                         int j;
2405
2406                                         for(j = 0; j < 4; ++j) {
2407                                                 w[j][prevS]  = (*weight)[j][0];
2408                                                 w[j][S]      = (*weight)[j][1];
2409                                                 w[j][nextS]  = (*weight)[j][2];
2410                                                 w[j][otherS] = (*weight)[j][3];
2411                                         }
2412
2413                                         DM_interp_face_data(dm, &ccgdm->dm, &origIndex, NULL,
2414                                                             &w, 1, faceNum);
2415                                         weight++;
2416
2417                                         ++faceNum;
2418                                 }
2419                         }
2420                 }
2421
2422                 faceFlags[index*2] = mface[origIndex].flag;
2423                 faceFlags[index*2 + 1] = mface[origIndex].mat_nr;
2424
2425                 edgeNum += numFinalEdges;
2426         }
2427
2428         if(useSubsurfUv) {
2429                 CustomData *fdata = &ccgdm->dm.faceData;
2430                 CustomData *dmfdata = &dm->faceData;
2431                 int numlayer = CustomData_number_of_layers(fdata, CD_MTFACE);
2432                 int dmnumlayer = CustomData_number_of_layers(dmfdata, CD_MTFACE);
2433
2434                 for (i=0; i<numlayer && i<dmnumlayer; i++)
2435                         set_subsurf_uv(ss, dm, &ccgdm->dm, i);
2436         }
2437
2438         edgeFlags = ccgdm->edgeFlags = MEM_callocN(sizeof(short)*totedge, "edgeFlags");
2439
2440         for(index = 0; index < totedge; ++index) {
2441                 CCGEdge *e = ccgdm->edgeMap[index].edge;
2442                 int numFinalEdges = edgeSize - 1;
2443                 int x;
2444                 int vertIdx[2];
2445                 int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
2446
2447                 CCGVert *v;
2448                 v = ccgSubSurf_getEdgeVert0(e);
2449                 vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2450                 v = ccgSubSurf_getEdgeVert1(e);
2451                 vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2452
2453                 ccgdm->edgeMap[index].startVert = vertNum;
2454                 ccgdm->edgeMap[index].startEdge = edgeNum;
2455
2456                 /* set the edge base vert */
2457                 *((int*)ccgSubSurf_getEdgeUserData(ss, e)) = vertNum;
2458
2459                 for(x = 1; x < edgeSize - 1; x++) {
2460                         float w[2];
2461                         w[1] = (float) x / (edgeSize - 1);
2462                         w[0] = 1 - w[1];
2463                         DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w, 2, vertNum);
2464                         ++vertNum;
2465                 }
2466
2467                 edgeFlags[index]= medge[edgeIdx].flag;
2468
2469                 edgeNum += numFinalEdges;
2470         }
2471
2472         for(index = 0; index < totvert; ++index) {
2473                 CCGVert *v = ccgdm->vertMap[index].vert;
2474                 int vertIdx;
2475
2476                 vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2477
2478                 ccgdm->vertMap[index].startVert = vertNum;
2479
2480                 /* set the vert base vert */
2481                 *((int*) ccgSubSurf_getVertUserData(ss, v)) = vertNum;
2482
2483                 DM_copy_vert_data(dm, &ccgdm->dm, vertIdx, vertNum, 1);
2484
2485                 ++vertNum;
2486         }
2487
2488         MEM_freeN(qweight);
2489         MEM_freeN(tweight);
2490
2491         return ccgdm;
2492 }
2493
2494 /***/
2495
2496 struct DerivedMesh *subsurf_make_derived_from_derived(
2497                         struct DerivedMesh *dm,
2498                         struct SubsurfModifierData *smd,
2499                         int useRenderParams, float (*vertCos)[3],
2500                         int isFinalCalc, int editMode)
2501 {
2502         int useSimple = smd->subdivType == ME_SIMPLE_SUBSURF;
2503         int useAging = smd->flags & eSubsurfModifierFlag_DebugIncr;
2504         int useSubsurfUv = smd->flags & eSubsurfModifierFlag_SubsurfUv;
2505         int drawInteriorEdges = !(smd->flags & eSubsurfModifierFlag_ControlEdges);
2506         CCGDerivedMesh *result;
2507
2508         if(editMode) {
2509                 int levels= (smd->modifier.scene)? get_render_subsurf_level(&smd->modifier.scene->r, smd->levels): smd->levels;
2510
2511                 smd->emCache = _getSubSurf(smd->emCache, levels, useAging, 0,
2512                                            useSimple);
2513                 ss_sync_from_derivedmesh(smd->emCache, dm, vertCos, useSimple);
2514
2515                 result = getCCGDerivedMesh(smd->emCache,
2516                                            drawInteriorEdges,
2517                                        useSubsurfUv, dm);
2518         } else if(useRenderParams) {
2519                 /* Do not use cache in render mode. */
2520                 CCGSubSurf *ss;
2521                 int levels= (smd->modifier.scene)? get_render_subsurf_level(&smd->modifier.scene->r, smd->renderLevels): smd->renderLevels;
2522
2523                 if(levels == 0)
2524                         return dm;
2525                 
2526                 ss = _getSubSurf(NULL, levels, 0, 1, useSimple);
2527
2528                 ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
2529
2530                 result = getCCGDerivedMesh(ss,
2531                         drawInteriorEdges, useSubsurfUv, dm);
2532
2533                 result->freeSS = 1;
2534         } else {
2535                 int useIncremental = (smd->flags & eSubsurfModifierFlag_Incremental);
2536                 int useAging = smd->flags & eSubsurfModifierFlag_DebugIncr;
2537                 int levels= (smd->modifier.scene)? get_render_subsurf_level(&smd->modifier.scene->r, smd->levels): smd->levels;
2538                 CCGSubSurf *ss;
2539                 
2540                 /* It is quite possible there is a much better place to do this. It
2541                  * depends a bit on how rigourously we expect this function to never
2542                  * be called in editmode. In semi-theory we could share a single
2543                  * cache, but the handles used inside and outside editmode are not
2544                  * the same so we would need some way of converting them. Its probably
2545                  * not worth the effort. But then why am I even writing this long
2546                  * comment that no one will read? Hmmm. - zr
2547                  */
2548                 if(smd->emCache) {
2549                         ccgSubSurf_free(smd->emCache);
2550                         smd->emCache = NULL;
2551                 }
2552
2553                 if(useIncremental && isFinalCalc) {
2554                         smd->mCache = ss = _getSubSurf(smd->mCache, levels,
2555                                                        useAging, 0, useSimple);
2556
2557                         ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
2558
2559                         result = getCCGDerivedMesh(smd->mCache,
2560                                                drawInteriorEdges,
2561                                            useSubsurfUv, dm);
2562                 } else {
2563                         if (smd->mCache && isFinalCalc) {
2564                                 ccgSubSurf_free(smd->mCache);
2565                                 smd->mCache = NULL;
2566                         }
2567
2568                         ss = _getSubSurf(NULL, levels, 0, 1, useSimple);
2569                         ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
2570
2571                         result = getCCGDerivedMesh(ss, drawInteriorEdges, useSubsurfUv, dm);
2572
2573                         if(isFinalCalc)
2574                                 smd->mCache = ss;
2575                         else
2576                                 result->freeSS = 1;
2577                 }
2578         }
2579
2580         return (DerivedMesh*)result;
2581 }
2582
2583 void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]) 
2584 {
2585         /* Finds the subsurf limit positions for the verts in a mesh 
2586          * and puts them in an array of floats. Please note that the 
2587          * calculated vert positions is incorrect for the verts 
2588          * on the boundary of the mesh.
2589          */
2590         CCGSubSurf *ss = _getSubSurf(NULL, 1, 0, 1, 0);
2591         float edge_sum[3], face_sum[3];
2592         CCGVertIterator *vi;
2593         DerivedMesh *dm = CDDM_from_mesh(me, NULL);
2594
2595         ss_sync_from_derivedmesh(ss, dm, NULL, 0);
2596
2597         vi = ccgSubSurf_getVertIterator(ss);
2598         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
2599                 CCGVert *v = ccgVertIterator_getCurrent(vi);
2600                 int idx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2601                 int N = ccgSubSurf_getVertNumEdges(v);
2602                 int numFaces = ccgSubSurf_getVertNumFaces(v);
2603                 float *co;
2604                 int i;
2605                 
2606                 edge_sum[0]= edge_sum[1]= edge_sum[2]= 0.0;
2607                 face_sum[0]= face_sum[1]= face_sum[2]= 0.0;
2608
2609                 for (i=0; i<N; i++) {
2610                         CCGEdge *e = ccgSubSurf_getVertEdge(v, i);
2611                         add_v3_v3v3(edge_sum, edge_sum, ccgSubSurf_getEdgeData(ss, e, 1));
2612                 }
2613                 for (i=0; i<numFaces; i++) {
2614                         CCGFace *f = ccgSubSurf_getVertFace(v, i);
2615                         add_v3_v3v3(face_sum, face_sum, ccgSubSurf_getFaceCenterData(f));
2616                 }
2617
2618                 /* ad-hoc correction for boundary vertices, to at least avoid them
2619                    moving completely out of place (brecht) */
2620                 if(numFaces && numFaces != N)
2621                         mul_v3_fl(face_sum, (float)N/(float)numFaces);
2622
2623                 co = ccgSubSurf_getVertData(ss, v);
2624                 positions_r[idx][0] = (co[0]*N*N + edge_sum[0]*4 + face_sum[0])/(N*(N+5));
2625                 positions_r[idx][1] = (co[1]*N*N + edge_sum[1]*4 + face_sum[1])/(N*(N+5));
2626                 positions_r[idx][2] = (co[2]*N*N + edge_sum[2]*4 + face_sum[2])/(N*(N+5));
2627         }
2628         ccgVertIterator_free(vi);
2629
2630         ccgSubSurf_free(ss);
2631
2632         dm->release(dm);
2633 }
2634