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