/blender-2.63a/source/blender/blenkernel/intern/subsurf_ccg.c
C | 3503 lines | 2713 code | 627 blank | 163 comment | 564 complexity | b01be4dca33570b01c74626b55444347 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-3.0, BSD-2-Clause, Apache-2.0, AGPL-1.0
Large files files are truncated, but you can click here to view the full file
- /*
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * The Original Code is Copyright (C) 2005 Blender Foundation.
- * All rights reserved.
- *
- * The Original Code is: all of this file.
- *
- * Contributor(s): none yet.
- *
- * ***** END GPL LICENSE BLOCK *****
- */
- /** \file blender/blenkernel/intern/subsurf_ccg.c
- * \ingroup bke
- */
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- #include <float.h>
- #include "MEM_guardedalloc.h"
- #include "DNA_mesh_types.h"
- #include "DNA_meshdata_types.h"
- #include "DNA_modifier_types.h"
- #include "DNA_object_types.h"
- #include "DNA_scene_types.h"
- #include "BLI_utildefines.h"
- #include "BLI_bitmap.h"
- #include "BLI_blenlib.h"
- #include "BLI_edgehash.h"
- #include "BLI_math.h"
- #include "BLI_memarena.h"
- #include "BLI_pbvh.h"
- #include "BKE_cdderivedmesh.h"
- #include "BKE_global.h"
- #include "BKE_mesh.h"
- #include "BKE_modifier.h"
- #include "BKE_multires.h"
- #include "BKE_paint.h"
- #include "BKE_scene.h"
- #include "BKE_subsurf.h"
- #include "BKE_tessmesh.h"
- #include "PIL_time.h"
- #include "BLI_array.h"
- #include "GL/glew.h"
- #include "GPU_draw.h"
- #include "GPU_extensions.h"
- #include "GPU_material.h"
- #include "CCGSubSurf.h"
- extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
- static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
- int drawInteriorEdges,
- int useSubsurfUv,
- DerivedMesh *dm);
- static int ccgDM_use_grid_pbvh(CCGDerivedMesh *ccgdm);
- ///
- static void *arena_alloc(CCGAllocatorHDL a, int numBytes)
- {
- return BLI_memarena_alloc(a, numBytes);
- }
- static void *arena_realloc(CCGAllocatorHDL a, void *ptr, int newSize, int oldSize)
- {
- void *p2 = BLI_memarena_alloc(a, newSize);
- if (ptr) {
- memcpy(p2, ptr, oldSize);
- }
- return p2;
- }
- static void arena_free(CCGAllocatorHDL UNUSED(a), void *UNUSED(ptr))
- {
- /* do nothing */
- }
- static void arena_release(CCGAllocatorHDL a)
- {
- BLI_memarena_free(a);
- }
- typedef enum {
- CCG_USE_AGING = 1,
- CCG_USE_ARENA = 2,
- CCG_CALC_NORMALS = 4
- } CCGFlags;
- static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels, CCGFlags flags)
- {
- CCGMeshIFC ifc;
- CCGSubSurf *ccgSS;
- int useAging = !!(flags & CCG_USE_AGING);
- int useArena = flags & CCG_USE_ARENA;
- /* subdivLevels==0 is not allowed */
- subdivLevels = MAX2(subdivLevels, 1);
- if (prevSS) {
- int oldUseAging;
- ccgSubSurf_getUseAgeCounts(prevSS, &oldUseAging, NULL, NULL, NULL);
- if (oldUseAging != useAging) {
- ccgSubSurf_free(prevSS);
- }
- else {
- ccgSubSurf_setSubdivisionLevels(prevSS, subdivLevels);
- return prevSS;
- }
- }
- if (useAging) {
- ifc.vertUserSize = ifc.edgeUserSize = ifc.faceUserSize = 12;
- }
- else {
- ifc.vertUserSize = ifc.edgeUserSize = ifc.faceUserSize = 8;
- }
- ifc.vertDataSize = sizeof(float) * (flags & CCG_CALC_NORMALS ? 6 : 3);
- if (useArena) {
- CCGAllocatorIFC allocatorIFC;
- CCGAllocatorHDL allocator = BLI_memarena_new((1 << 16), "subsurf arena");
- allocatorIFC.alloc = arena_alloc;
- allocatorIFC.realloc = arena_realloc;
- allocatorIFC.free = arena_free;
- allocatorIFC.release = arena_release;
- ccgSS = ccgSubSurf_new(&ifc, subdivLevels, &allocatorIFC, allocator);
- }
- else {
- ccgSS = ccgSubSurf_new(&ifc, subdivLevels, NULL, NULL);
- }
- if (useAging) {
- ccgSubSurf_setUseAgeCounts(ccgSS, 1, 8, 8, 8);
- }
- if (flags & CCG_CALC_NORMALS)
- ccgSubSurf_setCalcVertexNormals(ccgSS, 1, offsetof(DMGridData, no));
- else
- ccgSubSurf_setCalcVertexNormals(ccgSS, 0, 0);
- return ccgSS;
- }
- static int getEdgeIndex(CCGSubSurf *ss, CCGEdge *e, int x, int edgeSize)
- {
- CCGVert *v0 = ccgSubSurf_getEdgeVert0(e);
- CCGVert *v1 = ccgSubSurf_getEdgeVert1(e);
- int v0idx = *((int *) ccgSubSurf_getVertUserData(ss, v0));
- int v1idx = *((int *) ccgSubSurf_getVertUserData(ss, v1));
- int edgeBase = *((int *) ccgSubSurf_getEdgeUserData(ss, e));
- if (x == 0) {
- return v0idx;
- }
- else if (x == edgeSize - 1) {
- return v1idx;
- }
- else {
- return edgeBase + x - 1;
- }
- }
- static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edgeSize, int gridSize)
- {
- int faceBase = *((int *) ccgSubSurf_getFaceUserData(ss, f));
- int numVerts = ccgSubSurf_getFaceNumVerts(f);
- if (x == gridSize - 1 && y == gridSize - 1) {
- CCGVert *v = ccgSubSurf_getFaceVert(f, S);
- return *((int *) ccgSubSurf_getVertUserData(ss, v));
- }
- else if (x == gridSize - 1) {
- CCGVert *v = ccgSubSurf_getFaceVert(f, S);
- CCGEdge *e = ccgSubSurf_getFaceEdge(f, S);
- int edgeBase = *((int *) ccgSubSurf_getEdgeUserData(ss, e));
- if (v == ccgSubSurf_getEdgeVert0(e)) {
- return edgeBase + (gridSize - 1 - y) - 1;
- }
- else {
- return edgeBase + (edgeSize - 2 - 1) - ((gridSize - 1 - y) - 1);
- }
- }
- else if (y == gridSize - 1) {
- CCGVert *v = ccgSubSurf_getFaceVert(f, S);
- CCGEdge *e = ccgSubSurf_getFaceEdge(f, (S + numVerts - 1) % numVerts);
- int edgeBase = *((int *) ccgSubSurf_getEdgeUserData(ss, e));
- if (v == ccgSubSurf_getEdgeVert0(e)) {
- return edgeBase + (gridSize - 1 - x) - 1;
- }
- else {
- return edgeBase + (edgeSize - 2 - 1) - ((gridSize - 1 - x) - 1);
- }
- }
- else if (x == 0 && y == 0) {
- return faceBase;
- }
- else if (x == 0) {
- S = (S + numVerts - 1) % numVerts;
- return faceBase + 1 + (gridSize - 2) * S + (y - 1);
- }
- else if (y == 0) {
- return faceBase + 1 + (gridSize - 2) * S + (x - 1);
- }
- else {
- return faceBase + 1 + (gridSize - 2) * numVerts + S * (gridSize - 2) * (gridSize - 2) + (y - 1) * (gridSize - 2) + (x - 1);
- }
- }
- static void get_face_uv_map_vert(UvVertMap *vmap, struct MPoly *mpoly, struct MLoop *ml, int fi, CCGVertHDL *fverts)
- {
- UvMapVert *v, *nv;
- int j, nverts = mpoly[fi].totloop;
- for (j = 0; j < nverts; j++) {
- for (nv = v = get_uv_map_vert(vmap, ml[j].v); v; v = v->next) {
- if (v->separate)
- nv = v;
- if (v->f == fi)
- break;
- }
- fverts[j] = SET_INT_IN_POINTER(mpoly[nv->f].loopstart + nv->tfindex);
- }
- }
- static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm, MLoopUV *mloopuv)
- {
- MPoly *mpoly = dm->getPolyArray(dm);
- MLoop *mloop = dm->getLoopArray(dm);
- MVert *mvert = dm->getVertArray(dm);
- int totvert = dm->getNumVerts(dm);
- int totface = dm->getNumPolys(dm);
- int i, j, seam;
- UvMapVert *v;
- UvVertMap *vmap;
- float limit[2];
- CCGVertHDL *fverts = NULL;
- BLI_array_declare(fverts);
- EdgeHash *ehash;
- float creaseFactor = (float)ccgSubSurf_getSubdivisionLevels(ss);
- float uv[3] = {0.0f, 0.0f, 0.0f}; /* only first 2 values are written into */
- limit[0] = limit[1] = STD_UV_CONNECT_LIMIT;
- vmap = make_uv_vert_map(mpoly, mloop, mloopuv, totface, totvert, 0, limit);
- if (!vmap)
- return 0;
-
- ccgSubSurf_initFullSync(ss);
- /* create vertices */
- for (i = 0; i < totvert; i++) {
- if (!get_uv_map_vert(vmap, i))
- continue;
- for (v = get_uv_map_vert(vmap, i)->next; v; v = v->next)
- if (v->separate)
- break;
- seam = (v != NULL) || ((mvert + i)->flag & ME_VERT_MERGED);
- for (v = get_uv_map_vert(vmap, i); v; v = v->next) {
- if (v->separate) {
- CCGVert *ssv;
- int loopid = mpoly[v->f].loopstart + v->tfindex;
- CCGVertHDL vhdl = SET_INT_IN_POINTER(loopid);
- copy_v2_v2(uv, mloopuv[loopid].uv);
- ccgSubSurf_syncVert(ss, vhdl, uv, seam, &ssv);
- }
- }
- }
- /* create edges */
- ehash = BLI_edgehash_new();
- for (i = 0; i < totface; i++) {
- MPoly *mp = &((MPoly *) mpoly)[i];
- int nverts = mp->totloop;
- CCGFace *origf = ccgSubSurf_getFace(origss, SET_INT_IN_POINTER(i));
- /* unsigned int *fv = &mp->v1; */
- MLoop *ml = mloop + mp->loopstart;
- BLI_array_empty(fverts);
- BLI_array_growitems(fverts, nverts);
- get_face_uv_map_vert(vmap, mpoly, ml, i, fverts);
- for (j = 0; j < nverts; j++) {
- int v0 = GET_INT_FROM_POINTER(fverts[j]);
- int v1 = GET_INT_FROM_POINTER(fverts[(j + 1) % nverts]);
- MVert *mv0 = mvert + (ml[j].v);
- MVert *mv1 = mvert + (ml[((j + 1) % nverts)].v);
- if (!BLI_edgehash_haskey(ehash, v0, v1)) {
- CCGEdge *e, *orige = ccgSubSurf_getFaceEdge(origf, j);
- CCGEdgeHDL ehdl = SET_INT_IN_POINTER(mp->loopstart + j);
- float crease;
- if ((mv0->flag & mv1->flag) & ME_VERT_MERGED)
- crease = creaseFactor;
- else
- crease = ccgSubSurf_getEdgeCrease(orige);
- ccgSubSurf_syncEdge(ss, ehdl, fverts[j], fverts[(j + 1) % nverts], crease, &e);
- BLI_edgehash_insert(ehash, v0, v1, NULL);
- }
- }
- }
- BLI_edgehash_free(ehash, NULL);
- /* create faces */
- for (i = 0; i < totface; i++) {
- MPoly *mp = &mpoly[i];
- MLoop *ml = &mloop[mp->loopstart];
- int nverts = mp->totloop;
- CCGFace *f;
- BLI_array_empty(fverts);
- BLI_array_growitems(fverts, nverts);
- get_face_uv_map_vert(vmap, mpoly, ml, i, fverts);
- ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), nverts, fverts, &f);
- }
- BLI_array_free(fverts);
- free_uv_vert_map(vmap);
- ccgSubSurf_processSync(ss);
- return 1;
- }
- static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, int n)
- {
- CCGSubSurf *uvss;
- CCGFace **faceMap;
- MTFace *tf;
- MLoopUV *mluv;
- CCGFaceIterator *fi;
- int index, gridSize, gridFaces, /*edgeSize,*/ totface, x, y, S;
- MLoopUV *dmloopuv = CustomData_get_layer_n(&dm->loopData, CD_MLOOPUV, n);
- /* need to update both CD_MTFACE & CD_MLOOPUV, hrmf, we could get away with
- * just tface except applying the modifier then looses subsurf UV */
- MTFace *tface = CustomData_get_layer_n(&result->faceData, CD_MTFACE, n);
- MLoopUV *mloopuv = CustomData_get_layer_n(&result->loopData, CD_MLOOPUV, n);
- if (!dmloopuv || (!tface && !mloopuv))
- return;
- /* create a CCGSubSurf from uv's */
- uvss = _getSubSurf(NULL, ccgSubSurf_getSubdivisionLevels(ss), CCG_USE_ARENA);
- if (!ss_sync_from_uv(uvss, ss, dm, dmloopuv)) {
- ccgSubSurf_free(uvss);
- return;
- }
- /* get some info from CCGSubSurf */
- totface = ccgSubSurf_getNumFaces(uvss);
- /* edgeSize = ccgSubSurf_getEdgeSize(uvss); */ /*UNUSED*/
- gridSize = ccgSubSurf_getGridSize(uvss);
- gridFaces = gridSize - 1;
- /* make a map from original faces to CCGFaces */
- faceMap = MEM_mallocN(totface * sizeof(*faceMap), "facemapuv");
- fi = ccgSubSurf_getFaceIterator(uvss);
- for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
- CCGFace *f = ccgFaceIterator_getCurrent(fi);
- faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f))] = f;
- }
- ccgFaceIterator_free(fi);
- /* load coordinates from uvss into tface */
- tf = tface;
- mluv = mloopuv;
- for (index = 0; index < totface; index++) {
- CCGFace *f = faceMap[index];
- int numVerts = ccgSubSurf_getFaceNumVerts(f);
- for (S = 0; S < numVerts; S++) {
- float (*faceGridData)[3] = ccgSubSurf_getFaceGridDataArray(uvss, f, S);
- for (y = 0; y < gridFaces; y++) {
- for (x = 0; x < gridFaces; x++) {
- float *a = faceGridData[(y + 0) * gridSize + x + 0];
- float *b = faceGridData[(y + 0) * gridSize + x + 1];
- float *c = faceGridData[(y + 1) * gridSize + x + 1];
- float *d = faceGridData[(y + 1) * gridSize + x + 0];
- if (tf) {
- copy_v2_v2(tf->uv[0], a);
- copy_v2_v2(tf->uv[1], d);
- copy_v2_v2(tf->uv[2], c);
- copy_v2_v2(tf->uv[3], b);
- tf++;
- }
- if (mluv) {
- copy_v2_v2(mluv[0].uv, a);
- copy_v2_v2(mluv[1].uv, d);
- copy_v2_v2(mluv[2].uv, c);
- copy_v2_v2(mluv[3].uv, b);
- mluv += 4;
- }
- }
- }
- }
- }
- ccgSubSurf_free(uvss);
- MEM_freeN(faceMap);
- }
- /* face weighting */
- typedef struct FaceVertWeightEntry {
- FaceVertWeight *weight;
- float *w;
- int valid;
- } FaceVertWeightEntry;
- typedef struct WeightTable {
- FaceVertWeightEntry *weight_table;
- int len;
- } WeightTable;
- static float *get_ss_weights(WeightTable *wtable, int gridCuts, int faceLen)
- {
- int x, y, i, j;
- float *w, w1, w2, w4, fac, fac2, fx, fy;
- if (wtable->len <= faceLen) {
- void *tmp = MEM_callocN(sizeof(FaceVertWeightEntry) * (faceLen + 1), "weight table alloc 2");
-
- if (wtable->len) {
- memcpy(tmp, wtable->weight_table, sizeof(FaceVertWeightEntry) * wtable->len);
- MEM_freeN(wtable->weight_table);
- }
-
- wtable->weight_table = tmp;
- wtable->len = faceLen + 1;
- }
- if (!wtable->weight_table[faceLen].valid) {
- wtable->weight_table[faceLen].valid = 1;
- wtable->weight_table[faceLen].w = w = MEM_callocN(sizeof(float) * faceLen * faceLen * (gridCuts + 2) * (gridCuts + 2), "weight table alloc");
- fac = 1.0f / (float)faceLen;
- for (i = 0; i < faceLen; i++) {
- for (x = 0; x < gridCuts + 2; x++) {
- for (y = 0; y < gridCuts + 2; y++) {
- fx = 0.5f - (float)x / (float)(gridCuts + 1) / 2.0f;
- fy = 0.5f - (float)y / (float)(gridCuts + 1) / 2.0f;
-
- fac2 = faceLen - 4;
- w1 = (1.0f - fx) * (1.0f - fy) + (-fac2 * fx * fy * fac);
- w2 = (1.0f - fx + fac2 * fx * -fac) * (fy);
- w4 = (fx) * (1.0f - fy + -fac2 * fy * fac);
- fac2 = 1.0f - (w1 + w2 + w4);
- fac2 = fac2 / (float)(faceLen - 3);
- for (j = 0; j < faceLen; j++)
- w[j] = fac2;
-
- w[i] = w1;
- w[(i - 1 + faceLen) % faceLen] = w2;
- w[(i + 1) % faceLen] = w4;
- w += faceLen;
- }
- }
- }
- }
- return wtable->weight_table[faceLen].w;
- }
- static void free_ss_weights(WeightTable *wtable)
- {
- int i;
- for (i = 0; i < wtable->len; i++) {
- if (wtable->weight_table[i].valid)
- MEM_freeN(wtable->weight_table[i].w);
- }
-
- if (wtable->weight_table)
- MEM_freeN(wtable->weight_table);
- }
- static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
- float (*vertexCos)[3], int useFlatSubdiv)
- {
- float creaseFactor = (float) ccgSubSurf_getSubdivisionLevels(ss);
- CCGVertHDL *fVerts = NULL;
- BLI_array_declare(fVerts);
- MVert *mvert = dm->getVertArray(dm);
- MEdge *medge = dm->getEdgeArray(dm);
- /* MFace *mface = dm->getTessFaceArray(dm); */ /* UNUSED */
- MVert *mv;
- MEdge *me;
- MLoop *mloop = dm->getLoopArray(dm), *ml;
- MPoly *mpoly = dm->getPolyArray(dm), *mp;
- /*MFace *mf;*/ /*UNUSED*/
- int totvert = dm->getNumVerts(dm);
- int totedge = dm->getNumEdges(dm);
- /*int totface = dm->getNumTessFaces(dm);*/ /*UNUSED*/
- /*int totpoly = dm->getNumFaces(dm);*/ /*UNUSED*/
- int i, j;
- int *index;
- ccgSubSurf_initFullSync(ss);
- mv = mvert;
- index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
- for (i = 0; i < totvert; i++, mv++) {
- CCGVert *v;
- if (vertexCos) {
- ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), vertexCos[i], 0, &v);
- }
- else {
- ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), mv->co, 0, &v);
- }
- ((int *)ccgSubSurf_getVertUserData(ss, v))[1] = (index) ? *index++ : i;
- }
- me = medge;
- index = (int *)dm->getEdgeDataArray(dm, CD_ORIGINDEX);
- for (i = 0; i < totedge; i++, me++) {
- CCGEdge *e;
- float crease;
- crease = useFlatSubdiv ? creaseFactor :
- me->crease * creaseFactor / 255.0f;
- ccgSubSurf_syncEdge(ss, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(me->v1),
- SET_INT_IN_POINTER(me->v2), crease, &e);
- ((int *)ccgSubSurf_getEdgeUserData(ss, e))[1] = (index) ? *index++ : i;
- }
- mp = mpoly;
- index = DM_get_poly_data_layer(dm, CD_ORIGINDEX);
- for (i = 0; i < dm->numPolyData; i++, mp++) {
- CCGFace *f;
- BLI_array_empty(fVerts);
- BLI_array_growitems(fVerts, mp->totloop);
- ml = mloop + mp->loopstart;
- for (j = 0; j < mp->totloop; j++, ml++) {
- fVerts[j] = SET_INT_IN_POINTER(ml->v);
- }
- /* this is very bad, means mesh is internally inconsistent.
- * it is not really possible to continue without modifying
- * other parts of code significantly to handle missing faces.
- * since this really shouldn't even be possible we just bail.*/
- if (ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), mp->totloop,
- fVerts, &f) == eCCGError_InvalidValue) {
- static int hasGivenError = 0;
- if (!hasGivenError) {
- //XXX error("Unrecoverable error in SubSurf calculation,"
- // " mesh is inconsistent.");
- hasGivenError = 1;
- }
- return;
- }
- ((int *)ccgSubSurf_getFaceUserData(ss, f))[1] = (index) ? *index++ : i;
- }
- ccgSubSurf_processSync(ss);
- BLI_array_free(fVerts);
- }
- /***/
- static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v)
- {
- return ((int *) ccgSubSurf_getVertUserData(ss, v))[1];
- }
- static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e)
- {
- return ((int *) ccgSubSurf_getEdgeUserData(ss, e))[1];
- }
- static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f)
- {
- return ((int *) ccgSubSurf_getFaceUserData(ss, f))[1];
- }
- static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- CCGVertIterator *vi = ccgSubSurf_getVertIterator(ss);
- CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
- CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
- int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
- int gridSize = ccgSubSurf_getGridSize(ss);
- if (!ccgSubSurf_getNumVerts(ss))
- min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
- for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
- CCGVert *v = ccgVertIterator_getCurrent(vi);
- float *co = ccgSubSurf_getVertData(ss, v);
- DO_MINMAX(co, min_r, max_r);
- }
- for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
- CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
- DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
- for (i = 0; i < edgeSize; i++)
- DO_MINMAX(edgeData[i].co, min_r, max_r);
- }
- for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
- CCGFace *f = ccgFaceIterator_getCurrent(fi);
- int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
- for (S = 0; S < numVerts; S++) {
- DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
- for (y = 0; y < gridSize; y++)
- for (x = 0; x < gridSize; x++)
- DO_MINMAX(faceGridData[y * gridSize + x].co, min_r, max_r);
- }
- }
- ccgFaceIterator_free(fi);
- ccgEdgeIterator_free(ei);
- ccgVertIterator_free(vi);
- }
- static int ccgDM_getNumVerts(DerivedMesh *dm)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- return ccgSubSurf_getNumFinalVerts(ccgdm->ss);
- }
- static int ccgDM_getNumEdges(DerivedMesh *dm)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- return ccgSubSurf_getNumFinalEdges(ccgdm->ss);
- }
- static int ccgDM_getNumTessFaces(DerivedMesh *dm)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- return ccgSubSurf_getNumFinalFaces(ccgdm->ss);
- }
- static int ccgDM_getNumLoops(DerivedMesh *dm)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- /* All subsurf faces are quads */
- return 4 * ccgSubSurf_getNumFinalFaces(ccgdm->ss);
- }
- static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- DMGridData *vd;
- int i;
- memset(mv, 0, sizeof(*mv));
- if ((vertNum < ccgdm->edgeMap[0].startVert) && (ccgSubSurf_getNumFaces(ss) > 0)) {
- /* this vert comes from face data */
- int lastface = ccgSubSurf_getNumFaces(ss) - 1;
- CCGFace *f;
- int x, y, grid, numVerts;
- int offset;
- int gridSize = ccgSubSurf_getGridSize(ss);
- int gridSideVerts;
- int gridInternalVerts;
- int gridSideEnd;
- int gridInternalEnd;
- i = 0;
- while (i < lastface && vertNum >= ccgdm->faceMap[i + 1].startVert)
- ++i;
- f = ccgdm->faceMap[i].face;
- numVerts = ccgSubSurf_getFaceNumVerts(f);
- gridSideVerts = gridSize - 2;
- gridInternalVerts = gridSideVerts * gridSideVerts;
- gridSideEnd = 1 + numVerts * gridSideVerts;
- gridInternalEnd = gridSideEnd + numVerts * gridInternalVerts;
- offset = vertNum - ccgdm->faceMap[i].startVert;
- if (offset < 1) {
- vd = ccgSubSurf_getFaceCenterData(f);
- copy_v3_v3(mv->co, vd->co);
- normal_float_to_short_v3(mv->no, vd->no);
- }
- else if (offset < gridSideEnd) {
- offset -= 1;
- grid = offset / gridSideVerts;
- x = offset % gridSideVerts + 1;
- vd = ccgSubSurf_getFaceGridEdgeData(ss, f, grid, x);
- copy_v3_v3(mv->co, vd->co);
- normal_float_to_short_v3(mv->no, vd->no);
- }
- else if (offset < gridInternalEnd) {
- offset -= gridSideEnd;
- grid = offset / gridInternalVerts;
- offset %= gridInternalVerts;
- y = offset / gridSideVerts + 1;
- x = offset % gridSideVerts + 1;
- vd = ccgSubSurf_getFaceGridData(ss, f, grid, x, y);
- copy_v3_v3(mv->co, vd->co);
- normal_float_to_short_v3(mv->no, vd->no);
- }
- }
- else if ((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) {
- /* this vert comes from edge data */
- CCGEdge *e;
- int lastedge = ccgSubSurf_getNumEdges(ss) - 1;
- int x;
- i = 0;
- while (i < lastedge && vertNum >= ccgdm->edgeMap[i + 1].startVert)
- ++i;
- e = ccgdm->edgeMap[i].edge;
- x = vertNum - ccgdm->edgeMap[i].startVert + 1;
- vd = ccgSubSurf_getEdgeData(ss, e, x);
- copy_v3_v3(mv->co, vd->co);
- normal_float_to_short_v3(mv->no, vd->no);
- }
- else {
- /* this vert comes from vert data */
- CCGVert *v;
- i = vertNum - ccgdm->vertMap[0].startVert;
- v = ccgdm->vertMap[i].vert;
- vd = ccgSubSurf_getVertData(ss, v);
- copy_v3_v3(mv->co, vd->co);
- normal_float_to_short_v3(mv->no, vd->no);
- }
- }
- static void ccgDM_getFinalVertCo(DerivedMesh *dm, int vertNum, float co_r[3])
- {
- MVert mvert;
- ccgDM_getFinalVert(dm, vertNum, &mvert);
- copy_v3_v3(co_r, mvert.co);
- }
- static void ccgDM_getFinalVertNo(DerivedMesh *dm, int vertNum, float no_r[3])
- {
- MVert mvert;
- ccgDM_getFinalVert(dm, vertNum, &mvert);
- normal_short_to_float_v3(no_r, mvert.no);
- }
- static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int i;
- memset(med, 0, sizeof(*med));
- if (edgeNum < ccgdm->edgeMap[0].startEdge) {
- /* this edge comes from face data */
- int lastface = ccgSubSurf_getNumFaces(ss) - 1;
- CCGFace *f;
- int x, y, grid /*, numVerts*/;
- int offset;
- int gridSize = ccgSubSurf_getGridSize(ss);
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int gridSideEdges;
- int gridInternalEdges;
- /* code added in bmesh but works correctly without, commenting - campbell */
- #if 0
- int lasti, previ;
- i = lastface;
- lasti = 0;
- while (1) {
- previ = i;
- if (ccgdm->faceMap[i].startEdge >= edgeNum) {
- i -= fabsf(i - lasti) / 2.0f;
- }
- else if (ccgdm->faceMap[i].startEdge < edgeNum) {
- i += fabsf(i - lasti) / 2.0f;
- }
- else {
- break;
- }
- if (i < 0) {
- i = 0;
- break;
- }
- if (i > lastface) {
- i = lastface;
- break;
- }
- if (i == lasti)
- break;
- lasti = previ;
- }
- i = i > 0 ? i - 1 : i;
- #endif
- i = 0;
- while (i < lastface && edgeNum >= ccgdm->faceMap[i + 1].startEdge)
- ++i;
- f = ccgdm->faceMap[i].face;
- /* numVerts = ccgSubSurf_getFaceNumVerts(f); */ /*UNUSED*/
- gridSideEdges = gridSize - 1;
- gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2;
- offset = edgeNum - ccgdm->faceMap[i].startEdge;
- grid = offset / (gridSideEdges + gridInternalEdges);
- offset %= (gridSideEdges + gridInternalEdges);
- if (offset < gridSideEdges) {
- x = offset;
- med->v1 = getFaceIndex(ss, f, grid, x, 0, edgeSize, gridSize);
- med->v2 = getFaceIndex(ss, f, grid, x + 1, 0, edgeSize, gridSize);
- }
- else {
- offset -= gridSideEdges;
- x = (offset / 2) / gridSideEdges + 1;
- y = (offset / 2) % gridSideEdges;
- if (offset % 2 == 0) {
- med->v1 = getFaceIndex(ss, f, grid, x, y, edgeSize, gridSize);
- med->v2 = getFaceIndex(ss, f, grid, x, y + 1, edgeSize, gridSize);
- }
- else {
- med->v1 = getFaceIndex(ss, f, grid, y, x, edgeSize, gridSize);
- med->v2 = getFaceIndex(ss, f, grid, y + 1, x, edgeSize, gridSize);
- }
- }
- }
- else {
- /* this vert comes from edge data */
- CCGEdge *e;
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int x;
- short *edgeFlag;
- unsigned int flags = 0;
- i = (edgeNum - ccgdm->edgeMap[0].startEdge) / (edgeSize - 1);
- e = ccgdm->edgeMap[i].edge;
- if (!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
- x = edgeNum - ccgdm->edgeMap[i].startEdge;
- med->v1 = getEdgeIndex(ss, e, x, edgeSize);
- med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
- edgeFlag = (ccgdm->edgeFlags) ? &ccgdm->edgeFlags[i] : NULL;
- if (edgeFlag)
- flags |= (*edgeFlag & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER;
- else
- flags |= ME_EDGEDRAW | ME_EDGERENDER;
- med->flag = flags;
- }
- }
- static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int gridSize = ccgSubSurf_getGridSize(ss);
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int gridSideEdges = gridSize - 1;
- int gridFaces = gridSideEdges * gridSideEdges;
- int i;
- CCGFace *f;
- /*int numVerts;*/
- int offset;
- int grid;
- int x, y;
- /*int lastface = ccgSubSurf_getNumFaces(ss) - 1;*/ /*UNUSED*/
- DMFlagMat *faceFlags = ccgdm->faceFlags;
- memset(mf, 0, sizeof(*mf));
- if (faceNum >= ccgdm->dm.numTessFaceData)
- return;
- i = ccgdm->reverseFaceMap[faceNum];
- f = ccgdm->faceMap[i].face;
- /*numVerts = ccgSubSurf_getFaceNumVerts(f);*/ /*UNUSED*/
- offset = faceNum - ccgdm->faceMap[i].startFace;
- grid = offset / gridFaces;
- offset %= gridFaces;
- y = offset / gridSideEdges;
- x = offset % gridSideEdges;
- mf->v1 = getFaceIndex(ss, f, grid, x + 0, y + 0, edgeSize, gridSize);
- mf->v2 = getFaceIndex(ss, f, grid, x + 0, y + 1, edgeSize, gridSize);
- mf->v3 = getFaceIndex(ss, f, grid, x + 1, y + 1, edgeSize, gridSize);
- mf->v4 = getFaceIndex(ss, f, grid, x + 1, y + 0, edgeSize, gridSize);
- if (faceFlags) {
- mf->flag = faceFlags[i].flag;
- mf->mat_nr = faceFlags[i].mat_nr;
- }
- else mf->flag = ME_SMOOTH;
- }
- /* Translate GridHidden into the ME_HIDE flag for MVerts. Assumes
- * vertices are in the order output by ccgDM_copyFinalVertArray. */
- void subsurf_copy_grid_hidden(DerivedMesh *dm, const MPoly *mpoly,
- MVert *mvert, const MDisps *mdisps)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
- CCGSubSurf *ss = ccgdm->ss;
- int level = ccgSubSurf_getSubdivisionLevels(ss);
- int gridSize = ccgSubSurf_getGridSize(ss);
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int totface = ccgSubSurf_getNumFaces(ss);
- int i, j, x, y;
-
- for (i = 0; i < totface; i++) {
- CCGFace *f = ccgdm->faceMap[i].face;
- for (j = 0; j < mpoly[i].totloop; j++) {
- const MDisps *md = &mdisps[mpoly[i].loopstart + j];
- int hidden_gridsize = ccg_gridsize(md->level);
- int factor = ccg_factor(level, md->level);
-
- if (!md->hidden)
- continue;
-
- for (y = 0; y < gridSize; y++) {
- for (x = 0; x < gridSize; x++) {
- int vndx, offset;
-
- vndx = getFaceIndex(ss, f, j, x, y, edgeSize, gridSize);
- offset = (y * factor) * hidden_gridsize + (x * factor);
- if (BLI_BITMAP_GET(md->hidden, offset))
- mvert[vndx].flag |= ME_HIDE;
- }
- }
- }
- }
- }
- static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- DMGridData *vd;
- int index;
- int totvert, totedge, totface;
- int gridSize = ccgSubSurf_getGridSize(ss);
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int i = 0;
- totface = ccgSubSurf_getNumFaces(ss);
- for (index = 0; index < totface; index++) {
- CCGFace *f = ccgdm->faceMap[index].face;
- int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
- vd = ccgSubSurf_getFaceCenterData(f);
- copy_v3_v3(mvert[i].co, vd->co);
- normal_float_to_short_v3(mvert[i].no, vd->no);
- i++;
-
- for (S = 0; S < numVerts; S++) {
- for (x = 1; x < gridSize - 1; x++, i++) {
- vd = ccgSubSurf_getFaceGridEdgeData(ss, f, S, x);
- copy_v3_v3(mvert[i].co, vd->co);
- normal_float_to_short_v3(mvert[i].no, vd->no);
- }
- }
- for (S = 0; S < numVerts; S++) {
- for (y = 1; y < gridSize - 1; y++) {
- for (x = 1; x < gridSize - 1; x++, i++) {
- vd = ccgSubSurf_getFaceGridData(ss, f, S, x, y);
- copy_v3_v3(mvert[i].co, vd->co);
- normal_float_to_short_v3(mvert[i].no, vd->no);
- }
- }
- }
- }
- totedge = ccgSubSurf_getNumEdges(ss);
- for (index = 0; index < totedge; index++) {
- CCGEdge *e = ccgdm->edgeMap[index].edge;
- int x;
- for (x = 1; x < edgeSize - 1; x++, i++) {
- vd = ccgSubSurf_getEdgeData(ss, e, x);
- copy_v3_v3(mvert[i].co, vd->co);
- /* This gives errors with -debug-fpe
- * the normals don't seem to be unit length.
- * this is most likely caused by edges with no
- * faces which are now zerod out, see comment in:
- * ccgSubSurf__calcVertNormals(), - campbell */
- normal_float_to_short_v3(mvert[i].no, vd->no);
- }
- }
- totvert = ccgSubSurf_getNumVerts(ss);
- for (index = 0; index < totvert; index++) {
- CCGVert *v = ccgdm->vertMap[index].vert;
- vd = ccgSubSurf_getVertData(ss, v);
- copy_v3_v3(mvert[i].co, vd->co);
- normal_float_to_short_v3(mvert[i].no, vd->no);
- i++;
- }
- }
- static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int index;
- int totedge, totface;
- int gridSize = ccgSubSurf_getGridSize(ss);
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int i = 0;
- short *edgeFlags = ccgdm->edgeFlags;
- totface = ccgSubSurf_getNumFaces(ss);
- for (index = 0; index < totface; index++) {
- CCGFace *f = ccgdm->faceMap[index].face;
- int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
- for (S = 0; S < numVerts; S++) {
- for (x = 0; x < gridSize - 1; x++) {
- MEdge *med = &medge[i];
- if (ccgdm->drawInteriorEdges)
- med->flag = ME_EDGEDRAW | ME_EDGERENDER;
- med->v1 = getFaceIndex(ss, f, S, x, 0, edgeSize, gridSize);
- med->v2 = getFaceIndex(ss, f, S, x + 1, 0, edgeSize, gridSize);
- i++;
- }
- for (x = 1; x < gridSize - 1; x++) {
- for (y = 0; y < gridSize - 1; y++) {
- MEdge *med;
- med = &medge[i];
- if (ccgdm->drawInteriorEdges)
- med->flag = ME_EDGEDRAW | ME_EDGERENDER;
- med->v1 = getFaceIndex(ss, f, S, x, y,
- edgeSize, gridSize);
- med->v2 = getFaceIndex(ss, f, S, x, y + 1,
- edgeSize, gridSize);
- i++;
- med = &medge[i];
- if (ccgdm->drawInteriorEdges)
- med->flag = ME_EDGEDRAW | ME_EDGERENDER;
- med->v1 = getFaceIndex(ss, f, S, y, x,
- edgeSize, gridSize);
- med->v2 = getFaceIndex(ss, f, S, y + 1, x,
- edgeSize, gridSize);
- i++;
- }
- }
- }
- }
- totedge = ccgSubSurf_getNumEdges(ss);
- for (index = 0; index < totedge; index++) {
- CCGEdge *e = ccgdm->edgeMap[index].edge;
- unsigned int flags = 0;
- int x;
- int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
- if (!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
- if (edgeFlags) {
- if (edgeIdx != -1) {
- flags |= (edgeFlags[index] & (ME_SEAM | ME_SHARP))
- | ME_EDGEDRAW | ME_EDGERENDER;
- }
- }
- else {
- flags |= ME_EDGEDRAW | ME_EDGERENDER;
- }
- for (x = 0; x < edgeSize - 1; x++) {
- MEdge *med = &medge[i];
- med->v1 = getEdgeIndex(ss, e, x, edgeSize);
- med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
- med->flag = flags;
- i++;
- }
- }
- }
- static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int index;
- int totface;
- int gridSize = ccgSubSurf_getGridSize(ss);
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int i = 0;
- DMFlagMat *faceFlags = ccgdm->faceFlags;
- totface = ccgSubSurf_getNumFaces(ss);
- for (index = 0; index < totface; index++) {
- CCGFace *f = ccgdm->faceMap[index].face;
- int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
- /* keep types in sync with MFace, avoid many conversions */
- char flag = (faceFlags) ? faceFlags[index].flag : ME_SMOOTH;
- short mat_nr = (faceFlags) ? faceFlags[index].mat_nr : 0;
- for (S = 0; S < numVerts; S++) {
- for (y = 0; y < gridSize - 1; y++) {
- for (x = 0; x < gridSize - 1; x++) {
- MFace *mf = &mface[i];
- mf->v1 = getFaceIndex(ss, f, S, x + 0, y + 0,
- edgeSize, gridSize);
- mf->v2 = getFaceIndex(ss, f, S, x + 0, y + 1,
- edgeSize, gridSize);
- mf->v3 = getFaceIndex(ss, f, S, x + 1, y + 1,
- edgeSize, gridSize);
- mf->v4 = getFaceIndex(ss, f, S, x + 1, y + 0,
- edgeSize, gridSize);
- mf->mat_nr = mat_nr;
- mf->flag = flag;
- i++;
- }
- }
- }
- }
- }
- static void ccgDM_copyFinalLoopArray(DerivedMesh *dm, MLoop *mloop)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int index;
- int totface;
- int gridSize = ccgSubSurf_getGridSize(ss);
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int i = 0;
- MLoop *mv;
- /* DMFlagMat *faceFlags = ccgdm->faceFlags; */ /* UNUSED */
- if (!ccgdm->ehash) {
- MEdge *medge;
- ccgdm->ehash = BLI_edgehash_new();
- medge = ccgdm->dm.getEdgeArray((DerivedMesh *)ccgdm);
- for (i = 0; i < ccgdm->dm.numEdgeData; i++) {
- BLI_edgehash_insert(ccgdm->ehash, medge[i].v1, medge[i].v2, SET_INT_IN_POINTER(i));
- }
- }
- totface = ccgSubSurf_getNumFaces(ss);
- mv = mloop;
- for (index = 0; index < totface; index++) {
- CCGFace *f = ccgdm->faceMap[index].face;
- int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
- /* int flag = (faceFlags)? faceFlags[index*2]: ME_SMOOTH; */ /* UNUSED */
- /* int mat_nr = (faceFlags)? faceFlags[index*2+1]: 0; */ /* UNUSED */
- for (S = 0; S < numVerts; S++) {
- for (y = 0; y < gridSize - 1; y++) {
- for (x = 0; x < gridSize - 1; x++) {
- int v1, v2, v3, v4;
- v1 = getFaceIndex(ss, f, S, x + 0, y + 0,
- edgeSize, gridSize);
- v2 = getFaceIndex(ss, f, S, x + 0, y + 1,
- edgeSize, gridSize);
- v3 = getFaceIndex(ss, f, S, x + 1, y + 1,
- edgeSize, gridSize);
- v4 = getFaceIndex(ss, f, S, x + 1, y + 0,
- edgeSize, gridSize);
- mv->v = v1;
- mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v1, v2));
- mv++, i++;
- mv->v = v2;
- mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v2, v3));
- mv++, i++;
- mv->v = v3;
- mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v3, v4));
- mv++, i++;
- mv->v = v4;
- mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v4, v1));
- mv++, i++;
- }
- }
- }
- }
- }
- static void ccgDM_copyFinalPolyArray(DerivedMesh *dm, MPoly *mpoly)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int index;
- int totface;
- int gridSize = ccgSubSurf_getGridSize(ss);
- /* int edgeSize = ccgSubSurf_getEdgeSize(ss); */ /* UNUSED */
- int i = 0, k = 0;
- DMFlagMat *faceFlags = ccgdm->faceFlags;
- totface = ccgSubSurf_getNumFaces(ss);
- for (index = 0; index < totface; index++) {
- CCGFace *f = ccgdm->faceMap[index].face;
- int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
- int flag = (faceFlags) ? faceFlags[index].flag : ME_SMOOTH;
- int mat_nr = (faceFlags) ? faceFlags[index].mat_nr : 0;
- for (S = 0; S < numVerts; S++) {
- for (y = 0; y < gridSize - 1; y++) {
- for (x = 0; x < gridSize - 1; x++) {
- MPoly *mp = &mpoly[i];
- mp->mat_nr = mat_nr;
- mp->flag = flag;
- mp->loopstart = k;
- mp->totloop = 4;
- k += 4;
- i++;
- }
- }
- }
- }
- }
- static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3])
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int gridSize = ccgSubSurf_getGridSize(ss);
- int i;
- CCGVertIterator *vi;
- CCGEdgeIterator *ei;
- CCGFaceIterator *fi;
- CCGFace **faceMap2;
- CCGEdge **edgeMap2;
- CCGVert **vertMap2;
- int index, totvert, totedge, totface;
-
- totvert = ccgSubSurf_getNumVerts(ss);
- vertMap2 = MEM_mallocN(totvert * sizeof(*vertMap2), "vertmap");
- vi = ccgSubSurf_getVertIterator(ss);
- for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
- CCGVert *v = ccgVertIterator_getCurrent(vi);
- vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v;
- }
- ccgVertIterator_free(vi);
- totedge = ccgSubSurf_getNumEdges(ss);
- edgeMap2 = MEM_mallocN(totedge * sizeof(*edgeMap2), "edgemap");
- ei = ccgSubSurf_getEdgeIterator(ss);
- for (i = 0; !ccgEdgeIterator_isStopped(ei); i++, ccgEdgeIterator_next(ei)) {
- CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
- edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e;
- }
- totface = ccgSubSurf_getNumFaces(ss);
- faceMap2 = MEM_mallocN(totface * sizeof(*faceMap2), "facemap");
- fi = ccgSubSurf_getFaceIterator(ss);
- for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
- CCGFace *f = ccgFaceIterator_getCurrent(fi);
- faceMap2[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f))] = f;
- }
- ccgFaceIterator_free(fi);
- i = 0;
- for (index = 0; index < totface; index++) {
- CCGFace *f = faceMap2[index];
- int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
- copy_v3_v3(cos[i++], ccgSubSurf_getFaceCenterData(f));
-
- for (S = 0; S < numVerts; S++) {
- for (x = 1; x < gridSize - 1; x++) {
- copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
- }
- }
- for (S = 0; S < numVerts; S++) {
- for (y = 1; y < gridSize - 1; y++) {
- for (x = 1; x < gridSize - 1; x++) {
- copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridData(ss, f, S, x, y));
- }
- }
- }
- }
- for (index = 0; index < totedge; index++) {
- CCGEdge *e = edgeMap2[index];
- int x;
- for (x = 1; x < edgeSize - 1; x++) {
- copy_v3_v3(cos[i++], ccgSubSurf_getEdgeData(ss, e, x));
- }
- }
- for (index = 0; index < totvert; index++) {
- CCGVert *v = vertMap2[index];
- copy_v3_v3(cos[i++], ccgSubSurf_getVertData(ss, v));
- }
- MEM_freeN(vertMap2);
- MEM_freeN(edgeMap2);
- MEM_freeN(faceMap2);
- }
- static void ccgDM_foreachMappedVert(
- DerivedMesh *dm,
- void (*func)(void *userData, int index, const float co[3], const float no_f[3], const short no_s[3]),
- void *userData)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGVertIterator *vi = ccgSubSurf_getVertIterator(ccgdm->ss);
- for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
- CCGVert *v = ccgVertIterator_getCurrent(vi);
- DMGridData *vd = ccgSubSurf_getVertData(ccgdm->ss, v);
- int index = ccgDM_getVertMapIndex(ccgdm->ss, v);
- if (index != -1)
- func(userData, index, vd->co, vd->no, NULL);
- }
- ccgVertIterator_free(vi);
- }
- static void ccgDM_foreachMappedEdge(
- DerivedMesh *dm,
- void (*func)(void *userData, int index, const float v0co[3], const float v1co[3]),
- void *userData)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
- int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
- for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
- CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
- DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
- int index = ccgDM_getEdgeMapIndex(ss, e);
- if (index != -1) {
- for (i = 0; i < edgeSize - 1; i++)
- func(userData, index, edgeData[i].co, edgeData[i + 1].co);
- }
- }
- ccgEdgeIterator_free(ei);
- }
- static void ccgDM_drawVerts(DerivedMesh *dm)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- int gridSize = ccgSubSurf_getGridSize(ss);
- CCGVertIterator *vi;
- CCGEdgeIterator *ei;
- CCGFaceIterator *fi;
- glBegin(GL_POINTS);
- vi = ccgSubSurf_getVertIterator(ss);
- for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
- CCGVert *v = ccgVertIterator_getCurrent(vi);
- glVertex3fv(ccgSubSurf_getVertData(ss, v));
- }
- ccgVertIterator_free(vi);
- ei = ccgSubSurf_getEdgeIterator(ss);
- for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
- CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
- int x;
- for (x = 1; x < edgeSize - 1; x++)
- glVertex3fv(ccgSubSurf_getEdgeData(ss, e, x));
- }
- ccgEdgeIterator_free(ei);
- fi = ccgSubSurf_getFaceIterator(ss);
- for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
- CCGFace *f = ccgFaceIterator_getCurrent(fi);
- int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
- glVertex3fv(ccgSubSurf_getFaceCenterData(f));
- for (S = 0; S < numVerts; S++)
- for (x = 1; x < gridSize - 1; x++)
- glVertex3fv(ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
- for (S = 0; S < numVerts; S++)
- for (y = 1; y < gridSize - 1; y++)
- for (x = 1; x < gridSize - 1; x++)
- glVertex3fv(ccgSubSurf_getFaceGridData(ss, f, S, x, y));
- }
- ccgFaceIterator_free(fi);
- glEnd();
- }
- static void ccgdm_pbvh_update(CCGDerivedMesh *ccgdm)
- {
- if (ccgdm->pbvh && ccgDM_use_grid_pbvh(ccgdm)) {
- CCGFace **faces;
- int totface;
- BLI_pbvh_get_grid_updates(ccgdm->pbvh, 1, (void ***)&faces, &totface);
- if (totface) {
- ccgSubSurf_updateFromFaces(ccgdm->ss, 0, faces, totface);
- ccgSubSurf_updateNormals(ccgdm->ss, faces, totface);
- MEM_freeN(faces);
- }
- }
- }
- static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges, int drawAllEdges)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int i, j, edgeSize = ccgSubSurf_getEdgeSize(ss);
- int totedge = ccgSubSurf_getNumEdges(ss);
- int gridSize = ccgSubSurf_getGridSize(ss);
- int useAging;
- ccgdm_pbvh_update(ccgdm);
- ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
- for (j = 0; j < totedge; j++) {
- CCGEdge *e = ccgdm->edgeMap[j].edge;
- DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
- if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e))
- continue;
- if (!drawAllEdges && ccgdm->edgeFlags && !(ccgdm->edgeFlags[j] & ME_EDGEDRAW))
- continue;
- if (useAging && !(G.f & G_BACKBUFSEL)) {
- int ageCol = 255 - ccgSubSurf_getEdgeAge(ss, e) * 4;
- glColor3ub(0, ageCol > 0 ? ageCol : 0, 0);
- }
- glBegin(GL_LINE_STRIP);
- for (i = 0; i < edgeSize - 1; i++) {
- glVertex3fv(edgeData[i].co);
- glVertex3fv(edgeData[i + 1].co);
- }
- glEnd();
- }
- if (useAging && !(G.f & G_BACKBUFSEL)) {
- glColor3ub(0, 0, 0);
- }
- if (ccgdm->drawInteriorEdges) {
- int totface = ccgSubSurf_getNumFaces(ss);
- for (j = 0; j < totface; j++) {
- CCGFace *f = ccgdm->faceMap[j].face;
- int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
- for (S = 0; S < numVerts; S++) {
- DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
- glBegin(GL_LINE_STRIP);
- for (x = 0; x < gridSize; x++)
- glVertex3fv(faceGridData[x].co);
- glEnd();
- for (y = 1; y < gridSize - 1; y++) {
- glBegin(GL_LINE_STRIP);
- for (x = 0; x < gridSize; x++)
- glVertex3fv(faceGridData[y * gridSize + x].co);
- glEnd();
- }
- for (x = 1; x < gridSize - 1; x++) {
- glBegin(GL_LINE_STRIP);
- for (y = 0; y < gridSize; y++)
- glVertex3fv(faceGridData[y * gridSize + x].co);
- glEnd();
- }
- }
- }
- }
- }
- static void ccgDM_drawLooseEdges(DerivedMesh *dm)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int totedge = ccgSubSurf_getNumEdges(ss);
- int i, j, edgeSize = ccgSubSurf_getEdgeSize(ss);
- for (j = 0; j < totedge; j++) {
- CCGEdge *e = ccgdm->edgeMap[j].edge;
- DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
- if (!ccgSubSurf_getEdgeNumFaces(e)) {
- glBegin(GL_LINE_STRIP);
- for (i = 0; i < edgeSize - 1; i++) {
- glVertex3fv(edgeData[i].co);
- glVertex3fv(edgeData[i + 1].co);
- }
- glEnd();
- }
- }
- }
- static void ccgDM_glNormalFast(float *a, float *b, float *c, float *d)
- {
- float a_cX = c[0] - a[0], a_cY = c[1] - a[1], a_cZ = c[2] - a[2];
- float b_dX = d[0] - b[0], b_dY = d[1] - b[1], b_dZ = d[2] - b[2];
- float no[3];
- no[0] = b_dY * a_cZ - b_dZ * a_cY;
- no[1] = b_dZ * a_cX - b_dX * a_cZ;
- no[2] = b_dX * a_cY - b_dY * a_cX;
- /* don't normalize, GL_NORMALIZE is enabled */
- glNormal3fv(no);
- }
- /* Only used by non-editmesh types */
- static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes)[4], int fast, DMSetMaterial setMaterial)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- int gridSize = ccgSubSurf_getGridSize(ss);
- DMFlagMat *faceFlags = ccgdm->faceFlags;
- int step = (fast) ? gridSize - 1 : 1;
- int i, totface = ccgSubSurf_getNumFaces(ss);
- int drawcurrent = 0, matnr = -1, shademodel = -1;
- ccgdm_pbvh_update(ccgdm);
- if (ccgdm->pbvh && ccgdm->multires.mmd && !fast) {
- if (dm->numTessFaceData) {
- BLI_pbvh_draw(ccgdm->pbvh, partial_redraw_planes, NULL, setMaterial);
- glShadeModel(GL_FLAT);
- }
- return;
- }
- for (i = 0; i < totface; i++) {
- CCGFace *f = ccgdm->faceMap[i].face;
- int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
- int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
- int new_matnr, new_shademodel;
- if (faceFlags) {
- new_shademodel = (faceFlags[index].flag & ME_SMOOTH) ? GL_SMOOTH : GL_FLAT;
- new_matnr = faceFlags[index].mat_nr;
- }
- else {
- new_shademodel = GL_SMOOTH;
- new_matnr = 0;
- }
-
- if (shademodel != new_shademodel || matnr != new_matnr) {
- matnr = new_matnr;
- shademodel = new_shademodel;
- drawcurrent = setMaterial(matnr + 1, NULL);
- glShadeModel(shademodel);
- }
- if (!drawcurrent)
- continue;
- for (S = 0; S < numVerts; S++) {
- DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
- if (shademodel == GL_SMOOTH) {
- for (y = 0; y < gridSize - 1; y += step) {
- glBegin(GL_QUAD_STRIP);
- for (x = 0; x < gridSize; x += step) {
- DMGridData *a = &faceGridData[(y + 0) * gridSize + x];
- DMGridData *b = &faceGridData[(y + step) * gridSize + x];
- glNormal3fv(a->no);
- glVertex3fv(a->co);
- glNormal3fv(b->no);
- glVertex3fv(b->co);
- }
- glEnd();
- }
- }
- else {
- glBegin(GL_QUADS);
- for (y = 0; y < gridSize - 1; y += step) {
- for (x = 0; x < gridSize - 1; x += step) {
- float *a = faceGridData[(y + 0) * gridSize + x].co;
- float *b = faceGridData[(y + 0) * gridSize + x + step].co;
- float *c = faceGridData[(y + step) * gridSize + x + step].co;
- float *d = faceGridData[(y + step) * gridSize + x].co;
- ccgDM_glNormalFast(a, b, c, d);
- glVertex3fv(d);
- glVertex3fv(c);
- glVertex3fv(b);
- glVertex3fv(a);
- }
- }
- glEnd();
- }
- }
- }
- }
- /* Only used by non-editmesh types */
- static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm,
- DMSetMaterial setMaterial,
- DMSetDrawOptions setDrawOptions,
- void *userData)
- {
- CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
- CCGSubSurf *ss = ccgdm->ss;
- GPUVertexAttribs gattribs;
- DMVertexAttribs attribs = {{{NULL}}};
- /* MTFace *tf = dm->getTessFaceDataArray(dm, CD_MTFACE); */ /* UNUSED */
- int gridSize = ccgSubSurf_getGridSize(ss);
- int gridFaces = gridSize - 1;
- int edgeSize = ccgSubSurf_getEdgeSize(ss);
- DMFlagMat *faceFlags = ccgdm->faceFlags;
- int a, b, i, doDraw, numVerts, matnr, new_matnr, totface;
- ccgdm_pbvh_update(ccgdm);
- doDraw = 0;
- matnr = -1;
- #define PASSATTRIB(dx, dy, vert) { \
- if (attribs.totorco) { \
- index = getFaceIndex(ss, f, S, x + dx, y + dy, edgeSize, gridSize); \
- glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]); \
- } \
- for (b = 0; b < attribs.tottface; b++) { \
- MTFace *tf = &attribs.tface[b].array[a]; \
- glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]); \
- } \
- for (b = 0; b < attribs.totmcol; b++) { \
- MCol *cp = &attribs.mcol[b].array[a * 4 + vert]; \
- GLubyte col[4]; \
- col[0] = cp->b; col[1] = cp->g; col[2] = cp->r; col[3] = cp->a; \
- glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col); \
- } \
- if (attribs.tottang) { \
- float *tang = attribs.tang.array[a * 4 + vert]; \
- glVertexAttrib4fvARB(attribs.tang.glIndex, tang); \
- } \
- }
- totface = ccgSubSurf_getNumFaces(ss);
- for (a = 0, i = 0; i < totface; i++) {
- CCGFace *f = ccgdm->faceMap[i].face;
- int S, x, y, drawSmooth;
- int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
- int origIndex = ccgDM_getFaceMapIndex(ss, f);
-
- numVerts = ccgSubSurf_getFaceNumVerts(f);
- if (faceFlags) {
- drawSmooth = (faceFlags[index].flag & ME_SMOOTH);
- new_matnr = faceFlags[index].mat_nr + 1;
- }
- else {
- drawSmooth = 1;
- new_matnr = 1;
- }
- if (new_matnr != matnr) {
- doDraw = setMaterial(matnr = new_matnr, &gattribs);
- if (doDraw)
- DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
- }
- if (!doDraw || (setDrawOptions && (origIndex != ORIGINDEX_NONE) &&
- (setDrawOptions(userData, origIndex) == DM_DRAW_OPTION_SKIP)))
- {
- a += gridFaces * gridFaces * numVerts;
- continue;
- }
- glShadeModel(drawSmooth ? GL_SMOOTH : GL_FLAT);
- for (S = 0; S < numVerts; S++) {
- DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
- DMGridData *vda, *vdb;
- if (drawSmooth) {
- for (y = 0; y < gridFaces; y++) {
- glBegin(GL_QUAD_STRIP);
- for (x = 0; x < gridFaces; x++) {
- vda = &faceGridData[(y + 0) * gridSize + x];
- vdb = &faceGridData[(y + 1) * gri…
Large files files are truncated, but you can click here to view the full file