dde9ccde6bbf0cfe772f430af23b88cba9def0e2
[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                         normal_float_to_short_v3(mvert[i].no, vd->no);
823                 }
824         }
825
826         totvert = ccgSubSurf_getNumVerts(ss);
827         for(index = 0; index < totvert; index++) {
828                 CCGVert *v = ccgdm->vertMap[index].vert;
829
830                 vd= ccgSubSurf_getVertData(ss, v);
831                 copy_v3_v3(mvert[i].co, vd->co);
832                 normal_float_to_short_v3(mvert[i].no, vd->no);
833                 i++;
834         }
835 }
836
837 static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
838 {
839         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
840         CCGSubSurf *ss = ccgdm->ss;
841         int index;
842         int totedge, totface;
843         int gridSize = ccgSubSurf_getGridSize(ss);
844         int edgeSize = ccgSubSurf_getEdgeSize(ss);
845         int i = 0;
846         short *edgeFlags = ccgdm->edgeFlags;
847
848         totface = ccgSubSurf_getNumFaces(ss);
849         for(index = 0; index < totface; index++) {
850                 CCGFace *f = ccgdm->faceMap[index].face;
851                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
852
853                 for(S = 0; S < numVerts; S++) {
854                         for(x = 0; x < gridSize - 1; x++) {
855                                 MEdge *med = &medge[i];
856
857                                 if(ccgdm->drawInteriorEdges)
858                                     med->flag = ME_EDGEDRAW | ME_EDGERENDER;
859                                 med->v1 = getFaceIndex(ss, f, S, x, 0, edgeSize, gridSize);
860                                 med->v2 = getFaceIndex(ss, f, S, x + 1, 0, edgeSize, gridSize);
861                                 i++;
862                         }
863
864                         for(x = 1; x < gridSize - 1; x++) {
865                                 for(y = 0; y < gridSize - 1; y++) {
866                                         MEdge *med;
867
868                                         med = &medge[i];
869                                         if(ccgdm->drawInteriorEdges)
870                                             med->flag = ME_EDGEDRAW | ME_EDGERENDER;
871                                         med->v1 = getFaceIndex(ss, f, S, x, y,
872                                                                edgeSize, gridSize);
873                                         med->v2 = getFaceIndex(ss, f, S, x, y + 1,
874                                                                edgeSize, gridSize);
875                                         i++;
876
877                                         med = &medge[i];
878                                         if(ccgdm->drawInteriorEdges)
879                                             med->flag = ME_EDGEDRAW | ME_EDGERENDER;
880                                         med->v1 = getFaceIndex(ss, f, S, y, x,
881                                                                edgeSize, gridSize);
882                                         med->v2 = getFaceIndex(ss, f, S, y + 1, x,
883                                                                edgeSize, gridSize);
884                                         i++;
885                                 }
886                         }
887                 }
888         }
889
890         totedge = ccgSubSurf_getNumEdges(ss);
891         for(index = 0; index < totedge; index++) {
892                 CCGEdge *e = ccgdm->edgeMap[index].edge;
893                 unsigned int flags = 0;
894                 int x;
895                 int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
896
897                 if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
898
899                 if(edgeFlags) {
900                         if(edgeIdx != -1) {
901                                 flags |= (edgeFlags[index] & (ME_SEAM | ME_SHARP))
902                                          | ME_EDGEDRAW | ME_EDGERENDER;
903                         }
904                 } else {
905                         flags |= ME_EDGEDRAW | ME_EDGERENDER;
906                 }
907
908                 for(x = 0; x < edgeSize - 1; x++) {
909                         MEdge *med = &medge[i];
910                         med->v1 = getEdgeIndex(ss, e, x, edgeSize);
911                         med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
912                         med->flag = flags;
913                         i++;
914                 }
915         }
916 }
917
918 static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface)
919 {
920         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
921         CCGSubSurf *ss = ccgdm->ss;
922         int index;
923         int totface;
924         int gridSize = ccgSubSurf_getGridSize(ss);
925         int edgeSize = ccgSubSurf_getEdgeSize(ss);
926         int i = 0;
927         char *faceFlags = ccgdm->faceFlags;
928
929         totface = ccgSubSurf_getNumFaces(ss);
930         for(index = 0; index < totface; index++) {
931                 CCGFace *f = ccgdm->faceMap[index].face;
932                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
933                 int flag = (faceFlags)? faceFlags[index*2]: ME_SMOOTH;
934                 int mat_nr = (faceFlags)? faceFlags[index*2+1]: 0;
935
936                 for(S = 0; S < numVerts; S++) {
937                         for(y = 0; y < gridSize - 1; y++) {
938                                 for(x = 0; x < gridSize - 1; x++) {
939                                         MFace *mf = &mface[i];
940                                         mf->v1 = getFaceIndex(ss, f, S, x + 0, y + 0,
941                                                               edgeSize, gridSize);
942                                         mf->v2 = getFaceIndex(ss, f, S, x + 0, y + 1,
943                                                               edgeSize, gridSize);
944                                         mf->v3 = getFaceIndex(ss, f, S, x + 1, y + 1,
945                                                               edgeSize, gridSize);
946                                         mf->v4 = getFaceIndex(ss, f, S, x + 1, y + 0,
947                                                               edgeSize, gridSize);
948                                         mf->mat_nr = mat_nr;
949                                         mf->flag = flag;
950
951                                         i++;
952                                 }
953                         }
954                 }
955         }
956 }
957
958 static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) {
959         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
960         CCGSubSurf *ss = ccgdm->ss;
961         int edgeSize = ccgSubSurf_getEdgeSize(ss);
962         int gridSize = ccgSubSurf_getGridSize(ss);
963         int i;
964         CCGVertIterator *vi;
965         CCGEdgeIterator *ei;
966         CCGFaceIterator *fi;
967         CCGFace **faceMap2;
968         CCGEdge **edgeMap2;
969         CCGVert **vertMap2;
970         int index, totvert, totedge, totface;
971         
972         totvert = ccgSubSurf_getNumVerts(ss);
973         vertMap2 = MEM_mallocN(totvert*sizeof(*vertMap2), "vertmap");
974         vi = ccgSubSurf_getVertIterator(ss);
975         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
976                 CCGVert *v = ccgVertIterator_getCurrent(vi);
977
978                 vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v;
979         }
980         ccgVertIterator_free(vi);
981
982         totedge = ccgSubSurf_getNumEdges(ss);
983         edgeMap2 = MEM_mallocN(totedge*sizeof(*edgeMap2), "edgemap");
984         ei = ccgSubSurf_getEdgeIterator(ss);
985         for (i=0; !ccgEdgeIterator_isStopped(ei); i++,ccgEdgeIterator_next(ei)) {
986                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
987
988                 edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e;
989         }
990
991         totface = ccgSubSurf_getNumFaces(ss);
992         faceMap2 = MEM_mallocN(totface*sizeof(*faceMap2), "facemap");
993         fi = ccgSubSurf_getFaceIterator(ss);
994         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
995                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
996
997                 faceMap2[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f))] = f;
998         }
999         ccgFaceIterator_free(fi);
1000
1001         i = 0;
1002         for (index=0; index<totface; index++) {
1003                 CCGFace *f = faceMap2[index];
1004                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
1005
1006                 copy_v3_v3(cos[i++], ccgSubSurf_getFaceCenterData(f));
1007                 
1008                 for (S=0; S<numVerts; S++) {
1009                         for (x=1; x<gridSize-1; x++) {
1010                                 copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
1011                         }
1012                 }
1013
1014                 for (S=0; S<numVerts; S++) {
1015                         for (y=1; y<gridSize-1; y++) {
1016                                 for (x=1; x<gridSize-1; x++) {
1017                                         copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridData(ss, f, S, x, y));
1018                                 }
1019                         }
1020                 }
1021         }
1022
1023         for (index=0; index<totedge; index++) {
1024                 CCGEdge *e= edgeMap2[index];
1025                 int x;
1026
1027                 for (x=1; x<edgeSize-1; x++) {
1028                         copy_v3_v3(cos[i++], ccgSubSurf_getEdgeData(ss, e, x));
1029                 }
1030         }
1031
1032         for (index=0; index<totvert; index++) {
1033                 CCGVert *v = vertMap2[index];
1034                 copy_v3_v3(cos[i++], ccgSubSurf_getVertData(ss, v));
1035         }
1036
1037         MEM_freeN(vertMap2);
1038         MEM_freeN(edgeMap2);
1039         MEM_freeN(faceMap2);
1040 }
1041 static void ccgDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData) {
1042         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1043         CCGVertIterator *vi = ccgSubSurf_getVertIterator(ccgdm->ss);
1044
1045         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
1046                 CCGVert *v = ccgVertIterator_getCurrent(vi);
1047                 DMGridData *vd = ccgSubSurf_getVertData(ccgdm->ss, v);
1048                 int index = ccgDM_getVertMapIndex(ccgdm->ss, v);
1049
1050                 if (index!=-1)
1051                         func(userData, index, vd->co, vd->no, NULL);
1052         }
1053
1054         ccgVertIterator_free(vi);
1055 }
1056 static void ccgDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData) {
1057         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1058         CCGSubSurf *ss = ccgdm->ss;
1059         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1060         int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
1061
1062         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1063                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1064                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1065                 int index = ccgDM_getEdgeMapIndex(ss, e);
1066
1067                 if (index!=-1) {
1068                         for (i=0; i<edgeSize-1; i++)
1069                                 func(userData, index, edgeData[i].co, edgeData[i+1].co);
1070                 }
1071         }
1072
1073         ccgEdgeIterator_free(ei);
1074 }
1075
1076 static void ccgDM_drawVerts(DerivedMesh *dm) {
1077         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1078         CCGSubSurf *ss = ccgdm->ss;
1079         int edgeSize = ccgSubSurf_getEdgeSize(ss);
1080         int gridSize = ccgSubSurf_getGridSize(ss);
1081         CCGVertIterator *vi;
1082         CCGEdgeIterator *ei;
1083         CCGFaceIterator *fi;
1084
1085         glBegin(GL_POINTS);
1086         vi = ccgSubSurf_getVertIterator(ss);
1087         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
1088                 CCGVert *v = ccgVertIterator_getCurrent(vi);
1089                 glVertex3fv(ccgSubSurf_getVertData(ss, v));
1090         }
1091         ccgVertIterator_free(vi);
1092
1093         ei = ccgSubSurf_getEdgeIterator(ss);
1094         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1095                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1096                 int x;
1097
1098                 for (x=1; x<edgeSize-1; x++)
1099                         glVertex3fv(ccgSubSurf_getEdgeData(ss, e, x));
1100         }
1101         ccgEdgeIterator_free(ei);
1102
1103         fi = ccgSubSurf_getFaceIterator(ss);
1104         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1105                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1106                 int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
1107
1108                 glVertex3fv(ccgSubSurf_getFaceCenterData(f));
1109                 for (S=0; S<numVerts; S++)
1110                         for (x=1; x<gridSize-1; x++)
1111                                 glVertex3fv(ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
1112                 for (S=0; S<numVerts; S++)
1113                         for (y=1; y<gridSize-1; y++)
1114                                 for (x=1; x<gridSize-1; x++)
1115                                         glVertex3fv(ccgSubSurf_getFaceGridData(ss, f, S, x, y));
1116         }
1117         ccgFaceIterator_free(fi);
1118         glEnd();
1119 }
1120 static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) {
1121         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1122         CCGSubSurf *ss = ccgdm->ss;
1123         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1124         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1125         int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
1126         int gridSize = ccgSubSurf_getGridSize(ss);
1127         int useAging;
1128
1129         ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
1130
1131         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1132                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1133                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1134
1135                 if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e))
1136                         continue;
1137
1138                 if (useAging && !(G.f&G_BACKBUFSEL)) {
1139                         int ageCol = 255-ccgSubSurf_getEdgeAge(ss, e)*4;
1140                         glColor3ub(0, ageCol>0?ageCol:0, 0);
1141                 }
1142
1143                 glBegin(GL_LINE_STRIP);
1144                 for (i=0; i<edgeSize-1; i++) {
1145                         glVertex3fv(edgeData[i].co);
1146                         glVertex3fv(edgeData[i+1].co);
1147                 }
1148                 glEnd();
1149         }
1150
1151         if (useAging && !(G.f&G_BACKBUFSEL)) {
1152                 glColor3ub(0, 0, 0);
1153         }
1154
1155         if (ccgdm->drawInteriorEdges) {
1156                 for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1157                         CCGFace *f = ccgFaceIterator_getCurrent(fi);
1158                         int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1159
1160                         for (S=0; S<numVerts; S++) {
1161                                 DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1162
1163                                 glBegin(GL_LINE_STRIP);
1164                                 for (x=0; x<gridSize; x++)
1165                                         glVertex3fv(faceGridData[x].co);
1166                                 glEnd();
1167                                 for (y=1; y<gridSize-1; y++) {
1168                                         glBegin(GL_LINE_STRIP);
1169                                         for (x=0; x<gridSize; x++)
1170                                                 glVertex3fv(faceGridData[y*gridSize + x].co);
1171                                         glEnd();
1172                                 }
1173                                 for (x=1; x<gridSize-1; x++) {
1174                                         glBegin(GL_LINE_STRIP);
1175                                         for (y=0; y<gridSize; y++)
1176                                                 glVertex3fv(faceGridData[y*gridSize + x].co);
1177                                         glEnd();
1178                                 }
1179                         }
1180                 }
1181         }
1182
1183         ccgFaceIterator_free(fi);
1184         ccgEdgeIterator_free(ei);
1185 }
1186 static void ccgDM_drawLooseEdges(DerivedMesh *dm) {
1187         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1188         CCGSubSurf *ss = ccgdm->ss;
1189         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1190         int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
1191
1192         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1193                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1194                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1195
1196                 if (!ccgSubSurf_getEdgeNumFaces(e)) {
1197                         glBegin(GL_LINE_STRIP);
1198                         for (i=0; i<edgeSize-1; i++) {
1199                                 glVertex3fv(edgeData[i].co);
1200                                 glVertex3fv(edgeData[i+1].co);
1201                         }
1202                         glEnd();
1203                 }
1204         }
1205
1206         ccgEdgeIterator_free(ei);
1207 }
1208
1209 static void ccgDM_glNormalFast(float *a, float *b, float *c, float *d)
1210 {
1211         float a_cX = c[0]-a[0], a_cY = c[1]-a[1], a_cZ = c[2]-a[2];
1212         float b_dX = d[0]-b[0], b_dY = d[1]-b[1], b_dZ = d[2]-b[2];
1213         float no[3];
1214
1215         no[0] = b_dY*a_cZ - b_dZ*a_cY;
1216         no[1] = b_dZ*a_cX - b_dX*a_cZ;
1217         no[2] = b_dX*a_cY - b_dY*a_cX;
1218
1219         /* don't normalize, GL_NORMALIZE is be enabled */
1220         glNormal3fv(no);
1221 }
1222
1223         /* Only used by non-editmesh types */
1224 static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes)[4], int fast, int (*setMaterial)(int, void *attribs)) {
1225         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1226         CCGSubSurf *ss = ccgdm->ss;
1227         CCGFaceIterator *fi;
1228         int gridSize = ccgSubSurf_getGridSize(ss);
1229         char *faceFlags = ccgdm->faceFlags;
1230         int step = (fast)? gridSize-1: 1;
1231
1232         if(ccgdm->pbvh && ccgdm->multires.mmd && !fast) {
1233                 CCGFace **faces;
1234                 int totface;
1235
1236                 BLI_pbvh_get_grid_updates(ccgdm->pbvh, 1, (void***)&faces, &totface);
1237                 if(totface) {
1238                         ccgSubSurf_updateFromFaces(ss, 0, faces, totface);
1239                         ccgSubSurf_updateNormals(ss, faces, totface);
1240                         MEM_freeN(faces);
1241                 }
1242
1243                 if(dm->numFaceData) {
1244                         /* should be per face */
1245                         if(!setMaterial(faceFlags[1]+1, NULL))
1246                                 return;
1247
1248                         glShadeModel((faceFlags[0] & ME_SMOOTH)? GL_SMOOTH: GL_FLAT);
1249                         BLI_pbvh_draw(ccgdm->pbvh, partial_redraw_planes, NULL);
1250                         glShadeModel(GL_FLAT);
1251                 }
1252
1253                 return;
1254         }
1255
1256         fi = ccgSubSurf_getFaceIterator(ss);
1257         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1258                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1259                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1260                 int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1261                 int drawSmooth, mat_nr;
1262
1263                 if(faceFlags) {
1264                         drawSmooth = (faceFlags[index*2] & ME_SMOOTH);
1265                         mat_nr= faceFlags[index*2 + 1];
1266                 }
1267                 else {
1268                         drawSmooth = 1;
1269                         mat_nr= 0;
1270                 }
1271                 
1272                 if (!setMaterial(mat_nr+1, NULL))
1273                         continue;
1274
1275                 glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT);
1276                 for (S=0; S<numVerts; S++) {
1277                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1278
1279                         if (drawSmooth) {
1280                                 for (y=0; y<gridSize-1; y+=step) {
1281                                         glBegin(GL_QUAD_STRIP);
1282                                         for (x=0; x<gridSize; x+=step) {
1283                                                 DMGridData *a = &faceGridData[(y+0)*gridSize + x];
1284                                                 DMGridData *b = &faceGridData[(y+step)*gridSize + x];
1285
1286                                                 glNormal3fv(a->no);
1287                                                 glVertex3fv(a->co);
1288                                                 glNormal3fv(b->no);
1289                                                 glVertex3fv(b->co);
1290                                         }
1291                                         glEnd();
1292                                 }
1293                         } else {
1294                                 glBegin(GL_QUADS);
1295                                 for (y=0; y<gridSize-1; y+=step) {
1296                                         for (x=0; x<gridSize-1; x+=step) {
1297                                                 float *a = faceGridData[(y+0)*gridSize + x].co;
1298                                                 float *b = faceGridData[(y+0)*gridSize + x + step].co;
1299                                                 float *c = faceGridData[(y+step)*gridSize + x + step].co;
1300                                                 float *d = faceGridData[(y+step)*gridSize + x].co;
1301
1302                                                 ccgDM_glNormalFast(a, b, c, d);
1303
1304                                                 glVertex3fv(d);
1305                                                 glVertex3fv(c);
1306                                                 glVertex3fv(b);
1307                                                 glVertex3fv(a);
1308                                         }
1309                                 }
1310                                 glEnd();
1311                         }
1312                 }
1313         }
1314
1315         ccgFaceIterator_free(fi);
1316 }
1317
1318         /* Only used by non-editmesh types */
1319 static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs), int (*setDrawOptions)(void *userData, int index), void *userData) {
1320         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1321         CCGSubSurf *ss = ccgdm->ss;
1322         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1323         GPUVertexAttribs gattribs;
1324         DMVertexAttribs attribs;
1325         MTFace *tf = dm->getFaceDataArray(dm, CD_MTFACE);
1326         int gridSize = ccgSubSurf_getGridSize(ss);
1327         int gridFaces = gridSize - 1;
1328         int edgeSize = ccgSubSurf_getEdgeSize(ss);
1329         int transp, orig_transp, new_transp;
1330         char *faceFlags = ccgdm->faceFlags;
1331         int a, b, i, doDraw, numVerts, matnr, new_matnr, totface;
1332
1333         doDraw = 0;
1334         numVerts = 0;
1335         matnr = -1;
1336         transp = GPU_get_material_blend_mode();
1337         orig_transp = transp;
1338
1339         memset(&attribs, 0, sizeof(attribs));
1340
1341 #define PASSATTRIB(dx, dy, vert) {                                                                                              \
1342         if(attribs.totorco) {                                                                                                           \
1343                 index = getFaceIndex(ss, f, S, x+dx, y+dy, edgeSize, gridSize);                 \
1344                 glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]);  \
1345         }                                                                                                                                                       \
1346         for(b = 0; b < attribs.tottface; b++) {                                                                         \
1347                 MTFace *tf = &attribs.tface[b].array[a];                                                                \
1348                 glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]);                   \
1349         }                                                                                                                                                       \
1350         for(b = 0; b < attribs.totmcol; b++) {                                                                          \
1351                 MCol *cp = &attribs.mcol[b].array[a*4 + vert];                                                  \
1352                 GLubyte col[4];                                                                                                                 \
1353                 col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;                             \
1354                 glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col);                                    \
1355         }                                                                                                                                                       \
1356         if(attribs.tottang) {                                                                                                           \
1357                 float *tang = attribs.tang.array[a*4 + vert];                                                   \
1358                 glVertexAttrib3fvARB(attribs.tang.glIndex, tang);                                               \
1359         }                                                                                                                                                       \
1360 }
1361
1362         totface = ccgSubSurf_getNumFaces(ss);
1363         for(a = 0, i = 0; i < totface; i++) {
1364                 CCGFace *f = ccgdm->faceMap[i].face;
1365                 int S, x, y, drawSmooth;
1366                 int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1367                 int origIndex = ccgDM_getFaceMapIndex(ss, f);
1368                 
1369                 numVerts = ccgSubSurf_getFaceNumVerts(f);
1370
1371                 if(faceFlags) {
1372                         drawSmooth = (faceFlags[index*2] & ME_SMOOTH);
1373                         new_matnr= faceFlags[index*2 + 1] + 1;
1374                 }
1375                 else {
1376                         drawSmooth = 1;
1377                         new_matnr= 1;
1378                 }
1379
1380                 if(new_matnr != matnr) {
1381                         doDraw = setMaterial(matnr = new_matnr, &gattribs);
1382                         if(doDraw)
1383                                 DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
1384                 }
1385
1386                 if(!doDraw || (setDrawOptions && !setDrawOptions(userData, origIndex))) {
1387                         a += gridFaces*gridFaces*numVerts;
1388                         continue;
1389                 }
1390
1391                 if(tf) {
1392                         new_transp = tf[i].transp;
1393
1394                         if(new_transp != transp) {
1395                                 if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID)
1396                                         GPU_set_material_blend_mode(orig_transp);
1397                                 else
1398                                         GPU_set_material_blend_mode(new_transp);
1399                                 transp = new_transp;
1400                         }
1401                 }
1402
1403                 glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT);
1404                 for (S=0; S<numVerts; S++) {
1405                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1406                         DMGridData *vda, *vdb;
1407
1408                         if (drawSmooth) {
1409                                 for (y=0; y<gridFaces; y++) {
1410                                         glBegin(GL_QUAD_STRIP);
1411                                         for (x=0; x<gridFaces; x++) {
1412                                                 vda = &faceGridData[(y+0)*gridSize + x];
1413                                                 vdb = &faceGridData[(y+1)*gridSize + x];
1414                                                 
1415                                                 PASSATTRIB(0, 0, 0);
1416                                                 glNormal3fv(vda->no);
1417                                                 glVertex3fv(vda->co);
1418
1419                                                 PASSATTRIB(0, 1, 1);
1420                                                 glNormal3fv(vdb->no);
1421                                                 glVertex3fv(vdb->co);
1422
1423                                                 if(x != gridFaces-1)
1424                                                         a++;
1425                                         }
1426
1427                                         vda = &faceGridData[(y+0)*gridSize + x];
1428                                         vdb = &faceGridData[(y+1)*gridSize + x];
1429
1430                                         PASSATTRIB(0, 0, 3);
1431                                         glNormal3fv(vda->no);
1432                                         glVertex3fv(vda->co);
1433
1434                                         PASSATTRIB(0, 1, 2);
1435                                         glNormal3fv(vdb->no);
1436                                         glVertex3fv(vdb->co);
1437
1438                                         glEnd();
1439
1440                                         a++;
1441                                 }
1442                         } else {
1443                                 glBegin(GL_QUADS);
1444                                 for (y=0; y<gridFaces; y++) {
1445                                         for (x=0; x<gridFaces; x++) {
1446                                                 float *aco = faceGridData[(y+0)*gridSize + x].co;
1447                                                 float *bco = faceGridData[(y+0)*gridSize + x + 1].co;
1448                                                 float *cco = faceGridData[(y+1)*gridSize + x + 1].co;
1449                                                 float *dco = faceGridData[(y+1)*gridSize + x].co;
1450
1451                                                 ccgDM_glNormalFast(aco, bco, cco, dco);
1452
1453                                                 PASSATTRIB(0, 1, 1);
1454                                                 glVertex3fv(dco);
1455                                                 PASSATTRIB(1, 1, 2);
1456                                                 glVertex3fv(cco);
1457                                                 PASSATTRIB(1, 0, 3);
1458                                                 glVertex3fv(bco);
1459                                                 PASSATTRIB(0, 0, 0);
1460                                                 glVertex3fv(aco);
1461                                                 
1462                                                 a++;
1463                                         }
1464                                 }
1465                                 glEnd();
1466                         }
1467                 }
1468         }
1469
1470 #undef PASSATTRIB
1471
1472         ccgFaceIterator_free(fi);
1473 }
1474
1475 static void ccgDM_drawFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) {
1476         dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL);
1477 }
1478
1479 static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2) {
1480         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1481         CCGSubSurf *ss = ccgdm->ss;
1482         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1483         int gridSize = ccgSubSurf_getGridSize(ss);
1484         unsigned char *cp1, *cp2;
1485         int useTwoSide=1;
1486
1487         cp1= col1;
1488         if(col2) {
1489                 cp2= col2;
1490         } else {
1491                 cp2= NULL;
1492                 useTwoSide= 0;
1493         }
1494
1495         glShadeModel(GL_SMOOTH);
1496         if(col1 && col2)
1497                 glEnable(GL_CULL_FACE);
1498
1499         glBegin(GL_QUADS);
1500         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1501                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1502                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1503
1504                 for (S=0; S<numVerts; S++) {
1505                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1506                         for (y=0; y<gridSize-1; y++) {
1507                                 for (x=0; x<gridSize-1; x++) {
1508                                         float *a = faceGridData[(y+0)*gridSize + x].co;
1509                                         float *b = faceGridData[(y+0)*gridSize + x + 1].co;
1510                                         float *c = faceGridData[(y+1)*gridSize + x + 1].co;
1511                                         float *d = faceGridData[(y+1)*gridSize + x].co;
1512
1513                                         glColor3ub(cp1[3], cp1[2], cp1[1]);
1514                                         glVertex3fv(d);
1515                                         glColor3ub(cp1[7], cp1[6], cp1[5]);
1516                                         glVertex3fv(c);
1517                                         glColor3ub(cp1[11], cp1[10], cp1[9]);
1518                                         glVertex3fv(b);
1519                                         glColor3ub(cp1[15], cp1[14], cp1[13]);
1520                                         glVertex3fv(a);
1521
1522                                         if (useTwoSide) {
1523                                                 glColor3ub(cp2[15], cp2[14], cp2[13]);
1524                                                 glVertex3fv(a);
1525                                                 glColor3ub(cp2[11], cp2[10], cp2[9]);
1526                                                 glVertex3fv(b);
1527                                                 glColor3ub(cp2[7], cp2[6], cp2[5]);
1528                                                 glVertex3fv(c);
1529                                                 glColor3ub(cp2[3], cp2[2], cp2[1]);
1530                                                 glVertex3fv(d);
1531                                         }
1532
1533                                         if (cp2) cp2+=16;
1534                                         cp1+=16;
1535                                 }
1536                         }
1537                 }
1538         }
1539         glEnd();
1540
1541         ccgFaceIterator_free(fi);
1542 }
1543
1544 static void ccgDM_drawFacesTex_common(DerivedMesh *dm,
1545         int (*drawParams)(MTFace *tface, MCol *mcol, int matnr),
1546         int (*drawParamsMapped)(void *userData, int index),
1547         void *userData) 
1548 {
1549         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1550         CCGSubSurf *ss = ccgdm->ss;
1551         MCol *mcol = dm->getFaceDataArray(dm, CD_WEIGHT_MCOL);
1552         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
1553         char *faceFlags = ccgdm->faceFlags;
1554         int i, totface, flag, gridSize = ccgSubSurf_getGridSize(ss);
1555         int gridFaces = gridSize - 1;
1556
1557         if(!mcol)
1558                 mcol = dm->getFaceDataArray(dm, CD_MCOL);
1559
1560         totface = ccgSubSurf_getNumFaces(ss);
1561         for(i = 0; i < totface; i++) {
1562                 CCGFace *f = ccgdm->faceMap[i].face;
1563                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1564                 int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
1565                 int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1566                 unsigned char *cp= NULL;
1567                 int mat_nr;
1568
1569                 if(faceFlags) {
1570                         drawSmooth = (faceFlags[origIndex*2] & ME_SMOOTH);
1571                         mat_nr= faceFlags[origIndex*2 + 1];
1572                 }
1573                 else {
1574                         drawSmooth = 1;
1575                         mat_nr= 0;
1576                 }
1577
1578                 if(drawParams)
1579                         flag = drawParams(tf, mcol, mat_nr);
1580                 else
1581                         flag= (drawParamsMapped)? drawParamsMapped(userData, index): 1;
1582                 
1583                 if (flag == 0) { /* flag 0 == the face is hidden or invisible */
1584                         if(tf) tf += gridFaces*gridFaces*numVerts;
1585                         if(mcol) mcol += gridFaces*gridFaces*numVerts*4;
1586                         continue;
1587                 }
1588
1589                 /* flag 1 == use vertex colors */
1590                 if(mcol) {
1591                         if(flag==1) cp= (unsigned char*)mcol;
1592                         mcol += gridFaces*gridFaces*numVerts*4;
1593                 }
1594
1595                 for (S=0; S<numVerts; S++) {
1596                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1597                         DMGridData *a, *b;
1598
1599                         if (drawSmooth) {
1600                                 glShadeModel(GL_SMOOTH);
1601                                 for (y=0; y<gridFaces; y++) {
1602                                         glBegin(GL_QUAD_STRIP);
1603                                         for (x=0; x<gridFaces; x++) {
1604                                                 a = &faceGridData[(y+0)*gridSize + x];
1605                                                 b = &faceGridData[(y+1)*gridSize + x];
1606
1607                                                 if(tf) glTexCoord2fv(tf->uv[0]);
1608                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1609                                                 glNormal3fv(a->no);
1610                                                 glVertex3fv(a->co);
1611
1612                                                 if(tf) glTexCoord2fv(tf->uv[1]);
1613                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1614                                                 glNormal3fv(b->no);
1615                                                 glVertex3fv(b->co);
1616                                                 
1617                                                 if(x != gridFaces-1) {
1618                                                         if(tf) tf++;
1619                                                         if(cp) cp += 16;
1620                                                 }
1621                                         }
1622
1623                                         a = &faceGridData[(y+0)*gridSize + x];
1624                                         b = &faceGridData[(y+1)*gridSize + x];
1625
1626                                         if(tf) glTexCoord2fv(tf->uv[3]);
1627                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1628                                         glNormal3fv(a->no);
1629                                         glVertex3fv(a->co);
1630
1631                                         if(tf) glTexCoord2fv(tf->uv[2]);
1632                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1633                                         glNormal3fv(b->no);
1634                                         glVertex3fv(b->co);
1635
1636                                         if(tf) tf++;
1637                                         if(cp) cp += 16;
1638
1639                                         glEnd();
1640                                 }
1641                         } else {
1642                                 glShadeModel(GL_FLAT);
1643                                 glBegin(GL_QUADS);
1644                                 for (y=0; y<gridFaces; y++) {
1645                                         for (x=0; x<gridFaces; x++) {
1646                                                 float *a_co = faceGridData[(y+0)*gridSize + x].co;
1647                                                 float *b_co = faceGridData[(y+0)*gridSize + x + 1].co;
1648                                                 float *c_co = faceGridData[(y+1)*gridSize + x + 1].co;
1649                                                 float *d_co = faceGridData[(y+1)*gridSize + x].co;
1650
1651                                                 ccgDM_glNormalFast(a_co, b_co, c_co, d_co);
1652
1653                                                 if(tf) glTexCoord2fv(tf->uv[1]);
1654                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1655                                                 glVertex3fv(d_co);
1656
1657                                                 if(tf) glTexCoord2fv(tf->uv[2]);
1658                                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1659                                                 glVertex3fv(c_co);
1660
1661                                                 if(tf) glTexCoord2fv(tf->uv[3]);
1662                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1663                                                 glVertex3fv(b_co);
1664
1665                                                 if(tf) glTexCoord2fv(tf->uv[0]);
1666                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1667                                                 glVertex3fv(a_co);
1668
1669                                                 if(tf) tf++;
1670                                                 if(cp) cp += 16;
1671                                         }
1672                                 }
1673                                 glEnd();
1674                         }
1675                 }
1676         }
1677 }
1678
1679 static void ccgDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
1680 {
1681         ccgDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
1682 }
1683
1684 static void ccgDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
1685 {
1686         ccgDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
1687 }
1688
1689 static void ccgDM_drawUVEdges(DerivedMesh *dm)
1690 {
1691
1692         MFace *mf = dm->getFaceArray(dm);
1693         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
1694         int i;
1695         
1696         if (tf) {
1697                 glBegin(GL_LINES);
1698                 for(i = 0; i < dm->numFaceData; i++, mf++, tf++) {
1699                         if(!(mf->flag&ME_HIDE)) {
1700                                 glVertex2fv(tf->uv[0]);
1701                                 glVertex2fv(tf->uv[1]);
1702         
1703                                 glVertex2fv(tf->uv[1]);
1704                                 glVertex2fv(tf->uv[2]);
1705         
1706                                 if(!mf->v4) {
1707                                         glVertex2fv(tf->uv[2]);
1708                                         glVertex2fv(tf->uv[0]);
1709                                 } else {
1710                                         glVertex2fv(tf->uv[2]);
1711                                         glVertex2fv(tf->uv[3]);
1712         
1713                                         glVertex2fv(tf->uv[3]);
1714                                         glVertex2fv(tf->uv[0]);
1715                                 }
1716                         }
1717                 }
1718                 glEnd();
1719         }
1720 }
1721
1722 static void ccgDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors) {
1723         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1724         CCGSubSurf *ss = ccgdm->ss;
1725         MCol *mcol= NULL;
1726         int i, gridSize = ccgSubSurf_getGridSize(ss);
1727         char *faceFlags = ccgdm->faceFlags;
1728         int gridFaces = gridSize - 1, totface;
1729
1730         if(useColors) {
1731                 mcol = dm->getFaceDataArray(dm, CD_WEIGHT_MCOL);
1732                 if(!mcol)
1733                         mcol = dm->getFaceDataArray(dm, CD_MCOL);
1734         }
1735
1736         totface = ccgSubSurf_getNumFaces(ss);
1737         for(i = 0; i < totface; i++) {
1738                 CCGFace *f = ccgdm->faceMap[i].face;
1739                 int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
1740                 int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
1741                 int origIndex;
1742                 unsigned char *cp= NULL;
1743
1744                 origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
1745
1746                 if(faceFlags) drawSmooth = (faceFlags[origIndex*2] & ME_SMOOTH);
1747                 else drawSmooth = 1;
1748
1749                 if(mcol) {
1750                         cp= (unsigned char*)mcol;
1751                         mcol += gridFaces*gridFaces*numVerts*4;
1752                 }
1753
1754                 if (index!=-1) {
1755                         int draw;
1756                         draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, index, &drawSmooth);
1757                         
1758                         if (draw) {
1759                                 if (draw==2) {
1760                                         glEnable(GL_POLYGON_STIPPLE);
1761                                         glPolygonStipple(stipple_quarttone);
1762                                 }
1763                                 
1764                                 for (S=0; S<numVerts; S++) {
1765                                         DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
1766                                         if (drawSmooth) {
1767                                                 glShadeModel(GL_SMOOTH);
1768                                                 for (y=0; y<gridFaces; y++) {
1769                                                         DMGridData *a, *b;
1770                                                         glBegin(GL_QUAD_STRIP);
1771                                                         for (x=0; x<gridFaces; x++) {
1772                                                                 a = &faceGridData[(y+0)*gridSize + x];
1773                                                                 b = &faceGridData[(y+1)*gridSize + x];
1774         
1775                                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1776                                                                 glNormal3fv(a->no);
1777                                                                 glVertex3fv(a->co);
1778                                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1779                                                                 glNormal3fv(b->no);
1780                                                                 glVertex3fv(b->co);
1781
1782                                                                 if(x != gridFaces-1) {
1783                                                                         if(cp) cp += 16;
1784                                                                 }
1785                                                         }
1786
1787                                                         a = &faceGridData[(y+0)*gridSize + x];
1788                                                         b = &faceGridData[(y+1)*gridSize + x];
1789
1790                                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1791                                                         glNormal3fv(a->no);
1792                                                         glVertex3fv(a->co);
1793                                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1794                                                         glNormal3fv(b->no);
1795                                                         glVertex3fv(b->co);
1796
1797                                                         if(cp) cp += 16;
1798
1799                                                         glEnd();
1800                                                 }
1801                                         } else {
1802                                                 glShadeModel(GL_FLAT);
1803                                                 glBegin(GL_QUADS);
1804                                                 for (y=0; y<gridFaces; y++) {
1805                                                         for (x=0; x<gridFaces; x++) {
1806                                                                 float *a = faceGridData[(y+0)*gridSize + x].co;
1807                                                                 float *b = faceGridData[(y+0)*gridSize + x + 1].co;
1808                                                                 float *c = faceGridData[(y+1)*gridSize + x + 1].co;
1809                                                                 float *d = faceGridData[(y+1)*gridSize + x].co;
1810
1811                                                                 ccgDM_glNormalFast(a, b, c, d);
1812         
1813                                                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
1814                                                                 glVertex3fv(d);
1815                                                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
1816                                                                 glVertex3fv(c);
1817                                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
1818                                                                 glVertex3fv(b);
1819                                                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
1820                                                                 glVertex3fv(a);
1821
1822                                                                 if(cp) cp += 16;
1823                                                         }
1824                                                 }
1825                                                 glEnd();
1826                                         }
1827                                 }
1828                                 if (draw==2)
1829                                         glDisable(GL_POLYGON_STIPPLE);
1830                         }
1831                 }
1832         }
1833 }
1834 static void ccgDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) {
1835         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1836         CCGSubSurf *ss = ccgdm->ss;
1837         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1838         int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss);
1839
1840         ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
1841
1842         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1843                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1844                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1845                 int index = ccgDM_getEdgeMapIndex(ss, e);
1846
1847                 glBegin(GL_LINE_STRIP);
1848                 if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) {
1849                         if (useAging && !(G.f&G_BACKBUFSEL)) {
1850                                 int ageCol = 255-ccgSubSurf_getEdgeAge(ss, e)*4;
1851                                 glColor3ub(0, ageCol>0?ageCol:0, 0);
1852                         }
1853
1854                         for (i=0; i<edgeSize-1; i++) {
1855                                 glVertex3fv(edgeData[i].co);
1856                                 glVertex3fv(edgeData[i+1].co);
1857                         }
1858                 }
1859                 glEnd();
1860         }
1861
1862         ccgEdgeIterator_free(ei);
1863 }
1864 static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void (*setDrawInterpOptions)(void *userData, int index, float t), void *userData) {
1865         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1866         CCGSubSurf *ss = ccgdm->ss;
1867         CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
1868         int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss);
1869
1870         ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
1871
1872         for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
1873                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
1874                 DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
1875                 int index = ccgDM_getEdgeMapIndex(ss, e);
1876
1877                 glBegin(GL_LINE_STRIP);
1878                 if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) {
1879                         for (i=0; i<edgeSize; i++) {
1880                                 setDrawInterpOptions(userData, index, (float) i/(edgeSize-1));
1881
1882                                 if (useAging && !(G.f&G_BACKBUFSEL)) {
1883                                         int ageCol = 255-ccgSubSurf_getEdgeAge(ss, e)*4;
1884                                         glColor3ub(0, ageCol>0?ageCol:0, 0);
1885                                 }
1886
1887                                 glVertex3fv(edgeData[i].co);
1888                         }
1889                 }
1890                 glEnd();
1891         }
1892
1893         ccgEdgeIterator_free(ei);
1894 }
1895 static void ccgDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData) {
1896         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1897         CCGSubSurf *ss = ccgdm->ss;
1898         CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
1899
1900         for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
1901                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
1902                 int index = ccgDM_getFaceMapIndex(ss, f);
1903
1904                 if (index!=-1) {
1905                                 /* Face center data normal isn't updated atm. */
1906                         DMGridData *vd = ccgSubSurf_getFaceGridData(ss, f, 0, 0, 0);
1907
1908                         func(userData, index, vd->co, vd->no);
1909                 }
1910         }
1911
1912         ccgFaceIterator_free(fi);
1913 }
1914
1915 static void ccgDM_release(DerivedMesh *dm) {
1916         CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
1917
1918         if (DM_release(dm)) {
1919                 /* Before freeing, need to update the displacement map */
1920                 if(ccgdm->multires.modified) {
1921                         /* Check that mmd still exists */
1922                         if(!ccgdm->multires.local_mmd && BLI_findindex(&ccgdm->multires.ob->modifiers, ccgdm->multires.mmd) < 0)
1923                                 ccgdm->multires.mmd = NULL;
1924                         if(ccgdm->multires.mmd)
1925                                 ccgdm->multires.update(dm);
1926                 }
1927
1928                 if(ccgdm->pbvh) BLI_pbvh_free(ccgdm->pbvh);
1929                 if(ccgdm->gridFaces) MEM_freeN(ccgdm->gridFaces);
1930                 if(ccgdm->gridData) MEM_freeN(ccgdm->gridData);
1931                 if(ccgdm->gridAdjacency) MEM_freeN(ccgdm->gridAdjacency);
1932                 if(ccgdm->gridOffset) MEM_freeN(ccgdm->gridOffset);
1933                 if(ccgdm->freeSS) ccgSubSurf_free(ccgdm->ss);
1934                 MEM_freeN(ccgdm->edgeFlags);
1935                 MEM_freeN(ccgdm->faceFlags);
1936                 MEM_freeN(ccgdm->vertMap);
1937                 MEM_freeN(ccgdm->edgeMap);
1938                 MEM_freeN(ccgdm->faceMap);
1939                 MEM_freeN(ccgdm);
1940         }
1941 }
1942
1943 static void *ccgDM_get_vert_data_layer(DerivedMesh *dm, int type)
1944 {
1945         if(type == CD_ORIGINDEX) {
1946                 /* create origindex on demand to save memory */
1947                 CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
1948                 CCGSubSurf *ss= ccgdm->ss;
1949                 int *origindex;
1950                 int a, index, totnone, totorig;
1951
1952                 DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
1953                 origindex= DM_get_vert_data_layer(dm, CD_ORIGINDEX);
1954
1955                 totorig = ccgSubSurf_getNumVerts(ss);
1956                 totnone= dm->numVertData - totorig;
1957
1958                 /* original vertices are at the end */
1959                 for(a=0; a<totnone; a++)
1960                         origindex[a]= ORIGINDEX_NONE;
1961
1962                 for(index=0; index<totorig; index++, a++) {
1963                         CCGVert *v = ccgdm->vertMap[index].vert;
1964                         origindex[a] = ccgDM_getVertMapIndex(ccgdm->ss, v);
1965                 }
1966
1967                 return origindex;
1968         }
1969
1970         return DM_get_vert_data_layer(dm, type);
1971 }
1972
1973 static void *ccgDM_get_edge_data_layer(DerivedMesh *dm, int type)
1974 {
1975         if(type == CD_ORIGINDEX) {
1976                 /* create origindex on demand to save memory */
1977                 CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
1978                 CCGSubSurf *ss= ccgdm->ss;
1979                 int *origindex;
1980                 int a, i, index, totnone, totorig, totedge;
1981                 int edgeSize= ccgSubSurf_getEdgeSize(ss);
1982
1983                 DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
1984                 origindex= DM_get_edge_data_layer(dm, CD_ORIGINDEX);
1985
1986                 totedge= ccgSubSurf_getNumEdges(ss);
1987                 totorig= totedge*(edgeSize - 1);
1988                 totnone= dm->numEdgeData - totorig;
1989
1990                 /* original edges are at the end */
1991                 for(a=0; a<totnone; a++)
1992                         origindex[a]= ORIGINDEX_NONE;
1993
1994                 for(index=0; index<totedge; index++) {
1995                         CCGEdge *e= ccgdm->edgeMap[index].edge;
1996                         int mapIndex= ccgDM_getEdgeMapIndex(ss, e);
1997
1998                         for(i = 0; i < edgeSize - 1; i++, a++)
1999                                 origindex[a]= mapIndex;
2000                 }
2001
2002                 return origindex;
2003         }
2004
2005         return DM_get_edge_data_layer(dm, type);
2006 }
2007
2008 static void *ccgDM_get_face_data_layer(DerivedMesh *dm, int type)
2009 {
2010         if(type == CD_ORIGINDEX) {
2011                 /* create origindex on demand to save memory */
2012                 CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2013                 CCGSubSurf *ss= ccgdm->ss;
2014                 int *origindex;
2015                 int a, i, index, totface;
2016                 int gridFaces = ccgSubSurf_getGridSize(ss) - 1;
2017
2018                 DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
2019                 origindex= DM_get_face_data_layer(dm, CD_ORIGINDEX);
2020
2021                 totface= ccgSubSurf_getNumFaces(ss);
2022
2023                 for(a=0, index=0; index<totface; index++) {
2024                         CCGFace *f = ccgdm->faceMap[index].face;
2025                         int numVerts = ccgSubSurf_getFaceNumVerts(f);
2026                         int mapIndex = ccgDM_getFaceMapIndex(ss, f);
2027
2028                         for(i=0; i<gridFaces*gridFaces*numVerts; i++, a++)
2029                                 origindex[a]= mapIndex;
2030                 }
2031
2032                 return origindex;
2033         }
2034
2035         return DM_get_face_data_layer(dm, type);
2036 }
2037
2038 static int ccgDM_getNumGrids(DerivedMesh *dm)
2039 {
2040         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2041         int index, numFaces, numGrids;
2042
2043         numFaces= ccgSubSurf_getNumFaces(ccgdm->ss);
2044         numGrids= 0;
2045
2046         for(index=0; index<numFaces; index++) {
2047                 CCGFace *f = ccgdm->faceMap[index].face;
2048                 numGrids += ccgSubSurf_getFaceNumVerts(f);
2049         }
2050
2051         return numGrids;
2052 }
2053
2054 static int ccgDM_getGridSize(DerivedMesh *dm)
2055 {
2056         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2057         return ccgSubSurf_getGridSize(ccgdm->ss);
2058 }
2059
2060 static int ccgdm_adjacent_grid(CCGSubSurf *ss, int *gridOffset, CCGFace *f, int S, int offset)
2061 {
2062         CCGFace *adjf;
2063         CCGEdge *e;
2064         int i, j= 0, numFaces, fIndex, numEdges= 0;
2065
2066         e = ccgSubSurf_getFaceEdge(ss, f, S);
2067         numFaces = ccgSubSurf_getEdgeNumFaces(e);
2068
2069         if(numFaces != 2)
2070                 return -1;
2071
2072         for(i = 0; i < numFaces; i++) {
2073                 adjf = ccgSubSurf_getEdgeFace(e, i);
2074
2075                 if(adjf != f) {
2076                         numEdges = ccgSubSurf_getFaceNumVerts(adjf);
2077                         for(j = 0; j < numEdges; j++)
2078                                 if(ccgSubSurf_getFaceEdge(ss, adjf, j) == e)
2079                                         break;
2080
2081                         if(j != numEdges)
2082                                 break;
2083                 }
2084         }
2085         
2086         fIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, adjf));
2087
2088         return gridOffset[fIndex] + (j + offset)%numEdges;
2089 }
2090
2091 static void ccgdm_create_grids(DerivedMesh *dm)
2092 {
2093         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2094         CCGSubSurf *ss= ccgdm->ss;
2095         DMGridData **gridData;
2096         DMGridAdjacency *gridAdjacency, *adj;
2097         CCGFace **gridFaces;
2098         int *gridOffset;
2099         int index, numFaces, numGrids, S, gIndex, gridSize;
2100
2101         if(ccgdm->gridData)
2102                 return;
2103         
2104         numGrids = ccgDM_getNumGrids(dm);
2105         numFaces = ccgSubSurf_getNumFaces(ss);
2106         gridSize = ccgDM_getGridSize(dm);
2107
2108         /* compute offset into grid array for each face */
2109         gridOffset = MEM_mallocN(sizeof(int)*numFaces, "ccgdm.gridOffset");
2110
2111         for(gIndex = 0, index = 0; index < numFaces; index++) {
2112                 CCGFace *f = ccgdm->faceMap[index].face;
2113                 int numVerts = ccgSubSurf_getFaceNumVerts(f);
2114
2115                 gridOffset[index] = gIndex;
2116                 gIndex += numVerts;
2117         }
2118
2119         /* compute grid data */
2120         gridData = MEM_mallocN(sizeof(DMGridData*)*numGrids, "ccgdm.gridData");
2121         gridAdjacency = MEM_mallocN(sizeof(DMGridAdjacency)*numGrids, "ccgdm.gridAdjacency");
2122         gridFaces = MEM_mallocN(sizeof(CCGFace*)*numGrids, "ccgdm.gridFaces");
2123
2124         for(gIndex = 0, index = 0; index < numFaces; index++) {
2125                 CCGFace *f = ccgdm->faceMap[index].face;
2126                 int numVerts = ccgSubSurf_getFaceNumVerts(f);
2127
2128                 for(S = 0; S < numVerts; S++, gIndex++) {
2129                         int prevS = (S - 1 + numVerts) % numVerts;
2130                         int nextS = (S + 1 + numVerts) % numVerts;
2131
2132                         gridData[gIndex] = ccgSubSurf_getFaceGridDataArray(ss, f, S);
2133                         gridFaces[gIndex] = f;
2134
2135                         adj = &gridAdjacency[gIndex];
2136
2137                         adj->index[0] = gIndex - S + nextS;
2138                         adj->rotation[0] = 3;
2139                         adj->index[1] = ccgdm_adjacent_grid(ss, gridOffset, f, prevS, 0);
2140                         adj->rotation[1] = 1;
2141                         adj->index[2] = ccgdm_adjacent_grid(ss, gridOffset, f, S, 1);
2142                         adj->rotation[2] = 3;
2143                         adj->index[3] = gIndex - S + prevS;
2144                         adj->rotation[3] = 1;
2145                 }
2146         }
2147
2148         ccgdm->gridData = gridData;
2149         ccgdm->gridFaces = gridFaces;
2150         ccgdm->gridAdjacency = gridAdjacency;
2151         ccgdm->gridOffset = gridOffset;
2152 }
2153
2154 static DMGridData **ccgDM_getGridData(DerivedMesh *dm)
2155 {
2156         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2157
2158         ccgdm_create_grids(dm);
2159         return ccgdm->gridData;
2160 }
2161
2162 static DMGridAdjacency *ccgDM_getGridAdjacency(DerivedMesh *dm)
2163 {
2164         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2165
2166         ccgdm_create_grids(dm);
2167         return ccgdm->gridAdjacency;
2168 }
2169
2170 static int *ccgDM_getGridOffset(DerivedMesh *dm)
2171 {
2172         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2173
2174         ccgdm_create_grids(dm);
2175         return ccgdm->gridOffset;
2176 }
2177
2178 static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm)
2179 {
2180         CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm;
2181         int gridSize, numGrids;
2182
2183         if(ccgdm->pbvh)
2184                 return ccgdm->pbvh;
2185
2186         if(ccgdm->multires.mmd) {
2187                 ccgdm_create_grids(dm);
2188
2189                 gridSize = ccgDM_getGridSize(dm);
2190                 numGrids = ccgDM_getNumGrids(dm);
2191
2192                 ccgdm->pbvh = BLI_pbvh_new();
2193                 BLI_pbvh_build_grids(ccgdm->pbvh, ccgdm->gridData, ccgdm->gridAdjacency,
2194                         numGrids, gridSize, (void**)ccgdm->gridFaces);
2195         }
2196         else if(ob->type == OB_MESH) {
2197                 Mesh *me= ob->data;
2198
2199                 ccgdm->pbvh = BLI_pbvh_new();
2200                 BLI_pbvh_build_mesh(ccgdm->pbvh, me->mface, me->mvert,
2201                                me->totface, me->totvert);
2202         }
2203
2204         return ccgdm->pbvh;
2205 }
2206
2207 static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
2208                                          int drawInteriorEdges,
2209                                          int useSubsurfUv,
2210                                          DerivedMesh *dm)
2211 {
2212         CCGDerivedMesh *ccgdm = MEM_callocN(sizeof(*ccgdm), "ccgdm");
2213         CCGVertIterator *vi;
2214         CCGEdgeIterator *ei;
2215         CCGFaceIterator *fi;
2216         int index, totvert, totedge, totface;
2217         int i;
2218         int vertNum, edgeNum, faceNum;
2219         short *edgeFlags;
2220         char *faceFlags;
2221         int edgeSize;
2222         int gridSize;
2223         int gridFaces;
2224         int gridSideVerts;
2225         int gridSideEdges;
2226         int gridInternalEdges;
2227         MEdge *medge = NULL;
2228         MFace *mface = NULL;
2229         FaceVertWeight *qweight, *tweight;
2230
2231         DM_from_template(&ccgdm->dm, dm, DM_TYPE_CCGDM,
2232                                          ccgSubSurf_getNumFinalVerts(ss),
2233                                          ccgSubSurf_getNumFinalEdges(ss),
2234                                          ccgSubSurf_getNumFinalFaces(ss));
2235
2236         ccgdm->dm.getMinMax = ccgDM_getMinMax;
2237         ccgdm->dm.getNumVerts = ccgDM_getNumVerts;
2238         ccgdm->dm.getNumFaces = ccgDM_getNumFaces;
2239
2240         ccgdm->dm.getNumEdges = ccgDM_getNumEdges;
2241         ccgdm->dm.getVert = ccgDM_getFinalVert;
2242         ccgdm->dm.getEdge = ccgDM_getFinalEdge;
2243         ccgdm->dm.getFace = ccgDM_getFinalFace;
2244         ccgdm->dm.copyVertArray = ccgDM_copyFinalVertArray;
2245         ccgdm->dm.copyEdgeArray = ccgDM_copyFinalEdgeArray;
2246         ccgdm->dm.copyFaceArray = ccgDM_copyFinalFaceArray;
2247         ccgdm->dm.getVertData = DM_get_vert_data;
2248         ccgdm->dm.getEdgeData = DM_get_edge_data;
2249         ccgdm->dm.getFaceData = DM_get_face_data;
2250         ccgdm->dm.getVertDataArray = ccgDM_get_vert_data_layer;
2251         ccgdm->dm.getEdgeDataArray = ccgDM_get_edge_data_layer;
2252         ccgdm->dm.getFaceDataArray = ccgDM_get_face_data_layer;
2253         ccgdm->dm.getNumGrids = ccgDM_getNumGrids;
2254         ccgdm->dm.getGridSize = ccgDM_getGridSize;
2255         ccgdm->dm.getGridData = ccgDM_getGridData;
2256         ccgdm->dm.getGridAdjacency = ccgDM_getGridAdjacency;
2257         ccgdm->dm.getGridOffset = ccgDM_getGridOffset;
2258         ccgdm->dm.getPBVH = ccgDM_getPBVH;
2259
2260         ccgdm->dm.getVertCos = ccgdm_getVertCos;
2261         ccgdm->dm.foreachMappedVert = ccgDM_foreachMappedVert;
2262         ccgdm->dm.foreachMappedEdge = ccgDM_foreachMappedEdge;
2263         ccgdm->dm.foreachMappedFaceCenter = ccgDM_foreachMappedFaceCenter;
2264         
2265         ccgdm->dm.drawVerts = ccgDM_drawVerts;
2266         ccgdm->dm.drawEdges = ccgDM_drawEdges;
2267         ccgdm->dm.drawLooseEdges = ccgDM_drawLooseEdges;
2268         ccgdm->dm.drawFacesSolid = ccgDM_drawFacesSolid;
2269         ccgdm->dm.drawFacesColored = ccgDM_drawFacesColored;
2270         ccgdm->dm.drawFacesTex = ccgDM_drawFacesTex;
2271         ccgdm->dm.drawFacesGLSL = ccgDM_drawFacesGLSL;
2272         ccgdm->dm.drawMappedFaces = ccgDM_drawMappedFaces;
2273         ccgdm->dm.drawMappedFacesTex = ccgDM_drawMappedFacesTex;
2274         ccgdm->dm.drawMappedFacesGLSL = ccgDM_drawMappedFacesGLSL;
2275         ccgdm->dm.drawUVEdges = ccgDM_drawUVEdges;
2276
2277         ccgdm->dm.drawMappedEdgesInterp = ccgDM_drawMappedEdgesInterp;
2278         ccgdm->dm.drawMappedEdges = ccgDM_drawMappedEdges;
2279         
2280         ccgdm->dm.release = ccgDM_release;
2281         
2282         ccgdm->ss = ss;
2283         ccgdm->drawInteriorEdges = drawInteriorEdges;
2284         ccgdm->useSubsurfUv = useSubsurfUv;
2285
2286         totvert = ccgSubSurf_getNumVerts(ss);
2287         ccgdm->vertMap = MEM_mallocN(totvert * sizeof(*ccgdm->vertMap), "vertMap");
2288         vi = ccgSubSurf_getVertIterator(ss);
2289         for(; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
2290                 CCGVert *v = ccgVertIterator_getCurrent(vi);
2291
2292                 ccgdm->vertMap[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))].vert = v;
2293         }
2294         ccgVertIterator_free(vi);
2295
2296         totedge = ccgSubSurf_getNumEdges(ss);
2297         ccgdm->edgeMap = MEM_mallocN(totedge * sizeof(*ccgdm->edgeMap), "edgeMap");
2298         ei = ccgSubSurf_getEdgeIterator(ss);
2299         for(; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
2300                 CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
2301
2302                 ccgdm->edgeMap[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))].edge = e;
2303         }
2304
2305         totface = ccgSubSurf_getNumFaces(ss);
2306         ccgdm->faceMap = MEM_mallocN(totface * sizeof(*ccgdm->faceMap), "faceMap");
2307         fi = ccgSubSurf_getFaceIterator(ss);
2308         for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
2309                 CCGFace *f = ccgFaceIterator_getCurrent(fi);
2310
2311                 ccgdm->faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f))].face = f;
2312         }
2313         ccgFaceIterator_free(fi);
2314
2315         edgeSize = ccgSubSurf_getEdgeSize(ss);
2316         gridSize = ccgSubSurf_getGridSize(ss);
2317         gridFaces = gridSize - 1;
2318         gridSideVerts = gridSize - 2;
2319         /*gridInternalVerts = gridSideVerts * gridSideVerts; - as yet, unused */
2320         gridSideEdges = gridSize - 1;
2321         gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2; 
2322
2323         calc_ss_weights(gridFaces, &qweight, &tweight);
2324
2325         vertNum = 0;
2326         edgeNum = 0;
2327         faceNum = 0;
2328
2329         /* mvert = dm->getVertArray(dm); - as yet unused */
2330         medge = dm->getEdgeArray(dm);
2331         mface = dm->getFaceArray(dm);
2332
2333         faceFlags = ccgdm->faceFlags = MEM_callocN(sizeof(char)*2*totface, "faceFlags");
2334
2335         for(index = 0; index < totface; ++index) {
2336                 CCGFace *f = ccgdm->faceMap[index].face;
2337                 int numVerts = ccgSubSurf_getFaceNumVerts(f);
2338                 int numFinalEdges = numVerts * (gridSideEdges + gridInternalEdges);
2339                 int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
2340                 FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight;
2341                 int S, x, y;
2342                 int vertIdx[4];
2343
2344                 ccgdm->faceMap[index].startVert = vertNum;
2345                 ccgdm->faceMap[index].startEdge = edgeNum;
2346                 ccgdm->faceMap[index].startFace = faceNum;
2347
2348                 /* set the face base vert */
2349                 *((int*)ccgSubSurf_getFaceUserData(ss, f)) = vertNum;
2350
2351                 for(S = 0; S < numVerts; S++) {
2352                         CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
2353
2354                         vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2355                 }
2356
2357                 DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, weight[0][0],
2358                                     numVerts, vertNum);
2359                 ++vertNum;
2360
2361                 for(S = 0; S < numVerts; S++) {
2362                         int prevS = (S - 1 + numVerts) % numVerts;
2363                         int nextS = (S + 1) % numVerts;
2364                         int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
2365                         for(x = 1; x < gridFaces; x++) {
2366                                 float w[4];
2367                                 w[prevS]  = weight[x][0][0];
2368                                 w[S]      = weight[x][0][1];
2369                                 w[nextS]  = weight[x][0][2];
2370                                 w[otherS] = weight[x][0][3];
2371                                 DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w,
2372                                                     numVerts, vertNum);
2373                                 ++vertNum;
2374                         }
2375                 }
2376
2377                 for(S = 0; S < numVerts; S++) {
2378                         int prevS = (S - 1 + numVerts) % numVerts;
2379                         int nextS = (S + 1) % numVerts;
2380                         int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
2381                         for(y = 1; y < gridFaces; y++) {
2382                                 for(x = 1; x < gridFaces; x++) {
2383                                         float w[4];
2384                                         w[prevS]  = weight[y * gridFaces + x][0][0];
2385                                         w[S]      = weight[y * gridFaces + x][0][1];
2386                                         w[nextS]  = weight[y * gridFaces + x][0][2];
2387                                         w[otherS] = weight[y * gridFaces + x][0][3];
2388                                         DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w,
2389                                                             numVerts, vertNum);
2390                                         ++vertNum;
2391                                 }
2392                         }
2393                 }
2394
2395                 for(S = 0; S < numVerts; S++) {
2396                         int prevS = (S - 1 + numVerts) % numVerts;
2397                         int nextS = (S + 1) % numVerts;
2398                         int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
2399
2400                         weight = (numVerts == 4) ? qweight : tweight;
2401
2402                         for(y = 0; y < gridFaces; y++) {
2403                                 for(x = 0; x < gridFaces; x++) {
2404                                         FaceVertWeight w;
2405                                         int j;
2406
2407                                         for(j = 0; j < 4; ++j) {
2408                                                 w[j][prevS]  = (*weight)[j][0];
2409                                                 w[j][S]      = (*weight)[j][1];
2410                                                 w[j][nextS]  = (*weight)[j][2];
2411                                                 w[j][otherS] = (*weight)[j][3];
2412                                         }
2413
2414                                         DM_interp_face_data(dm, &ccgdm->dm, &origIndex, NULL,
2415                                                             &w, 1, faceNum);
2416                                         weight++;
2417
2418                                         ++faceNum;
2419                                 }
2420                         }
2421                 }
2422
2423                 faceFlags[index*2] = mface[origIndex].flag;
2424                 faceFlags[index*2 + 1] = mface[origIndex].mat_nr;
2425
2426                 edgeNum += numFinalEdges;
2427         }
2428
2429         if(useSubsurfUv) {
2430                 CustomData *fdata = &ccgdm->dm.faceData;
2431                 CustomData *dmfdata = &dm->faceData;
2432                 int numlayer = CustomData_number_of_layers(fdata, CD_MTFACE);
2433                 int dmnumlayer = CustomData_number_of_layers(dmfdata, CD_MTFACE);
2434
2435                 for (i=0; i<numlayer && i<dmnumlayer; i++)
2436                         set_subsurf_uv(ss, dm, &ccgdm->dm, i);
2437         }
2438
2439         edgeFlags = ccgdm->edgeFlags = MEM_callocN(sizeof(short)*totedge, "edgeFlags");
2440
2441         for(index = 0; index < totedge; ++index) {
2442                 CCGEdge *e = ccgdm->edgeMap[index].edge;
2443                 int numFinalEdges = edgeSize - 1;
2444                 int x;
2445                 int vertIdx[2];
2446                 int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
2447
2448                 CCGVert *v;
2449                 v = ccgSubSurf_getEdgeVert0(e);
2450                 vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2451                 v = ccgSubSurf_getEdgeVert1(e);
2452                 vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2453
2454                 ccgdm->edgeMap[index].startVert = vertNum;
2455                 ccgdm->edgeMap[index].startEdge = edgeNum;
2456
2457                 /* set the edge base vert */
2458                 *((int*)ccgSubSurf_getEdgeUserData(ss, e)) = vertNum;
2459
2460                 for(x = 1; x < edgeSize - 1; x++) {
2461                         float w[2];
2462                         w[1] = (float) x / (edgeSize - 1);
2463                         w[0] = 1 - w[1];
2464                         DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w, 2, vertNum);
2465                         ++vertNum;
2466                 }
2467
2468                 edgeFlags[index]= medge[edgeIdx].flag;
2469
2470                 edgeNum += numFinalEdges;
2471         }
2472
2473         for(index = 0; index < totvert; ++index) {
2474                 CCGVert *v = ccgdm->vertMap[index].vert;
2475                 int vertIdx;
2476
2477                 vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2478
2479                 ccgdm->vertMap[index].startVert = vertNum;
2480
2481                 /* set the vert base vert */
2482                 *((int*) ccgSubSurf_getVertUserData(ss, v)) = vertNum;
2483
2484                 DM_copy_vert_data(dm, &ccgdm->dm, vertIdx, vertNum, 1);
2485
2486                 ++vertNum;
2487         }
2488
2489         MEM_freeN(qweight);
2490         MEM_freeN(tweight);
2491
2492         return ccgdm;
2493 }
2494
2495 /***/
2496
2497 struct DerivedMesh *subsurf_make_derived_from_derived(
2498                         struct DerivedMesh *dm,
2499                         struct SubsurfModifierData *smd,
2500                         int useRenderParams, float (*vertCos)[3],
2501                         int isFinalCalc, int editMode)
2502 {
2503         int useSimple = smd->subdivType == ME_SIMPLE_SUBSURF;
2504         int useAging = smd->flags & eSubsurfModifierFlag_DebugIncr;
2505         int useSubsurfUv = smd->flags & eSubsurfModifierFlag_SubsurfUv;
2506         int drawInteriorEdges = !(smd->flags & eSubsurfModifierFlag_ControlEdges);
2507         CCGDerivedMesh *result;
2508
2509         if(editMode) {
2510                 int levels= (smd->modifier.scene)? get_render_subsurf_level(&smd->modifier.scene->r, smd->levels): smd->levels;
2511
2512                 smd->emCache = _getSubSurf(smd->emCache, levels, useAging, 0,
2513                                            useSimple);
2514                 ss_sync_from_derivedmesh(smd->emCache, dm, vertCos, useSimple);
2515
2516                 result = getCCGDerivedMesh(smd->emCache,
2517                                            drawInteriorEdges,
2518                                        useSubsurfUv, dm);
2519         } else if(useRenderParams) {
2520                 /* Do not use cache in render mode. */
2521                 CCGSubSurf *ss;
2522                 int levels= (smd->modifier.scene)? get_render_subsurf_level(&smd->modifier.scene->r, smd->renderLevels): smd->renderLevels;
2523
2524                 if(levels == 0)
2525                         return dm;
2526                 
2527                 ss = _getSubSurf(NULL, levels, 0, 1, useSimple);
2528
2529                 ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
2530
2531                 result = getCCGDerivedMesh(ss,
2532                         drawInteriorEdges, useSubsurfUv, dm);
2533
2534                 result->freeSS = 1;
2535         } else {
2536                 int useIncremental = (smd->flags & eSubsurfModifierFlag_Incremental);
2537                 int useAging = smd->flags & eSubsurfModifierFlag_DebugIncr;
2538                 int levels= (smd->modifier.scene)? get_render_subsurf_level(&smd->modifier.scene->r, smd->levels): smd->levels;
2539                 CCGSubSurf *ss;
2540                 
2541                 /* It is quite possible there is a much better place to do this. It
2542                  * depends a bit on how rigourously we expect this function to never
2543                  * be called in editmode. In semi-theory we could share a single
2544                  * cache, but the handles used inside and outside editmode are not
2545                  * the same so we would need some way of converting them. Its probably
2546                  * not worth the effort. But then why am I even writing this long
2547                  * comment that no one will read? Hmmm. - zr
2548                  */
2549                 if(smd->emCache) {
2550                         ccgSubSurf_free(smd->emCache);
2551                         smd->emCache = NULL;
2552                 }
2553
2554                 if(useIncremental && isFinalCalc) {
2555                         smd->mCache = ss = _getSubSurf(smd->mCache, levels,
2556                                                        useAging, 0, useSimple);
2557
2558                         ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
2559
2560                         result = getCCGDerivedMesh(smd->mCache,
2561                                                drawInteriorEdges,
2562                                            useSubsurfUv, dm);
2563                 } else {
2564                         if (smd->mCache && isFinalCalc) {
2565                                 ccgSubSurf_free(smd->mCache);
2566                                 smd->mCache = NULL;
2567                         }
2568
2569                         ss = _getSubSurf(NULL, levels, 0, 1, useSimple);
2570                         ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
2571
2572                         result = getCCGDerivedMesh(ss, drawInteriorEdges, useSubsurfUv, dm);
2573
2574                         if(isFinalCalc)
2575                                 smd->mCache = ss;
2576                         else
2577                                 result->freeSS = 1;
2578                 }
2579         }
2580
2581         return (DerivedMesh*)result;
2582 }
2583
2584 void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]) 
2585 {
2586         /* Finds the subsurf limit positions for the verts in a mesh 
2587          * and puts them in an array of floats. Please note that the 
2588          * calculated vert positions is incorrect for the verts 
2589          * on the boundary of the mesh.
2590          */
2591         CCGSubSurf *ss = _getSubSurf(NULL, 1, 0, 1, 0);
2592         float edge_sum[3], face_sum[3];
2593         CCGVertIterator *vi;
2594         DerivedMesh *dm = CDDM_from_mesh(me, NULL);
2595
2596         ss_sync_from_derivedmesh(ss, dm, NULL, 0);
2597
2598         vi = ccgSubSurf_getVertIterator(ss);
2599         for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
2600                 CCGVert *v = ccgVertIterator_getCurrent(vi);
2601                 int idx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
2602                 int N = ccgSubSurf_getVertNumEdges(v);
2603                 int numFaces = ccgSubSurf_getVertNumFaces(v);
2604                 float *co;
2605                 int i;
2606                 
2607                 edge_sum[0]= edge_sum[1]= edge_sum[2]= 0.0;
2608                 face_sum[0]= face_sum[1]= face_sum[2]= 0.0;
2609
2610                 for (i=0; i<N; i++) {
2611                         CCGEdge *e = ccgSubSurf_getVertEdge(v, i);
2612                         add_v3_v3v3(edge_sum, edge_sum, ccgSubSurf_getEdgeData(ss, e, 1));
2613                 }
2614                 for (i=0; i<numFaces; i++) {
2615                         CCGFace *f = ccgSubSurf_getVertFace(v, i);
2616                         add_v3_v3v3(face_sum, face_sum, ccgSubSurf_getFaceCenterData(f));
2617                 }
2618
2619                 /* ad-hoc correction for boundary vertices, to at least avoid them
2620                    moving completely out of place (brecht) */
2621                 if(numFaces && numFaces != N)
2622                         mul_v3_fl(face_sum, (float)N/(float)numFaces);
2623
2624                 co = ccgSubSurf_getVertData(ss, v);
2625                 positions_r[idx][0] = (co[0]*N*N + edge_sum[0]*4 + face_sum[0])/(N*(N+5));
2626                 positions_r[idx][1] = (co[1]*N*N + edge_sum[1]*4 + face_sum[1])/(N*(N+5));
2627                 positions_r[idx][2] = (co[2]*N*N + edge_sum[2]*4 + face_sum[2])/(N*(N+5));
2628         }
2629         ccgVertIterator_free(vi);
2630
2631         ccgSubSurf_free(ss);
2632
2633         dm->release(dm);
2634 }
2635