diff options
author | Nicholas Bishop <nicholasbishop@gmail.com> | 2012-05-11 00:33:09 +0400 |
---|---|---|
committer | Nicholas Bishop <nicholasbishop@gmail.com> | 2012-05-11 00:33:09 +0400 |
commit | f751d0f6ae7de155343e24e36965956bd7a061e3 (patch) | |
tree | d963bb17c3fb79066e9de499b20317626d60fe92 /source/blender/blenkernel/intern | |
parent | 2ca64189b734dbd289d1a07e1ce9d332eadebe19 (diff) |
Replace hardcoded DMGridData structure with CCGElem/CCGKey.
* Changes to DerivedMesh interface: DMGridData has been removed,
getGridData() now returns an array of CCGElem pointers. Also added
getGridKey() to initialize a CCGKey (implemented only by
CCGDerivedMesh.)
* PBVH: added BLI_pbvh_get_grid_key().
* A lot of code is affected, but mainly is just replacing
DMGridData.co, DMGridData.no, and sizeof(DMGridData) with the
CCG_*_elem functions, removing the reliance on grid elements of
exactly six floats.
Diffstat (limited to 'source/blender/blenkernel/intern')
-rw-r--r-- | source/blender/blenkernel/intern/multires.c | 182 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/subsurf_ccg.c | 352 |
2 files changed, 308 insertions, 226 deletions
diff --git a/source/blender/blenkernel/intern/multires.c b/source/blender/blenkernel/intern/multires.c index 37407ce7342..73705ab3752 100644 --- a/source/blender/blenkernel/intern/multires.c +++ b/source/blender/blenkernel/intern/multires.c @@ -46,6 +46,7 @@ #include "BLI_pbvh.h" #include "BLI_utildefines.h" +#include "BKE_ccg.h" #include "BKE_cdderivedmesh.h" #include "BKE_mesh.h" #include "BKE_modifier.h" @@ -75,7 +76,7 @@ typedef enum { } DispOp; static void multires_mvert_to_ss(DerivedMesh *dm, MVert *mvert); -static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm2, DispOp op, DMGridData **oldGridData, int totlvl); +static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm2, DispOp op, CCGElem **oldGridData, int totlvl); /** Customdata **/ @@ -569,23 +570,27 @@ static void multires_copy_grid(float (*gridA)[3], float (*gridB)[3], int sizeA, } } -static void multires_copy_dm_grid(DMGridData *gridA, DMGridData *gridB, int sizeA, int sizeB) +static void multires_copy_dm_grid(CCGElem *gridA, CCGElem *gridB, CCGKey *keyA, CCGKey *keyB) { int x, y, j, skip; - if (sizeA > sizeB) { - skip = (sizeA - 1) / (sizeB - 1); + if (keyA->grid_size > keyB->grid_size) { + skip = (keyA->grid_size - 1) / (keyB->grid_size - 1); - for (j = 0, y = 0; y < sizeB; y++) - for (x = 0; x < sizeB; x++, j++) - copy_v3_v3(gridA[y * skip * sizeA + x * skip].co, gridB[j].co); + for (j = 0, y = 0; y < keyB->grid_size; y++) + for (x = 0; x < keyB->grid_size; x++, j++) + memcpy(CCG_elem_offset_co(keyA, gridA, y * skip * keyA->grid_size + x * skip), + CCG_elem_offset_co(keyB, gridB, j), + sizeof(float) * keyA->num_layers); } else { - skip = (sizeB - 1) / (sizeA - 1); + skip = (keyB->grid_size - 1) / (keyA->grid_size - 1); - for (j = 0, y = 0; y < sizeA; y++) - for (x = 0; x < sizeA; x++, j++) - copy_v3_v3(gridA[j].co, gridB[y * skip * sizeB + x * skip].co); + for (j = 0, y = 0; y < keyA->grid_size; y++) + for (x = 0; x < keyA->grid_size; x++, j++) + memcpy(CCG_elem_offset_co(keyA, gridA, j), + CCG_elem_offset_co(keyB, gridB, y * skip * keyB->grid_size + x*skip), + sizeof(float) * keyA->num_layers); } } @@ -850,9 +855,10 @@ static void multires_subdivide(MultiresModifierData *mmd, Object *ob, int totlvl if (mdisps->disps && !updateblock && totlvl > 1) { /* upsample */ DerivedMesh *lowdm, *cddm, *highdm; - DMGridData **highGridData, **lowGridData, **subGridData; + CCGElem **highGridData, **lowGridData, **subGridData; + CCGKey highGridKey, lowGridKey; CCGSubSurf *ss; - int i, numGrids, highGridSize, lowGridSize; + int i, numGrids, highGridSize; /* create subsurf DM from original mesh at high level */ cddm = CDDM_from_mesh(me, NULL); @@ -867,18 +873,19 @@ static void multires_subdivide(MultiresModifierData *mmd, Object *ob, int totlvl numGrids = highdm->getNumGrids(highdm); highGridSize = highdm->getGridSize(highdm); highGridData = highdm->getGridData(highdm); - lowGridSize = lowdm->getGridSize(lowdm); + highdm->getGridKey(highdm, &highGridKey); lowGridData = lowdm->getGridData(lowdm); + lowdm->getGridKey(lowdm, &lowGridKey); subGridData = MEM_callocN(sizeof(float *) * numGrids, "subGridData*"); for (i = 0; i < numGrids; ++i) { /* backup subsurf grids */ - subGridData[i] = MEM_callocN(sizeof(DMGridData) * highGridSize * highGridSize, "subGridData"); - memcpy(subGridData[i], highGridData[i], sizeof(DMGridData) * highGridSize * highGridSize); + subGridData[i] = MEM_callocN(highGridKey.elem_size * highGridSize * highGridSize, "subGridData"); + memcpy(subGridData[i], highGridData[i], highGridKey.elem_size*highGridSize * highGridSize); /* overwrite with current displaced grids */ - multires_copy_dm_grid(highGridData[i], lowGridData[i], highGridSize, lowGridSize); + multires_copy_dm_grid(highGridData[i], lowGridData[i], &highGridKey, &lowGridKey); } /* low lower level dm no longer needed at this point */ @@ -914,34 +921,35 @@ void multiresModifier_subdivide(MultiresModifierData *mmd, Object *ob, int updat multires_subdivide(mmd, ob, mmd->totlvl + 1, updateblock, simple); } -void grid_tangent(int gridSize, int index, int x, int y, int axis, DMGridData **gridData, float t[3]) +void grid_tangent(const CCGKey *key, int x, int y, int axis, CCGElem *grid, float t[3]) { if (axis == 0) { - if (x == gridSize - 1) { - if (y == gridSize - 1) - sub_v3_v3v3(t, gridData[index][x + gridSize * (y - 1)].co, gridData[index][x - 1 + gridSize * (y - 1)].co); + if (x == key->grid_size - 1) { + if (y == key->grid_size - 1) + sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y - 1), CCG_grid_elem_co(key, grid, x - 1, y - 1)); else - sub_v3_v3v3(t, gridData[index][x + gridSize * y].co, gridData[index][x - 1 + gridSize * y].co); + sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x - 1, y)); } else - sub_v3_v3v3(t, gridData[index][x + 1 + gridSize * y].co, gridData[index][x + gridSize * y].co); + sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x + 1, y), CCG_grid_elem_co(key, grid, x, y)); } else if (axis == 1) { - if (y == gridSize - 1) { - if (x == gridSize - 1) - sub_v3_v3v3(t, gridData[index][x - 1 + gridSize * y].co, gridData[index][x - 1 + gridSize * (y - 1)].co); + if (y == key->grid_size - 1) { + if (x == key->grid_size - 1) + sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x - 1, y), CCG_grid_elem_co(key, grid, x - 1, (y - 1))); else - sub_v3_v3v3(t, gridData[index][x + gridSize * y].co, gridData[index][x + gridSize * (y - 1)].co); + sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x, (y - 1))); } else - sub_v3_v3v3(t, gridData[index][x + gridSize * (y + 1)].co, gridData[index][x + gridSize * y].co); + sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, (y + 1)), CCG_grid_elem_co(key, grid, x, y)); } } -static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm2, DispOp op, DMGridData **oldGridData, int totlvl) +static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm2, DispOp op, CCGElem **oldGridData, int totlvl) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm; - DMGridData **gridData, **subGridData; + CCGElem **gridData, **subGridData; + CCGKey key; MPoly *mpoly = me->mpoly; MDisps *mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS); int *gridOffset; @@ -971,7 +979,8 @@ static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm gridSize = dm->getGridSize(dm); gridData = dm->getGridData(dm); gridOffset = dm->getGridOffset(dm); - subGridData = (oldGridData) ? oldGridData : gridData; + dm->getGridKey(dm, &key); + subGridData = (oldGridData) ? oldGridData: gridData; dGridSize = multires_side_tot[totlvl]; dSkip = (dGridSize - 1) / (gridSize - 1); @@ -986,8 +995,8 @@ static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm for (S = 0; S < numVerts; ++S, ++gIndex, ++k) { MDisps *mdisp = &mdisps[mpoly[i].loopstart + S]; - DMGridData *grid = gridData[gIndex]; - DMGridData *subgrid = subGridData[gIndex]; + CCGElem *grid = gridData[gIndex]; + CCGElem *subgrid = subGridData[gIndex]; float (*dispgrid)[3] = NULL; /* when adding new faces in edit mode, need to allocate disps */ @@ -1001,17 +1010,17 @@ static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm for (y = 0; y < gridSize; y++) { for (x = 0; x < gridSize; x++) { - float *co = grid[x + y * gridSize].co; - float *sco = subgrid[x + y * gridSize].co; - float *no = subgrid[x + y * gridSize].no; + float *co = CCG_grid_elem_co(&key, grid, x, y); + float *sco = CCG_grid_elem_co(&key, subgrid, x, y); + float *no = CCG_grid_elem_no(&key, subgrid, x, y); float *data = dispgrid[dGridSize * y * dSkip + x * dSkip]; float mat[3][3], tx[3], ty[3], disp[3], d[3]; /* construct tangent space matrix */ - grid_tangent(gridSize, gIndex, x, y, 0, subGridData, tx); + grid_tangent(&key, x, y, 0, subGridData[gIndex], tx); normalize_v3(tx); - grid_tangent(gridSize, gIndex, x, y, 1, subGridData, ty); + grid_tangent(&key, x, y, 1, subGridData[gIndex], ty); normalize_v3(ty); //mul_v3_fl(tx, 1.0f/(gridSize-1)); @@ -1075,7 +1084,8 @@ void multires_modifier_update_mdisps(struct DerivedMesh *dm) if (lvl < totlvl) { Mesh *me = ob->data; DerivedMesh *lowdm, *cddm, *highdm; - DMGridData **highGridData, **lowGridData, **subGridData, **gridData, *diffGrid; + CCGElem **highGridData, **lowGridData, **subGridData, **gridData, *diffGrid; + CCGKey highGridKey, lowGridKey; CCGSubSurf *ss; int i, j, numGrids, highGridSize, lowGridSize; @@ -1094,23 +1104,30 @@ void multires_modifier_update_mdisps(struct DerivedMesh *dm) numGrids = highdm->getNumGrids(highdm); highGridSize = highdm->getGridSize(highdm); highGridData = highdm->getGridData(highdm); + highdm->getGridKey(highdm, &highGridKey); lowGridSize = lowdm->getGridSize(lowdm); lowGridData = lowdm->getGridData(lowdm); + lowdm->getGridKey(lowdm, &lowGridKey); gridData = dm->getGridData(dm); - subGridData = MEM_callocN(sizeof(DMGridData *) * numGrids, "subGridData*"); - diffGrid = MEM_callocN(sizeof(DMGridData) * lowGridSize * lowGridSize, "diff"); + BLI_assert(highGridKey.elem_size == lowGridKey.elem_size); + + subGridData = MEM_callocN(sizeof(CCGElem*)*numGrids, "subGridData*"); + diffGrid = MEM_callocN(lowGridKey.elem_size*lowGridSize*lowGridSize, "diff"); for (i = 0; i < numGrids; ++i) { /* backup subsurf grids */ - subGridData[i] = MEM_callocN(sizeof(DMGridData) * highGridSize * highGridSize, "subGridData"); - memcpy(subGridData[i], highGridData[i], sizeof(DMGridData) * highGridSize * highGridSize); + subGridData[i] = MEM_callocN(highGridKey.elem_size*highGridSize*highGridSize, "subGridData"); + memcpy(subGridData[i], highGridData[i], highGridKey.elem_size*highGridSize*highGridSize); /* write difference of subsurf and displaced low level into high subsurf */ - for (j = 0; j < lowGridSize * lowGridSize; ++j) - sub_v3_v3v3(diffGrid[j].co, gridData[i][j].co, lowGridData[i][j].co); + for (j = 0; j < lowGridSize*lowGridSize; ++j) { + sub_v3_v3v3(CCG_elem_offset_co(&lowGridKey, diffGrid, j), + CCG_elem_offset_co(&lowGridKey, gridData[i], j), + CCG_elem_offset_co(&lowGridKey, lowGridData[i], j)); + } - multires_copy_dm_grid(highGridData[i], diffGrid, highGridSize, lowGridSize); + multires_copy_dm_grid(highGridData[i], diffGrid, &highGridKey, &lowGridKey); } /* lower level dm no longer needed at this point */ @@ -1183,7 +1200,8 @@ void multires_modifier_update_hidden(DerivedMesh *dm) void multires_set_space(DerivedMesh *dm, Object *ob, int from, int to) { DerivedMesh *ccgdm = NULL, *subsurf = NULL; - DMGridData **gridData, **subGridData = NULL; + CCGElem **gridData, **subGridData = NULL; + CCGKey key; MPoly *mpoly = CustomData_get_layer(&dm->polyData, CD_MPOLY); MDisps *mdisps; MultiresModifierData *mmd = get_multires_modifier(NULL, ob, 1); @@ -1208,12 +1226,13 @@ void multires_set_space(DerivedMesh *dm, Object *ob, int from, int to) numGrids = subsurf->getNumGrids(subsurf); gridSize = subsurf->getGridSize(subsurf); gridData = subsurf->getGridData(subsurf); + subsurf->getGridKey(subsurf, &key); - subGridData = MEM_callocN(sizeof(DMGridData *) * numGrids, "subGridData*"); + subGridData = MEM_callocN(sizeof(CCGElem*)*numGrids, "subGridData*"); for (i = 0; i < numGrids; i++) { - subGridData[i] = MEM_callocN(sizeof(DMGridData) * gridSize * gridSize, "subGridData"); - memcpy(subGridData[i], gridData[i], sizeof(DMGridData) * gridSize * gridSize); + subGridData[i] = MEM_callocN(key.elem_size*gridSize*gridSize, "subGridData"); + memcpy(subGridData[i], gridData[i], key.elem_size*gridSize*gridSize); } /*numGrids = ccgdm->dm->getNumGrids((DerivedMesh*)ccgdm);*/ /*UNUSED*/ @@ -1234,8 +1253,8 @@ void multires_set_space(DerivedMesh *dm, Object *ob, int from, int to) for (S = 0; S < numVerts; ++S, ++gIndex, ++k) { MDisps *mdisp = &mdisps[mpoly[i].loopstart + S]; - /* DMGridData *grid = gridData[gIndex]; */ /* UNUSED */ - DMGridData *subgrid = subGridData[gIndex]; + /* CCGElem *grid = gridData[gIndex]; */ /* UNUSED */ + CCGElem *subgrid = subGridData[gIndex]; float (*dispgrid)[3] = NULL; /* when adding new faces in edit mode, need to allocate disps */ @@ -1249,16 +1268,16 @@ void multires_set_space(DerivedMesh *dm, Object *ob, int from, int to) for (y = 0; y < gridSize; y++) { for (x = 0; x < gridSize; x++) { - float *data = dispgrid[dGridSize * y * dSkip + x * dSkip]; - float *no = subgrid[x + y * gridSize].no; - float *co = subgrid[x + y * gridSize].co; + float *data = dispgrid[dGridSize*y*dSkip + x*dSkip]; + float *no = CCG_grid_elem_no(&key, subgrid, x, y); + float *co = CCG_grid_elem_co(&key, subgrid, x, y); float mat[3][3], tx[3], ty[3], dco[3]; /* construct tangent space matrix */ - grid_tangent(gridSize, gIndex, x, y, 0, subGridData, tx); + grid_tangent(&key, x, y, 0, subGridData[gIndex], tx); normalize_v3(tx); - grid_tangent(gridSize, gIndex, x, y, 1, subGridData, ty); + grid_tangent(&key, x, y, 1, subGridData[gIndex], ty); normalize_v3(ty); column_vectors_to_mat3(mat, tx, ty, no); @@ -1334,7 +1353,8 @@ DerivedMesh *multires_make_derived_from_derived(DerivedMesh *dm, Mesh *me = ob->data; DerivedMesh *result; CCGDerivedMesh *ccgdm = NULL; - DMGridData **gridData, **subGridData; + CCGElem **gridData, **subGridData; + CCGKey key; int lvl = multires_get_level(ob, mmd, (flags & MULTIRES_USE_RENDER_PARAMS)); int i, gridSize, numGrids; @@ -1359,12 +1379,13 @@ DerivedMesh *multires_make_derived_from_derived(DerivedMesh *dm, numGrids = result->getNumGrids(result); gridSize = result->getGridSize(result); gridData = result->getGridData(result); + result->getGridKey(result, &key); - subGridData = MEM_callocN(sizeof(DMGridData *) * numGrids, "subGridData*"); + subGridData = MEM_callocN(sizeof(CCGElem*)*numGrids, "subGridData*"); for (i = 0; i < numGrids; i++) { - subGridData[i] = MEM_callocN(sizeof(DMGridData) * gridSize * gridSize, "subGridData"); - memcpy(subGridData[i], gridData[i], sizeof(DMGridData) * gridSize * gridSize); + subGridData[i] = MEM_callocN(key.elem_size*gridSize*gridSize, "subGridData"); + memcpy(subGridData[i], gridData[i], key.elem_size*gridSize*gridSize); } multires_set_tot_mdisps(me, mmd->totlvl); @@ -1679,26 +1700,29 @@ static void multires_mvert_to_ss(DerivedMesh *dm, MVert *mvert) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; - DMGridData *vd; + CCGElem *vd; + CCGKey key; int index; int totvert, totedge, totface; int gridSize = ccgSubSurf_getGridSize(ss); int edgeSize = ccgSubSurf_getEdgeSize(ss); int i = 0; + dm->getGridKey(dm, &key); + 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(vd->co, mvert[i].co); + copy_v3_v3(CCG_elem_co(&key, vd), mvert[i].co); 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(vd->co, mvert[i].co); + copy_v3_v3(CCG_elem_co(&key, vd), mvert[i].co); } } @@ -1706,7 +1730,7 @@ static void multires_mvert_to_ss(DerivedMesh *dm, MVert *mvert) 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(vd->co, mvert[i].co); + copy_v3_v3(CCG_elem_co(&key, vd), mvert[i].co); } } } @@ -1719,7 +1743,7 @@ static void multires_mvert_to_ss(DerivedMesh *dm, MVert *mvert) for (x = 1; x < edgeSize - 1; x++, i++) { vd = ccgSubSurf_getEdgeData(ss, e, x); - copy_v3_v3(vd->co, mvert[i].co); + copy_v3_v3(CCG_elem_co(&key, vd), mvert[i].co); } } @@ -1728,7 +1752,7 @@ static void multires_mvert_to_ss(DerivedMesh *dm, MVert *mvert) CCGVert *v = ccgdm->vertMap[index].vert; vd = ccgSubSurf_getVertData(ss, v); - copy_v3_v3(vd->co, mvert[i].co); + copy_v3_v3(CCG_elem_co(&key, vd), mvert[i].co); i++; } @@ -2041,8 +2065,9 @@ static void multires_sync_levels(Scene *scene, Object *ob, Object *to_ob) static void multires_apply_smat(Scene *scene, Object *ob, float smat[3][3]) { DerivedMesh *dm = NULL, *cddm = NULL, *subdm = NULL; - DMGridData **gridData, **subGridData; - Mesh *me = (Mesh *)ob->data; + CCGElem **gridData, **subGridData; + CCGKey key; + Mesh *me = (Mesh*)ob->data; MPoly *mpoly = me->mpoly; /* MLoop *mloop = me->mloop; */ /* UNUSED */ MDisps *mdisps; @@ -2079,10 +2104,11 @@ static void multires_apply_smat(Scene *scene, Object *ob, float smat[3][3]) dm = subsurf_dm_create_local(ob, cddm, high_mmd.totlvl, high_mmd.simple, 0, mmd->flags & eMultiresModifierFlag_PlainUv); cddm->release(cddm); - /*numGrids= dm->getNumGrids(dm);*/ /*UNUSED*/ + /*numGrids = dm->getNumGrids(dm);*/ /*UNUSED*/ gridSize = dm->getGridSize(dm); gridData = dm->getGridData(dm); gridOffset = dm->getGridOffset(dm); + dm->getGridKey(dm, &key); subGridData = subdm->getGridData(subdm); dGridSize = multires_side_tot[high_mmd.totlvl]; @@ -2095,23 +2121,23 @@ static void multires_apply_smat(Scene *scene, Object *ob, float smat[3][3]) int S, x, y, gIndex = gridOffset[i]; for (S = 0; S < numVerts; ++S, ++gIndex, mdisp++) { - DMGridData *grid = gridData[gIndex]; - DMGridData *subgrid = subGridData[gIndex]; + CCGElem *grid = gridData[gIndex]; + CCGElem *subgrid = subGridData[gIndex]; float (*dispgrid)[3] = mdisp->disps; for (y = 0; y < gridSize; y++) { for (x = 0; x < gridSize; x++) { - float *co = grid[x + y * gridSize].co; - float *sco = subgrid[x + y * gridSize].co; - float *no = grid[x + y * gridSize].no; - float *data = dispgrid[dGridSize * y * dSkip + x * dSkip]; + float *co = CCG_grid_elem_co(&key, grid, x, y); + float *sco = CCG_grid_elem_co(&key, subgrid, x, y); + float *no = CCG_grid_elem_no(&key, grid, x, y); + float *data = dispgrid[dGridSize*y*dSkip + x*dSkip]; float mat[3][3], tx[3], ty[3], disp[3]; /* construct tangent space matrix */ - grid_tangent(gridSize, gIndex, x, y, 0, gridData, tx); + grid_tangent(&key, x, y, 0, gridData[gIndex], tx); normalize_v3(tx); - grid_tangent(gridSize, gIndex, x, y, 1, gridData, ty); + grid_tangent(&key, x, y, 1, gridData[gIndex], ty); normalize_v3(ty); column_vectors_to_mat3(mat, tx, ty, no); diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c index e4fcd002822..732e7fe108a 100644 --- a/source/blender/blenkernel/intern/subsurf_ccg.c +++ b/source/blender/blenkernel/intern/subsurf_ccg.c @@ -52,6 +52,7 @@ #include "BLI_memarena.h" #include "BLI_pbvh.h" +#include "BKE_ccg.h" #include "BKE_cdderivedmesh.h" #include "BKE_global.h" #include "BKE_mesh.h" @@ -639,6 +640,16 @@ static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f) return ((int *) ccgSubSurf_getFaceUserData(ss, f))[1]; } +static void minmax_v3_v3v3(const float vec[3], float min[3], float max[3]) +{ + if(min[0] > vec[0]) min[0]= vec[0]; + if(min[1] > vec[1]) min[1]= vec[1]; + if(min[2] > vec[2]) min[2]= vec[2]; + if(max[0] < vec[0]) max[0]= vec[0]; + if(max[1] < vec[1]) max[1]= vec[1]; + if(max[2] < vec[2]) max[2]= vec[2]; +} + static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; @@ -646,9 +657,12 @@ static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) CCGVertIterator *vi = ccgSubSurf_getVertIterator(ss); CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss); CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss); + CCGKey key; int i, edgeSize = ccgSubSurf_getEdgeSize(ss); int gridSize = ccgSubSurf_getGridSize(ss); + CCG_key_top_level(&key, 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; @@ -656,15 +670,15 @@ static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) CCGVert *v = ccgVertIterator_getCurrent(vi); float *co = ccgSubSurf_getVertData(ss, v); - DO_MINMAX(co, min_r, max_r); + minmax_v3_v3v3(co, min_r, max_r); } for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + CCGElem *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); - for (i = 0; i < edgeSize; i++) - DO_MINMAX(edgeData[i].co, min_r, max_r); + for (i=0; i < edgeSize; i++) + minmax_v3_v3v3(CCG_elem_offset_co(&key, edgeData, i), min_r, max_r); } for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { @@ -672,11 +686,11 @@ static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); for (S = 0; S < numVerts; S++) { - DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + CCGElem *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); + minmax_v3_v3v3(CCG_grid_elem_co(&key, faceGridData, x, y), min_r, max_r); } } @@ -718,9 +732,11 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; - DMGridData *vd; + CCGElem *vd; + CCGKey key; int i; + CCG_key_top_level(&key, ss); memset(mv, 0, sizeof(*mv)); if ((vertNum < ccgdm->edgeMap[0].startVert) && (ccgSubSurf_getNumFaces(ss) > 0)) { @@ -752,16 +768,16 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) 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); + copy_v3_v3(mv->co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd)); } 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); + copy_v3_v3(mv->co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd)); } else if (offset < gridInternalEnd) { offset -= gridSideEnd; @@ -770,8 +786,8 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) 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); + copy_v3_v3(mv->co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd)); } } else if ((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) { @@ -789,8 +805,8 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) 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); + copy_v3_v3(mv->co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd)); } else { /* this vert comes from vert data */ @@ -799,8 +815,8 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) 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); + copy_v3_v3(mv->co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd)); } } @@ -1025,28 +1041,31 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; - DMGridData *vd; + CCGElem *vd; + CCGKey key; int index; int totvert, totedge, totface; int gridSize = ccgSubSurf_getGridSize(ss); int edgeSize = ccgSubSurf_getEdgeSize(ss); int i = 0; + CCG_key_top_level(&key, ss); + 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); + copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd)); 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); + copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd)); } } @@ -1054,8 +1073,8 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) 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); + copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd)); } } } @@ -1068,13 +1087,13 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) for (x = 1; x < edgeSize - 1; x++, i++) { vd = ccgSubSurf_getEdgeData(ss, e, x); - copy_v3_v3(mvert[i].co, vd->co); + copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd)); /* 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); + normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd)); } } @@ -1083,8 +1102,8 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) 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); + copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd)); + normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd)); i++; } } @@ -1406,14 +1425,16 @@ static void ccgDM_foreachMappedVert( { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGVertIterator *vi = ccgSubSurf_getVertIterator(ccgdm->ss); + CCGKey key; + CCG_key_top_level(&key, ccgdm->ss); for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) { CCGVert *v = ccgVertIterator_getCurrent(vi); - DMGridData *vd = ccgSubSurf_getVertData(ccgdm->ss, v); + CCGElem *vd = ccgSubSurf_getVertData(ccgdm->ss, v); int index = ccgDM_getVertMapIndex(ccgdm->ss, v); if (index != -1) - func(userData, index, vd->co, vd->no, NULL); + func(userData, index, CCG_elem_co(&key, vd), CCG_elem_no(&key, vd), NULL); } ccgVertIterator_free(vi); @@ -1427,16 +1448,19 @@ static void ccgDM_foreachMappedEdge( CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss); + CCGKey key; int i, edgeSize = ccgSubSurf_getEdgeSize(ss); + CCG_key_top_level(&key, ss); + for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + CCGElem *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); + func(userData, index, CCG_elem_offset_co(&key, edgeData, i), CCG_elem_offset_co(&key, edgeData, i + 1)); } } @@ -1508,18 +1532,20 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges, int drawAllEdge { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; int i, j, edgeSize = ccgSubSurf_getEdgeSize(ss); int totedge = ccgSubSurf_getNumEdges(ss); int gridSize = ccgSubSurf_getGridSize(ss); int useAging; + CCG_key_top_level(&key, ss); 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); + CCGElem *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e)) continue; @@ -1534,8 +1560,8 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges, int drawAllEdge glBegin(GL_LINE_STRIP); for (i = 0; i < edgeSize - 1; i++) { - glVertex3fv(edgeData[i].co); - glVertex3fv(edgeData[i + 1].co); + glVertex3fv(CCG_elem_offset_co(&key, edgeData, i)); + glVertex3fv(CCG_elem_offset_co(&key, edgeData, i + 1)); } glEnd(); } @@ -1552,22 +1578,22 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges, int drawAllEdge int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); for (S = 0; S < numVerts; S++) { - DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + CCGElem *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); glBegin(GL_LINE_STRIP); for (x = 0; x < gridSize; x++) - glVertex3fv(faceGridData[x].co); + glVertex3fv(CCG_elem_offset_co(&key, faceGridData, x)); glEnd(); for (y = 1; y < gridSize - 1; y++) { glBegin(GL_LINE_STRIP); for (x = 0; x < gridSize; x++) - glVertex3fv(faceGridData[y * gridSize + x].co); + glVertex3fv(CCG_grid_elem_co(&key, faceGridData, x, y)); glEnd(); } for (x = 1; x < gridSize - 1; x++) { glBegin(GL_LINE_STRIP); for (y = 0; y < gridSize; y++) - glVertex3fv(faceGridData[y * gridSize + x].co); + glVertex3fv(CCG_grid_elem_co(&key, faceGridData, x, y)); glEnd(); } } @@ -1579,18 +1605,21 @@ static void ccgDM_drawLooseEdges(DerivedMesh *dm) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; int totedge = ccgSubSurf_getNumEdges(ss); int i, j, edgeSize = ccgSubSurf_getEdgeSize(ss); + CCG_key_top_level(&key, ss); + for (j = 0; j < totedge; j++) { CCGEdge *e = ccgdm->edgeMap[j].edge; - DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + CCGElem *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); + glVertex3fv(CCG_elem_offset_co(&key, edgeData, i)); + glVertex3fv(CCG_elem_offset_co(&key, edgeData, i + 1)); } glEnd(); } @@ -1616,12 +1645,14 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; 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; + CCG_key_top_level(&key, ss); ccgdm_pbvh_update(ccgdm); if (ccgdm->pbvh && ccgdm->multires.mmd && !fast) { @@ -1661,19 +1692,19 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes) continue; for (S = 0; S < numVerts; S++) { - DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + CCGElem *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]; + CCGElem *a = CCG_grid_elem(&key, faceGridData, x, y + 0); + CCGElem *b = CCG_grid_elem(&key, faceGridData, x, y + step); - glNormal3fv(a->no); - glVertex3fv(a->co); - glNormal3fv(b->no); - glVertex3fv(b->co); + glNormal3fv(CCG_elem_no(&key, a)); + glVertex3fv(CCG_elem_co(&key, a)); + glNormal3fv(CCG_elem_no(&key, b)); + glVertex3fv(CCG_elem_co(&key, b)); } glEnd(); } @@ -1682,10 +1713,10 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes) 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; + float *a = CCG_grid_elem_co(&key, faceGridData, x, y + 0); + float *b = CCG_grid_elem_co(&key, faceGridData, x + step, y + 0); + float *c = CCG_grid_elem_co(&key, faceGridData, x + step, y + step); + float *d = CCG_grid_elem_co(&key, faceGridData, x, y + step); ccgDM_glNormalFast(a, b, c, d); @@ -1709,6 +1740,7 @@ static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; GPUVertexAttribs gattribs; DMVertexAttribs attribs = {{{NULL}}}; /* MTFace *tf = dm->getTessFaceDataArray(dm, CD_MTFACE); */ /* UNUSED */ @@ -1718,6 +1750,7 @@ static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, DMFlagMat *faceFlags = ccgdm->faceFlags; int a, b, i, doDraw, numVerts, matnr, new_matnr, totface; + CCG_key_top_level(&key, ss); ccgdm_pbvh_update(ccgdm); doDraw = 0; @@ -1775,40 +1808,40 @@ static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, continue; } - glShadeModel(drawSmooth ? GL_SMOOTH : GL_FLAT); + glShadeModel(drawSmooth ? GL_SMOOTH: GL_FLAT); for (S = 0; S < numVerts; S++) { - DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); - DMGridData *vda, *vdb; + CCGElem *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + CCGElem *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) * gridSize + x]; + vda = CCG_grid_elem(&key, faceGridData, x, y + 0); + vdb = CCG_grid_elem(&key, faceGridData, x, y + 1); PASSATTRIB(0, 0, 0); - glNormal3fv(vda->no); - glVertex3fv(vda->co); + glNormal3fv(CCG_elem_no(&key, vda)); + glVertex3fv(CCG_elem_co(&key, vda)); PASSATTRIB(0, 1, 1); - glNormal3fv(vdb->no); - glVertex3fv(vdb->co); + glNormal3fv(CCG_elem_no(&key, vdb)); + glVertex3fv(CCG_elem_co(&key, vdb)); if (x != gridFaces - 1) a++; } - vda = &faceGridData[(y + 0) * gridSize + x]; - vdb = &faceGridData[(y + 1) * gridSize + x]; + vda = CCG_grid_elem(&key, faceGridData, x, y + 0); + vdb = CCG_grid_elem(&key, faceGridData, x, y + 1); PASSATTRIB(0, 0, 3); - glNormal3fv(vda->no); - glVertex3fv(vda->co); + glNormal3fv(CCG_elem_no(&key, vda)); + glVertex3fv(CCG_elem_co(&key, vda)); PASSATTRIB(0, 1, 2); - glNormal3fv(vdb->no); - glVertex3fv(vdb->co); + glNormal3fv(CCG_elem_no(&key, vdb)); + glVertex3fv(CCG_elem_co(&key, vdb)); glEnd(); @@ -1819,10 +1852,10 @@ static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, glBegin(GL_QUADS); for (y = 0; y < gridFaces; y++) { for (x = 0; x < gridFaces; x++) { - float *aco = faceGridData[(y + 0) * gridSize + x].co; - float *bco = faceGridData[(y + 0) * gridSize + x + 1].co; - float *cco = faceGridData[(y + 1) * gridSize + x + 1].co; - float *dco = faceGridData[(y + 1) * gridSize + x].co; + float *aco = CCG_grid_elem_co(&key, faceGridData, x, y); + float *bco = CCG_grid_elem_co(&key, faceGridData, x + 1, y); + float *cco = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 1); + float *dco = CCG_grid_elem_co(&key, faceGridData, x, y + 1); ccgDM_glNormalFast(aco, bco, cco, dco); @@ -1856,6 +1889,7 @@ static void ccgDM_drawMappedFacesMat(DerivedMesh *dm, void (*setMaterial)(void * { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; GPUVertexAttribs gattribs; DMVertexAttribs attribs = {{{NULL}}}; int gridSize = ccgSubSurf_getGridSize(ss); @@ -1864,6 +1898,7 @@ static void ccgDM_drawMappedFacesMat(DerivedMesh *dm, void (*setMaterial)(void * DMFlagMat *faceFlags = ccgdm->faceFlags; int a, b, i, numVerts, matnr, new_matnr, totface; + CCG_key_top_level(&key, ss); ccgdm_pbvh_update(ccgdm); matnr = -1; @@ -1927,40 +1962,40 @@ static void ccgDM_drawMappedFacesMat(DerivedMesh *dm, void (*setMaterial)(void * } /* draw face*/ - glShadeModel(drawSmooth ? GL_SMOOTH : GL_FLAT); + glShadeModel(drawSmooth ? GL_SMOOTH: GL_FLAT); for (S = 0; S < numVerts; S++) { - DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); - DMGridData *vda, *vdb; + CCGElem *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + CCGElem *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) * gridSize + x]; + vda = CCG_grid_elem(&key, faceGridData, x, y); + vdb = CCG_grid_elem(&key, faceGridData, x, y + 1); PASSATTRIB(0, 0, 0); - glNormal3fv(vda->no); - glVertex3fv(vda->co); + glNormal3fv(CCG_elem_no(&key, vda)); + glVertex3fv(CCG_elem_co(&key, vda)); PASSATTRIB(0, 1, 1); - glNormal3fv(vdb->no); - glVertex3fv(vdb->co); + glNormal3fv(CCG_elem_no(&key, vdb)); + glVertex3fv(CCG_elem_co(&key, vdb)); if (x != gridFaces - 1) a++; } - vda = &faceGridData[(y + 0) * gridSize + x]; - vdb = &faceGridData[(y + 1) * gridSize + x]; + vda = CCG_grid_elem(&key, faceGridData, x, y + 0); + vdb = CCG_grid_elem(&key, faceGridData, x, y + 1); PASSATTRIB(0, 0, 3); - glNormal3fv(vda->no); - glVertex3fv(vda->co); + glNormal3fv(CCG_elem_no(&key, vda)); + glVertex3fv(CCG_elem_co(&key, vda)); PASSATTRIB(0, 1, 2); - glNormal3fv(vdb->no); - glVertex3fv(vdb->co); + glNormal3fv(CCG_elem_no(&key, vdb)); + glVertex3fv(CCG_elem_co(&key, vdb)); glEnd(); @@ -1969,12 +2004,12 @@ static void ccgDM_drawMappedFacesMat(DerivedMesh *dm, void (*setMaterial)(void * } else { glBegin(GL_QUADS); - for (y = 0; y < gridFaces; y++) { - for (x = 0; x < gridFaces; x++) { - float *aco = faceGridData[(y + 0) * gridSize + x].co; - float *bco = faceGridData[(y + 0) * gridSize + x + 1].co; - float *cco = faceGridData[(y + 1) * gridSize + x + 1].co; - float *dco = faceGridData[(y + 1) * gridSize + x].co; + for (y=0; y < gridFaces; y++) { + for (x=0; x < gridFaces; x++) { + float *aco = CCG_grid_elem_co(&key, faceGridData, x, y + 0); + float *bco = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 0); + float *cco = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 1); + float *dco = CCG_grid_elem_co(&key, faceGridData, x, y + 1); ccgDM_glNormalFast(aco, bco, cco, dco); @@ -2006,6 +2041,7 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; MCol *mcol = dm->getTessFaceDataArray(dm, CD_PREVIEW_MCOL); MTFace *tf = DM_get_tessface_data_layer(dm, CD_MTFACE); DMFlagMat *faceFlags = ccgdm->faceFlags; @@ -2015,6 +2051,7 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, (void) compareDrawOptions; + CCG_key_top_level(&key, ss); ccgdm_pbvh_update(ccgdm); if (!mcol) @@ -2063,26 +2100,26 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, } for (S = 0; S < numVerts; S++) { - DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); - DMGridData *a, *b; + CCGElem *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + CCGElem *a, *b; if (drawSmooth) { glShadeModel(GL_SMOOTH); for (y = 0; y < gridFaces; y++) { glBegin(GL_QUAD_STRIP); - for (x = 0; x < gridFaces; x++) { - a = &faceGridData[(y + 0) * gridSize + x]; - b = &faceGridData[(y + 1) * gridSize + x]; + for (x=0; x<gridFaces; x++) { + a = CCG_grid_elem(&key, faceGridData, x, y + 0); + b = CCG_grid_elem(&key, faceGridData, x, y + 1); if (tf) glTexCoord2fv(tf->uv[0]); if (cp) glColor3ub(cp[3], cp[2], cp[1]); - glNormal3fv(a->no); - glVertex3fv(a->co); + glNormal3fv(CCG_elem_no(&key, a)); + glVertex3fv(CCG_elem_co(&key, a)); if (tf) glTexCoord2fv(tf->uv[1]); if (cp) glColor3ub(cp[7], cp[6], cp[5]); - glNormal3fv(b->no); - glVertex3fv(b->co); + glNormal3fv(CCG_elem_no(&key, b)); + glVertex3fv(CCG_elem_co(&key, b)); if (x != gridFaces - 1) { if (tf) tf++; @@ -2090,18 +2127,18 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, } } - a = &faceGridData[(y + 0) * gridSize + x]; - b = &faceGridData[(y + 1) * gridSize + x]; + a = CCG_grid_elem(&key, faceGridData, x, y + 0); + b = CCG_grid_elem(&key, faceGridData, x, y + 1); if (tf) glTexCoord2fv(tf->uv[3]); if (cp) glColor3ub(cp[15], cp[14], cp[13]); - glNormal3fv(a->no); - glVertex3fv(a->co); + glNormal3fv(CCG_elem_no(&key, a)); + glVertex3fv(CCG_elem_co(&key, a)); if (tf) glTexCoord2fv(tf->uv[2]); if (cp) glColor3ub(cp[11], cp[10], cp[9]); - glNormal3fv(b->no); - glVertex3fv(b->co); + glNormal3fv(CCG_elem_no(&key, b)); + glVertex3fv(CCG_elem_co(&key, b)); if (tf) tf++; if (cp) cp += 16; @@ -2114,10 +2151,10 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, glBegin(GL_QUADS); for (y = 0; y < gridFaces; y++) { for (x = 0; x < gridFaces; x++) { - float *a_co = faceGridData[(y + 0) * gridSize + x].co; - float *b_co = faceGridData[(y + 0) * gridSize + x + 1].co; - float *c_co = faceGridData[(y + 1) * gridSize + x + 1].co; - float *d_co = faceGridData[(y + 1) * gridSize + x].co; + float *a_co = CCG_grid_elem_co(&key, faceGridData, x, y + 0); + float *b_co = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 0); + float *c_co = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 1); + float *d_co = CCG_grid_elem_co(&key, faceGridData, x, y + 1); ccgDM_glNormalFast(a_co, b_co, c_co, d_co); @@ -2205,12 +2242,15 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; MCol *mcol = NULL; int i, gridSize = ccgSubSurf_getGridSize(ss); DMFlagMat *faceFlags = ccgdm->faceFlags; int useColors = flag & DM_DRAW_USE_COLORS; int gridFaces = gridSize - 1, totface; + CCG_key_top_level(&key, ss); + /* currently unused -- each original face is handled separately */ (void)compareDrawOptions; @@ -2257,37 +2297,37 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, * normals are already used to change shading */ glShadeModel(GL_SMOOTH); - for (S = 0; S < numVerts; S++) { - DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + for (S=0; S<numVerts; S++) { + CCGElem *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); if (drawSmooth) { for (y = 0; y < gridFaces; y++) { - DMGridData *a, *b; + CCGElem *a, *b; glBegin(GL_QUAD_STRIP); for (x = 0; x < gridFaces; x++) { - a = &faceGridData[(y + 0) * gridSize + x]; - b = &faceGridData[(y + 1) * gridSize + x]; + a = CCG_grid_elem(&key, faceGridData, x, y + 0); + b = CCG_grid_elem(&key, faceGridData, x, y + 1); if (cp) glColor3ub(cp[3], cp[2], cp[1]); - glNormal3fv(a->no); - glVertex3fv(a->co); + glNormal3fv(CCG_elem_no(&key, a)); + glVertex3fv(CCG_elem_co(&key, a)); if (cp) glColor3ub(cp[7], cp[6], cp[5]); - glNormal3fv(b->no); - glVertex3fv(b->co); + glNormal3fv(CCG_elem_no(&key, b)); + glVertex3fv(CCG_elem_co(&key, b)); if (x != gridFaces - 1) { if (cp) cp += 16; } } - a = &faceGridData[(y + 0) * gridSize + x]; - b = &faceGridData[(y + 1) * gridSize + x]; + a = CCG_grid_elem(&key, faceGridData, x, y + 0); + b = CCG_grid_elem(&key, faceGridData, x, y + 1); if (cp) glColor3ub(cp[15], cp[14], cp[13]); - glNormal3fv(a->no); - glVertex3fv(a->co); + glNormal3fv(CCG_elem_no(&key, a)); + glVertex3fv(CCG_elem_co(&key, a)); if (cp) glColor3ub(cp[11], cp[10], cp[9]); - glNormal3fv(b->no); - glVertex3fv(b->co); + glNormal3fv(CCG_elem_no(&key, b)); + glVertex3fv(CCG_elem_co(&key, b)); if (cp) cp += 16; @@ -2298,10 +2338,10 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, glBegin(GL_QUADS); for (y = 0; y < gridFaces; y++) { for (x = 0; x < gridFaces; x++) { - float *a = faceGridData[(y + 0) * gridSize + x].co; - float *b = faceGridData[(y + 0) * gridSize + x + 1].co; - float *c = faceGridData[(y + 1) * gridSize + x + 1].co; - float *d = faceGridData[(y + 1) * gridSize + x].co; + float *a = CCG_grid_elem_co(&key, faceGridData, x, y + 0); + float *b = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 0); + float *c = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 1); + float *d = CCG_grid_elem_co(&key, faceGridData, x, y + 1); ccgDM_glNormalFast(a, b, c, d); @@ -2334,13 +2374,15 @@ static void ccgDM_drawMappedEdges(DerivedMesh *dm, CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss); + CCGKey key; int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss); + CCG_key_top_level(&key, ss); ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL); for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + CCGElem *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); int index = ccgDM_getEdgeMapIndex(ss, e); glBegin(GL_LINE_STRIP); @@ -2351,8 +2393,8 @@ static void ccgDM_drawMappedEdges(DerivedMesh *dm, } for (i = 0; i < edgeSize - 1; i++) { - glVertex3fv(edgeData[i].co); - glVertex3fv(edgeData[i + 1].co); + glVertex3fv(CCG_elem_offset_co(&key, edgeData, i)); + glVertex3fv(CCG_elem_offset_co(&key, edgeData, i + 1)); } } glEnd(); @@ -2368,14 +2410,16 @@ static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm, { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss); int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss); + CCG_key_top_level(&key, ss); ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL); for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + CCGElem *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); int index = ccgDM_getEdgeMapIndex(ss, e); glBegin(GL_LINE_STRIP); @@ -2388,7 +2432,7 @@ static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm, glColor3ub(0, ageCol > 0 ? ageCol : 0, 0); } - glVertex3fv(edgeData[i].co); + glVertex3fv(CCG_elem_offset_co(&key, edgeData, i)); } } glEnd(); @@ -2404,17 +2448,20 @@ static void ccgDM_foreachMappedFaceCenter( { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm; CCGSubSurf *ss = ccgdm->ss; + CCGKey key; CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss); + CCG_key_top_level(&key, ss); + for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); int index = ccgDM_getFaceMapIndex(ss, f); if (index != -1) { - /* Face center data normal isn't updated atm. */ - DMGridData *vd = ccgSubSurf_getFaceGridData(ss, f, 0, 0, 0); + /* Face center data normal isn't updated atm. */ + CCGElem *vd = ccgSubSurf_getFaceGridData(ss, f, 0, 0, 0); - func(userData, index, vd->co, vd->no); + func(userData, index, CCG_elem_co(&key, vd), CCG_elem_no(&key, vd)); } } @@ -2727,7 +2774,7 @@ static void ccgdm_create_grids(DerivedMesh *dm) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm; CCGSubSurf *ss = ccgdm->ss; - DMGridData **gridData; + CCGElem **gridData; DMGridAdjacency *gridAdjacency, *adj; DMFlagMat *gridFlagMats; CCGFace **gridFaces; @@ -2753,7 +2800,7 @@ static void ccgdm_create_grids(DerivedMesh *dm) } /* compute grid data */ - gridData = MEM_mallocN(sizeof(DMGridData *) * numGrids, "ccgdm.gridData"); + gridData = MEM_mallocN(sizeof(CCGElem *) * numGrids, "ccgdm.gridData"); gridAdjacency = MEM_mallocN(sizeof(DMGridAdjacency) * numGrids, "ccgdm.gridAdjacency"); gridFaces = MEM_mallocN(sizeof(CCGFace *) * numGrids, "ccgdm.gridFaces"); gridFlagMats = MEM_mallocN(sizeof(DMFlagMat) * numGrids, "ccgdm.gridFlagMats"); @@ -2792,7 +2839,7 @@ static void ccgdm_create_grids(DerivedMesh *dm) ccgdm->gridFlagMats = gridFlagMats; } -static DMGridData **ccgDM_getGridData(DerivedMesh *dm) +static CCGElem **ccgDM_getGridData(DerivedMesh *dm) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm; @@ -2816,6 +2863,12 @@ static int *ccgDM_getGridOffset(DerivedMesh *dm) return ccgdm->gridOffset; } +static void ccgDM_getGridKey(DerivedMesh *dm, CCGKey *key) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + CCG_key_top_level(key, ccgdm->ss); +} + static DMFlagMat *ccgDM_getGridFlagMats(DerivedMesh *dm) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm; @@ -2862,7 +2915,10 @@ static int ccgDM_use_grid_pbvh(CCGDerivedMesh *ccgdm) static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm; - int gridSize, numGrids, grid_pbvh; + CCGKey key; + int numGrids, grid_pbvh; + + CCG_key_top_level(&key, ccgdm->ss); if (!ob) { ccgdm->pbvh = NULL; @@ -2896,16 +2952,15 @@ static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm) if (grid_pbvh) { ccgdm_create_grids(dm); - gridSize = ccgDM_getGridSize(dm); numGrids = ccgDM_getNumGrids(dm); ob->sculpt->pbvh = ccgdm->pbvh = BLI_pbvh_new(); BLI_pbvh_build_grids(ccgdm->pbvh, ccgdm->gridData, ccgdm->gridAdjacency, - numGrids, gridSize, (void **)ccgdm->gridFaces, ccgdm->gridFlagMats, ccgdm->gridHidden); + numGrids, &key, (void **) ccgdm->gridFaces, ccgdm->gridFlagMats, ccgdm->gridHidden); } - else if (ob->type == OB_MESH) { - Mesh *me = ob->data; - ob->sculpt->pbvh = ccgdm->pbvh = BLI_pbvh_new(); + else if(ob->type == OB_MESH) { + Mesh *me= ob->data; + ob->sculpt->pbvh= ccgdm->pbvh = BLI_pbvh_new(); BLI_assert(!(me->mface == NULL && me->mpoly != NULL)); /* BMESH ONLY complain if mpoly is valid but not mface */ BLI_pbvh_build_mesh(ccgdm->pbvh, me->mface, me->mvert, me->totface, me->totvert); @@ -3022,6 +3077,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, ccgdm->dm.getGridData = ccgDM_getGridData; ccgdm->dm.getGridAdjacency = ccgDM_getGridAdjacency; ccgdm->dm.getGridOffset = ccgDM_getGridOffset; + ccgdm->dm.getGridKey = ccgDM_getGridKey; ccgdm->dm.getGridFlagMats = ccgDM_getGridFlagMats; ccgdm->dm.getGridHidden = ccgDM_getGridHidden; ccgdm->dm.getPolyMap = ccgDM_getPolyMap; |