diff options
Diffstat (limited to 'source/blender')
73 files changed, 7055 insertions, 3688 deletions
diff --git a/source/blender/blenkernel/BKE_DerivedMesh.h b/source/blender/blenkernel/BKE_DerivedMesh.h index 514411e137d..a3a1ead3924 100644 --- a/source/blender/blenkernel/BKE_DerivedMesh.h +++ b/source/blender/blenkernel/BKE_DerivedMesh.h @@ -60,12 +60,24 @@ struct MCol; struct ColorBand; struct GPUVertexAttribs; struct GPUDrawObject; +struct ListBase; +struct PBVH; /* number of sub-elements each mesh element has (for interpolation) */ #define SUB_ELEMS_VERT 0 #define SUB_ELEMS_EDGE 2 #define SUB_ELEMS_FACE 4 +typedef struct DMGridData { + float co[3]; + float no[3]; +} DMGridData; + +typedef struct DMGridAdjacency { + int index[4]; + int rotation[4]; +} DMGridAdjacency; + typedef struct DerivedMesh DerivedMesh; struct DerivedMesh { /* Private DerivedMesh data, only for internal DerivedMesh use */ @@ -133,6 +145,13 @@ struct DerivedMesh { void *(*getEdgeDataArray)(DerivedMesh *dm, int type); void *(*getFaceDataArray)(DerivedMesh *dm, int type); + /* optional grid access for subsurf */ + int (*getNumGrids)(DerivedMesh *dm); + int (*getGridSize)(DerivedMesh *dm); + DMGridData **(*getGridData)(DerivedMesh *dm); + DMGridAdjacency *(*getGridAdjacency)(DerivedMesh *dm); + int *(*getGridOffset)(DerivedMesh *dm); + /* Iterate over each mapped vertex in the derived mesh, calling the * given function with the original vert and the mapped vert's new * coordinate and normal. For historical reasons the normal can be @@ -181,6 +200,14 @@ struct DerivedMesh { /* Get vertex normal, undefined if index is not valid */ void (*getVertNo)(DerivedMesh *dm, int index, float no_r[3]); + /* Get a map of vertices to faces + */ + struct ListBase *(*getFaceMap)(DerivedMesh *dm); + + /* Get the BVH used for paint modes + */ + struct PBVH *(*getPBVH)(struct Object *ob, DerivedMesh *dm); + /* Drawing Operations */ /* Draw all vertices as bgl points (no options) */ @@ -205,8 +232,8 @@ struct DerivedMesh { * * Also called for *final* editmode DerivedMeshes */ - void (*drawFacesSolid)(DerivedMesh *dm, - int (*setMaterial)(int, void *attribs)); + void (*drawFacesSolid)(DerivedMesh *dm, float (*partial_redraw_planes)[4], + int fast, int (*setMaterial)(int, void *attribs)); /* Draw all faces * o If useTwoSided, draw front and back using col arrays diff --git a/source/blender/blenkernel/BKE_blender.h b/source/blender/blenkernel/BKE_blender.h index 7bbcb63a7f5..08503a3d00c 100644 --- a/source/blender/blenkernel/BKE_blender.h +++ b/source/blender/blenkernel/BKE_blender.h @@ -43,7 +43,7 @@ struct bContext; struct ReportList; #define BLENDER_VERSION 250 -#define BLENDER_SUBVERSION 8 +#define BLENDER_SUBVERSION 9 #define BLENDER_MINVERSION 250 #define BLENDER_MINSUBVERSION 0 diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h index 95ee918a888..5f13e702343 100644 --- a/source/blender/blenkernel/BKE_customdata.h +++ b/source/blender/blenkernel/BKE_customdata.h @@ -32,6 +32,7 @@ #ifndef BKE_CUSTOMDATA_H #define BKE_CUSTOMDATA_H +struct ID; struct CustomData; struct CustomDataLayer; typedef unsigned int CustomDataMask; @@ -278,4 +279,19 @@ int CustomData_verify_versions(struct CustomData *data, int index); void CustomData_to_bmeshpoly(struct CustomData *fdata, struct CustomData *pdata, struct CustomData *ldata); void CustomData_from_bmeshpoly(struct CustomData *fdata, struct CustomData *pdata, struct CustomData *ldata, int total); void CustomData_bmesh_init_pool(struct CustomData *data, int allocsize); + +/* External file storage */ + +void CustomData_external_add(struct CustomData *data, + struct ID *id, int type, int totelem, const char *filename); +void CustomData_external_remove(struct CustomData *data, + struct ID *id, int type, int totelem); +int CustomData_external_test(struct CustomData *data, int type); + +void CustomData_external_write(struct CustomData *data, + struct ID *id, CustomDataMask mask, int totelem, int free); +void CustomData_external_read(struct CustomData *data, + struct ID *id, CustomDataMask mask, int totelem); + #endif + diff --git a/source/blender/blenkernel/BKE_customdata_file.h b/source/blender/blenkernel/BKE_customdata_file.h new file mode 100644 index 00000000000..5cbff193cd3 --- /dev/null +++ b/source/blender/blenkernel/BKE_customdata_file.h @@ -0,0 +1,59 @@ +/* + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef BKE_CUSTOMDATA_FILE_H +#define BKE_CUSTOMDATA_FILE_H + +#define CDF_TYPE_IMAGE 0 +#define CDF_TYPE_MESH 1 + +#define CDF_LAYER_NAME_MAX 64 + +typedef struct CDataFile CDataFile; +typedef struct CDataFileLayer CDataFileLayer; + +/* Create/Free */ + +CDataFile *cdf_create(int type); +void cdf_free(CDataFile *cdf); + +/* File read/write/remove */ + +int cdf_read_open(CDataFile *cdf, char *filename); +int cdf_read_layer(CDataFile *cdf, CDataFileLayer *blay); +int cdf_read_data(CDataFile *cdf, int size, void *data); +void cdf_read_close(CDataFile *cdf); + +int cdf_write_open(CDataFile *cdf, char *filename); +int cdf_write_layer(CDataFile *cdf, CDataFileLayer *blay); +int cdf_write_data(CDataFile *cdf, int size, void *data); +void cdf_write_close(CDataFile *cdf); + +void cdf_remove(char *filename); + +/* Layers */ + +CDataFileLayer *cdf_layer_find(CDataFile *cdf, int type, char *name); +CDataFileLayer *cdf_layer_add(CDataFile *cdf, int type, char *name, size_t datasize); + +#endif /* BKE_CUSTOMDATA_FILE_H */ + diff --git a/source/blender/blenkernel/BKE_modifier.h b/source/blender/blenkernel/BKE_modifier.h index 539c2e59dbe..dd5f391cc5c 100644 --- a/source/blender/blenkernel/BKE_modifier.h +++ b/source/blender/blenkernel/BKE_modifier.h @@ -228,7 +228,7 @@ typedef struct ModifierTypeInfo { * * This function is optional (assumes never disabled if not present). */ - int (*isDisabled)(struct ModifierData *md); + int (*isDisabled)(struct ModifierData *md, int userRenderParams); /* Add the appropriate relations to the DEP graph depending on the * modifier data. diff --git a/source/blender/blenkernel/BKE_multires.h b/source/blender/blenkernel/BKE_multires.h index a331479cad1..2f372f80957 100644 --- a/source/blender/blenkernel/BKE_multires.h +++ b/source/blender/blenkernel/BKE_multires.h @@ -27,51 +27,36 @@ * ***** END GPL LICENSE BLOCK ***** */ +#ifndef BKE_MULTIRES_H +#define BKE_MULTIRES_H + struct DerivedMesh; struct Mesh; struct MFace; +struct Multires; struct MultiresModifierData; struct Object; -typedef struct MultiresSubsurf { - struct MultiresModifierData *mmd; - struct Object *ob; - int local_mmd; -} MultiresSubsurf; - -/* MultiresDM */ -struct Object *MultiresDM_get_object(struct DerivedMesh *dm); -struct Mesh *MultiresDM_get_mesh(struct DerivedMesh *dm); -struct DerivedMesh *MultiresDM_new(struct MultiresSubsurf *, struct DerivedMesh*, int, int, int); -void *MultiresDM_get_vertnorm(struct DerivedMesh *); -void *MultiresDM_get_orco(struct DerivedMesh *); -struct MVert *MultiresDM_get_subco(struct DerivedMesh *); -struct ListBase *MultiresDM_get_vert_face_map(struct DerivedMesh *); -struct ListBase *MultiresDM_get_vert_edge_map(struct DerivedMesh *); -int *MultiresDM_get_face_offsets(struct DerivedMesh *); -int MultiresDM_get_totlvl(struct DerivedMesh *); -int MultiresDM_get_lvl(struct DerivedMesh *); -void MultiresDM_set_update(struct DerivedMesh *, void (*)(struct DerivedMesh*)); - -/* The displacements will only be updated when - the MultiresDM has been marked as modified */ -void MultiresDM_mark_as_modified(struct DerivedMesh *); void multires_mark_as_modified(struct Object *ob); void multires_force_update(struct Object *ob); -struct DerivedMesh *multires_dm_create_from_derived(struct MultiresModifierData*, int local_mmd, struct DerivedMesh*, - struct Object *, int, int); +struct DerivedMesh *multires_dm_create_from_derived(struct MultiresModifierData*, + int local_mmd, struct DerivedMesh*, struct Object *, int, int); struct MultiresModifierData *find_multires_modifier(struct Object *ob); -int multiresModifier_switch_level(struct Object *, const int); void multiresModifier_join(struct Object *); void multiresModifier_del_levels(struct MultiresModifierData *, struct Object *, int direction); -void multiresModifier_subdivide(struct MultiresModifierData *mmd, struct Object *ob, int distance, +void multiresModifier_subdivide(struct MultiresModifierData *mmd, struct Object *ob, int updateblock, int simple); int multiresModifier_reshape(struct MultiresModifierData *mmd, struct Object *dst, struct Object *src); +void multires_stitch_grids(struct Object *); + /* Related to the old multires */ -struct Multires; -void multires_load_old(struct DerivedMesh *, struct Multires *); -void multires_free(struct Multires*); +void multires_free(struct Multires *mr); +void multires_load_old(struct Object *ob, struct Mesh *me); +void multires_load_old_250(struct Mesh *); + +#endif + diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h index b532b0820d7..1688ecb3492 100644 --- a/source/blender/blenkernel/BKE_paint.h +++ b/source/blender/blenkernel/BKE_paint.h @@ -34,6 +34,7 @@ struct MultireModifierData; struct MVert; struct Object; struct Paint; +struct PBVH; struct Scene; struct StrokeCache; @@ -68,25 +69,24 @@ typedef struct SculptSession { struct MFace *mface; int totvert, totface; float *face_normals; - + struct PBVH *tree; struct Object *ob; struct KeyBlock *kb, *refkb; /* Mesh connectivity */ struct ListBase *fmap; - struct IndexNode *fmap_mem; - int fmap_size; /* Used temporarily per-stroke */ float *vertexcosnos; - ListBase damaged_rects; - ListBase damaged_verts; + + /* Partial redraw */ + int partial_redraw; /* Used to cache the render of the active texture */ unsigned int texcache_side, *texcache, texcache_actual; /* Layer brush persistence between strokes */ - float (*mesh_co_orig)[3]; /* Copy of the mesh vertices' locations */ + float (*layer_co)[3]; /* Copy of the mesh vertices' locations */ float *layer_disps; /* Displacements for each vertex */ struct SculptStroke *stroke; diff --git a/source/blender/blenkernel/BKE_subsurf.h b/source/blender/blenkernel/BKE_subsurf.h index f6dc22f650a..0a18850e3b7 100644 --- a/source/blender/blenkernel/BKE_subsurf.h +++ b/source/blender/blenkernel/BKE_subsurf.h @@ -34,21 +34,61 @@ struct DerivedMesh; struct EditMesh; struct MultiresSubsurf; struct SubsurfModifierData; +struct _CCGSubsurf; +struct _CCGVert; +struct _CCGEdge; +struct _CCGFace; +struct PBVH; +struct DMGridData; +struct DMGridAdjacency; -struct DerivedMesh *subsurf_make_derived_from_derived( - struct DerivedMesh *dm, - struct SubsurfModifierData *smd, - int useRenderParams, float (*vertCos)[3], - int isFinalCalc, int editMode); +/**************************** External *****************************/ -struct DerivedMesh *subsurf_make_derived_from_derived_with_multires( +struct DerivedMesh *subsurf_make_derived_from_derived( struct DerivedMesh *dm, struct SubsurfModifierData *smd, - struct MultiresSubsurf *ms, int useRenderParams, float (*vertCos)[3], int isFinalCalc, int editMode); void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]); +/**************************** Internal *****************************/ + +typedef struct CCGDerivedMesh { + DerivedMesh dm; + + struct _CCGSubSurf *ss; + int freeSS; + int drawInteriorEdges, useSubsurfUv; + + struct {int startVert; struct _CCGVert *vert;} *vertMap; + struct {int startVert; int startEdge; struct _CCGEdge *edge;} *edgeMap; + struct {int startVert; int startEdge; + int startFace; struct _CCGFace *face;} *faceMap; + + short *edgeFlags; + char *faceFlags; + + struct PBVH *pbvh; + + struct DMGridData **gridData; + struct DMGridAdjacency *gridAdjacency; + int *gridOffset; + struct _CCGFace **gridFaces; + + struct { + struct MultiresModifierData *mmd; + int local_mmd; + + int lvl, totlvl; + float (*orco)[3]; + + Object *ob; + int modified; + + void (*update)(DerivedMesh*); + } multires; +} CCGDerivedMesh; + #endif diff --git a/source/blender/blenkernel/BKE_utildefines.h b/source/blender/blenkernel/BKE_utildefines.h index c0ed8430177..c052af775f2 100644 --- a/source/blender/blenkernel/BKE_utildefines.h +++ b/source/blender/blenkernel/BKE_utildefines.h @@ -168,7 +168,15 @@ #define ENDB MAKE_ID('E','N','D','B') -/* This one rotates the bytes in an int */ +/* This one rotates the bytes in an int64, int (32) and short (16) */ +#define SWITCH_INT64(a) { \ + char s_i, *p_i; \ + p_i= (char *)&(a); \ + s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \ + s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \ + s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \ + s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; } + #define SWITCH_INT(a) { \ char s_i, *p_i; \ p_i= (char *)&(a); \ diff --git a/source/blender/blenkernel/intern/CCGSubSurf.c b/source/blender/blenkernel/intern/CCGSubSurf.c index 355ea70b178..dc863869ad8 100644 --- a/source/blender/blenkernel/intern/CCGSubSurf.c +++ b/source/blender/blenkernel/intern/CCGSubSurf.c @@ -8,6 +8,12 @@ #include "BLO_sys_types.h" // for intptr_t support +#ifdef _MSC_VER +#define CCG_INLINE __inline +#else +#define CCG_INLINE inline +#endif + /* used for normalize_v3 in BLI_math_vector * float.h's FLT_EPSILON causes trouble with subsurf normals - campbell */ #define EPSILON (1.0e-35f) @@ -523,19 +529,19 @@ static CCGFace *_face_new(CCGFaceHDL fHDL, CCGVert **verts, CCGEdge **edges, int return f; } -static void *_face_getIECo(CCGFace *f, int lvl, int S, int x, int levels, int dataSize) { +static CCG_INLINE void *_face_getIECo(CCGFace *f, int lvl, int S, int x, int levels, int dataSize) { int maxGridSize = 1 + (1<<(levels-1)); int spacing = 1<<(levels-lvl); byte *gridBase = FACE_getCenterData(f) + dataSize*(1 + S*(maxGridSize + maxGridSize*maxGridSize)); return &gridBase[dataSize*x*spacing]; } -static void *_face_getIFCo(CCGFace *f, int lvl, int S, int x, int y, int levels, int dataSize) { +static CCG_INLINE void *_face_getIFCo(CCGFace *f, int lvl, int S, int x, int y, int levels, int dataSize) { int maxGridSize = 1 + (1<<(levels-1)); int spacing = 1<<(levels-lvl); byte *gridBase = FACE_getCenterData(f) + dataSize*(1 + S*(maxGridSize + maxGridSize*maxGridSize)); return &gridBase[dataSize*(maxGridSize + (y*maxGridSize + x)*spacing)]; } -static float *_face_getIFNo(CCGFace *f, int lvl, int S, int x, int y, int levels, int dataSize, int normalDataOffset) { +static CCG_INLINE float *_face_getIFNo(CCGFace *f, int lvl, int S, int x, int y, int levels, int dataSize, int normalDataOffset) { int maxGridSize = 1 + (1<<(levels-1)); int spacing = 1<<(levels-lvl); byte *gridBase = FACE_getCenterData(f) + dataSize*(1 + S*(maxGridSize + maxGridSize*maxGridSize)); @@ -548,7 +554,7 @@ static int _face_getVertIndex(CCGFace *f, CCGVert *v) { return i; return -1; } -static void *_face_getIFCoEdge(CCGFace *f, CCGEdge *e, int lvl, int eX, int eY, int levels, int dataSize) { +static CCG_INLINE void *_face_getIFCoEdge(CCGFace *f, CCGEdge *e, int lvl, int eX, int eY, int levels, int dataSize) { int maxGridSize = 1 + (1<<(levels-1)); int spacing = 1<<(levels-lvl); int S, x, y, cx, cy; @@ -1133,6 +1139,666 @@ CCGError ccgSubSurf_processSync(CCGSubSurf *ss) { return eCCGError_None; } +#define FACE_getIFNo(f, lvl, S, x, y) _face_getIFNo(f, lvl, S, x, y, subdivLevels, vertDataSize, normalDataOffset) +#define FACE_calcIFNo(f, lvl, S, x, y, no) _face_calcIFNo(f, lvl, S, x, y, no, subdivLevels, vertDataSize) +static void ccgSubSurf__calcVertNormals(CCGSubSurf *ss, + CCGVert **effectedV, CCGEdge **effectedE, CCGFace **effectedF, + int numEffectedV, int numEffectedE, int numEffectedF) { + int i,ptrIdx; + int subdivLevels = ss->subdivLevels; + int lvl = ss->subdivLevels; + int edgeSize = 1 + (1<<lvl); + int gridSize = 1 + (1<<(lvl-1)); + int normalDataOffset = ss->normalDataOffset; + int vertDataSize = ss->meshIFC.vertDataSize; + + #pragma omp parallel for private(ptrIdx) schedule(static) + for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { + CCGFace *f = (CCGFace*) effectedF[ptrIdx]; + int S, x, y; + float no[3]; + + for (S=0; S<f->numVerts; S++) { + for (y=0; y<gridSize-1; y++) + for (x=0; x<gridSize-1; x++) + NormZero(FACE_getIFNo(f, lvl, S, x, y)); + + if (FACE_getEdges(f)[(S-1+f->numVerts)%f->numVerts]->flags&Edge_eEffected) + for (x=0; x<gridSize-1; x++) + NormZero(FACE_getIFNo(f, lvl, S, x, gridSize-1)); + if (FACE_getEdges(f)[S]->flags&Edge_eEffected) + for (y=0; y<gridSize-1; y++) + NormZero(FACE_getIFNo(f, lvl, S, gridSize-1, y)); + if (FACE_getVerts(f)[S]->flags&Vert_eEffected) + NormZero(FACE_getIFNo(f, lvl, S, gridSize-1, gridSize-1)); + } + + for (S=0; S<f->numVerts; S++) { + int yLimit = !(FACE_getEdges(f)[(S-1+f->numVerts)%f->numVerts]->flags&Edge_eEffected); + int xLimit = !(FACE_getEdges(f)[S]->flags&Edge_eEffected); + int yLimitNext = xLimit; + int xLimitPrev = yLimit; + + for (y=0; y<gridSize - 1; y++) { + for (x=0; x<gridSize - 1; x++) { + int xPlusOk = (!xLimit || x<gridSize-2); + int yPlusOk = (!yLimit || y<gridSize-2); + + FACE_calcIFNo(f, lvl, S, x, y, no); + + NormAdd(FACE_getIFNo(f, lvl, S, x+0, y+0), no); + if (xPlusOk) + NormAdd(FACE_getIFNo(f, lvl, S, x+1, y+0), no); + if (yPlusOk) + NormAdd(FACE_getIFNo(f, lvl, S, x+0, y+1), no); + if (xPlusOk && yPlusOk) { + if (x<gridSize-2 || y<gridSize-2 || FACE_getVerts(f)[S]->flags&Vert_eEffected) { + NormAdd(FACE_getIFNo(f, lvl, S, x+1, y+1), no); + } + } + + if (x==0 && y==0) { + int K; + + if (!yLimitNext || 1<gridSize-1) + NormAdd(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, 1), no); + if (!xLimitPrev || 1<gridSize-1) + NormAdd(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, 1, 0), no); + + for (K=0; K<f->numVerts; K++) { + if (K!=S) { + NormAdd(FACE_getIFNo(f, lvl, K, 0, 0), no); + } + } + } else if (y==0) { + NormAdd(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, x), no); + if (!yLimitNext || x<gridSize-2) + NormAdd(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, x+1), no); + } else if (x==0) { + NormAdd(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, y, 0), no); + if (!xLimitPrev || y<gridSize-2) + NormAdd(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, y+1, 0), no); + } + } + } + } + } + // XXX can I reduce the number of normalisations here? + for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) { + CCGVert *v = (CCGVert*) effectedV[ptrIdx]; + float length, *no = _vert_getNo(v, lvl, vertDataSize, normalDataOffset); + + NormZero(no); + + for (i=0; i<v->numFaces; i++) { + CCGFace *f = v->faces[i]; + NormAdd(no, FACE_getIFNo(f, lvl, _face_getVertIndex(f,v), gridSize-1, gridSize-1)); + } + + length = sqrt(no[0]*no[0] + no[1]*no[1] + no[2]*no[2]); + + if (length>EPSILON) { + float invLength = 1.0f/length; + no[0] *= invLength; + no[1] *= invLength; + no[2] *= invLength; + } else { + NormZero(no); + } + + for (i=0; i<v->numFaces; i++) { + CCGFace *f = v->faces[i]; + NormCopy(FACE_getIFNo(f, lvl, _face_getVertIndex(f,v), gridSize-1, gridSize-1), no); + } + } + for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { + CCGEdge *e = (CCGEdge*) effectedE[ptrIdx]; + + if (e->numFaces) { + CCGFace *fLast = e->faces[e->numFaces-1]; + int x; + + for (i=0; i<e->numFaces-1; i++) { + CCGFace *f = e->faces[i]; + + for (x=1; x<edgeSize-1; x++) { + NormAdd(_face_getIFNoEdge(fLast, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset), + _face_getIFNoEdge(f, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset)); + } + } + + for (i=0; i<e->numFaces-1; i++) { + CCGFace *f = e->faces[i]; + + for (x=1; x<edgeSize-1; x++) { + NormCopy(_face_getIFNoEdge(f, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset), + _face_getIFNoEdge(fLast, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset)); + } + } + } + } + + #pragma omp parallel for private(ptrIdx) schedule(static) + for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { + CCGFace *f = (CCGFace*) effectedF[ptrIdx]; + int S, x, y; + + for (S=0; S<f->numVerts; S++) { + NormCopy(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, gridSize-1), + FACE_getIFNo(f, lvl, S, gridSize-1, 0)); + } + + for (S=0; S<f->numVerts; S++) { + for (y=0; y<gridSize; y++) { + for (x=0; x<gridSize; x++) { + float *no = FACE_getIFNo(f, lvl, S, x, y); + float length = sqrt(no[0]*no[0] + no[1]*no[1] + no[2]*no[2]); + + if (length>EPSILON) { + float invLength = 1.0f/length; + no[0] *= invLength; + no[1] *= invLength; + no[2] *= invLength; + } else { + NormZero(no); + } + } + } + } + } +} +#undef FACE_getIFNo + +#define VERT_getCo(v, lvl) _vert_getCo(v, lvl, vertDataSize) +#define EDGE_getCo(e, lvl, x) _edge_getCo(e, lvl, x, vertDataSize) +#define FACE_getIECo(f, lvl, S, x) _face_getIECo(f, lvl, S, x, subdivLevels, vertDataSize) +#define FACE_getIFCo(f, lvl, S, x, y) _face_getIFCo(f, lvl, S, x, y, subdivLevels, vertDataSize) +static void ccgSubSurf__calcSubdivLevel(CCGSubSurf *ss, + CCGVert **effectedV, CCGEdge **effectedE, CCGFace **effectedF, + int numEffectedV, int numEffectedE, int numEffectedF, int curLvl) { + int subdivLevels = ss->subdivLevels; + int edgeSize = 1 + (1<<curLvl); + int gridSize = 1 + (1<<(curLvl-1)); + int nextLvl = curLvl+1; + int ptrIdx, cornerIdx, i; + int vertDataSize = ss->meshIFC.vertDataSize; + void *q = ss->q, *r = ss->r; + + #pragma omp parallel for private(ptrIdx) schedule(static) + for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { + CCGFace *f = (CCGFace*) effectedF[ptrIdx]; + int S, x, y; + + /* interior face midpoints + * o old interior face points + */ + for (S=0; S<f->numVerts; S++) { + for (y=0; y<gridSize-1; y++) { + for (x=0; x<gridSize-1; x++) { + int fx = 1 + 2*x; + int fy = 1 + 2*y; + void *co0 = FACE_getIFCo(f, curLvl, S, x+0, y+0); + void *co1 = FACE_getIFCo(f, curLvl, S, x+1, y+0); + void *co2 = FACE_getIFCo(f, curLvl, S, x+1, y+1); + void *co3 = FACE_getIFCo(f, curLvl, S, x+0, y+1); + void *co = FACE_getIFCo(f, nextLvl, S, fx, fy); + + VertDataAvg4(co, co0, co1, co2, co3); + } + } + } + + /* interior edge midpoints + * o old interior edge points + * o new interior face midpoints + */ + for (S=0; S<f->numVerts; S++) { + for (x=0; x<gridSize-1; x++) { + int fx = x*2 + 1; + void *co0 = FACE_getIECo(f, curLvl, S, x+0); + void *co1 = FACE_getIECo(f, curLvl, S, x+1); + void *co2 = FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx); + void *co3 = FACE_getIFCo(f, nextLvl, S, fx, 1); + void *co = FACE_getIECo(f, nextLvl, S, fx); + + VertDataAvg4(co, co0, co1, co2, co3); + } + + /* interior face interior edge midpoints + * o old interior face points + * o new interior face midpoints + */ + + /* vertical */ + for (x=1; x<gridSize-1; x++) { + for (y=0; y<gridSize-1; y++) { + int fx = x*2; + int fy = y*2+1; + void *co0 = FACE_getIFCo(f, curLvl, S, x, y+0); + void *co1 = FACE_getIFCo(f, curLvl, S, x, y+1); + void *co2 = FACE_getIFCo(f, nextLvl, S, fx-1, fy); + void *co3 = FACE_getIFCo(f, nextLvl, S, fx+1, fy); + void *co = FACE_getIFCo(f, nextLvl, S, fx, fy); + + VertDataAvg4(co, co0, co1, co2, co3); + } + } + + /* horizontal */ + for (y=1; y<gridSize-1; y++) { + for (x=0; x<gridSize-1; x++) { + int fx = x*2+1; + int fy = y*2; + void *co0 = FACE_getIFCo(f, curLvl, S, x+0, y); + void *co1 = FACE_getIFCo(f, curLvl, S, x+1, y); + void *co2 = FACE_getIFCo(f, nextLvl, S, fx, fy-1); + void *co3 = FACE_getIFCo(f, nextLvl, S, fx, fy+1); + void *co = FACE_getIFCo(f, nextLvl, S, fx, fy); + + VertDataAvg4(co, co0, co1, co2, co3); + } + } + } + } + + /* exterior edge midpoints + * o old exterior edge points + * o new interior face midpoints + */ + for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { + CCGEdge *e = (CCGEdge*) effectedE[ptrIdx]; + float sharpness = EDGE_getSharpness(e, curLvl); + int x, j; + + if (_edge_isBoundary(e) || sharpness>1.0) { + for (x=0; x<edgeSize-1; x++) { + int fx = x*2 + 1; + void *co0 = EDGE_getCo(e, curLvl, x+0); + void *co1 = EDGE_getCo(e, curLvl, x+1); + void *co = EDGE_getCo(e, nextLvl, fx); + + VertDataCopy(co, co0); + VertDataAdd(co, co1); + VertDataMulN(co, 0.5); + } + } else { + for (x=0; x<edgeSize-1; x++) { + int fx = x*2 + 1; + void *co0 = EDGE_getCo(e, curLvl, x+0); + void *co1 = EDGE_getCo(e, curLvl, x+1); + void *co = EDGE_getCo(e, nextLvl, fx); + int numFaces = 0; + + VertDataCopy(q, co0); + VertDataAdd(q, co1); + + for (j=0; j<e->numFaces; j++) { + CCGFace *f = e->faces[j]; + VertDataAdd(q, _face_getIFCoEdge(f, e, nextLvl, fx, 1, subdivLevels, vertDataSize)); + numFaces++; + } + + VertDataMulN(q, 1.0f/(2.0f+numFaces)); + + VertDataCopy(r, co0); + VertDataAdd(r, co1); + VertDataMulN(r, 0.5); + + VertDataCopy(co, q); + VertDataSub(r, q); + VertDataMulN(r, sharpness); + VertDataAdd(co, r); + } + } + } + + /* exterior vertex shift + * o old vertex points (shifting) + * o old exterior edge points + * o new interior face midpoints + */ + for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) { + CCGVert *v = (CCGVert*) effectedV[ptrIdx]; + void *co = VERT_getCo(v, curLvl); + void *nCo = VERT_getCo(v, nextLvl); + int sharpCount = 0, allSharp = 1; + float avgSharpness = 0.0; + int j, seam = VERT_seam(v), seamEdges = 0; + + for (j=0; j<v->numEdges; j++) { + CCGEdge *e = v->edges[j]; + float sharpness = EDGE_getSharpness(e, curLvl); + + if (seam && _edge_isBoundary(e)) + seamEdges++; + + if (sharpness!=0.0f) { + sharpCount++; + avgSharpness += sharpness; + } else { + allSharp = 0; + } + } + + if(sharpCount) { + avgSharpness /= sharpCount; + if (avgSharpness>1.0) { + avgSharpness = 1.0; + } + } + + if (seam && seamEdges < 2) + seam = 0; + + if (!v->numEdges) { + VertDataCopy(nCo, co); + } else if (_vert_isBoundary(v)) { + int numBoundary = 0; + + VertDataZero(r); + for (j=0; j<v->numEdges; j++) { + CCGEdge *e = v->edges[j]; + if (_edge_isBoundary(e)) { + VertDataAdd(r, _edge_getCoVert(e, v, curLvl, 1, vertDataSize)); + numBoundary++; + } + } + + VertDataCopy(nCo, co); + VertDataMulN(nCo, 0.75); + VertDataMulN(r, 0.25f/numBoundary); + VertDataAdd(nCo, r); + } else { + int cornerIdx = (1 + (1<<(curLvl))) - 2; + int numEdges = 0, numFaces = 0; + + VertDataZero(q); + for (j=0; j<v->numFaces; j++) { + CCGFace *f = v->faces[j]; + VertDataAdd(q, FACE_getIFCo(f, nextLvl, _face_getVertIndex(f,v), cornerIdx, cornerIdx)); + numFaces++; + } + VertDataMulN(q, 1.0f/numFaces); + VertDataZero(r); + for (j=0; j<v->numEdges; j++) { + CCGEdge *e = v->edges[j]; + VertDataAdd(r, _edge_getCoVert(e, v, curLvl, 1,vertDataSize)); + numEdges++; + } + VertDataMulN(r, 1.0f/numEdges); + + VertDataCopy(nCo, co); + VertDataMulN(nCo, numEdges-2.0f); + VertDataAdd(nCo, q); + VertDataAdd(nCo, r); + VertDataMulN(nCo, 1.0f/numEdges); + } + + if ((sharpCount>1 && v->numFaces) || seam) { + VertDataZero(q); + + if (seam) { + avgSharpness = 1.0f; + sharpCount = seamEdges; + allSharp = 1; + } + + for (j=0; j<v->numEdges; j++) { + CCGEdge *e = v->edges[j]; + float sharpness = EDGE_getSharpness(e, curLvl); + + if (seam) { + if (_edge_isBoundary(e)) + VertDataAdd(q, _edge_getCoVert(e, v, curLvl, 1, vertDataSize)); + } else if (sharpness != 0.0) { + VertDataAdd(q, _edge_getCoVert(e, v, curLvl, 1, vertDataSize)); + } + } + + VertDataMulN(q, (float) 1/sharpCount); + + if (sharpCount!=2 || allSharp) { + // q = q + (co-q)*avgSharpness + VertDataCopy(r, co); + VertDataSub(r, q); + VertDataMulN(r, avgSharpness); + VertDataAdd(q, r); + } + + // r = co*.75 + q*.25 + VertDataCopy(r, co); + VertDataMulN(r, .75); + VertDataMulN(q, .25); + VertDataAdd(r, q); + + // nCo = nCo + (r-nCo)*avgSharpness + VertDataSub(r, nCo); + VertDataMulN(r, avgSharpness); + VertDataAdd(nCo, r); + } + } + + /* exterior edge interior shift + * o old exterior edge midpoints (shifting) + * o old exterior edge midpoints + * o new interior face midpoints + */ + for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { + CCGEdge *e = (CCGEdge*) effectedE[ptrIdx]; + float sharpness = EDGE_getSharpness(e, curLvl); + int sharpCount = 0; + float avgSharpness = 0.0; + int x, j; + + if (sharpness!=0.0f) { + sharpCount = 2; + avgSharpness += sharpness; + + if (avgSharpness>1.0) { + avgSharpness = 1.0; + } + } else { + sharpCount = 0; + avgSharpness = 0; + } + + if (_edge_isBoundary(e) && (!e->numFaces || sharpCount<2)) { + for (x=1; x<edgeSize-1; x++) { + int fx = x*2; + void *co = EDGE_getCo(e, curLvl, x); + void *nCo = EDGE_getCo(e, nextLvl, fx); + VertDataCopy(r, EDGE_getCo(e, curLvl, x-1)); + VertDataAdd(r, EDGE_getCo(e, curLvl, x+1)); + VertDataMulN(r, 0.5); + VertDataCopy(nCo, co); + VertDataMulN(nCo, 0.75); + VertDataMulN(r, 0.25); + VertDataAdd(nCo, r); + } + } else { + for (x=1; x<edgeSize-1; x++) { + int fx = x*2; + void *co = EDGE_getCo(e, curLvl, x); + void *nCo = EDGE_getCo(e, nextLvl, fx); + int numFaces = 0; + + VertDataZero(q); + VertDataZero(r); + VertDataAdd(r, EDGE_getCo(e, curLvl, x-1)); + VertDataAdd(r, EDGE_getCo(e, curLvl, x+1)); + for (j=0; j<e->numFaces; j++) { + CCGFace *f = e->faces[j]; + VertDataAdd(q, _face_getIFCoEdge(f, e, nextLvl, fx-1, 1, subdivLevels, vertDataSize)); + VertDataAdd(q, _face_getIFCoEdge(f, e, nextLvl, fx+1, 1, subdivLevels, vertDataSize)); + + VertDataAdd(r, _face_getIFCoEdge(f, e, curLvl, x, 1, subdivLevels, vertDataSize)); + numFaces++; + } + VertDataMulN(q, 1.0/(numFaces*2.0f)); + VertDataMulN(r, 1.0/(2.0f + numFaces)); + + VertDataCopy(nCo, co); + VertDataMulN(nCo, (float) numFaces); + VertDataAdd(nCo, q); + VertDataAdd(nCo, r); + VertDataMulN(nCo, 1.0f/(2+numFaces)); + + if (sharpCount==2) { + VertDataCopy(q, co); + VertDataMulN(q, 6.0f); + VertDataAdd(q, EDGE_getCo(e, curLvl, x-1)); + VertDataAdd(q, EDGE_getCo(e, curLvl, x+1)); + VertDataMulN(q, 1/8.0f); + + VertDataSub(q, nCo); + VertDataMulN(q, avgSharpness); + VertDataAdd(nCo, q); + } + } + } + } + + #pragma omp parallel private(ptrIdx) + { + void *q, *r; + + #pragma omp critical + { + q = CCGSUBSURF_alloc(ss, ss->meshIFC.vertDataSize); + r = CCGSUBSURF_alloc(ss, ss->meshIFC.vertDataSize); + } + + #pragma omp for schedule(static) + for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { + CCGFace *f = (CCGFace*) effectedF[ptrIdx]; + int S, x, y; + + /* interior center point shift + * o old face center point (shifting) + * o old interior edge points + * o new interior face midpoints + */ + VertDataZero(q); + for (S=0; S<f->numVerts; S++) { + VertDataAdd(q, FACE_getIFCo(f, nextLvl, S, 1, 1)); + } + VertDataMulN(q, 1.0f/f->numVerts); + VertDataZero(r); + for (S=0; S<f->numVerts; S++) { + VertDataAdd(r, FACE_getIECo(f, curLvl, S, 1)); + } + VertDataMulN(r, 1.0f/f->numVerts); + + VertDataMulN(FACE_getCenterData(f), f->numVerts-2.0f); + VertDataAdd(FACE_getCenterData(f), q); + VertDataAdd(FACE_getCenterData(f), r); + VertDataMulN(FACE_getCenterData(f), 1.0f/f->numVerts); + + for (S=0; S<f->numVerts; S++) { + /* interior face shift + * o old interior face point (shifting) + * o new interior edge midpoints + * o new interior face midpoints + */ + for (x=1; x<gridSize-1; x++) { + for (y=1; y<gridSize-1; y++) { + int fx = x*2; + int fy = y*2; + void *co = FACE_getIFCo(f, curLvl, S, x, y); + void *nCo = FACE_getIFCo(f, nextLvl, S, fx, fy); + + VertDataAvg4(q, FACE_getIFCo(f, nextLvl, S, fx-1, fy-1), + FACE_getIFCo(f, nextLvl, S, fx+1, fy-1), + FACE_getIFCo(f, nextLvl, S, fx+1, fy+1), + FACE_getIFCo(f, nextLvl, S, fx-1, fy+1)); + + VertDataAvg4(r, FACE_getIFCo(f, nextLvl, S, fx-1, fy+0), + FACE_getIFCo(f, nextLvl, S, fx+1, fy+0), + FACE_getIFCo(f, nextLvl, S, fx+0, fy-1), + FACE_getIFCo(f, nextLvl, S, fx+0, fy+1)); + + VertDataCopy(nCo, co); + VertDataSub(nCo, q); + VertDataMulN(nCo, 0.25f); + VertDataAdd(nCo, r); + } + } + + /* interior edge interior shift + * o old interior edge point (shifting) + * o new interior edge midpoints + * o new interior face midpoints + */ + for (x=1; x<gridSize-1; x++) { + int fx = x*2; + void *co = FACE_getIECo(f, curLvl, S, x); + void *nCo = FACE_getIECo(f, nextLvl, S, fx); + + VertDataAvg4(q, FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx-1), + FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx+1), + FACE_getIFCo(f, nextLvl, S, fx+1, +1), + FACE_getIFCo(f, nextLvl, S, fx-1, +1)); + + VertDataAvg4(r, FACE_getIECo(f, nextLvl, S, fx-1), + FACE_getIECo(f, nextLvl, S, fx+1), + FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx), + FACE_getIFCo(f, nextLvl, S, fx, 1)); + + VertDataCopy(nCo, co); + VertDataSub(nCo, q); + VertDataMulN(nCo, 0.25f); + VertDataAdd(nCo, r); + } + } + } + + #pragma omp critical + { + CCGSUBSURF_free(ss, q); + CCGSUBSURF_free(ss, r); + } + } + + /* copy down */ + edgeSize = 1 + (1<<(nextLvl)); + gridSize = 1 + (1<<((nextLvl)-1)); + cornerIdx = gridSize-1; + + #pragma omp parallel for private(i) schedule(static) + for (i=0; i<numEffectedE; i++) { + CCGEdge *e = effectedE[i]; + VertDataCopy(EDGE_getCo(e, nextLvl, 0), VERT_getCo(e->v0, nextLvl)); + VertDataCopy(EDGE_getCo(e, nextLvl, edgeSize-1), VERT_getCo(e->v1, nextLvl)); + } + + #pragma omp parallel for private(i) schedule(static) + for (i=0; i<numEffectedF; i++) { + CCGFace *f = effectedF[i]; + int S, x; + + for (S=0; S<f->numVerts; S++) { + CCGEdge *e = FACE_getEdges(f)[S]; + CCGEdge *prevE = FACE_getEdges(f)[(S+f->numVerts-1)%f->numVerts]; + + VertDataCopy(FACE_getIFCo(f, nextLvl, S, 0, 0), FACE_getCenterData(f)); + VertDataCopy(FACE_getIECo(f, nextLvl, S, 0), FACE_getCenterData(f)); + VertDataCopy(FACE_getIFCo(f, nextLvl, S, cornerIdx, cornerIdx), VERT_getCo(FACE_getVerts(f)[S], nextLvl)); + VertDataCopy(FACE_getIECo(f, nextLvl, S, cornerIdx), EDGE_getCo(FACE_getEdges(f)[S], nextLvl, cornerIdx)); + for (x=1; x<gridSize-1; x++) { + void *co = FACE_getIECo(f, nextLvl, S, x); + VertDataCopy(FACE_getIFCo(f, nextLvl, S, x, 0), co); + VertDataCopy(FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 0, x), co); + } + for (x=0; x<gridSize-1; x++) { + int eI = gridSize-1-x; + VertDataCopy(FACE_getIFCo(f, nextLvl, S, cornerIdx, x), _edge_getCoVert(e, FACE_getVerts(f)[S], nextLvl, eI,vertDataSize)); + VertDataCopy(FACE_getIFCo(f, nextLvl, S, x, cornerIdx), _edge_getCoVert(prevE, FACE_getVerts(f)[S], nextLvl, eI,vertDataSize)); + } + } + } +} + + static void ccgSubSurf__sync(CCGSubSurf *ss) { CCGVert **effectedV; CCGEdge **effectedE; @@ -1140,11 +1806,9 @@ static void ccgSubSurf__sync(CCGSubSurf *ss) { int numEffectedV, numEffectedE, numEffectedF; int subdivLevels = ss->subdivLevels; int vertDataSize = ss->meshIFC.vertDataSize; - int i,ptrIdx,cornerIdx; - int S,x,y; - void *q = ss->q, *r = ss->r; + int i, j, ptrIdx, S; int curLvl, nextLvl; - int j; + void *q = ss->q, *r = ss->r; effectedV = CCGSUBSURF_alloc(ss, sizeof(*effectedV)*ss->vMap->numEntries); effectedE = CCGSUBSURF_alloc(ss, sizeof(*effectedE)*ss->eMap->numEntries); @@ -1175,10 +1839,6 @@ static void ccgSubSurf__sync(CCGSubSurf *ss) { } } -#define VERT_getCo(v, lvl) _vert_getCo(v, lvl, vertDataSize) -#define EDGE_getCo(e, lvl, x) _edge_getCo(e, lvl, x, vertDataSize) -#define FACE_getIECo(f, lvl, S, x) _face_getIECo(f, lvl, S, x, subdivLevels, vertDataSize) -#define FACE_getIFCo(f, lvl, S, x, y) _face_getIFCo(f, lvl, S, x, y, subdivLevels, vertDataSize) curLvl = 0; nextLvl = curLvl+1; @@ -1391,645 +2051,389 @@ static void ccgSubSurf__sync(CCGSubSurf *ss) { } for (curLvl=1; curLvl<subdivLevels; curLvl++) { - int edgeSize = 1 + (1<<curLvl); - int gridSize = 1 + (1<<(curLvl-1)); - nextLvl = curLvl+1; - - for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { - CCGFace *f = (CCGFace*) effectedF[ptrIdx]; + ccgSubSurf__calcSubdivLevel(ss, + effectedV, effectedE, effectedF, + numEffectedV, numEffectedE, numEffectedF, curLvl); + } - /* interior face midpoints - * o old interior face points - */ - for (S=0; S<f->numVerts; S++) { - for (y=0; y<gridSize-1; y++) { - for (x=0; x<gridSize-1; x++) { - int fx = 1 + 2*x; - int fy = 1 + 2*y; - void *co0 = FACE_getIFCo(f, curLvl, S, x+0, y+0); - void *co1 = FACE_getIFCo(f, curLvl, S, x+1, y+0); - void *co2 = FACE_getIFCo(f, curLvl, S, x+1, y+1); - void *co3 = FACE_getIFCo(f, curLvl, S, x+0, y+1); - void *co = FACE_getIFCo(f, nextLvl, S, fx, fy); - - VertDataAvg4(co, co0, co1, co2, co3); - } - } - } + if (ss->calcVertNormals) + ccgSubSurf__calcVertNormals(ss, + effectedV, effectedE, effectedF, + numEffectedV, numEffectedE, numEffectedF); - /* interior edge midpoints - * o old interior edge points - * o new interior face midpoints - */ - for (S=0; S<f->numVerts; S++) { - for (x=0; x<gridSize-1; x++) { - int fx = x*2 + 1; - void *co0 = FACE_getIECo(f, curLvl, S, x+0); - void *co1 = FACE_getIECo(f, curLvl, S, x+1); - void *co2 = FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx); - void *co3 = FACE_getIFCo(f, nextLvl, S, fx, 1); - void *co = FACE_getIECo(f, nextLvl, S, fx); - - VertDataAvg4(co, co0, co1, co2, co3); - } + for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) { + CCGVert *v = effectedV[ptrIdx]; + v->flags = 0; + } + for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { + CCGEdge *e = effectedE[ptrIdx]; + e->flags = 0; + } - /* interior face interior edge midpoints - * o old interior face points - * o new interior face midpoints - */ + CCGSUBSURF_free(ss, effectedF); + CCGSUBSURF_free(ss, effectedE); + CCGSUBSURF_free(ss, effectedV); +} - /* vertical */ - for (x=1; x<gridSize-1; x++) { - for (y=0; y<gridSize-1; y++) { - int fx = x*2; - int fy = y*2+1; - void *co0 = FACE_getIFCo(f, curLvl, S, x, y+0); - void *co1 = FACE_getIFCo(f, curLvl, S, x, y+1); - void *co2 = FACE_getIFCo(f, nextLvl, S, fx-1, fy); - void *co3 = FACE_getIFCo(f, nextLvl, S, fx+1, fy); - void *co = FACE_getIFCo(f, nextLvl, S, fx, fy); - - VertDataAvg4(co, co0, co1, co2, co3); - } - } +static void ccgSubSurf__allFaces(CCGSubSurf *ss, CCGFace ***faces, int *numFaces, int *freeFaces) +{ + CCGFace **array; + int i, num; - /* horizontal */ - for (y=1; y<gridSize-1; y++) { - for (x=0; x<gridSize-1; x++) { - int fx = x*2+1; - int fy = y*2; - void *co0 = FACE_getIFCo(f, curLvl, S, x+0, y); - void *co1 = FACE_getIFCo(f, curLvl, S, x+1, y); - void *co2 = FACE_getIFCo(f, nextLvl, S, fx, fy-1); - void *co3 = FACE_getIFCo(f, nextLvl, S, fx, fy+1); - void *co = FACE_getIFCo(f, nextLvl, S, fx, fy); + if(!*faces) { + array = CCGSUBSURF_alloc(ss, sizeof(*array)*ss->fMap->numEntries); + num = 0; + for (i=0; i<ss->fMap->curSize; i++) { + CCGFace *f = (CCGFace*) ss->fMap->buckets[i]; - VertDataAvg4(co, co0, co1, co2, co3); - } - } - } + for (; f; f = f->next) + array[num++] = f; } - /* exterior edge midpoints - * o old exterior edge points - * o new interior face midpoints - */ - for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { - CCGEdge *e = (CCGEdge*) effectedE[ptrIdx]; - float sharpness = EDGE_getSharpness(e, curLvl); + *faces = array; + *numFaces = num; + *freeFaces= 1; + } + else + *freeFaces= 0; +} - if (_edge_isBoundary(e) || sharpness>1.0) { - for (x=0; x<edgeSize-1; x++) { - int fx = x*2 + 1; - void *co0 = EDGE_getCo(e, curLvl, x+0); - void *co1 = EDGE_getCo(e, curLvl, x+1); - void *co = EDGE_getCo(e, nextLvl, fx); +static void ccgSubSurf__effectedFaceNeighbours(CCGSubSurf *ss, CCGFace **faces, int numFaces, CCGVert ***verts, int *numVerts, CCGEdge ***edges, int *numEdges) +{ + CCGVert **arrayV; + CCGEdge **arrayE; + int numV, numE, i, j; - VertDataCopy(co, co0); - VertDataAdd(co, co1); - VertDataMulN(co, 0.5); - } - } else { - for (x=0; x<edgeSize-1; x++) { - int fx = x*2 + 1; - void *co0 = EDGE_getCo(e, curLvl, x+0); - void *co1 = EDGE_getCo(e, curLvl, x+1); - void *co = EDGE_getCo(e, nextLvl, fx); - int numFaces = 0; - - VertDataCopy(q, co0); - VertDataAdd(q, co1); - - for (i=0; i<e->numFaces; i++) { - CCGFace *f = e->faces[i]; - VertDataAdd(q, _face_getIFCoEdge(f, e, nextLvl, fx, 1, subdivLevels, vertDataSize)); - numFaces++; - } + arrayV = CCGSUBSURF_alloc(ss, sizeof(*arrayV)*ss->vMap->numEntries); + arrayE = CCGSUBSURF_alloc(ss, sizeof(*arrayE)*ss->eMap->numEntries); + numV = numE = 0; - VertDataMulN(q, 1.0f/(2.0f+numFaces)); + for (i=0; i<numFaces; i++) { + CCGFace *f = faces[i]; + f->flags |= Face_eEffected; + } - VertDataCopy(r, co0); - VertDataAdd(r, co1); - VertDataMulN(r, 0.5); + for (i=0; i<ss->vMap->curSize; i++) { + CCGVert *v = (CCGVert*) ss->vMap->buckets[i]; - VertDataCopy(co, q); - VertDataSub(r, q); - VertDataMulN(r, sharpness); - VertDataAdd(co, r); - } + for (; v; v = v->next) { + for(j=0; j<v->numFaces; j++) + if(!(v->faces[j]->flags & Face_eEffected)) + break; + + if(j == v->numFaces) { + arrayV[numV++] = v; + v->flags |= Vert_eEffected; } } + } - /* exterior vertex shift - * o old vertex points (shifting) - * o old exterior edge points - * o new interior face midpoints - */ - for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) { - CCGVert *v = (CCGVert*) effectedV[ptrIdx]; - void *co = VERT_getCo(v, curLvl); - void *nCo = VERT_getCo(v, nextLvl); - int sharpCount = 0, allSharp = 1; - float avgSharpness = 0.0; - int seam = VERT_seam(v), seamEdges = 0; + for (i=0; i<ss->eMap->curSize; i++) { + CCGEdge *e = (CCGEdge*) ss->eMap->buckets[i]; - for (i=0; i<v->numEdges; i++) { - CCGEdge *e = v->edges[i]; - float sharpness = EDGE_getSharpness(e, curLvl); + for (; e; e = e->next) { + for(j=0; j<e->numFaces; j++) + if(!(e->faces[j]->flags & Face_eEffected)) + break; + + if(j == e->numFaces) { + e->flags |= Edge_eEffected; + arrayE[numE++] = e; + } + } + } - if (seam && _edge_isBoundary(e)) - seamEdges++; + *verts = arrayV; + *numVerts = numV; + *edges = arrayE; + *numEdges = numE; +} - if (sharpness!=0.0f) { - sharpCount++; - avgSharpness += sharpness; - } else { - allSharp = 0; - } - } +/* copy face grid coordinates to other places */ +CCGError ccgSubSurf_updateFromFaces(CCGSubSurf *ss, int lvl, CCGFace **effectedF, int numEffectedF) +{ + int i, S, x, gridSize, cornerIdx, subdivLevels; + int vertDataSize = ss->meshIFC.vertDataSize, freeF; - if(sharpCount) { - avgSharpness /= sharpCount; - if (avgSharpness>1.0) { - avgSharpness = 1.0; - } - } + subdivLevels = ss->subdivLevels; + lvl = (lvl)? lvl: subdivLevels; + gridSize = 1 + (1<<(lvl-1)); + cornerIdx = gridSize-1; - if (seam && seamEdges < 2) - seam = 0; + ccgSubSurf__allFaces(ss, &effectedF, &numEffectedF, &freeF); - if (!v->numEdges) { - VertDataCopy(nCo, co); - } else if (_vert_isBoundary(v)) { - int numBoundary = 0; + for (i=0; i<numEffectedF; i++) { + CCGFace *f = effectedF[i]; - VertDataZero(r); - for (i=0; i<v->numEdges; i++) { - CCGEdge *e = v->edges[i]; - if (_edge_isBoundary(e)) { - VertDataAdd(r, _edge_getCoVert(e, v, curLvl, 1, vertDataSize)); - numBoundary++; - } - } + for (S=0; S<f->numVerts; S++) { + CCGEdge *e = FACE_getEdges(f)[S]; + CCGEdge *prevE = FACE_getEdges(f)[(S+f->numVerts-1)%f->numVerts]; - VertDataCopy(nCo, co); - VertDataMulN(nCo, 0.75); - VertDataMulN(r, 0.25f/numBoundary); - VertDataAdd(nCo, r); - } else { - int cornerIdx = (1 + (1<<(curLvl))) - 2; - int numEdges = 0, numFaces = 0; + VertDataCopy(FACE_getCenterData(f), FACE_getIFCo(f, lvl, S, 0, 0)); + VertDataCopy(VERT_getCo(FACE_getVerts(f)[S], lvl), FACE_getIFCo(f, lvl, S, cornerIdx, cornerIdx)); - VertDataZero(q); - for (i=0; i<v->numFaces; i++) { - CCGFace *f = v->faces[i]; - VertDataAdd(q, FACE_getIFCo(f, nextLvl, _face_getVertIndex(f,v), cornerIdx, cornerIdx)); - numFaces++; - } - VertDataMulN(q, 1.0f/numFaces); - VertDataZero(r); - for (i=0; i<v->numEdges; i++) { - CCGEdge *e = v->edges[i]; - VertDataAdd(r, _edge_getCoVert(e, v, curLvl, 1,vertDataSize)); - numEdges++; - } - VertDataMulN(r, 1.0f/numEdges); + for (x=0; x<gridSize; x++) + VertDataCopy(FACE_getIECo(f, lvl, S, x), FACE_getIFCo(f, lvl, S, x, 0)); - VertDataCopy(nCo, co); - VertDataMulN(nCo, numEdges-2.0f); - VertDataAdd(nCo, q); - VertDataAdd(nCo, r); - VertDataMulN(nCo, 1.0f/numEdges); + for (x=0; x<gridSize; x++) { + int eI = gridSize-1-x; + VertDataCopy(_edge_getCoVert(e, FACE_getVerts(f)[S], lvl, eI,vertDataSize), FACE_getIFCo(f, lvl, S, cornerIdx, x)); + VertDataCopy(_edge_getCoVert(prevE, FACE_getVerts(f)[S], lvl, eI,vertDataSize), FACE_getIFCo(f, lvl, S, x, cornerIdx)); } + } + } - if ((sharpCount>1 && v->numFaces) || seam) { - VertDataZero(q); + if(freeF) CCGSUBSURF_free(ss, effectedF); - if (seam) { - avgSharpness = 1.0f; - sharpCount = seamEdges; - allSharp = 1; - } + return eCCGError_None; +} - for (i=0; i<v->numEdges; i++) { - CCGEdge *e = v->edges[i]; - float sharpness = EDGE_getSharpness(e, curLvl); +/* copy other places to face grid coordinates */ +CCGError ccgSubSurf_updateToFaces(CCGSubSurf *ss, int lvl, CCGFace **effectedF, int numEffectedF) +{ + int i, S, x, gridSize, cornerIdx, subdivLevels; + int vertDataSize = ss->meshIFC.vertDataSize, freeF; - if (seam) { - if (_edge_isBoundary(e)) - VertDataAdd(q, _edge_getCoVert(e, v, curLvl, 1, vertDataSize)); - } else if (sharpness != 0.0) { - VertDataAdd(q, _edge_getCoVert(e, v, curLvl, 1, vertDataSize)); - } - } + subdivLevels = ss->subdivLevels; + lvl = (lvl)? lvl: subdivLevels; + gridSize = 1 + (1<<(lvl-1)); + cornerIdx = gridSize-1; - VertDataMulN(q, (float) 1/sharpCount); + ccgSubSurf__allFaces(ss, &effectedF, &numEffectedF, &freeF); - if (sharpCount!=2 || allSharp) { - // q = q + (co-q)*avgSharpness - VertDataCopy(r, co); - VertDataSub(r, q); - VertDataMulN(r, avgSharpness); - VertDataAdd(q, r); - } + for (i=0; i<numEffectedF; i++) { + CCGFace *f = effectedF[i]; - // r = co*.75 + q*.25 - VertDataCopy(r, co); - VertDataMulN(r, .75); - VertDataMulN(q, .25); - VertDataAdd(r, q); + for (S=0; S<f->numVerts; S++) { + int prevS = (S+f->numVerts-1)%f->numVerts; + CCGEdge *e = FACE_getEdges(f)[S]; + CCGEdge *prevE = FACE_getEdges(f)[prevS]; - // nCo = nCo + (r-nCo)*avgSharpness - VertDataSub(r, nCo); - VertDataMulN(r, avgSharpness); - VertDataAdd(nCo, r); + for (x=0; x<gridSize; x++) { + int eI = gridSize-1-x; + VertDataCopy(FACE_getIFCo(f, lvl, S, cornerIdx, x), _edge_getCoVert(e, FACE_getVerts(f)[S], lvl, eI,vertDataSize)); + VertDataCopy(FACE_getIFCo(f, lvl, S, x, cornerIdx), _edge_getCoVert(prevE, FACE_getVerts(f)[S], lvl, eI,vertDataSize)); } + + for (x=1; x<gridSize-1; x++) { + VertDataCopy(FACE_getIFCo(f, lvl, S, 0, x), FACE_getIECo(f, lvl, prevS, x)); + VertDataCopy(FACE_getIFCo(f, lvl, S, x, 0), FACE_getIECo(f, lvl, S, x)); + } + + VertDataCopy(FACE_getIFCo(f, lvl, S, 0, 0), FACE_getCenterData(f)); + VertDataCopy(FACE_getIFCo(f, lvl, S, cornerIdx, cornerIdx), VERT_getCo(FACE_getVerts(f)[S], lvl)); } + } - /* exterior edge interior shift - * o old exterior edge midpoints (shifting) - * o old exterior edge midpoints - * o new interior face midpoints - */ - for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { - CCGEdge *e = (CCGEdge*) effectedE[ptrIdx]; - float sharpness = EDGE_getSharpness(e, curLvl); - int sharpCount = 0; - float avgSharpness = 0.0; + if(freeF) CCGSUBSURF_free(ss, effectedF); - if (sharpness!=0.0f) { - sharpCount = 2; - avgSharpness += sharpness; + return eCCGError_None; +} - if (avgSharpness>1.0) { - avgSharpness = 1.0; - } - } else { - sharpCount = 0; - avgSharpness = 0; - } +/* stitch together face grids, averaging coordinates at edges + and vertices, for multires displacements */ +CCGError ccgSubSurf_stitchFaces(CCGSubSurf *ss, int lvl, CCGFace **effectedF, int numEffectedF) +{ + CCGVert **effectedV; + CCGEdge **effectedE; + int numEffectedV, numEffectedE, freeF; + int i, S, x, gridSize, cornerIdx, subdivLevels, edgeSize; + int vertDataSize = ss->meshIFC.vertDataSize; - if (_edge_isBoundary(e) && (!e->numFaces || sharpCount<2)) { - for (x=1; x<edgeSize-1; x++) { - int fx = x*2; - void *co = EDGE_getCo(e, curLvl, x); - void *nCo = EDGE_getCo(e, nextLvl, fx); - VertDataCopy(r, EDGE_getCo(e, curLvl, x-1)); - VertDataAdd(r, EDGE_getCo(e, curLvl, x+1)); - VertDataMulN(r, 0.5); - VertDataCopy(nCo, co); - VertDataMulN(nCo, 0.75); - VertDataMulN(r, 0.25); - VertDataAdd(nCo, r); - } - } else { - for (x=1; x<edgeSize-1; x++) { - int fx = x*2; - void *co = EDGE_getCo(e, curLvl, x); - void *nCo = EDGE_getCo(e, nextLvl, fx); - int numFaces = 0; - - VertDataZero(q); - VertDataZero(r); - VertDataAdd(r, EDGE_getCo(e, curLvl, x-1)); - VertDataAdd(r, EDGE_getCo(e, curLvl, x+1)); - for (i=0; i<e->numFaces; i++) { - CCGFace *f = e->faces[i]; - VertDataAdd(q, _face_getIFCoEdge(f, e, nextLvl, fx-1, 1, subdivLevels, vertDataSize)); - VertDataAdd(q, _face_getIFCoEdge(f, e, nextLvl, fx+1, 1, subdivLevels, vertDataSize)); - - VertDataAdd(r, _face_getIFCoEdge(f, e, curLvl, x, 1, subdivLevels, vertDataSize)); - numFaces++; - } - VertDataMulN(q, 1.0/(numFaces*2.0f)); - VertDataMulN(r, 1.0/(2.0f + numFaces)); + subdivLevels = ss->subdivLevels; + lvl = (lvl)? lvl: subdivLevels; + gridSize = 1 + (1<<(lvl-1)); + edgeSize = 1 + (1<<lvl); + cornerIdx = gridSize-1; - VertDataCopy(nCo, co); - VertDataMulN(nCo, (float) numFaces); - VertDataAdd(nCo, q); - VertDataAdd(nCo, r); - VertDataMulN(nCo, 1.0f/(2+numFaces)); - - if (sharpCount==2) { - VertDataCopy(q, co); - VertDataMulN(q, 6.0f); - VertDataAdd(q, EDGE_getCo(e, curLvl, x-1)); - VertDataAdd(q, EDGE_getCo(e, curLvl, x+1)); - VertDataMulN(q, 1/8.0f); - - VertDataSub(q, nCo); - VertDataMulN(q, avgSharpness); - VertDataAdd(nCo, q); - } - } - } - } + ccgSubSurf__allFaces(ss, &effectedF, &numEffectedF, &freeF); + ccgSubSurf__effectedFaceNeighbours(ss, effectedF, numEffectedF, + &effectedV, &numEffectedV, &effectedE, &numEffectedE); - for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { - CCGFace *f = (CCGFace*) effectedF[ptrIdx]; + /* zero */ + for (i=0; i<numEffectedV; i++) { + CCGVert *v = effectedV[i]; + VertDataZero(VERT_getCo(v, lvl)); + } - /* interior center point shift - * o old face center point (shifting) - * o old interior edge points - * o new interior face midpoints - */ - VertDataZero(q); - for (S=0; S<f->numVerts; S++) { - VertDataAdd(q, FACE_getIFCo(f, nextLvl, S, 1, 1)); - } - VertDataMulN(q, 1.0f/f->numVerts); - VertDataZero(r); - for (S=0; S<f->numVerts; S++) { - VertDataAdd(r, FACE_getIECo(f, curLvl, S, 1)); - } - VertDataMulN(r, 1.0f/f->numVerts); + for (i=0; i<numEffectedE; i++) { + CCGEdge *e = effectedE[i]; - VertDataMulN(FACE_getCenterData(f), f->numVerts-2.0f); - VertDataAdd(FACE_getCenterData(f), q); - VertDataAdd(FACE_getCenterData(f), r); - VertDataMulN(FACE_getCenterData(f), 1.0f/f->numVerts); + for (x=0; x<edgeSize; x++) + VertDataZero(EDGE_getCo(e, lvl, x)); + } - for (S=0; S<f->numVerts; S++) { - /* interior face shift - * o old interior face point (shifting) - * o new interior edge midpoints - * o new interior face midpoints - */ - for (x=1; x<gridSize-1; x++) { - for (y=1; y<gridSize-1; y++) { - int fx = x*2; - int fy = y*2; - void *co = FACE_getIFCo(f, curLvl, S, x, y); - void *nCo = FACE_getIFCo(f, nextLvl, S, fx, fy); - - VertDataAvg4(q, FACE_getIFCo(f, nextLvl, S, fx-1, fy-1), - FACE_getIFCo(f, nextLvl, S, fx+1, fy-1), - FACE_getIFCo(f, nextLvl, S, fx+1, fy+1), - FACE_getIFCo(f, nextLvl, S, fx-1, fy+1)); + /* add */ + for (i=0; i<numEffectedF; i++) { + CCGFace *f = effectedF[i]; - VertDataAvg4(r, FACE_getIFCo(f, nextLvl, S, fx-1, fy+0), - FACE_getIFCo(f, nextLvl, S, fx+1, fy+0), - FACE_getIFCo(f, nextLvl, S, fx+0, fy-1), - FACE_getIFCo(f, nextLvl, S, fx+0, fy+1)); + VertDataZero(FACE_getCenterData(f)); - VertDataCopy(nCo, co); - VertDataSub(nCo, q); - VertDataMulN(nCo, 0.25f); - VertDataAdd(nCo, r); - } - } + for (S=0; S<f->numVerts; S++) + if (FACE_getEdges(f)[S]->flags&Edge_eEffected) + for (x=0; x<gridSize; x++) + VertDataZero(FACE_getIECo(f, lvl, S, x)); - /* interior edge interior shift - * o old interior edge point (shifting) - * o new interior edge midpoints - * o new interior face midpoints - */ - for (x=1; x<gridSize-1; x++) { - int fx = x*2; - void *co = FACE_getIECo(f, curLvl, S, x); - void *nCo = FACE_getIECo(f, nextLvl, S, fx); - - VertDataAvg4(q, FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx-1), - FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx+1), - FACE_getIFCo(f, nextLvl, S, fx+1, +1), - FACE_getIFCo(f, nextLvl, S, fx-1, +1)); + for (S=0; S<f->numVerts; S++) { + int prevS = (S+f->numVerts-1)%f->numVerts; + CCGEdge *e = FACE_getEdges(f)[S]; + CCGEdge *prevE = FACE_getEdges(f)[prevS]; - VertDataAvg4(r, FACE_getIECo(f, nextLvl, S, fx-1), - FACE_getIECo(f, nextLvl, S, fx+1), - FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx), - FACE_getIFCo(f, nextLvl, S, fx, 1)); + VertDataAdd(FACE_getCenterData(f), FACE_getIFCo(f, lvl, S, 0, 0)); + if (FACE_getVerts(f)[S]->flags&Vert_eEffected) + VertDataAdd(VERT_getCo(FACE_getVerts(f)[S], lvl), FACE_getIFCo(f, lvl, S, cornerIdx, cornerIdx)); - VertDataCopy(nCo, co); - VertDataSub(nCo, q); - VertDataMulN(nCo, 0.25f); - VertDataAdd(nCo, r); - } + for (x=1; x<gridSize-1; x++) { + if (FACE_getEdges(f)[S]->flags&Edge_eEffected) + VertDataAdd(FACE_getIECo(f, lvl, S, x), FACE_getIFCo(f, lvl, S, x, 0)); + if (FACE_getEdges(f)[prevS]->flags&Edge_eEffected) + VertDataAdd(FACE_getIECo(f, lvl, prevS, x), FACE_getIFCo(f, lvl, S, 0, x)); } - } - /* copy down */ - edgeSize = 1 + (1<<(nextLvl)); - gridSize = 1 + (1<<((nextLvl)-1)); - cornerIdx = gridSize-1; - for (i=0; i<numEffectedE; i++) { - CCGEdge *e = effectedE[i]; - VertDataCopy(EDGE_getCo(e, nextLvl, 0), VERT_getCo(e->v0, nextLvl)); - VertDataCopy(EDGE_getCo(e, nextLvl, edgeSize-1), VERT_getCo(e->v1, nextLvl)); - } - for (i=0; i<numEffectedF; i++) { - CCGFace *f = effectedF[i]; - for (S=0; S<f->numVerts; S++) { - CCGEdge *e = FACE_getEdges(f)[S]; - CCGEdge *prevE = FACE_getEdges(f)[(S+f->numVerts-1)%f->numVerts]; - - VertDataCopy(FACE_getIFCo(f, nextLvl, S, 0, 0), FACE_getCenterData(f)); - VertDataCopy(FACE_getIECo(f, nextLvl, S, 0), FACE_getCenterData(f)); - VertDataCopy(FACE_getIFCo(f, nextLvl, S, cornerIdx, cornerIdx), VERT_getCo(FACE_getVerts(f)[S], nextLvl)); - VertDataCopy(FACE_getIECo(f, nextLvl, S, cornerIdx), EDGE_getCo(FACE_getEdges(f)[S], nextLvl, cornerIdx)); - for (x=1; x<gridSize-1; x++) { - void *co = FACE_getIECo(f, nextLvl, S, x); - VertDataCopy(FACE_getIFCo(f, nextLvl, S, x, 0), co); - VertDataCopy(FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 0, x), co); - } - for (x=0; x<gridSize-1; x++) { - int eI = gridSize-1-x; - VertDataCopy(FACE_getIFCo(f, nextLvl, S, cornerIdx, x), _edge_getCoVert(e, FACE_getVerts(f)[S], nextLvl, eI,vertDataSize)); - VertDataCopy(FACE_getIFCo(f, nextLvl, S, x, cornerIdx), _edge_getCoVert(prevE, FACE_getVerts(f)[S], nextLvl, eI,vertDataSize)); - } + for (x=0; x<gridSize-1; x++) { + int eI = gridSize-1-x; + if (FACE_getEdges(f)[S]->flags&Edge_eEffected) + VertDataAdd(_edge_getCoVert(e, FACE_getVerts(f)[S], lvl, eI,vertDataSize), FACE_getIFCo(f, lvl, S, cornerIdx, x)); + if (FACE_getEdges(f)[prevS]->flags&Edge_eEffected) + if(x != 0) + VertDataAdd(_edge_getCoVert(prevE, FACE_getVerts(f)[S], lvl, eI,vertDataSize), FACE_getIFCo(f, lvl, S, x, cornerIdx)); } } } -#define FACE_getIFNo(f, lvl, S, x, y) _face_getIFNo(f, lvl, S, x, y, subdivLevels, vertDataSize, normalDataOffset) -#define FACE_calcIFNo(f, lvl, S, x, y, no) _face_calcIFNo(f, lvl, S, x, y, no, subdivLevels, vertDataSize) - if (ss->calcVertNormals) { - int lvl = ss->subdivLevels; - int edgeSize = 1 + (1<<lvl); - int gridSize = 1 + (1<<(lvl-1)); - int normalDataOffset = ss->normalDataOffset; + /* average */ + for (i=0; i<numEffectedV; i++) { + CCGVert *v = effectedV[i]; + VertDataMulN(VERT_getCo(v, lvl), 1.0f/v->numFaces); + } - for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { - CCGFace *f = (CCGFace*) effectedF[ptrIdx]; - int S, x, y; + for (i=0; i<numEffectedE; i++) { + CCGEdge *e = effectedE[i]; - for (S=0; S<f->numVerts; S++) { - for (y=0; y<gridSize-1; y++) - for (x=0; x<gridSize-1; x++) - NormZero(FACE_getIFNo(f, lvl, S, x, y)); + VertDataCopy(EDGE_getCo(e, lvl, 0), VERT_getCo(e->v0, lvl)); + VertDataCopy(EDGE_getCo(e, lvl, edgeSize-1), VERT_getCo(e->v1, lvl)); - if (FACE_getEdges(f)[(S-1+f->numVerts)%f->numVerts]->flags&Edge_eEffected) - for (x=0; x<gridSize-1; x++) - NormZero(FACE_getIFNo(f, lvl, S, x, gridSize-1)); - if (FACE_getEdges(f)[S]->flags&Edge_eEffected) - for (y=0; y<gridSize-1; y++) - NormZero(FACE_getIFNo(f, lvl, S, gridSize-1, y)); - if (FACE_getVerts(f)[S]->flags&Vert_eEffected) - NormZero(FACE_getIFNo(f, lvl, S, gridSize-1, gridSize-1)); - } - } + for (x=1; x<edgeSize-1; x++) + VertDataMulN(EDGE_getCo(e, lvl, x), 1.0f/e->numFaces); + } - for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { - CCGFace *f = (CCGFace*) effectedF[ptrIdx]; - int S, x, y; - float no[3]; + /* copy */ + for (i=0; i<numEffectedF; i++) { + CCGFace *f = effectedF[i]; - for (S=0; S<f->numVerts; S++) { - int yLimit = !(FACE_getEdges(f)[(S-1+f->numVerts)%f->numVerts]->flags&Edge_eEffected); - int xLimit = !(FACE_getEdges(f)[S]->flags&Edge_eEffected); - int yLimitNext = xLimit; - int xLimitPrev = yLimit; - - for (y=0; y<gridSize - 1; y++) { - for (x=0; x<gridSize - 1; x++) { - int xPlusOk = (!xLimit || x<gridSize-2); - int yPlusOk = (!yLimit || y<gridSize-2); - - FACE_calcIFNo(f, lvl, S, x, y, no); - - NormAdd(FACE_getIFNo(f, lvl, S, x+0, y+0), no); - if (xPlusOk) - NormAdd(FACE_getIFNo(f, lvl, S, x+1, y+0), no); - if (yPlusOk) - NormAdd(FACE_getIFNo(f, lvl, S, x+0, y+1), no); - if (xPlusOk && yPlusOk) { - if (x<gridSize-2 || y<gridSize-2 || FACE_getVerts(f)[S]->flags&Vert_eEffected) { - NormAdd(FACE_getIFNo(f, lvl, S, x+1, y+1), no); - } - } + VertDataMulN(FACE_getCenterData(f), 1.0f/f->numVerts); - if (x==0 && y==0) { - int K; + for (S=0; S<f->numVerts; S++) + if (FACE_getEdges(f)[S]->flags&Edge_eEffected) + for (x=1; x<gridSize-1; x++) + VertDataMulN(FACE_getIECo(f, lvl, S, x), 0.5f); - if (!yLimitNext || 1<gridSize-1) - NormAdd(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, 1), no); - if (!xLimitPrev || 1<gridSize-1) - NormAdd(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, 1, 0), no); + for (S=0; S<f->numVerts; S++) { + int prevS = (S+f->numVerts-1)%f->numVerts; + CCGEdge *e = FACE_getEdges(f)[S]; + CCGEdge *prevE = FACE_getEdges(f)[prevS]; - for (K=0; K<f->numVerts; K++) { - if (K!=S) { - NormAdd(FACE_getIFNo(f, lvl, K, 0, 0), no); - } - } - } else if (y==0) { - NormAdd(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, x), no); - if (!yLimitNext || x<gridSize-2) - NormAdd(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, x+1), no); - } else if (x==0) { - NormAdd(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, y, 0), no); - if (!xLimitPrev || y<gridSize-2) - NormAdd(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, y+1, 0), no); - } - } - } + VertDataCopy(FACE_getIFCo(f, lvl, S, 0, 0), FACE_getCenterData(f)); + VertDataCopy(FACE_getIFCo(f, lvl, S, cornerIdx, cornerIdx), VERT_getCo(FACE_getVerts(f)[S], lvl)); + + for (x=1; x<gridSize-1; x++) { + VertDataCopy(FACE_getIFCo(f, lvl, S, x, 0), FACE_getIECo(f, lvl, S, x)); + VertDataCopy(FACE_getIFCo(f, lvl, S, 0, x), FACE_getIECo(f, lvl, prevS, x)); } + + for (x=0; x<gridSize-1; x++) { + int eI = gridSize-1-x; + VertDataCopy(FACE_getIFCo(f, lvl, S, cornerIdx, x), _edge_getCoVert(e, FACE_getVerts(f)[S], lvl, eI,vertDataSize)); + VertDataCopy(FACE_getIFCo(f, lvl, S, x, cornerIdx), _edge_getCoVert(prevE, FACE_getVerts(f)[S], lvl, eI,vertDataSize)); + } + + VertDataCopy(FACE_getIECo(f, lvl, S, 0), FACE_getCenterData(f)); + VertDataCopy(FACE_getIECo(f, lvl, S, gridSize-1), FACE_getIFCo(f, lvl, S, gridSize-1, 0)); } - // XXX can I reduce the number of normalisations here? - for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) { - CCGVert *v = (CCGVert*) effectedV[ptrIdx]; - float length, *no = _vert_getNo(v, lvl, vertDataSize, normalDataOffset); + } - NormZero(no); + for (i=0; i<numEffectedV; i++) + effectedV[i]->flags = 0; + for (i=0; i<numEffectedE; i++) + effectedE[i]->flags = 0; + for (i=0; i<numEffectedF; i++) + effectedF[i]->flags = 0; - for (i=0; i<v->numFaces; i++) { - CCGFace *f = v->faces[i]; - NormAdd(no, FACE_getIFNo(f, lvl, _face_getVertIndex(f,v), gridSize-1, gridSize-1)); - } + CCGSUBSURF_free(ss, effectedE); + CCGSUBSURF_free(ss, effectedV); + if(freeF) CCGSUBSURF_free(ss, effectedF); - length = sqrt(no[0]*no[0] + no[1]*no[1] + no[2]*no[2]); + return eCCGError_None; +} - if (length>EPSILON) { - float invLength = 1.0f/length; - no[0] *= invLength; - no[1] *= invLength; - no[2] *= invLength; - } else { - NormZero(no); - } +/* update normals for specified faces */ +CCGError ccgSubSurf_updateNormals(CCGSubSurf *ss, CCGFace **effectedF, int numEffectedF) { + CCGVert **effectedV; + CCGEdge **effectedE; + int i, numEffectedV, numEffectedE, freeF; - for (i=0; i<v->numFaces; i++) { - CCGFace *f = v->faces[i]; - NormCopy(FACE_getIFNo(f, lvl, _face_getVertIndex(f,v), gridSize-1, gridSize-1), no); - } - } - for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { - CCGEdge *e = (CCGEdge*) effectedE[ptrIdx]; + ccgSubSurf__allFaces(ss, &effectedF, &numEffectedF, &freeF); + ccgSubSurf__effectedFaceNeighbours(ss, effectedF, numEffectedF, + &effectedV, &numEffectedV, &effectedE, &numEffectedE); - if (e->numFaces) { - CCGFace *fLast = e->faces[e->numFaces-1]; - int x; + if (ss->calcVertNormals) + ccgSubSurf__calcVertNormals(ss, + effectedV, effectedE, effectedF, + numEffectedV, numEffectedE, numEffectedF); - for (i=0; i<e->numFaces-1; i++) { - CCGFace *f = e->faces[i]; + for (i=0; i<numEffectedV; i++) + effectedV[i]->flags = 0; + for (i=0; i<numEffectedE; i++) + effectedE[i]->flags = 0; + for (i=0; i<numEffectedF; i++) + effectedF[i]->flags = 0; - for (x=1; x<edgeSize-1; x++) { - NormAdd(_face_getIFNoEdge(fLast, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset), - _face_getIFNoEdge(f, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset)); - } - } + CCGSUBSURF_free(ss, effectedE); + CCGSUBSURF_free(ss, effectedV); + if(freeF) CCGSUBSURF_free(ss, effectedF); - for (i=0; i<e->numFaces-1; i++) { - CCGFace *f = e->faces[i]; + return eCCGError_None; +} - for (x=1; x<edgeSize-1; x++) { - NormCopy(_face_getIFNoEdge(f, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset), - _face_getIFNoEdge(fLast, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset)); - } - } - } - } - for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { - CCGFace *f = (CCGFace*) effectedF[ptrIdx]; - int S; +/* compute subdivision levels from a given starting point, used by + multires subdivide/propagate, by filling in coordinates at a + certain level, and then subdividing that up to the highest level */ +CCGError ccgSubSurf_updateLevels(CCGSubSurf *ss, int lvl, CCGFace **effectedF, int numEffectedF) +{ + CCGVert **effectedV; + CCGEdge **effectedE; + int numEffectedV, numEffectedE, freeF, i; + int curLvl, subdivLevels = ss->subdivLevels; - for (S=0; S<f->numVerts; S++) { - NormCopy(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, gridSize-1), - FACE_getIFNo(f, lvl, S, gridSize-1, 0)); - } - } - for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) { - CCGFace *f = (CCGFace*) effectedF[ptrIdx]; - int S, x, y; + ccgSubSurf__allFaces(ss, &effectedF, &numEffectedF, &freeF); + ccgSubSurf__effectedFaceNeighbours(ss, effectedF, numEffectedF, + &effectedV, &numEffectedV, &effectedE, &numEffectedE); - for (S=0; S<f->numVerts; S++) { - for (y=0; y<gridSize; y++) { - for (x=0; x<gridSize; x++) { - float *no = FACE_getIFNo(f, lvl, S, x, y); - float length = sqrt(no[0]*no[0] + no[1]*no[1] + no[2]*no[2]); - - if (length>EPSILON) { - float invLength = 1.0f/length; - no[0] *= invLength; - no[1] *= invLength; - no[2] *= invLength; - } else { - NormZero(no); - } - } - } - } - } + for (curLvl=lvl; curLvl<subdivLevels; curLvl++) { + ccgSubSurf__calcSubdivLevel(ss, + effectedV, effectedE, effectedF, + numEffectedV, numEffectedE, numEffectedF, curLvl); } -#undef FACE_getIFNo - for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) { - CCGVert *v = effectedV[ptrIdx]; - v->flags = 0; - } - for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) { - CCGEdge *e = effectedE[ptrIdx]; - e->flags = 0; - } + for (i=0; i<numEffectedV; i++) + effectedV[i]->flags = 0; + for (i=0; i<numEffectedE; i++) + effectedE[i]->flags = 0; + for (i=0; i<numEffectedF; i++) + effectedF[i]->flags = 0; + + CCGSUBSURF_free(ss, effectedE); + CCGSUBSURF_free(ss, effectedV); + if(freeF) CCGSUBSURF_free(ss, effectedF); + + return eCCGError_None; +} #undef VERT_getCo #undef EDGE_getCo #undef FACE_getIECo #undef FACE_getIFCo - CCGSUBSURF_free(ss, effectedF); - CCGSUBSURF_free(ss, effectedE); - CCGSUBSURF_free(ss, effectedV); -} - /*** External API accessor functions ***/ int ccgSubSurf_getNumVerts(CCGSubSurf *ss) { diff --git a/source/blender/blenkernel/intern/CCGSubSurf.h b/source/blender/blenkernel/intern/CCGSubSurf.h index fbd0aecc0a5..12212c7a37b 100644 --- a/source/blender/blenkernel/intern/CCGSubSurf.h +++ b/source/blender/blenkernel/intern/CCGSubSurf.h @@ -59,6 +59,12 @@ CCGError ccgSubSurf_syncFaceDel (CCGSubSurf *ss, CCGFaceHDL fHDL); CCGError ccgSubSurf_processSync (CCGSubSurf *ss); +CCGError ccgSubSurf_updateFromFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces); +CCGError ccgSubSurf_updateToFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces); +CCGError ccgSubSurf_updateNormals(CCGSubSurf *ss, CCGFace **faces, int numFaces); +CCGError ccgSubSurf_updateLevels(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces); +CCGError ccgSubSurf_stitchFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces); + CCGError ccgSubSurf_setSubdivisionLevels (CCGSubSurf *ss, int subdivisionLevels); CCGError ccgSubSurf_setAllowEdgeCreation (CCGSubSurf *ss, int allowEdgeCreation, float defaultCreaseValue, void *defaultUserData); diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c index a681b539097..a614404dc5e 100644 --- a/source/blender/blenkernel/intern/DerivedMesh.c +++ b/source/blender/blenkernel/intern/DerivedMesh.c @@ -1555,7 +1555,7 @@ DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, Modifier md->scene= scene; if (!(md->mode&eModifierMode_Realtime)) return NULL; - if (mti->isDisabled && mti->isDisabled(md)) return NULL; + if (mti->isDisabled && mti->isDisabled(md, 0)) return NULL; if (mti->type==eModifierTypeType_OnlyDeform) { int numVerts; @@ -1906,7 +1906,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos mask &= ~CD_MASK_ORCO; DM_set_only_copy(orcodm, mask); - ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, !inputVertexCos); + ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, 0); if(ndm) { /* if the modifier returned a new dm, release the old one */ diff --git a/source/blender/blenkernel/intern/brush.c b/source/blender/blenkernel/intern/brush.c index a9d72d93091..09d73f20b9a 100644 --- a/source/blender/blenkernel/intern/brush.c +++ b/source/blender/blenkernel/intern/brush.c @@ -75,13 +75,14 @@ Brush *add_brush(const char *name) brush->rgb[2]= 1.0f; brush->alpha= 0.2f; brush->size= 25; - brush->spacing= 10.0f; + brush->spacing= 7.5f; brush->smooth_stroke_radius= 75; brush->smooth_stroke_factor= 0.9; brush->rate= 0.1f; brush->jitter= 0.0f; brush->clone.alpha= 0.5; brush->sculpt_tool = SCULPT_TOOL_DRAW; + brush->flag |= BRUSH_SPACE; brush_curve_preset(brush, BRUSH_PRESET_SMOOTH); diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c index 30d1b65cc8b..77ad9fb7a7b 100644 --- a/source/blender/blenkernel/intern/cdderivedmesh.c +++ b/source/blender/blenkernel/intern/cdderivedmesh.c @@ -49,7 +49,7 @@ #include "BLI_blenlib.h" #include "BLI_edgehash.h" #include "BLI_editVert.h" -#include "BLI_ghash.h" +#include "BLI_pbvh.h" #include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" @@ -77,6 +77,12 @@ typedef struct { MVert *mvert; MEdge *medge; MFace *mface; + + /* Cached */ + struct PBVH *pbvh; + /* Mesh connectivity */ + struct ListBase *fmap; + struct IndexNode *fmap_mem; } CDDerivedMesh; /**************** DerivedMesh interface functions ****************/ @@ -171,6 +177,33 @@ static void cdDM_getVertNo(DerivedMesh *dm, int index, float no_r[3]) no_r[2] = no[2]/32767.f; } +static ListBase *cdDM_getFaceMap(DerivedMesh *dm) +{ + CDDerivedMesh *cddm = (CDDerivedMesh*) dm; + + if(!cddm->fmap) { + create_vert_face_map(&cddm->fmap, &cddm->fmap_mem, cddm->mface, + dm->getNumVerts(dm), dm->getNumFaces(dm)); + } + + return cddm->fmap; +} + +static struct PBVH *cdDM_getPBVH(Object *ob, DerivedMesh *dm) +{ + CDDerivedMesh *cddm = (CDDerivedMesh*) dm; + + if(!cddm->pbvh && ob->type == OB_MESH) { + Mesh *me= ob->data; + + cddm->pbvh = BLI_pbvh_new(); + BLI_pbvh_build_mesh(cddm->pbvh, me->mface, me->mvert, + me->totface, me->totvert); + } + + return cddm->pbvh; +} + static void cdDM_drawVerts(DerivedMesh *dm) { CDDerivedMesh *cddm = (CDDerivedMesh*) dm; @@ -360,7 +393,9 @@ static void cdDM_drawLooseEdges(DerivedMesh *dm) } } -static void cdDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) +static void cdDM_drawFacesSolid(DerivedMesh *dm, + float (*partial_redraw_planes)[4], + int fast, int (*setMaterial)(int, void *attribs)) { CDDerivedMesh *cddm = (CDDerivedMesh*) dm; MVert *mvert = cddm->mvert; @@ -376,6 +411,20 @@ static void cdDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void *a glVertex3fv(mvert[index].co); \ } + if(cddm->pbvh) { + float (*face_nors)[3] = CustomData_get_layer(&dm->faceData, CD_NORMAL); + + /* should be per face */ + if(dm->numFaceData && mface->flag & ME_SMOOTH) + glShadeModel(GL_SMOOTH); + + BLI_pbvh_draw(cddm->pbvh, partial_redraw_planes, face_nors); + + glShadeModel(GL_FLAT); + + return; + } + if( GPU_buffer_legacy(dm) ) { DEBUG_VBO( "Using legacy code. cdDM_drawFacesSolid\n" ); glBegin(glmode = GL_QUADS); @@ -886,7 +935,7 @@ static void cdDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, vo continue; } else if(setDrawOptions) { - orig = index[a]; + orig = (index)? index[a]: a; if(orig == ORIGINDEX_NONE) continue; @@ -1275,12 +1324,21 @@ static void cdDM_foreachMappedFaceCenter( } } +static void cdDM_free_internal(CDDerivedMesh *cddm) +{ + if(cddm->pbvh) BLI_pbvh_free(cddm->pbvh); + if(cddm->fmap) MEM_freeN(cddm->fmap); + if(cddm->fmap_mem) MEM_freeN(cddm->fmap_mem); +} + static void cdDM_release(DerivedMesh *dm) { CDDerivedMesh *cddm = (CDDerivedMesh*)dm; - if (DM_release(dm)) + if (DM_release(dm)) { + cdDM_free_internal(cddm); MEM_freeN(cddm); + } } /**************** CDDM interface functions ****************/ @@ -1315,6 +1373,9 @@ static CDDerivedMesh *cdDM_create(const char *desc) dm->getVertCo = cdDM_getVertCo; dm->getVertNo = cdDM_getVertNo; + dm->getPBVH = cdDM_getPBVH; + dm->getFaceMap = cdDM_getFaceMap; + dm->drawVerts = cdDM_drawVerts; dm->drawUVEdges = cdDM_drawUVEdges; @@ -1366,17 +1427,12 @@ DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob) CDDerivedMesh *cddm = cdDM_create("CDDM_from_mesh dm"); DerivedMesh *dm = &cddm->dm; CustomDataMask mask = CD_MASK_MESH & (~CD_MASK_MDISPS); - int i, *index, alloctype; + int alloctype; - /* this does a referenced copy, the only new layers being ORIGINDEX, - * with an exception for fluidsim */ + /* this does a referenced copy, with an exception for fluidsim */ DM_init(dm, mesh->totvert, mesh->totedge, mesh->totface); - CustomData_add_layer(&dm->vertData, CD_ORIGINDEX, CD_CALLOC, NULL, mesh->totvert); - CustomData_add_layer(&dm->edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, mesh->totedge); - CustomData_add_layer(&dm->faceData, CD_ORIGINDEX, CD_CALLOC, NULL, mesh->totface); - dm->deformedOnly = 1; alloctype= CD_REFERENCE; @@ -1392,18 +1448,6 @@ DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob) cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE); cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE); - index = CustomData_get_layer(&dm->vertData, CD_ORIGINDEX); - for(i = 0; i < mesh->totvert; ++i, ++index) - *index = i; - - index = CustomData_get_layer(&dm->edgeData, CD_ORIGINDEX); - for(i = 0; i < mesh->totedge; ++i, ++index) - *index = i; - - index = CustomData_get_layer(&dm->faceData, CD_ORIGINDEX); - for(i = 0; i < mesh->totface; ++i, ++index) - *index = i; - return dm; } @@ -1514,6 +1558,11 @@ DerivedMesh *CDDM_copy(DerivedMesh *source) int numEdges = source->numEdgeData; int numFaces = source->numFaceData; + /* ensure these are created if they are made on demand */ + source->getVertDataArray(source, CD_ORIGINDEX); + source->getEdgeDataArray(source, CD_ORIGINDEX); + source->getFaceDataArray(source, CD_ORIGINDEX); + /* this initializes dm, and copies all non mvert/medge/mface layers */ DM_from_template(dm, source, numVerts, numEdges, numFaces); dm->deformedOnly = source->deformedOnly; @@ -1548,6 +1597,13 @@ DerivedMesh *CDDM_from_template(DerivedMesh *source, CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges); CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL, numFaces); + if(!CustomData_get_layer(&dm->vertData, CD_ORIGINDEX)) + CustomData_add_layer(&dm->vertData, CD_ORIGINDEX, CD_CALLOC, NULL, numVerts); + if(!CustomData_get_layer(&dm->edgeData, CD_ORIGINDEX)) + CustomData_add_layer(&dm->edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges); + if(!CustomData_get_layer(&dm->faceData, CD_ORIGINDEX)) + CustomData_add_layer(&dm->faceData, CD_ORIGINDEX, CD_CALLOC, NULL, numFaces); + cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT); cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE); cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE); @@ -1754,207 +1810,3 @@ MFace *CDDM_get_faces(DerivedMesh *dm) return ((CDDerivedMesh*)dm)->mface; } -/* Multires DerivedMesh, extends CDDM */ -typedef struct MultiresDM { - CDDerivedMesh cddm; - - MultiresModifierData *mmd; - int local_mmd; - - int lvl, totlvl; - float (*orco)[3]; - MVert *subco; - - ListBase *vert_face_map, *vert_edge_map; - IndexNode *vert_face_map_mem, *vert_edge_map_mem; - int *face_offsets; - - Object *ob; - int modified; - - void (*update)(DerivedMesh*); -} MultiresDM; - -static void MultiresDM_release(DerivedMesh *dm) -{ - MultiresDM *mrdm = (MultiresDM*)dm; - int mvert_layer; - - /* Before freeing, need to update the displacement map */ - if(dm->needsFree && mrdm->modified) { - /* Check that mmd still exists */ - if(!mrdm->local_mmd && BLI_findindex(&mrdm->ob->modifiers, mrdm->mmd) < 0) - mrdm->mmd = NULL; - if(mrdm->mmd) - mrdm->update(dm); - } - - /* If the MVert data is being used as the sculpt undo store, don't free it */ - mvert_layer = CustomData_get_layer_index(&dm->vertData, CD_MVERT); - if(mvert_layer != -1) { - CustomDataLayer *cd = &dm->vertData.layers[mvert_layer]; - if(mrdm->mmd && cd->data == mrdm->mmd->undo_verts) - cd->flag |= CD_FLAG_NOFREE; - } - - if(DM_release(dm)) { - MEM_freeN(mrdm->subco); - MEM_freeN(mrdm->orco); - if(mrdm->vert_face_map) - MEM_freeN(mrdm->vert_face_map); - if(mrdm->vert_face_map_mem) - MEM_freeN(mrdm->vert_face_map_mem); - if(mrdm->vert_edge_map) - MEM_freeN(mrdm->vert_edge_map); - if(mrdm->vert_edge_map_mem) - MEM_freeN(mrdm->vert_edge_map_mem); - if(mrdm->face_offsets) - MEM_freeN(mrdm->face_offsets); - MEM_freeN(mrdm); - } -} - -DerivedMesh *MultiresDM_new(MultiresSubsurf *ms, DerivedMesh *orig, int numVerts, int numEdges, int numFaces) -{ - MultiresDM *mrdm = MEM_callocN(sizeof(MultiresDM), "MultiresDM"); - CDDerivedMesh *cddm = cdDM_create("MultiresDM CDDM"); - DerivedMesh *dm = NULL; - - mrdm->cddm = *cddm; - MEM_freeN(cddm); - dm = &mrdm->cddm.dm; - - mrdm->mmd = ms->mmd; - mrdm->ob = ms->ob; - mrdm->local_mmd = ms->local_mmd; - - if(dm) { - MDisps *disps; - MVert *mvert; - int i; - - DM_from_template(dm, orig, numVerts, numEdges, numFaces); - CustomData_free_layers(&dm->faceData, CD_MDISPS, numFaces); - - disps = CustomData_get_layer(&orig->faceData, CD_MDISPS); - if(disps) - CustomData_add_layer(&dm->faceData, CD_MDISPS, CD_REFERENCE, disps, numFaces); - - - mvert = CustomData_get_layer(&orig->vertData, CD_MVERT); - mrdm->orco = MEM_callocN(sizeof(float) * 3 * orig->getNumVerts(orig), "multires orco"); - for(i = 0; i < orig->getNumVerts(orig); ++i) - copy_v3_v3(mrdm->orco[i], mvert[i].co); - } - else - DM_init(dm, numVerts, numEdges, numFaces); - - CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL, numVerts); - CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges); - CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL, numFaces); - - mrdm->cddm.mvert = CustomData_get_layer(&dm->vertData, CD_MVERT); - mrdm->cddm.medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE); - mrdm->cddm.mface = CustomData_get_layer(&dm->faceData, CD_MFACE); - - mrdm->lvl = ms->mmd->lvl; - mrdm->totlvl = ms->mmd->totlvl; - mrdm->subco = MEM_callocN(sizeof(MVert)*numVerts, "multires subdivided verts"); - mrdm->modified = 0; - - dm->release = MultiresDM_release; - - return dm; -} - -Mesh *MultiresDM_get_mesh(DerivedMesh *dm) -{ - return get_mesh(((MultiresDM*)dm)->ob); -} - -Object *MultiresDM_get_object(DerivedMesh *dm) -{ - return ((MultiresDM*)dm)->ob; -} - -void *MultiresDM_get_orco(DerivedMesh *dm) -{ - return ((MultiresDM*)dm)->orco; - -} - -MVert *MultiresDM_get_subco(DerivedMesh *dm) -{ - return ((MultiresDM*)dm)->subco; -} - -int MultiresDM_get_totlvl(DerivedMesh *dm) -{ - return ((MultiresDM*)dm)->totlvl; -} - -int MultiresDM_get_lvl(DerivedMesh *dm) -{ - return ((MultiresDM*)dm)->lvl; -} - -void MultiresDM_set_orco(DerivedMesh *dm, float (*orco)[3]) -{ - ((MultiresDM*)dm)->orco = orco; -} - -void MultiresDM_set_update(DerivedMesh *dm, void (*update)(DerivedMesh*)) -{ - ((MultiresDM*)dm)->update = update; -} - -ListBase *MultiresDM_get_vert_face_map(DerivedMesh *dm) -{ - MultiresDM *mrdm = (MultiresDM*)dm; - Mesh *me = mrdm->ob->data; - - if(!mrdm->vert_face_map) - create_vert_face_map(&mrdm->vert_face_map, &mrdm->vert_face_map_mem, me->mface, - me->totvert, me->totface); - - return mrdm->vert_face_map; -} - -ListBase *MultiresDM_get_vert_edge_map(DerivedMesh *dm) -{ - MultiresDM *mrdm = (MultiresDM*)dm; - Mesh *me = mrdm->ob->data; - - if(!mrdm->vert_edge_map) - create_vert_edge_map(&mrdm->vert_edge_map, &mrdm->vert_edge_map_mem, me->medge, - me->totvert, me->totedge); - - return mrdm->vert_edge_map; -} - -int *MultiresDM_get_face_offsets(DerivedMesh *dm) -{ - MultiresDM *mrdm = (MultiresDM*)dm; - Mesh *me = mrdm->ob->data; - int i, accum = 0; - - if(!mrdm->face_offsets) { - int len = (int)pow(2, mrdm->lvl - 2) - 1; - int area = len * len; - int t = 1 + len * 3 + area * 3, q = t + len + area; - - mrdm->face_offsets = MEM_callocN(sizeof(int) * me->totface, "mrdm face offsets"); - for(i = 0; i < me->totface; ++i) { - mrdm->face_offsets[i] = accum; - - accum += (me->mface[i].v4 ? q : t); - } - } - - return mrdm->face_offsets; -} - -void MultiresDM_mark_as_modified(DerivedMesh *dm) -{ - ((MultiresDM*)dm)->modified = 1; -} diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c index 0f3213cbc5d..aff3bf058fd 100644 --- a/source/blender/blenkernel/intern/constraint.c +++ b/source/blender/blenkernel/intern/constraint.c @@ -440,15 +440,14 @@ static void contarget_get_mesh_mat (Scene *scene, Object *ob, char *substring, f /* only continue if there's a valid DerivedMesh */ if (dm) { MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT); - int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX); int numVerts = dm->getNumVerts(dm); int i, j, count = 0; float co[3], nor[3]; - /* check that dvert and index are valid pointers (just in case) */ - if (dvert && index) { + /* check that dvert is a valid pointers (just in case) */ + if (dvert) { /* get the average of all verts with that are in the vertex-group */ - for (i = 0; i < numVerts; i++, index++) { + for (i = 0; i < numVerts; i++) { for (j = 0; j < dvert[i].totweight; j++) { /* does this vertex belong to nominated vertex group? */ if (dvert[i].dw[j].def_nr == dgroup) { diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c index 7b754025b6c..d9e85d5d412 100644 --- a/source/blender/blenkernel/intern/customdata.c +++ b/source/blender/blenkernel/intern/customdata.c @@ -32,21 +32,26 @@ * */ -#include "BKE_customdata.h" -#include "BKE_utildefines.h" // CLAMP -#include "BLI_math.h" -#include "BLI_blenlib.h" -#include "BLI_linklist.h" -#include "BLI_mempool.h" +#include <math.h> +#include <string.h> + +#include "MEM_guardedalloc.h" #include "DNA_customdata_types.h" #include "DNA_listBase.h" #include "DNA_meshdata_types.h" +#include "DNA_ID.h" -#include "MEM_guardedalloc.h" +#include "BLI_blenlib.h" +#include "BLI_linklist.h" +#include "BLI_math.h" +#include "BLI_mempool.h" +#include "BLI_string.h" -#include <math.h> -#include <string.h> +#include "BKE_customdata.h" +#include "BKE_customdata_file.h" +#include "BKE_global.h" +#include "BKE_utildefines.h" /* number of layers to add when growing a CustomData object */ #define CUSTOMDATA_GROW 5 @@ -89,6 +94,15 @@ typedef struct LayerTypeInfo { /* a function to set a layer's data to default values. if NULL, the default is assumed to be all zeros */ void (*set_default)(void *data, int count); + + /* a function to read data from a cdf file */ + int (*read)(CDataFile *cdf, void *data, int count); + + /* a function to write data to a cdf file */ + int (*write)(CDataFile *cdf, void *data, int count); + + /* a function to determine file size */ + size_t (*filesize)(CDataFile *cdf, void *data, int count); } LayerTypeInfo; static void layerCopy_mdeformvert(const void *source, void *dest, @@ -381,6 +395,7 @@ static void layerDefault_origspace_face(void *data, int count) osf[i] = default_osf; } +#if 0 /* Adapted from sculptmode.c */ static void mdisps_bilinear(float out[3], float (*disps)[3], int st, float u, float v) { @@ -426,23 +441,28 @@ static void mdisps_bilinear(float out[3], float (*disps)[3], int st, float u, fl add_v3_v3v3(out, d2[0], d2[1]); } +#endif + +static int mdisp_corners(MDisps *s) +{ + /* silly trick because we don't get it from callback */ + return (s->totdisp % (3*3) == 0)? 3: 4; +} static void layerSwap_mdisps(void *data, int *ci) { MDisps *s = data; float (*d)[3] = NULL; - int x, y, st; + int corners, cornersize, S; - if(!(ci[0] == 2 && ci[1] == 3 && ci[2] == 0 && ci[3] == 1)) return; + /* this function is untested .. */ + corners = mdisp_corners(s); + cornersize = s->totdisp/corners; d = MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap"); - st = sqrt(s->totdisp); - for(y = 0; y < st; ++y) { - for(x = 0; x < st; ++x) { - copy_v3_v3(d[(st - y - 1) * st + (st - x - 1)], s->disps[y * st + x]); - } - } + for(S = 0; S < corners; S++) + memcpy(d + cornersize*S, s->disps + cornersize*ci[S], cornersize*3*sizeof(float)); if(s->disps) MEM_freeN(s->disps); @@ -452,6 +472,8 @@ static void layerSwap_mdisps(void *data, int *ci) static void layerInterp_mdisps(void **sources, float *weights, float *sub_weights, int count, void *dest) { + // XXX +#if 0 MDisps *d = dest; MDisps *s = NULL; int st, stl; @@ -496,6 +518,7 @@ static void layerInterp_mdisps(void **sources, float *weights, float *sub_weight copy_v3_v3(d->disps[y * st + x], srcdisp); } } +#endif } static void layerCopy_mdisps(const void *source, void *dest, int count) @@ -530,6 +553,51 @@ static void layerFree_mdisps(void *data, int count, int size) } } +static int layerRead_mdisps(CDataFile *cdf, void *data, int count) +{ + MDisps *d = data; + int i; + + for(i = 0; i < count; ++i) { + if(!d[i].disps) + d[i].disps = MEM_callocN(sizeof(float)*3*d[i].totdisp, "mdisps read"); + + if(!cdf_read_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) { + printf("failed to read %d/%d %d\n", i, count, d[i].totdisp); + return 0; + } + } + + return 1; +} + +static int layerWrite_mdisps(CDataFile *cdf, void *data, int count) +{ + MDisps *d = data; + int i; + + for(i = 0; i < count; ++i) { + if(!cdf_write_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) { + printf("failed to write %d/%d %d\n", i, count, d[i].totdisp); + return 0; + } + } + + return 1; +} + +static size_t layerFilesize_mdisps(CDataFile *cdf, void *data, int count) +{ + MDisps *d = data; + size_t size = 0; + int i; + + for(i = 0; i < count; ++i) + size += d[i].totdisp*3*sizeof(float); + + return size; +} + /* --------- */ static void layerDefault_mloopcol(void *data, int count) @@ -723,7 +791,7 @@ const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = { {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL, layerDefault_mloopcol}, {sizeof(float)*3*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL}, {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps, - layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL}, + layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL, layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps}, {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol, layerSwap_mcol, layerDefault_mcol}, {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol, @@ -845,6 +913,14 @@ static void customData_free_layer__internal(CustomDataLayer *layer, int totelem) } } +static void CustomData_external_free(CustomData *data) +{ + if(data->external) { + MEM_freeN(data->external); + data->external= NULL; + } +} + void CustomData_free(CustomData *data, int totelem) { int i; @@ -855,6 +931,8 @@ void CustomData_free(CustomData *data, int totelem) if(data->layers) MEM_freeN(data->layers); + CustomData_external_free(data); + memset(data, 0, sizeof(*data)); } @@ -2230,3 +2308,236 @@ int CustomData_verify_versions(struct CustomData *data, int index) return keeplayer; } +/****************************** External Files *******************************/ + +static void customdata_external_filename(char filename[FILE_MAX], ID *id, CustomDataExternal *external) +{ + char *path = (id->lib)? id->lib->filename: G.sce; + + BLI_strncpy(filename, external->filename, FILE_MAX); + BLI_convertstringcode(filename, path); +} + +void CustomData_external_read(CustomData *data, ID *id, CustomDataMask mask, int totelem) +{ + CustomDataExternal *external= data->external; + CustomDataLayer *layer; + CDataFile *cdf; + CDataFileLayer *blay; + char filename[FILE_MAX]; + const LayerTypeInfo *typeInfo; + int i, update = 0; + + if(!external) + return; + + for(i=0; i<data->totlayer; i++) { + layer = &data->layers[i]; + typeInfo = layerType_getInfo(layer->type); + + if(!(mask & (1<<layer->type))); + else if(layer->flag & CD_FLAG_IN_MEMORY); + else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) + update= 1; + } + + if(!update) + return; + + customdata_external_filename(filename, id, external); + + cdf= cdf_create(CDF_TYPE_MESH); + if(!cdf_read_open(cdf, filename)) + return; + + for(i=0; i<data->totlayer; i++) { + layer = &data->layers[i]; + typeInfo = layerType_getInfo(layer->type); + + if(!(mask & (1<<layer->type))); + else if(layer->flag & CD_FLAG_IN_MEMORY); + else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) { + blay= cdf_layer_find(cdf, layer->type, layer->name); + + if(blay) { + if(cdf_read_layer(cdf, blay)) { + if(typeInfo->read(cdf, layer->data, totelem)); + else break; + layer->flag |= CD_FLAG_IN_MEMORY; + } + else + break; + } + } + } + + cdf_read_close(cdf); + cdf_free(cdf); +} + +void CustomData_external_write(CustomData *data, ID *id, CustomDataMask mask, int totelem, int free) +{ + CustomDataExternal *external= data->external; + CustomDataLayer *layer; + CDataFile *cdf; + CDataFileLayer *blay; + const LayerTypeInfo *typeInfo; + int i, update = 0; + char filename[FILE_MAX]; + + if(!external) + return; + + for(i=0; i<data->totlayer; i++) { + layer = &data->layers[i]; + typeInfo = layerType_getInfo(layer->type); + + if(!(mask & (1<<layer->type))); + else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) + update= 1; + } + + if(!update) + return; + + CustomData_external_read(data, id, mask, totelem); + + cdf= cdf_create(CDF_TYPE_MESH); + + for(i=0; i<data->totlayer; i++) { + layer = &data->layers[i]; + typeInfo = layerType_getInfo(layer->type); + + if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) + cdf_layer_add(cdf, layer->type, layer->name, + typeInfo->filesize(cdf, layer->data, totelem)); + } + + customdata_external_filename(filename, id, external); + if(!cdf_write_open(cdf, filename)) + return; + + for(i=0; i<data->totlayer; i++) { + layer = &data->layers[i]; + typeInfo = layerType_getInfo(layer->type); + + if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) { + blay= cdf_layer_find(cdf, layer->type, layer->name); + + if(cdf_write_layer(cdf, blay)) { + if(typeInfo->write(cdf, layer->data, totelem)); + else break; + } + else + break; + } + } + + if(i != data->totlayer) { + cdf_free(cdf); + return; + } + + for(i=0; i<data->totlayer; i++) { + layer = &data->layers[i]; + typeInfo = layerType_getInfo(layer->type); + + if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) { + if(free) { + if(typeInfo->free) + typeInfo->free(layer->data, totelem, typeInfo->size); + layer->flag &= ~CD_FLAG_IN_MEMORY; + } + } + } + + cdf_write_close(cdf); + cdf_free(cdf); +} + +void CustomData_external_add(CustomData *data, ID *id, int type, int totelem, const char *filename) +{ + CustomDataExternal *external= data->external; + CustomDataLayer *layer; + int layer_index; + + layer_index = CustomData_get_active_layer_index(data, type); + if(layer_index < 0) return; + + layer = &data->layers[layer_index]; + + if(layer->flag & CD_FLAG_EXTERNAL) + return; + + if(!external) { + external= MEM_callocN(sizeof(CustomDataExternal), "CustomDataExternal"); + BLI_strncpy(external->filename, filename, sizeof(external->filename)); + data->external= external; + } + + layer->flag |= CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY; +} + +void CustomData_external_remove(CustomData *data, ID *id, int type, int totelem) +{ + CustomDataExternal *external= data->external; + CustomDataLayer *layer; + //char filename[FILE_MAX]; + int layer_index; // i, remove_file; + + layer_index = CustomData_get_active_layer_index(data, type); + if(layer_index < 0) return; + + layer = &data->layers[layer_index]; + + if(!external) + return; + + if(layer->flag & CD_FLAG_EXTERNAL) { + if(!(layer->flag & CD_FLAG_IN_MEMORY)) + CustomData_external_read(data, id, (1<<layer->type), totelem); + + layer->flag &= ~CD_FLAG_EXTERNAL; + +#if 0 + remove_file= 1; + for(i=0; i<data->totlayer; i++) + if(data->layers[i].flag & CD_FLAG_EXTERNAL) + remove_file= 0; + + if(remove_file) { + customdata_external_filename(filename, id, external); + cdf_remove(filename); + CustomData_external_free(data); + } +#endif + } +} + +int CustomData_external_test(CustomData *data, int type) +{ + CustomDataLayer *layer; + int layer_index; + + layer_index = CustomData_get_active_layer_index(data, type); + if(layer_index < 0) return 0; + + layer = &data->layers[layer_index]; + return (layer->flag & CD_FLAG_EXTERNAL); +} + +#if 0 +void CustomData_external_remove_object(CustomData *data, ID *id) +{ + CustomDataExternal *external= data->external; + char filename[FILE_MAX]; + + if(!external) + return; + + customdata_external_filename(filename, id, external); + cdf_remove(filename); + CustomData_external_free(data); +} +#endif + diff --git a/source/blender/blenkernel/intern/customdata_file.c b/source/blender/blenkernel/intern/customdata_file.c new file mode 100644 index 00000000000..b58ada878de --- /dev/null +++ b/source/blender/blenkernel/intern/customdata_file.c @@ -0,0 +1,446 @@ +/* + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_fileops.h" +#include "BLI_string.h" + +#include "BKE_customdata_file.h" +#include "BKE_global.h" +#include "BKE_utildefines.h" + +/************************* File Format Definitions ***************************/ + +#define CDF_ENDIAN_LITTLE 0 +#define CDF_ENDIAN_BIG 1 + +#define CDF_DATA_FLOAT 0 + +typedef struct CDataFileHeader { + char ID[4]; /* "BCDF" */ + char endian; /* little, big */ + char version; /* non-compatible versions */ + char subversion; /* compatible sub versions */ + char pad; /* padding */ + + int structbytes; /* size of this struct in bytes */ + int type; /* image, mesh */ + int totlayer; /* number of layers in the file */ +} CDataFileHeader; + +typedef struct CDataFileImageHeader { + int structbytes; /* size of this struct in bytes */ + int width; /* image width */ + int height; /* image height */ + int tile_size; /* tile size (required power of 2) */ +} CDataFileImageHeader; + +typedef struct CDataFileMeshHeader { + int structbytes; /* size of this struct in bytes */ +} CDataFileMeshHeader; + +struct CDataFileLayer { + int structbytes; /* size of this struct in bytes */ + int datatype; /* only float for now */ + uint64_t datasize; /* size of data in layer */ + int type; /* layer type */ + char name[CDF_LAYER_NAME_MAX]; /* layer name */ +}; + +/**************************** Other Definitions ******************************/ + +#define CDF_VERSION 0 +#define CDF_SUBVERSION 0 +#define CDF_TILE_SIZE 64 + +struct CDataFile { + int type; + + CDataFileHeader header; + union { + CDataFileImageHeader image; + CDataFileMeshHeader mesh; + } btype; + + CDataFileLayer *layer; + int totlayer; + + FILE *readf; + FILE *writef; + int switchendian; + size_t dataoffset; +}; + +/********************************* Create/Free *******************************/ + +static int cdf_endian(void) +{ + if(ENDIAN_ORDER == L_ENDIAN) + return CDF_ENDIAN_LITTLE; + else + return CDF_ENDIAN_BIG; +} + +/*static int cdf_data_type_size(int datatype) +{ + if(datatype == CDF_DATA_FLOAT) + return sizeof(float); + + return 0; +}*/ + +CDataFile *cdf_create(int type) +{ + CDataFile *cdf= MEM_callocN(sizeof(CDataFile), "CDataFile"); + + cdf->type= type; + + return cdf; +} + +void cdf_free(CDataFile *cdf) +{ + cdf_read_close(cdf); + cdf_write_close(cdf); + + if(cdf->layer) + MEM_freeN(cdf->layer); + + MEM_freeN(cdf); +} + +/********************************* Read/Write ********************************/ + +static int cdf_read_header(CDataFile *cdf) +{ + CDataFileHeader *header; + CDataFileImageHeader *image; + CDataFileMeshHeader *mesh; + CDataFileLayer *layer; + FILE *f= cdf->readf; + size_t offset = 0; + int a; + + header= &cdf->header; + + if(!fread(header, sizeof(CDataFileHeader), 1, cdf->readf)) + return 0; + + if(memcmp(header->ID, "BCDF", sizeof(header->ID)) != 0) + return 0; + if(header->version > CDF_VERSION) + return 0; + + cdf->switchendian= header->endian != cdf_endian(); + header->endian= cdf_endian(); + + if(cdf->switchendian) { + SWITCH_INT(header->type); + SWITCH_INT(header->totlayer); + SWITCH_INT(header->structbytes); + } + + if(!ELEM(header->type, CDF_TYPE_IMAGE, CDF_TYPE_MESH)) + return 0; + + offset += header->structbytes; + header->structbytes= sizeof(CDataFileHeader); + + if(fseek(f, offset, SEEK_SET) != 0) + return 0; + + if(header->type == CDF_TYPE_IMAGE) { + image= &cdf->btype.image; + if(!fread(image, sizeof(CDataFileImageHeader), 1, f)) + return 0; + + if(cdf->switchendian) { + SWITCH_INT(image->width); + SWITCH_INT(image->height); + SWITCH_INT(image->tile_size); + SWITCH_INT(image->structbytes); + } + + offset += image->structbytes; + image->structbytes= sizeof(CDataFileImageHeader); + } + else if(header->type == CDF_TYPE_MESH) { + mesh= &cdf->btype.mesh; + if(!fread(mesh, sizeof(CDataFileMeshHeader), 1, f)) + return 0; + + if(cdf->switchendian) + SWITCH_INT(mesh->structbytes); + + offset += mesh->structbytes; + mesh->structbytes= sizeof(CDataFileMeshHeader); + } + + if(fseek(f, offset, SEEK_SET) != 0) + return 0; + + cdf->layer= MEM_callocN(sizeof(CDataFileLayer)*header->totlayer, "CDataFileLayer"); + cdf->totlayer= header->totlayer; + + for(a=0; a<header->totlayer; a++) { + layer= &cdf->layer[a]; + + if(!fread(layer, sizeof(CDataFileLayer), 1, f)) + return 0; + + if(cdf->switchendian) { + SWITCH_INT(layer->type); + SWITCH_INT(layer->datatype); + SWITCH_INT64(layer->datasize); + SWITCH_INT(layer->structbytes); + } + + if(layer->datatype != CDF_DATA_FLOAT) + return 0; + + offset += layer->structbytes; + layer->structbytes= sizeof(CDataFileLayer); + + if(fseek(f, offset, SEEK_SET) != 0) + return 0; + } + + cdf->dataoffset= offset; + + return 1; +} + +static int cdf_write_header(CDataFile *cdf) +{ + CDataFileHeader *header; + CDataFileImageHeader *image; + CDataFileMeshHeader *mesh; + CDataFileLayer *layer; + FILE *f= cdf->writef; + int a; + + header= &cdf->header; + + if(!fwrite(header, sizeof(CDataFileHeader), 1, f)) + return 0; + + if(header->type == CDF_TYPE_IMAGE) { + image= &cdf->btype.image; + if(!fwrite(image, sizeof(CDataFileImageHeader), 1, f)) + return 0; + } + else if(header->type == CDF_TYPE_MESH) { + mesh= &cdf->btype.mesh; + if(!fwrite(mesh, sizeof(CDataFileMeshHeader), 1, f)) + return 0; + } + + for(a=0; a<header->totlayer; a++) { + layer= &cdf->layer[a]; + + if(!fwrite(layer, sizeof(CDataFileLayer), 1, f)) + return 0; + } + + return 1; +} + +int cdf_read_open(CDataFile *cdf, char *filename) +{ + FILE *f; + + f= fopen(filename, "rb"); + if(!f) + return 0; + + cdf->readf= f; + + if(!cdf_read_header(cdf)) { + cdf_read_close(cdf); + return 0; + } + + if(cdf->header.type != cdf->type) { + cdf_read_close(cdf); + return 0; + } + + return 1; +} + +int cdf_read_layer(CDataFile *cdf, CDataFileLayer *blay) +{ + size_t offset; + int a; + + /* seek to right location in file */ + offset= cdf->dataoffset; + for(a=0; a<cdf->totlayer; a++) { + if(&cdf->layer[a] == blay) + break; + else + offset += cdf->layer[a].datasize; + } + + return (fseek(cdf->readf, offset, SEEK_SET) == 0); +} + +int cdf_read_data(CDataFile *cdf, int size, void *data) +{ + float *fdata; + int a; + + /* read data */ + if(!fread(data, size, 1, cdf->readf)) + return 0; + + /* switch endian if necessary */ + if(cdf->switchendian) { + fdata= data; + + for(a=0; a<size/sizeof(float); a++) + SWITCH_INT(fdata[a]) + } + + return 1; +} + +void cdf_read_close(CDataFile *cdf) +{ + if(cdf->readf) { + fclose(cdf->readf); + cdf->readf= NULL; + } +} + +int cdf_write_open(CDataFile *cdf, char *filename) +{ + CDataFileHeader *header; + CDataFileImageHeader *image; + CDataFileMeshHeader *mesh; + FILE *f; + + f= fopen(filename, "wb"); + if(!f) + return 0; + + cdf->writef= f; + + /* fill header */ + header= &cdf->header; + strcpy(header->ID, "BCDF"); + header->endian= cdf_endian(); + header->version= CDF_VERSION; + header->subversion= CDF_SUBVERSION; + + header->structbytes= sizeof(CDataFileHeader); + header->type= cdf->type; + header->totlayer= cdf->totlayer; + + if(cdf->type == CDF_TYPE_IMAGE) { + /* fill image header */ + image= &cdf->btype.image; + image->structbytes= sizeof(CDataFileImageHeader); + image->tile_size= CDF_TILE_SIZE; + } + else if(cdf->type == CDF_TYPE_MESH) { + /* fill mesh header */ + mesh= &cdf->btype.mesh; + mesh->structbytes= sizeof(CDataFileMeshHeader); + } + + cdf_write_header(cdf); + + return 1; +} + +int cdf_write_layer(CDataFile *cdf, CDataFileLayer *blay) +{ + return 1; +} + +int cdf_write_data(CDataFile *cdf, int size, void *data) +{ + /* write data */ + if(!fwrite(data, size, 1, cdf->writef)) + return 0; + + return 1; +} + +void cdf_write_close(CDataFile *cdf) +{ + if(cdf->writef) { + fclose(cdf->writef); + cdf->writef= NULL; + } +} + +void cdf_remove(char *filename) +{ + BLI_delete(filename, 0, 0); +} + +/********************************** Layers ***********************************/ + +CDataFileLayer *cdf_layer_find(CDataFile *cdf, int type, char *name) +{ + CDataFileLayer *layer; + int a; + + for(a=0; a<cdf->totlayer; a++) { + layer= &cdf->layer[a]; + + if(layer->type == type && strcmp(layer->name, name) == 0) + return layer; + } + + return NULL; +} + +CDataFileLayer *cdf_layer_add(CDataFile *cdf, int type, char *name, size_t datasize) +{ + CDataFileLayer *newlayer, *layer; + + /* expand array */ + newlayer= MEM_callocN(sizeof(CDataFileLayer)*(cdf->totlayer+1), "CDataFileLayer"); + memcpy(newlayer, cdf->layer, sizeof(CDataFileLayer)*cdf->totlayer); + cdf->layer= newlayer; + + cdf->totlayer++; + + /* fill in new layer */ + layer= &cdf->layer[cdf->totlayer-1]; + layer->structbytes= sizeof(CDataFileLayer); + layer->datatype= CDF_DATA_FLOAT; + layer->datasize= datasize; + layer->type= type; + BLI_strncpy(layer->name, name, CDF_LAYER_NAME_MAX); + + return layer; +} + diff --git a/source/blender/blenkernel/intern/displist.c b/source/blender/blenkernel/intern/displist.c index 48fb283c404..b35cf917895 100644 --- a/source/blender/blenkernel/intern/displist.c +++ b/source/blender/blenkernel/intern/displist.c @@ -1225,7 +1225,7 @@ static ModifierData *curve_get_tesselate_point(Object *ob, int forRender, int ed ModifierTypeInfo *mti = modifierType_getInfo(md->type); if ((md->mode & required_mode) != required_mode) continue; - if (mti->isDisabled && mti->isDisabled(md)) continue; + if (mti->isDisabled && mti->isDisabled(md, forRender)) continue; if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) { preTesselatePoint = md; @@ -1275,7 +1275,7 @@ static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, fl md->scene= scene; if ((md->mode & required_mode) != required_mode) continue; - if (mti->isDisabled && mti->isDisabled(md)) continue; + if (mti->isDisabled && mti->isDisabled(md, forRender)) continue; if (mti->type!=eModifierTypeType_OnlyDeform) continue; if (!deformedVerts) { @@ -1330,7 +1330,7 @@ static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispba md->scene= scene; if ((md->mode & required_mode) != required_mode) continue; - if (mti->isDisabled && mti->isDisabled(md)) continue; + if (mti->isDisabled && mti->isDisabled(md, forRender)) continue; if (mti->type!=eModifierTypeType_OnlyDeform && mti->type!=eModifierTypeType_DeformOrConstruct) continue; /* need to put all verts in 1 block for curve deform */ diff --git a/source/blender/blenkernel/intern/lattice.c b/source/blender/blenkernel/intern/lattice.c index dc548edbb25..e963b2e9fb6 100644 --- a/source/blender/blenkernel/intern/lattice.c +++ b/source/blender/blenkernel/intern/lattice.c @@ -1002,7 +1002,7 @@ void lattice_calc_modifiers(Scene *scene, Object *ob) if (!(md->mode&eModifierMode_Realtime)) continue; if (editmode && !(md->mode&eModifierMode_Editmode)) continue; - if (mti->isDisabled && mti->isDisabled(md)) continue; + if (mti->isDisabled && mti->isDisabled(md, 0)) continue; if (mti->type!=eModifierTypeType_OnlyDeform) continue; if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts); diff --git a/source/blender/blenkernel/intern/modifier.c b/source/blender/blenkernel/intern/modifier.c index 08c979b89db..9390c7d67c4 100644 --- a/source/blender/blenkernel/intern/modifier.c +++ b/source/blender/blenkernel/intern/modifier.c @@ -187,7 +187,7 @@ static DerivedMesh *get_original_dm(Scene *scene, Object *ob, float (*vertexCos) /***/ -static int noneModifier_isDisabled(ModifierData *md) +static int noneModifier_isDisabled(ModifierData *md, int userRenderParams) { return 1; } @@ -222,7 +222,7 @@ static CustomDataMask curveModifier_requiredDataMask(Object *ob, ModifierData *m return dataMask; } -static int curveModifier_isDisabled(ModifierData *md) +static int curveModifier_isDisabled(ModifierData *md, int userRenderParams) { CurveModifierData *cmd = (CurveModifierData*) md; @@ -298,7 +298,7 @@ static CustomDataMask latticeModifier_requiredDataMask(Object *ob, ModifierData return dataMask; } -static int latticeModifier_isDisabled(ModifierData *md) +static int latticeModifier_isDisabled(ModifierData *md, int userRenderParams) { LatticeModifierData *lmd = (LatticeModifierData*) md; @@ -402,6 +402,13 @@ static void subsurfModifier_freeData(ModifierData *md) } } +static int subsurfModifier_isDisabled(ModifierData *md, int useRenderParams) +{ + SubsurfModifierData *smd = (SubsurfModifierData*) md; + + return (useRenderParams)? (smd->renderLevels == 0): (smd->levels == 0); +} + static DerivedMesh *subsurfModifier_applyModifier( ModifierData *md, Object *ob, DerivedMesh *derivedData, int useRenderParams, int isFinalCalc) @@ -410,8 +417,13 @@ static DerivedMesh *subsurfModifier_applyModifier( DerivedMesh *result; result = subsurf_make_derived_from_derived(derivedData, smd, - useRenderParams, NULL, - isFinalCalc, 0); + useRenderParams, NULL, isFinalCalc, 0); + + if(useRenderParams || !isFinalCalc) { + DerivedMesh *cddm= CDDM_copy(result); + result->release(result); + result= cddm; + } return result; } @@ -3524,7 +3536,7 @@ static void displaceModifier_foreachIDLink(ModifierData *md, Object *ob, displaceModifier_foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData); } -static int displaceModifier_isDisabled(ModifierData *md) +static int displaceModifier_isDisabled(ModifierData *md, int useRenderParams) { DisplaceModifierData *dmd = (DisplaceModifierData*) md; @@ -4268,7 +4280,7 @@ static void smoothModifier_copyData(ModifierData *md, ModifierData *target) strncpy(tsmd->defgrp_name, smd->defgrp_name, 32); } -static int smoothModifier_isDisabled(ModifierData *md) +static int smoothModifier_isDisabled(ModifierData *md, int useRenderParams) { SmoothModifierData *smd = (SmoothModifierData*) md; short flag; @@ -4498,7 +4510,7 @@ static void castModifier_copyData(ModifierData *md, ModifierData *target) strncpy(tcmd->defgrp_name, cmd->defgrp_name, 32); } -static int castModifier_isDisabled(ModifierData *md) +static int castModifier_isDisabled(ModifierData *md, int useRenderParams) { CastModifierData *cmd = (CastModifierData*) md; short flag; @@ -5487,7 +5499,7 @@ static CustomDataMask armatureModifier_requiredDataMask(Object *ob, ModifierData return dataMask; } -static int armatureModifier_isDisabled(ModifierData *md) +static int armatureModifier_isDisabled(ModifierData *md, int useRenderParams) { ArmatureModifierData *amd = (ArmatureModifierData*) md; @@ -5610,7 +5622,7 @@ static void hookModifier_freeData(ModifierData *md) if (hmd->indexar) MEM_freeN(hmd->indexar); } -static int hookModifier_isDisabled(ModifierData *md) +static int hookModifier_isDisabled(ModifierData *md, int useRenderParams) { HookModifierData *hmd = (HookModifierData*) md; @@ -5682,7 +5694,7 @@ static void hookModifier_deformVerts( /* if DerivedMesh is present and has original index data, * use it */ - if(dm && dm->getVertData(dm, 0, CD_ORIGINDEX)) { + if(dm && dm->getVertDataArray(dm, CD_ORIGINDEX)) { int j; int orig_index; for(j = 0; j < numVerts; ++j) { @@ -6308,7 +6320,7 @@ static void booleanModifier_copyData(ModifierData *md, ModifierData *target) tbmd->operation = bmd->operation; } -static int booleanModifier_isDisabled(ModifierData *md) +static int booleanModifier_isDisabled(ModifierData *md, int useRenderParams) { BooleanModifierData *bmd = (BooleanModifierData*) md; @@ -7757,7 +7769,7 @@ static CustomDataMask meshdeformModifier_requiredDataMask(Object *ob, ModifierDa return dataMask; } -static int meshdeformModifier_isDisabled(ModifierData *md) +static int meshdeformModifier_isDisabled(ModifierData *md, int useRenderParams) { MeshDeformModifierData *mmd = (MeshDeformModifierData*) md; @@ -8047,15 +8059,10 @@ static void multiresModifier_initData(ModifierData *md) { MultiresModifierData *mmd = (MultiresModifierData*)md; - mmd->lvl = mmd->totlvl = 1; -} - -static void multiresModifier_freeData(ModifierData *md) -{ - MultiresModifierData *mmd = (MultiresModifierData*)md; - - if(mmd->undo_verts) - MEM_freeN(mmd->undo_verts); + mmd->lvl = 0; + mmd->sculptlvl = 0; + mmd->renderlvl = 0; + mmd->totlvl = 0; } static void multiresModifier_copyData(ModifierData *md, ModifierData *target) @@ -8063,37 +8070,35 @@ static void multiresModifier_copyData(ModifierData *md, ModifierData *target) MultiresModifierData *mmd = (MultiresModifierData*) md; MultiresModifierData *tmmd = (MultiresModifierData*) target; - tmmd->totlvl = mmd->totlvl; tmmd->lvl = mmd->lvl; + tmmd->sculptlvl = mmd->sculptlvl; + tmmd->renderlvl = mmd->renderlvl; + tmmd->totlvl = mmd->totlvl; } static DerivedMesh *multiresModifier_applyModifier(ModifierData *md, Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc) { MultiresModifierData *mmd = (MultiresModifierData*)md; - DerivedMesh *final; + DerivedMesh *result; - /* TODO: for now just skip a level1 mesh */ - if(mmd->lvl == 1) - return dm; + result = multires_dm_create_from_derived(mmd, 0, dm, ob, useRenderParams, isFinalCalc); - final = multires_dm_create_from_derived(mmd, 0, dm, ob, useRenderParams, isFinalCalc); - if(mmd->undo_signal && mmd->undo_verts && mmd->undo_verts_tot == final->getNumVerts(final)) { - int i; - MVert *dst = CDDM_get_verts(final); - for(i = 0; i < mmd->undo_verts_tot; ++i) { - copy_v3_v3(dst[i].co, mmd->undo_verts[i].co); - } - CDDM_calc_normals(final); - - MultiresDM_mark_as_modified(final); + if(result == dm) + return dm; - MEM_freeN(mmd->undo_verts); - mmd->undo_signal = 0; - mmd->undo_verts = NULL; + if(useRenderParams || !isFinalCalc) { + DerivedMesh *cddm= CDDM_copy(result); + result->release(result); + result= cddm; + } + else if(ob->mode & OB_MODE_SCULPT) { + /* would be created on the fly too, just nicer this + way on first stroke after e.g. switching levels */ + result->getPBVH(ob, result); } - return final; + return result; } /* Shrinkwrap */ @@ -8142,7 +8147,7 @@ static CustomDataMask shrinkwrapModifier_requiredDataMask(Object *ob, ModifierDa return dataMask; } -static int shrinkwrapModifier_isDisabled(ModifierData *md) +static int shrinkwrapModifier_isDisabled(ModifierData *md, int useRenderParams) { ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md; return !smd->target; @@ -8438,6 +8443,7 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type) mti->initData = subsurfModifier_initData; mti->copyData = subsurfModifier_copyData; mti->freeData = subsurfModifier_freeData; + mti->isDisabled = subsurfModifier_isDisabled; mti->applyModifier = subsurfModifier_applyModifier; mti->applyModifierEM = subsurfModifier_applyModifierEM; @@ -8770,7 +8776,6 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type) mti->type = eModifierTypeType_Constructive; mti->flags = eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_RequiresOriginalData; mti->initData = multiresModifier_initData; - mti->freeData = multiresModifier_freeData; mti->copyData = multiresModifier_copyData; mti->applyModifier = multiresModifier_applyModifier; @@ -8920,7 +8925,7 @@ int modifier_couldBeCage(ModifierData *md) return ( (md->mode & eModifierMode_Realtime) && (md->mode & eModifierMode_Editmode) && - (!mti->isDisabled || !mti->isDisabled(md)) && + (!mti->isDisabled || !mti->isDisabled(md, 0)) && modifier_supportsMapping(md)); } @@ -8963,7 +8968,7 @@ int modifiers_getCageIndex(Object *ob, int *lastPossibleCageIndex_r, int virtual if (!(md->mode & eModifierMode_Realtime)) continue; if (!(md->mode & eModifierMode_Editmode)) continue; - if (mti->isDisabled && mti->isDisabled(md)) continue; + if (mti->isDisabled && mti->isDisabled(md, 0)) continue; if (!(mti->flags & eModifierTypeFlag_SupportsEditmode)) continue; if (md->mode & eModifierMode_DisableTemporary) continue; @@ -9005,7 +9010,7 @@ int modifier_isEnabled(ModifierData *md, int required_mode) ModifierTypeInfo *mti = modifierType_getInfo(md->type); if((md->mode & required_mode) != required_mode) return 0; - if(mti->isDisabled && mti->isDisabled(md)) return 0; + if(mti->isDisabled && mti->isDisabled(md, required_mode == eModifierMode_Render)) return 0; if(md->mode & eModifierMode_DisableTemporary) return 0; if(required_mode & eModifierMode_Editmode) if(!(mti->flags & eModifierTypeFlag_SupportsEditmode)) return 0; diff --git a/source/blender/blenkernel/intern/multires.c b/source/blender/blenkernel/intern/multires.c index 47b2914b0f5..e1f2b22c9f0 100644 --- a/source/blender/blenkernel/intern/multires.c +++ b/source/blender/blenkernel/intern/multires.c @@ -39,6 +39,7 @@ #include "BLI_math.h" #include "BLI_blenlib.h" +#include "BLI_pbvh.h" #include "BKE_cdderivedmesh.h" #include "BKE_customdata.h" @@ -50,14 +51,19 @@ #include "BKE_multires.h" #include "BKE_object.h" #include "BKE_subsurf.h" +#include "BKE_utildefines.h" + +#include "CCGSubSurf.h" #include <math.h> #include <string.h> /* MULTIRES MODIFIER */ static const int multires_max_levels = 13; -static const int multires_quad_tot[] = {4, 9, 25, 81, 289, 1089, 4225, 16641, 66049, 263169, 1050625, 4198401, 16785409}; -static const int multires_side_tot[] = {2, 3, 5, 9, 17, 33, 65, 129, 257, 513, 1025, 2049, 4097}; +static const int multires_grid_tot[] = {0, 4, 9, 25, 81, 289, 1089, 4225, 16641, 66049, 263169, 1050625, 4198401, 16785409}; +static const int multires_side_tot[] = {0, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513, 1025, 2049, 4097}; + +static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, int invert, int add, DMGridData **oldGridData, int totlvl); MultiresModifierData *find_multires_modifier(Object *ob) { @@ -72,23 +78,32 @@ MultiresModifierData *find_multires_modifier(Object *ob) } return mmd; +} +static int multires_get_level(Object *ob, MultiresModifierData *mmd, int render) +{ + if(render) + return mmd->renderlvl; + else if(ob->mode == OB_MODE_SCULPT) + return mmd->sculptlvl; + else + return mmd->lvl; } -int multiresModifier_switch_level(Object *ob, const int distance) +static void multires_set_tot_level(Object *ob, MultiresModifierData *mmd, int lvl) { - MultiresModifierData *mmd = find_multires_modifier(ob); - - if(mmd) { - mmd->lvl += distance; - if(mmd->lvl < 1) mmd->lvl = 1; - else if(mmd->lvl > mmd->totlvl) mmd->lvl = mmd->totlvl; - /* XXX: DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - object_handle_update(ob);*/ - return 1; + mmd->totlvl = lvl; + + if(ob->mode != OB_MODE_SCULPT) { + mmd->lvl = MAX2(mmd->lvl, lvl); + CLAMP(mmd->lvl, 0, mmd->totlvl); } - else - return 0; + + mmd->sculptlvl = MAX2(mmd->sculptlvl, lvl); + CLAMP(mmd->sculptlvl, 0, mmd->totlvl); + + mmd->renderlvl = MAX2(mmd->renderlvl, lvl); + CLAMP(mmd->renderlvl, 0, mmd->totlvl); } /* XXX */ @@ -156,6 +171,8 @@ void multiresModifier_join(Object *ob) /* Returns 0 on success, 1 if the src's totvert doesn't match */ int multiresModifier_reshape(MultiresModifierData *mmd, Object *dst, Object *src) { + /* XXX */ +#if 0 Mesh *src_me = get_mesh(src); DerivedMesh *mrdm = dst->derivedFinal; @@ -172,319 +189,147 @@ int multiresModifier_reshape(MultiresModifierData *mmd, Object *dst, Object *src return 0; } +#endif return 1; } -static void Mat3FromColVecs(float mat[][3], float v1[3], float v2[3], float v3[3]) +static void column_vectors_to_mat3(float mat[][3], float v1[3], float v2[3], float v3[3]) { copy_v3_v3(mat[0], v1); copy_v3_v3(mat[1], v2); copy_v3_v3(mat[2], v3); } -static DerivedMesh *multires_subdisp_pre(DerivedMesh *mrdm, int distance, int simple) +static void multires_copy_grid(float (*gridA)[3], float (*gridB)[3], int sizeA, int sizeB) { - DerivedMesh *final; - SubsurfModifierData smd; + int x, y, j, skip; - memset(&smd, 0, sizeof(SubsurfModifierData)); - smd.levels = distance; - if(simple) - smd.subdivType = ME_SIMPLE_SUBSURF; + if(sizeA > sizeB) { + skip = (sizeA-1)/(sizeB-1); - final = subsurf_make_derived_from_derived_with_multires(mrdm, &smd, NULL, 0, NULL, 0, 0); + for(j = 0, y = 0; y < sizeB; y++) + for(x = 0; x < sizeB; x++, j++) + copy_v3_v3(gridA[y*skip*sizeA + x*skip], gridB[j]); + } + else { + skip = (sizeB-1)/(sizeA-1); - return final; + for(j = 0, y = 0; y < sizeA; y++) + for(x = 0; x < sizeA; x++, j++) + copy_v3_v3(gridA[j], gridB[y*skip*sizeB + x*skip]); + } } -static void VecAddUf(float a[3], float b[3]) +static void multires_copy_dm_grid(DMGridData *gridA, DMGridData *gridB, int sizeA, int sizeB) { - a[0] += b[0]; - a[1] += b[1]; - a[2] += b[2]; -} + int x, y, j, skip; -static void multires_subdisp(DerivedMesh *orig, Object *ob, DerivedMesh *final, int lvl, int totlvl, - int totsubvert, int totsubedge, int totsubface, int addverts) -{ - DerivedMesh *mrdm; - Mesh *me = ob->data; - MultiresModifierData mmd_sub; - MVert *mvs = CDDM_get_verts(final); - MVert *mvd, *mvd_f1, *mvs_f1, *mvd_f3, *mvd_f4; - MVert *mvd_f2, *mvs_f2, *mvs_e1, *mvd_e1, *mvs_e2; - int totvert; - int slo1 = multires_side_tot[lvl - 1]; - int sll = slo1 / 2; - int slo2 = multires_side_tot[totlvl - 2]; - int shi2 = multires_side_tot[totlvl - 1]; - int skip = multires_side_tot[totlvl - lvl] - 1; - int i, j, k; - - memset(&mmd_sub, 0, sizeof(MultiresModifierData)); - mmd_sub.lvl = mmd_sub.totlvl = totlvl; - mrdm = multires_dm_create_from_derived(&mmd_sub, 1, orig, ob, 0, 0); - - mvd = CDDM_get_verts(mrdm); - /* Need to map from ccg to mrdm */ - totvert = mrdm->getNumVerts(mrdm); - - if(!addverts) { - for(i = 0; i < totvert; ++i) { - float z[3] = {0,0,0}; - copy_v3_v3(mvd[i].co, z); - } + if(sizeA > sizeB) { + skip = (sizeA-1)/(sizeB-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); } + else { + skip = (sizeB-1)/(sizeA-1); - /* Load base verts */ - for(i = 0; i < me->totvert; ++i) - VecAddUf(mvd[totvert - me->totvert + i].co, mvs[totvert - me->totvert + i].co); + 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); + } +} - mvd_f1 = mvd; - mvs_f1 = mvs; - mvd_f2 = mvd; - mvs_f2 = mvs + totvert - totsubvert; - mvs_e1 = mvs + totsubface * (skip-1) * (skip-1); +/* direction=1 for delete higher, direction=0 for lower (not implemented yet) */ +void multiresModifier_del_levels(struct MultiresModifierData *mmd, struct Object *ob, int direction) +{ + Mesh *me = get_mesh(ob); + int lvl = multires_get_level(ob, mmd, 0); + int levels = mmd->totlvl - lvl; + MDisps *mdisps; - for(i = 0; i < me->totface; ++i) { - const int end = me->mface[i].v4 ? 4 : 3; - int x, y, x2, y2, mov= 0; - - mvd_f1 += 1 + end * (slo2-2); //center+edgecross - mvd_f3 = mvd_f4 = mvd_f1; - - for(j = 0; j < end; ++j) { - mvd_f1 += (skip/2 - 1) * (slo2 - 2) + (skip/2 - 1); - /* Update sub faces */ - for(y = 0; y < sll; ++y) { - for(x = 0; x < sll; ++x) { - /* Face center */ - VecAddUf(mvd_f1->co, mvs_f1->co); - mvs_f1 += 1; - - /* Now we hold the center of the subface at mvd_f1 - and offset it to the edge cross and face verts */ - - /* Edge cross */ - for(k = 0; k < 4; ++k) { - if(k == 0) mov = -1; - else if(k == 1) mov = slo2 - 2; - else if(k == 2) mov = 1; - else if(k == 3) mov = -(slo2 - 2); - - for(x2 = 1; x2 < skip/2; ++x2) { - VecAddUf((mvd_f1 + mov * x2)->co, mvs_f1->co); - ++mvs_f1; - } - } + CustomData_external_read(&me->fdata, &me->id, CD_MASK_MDISPS, me->totface); + mdisps= CustomData_get_layer(&me->fdata, CD_MDISPS); - /* Main face verts */ - for(k = 0; k < 4; ++k) { - int movx= 0, movy= 0; - - if(k == 0) { movx = -1; movy = -(slo2 - 2); } - else if(k == 1) { movx = slo2 - 2; movy = -1; } - else if(k == 2) { movx = 1; movy = slo2 - 2; } - else if(k == 3) { movx = -(slo2 - 2); movy = 1; } - - for(y2 = 1; y2 < skip/2; ++y2) { - for(x2 = 1; x2 < skip/2; ++x2) { - VecAddUf((mvd_f1 + movy * y2 + movx * x2)->co, mvs_f1->co); - ++mvs_f1; - } - } - } - - mvd_f1 += skip; - } - mvd_f1 += (skip - 1) * (slo2 - 2) - 1; - } - mvd_f1 -= (skip - 1) * (slo2 - 2) - 1 + skip; - mvd_f1 += (slo2 - 2) * (skip/2-1) + skip/2-1 + 1; - } + multires_force_update(ob); - /* update face center verts */ - VecAddUf(mvd_f2->co, mvs_f2->co); + if(mdisps && levels > 0 && direction == 1) { + if(lvl > 0) { + int nsize = multires_side_tot[lvl]; + int hsize = multires_side_tot[mmd->totlvl]; + int i; - mvd_f2 += 1; - mvs_f2 += 1; + for(i = 0; i < me->totface; ++i) { + MDisps *mdisp= &mdisps[i]; + float (*disps)[3], (*ndisps)[3], (*hdisps)[3]; + int nvert = (me->mface[i].v4)? 4: 3; + int totdisp = multires_grid_tot[lvl]*nvert; + int S; - /* update face edge verts */ - for(j = 0; j < end; ++j) { - MVert *restore; + disps = MEM_callocN(sizeof(float) * 3 * totdisp, "multires disps"); - /* Super-face edge cross */ - for(k = 0; k < skip-1; ++k) { - VecAddUf(mvd_f2->co, mvs_e1->co); - mvd_f2++; - mvs_e1++; - } - for(x = 1; x < sll; ++x) { - VecAddUf(mvd_f2->co, mvs_f2->co); - mvd_f2++; - mvs_f2++; - - for(k = 0; k < skip-1; ++k) { - VecAddUf(mvd_f2->co, mvs_e1->co); - mvd_f2++; - mvs_e1++; - } - } + ndisps = disps; + hdisps = mdisp->disps; - restore = mvs_e1; - for(y = 0; y < sll - 1; ++y) { - for(x = 0; x < sll; ++x) { - for(k = 0; k < skip - 1; ++k) { - VecAddUf(mvd_f3[(skip-1)+(y*skip) + (x*skip+k)*(slo2-2)].co, - mvs_e1->co); - ++mvs_e1; - } - mvs_e1 += skip-1; - } - } - - mvs_e1 = restore + skip - 1; - for(y = 0; y < sll - 1; ++y) { - for(x = 0; x < sll; ++x) { - for(k = 0; k < skip - 1; ++k) { - VecAddUf(mvd_f3[(slo2-2)*(skip-1)+(x*skip)+k + y*skip*(slo2-2)].co, - mvs_e1->co); - ++mvs_e1; - } - mvs_e1 += skip - 1; - } - } + for(S = 0; S < nvert; S++) { + multires_copy_grid(ndisps, hdisps, nsize, hsize); - mvd_f3 += (slo2-2)*(slo2-2); - mvs_e1 -= skip - 1; - } - - /* update base (2) face verts */ - for(j = 0; j < end; ++j) { - mvd_f2 += (slo2 - 1) * (skip - 1); - for(y = 0; y < sll - 1; ++y) { - for(x = 0; x < sll - 1; ++x) { - VecAddUf(mvd_f2->co, mvs_f2->co); - mvd_f2 += skip; - ++mvs_f2; + ndisps += nsize*nsize; + hdisps += hsize*hsize; } - mvd_f2 += (slo2 - 1) * (skip - 1); - } - mvd_f2 -= (skip - 1); - } - } - /* edges */ - mvd_e1 = mvd + totvert - me->totvert - me->totedge * (shi2-2); - mvs_e2 = mvs + totvert - me->totvert - me->totedge * (slo1-2); - for(i = 0; i < me->totedge; ++i) { - for(j = 0; j < skip - 1; ++j) { - VecAddUf(mvd_e1->co, mvs_e1->co); - mvd_e1++; - mvs_e1++; - } - for(j = 0; j < slo1 - 2; j++) { - VecAddUf(mvd_e1->co, mvs_e2->co); - mvd_e1++; - mvs_e2++; - - for(k = 0; k < skip - 1; ++k) { - VecAddUf(mvd_e1->co, mvs_e1->co); - mvd_e1++; - mvs_e1++; + MEM_freeN(mdisp->disps); + mdisp->disps = disps; + mdisp->totdisp = totdisp; } } + else { + CustomData_external_remove(&me->fdata, &me->id, CD_MDISPS, me->totface); + CustomData_free_layer_active(&me->fdata, CD_MDISPS, me->totface); + } } - final->needsFree = 1; - final->release(final); - mrdm->needsFree = 1; - MultiresDM_mark_as_modified(mrdm); - mrdm->release(mrdm); + multires_set_tot_level(ob, mmd, lvl); } -/* direction=1 for delete higher, direction=0 for lower (not implemented yet) */ -void multiresModifier_del_levels(struct MultiresModifierData *mmd, struct Object *ob, int direction) +static DerivedMesh *multires_dm_create_local(Object *ob, DerivedMesh *dm, int lvl, int totlvl, int simple) { - Mesh *me = get_mesh(ob); - int distance = mmd->totlvl - mmd->lvl; - MDisps *mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS); - - multires_force_update(ob); + MultiresModifierData mmd; - if(mdisps && distance > 0 && direction == 1) { - int skip = multires_side_tot[distance] - 1; - int st = multires_side_tot[mmd->totlvl - 1]; - int totdisp = multires_quad_tot[mmd->lvl - 1]; - int i, j, x, y; - - for(i = 0; i < me->totface; ++i) { - float (*disps)[3] = MEM_callocN(sizeof(float) * 3 * totdisp, "multires del disps"); - - for(j = 0, y = 0; y < st; y += skip) { - for(x = 0; x < st; x += skip) { - copy_v3_v3(disps[j], mdisps[i].disps[y * st + x]); - ++j; - } - } + memset(&mmd, 0, sizeof(MultiresModifierData)); + mmd.lvl = lvl; + mmd.sculptlvl = lvl; + mmd.renderlvl = lvl; + mmd.totlvl = totlvl; + mmd.simple = simple; - MEM_freeN(mdisps[i].disps); - mdisps[i].disps = disps; - mdisps[i].totdisp = totdisp; - } - } - - mmd->totlvl = mmd->lvl; + return multires_dm_create_from_derived(&mmd, 1, dm, ob, 0, 0); } -void multiresModifier_subdivide(MultiresModifierData *mmd, Object *ob, int distance, int updateblock, int simple) +static DerivedMesh *subsurf_dm_create_local(Object *ob, DerivedMesh *dm, int lvl, int simple, int optimal) { - DerivedMesh *final = NULL; - int totsubvert = 0, totsubface = 0, totsubedge = 0; - Mesh *me = get_mesh(ob); - MDisps *mdisps; - int i; - - if(distance == 0) - return; - - if(mmd->totlvl > multires_max_levels) - mmd->totlvl = multires_max_levels; - if(mmd->lvl > multires_max_levels) - mmd->lvl = multires_max_levels; - - multires_force_update(ob); + SubsurfModifierData smd; - mmd->lvl = mmd->totlvl; - mmd->totlvl += distance; + memset(&smd, 0, sizeof(SubsurfModifierData)); + smd.levels = smd.renderLevels = lvl; + smd.flags |= eSubsurfModifierFlag_SubsurfUv; + if(simple) + smd.subdivType = ME_SIMPLE_SUBSURF; + if(optimal) + smd.flags |= eSubsurfModifierFlag_ControlEdges; - mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS); - if(!mdisps) - mdisps = CustomData_add_layer(&me->fdata, CD_MDISPS, CD_DEFAULT, NULL, me->totface); + return subsurf_make_derived_from_derived(dm, &smd, 0, NULL, 0, 0); +} - if(mdisps->disps && !updateblock && mmd->totlvl > 2) { - DerivedMesh *orig, *mrdm; - MultiresModifierData mmd_sub; - - orig = CDDM_from_mesh(me, NULL); - memset(&mmd_sub, 0, sizeof(MultiresModifierData)); - mmd_sub.lvl = mmd_sub.totlvl = mmd->lvl; - mmd_sub.simple = simple; - mrdm = multires_dm_create_from_derived(&mmd_sub, 1, orig, ob, 0, 0); - totsubvert = mrdm->getNumVerts(mrdm); - totsubedge = mrdm->getNumEdges(mrdm); - totsubface = mrdm->getNumFaces(mrdm); - orig->needsFree = 1; - orig->release(orig); - - final = multires_subdisp_pre(mrdm, distance, simple); - mrdm->needsFree = 1; - mrdm->release(mrdm); - } +static void multires_reallocate_mdisps(Mesh *me, MDisps *mdisps, int lvl) +{ + int i; + /* reallocate displacements to be filled in */ for(i = 0; i < me->totface; ++i) { - const int totdisp = multires_quad_tot[mmd->totlvl - 1]; + int nvert = (me->mface[i].v4)? 4: 3; + int totdisp = multires_grid_tot[lvl]*nvert; float (*disps)[3] = MEM_callocN(sizeof(float) * 3 * totdisp, "multires disps"); if(mdisps[i].disps) @@ -493,772 +338,468 @@ void multiresModifier_subdivide(MultiresModifierData *mmd, Object *ob, int dista mdisps[i].disps = disps; mdisps[i].totdisp = totdisp; } +} +void multiresModifier_subdivide(MultiresModifierData *mmd, Object *ob, int updateblock, int simple) +{ + Mesh *me = ob->data; + MDisps *mdisps; + int lvl= mmd->totlvl; + int totlvl= mmd->totlvl+1; - if(final) { - DerivedMesh *orig; + if(totlvl > multires_max_levels) + return; - orig = CDDM_from_mesh(me, NULL); + multires_force_update(ob); - multires_subdisp(orig, ob, final, mmd->lvl, mmd->totlvl, totsubvert, totsubedge, totsubface, 0); + mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS); + if(!mdisps) + mdisps = CustomData_add_layer(&me->fdata, CD_MDISPS, CD_DEFAULT, NULL, me->totface); - orig->needsFree = 1; - orig->release(orig); - } + if(mdisps->disps && !updateblock && totlvl > 1) { + /* upsample */ + DerivedMesh *lowdm, *cddm, *highdm; + DMGridData **highGridData, **lowGridData, **subGridData; + CCGSubSurf *ss; + int i, numGrids, highGridSize, lowGridSize; + + /* create subsurf DM from original mesh at high level */ + cddm = CDDM_from_mesh(me, NULL); + highdm = subsurf_dm_create_local(ob, cddm, totlvl, simple, 0); + + /* create multires DM from original mesh at low level */ + lowdm = multires_dm_create_local(ob, cddm, lvl, lvl, simple); + cddm->release(cddm); + + /* copy subsurf grids and replace them with low displaced grids */ + numGrids = highdm->getNumGrids(highdm); + highGridSize = highdm->getGridSize(highdm); + highGridData = highdm->getGridData(highdm); + lowGridSize = lowdm->getGridSize(lowdm); + lowGridData = lowdm->getGridData(lowdm); + + 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); + + /* overwrite with current displaced grids */ + multires_copy_dm_grid(highGridData[i], lowGridData[i], highGridSize, lowGridSize); + } - mmd->lvl = mmd->totlvl; -} + /* low lower level dm no longer needed at this point */ + lowdm->release(lowdm); -typedef struct DisplacerEdges { - /* DerivedMesh index at the start of each edge (using face x/y directions to define the start) */ - int base[4]; - /* 1 if edge moves in the positive x or y direction, -1 otherwise */ - int dir[4]; -} DisplacerEdges; + /* subsurf higher levels again with displaced data */ + ss= ((CCGDerivedMesh*)highdm)->ss; + ccgSubSurf_updateFromFaces(ss, lvl, NULL, 0); + ccgSubSurf_updateLevels(ss, lvl, NULL, 0); -typedef struct DisplacerSpill { - /* Index of face (in base mesh), -1 for none */ - int face; + /* reallocate displacements */ + multires_reallocate_mdisps(me, mdisps, totlvl); - /* Spill flag */ - /* 1 = Negative variable axis */ - /* 2 = Near fixed axis */ - /* 4 = Flip axes */ - int f; + /* compute displacements */ + multiresModifier_disp_run(highdm, me, 1, 0, subGridData, totlvl); - /* Neighboring edges */ - DisplacerEdges edges; -} DisplacerSpill; + /* free */ + highdm->release(highdm); + for(i = 0; i < numGrids; ++i) + MEM_freeN(subGridData[i]); + MEM_freeN(subGridData); + } + else { + /* only reallocate, nothing to upsample */ + multires_reallocate_mdisps(me, mdisps, totlvl); + } -typedef struct MultiresDisplacer { - Mesh *me; - MDisps *grid; - MFace *face; - - int dm_first_base_vert_index; + multires_set_tot_level(ob, mmd, totlvl); +} - int spacing; - int sidetot, interior_st, disp_st; - int sidendx; - int type; - int invert; - MVert *subco; - int subco_index, face_index; - float weight; +static void grid_tangent(int gridSize, int index, int x, int y, int axis, DMGridData **gridData, 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); + else + sub_v3_v3v3(t, gridData[index][x + gridSize*y].co, gridData[index][x - 1 + gridSize*y].co); + } + else + sub_v3_v3v3(t, gridData[index][x + 1 + gridSize*y].co, gridData[index][x + gridSize*y].co); + } + 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); + else + sub_v3_v3v3(t, gridData[index][x + gridSize*y].co, gridData[index][x + gridSize*(y - 1)].co); + } + else + sub_v3_v3v3(t, gridData[index][x + gridSize*(y + 1)].co, gridData[index][x + gridSize*y].co); + } +} - /* Valence for each corner */ - int valence[4]; +static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, int invert, int add, DMGridData **oldGridData, int totlvl) +{ + CCGDerivedMesh *ccgdm = (CCGDerivedMesh*)dm; + DMGridData **gridData, **subGridData; + MFace *mface = me->mface; + MDisps *mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS); + int *gridOffset; + int i, numGrids, gridSize, dGridSize, dSkip; + + numGrids = dm->getNumGrids(dm); + gridSize = dm->getGridSize(dm); + gridData = dm->getGridData(dm); + gridOffset = dm->getGridOffset(dm); + subGridData = (oldGridData)? oldGridData: gridData; - /* Neighboring edges for current face */ - DisplacerEdges edges_primary; + dGridSize = multires_side_tot[totlvl]; + dSkip = (dGridSize-1)/(gridSize-1); + + #pragma omp parallel for private(i) schedule(static) + for(i = 0; i < me->totface; ++i) { + const int numVerts = mface[i].v4 ? 4 : 3; + MDisps *mdisp = &mdisps[i]; + int S, x, y, gIndex = gridOffset[i]; + + /* when adding new faces in edit mode, need to allocate disps */ + if(!mdisp->disps) + #pragma omp critical + { + multires_reallocate_mdisps(me, mdisps, totlvl); + } - /* Neighboring faces */ - DisplacerSpill spill_x, spill_y; + for(S = 0; S < numVerts; ++S, ++gIndex) { + DMGridData *grid = gridData[gIndex]; + DMGridData *subgrid = subGridData[gIndex]; + float (*dispgrid)[3] = &mdisp->disps[S*dGridSize*dGridSize]; - int *face_offsets; + 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 *data = dispgrid[dGridSize*y*dSkip + x*dSkip]; + float mat[3][3], tx[3], ty[3], disp[3], d[3]; - int x, y, ax, ay; -} MultiresDisplacer; + /* construct tangent space matrix */ + grid_tangent(gridSize, gIndex, x, y, 0, subGridData, tx); + normalize_v3(tx); -static int mface_v(MFace *f, int v) -{ - return v == 0 ? f->v1 : v == 1 ? f->v2 : v == 2 ? f->v3 : v == 3 ? f->v4 : -1; -} + grid_tangent(gridSize, gIndex, x, y, 1, subGridData, ty); + normalize_v3(ty); -/* Get the edges (and their directions) */ -static void find_displacer_edges(MultiresDisplacer *d, DerivedMesh *dm, DisplacerEdges *de, MFace *f) -{ - ListBase *emap = MultiresDM_get_vert_edge_map(dm); - IndexNode *n; - int i, end = f->v4 ? 4 : 3; - int offset = dm->getNumVerts(dm) - d->me->totvert - d->me->totedge * d->interior_st; + //mul_v3_fl(tx, 1.0f/(gridSize-1)); + //mul_v3_fl(ty, 1.0f/(gridSize-1)); + //cross_v3_v3v3(no, tx, ty); - for(i = 0; i < end; ++i) { - int vcur = mface_v(f, i); - int vnext = mface_v(f, i == end - 1 ? 0 : i + 1); + column_vectors_to_mat3(mat, tx, ty, no); - de->dir[i] = 1; - - for(n = emap[vcur].first; n; n = n->next) { - MEdge *e = &d->me->medge[n->index]; - - if(e->v1 == vnext || e->v2 == vnext) { - de->base[i] = n->index * d->interior_st; - if(((i == 0 || i == 1) && e->v1 == vnext) || - ((i == 2 || i == 3) && e->v2 == vnext)) { - de->dir[i] = -1; - de->base[i] += d->interior_st - 1; + if(!invert) { + /* convert to object space and add */ + mul_v3_m3v3(disp, mat, data); + add_v3_v3v3(co, sco, disp); + } + else if(!add) { + /* convert difference to tangent space */ + sub_v3_v3v3(disp, co, sco); + invert_m3(mat); + mul_v3_m3v3(data, mat, disp); + } + else { + /* convert difference to tangent space */ + invert_m3(mat); + mul_v3_m3v3(d, mat, co); + add_v3_v3(data, d); + } } - de->base[i] += offset; - break; } } } -} -/* Returns in out the corners [0-3] that use v1 and v2 */ -static void find_face_corners(MFace *f, int v1, int v2, int out[2]) -{ - int i, end = f->v4 ? 4 : 3; - - for(i = 0; i < end; ++i) { - int corner = mface_v(f, i); - if(corner == v1) - out[0] = i; - if(corner == v2) - out[1] = i; + if(!invert) { + ccgSubSurf_stitchFaces(ccgdm->ss, 0, NULL, 0); + ccgSubSurf_updateNormals(ccgdm->ss, NULL, 0); } } -static void multires_displacer_get_spill_faces(MultiresDisplacer *d, DerivedMesh *dm, MFace *mface) +static void multiresModifier_update(DerivedMesh *dm) { - ListBase *map = MultiresDM_get_vert_face_map(dm); - IndexNode *n1, *n2; - int v4 = d->face->v4 ? d->face->v4 : d->face->v1; - int crn[2], lv; - - memset(&d->spill_x, 0, sizeof(DisplacerSpill)); - memset(&d->spill_y, 0, sizeof(DisplacerSpill)); - d->spill_x.face = d->spill_y.face = -1; - - for(n1 = map[d->face->v3].first; n1; n1 = n1->next) { - if(n1->index == d->face_index) - continue; + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + Object *ob; + Mesh *me; + MDisps *mdisps; + MultiresModifierData *mmd; - for(n2 = map[d->face->v2].first; n2; n2 = n2->next) { - if(n1->index == n2->index) - d->spill_x.face = n1->index; - } - for(n2 = map[v4].first; n2; n2 = n2->next) { - if(n1->index == n2->index) - d->spill_y.face = n1->index; - } - } + ob = ccgdm->multires.ob; + me = ccgdm->multires.ob->data; + mmd = ccgdm->multires.mmd; + CustomData_external_read(&me->fdata, &me->id, CD_MASK_MDISPS, me->totface); + mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS); - if(d->spill_x.face != -1) { - /* Neighbor of v2/v3 found, find flip and orientation */ - find_face_corners(&mface[d->spill_x.face], d->face->v2, d->face->v3, crn); - lv = mface[d->spill_x.face].v4 ? 3 : 2; - - if(crn[0] == 0 && crn[1] == lv) - d->spill_x.f = 0+2+0; - else if(crn[0] == lv && crn[1] == 0) - d->spill_x.f = 1+2+0; - else if(crn[0] == 1 && crn[1] == 0) - d->spill_x.f = 1+2+4; - else if(crn[0] == 0 && crn[1] == 1) - d->spill_x.f = 0+2+4; - else if(crn[0] == 2 && crn[1] == 1) - d->spill_x.f = 1+0+0; - else if(crn[0] == 1 && crn[1] == 2) - d->spill_x.f = 0+0+0; - else if(crn[0] == 3 && crn[1] == 2) - d->spill_x.f = 0+0+4; - else if(crn[0] == 2 && crn[1] == 3) - d->spill_x.f = 1+0+4; - - find_displacer_edges(d, dm, &d->spill_x.edges, &mface[d->spill_x.face]); - } + if(mdisps) { + int lvl = ccgdm->multires.lvl; + int totlvl = ccgdm->multires.totlvl; + + if(lvl < totlvl) { + Mesh *me = ob->data; + DerivedMesh *lowdm, *cddm, *highdm; + DMGridData **highGridData, **lowGridData, **subGridData, **gridData, *diffGrid; + CCGSubSurf *ss; + int i, j, numGrids, highGridSize, lowGridSize; + + /* create subsurf DM from original mesh at high level */ + cddm = CDDM_from_mesh(me, NULL); + highdm = subsurf_dm_create_local(ob, cddm, totlvl, mmd->simple, 0); + + /* create multires DM from original mesh and displacements */ + lowdm = multires_dm_create_local(ob, cddm, lvl, totlvl, mmd->simple); + cddm->release(cddm); + + /* gather grid data */ + numGrids = highdm->getNumGrids(highdm); + highGridSize = highdm->getGridSize(highdm); + highGridData = highdm->getGridData(highdm); + lowGridSize = lowdm->getGridSize(lowdm); + lowGridData = lowdm->getGridData(lowdm); + gridData = dm->getGridData(dm); + + subGridData = MEM_callocN(sizeof(DMGridData*)*numGrids, "subGridData*"); + diffGrid = MEM_callocN(sizeof(DMGridData)*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); + + /* 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); + + multires_copy_dm_grid(highGridData[i], diffGrid, highGridSize, lowGridSize); + } - if(d->spill_y.face != -1) { - /* Neighbor of v3/v4 found, find flip and orientation */ - find_face_corners(&mface[d->spill_y.face], d->face->v3, v4, crn); - lv = mface[d->spill_y.face].v4 ? 3 : 2; - - if(crn[0] == 1 && crn[1] == 0) - d->spill_y.f = 1+2+0; - else if(crn[0] == 0 && crn[1] == 1) - d->spill_y.f = 0+2+0; - else if(crn[0] == 2 && crn[1] == 1) - d->spill_y.f = 1+0+4; - else if(crn[0] == 1 && crn[1] == 2) - d->spill_y.f = 0+0+4; - else if(crn[0] == 3 && crn[1] == 2) - d->spill_y.f = 0+0+0; - else if(crn[0] == 2 && crn[1] == 3) - d->spill_y.f = 1+0+0; - else if(crn[0] == 0 && crn[1] == lv) - d->spill_y.f = 0+2+4; - else if(crn[0] == lv && crn[1] == 0) - d->spill_y.f = 1+2+4; - - find_displacer_edges(d, dm, &d->spill_y.edges, &mface[d->spill_y.face]); - } -} + /* lower level dm no longer needed at this point */ + MEM_freeN(diffGrid); + lowdm->release(lowdm); -static void find_corner_valences(MultiresDisplacer *d, DerivedMesh *dm) -{ - int i; + /* subsurf higher levels again with difference of coordinates */ + ss= ((CCGDerivedMesh*)highdm)->ss; + ccgSubSurf_updateFromFaces(ss, lvl, NULL, 0); + ccgSubSurf_updateLevels(ss, lvl, NULL, 0); - d->valence[3] = -1; + /* add to displacements */ + multiresModifier_disp_run(highdm, me, 1, 1, subGridData, mmd->totlvl); - /* Set the vertex valence for the corners */ - for(i = 0; i < (d->face->v4 ? 4 : 3); ++i) - d->valence[i] = BLI_countlist(&MultiresDM_get_vert_edge_map(dm)[mface_v(d->face, i)]); -} + /* free */ + highdm->release(highdm); + for(i = 0; i < numGrids; ++i) + MEM_freeN(subGridData[i]); + MEM_freeN(subGridData); + } + else { + DerivedMesh *cddm, *subdm; -static void multires_displacer_init(MultiresDisplacer *d, DerivedMesh *dm, - const int face_index, const int invert) -{ - Mesh *me = MultiresDM_get_mesh(dm); - - d->me = me; - d->face = me->mface + face_index; - d->face_index = face_index; - d->face_offsets = MultiresDM_get_face_offsets(dm); - /* Get the multires grid from customdata */ - d->grid = CustomData_get_layer(&me->fdata, CD_MDISPS); - if(d->grid) - d->grid += face_index; - - d->spacing = pow(2, MultiresDM_get_totlvl(dm) - MultiresDM_get_lvl(dm)); - d->sidetot = multires_side_tot[MultiresDM_get_lvl(dm) - 1]; - d->interior_st = d->sidetot - 2; - d->disp_st = multires_side_tot[MultiresDM_get_totlvl(dm) - 1]; - d->invert = invert; - - multires_displacer_get_spill_faces(d, dm, me->mface); - find_displacer_edges(d, dm, &d->edges_primary, d->face); - find_corner_valences(d, dm); - - d->dm_first_base_vert_index = dm->getNumVerts(dm) - me->totvert; -} + cddm = CDDM_from_mesh(me, NULL); + subdm = subsurf_dm_create_local(ob, cddm, mmd->totlvl, mmd->simple, 0); + cddm->release(cddm); -static void multires_displacer_weight(MultiresDisplacer *d, const float w) -{ - d->weight = w; -} + multiresModifier_disp_run(dm, me, 1, 0, subdm->getGridData(subdm), mmd->totlvl); -static void multires_displacer_anchor(MultiresDisplacer *d, const int type, const int side_index) -{ - d->sidendx = side_index; - d->x = d->y = d->sidetot / 2; - d->type = type; - - if(type == 2) { - if(side_index == 0) - d->y -= 1; - else if(side_index == 1) - d->x += 1; - else if(side_index == 2) - d->y += 1; - else if(side_index == 3) - d->x -= 1; - } - else if(type == 3) { - if(side_index == 0) { - d->x -= 1; - d->y -= 1; - } - else if(side_index == 1) { - d->x += 1; - d->y -= 1; - } - else if(side_index == 2) { - d->x += 1; - d->y += 1; - } - else if(side_index == 3) { - d->x -= 1; - d->y += 1; + subdm->release(subdm); } } - - d->ax = d->x; - d->ay = d->y; } -static void multires_displacer_anchor_edge(MultiresDisplacer *d, int v1, int v2, int x) +static void multires_dm_mark_as_modified(struct DerivedMesh *dm) { - d->type = 4; - - if(v1 == d->face->v1) { - d->x = 0; - d->y = 0; - if(v2 == d->face->v2) - d->x += x; - else if(v2 == d->face->v3) { - if(x < d->sidetot / 2) - d->y = x; - else { - d->x = x; - d->y = d->sidetot - 1; - } - } - else - d->y += x; - } - else if(v1 == d->face->v2) { - d->x = d->sidetot - 1; - d->y = 0; - if(v2 == d->face->v1) - d->x -= x; - else - d->y += x; - } - else if(v1 == d->face->v3) { - d->x = d->sidetot - 1; - d->y = d->sidetot - 1; - if(v2 == d->face->v2) - d->y -= x; - else if(v2 == d->face->v1) { - if(x < d->sidetot / 2) - d->x -= x; - else { - d->x = 0; - d->y -= x; - } - } - else - d->x -= x; - } - else if(v1 == d->face->v4) { - d->x = 0; - d->y = d->sidetot - 1; - if(v2 == d->face->v3) - d->x += x; - else - d->y -= x; - } + CCGDerivedMesh *ccgdm = (CCGDerivedMesh*)dm; + ccgdm->multires.modified = 1; } -static void multires_displacer_anchor_vert(MultiresDisplacer *d, const int v) +void multires_mark_as_modified(struct Object *ob) { - const int e = d->sidetot - 1; - - d->type = 5; - - d->x = d->y = 0; - if(v == d->face->v2) - d->x = e; - else if(v == d->face->v3) - d->x = d->y = e; - else if(v == d->face->v4) - d->y = e; + if(ob && ob->derivedFinal) + multires_dm_mark_as_modified(ob->derivedFinal); } -static void multires_displacer_jump(MultiresDisplacer *d) +void multires_force_update(Object *ob) { - if(d->sidendx == 0) { - d->x -= 1; - d->y = d->ay; - } - else if(d->sidendx == 1) { - d->x = d->ax; - d->y -= 1; - } - else if(d->sidendx == 2) { - d->x += 1; - d->y = d->ay; - } - else if(d->sidendx == 3) { - d->x = d->ax; - d->y += 1; + if(ob && ob->derivedFinal) { + ob->derivedFinal->needsFree =1; + ob->derivedFinal->release(ob->derivedFinal); + ob->derivedFinal = NULL; } } -/* Treating v1 as (0,0) and v3 as (st-1,st-1), - returns the index of the vertex at (x,y). - If x or y is >= st, wraps over to the adjacent face, - or if there is no adjacent face, returns -2. */ -static int multires_index_at_loc(int face_index, int x, int y, MultiresDisplacer *d, DisplacerEdges *de) +void multires_stitch_grids(Object *ob) { - int coord_edge = d->sidetot - 1; /* Max value of x/y at edge of grid */ - int mid = d->sidetot / 2; - int lim = mid - 1; - int qtot = lim * lim; - int base = d->face_offsets[face_index]; - - /* Edge spillover */ - if(x == d->sidetot || y == d->sidetot) { - int flags, v_axis, f_axis, lx, ly; - - if(x == d->sidetot && d->spill_x.face != -1) { - flags = d->spill_x.f; - - /* Handle triangle seam between v1 and v3 */ - if(!d->me->mface[d->spill_x.face].v4 && - ((flags == 2 && y >= mid) || (flags == 3 && y < mid))) - flags += 2; - - v_axis = (flags & 1) ? d->sidetot - 1 - y : y; - f_axis = (flags & 2) ? 1 : d->sidetot - 2; - lx = f_axis, ly = v_axis; - - if(flags & 4) { - lx = v_axis; - ly = f_axis; - } - - return multires_index_at_loc(d->spill_x.face, lx, ly, d, &d->spill_x.edges); - } - else if(y == d->sidetot && d->spill_y.face != -1) { - flags = d->spill_y.f; - - /* Handle triangle seam between v1 and v3 */ - if(!d->me->mface[d->spill_y.face].v4 && - ((flags == 6 && x >= mid) || (flags == 7 && x < mid))) - flags = ~flags; + /* utility for smooth brush */ + if(ob && ob->derivedFinal) { + CCGDerivedMesh *ccgdm = (CCGDerivedMesh*)ob->derivedFinal; + CCGFace **faces; + int totface; - v_axis = (flags & 1) ? x : d->sidetot - 1 - x; - f_axis = (flags & 2) ? 1 : d->sidetot - 2; - lx = v_axis, ly = f_axis; + if(ccgdm->pbvh) { + BLI_pbvh_get_grid_updates(ccgdm->pbvh, 0, (void***)&faces, &totface); - if(flags & 4) { - lx = f_axis; - ly = v_axis; + if(totface) { + ccgSubSurf_stitchFaces(ccgdm->ss, 0, faces, totface); + MEM_freeN(faces); } - - return multires_index_at_loc(d->spill_y.face, lx, ly, d, &d->spill_y.edges); } - else - return -2; } - /* Corners */ - else if(x == 0 && y == 0) - return d->dm_first_base_vert_index + d->face->v1; - else if(x == coord_edge && y == 0) - return d->dm_first_base_vert_index + d->face->v2; - else if(x == coord_edge && y == coord_edge) - return d->dm_first_base_vert_index + d->face->v3; - else if(x == 0 && y == coord_edge) - return d->dm_first_base_vert_index + d->face->v4; - /* Edges */ - else if(x == 0) { - if(d->face->v4) - return de->base[3] + de->dir[3] * (y - 1); - else - return de->base[2] + de->dir[2] * (y - 1); - } - else if(y == 0) - return de->base[0] + de->dir[0] * (x - 1); - else if(x == d->sidetot - 1) - return de->base[1] + de->dir[1] * (y - 1); - else if(y == d->sidetot - 1) - return de->base[2] + de->dir[2] * (x - 1); - /* Face center */ - else if(x == mid && y == mid) - return base; - /* Cross */ - else if(x == mid && y < mid) - return base + (mid - y); - else if(y == mid && x > mid) - return base + lim + (x - mid); - else if(x == mid && y > mid) - return base + lim*2 + (y - mid); - else if(y == mid && x < mid) { - if(d->face->v4) - return base + lim*3 + (mid - x); - else - return base + lim*2 + (mid - x); - } - /* Quarters */ - else { - int offset = base + lim * (d->face->v4 ? 4 : 3); - if(x < mid && y < mid) - return offset + ((mid - x - 1)*lim + (mid - y)); - else if(x > mid && y < mid) - return offset + qtot + ((mid - y - 1)*lim + (x - mid)); - else if(x > mid && y > mid) - return offset + qtot*2 + ((x - mid - 1)*lim + (y - mid)); - else if(x < mid && y > mid) - return offset + qtot*3 + ((y - mid - 1)*lim + (mid - x)); - } - - return -1; } -/* Calculate the TS matrix used for applying displacements. - Uses the undisplaced subdivided mesh's curvature to find a - smoothly normal and tangents. */ -static void calc_disp_mat(MultiresDisplacer *d, float mat[3][3]) +struct DerivedMesh *multires_dm_create_from_derived(MultiresModifierData *mmd, int local_mmd, DerivedMesh *dm, Object *ob, + int useRenderParams, int isFinalCalc) { - int u = multires_index_at_loc(d->face_index, d->x + 1, d->y, d, &d->edges_primary); - int v = multires_index_at_loc(d->face_index, d->x, d->y + 1, d, &d->edges_primary); - float norm[3], t1[3], t2[3], inv[3][3]; - MVert *base = d->subco + d->subco_index; - - //printf("f=%d, x=%d, y=%d, i=%d, u=%d, v=%d ", d->face_index, d->x, d->y, d->subco_index, u, v); - - norm[0] = base->no[0] / 32767.0f; - norm[1] = base->no[1] / 32767.0f; - norm[2] = base->no[2] / 32767.0f; - - /* Special handling for vertices of valence 3 */ - if(d->valence[1] == 3 && d->x == d->sidetot - 1 && d->y == 0) - u = -1; - else if(d->valence[2] == 3 && d->x == d->sidetot - 1 && d->y == d->sidetot - 1) - u = v = -1; - else if(d->valence[3] == 3 && d->x == 0 && d->y == d->sidetot - 1) - v = -1; - - /* If either u or v is -2, it's on a boundary. In this - case, back up by one row/column and use the same - vector as the preceeding sub-edge. */ - - if(u < 0) { - u = multires_index_at_loc(d->face_index, d->x - 1, d->y, d, &d->edges_primary); - sub_v3_v3v3(t1, base->co, d->subco[u].co); - } - else - sub_v3_v3v3(t1, d->subco[u].co, base->co); - - if(v < 0) { - v = multires_index_at_loc(d->face_index, d->x, d->y - 1, d, &d->edges_primary); - sub_v3_v3v3(t2, base->co, d->subco[v].co); + Mesh *me= ob->data; + DerivedMesh *result; + CCGDerivedMesh *ccgdm; + DMGridData **gridData, **subGridData; + int lvl= multires_get_level(ob, mmd, useRenderParams); + int i, gridSize, numGrids; + + if(lvl == 0) + return dm; + + result = subsurf_dm_create_local(ob, dm, lvl, + mmd->simple, mmd->flags & eMultiresModifierFlag_ControlEdges); + + if(!local_mmd) { + ccgdm = (CCGDerivedMesh*)result; + + ccgdm->multires.ob = ob; + ccgdm->multires.mmd = mmd; + ccgdm->multires.local_mmd = local_mmd; + ccgdm->multires.lvl = lvl; + ccgdm->multires.totlvl = mmd->totlvl; + ccgdm->multires.modified = 0; + ccgdm->multires.update = multiresModifier_update; } - else - sub_v3_v3v3(t2, d->subco[v].co, base->co); - //printf("uu=%d, vv=%d\n", u, v); + numGrids = result->getNumGrids(result); + gridSize = result->getGridSize(result); + gridData = result->getGridData(result); - normalize_v3(t1); - normalize_v3(t2); - Mat3FromColVecs(mat, t1, t2, norm); + subGridData = MEM_callocN(sizeof(DMGridData*)*numGrids, "subGridData*"); - if(d->invert) { - invert_m3_m3(inv, mat); - copy_m3_m3(mat, inv); + for(i = 0; i < numGrids; i++) { + subGridData[i] = MEM_callocN(sizeof(DMGridData)*gridSize*gridSize, "subGridData"); + memcpy(subGridData[i], gridData[i], sizeof(DMGridData)*gridSize*gridSize); } -} -static void multires_displace(MultiresDisplacer *d, float co[3]) -{ - float disp[3], mat[3][3]; - float *data; - MVert *subco = &d->subco[d->subco_index]; + CustomData_external_read(&me->fdata, &me->id, CD_MASK_MDISPS, me->totface); + multiresModifier_disp_run(result, ob->data, 0, 0, subGridData, mmd->totlvl); - if(!d->grid || !d->grid->disps) return; + for(i = 0; i < numGrids; i++) + MEM_freeN(subGridData[i]); + MEM_freeN(subGridData); - data = d->grid->disps[(d->y * d->spacing) * d->disp_st + (d->x * d->spacing)]; + return result; +} - if(d->invert) - sub_v3_v3v3(disp, co, subco->co); - else - copy_v3_v3(disp, data); +/**** Old Multires code **** +***************************/ +/* Adapted from sculptmode.c */ +static void old_mdisps_bilinear(float out[3], float (*disps)[3], int st, float u, float v) +{ + int x, y, x2, y2; + const int st_max = st - 1; + float urat, vrat, uopp; + float d[4][3], d2[2][3]; + + if(u < 0) + u = 0; + else if(u >= st) + u = st_max; + if(v < 0) + v = 0; + else if(v >= st) + v = st_max; + + x = floor(u); + y = floor(v); + x2 = x + 1; + y2 = y + 1; + + if(x2 >= st) x2 = st_max; + if(y2 >= st) y2 = st_max; + + urat = u - x; + vrat = v - y; + uopp = 1 - urat; - /* Apply ts matrix to displacement */ - calc_disp_mat(d, mat); - mul_m3_v3(mat, disp); + mul_v3_v3fl(d[0], disps[y * st + x], uopp); + mul_v3_v3fl(d[1], disps[y * st + x2], urat); + mul_v3_v3fl(d[2], disps[y2 * st + x], uopp); + mul_v3_v3fl(d[3], disps[y2 * st + x2], urat); - if(d->invert) { - copy_v3_v3(data, disp); - - } - else { - if(d->type == 4 || d->type == 5) - mul_v3_fl(disp, d->weight); - add_v3_v3v3(co, co, disp); - } + add_v3_v3v3(d2[0], d[0], d[1]); + add_v3_v3v3(d2[1], d[2], d[3]); + mul_v3_fl(d2[0], 1 - vrat); + mul_v3_fl(d2[1], vrat); - if(d->type == 2) { - if(d->sidendx == 0) - d->y -= 1; - else if(d->sidendx == 1) - d->x += 1; - else if(d->sidendx == 2) - d->y += 1; - else if(d->sidendx == 3) - d->x -= 1; - } - else if(d->type == 3) { - if(d->sidendx == 0) - d->y -= 1; - else if(d->sidendx == 1) - d->x += 1; - else if(d->sidendx == 2) - d->y += 1; - else if(d->sidendx == 3) - d->x -= 1; - } + add_v3_v3v3(out, d2[0], d2[1]); } -static void multiresModifier_disp_run(DerivedMesh *dm, MVert *subco, int invert) +static void old_mdisps_rotate(int S, int newside, int oldside, int x, int y, float *u, float *v) { - const int lvl = MultiresDM_get_lvl(dm); - const int gridFaces = multires_side_tot[lvl - 2] - 1; - const int edgeSize = multires_side_tot[lvl - 1] - 1; - MVert *mvert = CDDM_get_verts(dm); - MEdge *medge = MultiresDM_get_mesh(dm)->medge; - MFace *mface = MultiresDM_get_mesh(dm)->mface; - ListBase *map = MultiresDM_get_vert_face_map(dm); - Mesh *me = MultiresDM_get_mesh(dm); - MultiresDisplacer d; - int i, S, x, y; - - d.subco = subco; - d.subco_index = 0; + float offset = oldside*0.5f - 0.5f; - for(i = 0; i < me->totface; ++i) { - const int numVerts = mface[i].v4 ? 4 : 3; - - /* Center */ - multires_displacer_init(&d, dm, i, invert); - multires_displacer_anchor(&d, 1, 0); - multires_displace(&d, mvert->co); - ++mvert; - ++d.subco_index; - - /* Cross */ - for(S = 0; S < numVerts; ++S) { - multires_displacer_anchor(&d, 2, S); - for(x = 1; x < gridFaces; ++x) { - multires_displace(&d, mvert->co); - ++mvert; - ++d.subco_index; - } - } + if(S == 1) { *u= offset + x; *v = offset - y; } + if(S == 2) { *u= offset + y; *v = offset + x; } + if(S == 3) { *u= offset - x; *v = offset + y; } + if(S == 0) { *u= offset - y; *v = offset - x; } +} - /* Quarters */ - for(S = 0; S < numVerts; S++) { - multires_displacer_anchor(&d, 3, S); - for(y = 1; y < gridFaces; y++) { - for(x = 1; x < gridFaces; x++) { - multires_displace(&d, mvert->co); - ++mvert; - ++d.subco_index; - } - multires_displacer_jump(&d); +static void old_mdisps_convert(MFace *mface, MDisps *mdisp) +{ + int newlvl = log(sqrt(mdisp->totdisp)-1)/log(2); + int oldlvl = newlvl+1; + int oldside = multires_side_tot[oldlvl]; + int newside = multires_side_tot[newlvl]; + int nvert = (mface->v4)? 4: 3; + int newtotdisp = multires_grid_tot[newlvl]*nvert; + int x, y, S; + float (*disps)[3], (*out)[3], u, v; + + disps = MEM_callocN(sizeof(float) * 3 * newtotdisp, "multires disps"); + + out = disps; + for(S = 0; S < nvert; S++) { + for(y = 0; y < newside; ++y) { + for(x = 0; x < newside; ++x, ++out) { + old_mdisps_rotate(S, newside, oldside, x, y, &u, &v); + old_mdisps_bilinear(*out, mdisp->disps, oldside, u, v); } } } - for(i = 0; i < me->totedge; ++i) { - const MEdge *e = &medge[i]; - for(x = 1; x < edgeSize; ++x) { - IndexNode *n1, *n2; - int numFaces = 0; - for(n1 = map[e->v1].first; n1; n1 = n1->next) { - for(n2 = map[e->v2].first; n2; n2 = n2->next) { - if(n1->index == n2->index) - ++numFaces; - } - } - multires_displacer_weight(&d, 1.0f / numFaces); - /* TODO: Better to have these loops outside the x loop */ - for(n1 = map[e->v1].first; n1; n1 = n1->next) { - for(n2 = map[e->v2].first; n2; n2 = n2->next) { - if(n1->index == n2->index) { - multires_displacer_init(&d, dm, n1->index, invert); - multires_displacer_anchor_edge(&d, e->v1, e->v2, x); - multires_displace(&d, mvert->co); - } - } - } - ++mvert; - ++d.subco_index; - } - } - - for(i = 0; i < me->totvert; ++i) { - IndexNode *n; - multires_displacer_weight(&d, 1.0f / BLI_countlist(&map[i])); - for(n = map[i].first; n; n = n->next) { - multires_displacer_init(&d, dm, n->index, invert); - multires_displacer_anchor_vert(&d, i); - multires_displace(&d, mvert->co); - } - ++mvert; - ++d.subco_index; - } + MEM_freeN(mdisp->disps); - if(!invert) - CDDM_calc_normals(dm); + mdisp->totdisp= newtotdisp; + mdisp->disps= disps; } -static void multiresModifier_update(DerivedMesh *dm) +void multires_load_old_250(Mesh *me) { - Object *ob; - Mesh *me; MDisps *mdisps; + int a; - ob = MultiresDM_get_object(dm); - me = MultiresDM_get_mesh(dm); - mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS); + mdisps= CustomData_get_layer(&me->fdata, CD_MDISPS); if(mdisps) { - const int lvl = MultiresDM_get_lvl(dm); - const int totlvl = MultiresDM_get_totlvl(dm); - - if(lvl < totlvl) { - /* Propagate disps upwards */ - DerivedMesh *final, *subco_dm, *orig; - MVert *verts_new = NULL, *cur_lvl_orig_verts = NULL; - MultiresModifierData mmd; - int i; - - orig = CDDM_from_mesh(me, NULL); - - /* Regenerate the current level's vertex coordinates - (includes older displacements but not new sculpts) */ - mmd.totlvl = totlvl; - mmd.lvl = lvl; - subco_dm = multires_dm_create_from_derived(&mmd, 1, orig, ob, 0, 0); - cur_lvl_orig_verts = CDDM_get_verts(subco_dm); - - /* Subtract the original vertex cos from the new vertex cos */ - verts_new = CDDM_get_verts(dm); - for(i = 0; i < dm->getNumVerts(dm); ++i) - sub_v3_v3v3(verts_new[i].co, verts_new[i].co, cur_lvl_orig_verts[i].co); - - final = multires_subdisp_pre(dm, totlvl - lvl, 0); - - multires_subdisp(orig, ob, final, lvl, totlvl, dm->getNumVerts(dm), dm->getNumEdges(dm), - dm->getNumFaces(dm), 1); - - subco_dm->release(subco_dm); - orig->release(orig); - } - else - multiresModifier_disp_run(dm, MultiresDM_get_subco(dm), 1); - } -} - -void multires_mark_as_modified(struct Object *ob) -{ - if(ob && ob->derivedFinal) { - MultiresDM_mark_as_modified(ob->derivedFinal); - } -} - -void multires_force_update(Object *ob) -{ - if(ob && ob->derivedFinal) { - ob->derivedFinal->needsFree =1; - ob->derivedFinal->release(ob->derivedFinal); - ob->derivedFinal = NULL; + for(a=0; a<me->totface; a++) + old_mdisps_convert(&me->mface[a], &mdisps[a]); } } -struct DerivedMesh *multires_dm_create_from_derived(MultiresModifierData *mmd, int local_mmd, DerivedMesh *dm, Object *ob, - int useRenderParams, int isFinalCalc) -{ - SubsurfModifierData smd; - MultiresSubsurf ms; - DerivedMesh *result; - int i; - - ms.mmd = mmd; - ms.ob = ob; - ms.local_mmd = local_mmd; - - memset(&smd, 0, sizeof(SubsurfModifierData)); - smd.levels = smd.renderLevels = mmd->lvl - 1; - smd.flags |= eSubsurfModifierFlag_SubsurfUv; - - result = subsurf_make_derived_from_derived_with_multires(dm, &smd, &ms, useRenderParams, NULL, isFinalCalc, 0); - for(i = 0; i < result->getNumVerts(result); ++i) - MultiresDM_get_subco(result)[i] = CDDM_get_verts(result)[i]; - multiresModifier_disp_run(result, MultiresDM_get_subco(result), 0); - MultiresDM_set_update(result, multiresModifier_update); - - return result; -} - -/**** Old Multires code **** -***************************/ - /* Does not actually free lvl itself */ static void multires_free_level(MultiresLevel *lvl) { @@ -1419,15 +960,75 @@ static void multires_load_old_faces(ListBase **fmap, ListBase **emap, MultiresLe } } +static void multires_old_mvert_to_ss(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(vd->co, 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); + } + } + + 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(vd->co, mvert[i].co); + } + } + } + } + + 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(vd->co, mvert[i].co); + } + } + + totvert = ccgSubSurf_getNumVerts(ss); + for(index = 0; index < totvert; index++) { + CCGVert *v = ccgdm->vertMap[index].vert; + + vd= ccgSubSurf_getVertData(ss, v); + copy_v3_v3(vd->co, mvert[i].co); + i++; + } + + ccgSubSurf_updateToFaces(ss, 0, NULL, 0); +} + /* Loads a multires object stored in the old Multires struct into the new format */ -void multires_load_old(DerivedMesh *dm, Multires *mr) +static void multires_load_old_dm(DerivedMesh *dm, Mesh *me, int totlvl) { MultiresLevel *lvl, *lvl1; + Multires *mr= me->mr; MVert *vsrc, *vdst; int src, dst; - int totlvl = MultiresDM_get_totlvl(dm); - int st = multires_side_tot[totlvl - 2] - 1; - int extedgelen = multires_side_tot[totlvl - 1] - 2; + int st = multires_side_tot[totlvl - 1] - 1; + int extedgelen = multires_side_tot[totlvl] - 2; int *vvmap; // inorder for dst, map to src int crossedgelen; int i, j, s, x, totvert, tottri, totquad; @@ -1435,7 +1036,7 @@ void multires_load_old(DerivedMesh *dm, Multires *mr) src = 0; dst = 0; vsrc = mr->verts; - vdst = CDDM_get_verts(dm); + vdst = dm->getVertArray(dm); totvert = dm->getNumVerts(dm); vvmap = MEM_callocN(sizeof(int) * totvert, "multires vvmap"); @@ -1454,9 +1055,9 @@ void multires_load_old(DerivedMesh *dm, Multires *mr) lvl = lvl1->next; for(j = 2; j <= mr->level_count; ++j) { - int base = multires_side_tot[totlvl - j] - 2; - int skip = multires_side_tot[totlvl - j + 1] - 1; - int st = multires_side_tot[j - 2] - 1; + int base = multires_side_tot[totlvl - j + 1] - 2; + int skip = multires_side_tot[totlvl - j + 2] - 1; + int st = multires_side_tot[j - 1] - 1; for(x = 0; x < st; ++x) vvmap[ldst + base + x * skip] = lsrc + st * i + x; @@ -1483,7 +1084,7 @@ void multires_load_old(DerivedMesh *dm, Multires *mr) /* Face edge cross */ tottri = totquad = 0; - crossedgelen = multires_side_tot[totlvl - 2] - 2; + crossedgelen = multires_side_tot[totlvl - 1] - 2; dst = 0; for(i = 0; i < lvl1->totface; ++i) { int sides = lvl1->faces[i].v[3] ? 4 : 3; @@ -1492,8 +1093,8 @@ void multires_load_old(DerivedMesh *dm, Multires *mr) ++dst; for(j = 3; j <= mr->level_count; ++j) { - int base = multires_side_tot[totlvl - j] - 2; - int skip = multires_side_tot[totlvl - j + 1] - 1; + int base = multires_side_tot[totlvl - j + 1] - 2; + int skip = multires_side_tot[totlvl - j + 2] - 1; int st = pow(2, j - 2); int st2 = pow(2, j - 3); int lsrc = lvl->prev->totvert; @@ -1541,8 +1142,8 @@ void multires_load_old(DerivedMesh *dm, Multires *mr) int ldst = dst + 1 + sides * (st - 1); for(s = 0; s < sides; ++s) { - int st2 = multires_side_tot[totlvl - 2] - 2; - int st3 = multires_side_tot[totlvl - 3] - 2; + int st2 = multires_side_tot[totlvl - 1] - 2; + int st3 = multires_side_tot[totlvl - 2] - 2; int st4 = st3 == 0 ? 1 : (st3 + 1) / 2; int mid = ldst + st2 * st3 + st3; int cv = lvl1->faces[j].v[s]; @@ -1582,4 +1183,64 @@ void multires_load_old(DerivedMesh *dm, Multires *mr) copy_v3_v3(vdst[i].co, vsrc[vvmap[i]].co); MEM_freeN(vvmap); + + multires_old_mvert_to_ss(dm, vdst); } + + +void multires_load_old(Object *ob, Mesh *me) +{ + MultiresLevel *lvl; + ModifierData *md; + MultiresModifierData *mmd; + DerivedMesh *dm, *orig; + int i; + + /* Load original level into the mesh */ + lvl = me->mr->levels.first; + CustomData_free_layers(&me->vdata, CD_MVERT, lvl->totvert); + CustomData_free_layers(&me->edata, CD_MEDGE, lvl->totedge); + CustomData_free_layers(&me->fdata, CD_MFACE, lvl->totface); + me->totvert = lvl->totvert; + me->totedge = lvl->totedge; + me->totface = lvl->totface; + me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, me->totvert); + me->medge = CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, NULL, me->totedge); + me->mface = CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, me->totface); + memcpy(me->mvert, me->mr->verts, sizeof(MVert) * me->totvert); + for(i = 0; i < me->totedge; ++i) { + me->medge[i].v1 = lvl->edges[i].v[0]; + me->medge[i].v2 = lvl->edges[i].v[1]; + } + for(i = 0; i < me->totface; ++i) { + me->mface[i].v1 = lvl->faces[i].v[0]; + me->mface[i].v2 = lvl->faces[i].v[1]; + me->mface[i].v3 = lvl->faces[i].v[2]; + me->mface[i].v4 = lvl->faces[i].v[3]; + } + + /* Add a multires modifier to the object */ + md = ob->modifiers.first; + while(md && modifierType_getInfo(md->type)->type == eModifierTypeType_OnlyDeform) + md = md->next; + mmd = (MultiresModifierData*)modifier_new(eModifierType_Multires); + BLI_insertlinkbefore(&ob->modifiers, md, mmd); + + for(i = 0; i < me->mr->level_count - 1; ++i) + multiresModifier_subdivide(mmd, ob, 1, 0); + + mmd->lvl = mmd->totlvl; + orig = CDDM_from_mesh(me, NULL); + dm = multires_dm_create_from_derived(mmd, 0, orig, ob, 0, 0); + + multires_load_old_dm(dm, me, mmd->totlvl+1); + + multires_dm_mark_as_modified(dm); + dm->release(dm); + orig->release(orig); + + /* Remove the old multires */ + multires_free(me->mr); + me->mr= NULL; +} + diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c index 048e1cd01e3..47830453c2e 100644 --- a/source/blender/blenkernel/intern/object.c +++ b/source/blender/blenkernel/intern/object.c @@ -228,14 +228,6 @@ void free_sculptsession(SculptSession **ssp) { if(ssp && *ssp) { SculptSession *ss = *ssp; - if(ss->projverts) - MEM_freeN(ss->projverts); - - if(ss->fmap) - MEM_freeN(ss->fmap); - - if(ss->fmap_mem) - MEM_freeN(ss->fmap_mem); if(ss->texcache) MEM_freeN(ss->texcache); @@ -243,8 +235,8 @@ void free_sculptsession(SculptSession **ssp) if(ss->layer_disps) MEM_freeN(ss->layer_disps); - if(ss->mesh_co_orig) - MEM_freeN(ss->mesh_co_orig); + if(ss->layer_co) + MEM_freeN(ss->layer_co); MEM_freeN(ss); @@ -1777,13 +1769,15 @@ static void give_parvert(Object *par, int nr, float *vec) DerivedMesh *dm = par->derivedFinal; if(dm) { - int i, count = 0, numVerts = dm->getNumVerts(dm); + int i, count = 0, vindex, numVerts = dm->getNumVerts(dm); int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX); float co[3]; /* get the average of all verts with (original index == nr) */ - for(i = 0; i < numVerts; ++i, ++index) { - if(*index == nr) { + for(i = 0; i < numVerts; ++i) { + vindex= (index)? *index: i; + + if(vindex == nr) { dm->getVertCo(dm, i, co); add_v3_v3v3(vec, vec, co); count++; diff --git a/source/blender/blenkernel/intern/paint.c b/source/blender/blenkernel/intern/paint.c index 27fcfa8b9cb..4fe63a966be 100644 --- a/source/blender/blenkernel/intern/paint.c +++ b/source/blender/blenkernel/intern/paint.c @@ -175,6 +175,8 @@ void paint_init(Paint *p, const char col[3]) memcpy(p->paint_cursor_col, col, 3); p->paint_cursor_col[3] = 128; + + p->flags |= PAINT_SHOW_BRUSH; } void free_paint(Paint *paint) diff --git a/source/blender/blenkernel/intern/particle.c b/source/blender/blenkernel/intern/particle.c index ab73b24ba39..1968673e568 100644 --- a/source/blender/blenkernel/intern/particle.c +++ b/source/blender/blenkernel/intern/particle.c @@ -790,7 +790,7 @@ int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot) totface= dm->getNumFaces(dm); totorigface= me->totface; - if(totface == 0 || totorigface == 0 || origindex == NULL) + if(totface == 0 || totorigface == 0) return tot; facearea= MEM_callocN(sizeof(float)*totorigface, "SimplifyFaceArea"); @@ -807,14 +807,14 @@ int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot) /* compute number of children per original face */ for(a=0; a<tot; a++) { - b= origindex[ctx->index[a]]; + b= (origindex)? origindex[ctx->index[a]]: ctx->index[a]; if(b != -1) elems[b].totchild++; } /* compute areas and centers of original faces */ for(mf=mface, a=0; a<totface; a++, mf++) { - b= origindex[a]; + b= (origindex)? origindex[a]: a; if(b != -1) { VECCOPY(co1, mvert[mf->v1].co); @@ -910,7 +910,7 @@ int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot) skipped= 0; for(a=0, newtot=0; a<tot; a++) { - b= origindex[ctx->index[a]]; + b= (origindex)? origindex[ctx->index[a]]: ctx->index[a]; if(b != -1) { if(elems[b].curchild++ < ceil(elems[b].lambda*elems[b].totchild)) { ctx->index[newtot]= ctx->index[a]; @@ -943,7 +943,7 @@ int psys_render_simplify_params(ParticleSystem *psys, ChildParticle *cpa, float if(!data->dosimplify) return 0; - b= data->origindex[cpa->num]; + b= (data->origindex)? data->origindex[cpa->num]: cpa->num; if(b == -1) return 0; diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index 3c660be64ce..ce595bb280d 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -287,12 +287,12 @@ void psys_calc_dmcache(Object *ob, DerivedMesh *dm, ParticleSystem *psys) if(psys->part->from == PART_FROM_VERT) { totdmelem= dm->getNumVerts(dm); totelem= me->totvert; - origindex= DM_get_vert_data_layer(dm, CD_ORIGINDEX); + origindex= dm->getVertDataArray(dm, CD_ORIGINDEX); } else { /* FROM_FACE/FROM_VOLUME */ totdmelem= dm->getNumFaces(dm); totelem= me->totface; - origindex= DM_get_face_data_layer(dm, CD_ORIGINDEX); + origindex= dm->getFaceDataArray(dm, CD_ORIGINDEX); } nodedmelem= MEM_callocN(sizeof(LinkNode)*totdmelem, "psys node elems"); @@ -948,7 +948,8 @@ static int psys_threads_init_distribution(ParticleThread *threads, Scene *scene, if(totpart==0) return 0; - if (!finaldm->deformedOnly && !CustomData_has_layer( &finaldm->faceData, CD_ORIGINDEX ) ) { + if (!finaldm->deformedOnly && !finaldm->getFaceDataArray(finaldm, CD_ORIGINDEX)) { + printf("Can't create particles with the current modifier stack, disable destructive modifiers\n"); // XXX error("Can't paint with the current modifier stack, disable destructive modifiers"); return 0; } diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c index cb2e2c437bf..f6abedda2b6 100644 --- a/source/blender/blenkernel/intern/subsurf_ccg.c +++ b/source/blender/blenkernel/intern/subsurf_ccg.c @@ -53,11 +53,12 @@ #include "BKE_subsurf.h" #include "BLI_blenlib.h" +#include "BLI_edgehash.h" #include "BLI_editVert.h" -#include "BLI_math.h" #include "BLI_linklist.h" +#include "BLI_math.h" #include "BLI_memarena.h" -#include "BLI_edgehash.h" +#include "BLI_pbvh.h" #include "BIF_gl.h" #include "BIF_glutil.h" @@ -68,25 +69,6 @@ #include "CCGSubSurf.h" -typedef struct _VertData { - float co[3]; - float no[3]; -} VertData; - -struct CCGDerivedMesh { - DerivedMesh dm; - - CCGSubSurf *ss; - int drawInteriorEdges, useSubsurfUv; - - struct {int startVert; CCGVert *vert;} *vertMap; - struct {int startVert; int startEdge; CCGEdge *edge;} *edgeMap; - struct {int startVert; int startEdge; - int startFace; CCGFace *face;} *faceMap; -}; - -typedef struct CCGDerivedMesh CCGDerivedMesh; - static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v); static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e); static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f); @@ -136,7 +118,7 @@ static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels, int useAgin } else { ifc.vertUserSize = ifc.edgeUserSize = ifc.faceUserSize = 8; } - ifc.vertDataSize = sizeof(VertData); + ifc.vertDataSize = sizeof(DMGridData); if (useArena) { CCGAllocatorIFC allocatorIFC; @@ -156,7 +138,7 @@ static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels, int useAgin ccgSubSurf_setUseAgeCounts(ccgSS, 1, 8, 8, 8); } - ccgSubSurf_setCalcVertexNormals(ccgSS, 1, BLI_STRUCT_OFFSET(VertData, no)); + ccgSubSurf_setCalcVertexNormals(ccgSS, 1, BLI_STRUCT_OFFSET(DMGridData, no)); return ccgSS; } @@ -340,7 +322,7 @@ static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, if(!dmtface || !tface) return; - /* create a CCGSubsurf from uv's */ + /* create a CCGSubSurf from uv's */ uvss = _getSubSurf(NULL, ccgSubSurf_getSubdivisionLevels(ss), 0, 1, 0); if(!ss_sync_from_uv(uvss, ss, dm, dmtface)) { @@ -348,7 +330,7 @@ static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, return; } - /* get some info from CCGSubsurf */ + /* get some info from CCGSubSurf */ totface = ccgSubSurf_getNumFaces(uvss); edgeSize = ccgSubSurf_getEdgeSize(uvss); gridSize = ccgSubSurf_getGridSize(uvss); @@ -372,7 +354,7 @@ static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, int numVerts = ccgSubSurf_getFaceNumVerts(f); for (S=0; S<numVerts; S++) { - VertData *faceGridData= ccgSubSurf_getFaceGridDataArray(uvss, f, S); + DMGridData *faceGridData= ccgSubSurf_getFaceGridDataArray(uvss, f, S); for(y = 0; y < gridFaces; y++) { for(x = 0; x < gridFaces; x++) { @@ -396,37 +378,6 @@ static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, MEM_freeN(faceMap); } -#if 0 -static unsigned int ss_getEdgeFlags(CCGSubSurf *ss, CCGEdge *e, int ssFromEditmesh, DispListMesh *dlm, MEdge *medge, MTFace *tface) -{ - unsigned int flags = 0; - int N = ccgSubSurf_getEdgeNumFaces(e); - - if (!N) flags |= ME_LOOSEEDGE; - - if (ssFromEditmesh) { - EditEdge *eed = ccgSubSurf_getEdgeEdgeHandle(e); - - flags |= ME_EDGEDRAW|ME_EDGERENDER; - if (eed->seam) { - flags |= ME_SEAM; - } - } else { - if (edgeIdx!=-1) { - MEdge *origMed = &medge[edgeIdx]; - - if (dlm) { - flags |= origMed->flag&~ME_EDGE_STEPINDEX; - } else { - flags |= (origMed->flag&ME_SEAM)|ME_EDGEDRAW|ME_EDGERENDER; - } - } - } - - return flags; -} -#endif - /* face weighting */ static void calc_ss_weights(int gridFaces, FaceVertWeight **qweight, FaceVertWeight **tweight) @@ -471,360 +422,6 @@ static void calc_ss_weights(int gridFaces, } } -static DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, - int drawInteriorEdges, int useSubsurfUv, - DerivedMesh *dm, MultiresSubsurf *ms) -{ - DerivedMesh *result; - int edgeSize = ccgSubSurf_getEdgeSize(ss); - int gridSize = ccgSubSurf_getGridSize(ss); - int gridFaces = gridSize - 1; - int edgeBase, faceBase; - int i, j, k, S, x, y, index; - CCGVertIterator *vi; - CCGEdgeIterator *ei; - CCGFaceIterator *fi; - CCGFace **faceMap2; - CCGEdge **edgeMap2; - CCGVert **vertMap2; - int totvert, totedge, totface; - MVert *mvert; - MEdge *med; - MFace *mf; - int *origIndex; - FaceVertWeight *qweight, *tweight; - - calc_ss_weights(gridFaces, &qweight, &tweight); - - /* vert map */ - 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(; !ccgEdgeIterator_isStopped(ei); 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(ss, f))] = f; - } - ccgFaceIterator_free(fi); - - if(ms) { - result = MultiresDM_new(ms, dm, ccgSubSurf_getNumFinalVerts(ss), - ccgSubSurf_getNumFinalEdges(ss), - ccgSubSurf_getNumFinalFaces(ss)); - } - else { - if(dm) { - result = CDDM_from_template(dm, ccgSubSurf_getNumFinalVerts(ss), - ccgSubSurf_getNumFinalEdges(ss), - ccgSubSurf_getNumFinalFaces(ss)); - } else { - result = CDDM_new(ccgSubSurf_getNumFinalVerts(ss), - ccgSubSurf_getNumFinalEdges(ss), - ccgSubSurf_getNumFinalFaces(ss)); - } - } - - // load verts - faceBase = i = 0; - mvert = CDDM_get_verts(result); - origIndex = result->getVertData(result, 0, CD_ORIGINDEX); - - for(index = 0; index < totface; index++) { - CCGFace *f = faceMap2[index]; - int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); - FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight; - int vertIdx[4]; - - for(S = 0; S < numVerts; S++) { - CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S); - - vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); - } - - DM_interp_vert_data(dm, result, vertIdx, weight[0][0], numVerts, i); - copy_v3_v3(mvert->co, ccgSubSurf_getFaceCenterData(f)); - *origIndex = ORIGINDEX_NONE; - ++mvert; - ++origIndex; - i++; - - for(S = 0; S < numVerts; S++) { - int prevS = (S - 1 + numVerts) % numVerts; - int nextS = (S + 1) % numVerts; - int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3; - - for(x = 1; x < gridFaces; x++) { - float w[4]; - w[prevS] = weight[x][0][0]; - w[S] = weight[x][0][1]; - w[nextS] = weight[x][0][2]; - w[otherS] = weight[x][0][3]; - DM_interp_vert_data(dm, result, vertIdx, w, numVerts, i); - copy_v3_v3(mvert->co, - ccgSubSurf_getFaceGridEdgeData(ss, f, S, x)); - - *origIndex = ORIGINDEX_NONE; - ++mvert; - ++origIndex; - i++; - } - } - - for(S = 0; S < numVerts; S++) { - int prevS = (S - 1 + numVerts) % numVerts; - int nextS = (S + 1) % numVerts; - int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3; - - for(y = 1; y < gridFaces; y++) { - for(x = 1; x < gridFaces; x++) { - float w[4]; - w[prevS] = weight[y * gridFaces + x][0][0]; - w[S] = weight[y * gridFaces + x][0][1]; - w[nextS] = weight[y * gridFaces + x][0][2]; - w[otherS] = weight[y * gridFaces + x][0][3]; - DM_interp_vert_data(dm, result, vertIdx, w, numVerts, i); - copy_v3_v3(mvert->co, - ccgSubSurf_getFaceGridData(ss, f, S, x, y)); - *origIndex = ORIGINDEX_NONE; - ++mvert; - ++origIndex; - i++; - } - } - } - - *((int*)ccgSubSurf_getFaceUserData(ss, f)) = faceBase; - faceBase += 1 + numVerts * ((gridSize-2) + (gridSize-2) * (gridSize-2)); - } - - edgeBase = i; - for(index = 0; index < totedge; index++) { - CCGEdge *e = edgeMap2[index]; - int x; - int vertIdx[2]; - - CCGVert *v; - v = ccgSubSurf_getEdgeVert0(e); - vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); - v = ccgSubSurf_getEdgeVert1(e); - vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); - - for(x = 1; x < edgeSize - 1; x++) { - float w[2]; - w[1] = (float) x / (edgeSize - 1); - w[0] = 1 - w[1]; - DM_interp_vert_data(dm, result, vertIdx, w, 2, i); - copy_v3_v3(mvert->co, ccgSubSurf_getEdgeData(ss, e, x)); - *origIndex = ORIGINDEX_NONE; - ++mvert; - ++origIndex; - i++; - } - - *((int*)ccgSubSurf_getEdgeUserData(ss, e)) = edgeBase; - edgeBase += edgeSize-2; - } - - for(index = 0; index < totvert; index++) { - CCGVert *v = vertMap2[index]; - int vertIdx; - - vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); - - DM_copy_vert_data(dm, result, vertIdx, i, 1); - copy_v3_v3(mvert->co, ccgSubSurf_getVertData(ss, v)); - - *((int*)ccgSubSurf_getVertUserData(ss, v)) = i; - *origIndex = ccgDM_getVertMapIndex(ss, v); - ++mvert; - ++origIndex; - i++; - } - - // load edges - i = 0; - med = CDDM_get_edges(result); - origIndex = result->getEdgeData(result, 0, CD_ORIGINDEX); - - for(index = 0; index < totface; index++) { - CCGFace *f = faceMap2[index]; - int numVerts = ccgSubSurf_getFaceNumVerts(f); - - for(k = 0; k < numVerts; k++) { - for(x = 0; x < gridFaces; x++) { - if(drawInteriorEdges) med->flag = ME_EDGEDRAW | ME_EDGERENDER; - med->v1 = getFaceIndex(ss, f, k, x, 0, edgeSize, gridSize); - med->v2 = getFaceIndex(ss, f, k, x+1, 0, edgeSize, gridSize); - *origIndex = ORIGINDEX_NONE; - ++med; - ++origIndex; - i++; - } - - for(x = 1; x < gridFaces; x++) { - for(y = 0; y < gridFaces; y++) { - if(drawInteriorEdges) - med->flag = ME_EDGEDRAW | ME_EDGERENDER; - med->v1 = getFaceIndex(ss, f, k, x, y, edgeSize, gridSize); - med->v2 = getFaceIndex(ss, f, k, x, y + 1, - edgeSize, gridSize); - *origIndex = ORIGINDEX_NONE; - ++med; - ++origIndex; - i++; - - if(drawInteriorEdges) - med->flag = ME_EDGEDRAW | ME_EDGERENDER; - med->v1 = getFaceIndex(ss, f, k, y, x, edgeSize, gridSize); - med->v2 = getFaceIndex(ss, f, k, y + 1, x, - edgeSize, gridSize); - *origIndex = ORIGINDEX_NONE; - ++med; - ++origIndex; - i++; - } - } - } - } - - for(index = 0; index < totedge; index++) { - CCGEdge *e = edgeMap2[index]; - unsigned int flags = 0; - char bweight = 0; - int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e)); - - if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE; - - - if(edgeIdx != -1 && dm) { - MEdge origMed; - dm->getEdge(dm, edgeIdx, &origMed); - - flags |= origMed.flag; - bweight = origMed.bweight; - } - - for(x = 0; x < edgeSize - 1; x++) { - med->v1 = getEdgeIndex(ss, e, x, edgeSize); - med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize); - med->flag = flags; - med->bweight = bweight; - *origIndex = ccgDM_getEdgeMapIndex(ss, e); - ++med; - ++origIndex; - i++; - } - } - - // load faces - i = 0; - mf = CDDM_get_faces(result); - origIndex = result->getFaceData(result, 0, CD_ORIGINDEX); - - for(index = 0; index < totface; index++) { - CCGFace *f = faceMap2[index]; - int numVerts = ccgSubSurf_getFaceNumVerts(f); - int mat_nr; - int flag; - int mapIndex = ccgDM_getFaceMapIndex(ss, f); - int faceIdx = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); - - if(!ssFromEditmesh) { - MFace origMFace; - dm->getFace(dm, faceIdx, &origMFace); - - mat_nr = origMFace.mat_nr; - flag = origMFace.flag; - } else { - EditFace *ef = ccgSubSurf_getFaceFaceHandle(ss, f); - mat_nr = ef->mat_nr; - flag = ef->flag; - } - - for(S = 0; S < numVerts; S++) { - FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight; - - for(y = 0; y < gridFaces; y++) { - for(x = 0; x < gridFaces; x++) { - 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; - - if(dm) { - int prevS = (S - 1 + numVerts) % numVerts; - int nextS = (S + 1) % numVerts; - int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3; - FaceVertWeight w; - - for(j = 0; j < 4; ++j) { - w[j][prevS] = (*weight)[j][0]; - w[j][S] = (*weight)[j][1]; - w[j][nextS] = (*weight)[j][2]; - w[j][otherS] = (*weight)[j][3]; - } - - DM_interp_face_data(dm, result, &faceIdx, NULL, - &w, 1, i); - weight++; - } - - *origIndex = mapIndex; - ++mf; - ++origIndex; - i++; - } - } - } - } - - MEM_freeN(faceMap2); - MEM_freeN(edgeMap2); - MEM_freeN(vertMap2); - - MEM_freeN(tweight); - MEM_freeN(qweight); - - if(useSubsurfUv) { - CustomData *fdata = &result->faceData; - CustomData *dmfdata = &dm->faceData; - int numlayer = CustomData_number_of_layers(fdata, CD_MTFACE); - int dmnumlayer = CustomData_number_of_layers(dmfdata, CD_MTFACE); - - for (i=0; i<numlayer && i<dmnumlayer; i++) - set_subsurf_uv(ss, dm, result, i); - } - - CDDM_calc_normals(result); - - return result; -} - static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm, float (*vertexCos)[3], int useFlatSubdiv) { @@ -846,7 +443,7 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm, mv = mvert; index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX); - for(i = 0; i < totvert; i++, mv++, index++) { + for(i = 0; i < totvert; i++, mv++) { CCGVert *v; if(vertexCos) { @@ -855,12 +452,12 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm, ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), mv->co, 0, &v); } - ((int*)ccgSubSurf_getVertUserData(ss, v))[1] = *index; + ((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++, index++) { + for(i = 0; i < totedge; i++, me++) { CCGEdge *e; float crease; @@ -870,12 +467,12 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm, 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; + ((int*)ccgSubSurf_getEdgeUserData(ss, e))[1] = (index)? *index++: i; } mf = mface; index = (int *)dm->getFaceDataArray(dm, CD_ORIGINDEX); - for (i = 0; i < totface; i++, mf++, index++) { + for (i = 0; i < totface; i++, mf++) { CCGFace *f; fVerts[0] = SET_INT_IN_POINTER(mf->v1); @@ -901,7 +498,7 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm, return; } - ((int*)ccgSubSurf_getFaceUserData(ss, f))[1] = *index; + ((int*)ccgSubSurf_getFaceUserData(ss, f))[1] = (index)? *index++: i; } ccgSubSurf_processSync(ss); @@ -942,7 +539,7 @@ static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) { for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); for (i=0; i<edgeSize; i++) DO_MINMAX(edgeData[i].co, min_r, max_r); @@ -953,7 +550,7 @@ 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++) { - VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); for (y=0; y<gridSize; y++) for (x=0; x<gridSize; x++) @@ -1107,7 +704,8 @@ static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med) /* this vert comes from edge data */ CCGEdge *e; int edgeSize = ccgSubSurf_getEdgeSize(ss); - int x, *edgeFlag; + int x; + short *edgeFlag; unsigned int flags = 0; i = (edgeNum - ccgdm->edgeMap[0].startEdge) / (edgeSize - 1); @@ -1121,7 +719,7 @@ static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med) med->v1 = getEdgeIndex(ss, e, x, edgeSize); med->v2 = getEdgeIndex(ss, e, x+1, edgeSize); - edgeFlag = dm->getEdgeData(dm, edgeNum, CD_FLAGS); + edgeFlag = (ccgdm->edgeFlags)? &ccgdm->edgeFlags[i]: NULL; if(edgeFlag) flags |= (*edgeFlag & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER; @@ -1147,7 +745,7 @@ static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf) int grid; int x, y; int lastface = ccgSubSurf_getNumFaces(ss) - 1; - char *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS); + char *faceFlags = ccgdm->faceFlags; memset(mf, 0, sizeof(*mf)); @@ -1169,7 +767,7 @@ static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf) 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*4]; + if(faceFlags) mf->flag = faceFlags[i*2]; else mf->flag = ME_SMOOTH; } @@ -1177,6 +775,7 @@ 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); @@ -1188,20 +787,25 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) CCGFace *f = ccgdm->faceMap[index].face; int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); - copy_v3_v3(mvert[i++].co, ccgSubSurf_getFaceCenterData(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++) { - copy_v3_v3(mvert[i++].co, - ccgSubSurf_getFaceGridEdgeData(ss, f, S, x)); + 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++) { - copy_v3_v3(mvert[i++].co, - ccgSubSurf_getFaceGridData(ss, f, S, x, 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); } } } @@ -1212,8 +816,11 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) CCGEdge *e = ccgdm->edgeMap[index].edge; int x; - for(x = 1; x < edgeSize - 1; x++) { - copy_v3_v3(mvert[i++].co, ccgSubSurf_getEdgeData(ss, e, x)); + for(x = 1; x < edgeSize - 1; x++, i++) { + vd= ccgSubSurf_getEdgeData(ss, e, x); + copy_v3_v3(mvert[i].co, vd->co); + /* TODO CCGSubsurf does not set these */ + normal_float_to_short_v3(mvert[i].no, vd->no); } } @@ -1221,8 +828,9 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) for(index = 0; index < totvert; index++) { CCGVert *v = ccgdm->vertMap[index].vert; - copy_v3_v3(mvert[i].co, ccgSubSurf_getVertData(ss, v)); - + vd= ccgSubSurf_getVertData(ss, v); + copy_v3_v3(mvert[i].co, vd->co); + normal_float_to_short_v3(mvert[i].no, vd->no); i++; } } @@ -1236,7 +844,7 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge) int gridSize = ccgSubSurf_getGridSize(ss); int edgeSize = ccgSubSurf_getEdgeSize(ss); int i = 0; - int *edgeFlags = dm->getEdgeDataArray(dm, CD_FLAGS); + short *edgeFlags = ccgdm->edgeFlags; totface = ccgSubSurf_getNumFaces(ss); for(index = 0; index < totface; index++) { @@ -1291,7 +899,7 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge) if(edgeFlags) { if(edgeIdx != -1) { - flags |= (edgeFlags[i] & (ME_SEAM | ME_SHARP)) + flags |= (edgeFlags[index] & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER; } } else { @@ -1317,7 +925,7 @@ static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface) int gridSize = ccgSubSurf_getGridSize(ss); int edgeSize = ccgSubSurf_getEdgeSize(ss); int i = 0; - char *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS); + char *faceFlags = ccgdm->faceFlags; totface = ccgSubSurf_getNumFaces(ss); for(index = 0; index < totface; index++) { @@ -1339,7 +947,7 @@ static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface) mf->v4 = getFaceIndex(ss, f, S, x + 1, y + 0, edgeSize, gridSize); mf->mat_nr = mat_nr; - if(faceFlags) mf->flag = faceFlags[index*4]; + if(faceFlags) mf->flag = faceFlags[index*2]; else mf->flag = flag; i++; @@ -1438,7 +1046,7 @@ static void ccgDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) { CCGVert *v = ccgVertIterator_getCurrent(vi); - VertData *vd = ccgSubSurf_getVertData(ccgdm->ss, v); + DMGridData *vd = ccgSubSurf_getVertData(ccgdm->ss, v); int index = ccgDM_getVertMapIndex(ccgdm->ss, v); if (index!=-1) @@ -1455,7 +1063,7 @@ static void ccgDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); int index = ccgDM_getEdgeMapIndex(ss, e); if (index!=-1) { @@ -1524,7 +1132,7 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) { for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e)) continue; @@ -1552,7 +1160,7 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) { int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); for (S=0; S<numVerts; S++) { - VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); glBegin(GL_LINE_STRIP); for (x=0; x<gridSize; x++) @@ -1585,7 +1193,7 @@ static void ccgDM_drawLooseEdges(DerivedMesh *dm) { for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); if (!ccgSubSurf_getEdgeNumFaces(e)) { glBegin(GL_LINE_STRIP); @@ -1615,13 +1223,37 @@ static void ccgDM_glNormalFast(float *a, float *b, float *c, float *d) } /* Only used by non-editmesh types */ -static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) { +static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes)[4], int fast, int (*setMaterial)(int, void *attribs)) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm; CCGSubSurf *ss = ccgdm->ss; - CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss); + CCGFaceIterator *fi; int gridSize = ccgSubSurf_getGridSize(ss); - char *faceFlags = DM_get_face_data_layer(dm, CD_FLAGS); + char *faceFlags = ccgdm->faceFlags; + int step = (fast)? gridSize-1: 1; + + if(ccgdm->pbvh && ccgdm->multires.mmd && !fast) { + CCGFace **faces; + int totface; + + BLI_pbvh_get_grid_updates(ccgdm->pbvh, 1, (void***)&faces, &totface); + if(totface) { + ccgSubSurf_updateFromFaces(ss, 0, faces, totface); + ccgSubSurf_updateNormals(ss, faces, totface); + MEM_freeN(faces); + } + + /* should be per face */ + if(faceFlags && faceFlags[0] & ME_SMOOTH) + glShadeModel(GL_SMOOTH); + + BLI_pbvh_draw(ccgdm->pbvh, partial_redraw_planes, NULL); + + glShadeModel(GL_FLAT); + + return; + } + fi = ccgSubSurf_getFaceIterator(ss); for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); @@ -1629,8 +1261,8 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void * int drawSmooth, mat_nr; if(faceFlags) { - drawSmooth = (faceFlags[index*4] & ME_SMOOTH); - mat_nr= faceFlags[index*4 + 1]; + drawSmooth = (faceFlags[index*2] & ME_SMOOTH); + mat_nr= faceFlags[index*2 + 1]; } else { drawSmooth = 1; @@ -1642,14 +1274,14 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void * glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT); for (S=0; S<numVerts; S++) { - VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); if (drawSmooth) { - for (y=0; y<gridSize-1; y++) { + for (y=0; y<gridSize-1; y+=step) { glBegin(GL_QUAD_STRIP); - for (x=0; x<gridSize; x++) { - VertData *a = &faceGridData[(y+0)*gridSize + x]; - VertData *b = &faceGridData[(y+1)*gridSize + x]; + 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); @@ -1660,12 +1292,12 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void * } } else { glBegin(GL_QUADS); - for (y=0; y<gridSize-1; y++) { - for (x=0; x<gridSize-1; x++) { + 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 + 1].co; - float *c = faceGridData[(y+1)*gridSize + x + 1].co; - float *d = faceGridData[(y+1)*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); @@ -1695,7 +1327,7 @@ static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, v int gridFaces = gridSize - 1; int edgeSize = ccgSubSurf_getEdgeSize(ss); int transp, orig_transp, new_transp; - char *faceFlags = DM_get_face_data_layer(dm, CD_FLAGS); + char *faceFlags = ccgdm->faceFlags; int a, b, i, doDraw, numVerts, matnr, new_matnr, totface; doDraw = 0; @@ -1737,8 +1369,8 @@ static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, v numVerts = ccgSubSurf_getFaceNumVerts(f); if(faceFlags) { - drawSmooth = (faceFlags[index*4] & ME_SMOOTH); - new_matnr= faceFlags[index*4 + 1] + 1; + drawSmooth = (faceFlags[index*2] & ME_SMOOTH); + new_matnr= faceFlags[index*2 + 1] + 1; } else { drawSmooth = 1; @@ -1770,8 +1402,8 @@ static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, v glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT); for (S=0; S<numVerts; S++) { - VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); - VertData *vda, *vdb; + DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + DMGridData *vda, *vdb; if (drawSmooth) { for (y=0; y<gridFaces; y++) { @@ -1870,7 +1502,7 @@ static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned ch int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); for (S=0; S<numVerts; S++) { - VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); for (y=0; y<gridSize-1; y++) { for (x=0; x<gridSize-1; x++) { float *a = faceGridData[(y+0)*gridSize + x].co; @@ -1918,7 +1550,7 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, CCGSubSurf *ss = ccgdm->ss; MCol *mcol = DM_get_face_data_layer(dm, CD_MCOL); MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE); - char *faceFlags = DM_get_face_data_layer(dm, CD_FLAGS); + char *faceFlags = ccgdm->faceFlags; int i, totface, flag, gridSize = ccgSubSurf_getGridSize(ss); int gridFaces = gridSize - 1; @@ -1932,8 +1564,8 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, int mat_nr; if(faceFlags) { - drawSmooth = (faceFlags[origIndex*4] & ME_SMOOTH); - mat_nr= faceFlags[origIndex*4 + 1]; + drawSmooth = (faceFlags[origIndex*2] & ME_SMOOTH); + mat_nr= faceFlags[origIndex*2 + 1]; } else { drawSmooth = 1; @@ -1958,8 +1590,8 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, } for (S=0; S<numVerts; S++) { - VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); - VertData *a, *b; + DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + DMGridData *a, *b; if (drawSmooth) { glShadeModel(GL_SMOOTH); @@ -2089,7 +1721,7 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *u CCGSubSurf *ss = ccgdm->ss; CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss); int i, gridSize = ccgSubSurf_getGridSize(ss); - char *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS); + char *faceFlags = ccgdm->faceFlags; for (i=0; !ccgFaceIterator_isStopped(fi); i++,ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); @@ -2099,7 +1731,7 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *u origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); - if(faceFlags) drawSmooth = (faceFlags[origIndex*4] & ME_SMOOTH); + if(faceFlags) drawSmooth = (faceFlags[origIndex*2] & ME_SMOOTH); else drawSmooth = 1; if (index!=-1) { @@ -2113,14 +1745,14 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *u } for (S=0; S<numVerts; S++) { - VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); if (drawSmooth) { glShadeModel(GL_SMOOTH); for (y=0; y<gridSize-1; y++) { glBegin(GL_QUAD_STRIP); for (x=0; x<gridSize; x++) { - VertData *a = &faceGridData[(y+0)*gridSize + x]; - VertData *b = &faceGridData[(y+1)*gridSize + x]; + DMGridData *a = &faceGridData[(y+0)*gridSize + x]; + DMGridData *b = &faceGridData[(y+1)*gridSize + x]; glNormal3fv(a->no); glVertex3fv(a->co); @@ -2174,7 +1806,7 @@ static void ccgDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *u for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); int index = ccgDM_getEdgeMapIndex(ss, e); glBegin(GL_LINE_STRIP); @@ -2204,7 +1836,7 @@ static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(v for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); + DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); int index = ccgDM_getEdgeMapIndex(ss, e); glBegin(GL_LINE_STRIP); @@ -2236,7 +1868,7 @@ static void ccgDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *us if (index!=-1) { /* Face center data normal isn't updated atm. */ - VertData *vd = ccgSubSurf_getFaceGridData(ss, f, 0, 0, 0); + DMGridData *vd = ccgSubSurf_getFaceGridData(ss, f, 0, 0, 0); func(userData, index, vd->co, vd->no); } @@ -2249,6 +1881,23 @@ static void ccgDM_release(DerivedMesh *dm) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm; if (DM_release(dm)) { + /* Before freeing, need to update the displacement map */ + if(ccgdm->multires.modified) { + /* Check that mmd still exists */ + if(!ccgdm->multires.local_mmd && BLI_findindex(&ccgdm->multires.ob->modifiers, ccgdm->multires.mmd) < 0) + ccgdm->multires.mmd = NULL; + if(ccgdm->multires.mmd) + ccgdm->multires.update(dm); + } + + if(ccgdm->pbvh) BLI_pbvh_free(ccgdm->pbvh); + if(ccgdm->gridFaces) MEM_freeN(ccgdm->gridFaces); + if(ccgdm->gridData) MEM_freeN(ccgdm->gridData); + if(ccgdm->gridAdjacency) MEM_freeN(ccgdm->gridAdjacency); + if(ccgdm->gridOffset) MEM_freeN(ccgdm->gridOffset); + if(ccgdm->freeSS) ccgSubSurf_free(ccgdm->ss); + MEM_freeN(ccgdm->edgeFlags); + MEM_freeN(ccgdm->faceFlags); MEM_freeN(ccgdm->vertMap); MEM_freeN(ccgdm->edgeMap); MEM_freeN(ccgdm->faceMap); @@ -2256,6 +1905,270 @@ static void ccgDM_release(DerivedMesh *dm) { } } +static void *ccgDM_get_vert_data_layer(DerivedMesh *dm, int type) +{ + if(type == CD_ORIGINDEX) { + /* create origindex on demand to save memory */ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + CCGSubSurf *ss= ccgdm->ss; + int *origindex; + int a, index, totnone, totorig; + + DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL); + origindex= DM_get_vert_data_layer(dm, CD_ORIGINDEX); + + totorig = ccgSubSurf_getNumVerts(ss); + totnone= dm->numVertData - totorig; + + /* original vertices are at the end */ + for(a=0; a<totnone; a++) + origindex[a]= ORIGINDEX_NONE; + + for(index=0; index<totorig; index++, a++) { + CCGVert *v = ccgdm->vertMap[index].vert; + origindex[a] = ccgDM_getVertMapIndex(ccgdm->ss, v); + } + + return origindex; + } + + return DM_get_vert_data_layer(dm, type); +} + +static void *ccgDM_get_edge_data_layer(DerivedMesh *dm, int type) +{ + if(type == CD_ORIGINDEX) { + /* create origindex on demand to save memory */ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + CCGSubSurf *ss= ccgdm->ss; + int *origindex; + int a, i, index, totnone, totorig, totedge; + int edgeSize= ccgSubSurf_getEdgeSize(ss); + + DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL); + origindex= DM_get_edge_data_layer(dm, CD_ORIGINDEX); + + totedge= ccgSubSurf_getNumEdges(ss); + totorig= totedge*(edgeSize - 1); + totnone= dm->numEdgeData - totorig; + + /* original edges are at the end */ + for(a=0; a<totnone; a++) + origindex[a]= ORIGINDEX_NONE; + + for(index=0; index<totedge; index++) { + CCGEdge *e= ccgdm->edgeMap[index].edge; + int mapIndex= ccgDM_getEdgeMapIndex(ss, e); + + for(i = 0; i < edgeSize - 1; i++, a++) + origindex[a]= mapIndex; + } + + return origindex; + } + + return DM_get_edge_data_layer(dm, type); +} + +static void *ccgDM_get_face_data_layer(DerivedMesh *dm, int type) +{ + if(type == CD_ORIGINDEX) { + /* create origindex on demand to save memory */ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + CCGSubSurf *ss= ccgdm->ss; + int *origindex; + int a, i, index, totface; + int gridFaces = ccgSubSurf_getGridSize(ss) - 1; + + DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL); + origindex= DM_get_face_data_layer(dm, CD_ORIGINDEX); + + totface= ccgSubSurf_getNumFaces(ss); + + for(a=0, index=0; index<totface; index++) { + CCGFace *f = ccgdm->faceMap[index].face; + int numVerts = ccgSubSurf_getFaceNumVerts(f); + int mapIndex = ccgDM_getFaceMapIndex(ss, f); + + for(i=0; i<gridFaces*gridFaces*numVerts; i++, a++) + origindex[a]= mapIndex; + } + + return origindex; + } + + return DM_get_face_data_layer(dm, type); +} + +static int ccgDM_getNumGrids(DerivedMesh *dm) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + int index, numFaces, numGrids; + + numFaces= ccgSubSurf_getNumFaces(ccgdm->ss); + numGrids= 0; + + for(index=0; index<numFaces; index++) { + CCGFace *f = ccgdm->faceMap[index].face; + numGrids += ccgSubSurf_getFaceNumVerts(f); + } + + return numGrids; +} + +static int ccgDM_getGridSize(DerivedMesh *dm) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + return ccgSubSurf_getGridSize(ccgdm->ss); +} + +static int ccgdm_adjacent_grid(CCGSubSurf *ss, int *gridOffset, CCGFace *f, int S, int offset) +{ + CCGFace *adjf; + CCGEdge *e; + int i, j= 0, numFaces, fIndex, numEdges= 0; + + e = ccgSubSurf_getFaceEdge(ss, f, S); + numFaces = ccgSubSurf_getEdgeNumFaces(e); + + if(numFaces != 2) + return -1; + + for(i = 0; i < numFaces; i++) { + adjf = ccgSubSurf_getEdgeFace(e, i); + + if(adjf != f) { + numEdges = ccgSubSurf_getFaceNumVerts(adjf); + for(j = 0; j < numEdges; j++) + if(ccgSubSurf_getFaceEdge(ss, adjf, j) == e) + break; + + if(j != numEdges) + break; + } + } + + fIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, adjf)); + + return gridOffset[fIndex] + (j + offset)%numEdges; +} + +static void ccgdm_create_grids(DerivedMesh *dm) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + CCGSubSurf *ss= ccgdm->ss; + DMGridData **gridData; + DMGridAdjacency *gridAdjacency, *adj; + CCGFace **gridFaces; + int *gridOffset; + int index, numFaces, numGrids, S, gIndex, gridSize; + + if(ccgdm->gridData) + return; + + numGrids = ccgDM_getNumGrids(dm); + numFaces = ccgSubSurf_getNumFaces(ss); + gridSize = ccgDM_getGridSize(dm); + + /* compute offset into grid array for each face */ + gridOffset = MEM_mallocN(sizeof(int)*numFaces, "ccgdm.gridOffset"); + + for(gIndex = 0, index = 0; index < numFaces; index++) { + CCGFace *f = ccgdm->faceMap[index].face; + int numVerts = ccgSubSurf_getFaceNumVerts(f); + + gridOffset[index] = gIndex; + gIndex += numVerts; + } + + /* compute grid data */ + gridData = MEM_mallocN(sizeof(DMGridData*)*numGrids, "ccgdm.gridData"); + gridAdjacency = MEM_mallocN(sizeof(DMGridAdjacency)*numGrids, "ccgdm.gridAdjacency"); + gridFaces = MEM_mallocN(sizeof(CCGFace*)*numGrids, "ccgdm.gridFaces"); + + for(gIndex = 0, index = 0; index < numFaces; index++) { + CCGFace *f = ccgdm->faceMap[index].face; + int numVerts = ccgSubSurf_getFaceNumVerts(f); + + for(S = 0; S < numVerts; S++, gIndex++) { + int prevS = (S - 1 + numVerts) % numVerts; + int nextS = (S + 1 + numVerts) % numVerts; + + gridData[gIndex] = ccgSubSurf_getFaceGridDataArray(ss, f, S); + gridFaces[gIndex] = f; + + adj = &gridAdjacency[gIndex]; + + adj->index[0] = gIndex - S + nextS; + adj->rotation[0] = 3; + adj->index[1] = ccgdm_adjacent_grid(ss, gridOffset, f, prevS, 0); + adj->rotation[1] = 1; + adj->index[2] = ccgdm_adjacent_grid(ss, gridOffset, f, S, 1); + adj->rotation[2] = 3; + adj->index[3] = gIndex - S + prevS; + adj->rotation[3] = 1; + } + } + + ccgdm->gridData = gridData; + ccgdm->gridFaces = gridFaces; + ccgdm->gridAdjacency = gridAdjacency; + ccgdm->gridOffset = gridOffset; +} + +static DMGridData **ccgDM_getGridData(DerivedMesh *dm) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + + ccgdm_create_grids(dm); + return ccgdm->gridData; +} + +static DMGridAdjacency *ccgDM_getGridAdjacency(DerivedMesh *dm) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + + ccgdm_create_grids(dm); + return ccgdm->gridAdjacency; +} + +static int *ccgDM_getGridOffset(DerivedMesh *dm) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + + ccgdm_create_grids(dm); + return ccgdm->gridOffset; +} + +static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm) +{ + CCGDerivedMesh *ccgdm= (CCGDerivedMesh*)dm; + int gridSize, numGrids; + + if(ccgdm->pbvh) + return ccgdm->pbvh; + + if(ccgdm->multires.mmd) { + ccgdm_create_grids(dm); + + gridSize = ccgDM_getGridSize(dm); + numGrids = ccgDM_getNumGrids(dm); + + ccgdm->pbvh = BLI_pbvh_new(); + BLI_pbvh_build_grids(ccgdm->pbvh, ccgdm->gridData, ccgdm->gridAdjacency, + numGrids, gridSize, (void**)ccgdm->gridFaces); + } + else if(ob->type == OB_MESH) { + Mesh *me= ob->data; + + ccgdm->pbvh = BLI_pbvh_new(); + BLI_pbvh_build_mesh(ccgdm->pbvh, me->mface, me->mvert, + me->totface, me->totvert); + } + + return ccgdm->pbvh; +} + static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, int drawInteriorEdges, int useSubsurfUv, @@ -2268,17 +2181,14 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, int index, totvert, totedge, totface; int i; int vertNum, edgeNum, faceNum; - int *vertOrigIndex, *faceOrigIndex; /* *edgeOrigIndex - as yet, unused */ - int *edgeFlags; + short *edgeFlags; char *faceFlags; int edgeSize; int gridSize; int gridFaces; int gridSideVerts; - /*int gridInternalVerts; - as yet unused */ int gridSideEdges; int gridInternalEdges; - /* MVert *mvert = NULL; - as yet unused */ MEdge *medge = NULL; MFace *mface = NULL; FaceVertWeight *qweight, *tweight; @@ -2286,11 +2196,6 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, DM_from_template(&ccgdm->dm, dm, ccgSubSurf_getNumFinalVerts(ss), ccgSubSurf_getNumFinalEdges(ss), ccgSubSurf_getNumFinalFaces(ss)); - DM_add_face_layer(&ccgdm->dm, CD_FLAGS, CD_CALLOC, NULL); - DM_add_edge_layer(&ccgdm->dm, CD_FLAGS, CD_CALLOC, NULL); - - CustomData_set_layer_flag(&ccgdm->dm.faceData, CD_FLAGS, CD_FLAG_NOCOPY); - CustomData_set_layer_flag(&ccgdm->dm.edgeData, CD_FLAGS, CD_FLAG_NOCOPY); ccgdm->dm.getMinMax = ccgDM_getMinMax; ccgdm->dm.getNumVerts = ccgDM_getNumVerts; @@ -2306,9 +2211,15 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, ccgdm->dm.getVertData = DM_get_vert_data; ccgdm->dm.getEdgeData = DM_get_edge_data; ccgdm->dm.getFaceData = DM_get_face_data; - ccgdm->dm.getVertDataArray = DM_get_vert_data_layer; - ccgdm->dm.getEdgeDataArray = DM_get_edge_data_layer; - ccgdm->dm.getFaceDataArray = DM_get_face_data_layer; + ccgdm->dm.getVertDataArray = ccgDM_get_vert_data_layer; + ccgdm->dm.getEdgeDataArray = ccgDM_get_edge_data_layer; + ccgdm->dm.getFaceDataArray = ccgDM_get_face_data_layer; + ccgdm->dm.getNumGrids = ccgDM_getNumGrids; + ccgdm->dm.getGridSize = ccgDM_getGridSize; + ccgdm->dm.getGridData = ccgDM_getGridData; + ccgdm->dm.getGridAdjacency = ccgDM_getGridAdjacency; + ccgdm->dm.getGridOffset = ccgDM_getGridOffset; + ccgdm->dm.getPBVH = ccgDM_getPBVH; ccgdm->dm.getVertCos = ccgdm_getVertCos; ccgdm->dm.foreachMappedVert = ccgDM_foreachMappedVert; @@ -2383,17 +2294,12 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, medge = dm->getEdgeArray(dm); mface = dm->getFaceArray(dm); - vertOrigIndex = DM_get_vert_data_layer(&ccgdm->dm, CD_ORIGINDEX); - /*edgeOrigIndex = DM_get_edge_data_layer(&ccgdm->dm, CD_ORIGINDEX);*/ - faceOrigIndex = DM_get_face_data_layer(&ccgdm->dm, CD_ORIGINDEX); - - faceFlags = DM_get_face_data_layer(&ccgdm->dm, CD_FLAGS); + faceFlags = ccgdm->faceFlags = MEM_callocN(sizeof(char)*2*totface, "faceFlags"); for(index = 0; index < totface; ++index) { CCGFace *f = ccgdm->faceMap[index].face; int numVerts = ccgSubSurf_getFaceNumVerts(f); int numFinalEdges = numVerts * (gridSideEdges + gridInternalEdges); - int mapIndex = ccgDM_getFaceMapIndex(ss, f); int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight; int S, x, y; @@ -2414,8 +2320,6 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, weight[0][0], numVerts, vertNum); - *vertOrigIndex = ORIGINDEX_NONE; - ++vertOrigIndex; ++vertNum; for(S = 0; S < numVerts; S++) { @@ -2430,8 +2334,6 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, w[otherS] = weight[x][0][3]; DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w, numVerts, vertNum); - *vertOrigIndex = ORIGINDEX_NONE; - ++vertOrigIndex; ++vertNum; } } @@ -2449,17 +2351,11 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, w[otherS] = weight[y * gridFaces + x][0][3]; DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w, numVerts, vertNum); - *vertOrigIndex = ORIGINDEX_NONE; - ++vertOrigIndex; ++vertNum; } } } - for(i = 0; i < numFinalEdges; ++i) - *(int *)DM_get_edge_data(&ccgdm->dm, edgeNum + i, - CD_ORIGINDEX) = ORIGINDEX_NONE; - for(S = 0; S < numVerts; S++) { int prevS = (S - 1 + numVerts) % numVerts; int nextS = (S + 1) % numVerts; @@ -2483,16 +2379,13 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, &w, 1, faceNum); weight++; - *faceOrigIndex = mapIndex; - - ++faceOrigIndex; ++faceNum; } } } - faceFlags[index*4] = mface[origIndex].flag; - faceFlags[index*4 + 1] = mface[origIndex].mat_nr; + faceFlags[index*2] = mface[origIndex].flag; + faceFlags[index*2 + 1] = mface[origIndex].mat_nr; edgeNum += numFinalEdges; } @@ -2507,12 +2400,11 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, set_subsurf_uv(ss, dm, &ccgdm->dm, i); } - edgeFlags = DM_get_edge_data_layer(&ccgdm->dm, CD_FLAGS); + edgeFlags = ccgdm->edgeFlags = MEM_callocN(sizeof(short)*totedge, "edgeFlags"); for(index = 0; index < totedge; ++index) { CCGEdge *e = ccgdm->edgeMap[index].edge; int numFinalEdges = edgeSize - 1; - int mapIndex = ccgDM_getEdgeMapIndex(ss, e); int x; int vertIdx[2]; int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e)); @@ -2534,25 +2426,16 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, w[1] = (float) x / (edgeSize - 1); w[0] = 1 - w[1]; DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w, 2, vertNum); - *vertOrigIndex = ORIGINDEX_NONE; - ++vertOrigIndex; ++vertNum; } - for(i = 0; i < numFinalEdges; ++i) { - if(edgeIdx >= 0 && edgeFlags) - edgeFlags[edgeNum + i] = medge[edgeIdx].flag; - - *(int *)DM_get_edge_data(&ccgdm->dm, edgeNum + i, - CD_ORIGINDEX) = mapIndex; - } + edgeFlags[index]= medge[edgeIdx].flag; edgeNum += numFinalEdges; } for(index = 0; index < totvert; ++index) { CCGVert *v = ccgdm->vertMap[index].vert; - int mapIndex = ccgDM_getVertMapIndex(ccgdm->ss, v); int vertIdx; vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); @@ -2564,8 +2447,6 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, DM_copy_vert_data(dm, &ccgdm->dm, vertIdx, vertNum, 1); - *vertOrigIndex = mapIndex; - ++vertOrigIndex; ++vertNum; } @@ -2577,10 +2458,9 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, /***/ -struct DerivedMesh *subsurf_make_derived_from_derived_with_multires( +struct DerivedMesh *subsurf_make_derived_from_derived( struct DerivedMesh *dm, struct SubsurfModifierData *smd, - struct MultiresSubsurf *ms, int useRenderParams, float (*vertCos)[3], int isFinalCalc, int editMode) { @@ -2588,16 +2468,16 @@ struct DerivedMesh *subsurf_make_derived_from_derived_with_multires( int useAging = smd->flags & eSubsurfModifierFlag_DebugIncr; int useSubsurfUv = smd->flags & eSubsurfModifierFlag_SubsurfUv; int drawInteriorEdges = !(smd->flags & eSubsurfModifierFlag_ControlEdges); - DerivedMesh *result; + CCGDerivedMesh *result; if(editMode) { smd->emCache = _getSubSurf(smd->emCache, smd->levels, useAging, 0, useSimple); ss_sync_from_derivedmesh(smd->emCache, dm, vertCos, useSimple); - return (DerivedMesh *)getCCGDerivedMesh(smd->emCache, - drawInteriorEdges, - useSubsurfUv, dm); + result = getCCGDerivedMesh(smd->emCache, + drawInteriorEdges, + useSubsurfUv, dm); } else if(useRenderParams) { /* Do not use cache in render mode. */ CCGSubSurf *ss; @@ -2611,12 +2491,10 @@ struct DerivedMesh *subsurf_make_derived_from_derived_with_multires( ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple); - result = ss_to_cdderivedmesh(ss, 0, drawInteriorEdges, - useSubsurfUv, dm, ms); + result = getCCGDerivedMesh(ss, + drawInteriorEdges, useSubsurfUv, dm); - ccgSubSurf_free(ss); - - return result; + result->freeSS = 1; } else { int useIncremental = (smd->flags & eSubsurfModifierFlag_Incremental); int useAging = smd->flags & eSubsurfModifierFlag_DebugIncr; @@ -2641,13 +2519,9 @@ struct DerivedMesh *subsurf_make_derived_from_derived_with_multires( ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple); - - return ss_to_cdderivedmesh(ss, 0, drawInteriorEdges, - useSubsurfUv, dm, ms); - - /*return (DerivedMesh *)getCCGDerivedMesh(smd->mCache, - drawInteriorEdges, - useSubsurfUv, dm);*/ + result = getCCGDerivedMesh(smd->mCache, + drawInteriorEdges, + useSubsurfUv, dm); } else { if (smd->mCache && isFinalCalc) { ccgSubSurf_free(smd->mCache); @@ -2657,28 +2531,16 @@ struct DerivedMesh *subsurf_make_derived_from_derived_with_multires( ss = _getSubSurf(NULL, smd->levels, 0, 1, useSimple); ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple); - /*smd->mCache = ss; - result = (DerivedMesh *)getCCGDerivedMesh(smd->mCache, - drawInteriorEdges, - useSubsurfUv, dm);*/ - - result = ss_to_cdderivedmesh(ss, 0, drawInteriorEdges, - useSubsurfUv, dm, ms); + result = getCCGDerivedMesh(ss, drawInteriorEdges, useSubsurfUv, dm); - ccgSubSurf_free(ss); - - return result; + if(isFinalCalc) + smd->mCache = ss; + else + result->freeSS = 1; } } -} -struct DerivedMesh *subsurf_make_derived_from_derived( - struct DerivedMesh *dm, - struct SubsurfModifierData *smd, - int useRenderParams, float (*vertCos)[3], - int isFinalCalc, int editMode) -{ - return subsurf_make_derived_from_derived_with_multires(dm, smd, NULL, useRenderParams, vertCos, isFinalCalc, editMode); + return (DerivedMesh*)result; } void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]) diff --git a/source/blender/blenlib/BLI_pbvh.h b/source/blender/blenlib/BLI_pbvh.h new file mode 100644 index 00000000000..12c13de183c --- /dev/null +++ b/source/blender/blenlib/BLI_pbvh.h @@ -0,0 +1,198 @@ +/** + * A BVH for high poly meshes. + * + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef BLI_PBVH_H +#define BLI_PBVH_H + +struct MFace; +struct MVert; +struct DMGridAdjacency; +struct DMGridData; +struct PBVH; +struct PBVHNode; +struct ListBase; + +typedef struct PBVH PBVH; +typedef struct PBVHNode PBVHNode; + +/* Callbacks */ + +/* returns 1 if the search should continue from this node, 0 otherwise */ +typedef int (*BLI_pbvh_SearchCallback)(PBVHNode *node, void *data); + +typedef void (*BLI_pbvh_HitCallback)(PBVHNode *node, void *data); + +/* Building */ + +PBVH *BLI_pbvh_new(void); +void BLI_pbvh_build_mesh(PBVH *bvh, struct MFace *faces, struct MVert *verts, + int totface, int totvert); +void BLI_pbvh_build_grids(PBVH *bvh, struct DMGridData **grids, + struct DMGridAdjacency *gridadj, int totgrid, + int gridsize, void **gridfaces); +void BLI_pbvh_free(PBVH *bvh); + +/* Hierarchical Search in the BVH, two methods: + * for each hit calling a callback + * gather nodes in an array (easy to multithread) */ + +void BLI_pbvh_search_callback(PBVH *bvh, + BLI_pbvh_SearchCallback scb, void *search_data, + BLI_pbvh_HitCallback hcb, void *hit_data); + +void BLI_pbvh_search_gather(PBVH *bvh, + BLI_pbvh_SearchCallback scb, void *search_data, + PBVHNode ***array, int *tot); + +/* Raycast + the hit callback is called for all leaf nodes intersecting the ray; + it's up to the callback to find the primitive within the leaves that is + hit first */ + +void BLI_pbvh_raycast(PBVH *bvh, BLI_pbvh_HitCallback cb, void *data, + float ray_start[3], float ray_normal[3], int original); +int BLI_pbvh_node_raycast(PBVH *bvh, PBVHNode *node, float (*origco)[3], + float ray_start[3], float ray_normal[3], float *dist); + +/* Drawing */ + +void BLI_pbvh_node_draw(PBVHNode *node, void *data); +int BLI_pbvh_node_planes_contain_AABB(PBVHNode *node, void *data); +void BLI_pbvh_draw(PBVH *bvh, float (*planes)[4], float (*face_nors)[3]); + +/* Node Access */ + +typedef enum { + PBVH_Leaf = 1, + + PBVH_UpdateNormals = 2, + PBVH_UpdateBB = 4, + PBVH_UpdateOriginalBB = 8, + PBVH_UpdateDrawBuffers = 16, + PBVH_UpdateRedraw = 32 +} PBVHNodeFlags; + +void BLI_pbvh_node_mark_update(PBVHNode *node); + +void BLI_pbvh_node_get_grids(PBVH *bvh, PBVHNode *node, + int **grid_indices, int *totgrid, int *maxgrid, int *gridsize, + struct DMGridData ***griddata, struct DMGridAdjacency **gridadj); +void BLI_pbvh_node_num_verts(PBVH *bvh, PBVHNode *node, + int *uniquevert, int *totvert); + +void BLI_pbvh_node_get_BB(PBVHNode *node, float bb_min[3], float bb_max[3]); +void BLI_pbvh_node_get_original_BB(PBVHNode *node, float bb_min[3], float bb_max[3]); + +/* Update Normals/Bounding Box/Draw Buffers/Redraw and clear flags */ + +void BLI_pbvh_update(PBVH *bvh, int flags, float (*face_nors)[3]); +void BLI_pbvh_redraw_BB(PBVH *bvh, float bb_min[3], float bb_max[3]); +void BLI_pbvh_get_grid_updates(PBVH *bvh, int clear, void ***gridfaces, int *totface); + +/* Vertex Iterator */ + +/* this iterator has quite a lot of code, but it's designed to: + - allow the compiler to eliminate dead code and variables + - spend most of the time in the relatively simple inner loop */ + +#define PBVH_ITER_ALL 0 +#define PBVH_ITER_UNIQUE 1 + +typedef struct PBVHVertexIter { + /* iteration */ + int g; + int width; + int height; + int skip; + int gx; + int gy; + int i; + + /* grid */ + struct DMGridData **grids; + struct DMGridData *grid; + int *grid_indices; + int totgrid; + int gridsize; + + /* mesh */ + struct MVert *mverts; + int totvert; + int *vert_indices; + + /* result: these are all computed in the macro, but we assume + that compiler optimizations will skip the ones we don't use */ + struct MVert *mvert; + float *co; + short *no; + float *fno; +} PBVHVertexIter; + +void BLI_pbvh_node_verts_iter_init(PBVH *bvh, PBVHNode *node, PBVHVertexIter *vi, int mode); + +#define BLI_pbvh_vertex_iter_begin(bvh, node, vi, mode) \ + /* XXX breaks aliasing! */ \ + BLI_pbvh_node_verts_iter_init(bvh, node, &vi, mode); \ + \ + for(vi.i=0, vi.g=0; vi.g<vi.totgrid; vi.g++) { \ + if(vi.grids) { \ + vi.width= vi.gridsize; \ + vi.height= vi.gridsize; \ + vi.grid= vi.grids[vi.grid_indices[vi.g]]; \ + vi.skip= 0; \ + \ + /*if(mode == PVBH_ITER_UNIQUE) { \ + vi.grid += subm->grid.offset; \ + vi.skip= subm->grid.skip; \ + vi.grid -= skip; \ + }*/ \ + } \ + else { \ + vi.width= vi.totvert; \ + vi.height= 1; \ + } \ + \ + for(vi.gy=0; vi.gy<vi.height; vi.gy++) { \ + if(vi.grid) vi.grid += vi.skip; \ + \ + for(vi.gx=0; vi.gx<vi.width; vi.gx++, vi.i++) { \ + if(vi.grid) { \ + vi.co= vi.grid->co; \ + vi.fno= vi.grid->no; \ + vi.grid++; \ + } \ + else { \ + vi.mvert= &vi.mverts[vi.vert_indices[vi.gx]]; \ + vi.co= vi.mvert->co; \ + vi.no= vi.mvert->no; \ + } \ + +#define BLI_pbvh_vertex_iter_end \ + } \ + } \ + } + + +#endif /* BLI_PBVH_H */ + diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 4ed9eb4b007..00a5440bc75 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -28,6 +28,7 @@ FILE(GLOB SRC intern/*.c) SET(INC . ../makesdna ../blenkernel ../../../intern/guardedalloc ../include + ../gpu ${FREETYPE_INCLUDE_DIRS} ${ZLIB_INC} ) diff --git a/source/blender/blenlib/SConscript b/source/blender/blenlib/SConscript index fc586de5085..bca9399bc27 100644 --- a/source/blender/blenlib/SConscript +++ b/source/blender/blenlib/SConscript @@ -4,7 +4,7 @@ Import ('env') sources = env.Glob('intern/*.c') cflags='' -incs = '. ../makesdna ../blenkernel #/intern/guardedalloc ../editors/include' +incs = '. ../makesdna ../blenkernel #/intern/guardedalloc ../editors/include ../gpu' incs += ' ' + env['BF_FREETYPE_INC'] incs += ' ' + env['BF_ZLIB_INC'] defs = '' diff --git a/source/blender/blenlib/intern/pbvh.c b/source/blender/blenlib/intern/pbvh.c new file mode 100644 index 00000000000..f0464438b68 --- /dev/null +++ b/source/blender/blenlib/intern/pbvh.c @@ -0,0 +1,1317 @@ +/** + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include <float.h> +#include <stdlib.h> +#include <string.h> + +#include "MEM_guardedalloc.h" + +#include "DNA_meshdata_types.h" + +#include "BLI_math.h" +#include "BLI_ghash.h" +#include "BLI_pbvh.h" + +#include "BKE_DerivedMesh.h" +#include "BKE_mesh.h" +#include "BKE_utildefines.h" + +#include "gpu_buffers.h" + +#define LEAF_LIMIT 10000 + +//#define PERFCNTRS + +/* Bitmap */ +typedef char* BLI_bitmap; + +BLI_bitmap BLI_bitmap_new(int tot) +{ + return MEM_callocN((tot >> 3) + 1, "BLI bitmap"); +} + +int BLI_bitmap_get(BLI_bitmap b, int index) +{ + return b[index >> 3] & (1 << (index & 7)); +} + +void BLI_bitmap_set(BLI_bitmap b, int index) +{ + b[index >> 3] |= (1 << (index & 7)); +} + +void BLI_bitmap_clear(BLI_bitmap b, int index) +{ + b[index >> 3] &= ~(1 << (index & 7)); +} + +/* Axis-aligned bounding box */ +typedef struct { + float bmin[3], bmax[3]; +} BB; + +/* Axis-aligned bounding box with centroid */ +typedef struct { + float bmin[3], bmax[3], bcentroid[3]; +} BBC; + +struct PBVHNode { + /* Opaque handle for drawing code */ + void *draw_buffers; + + int *vert_indices; + + /* Voxel bounds */ + BB vb; + BB orig_vb; + + /* For internal nodes */ + int children_offset; + + /* Pointer into bvh prim_indices */ + int *prim_indices; + int *face_vert_indices; + + unsigned int totprim; + unsigned int uniq_verts, face_verts; + + char flag; +}; + +struct PBVH { + PBVHNode *nodes; + int node_mem_count, totnode; + + int *prim_indices; + int totprim; + int totvert; + + int leaf_limit; + + /* Mesh data */ + MVert *verts; + MFace *faces; + + /* Grid Data */ + DMGridData **grids; + DMGridAdjacency *gridadj; + void **gridfaces; + int totgrid; + int gridsize; + + /* Only used during BVH build and update, + don't need to remain valid after */ + BLI_bitmap vert_bitmap; + +#ifdef PERFCNTRS + int perf_modified; +#endif +}; + +#define STACK_FIXED_DEPTH 100 + +typedef struct PBVHStack { + PBVHNode *node; + int revisiting; +} PBVHStack; + +typedef struct PBVHIter { + PBVH *bvh; + BLI_pbvh_SearchCallback scb; + void *search_data; + + PBVHStack *stack; + int stacksize; + + PBVHStack stackfixed[STACK_FIXED_DEPTH]; + int stackspace; +} PBVHIter; + +static void BB_reset(BB *bb) +{ + bb->bmin[0] = bb->bmin[1] = bb->bmin[2] = FLT_MAX; + bb->bmax[0] = bb->bmax[1] = bb->bmax[2] = -FLT_MAX; +} + +/* Expand the bounding box to include a new coordinate */ +static void BB_expand(BB *bb, float co[3]) +{ + int i; + for(i = 0; i < 3; ++i) { + bb->bmin[i] = MIN2(bb->bmin[i], co[i]); + bb->bmax[i] = MAX2(bb->bmax[i], co[i]); + } +} + +/* Expand the bounding box to include another bounding box */ +static void BB_expand_with_bb(BB *bb, BB *bb2) +{ + int i; + for(i = 0; i < 3; ++i) { + bb->bmin[i] = MIN2(bb->bmin[i], bb2->bmin[i]); + bb->bmax[i] = MAX2(bb->bmax[i], bb2->bmax[i]); + } +} + +/* Return 0, 1, or 2 to indicate the widest axis of the bounding box */ +static int BB_widest_axis(BB *bb) +{ + float dim[3]; + int i; + + for(i = 0; i < 3; ++i) + dim[i] = bb->bmax[i] - bb->bmin[i]; + + if(dim[0] > dim[1]) { + if(dim[0] > dim[2]) + return 0; + else + return 2; + } + else { + if(dim[1] > dim[2]) + return 1; + else + return 2; + } +} + +static void BBC_update_centroid(BBC *bbc) +{ + int i; + for(i = 0; i < 3; ++i) + bbc->bcentroid[i] = (bbc->bmin[i] + bbc->bmax[i]) * 0.5f; +} + +/* Not recursive */ +static void update_node_vb(PBVH *bvh, PBVHNode *node) +{ + BB vb; + + BB_reset(&vb); + + if(node->flag & PBVH_Leaf) { + PBVHVertexIter vd; + + BLI_pbvh_vertex_iter_begin(bvh, node, vd, PBVH_ITER_ALL) { + BB_expand(&vb, vd.co); + } + BLI_pbvh_vertex_iter_end; + } + else { + BB_expand_with_bb(&vb, + &bvh->nodes[node->children_offset].vb); + BB_expand_with_bb(&vb, + &bvh->nodes[node->children_offset + 1].vb); + } + + node->vb= vb; +} + +/* Adapted from BLI_kdopbvh.c */ +/* Returns the index of the first element on the right of the partition */ +static int partition_indices(int *prim_indices, int lo, int hi, int axis, + float mid, BBC *prim_bbc) +{ + int i=lo, j=hi; + for(;;) { + for(; prim_bbc[prim_indices[i]].bcentroid[axis] < mid; i++); + for(; mid < prim_bbc[prim_indices[j]].bcentroid[axis]; j--); + + if(!(i < j)) + return i; + + SWAP(int, prim_indices[i], prim_indices[j]); + i++; + } +} + +void check_partitioning(int *prim_indices, int lo, int hi, int axis, + float mid, BBC *prim_bbc, int index_of_2nd_partition) +{ + int i; + for(i = lo; i <= hi; ++i) { + const float c = prim_bbc[prim_indices[i]].bcentroid[axis]; + + if((i < index_of_2nd_partition && c > mid) || + (i > index_of_2nd_partition && c < mid)) { + printf("fail\n"); + } + } +} + +static void grow_nodes(PBVH *bvh, int totnode) +{ + if(totnode > bvh->node_mem_count) { + PBVHNode *prev = bvh->nodes; + bvh->node_mem_count *= 1.33; + if(bvh->node_mem_count < totnode) + bvh->node_mem_count = totnode; + bvh->nodes = MEM_callocN(sizeof(PBVHNode) * bvh->node_mem_count, + "bvh nodes"); + memcpy(bvh->nodes, prev, bvh->totnode * sizeof(PBVHNode)); + MEM_freeN(prev); + } + + bvh->totnode = totnode; +} + +/* Add a vertex to the map, with a positive value for unique vertices and + a negative value for additional vertices */ +static int map_insert_vert(PBVH *bvh, GHash *map, + unsigned int *face_verts, + unsigned int *uniq_verts, int vertex) +{ + void *value, *key = SET_INT_IN_POINTER(vertex); + + if(!BLI_ghash_haskey(map, key)) { + if(BLI_bitmap_get(bvh->vert_bitmap, vertex)) { + value = SET_INT_IN_POINTER(-(*face_verts) - 1); + ++(*face_verts); + } + else { + BLI_bitmap_set(bvh->vert_bitmap, vertex); + value = SET_INT_IN_POINTER(*uniq_verts); + ++(*uniq_verts); + } + + BLI_ghash_insert(map, key, value); + return GET_INT_FROM_POINTER(value); + } + else + return GET_INT_FROM_POINTER(BLI_ghash_lookup(map, key)); +} + +/* Find vertices used by the faces in this node and update the draw buffers */ +static void build_mesh_leaf_node(PBVH *bvh, PBVHNode *node) +{ + GHashIterator *iter; + GHash *map; + int i, j, totface; + + map = BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp); + + node->uniq_verts = node->face_verts = 0; + totface= node->totprim; + + node->face_vert_indices = MEM_callocN(sizeof(int) * + 4*totface, "bvh node face vert indices"); + + for(i = 0; i < totface; ++i) { + MFace *f = bvh->faces + node->prim_indices[i]; + int sides = f->v4 ? 4 : 3; + + for(j = 0; j < sides; ++j) { + node->face_vert_indices[i*4 + j]= + map_insert_vert(bvh, map, &node->face_verts, + &node->uniq_verts, (&f->v1)[j]); + } + } + + node->vert_indices = MEM_callocN(sizeof(int) * + (node->uniq_verts + node->face_verts), + "bvh node vert indices"); + + /* Build the vertex list, unique verts first */ + for(iter = BLI_ghashIterator_new(map), i = 0; + !BLI_ghashIterator_isDone(iter); + BLI_ghashIterator_step(iter), ++i) { + void *value = BLI_ghashIterator_getValue(iter); + int ndx = GET_INT_FROM_POINTER(value); + + if(ndx < 0) + ndx = -ndx + node->uniq_verts - 1; + + node->vert_indices[ndx] = + GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(iter)); + } + + for(i = 0; i < totface*4; ++i) + if(node->face_vert_indices[i] < 0) + node->face_vert_indices[i]= -node->face_vert_indices[i] + node->uniq_verts - 1; + + node->draw_buffers = + GPU_build_mesh_buffers(map, bvh->verts, bvh->faces, + node->prim_indices, + node->totprim, node->vert_indices, + node->uniq_verts, + node->uniq_verts + node->face_verts); + + BLI_ghash_free(map, NULL, NULL); +} + +static void build_grids_leaf_node(PBVH *bvh, PBVHNode *node) +{ + node->draw_buffers = + GPU_build_grid_buffers(bvh->grids, node->prim_indices, + node->totprim, bvh->gridsize); +} + +/* Recursively build a node in the tree + + vb is the voxel box around all of the primitives contained in + this node. + + cb is the bounding box around all the centroids of the primitives + contained in this node + + offset and start indicate a range in the array of primitive indices +*/ + +void build_sub(PBVH *bvh, int node_index, BB *cb, BBC *prim_bbc, + int offset, int count) +{ + int i, axis, end; + BB cb_backing; + + /* Decide whether this is a leaf or not */ + // XXX adapt leaf limit for grids + if(count <= bvh->leaf_limit) { + bvh->nodes[node_index].flag |= PBVH_Leaf; + + bvh->nodes[node_index].prim_indices = bvh->prim_indices + offset; + bvh->nodes[node_index].totprim = count; + + /* Still need vb for searches */ + BB_reset(&bvh->nodes[node_index].vb); + for(i = offset + count - 1; i >= offset; --i) { + BB_expand_with_bb(&bvh->nodes[node_index].vb, + (BB*)(prim_bbc + + bvh->prim_indices[i])); + } + + if(bvh->faces) + build_mesh_leaf_node(bvh, bvh->nodes + node_index); + else + build_grids_leaf_node(bvh, bvh->nodes + node_index); + bvh->nodes[node_index].orig_vb= bvh->nodes[node_index].vb; + + /* Done with this subtree */ + return; + } + else { + BB_reset(&bvh->nodes[node_index].vb); + bvh->nodes[node_index].children_offset = bvh->totnode; + grow_nodes(bvh, bvh->totnode + 2); + + if(!cb) { + cb = &cb_backing; + BB_reset(cb); + for(i = offset + count - 1; i >= offset; --i) + BB_expand(cb, prim_bbc[bvh->prim_indices[i]].bcentroid); + } + } + + axis = BB_widest_axis(cb); + + for(i = offset + count - 1; i >= offset; --i) { + BB_expand_with_bb(&bvh->nodes[node_index].vb, + (BB*)(prim_bbc + bvh->prim_indices[i])); + } + + bvh->nodes[node_index].orig_vb= bvh->nodes[node_index].vb; + + end = partition_indices(bvh->prim_indices, offset, offset + count - 1, + axis, + (cb->bmax[axis] + cb->bmin[axis]) * 0.5f, + prim_bbc); + check_partitioning(bvh->prim_indices, offset, offset + count - 1, + axis, + (cb->bmax[axis] + cb->bmin[axis]) * 0.5f, + prim_bbc, end); + + build_sub(bvh, bvh->nodes[node_index].children_offset, NULL, + prim_bbc, offset, end - offset); + build_sub(bvh, bvh->nodes[node_index].children_offset + 1, NULL, + prim_bbc, end, offset + count - end); +} + +static void pbvh_build(PBVH *bvh, BB *cb, BBC *prim_bbc, int totprim) +{ + int i; + + if(totprim != bvh->totprim) { + bvh->totprim = totprim; + if(bvh->nodes) MEM_freeN(bvh->nodes); + if(bvh->prim_indices) MEM_freeN(bvh->prim_indices); + bvh->prim_indices = MEM_callocN(sizeof(int) * totprim, + "bvh prim indices"); + for(i = 0; i < totprim; ++i) + bvh->prim_indices[i] = i; + bvh->totnode = 0; + if(bvh->node_mem_count < 100) { + bvh->node_mem_count = 100; + bvh->nodes = MEM_callocN(sizeof(PBVHNode) * + bvh->node_mem_count, + "bvh initial nodes"); + } + } + + bvh->totnode = 1; + build_sub(bvh, 0, cb, prim_bbc, 0, totprim); +} + +/* Do a full rebuild with on Mesh data structure */ +void BLI_pbvh_build_mesh(PBVH *bvh, MFace *faces, MVert *verts, int totface, int totvert) +{ + BBC *prim_bbc = NULL; + BB cb; + int i, j; + + bvh->faces = faces; + bvh->verts = verts; + bvh->vert_bitmap = BLI_bitmap_new(totvert); + bvh->totvert = totvert; + bvh->leaf_limit = LEAF_LIMIT; + + BB_reset(&cb); + + /* For each face, store the AABB and the AABB centroid */ + prim_bbc = MEM_mallocN(sizeof(BBC) * totface, "prim_bbc"); + + for(i = 0; i < totface; ++i) { + MFace *f = faces + i; + const int sides = f->v4 ? 4 : 3; + BBC *bbc = prim_bbc + i; + + BB_reset((BB*)bbc); + + for(j = 0; j < sides; ++j) + BB_expand((BB*)bbc, verts[(&f->v1)[j]].co); + + BBC_update_centroid(bbc); + + BB_expand(&cb, bbc->bcentroid); + } + + pbvh_build(bvh, &cb, prim_bbc, totface); + + MEM_freeN(prim_bbc); + MEM_freeN(bvh->vert_bitmap); +} + +/* Do a full rebuild with on Grids data structure */ +void BLI_pbvh_build_grids(PBVH *bvh, DMGridData **grids, DMGridAdjacency *gridadj, + int totgrid, int gridsize, void **gridfaces) +{ + BBC *prim_bbc = NULL; + BB cb; + int i, j; + + bvh->grids= grids; + bvh->gridadj= gridadj; + bvh->gridfaces= gridfaces; + bvh->totgrid= totgrid; + bvh->gridsize= gridsize; + bvh->leaf_limit = MAX2(LEAF_LIMIT/((gridsize-1)*(gridsize-1)), 1); + + BB_reset(&cb); + + /* For each grid, store the AABB and the AABB centroid */ + prim_bbc = MEM_mallocN(sizeof(BBC) * totgrid, "prim_bbc"); + + for(i = 0; i < totgrid; ++i) { + DMGridData *grid= grids[i]; + BBC *bbc = prim_bbc + i; + + BB_reset((BB*)bbc); + + for(j = 0; j < gridsize*gridsize; ++j) + BB_expand((BB*)bbc, grid[j].co); + + BBC_update_centroid(bbc); + + BB_expand(&cb, bbc->bcentroid); + } + + pbvh_build(bvh, &cb, prim_bbc, totgrid); + + MEM_freeN(prim_bbc); +} + +PBVH *BLI_pbvh_new(void) +{ + PBVH *bvh = MEM_callocN(sizeof(PBVH), "pbvh"); + + return bvh; +} + +void BLI_pbvh_free(PBVH *bvh) +{ + PBVHNode *node; + int i; + + for(i = 0; i < bvh->totnode; ++i) { + node= &bvh->nodes[i]; + + if(node->flag & PBVH_Leaf) { + if(node->draw_buffers) + GPU_free_buffers(node->draw_buffers); + if(node->vert_indices) + MEM_freeN(node->vert_indices); + if(node->face_vert_indices) + MEM_freeN(node->face_vert_indices); + } + } + + MEM_freeN(bvh->nodes); + MEM_freeN(bvh->prim_indices); + MEM_freeN(bvh); +} + +static void pbvh_iter_begin(PBVHIter *iter, PBVH *bvh, BLI_pbvh_SearchCallback scb, void *search_data) +{ + iter->bvh= bvh; + iter->scb= scb; + iter->search_data= search_data; + + iter->stack= iter->stackfixed; + iter->stackspace= STACK_FIXED_DEPTH; + + iter->stack[0].node= bvh->nodes; + iter->stack[0].revisiting= 0; + iter->stacksize= 1; +} + +static void pbvh_iter_end(PBVHIter *iter) +{ + if(iter->stackspace > STACK_FIXED_DEPTH) + MEM_freeN(iter->stack); +} + +static void pbvh_stack_push(PBVHIter *iter, PBVHNode *node, int revisiting) +{ + if(iter->stacksize == iter->stackspace) { + PBVHStack *newstack; + + iter->stackspace *= 2; + newstack= MEM_callocN(sizeof(PBVHStack)*iter->stackspace, "PBVHStack"); + memcpy(newstack, iter->stack, sizeof(PBVHStack)*iter->stacksize); + + if(iter->stackspace > STACK_FIXED_DEPTH) + MEM_freeN(iter->stack); + iter->stack= newstack; + } + + iter->stack[iter->stacksize].node= node; + iter->stack[iter->stacksize].revisiting= revisiting; + iter->stacksize++; +} + +static PBVHNode *pbvh_iter_next(PBVHIter *iter) +{ + PBVHNode *node; + int revisiting; + void *search_data; + + /* purpose here is to traverse tree, visiting child nodes before their + parents, this order is necessary for e.g. computing bounding boxes */ + + while(iter->stacksize) { + /* pop node */ + iter->stacksize--; + node= iter->stack[iter->stacksize].node; + revisiting= iter->stack[iter->stacksize].revisiting; + + /* revisiting node already checked */ + if(revisiting) + return node; + + /* check search callback */ + search_data= iter->search_data; + + if(iter->scb && !iter->scb(node, search_data)) + continue; /* don't traverse, outside of search zone */ + + if(node->flag & PBVH_Leaf) { + /* immediately hit leaf node */ + return node; + } + else { + /* come back later when children are done */ + pbvh_stack_push(iter, node, 1); + + /* push two child nodes on the stack */ + pbvh_stack_push(iter, iter->bvh->nodes+node->children_offset+1, 0); + pbvh_stack_push(iter, iter->bvh->nodes+node->children_offset, 0); + } + } + + return NULL; +} + +void BLI_pbvh_search_gather(PBVH *bvh, + BLI_pbvh_SearchCallback scb, void *search_data, + PBVHNode ***r_array, int *r_tot) +{ + PBVHIter iter; + PBVHNode **array= NULL, **newarray, *node; + int tot= 0, space= 0; + + pbvh_iter_begin(&iter, bvh, scb, search_data); + + while((node=pbvh_iter_next(&iter))) { + if(node->flag & PBVH_Leaf) { + if(tot == space) { + /* resize array if needed */ + space= (tot == 0)? 32: space*2; + newarray= MEM_callocN(sizeof(PBVHNode)*space, "PBVHNodeSearch"); + + if(array) { + memcpy(newarray, array, sizeof(PBVHNode)*tot); + MEM_freeN(array); + } + + array= newarray; + } + + array[tot]= node; + tot++; + } + } + + pbvh_iter_end(&iter); + + *r_array= array; + *r_tot= tot; +} + +void BLI_pbvh_search_callback(PBVH *bvh, + BLI_pbvh_SearchCallback scb, void *search_data, + BLI_pbvh_HitCallback hcb, void *hit_data) +{ + PBVHIter iter; + PBVHNode *node; + + pbvh_iter_begin(&iter, bvh, scb, search_data); + + while((node=pbvh_iter_next(&iter))) + if(node->flag & PBVH_Leaf) + hcb(node, hit_data); + + pbvh_iter_end(&iter); +} + +static int update_search_cb(PBVHNode *node, void *data_v) +{ + int flag= GET_INT_FROM_POINTER(data_v); + + if(node->flag & PBVH_Leaf) + return (node->flag & flag); + + return 1; +} + +static void pbvh_update_normals(PBVH *bvh, PBVHNode **nodes, + int totnode, float (*face_nors)[3]) +{ + float (*vnor)[3]; + int n; + + if(bvh->grids) + return; + + /* could be per node to save some memory, but also means + we have to store for each vertex which node it is in */ + vnor= MEM_callocN(sizeof(float)*3*bvh->totvert, "bvh temp vnors"); + + /* subtle assumptions: + - We know that for all edited vertices, the nodes with faces + adjacent to these vertices have been marked with PBVH_UpdateNormals. + This is true because if the vertex is inside the brush radius, the + bounding box of it's adjacent faces will be as well. + - However this is only true for the vertices that have actually been + edited, not for all vertices in the nodes marked for update, so we + can only update vertices marked with ME_VERT_PBVH_UPDATE. + */ + + #pragma omp parallel for private(n) schedule(static) + for(n = 0; n < totnode; n++) { + PBVHNode *node= nodes[n]; + + if((node->flag & PBVH_UpdateNormals)) { + int i, j, totface, *faces; + + faces= node->prim_indices; + totface= node->totprim; + + for(i = 0; i < totface; ++i) { + MFace *f= bvh->faces + faces[i]; + float fn[3]; + unsigned int *fv = &f->v1; + int sides= (f->v4)? 4: 3; + + if(f->v4) + normal_quad_v3(fn, bvh->verts[f->v1].co, bvh->verts[f->v2].co, + bvh->verts[f->v3].co, bvh->verts[f->v4].co); + else + normal_tri_v3(fn, bvh->verts[f->v1].co, bvh->verts[f->v2].co, + bvh->verts[f->v3].co); + + for(j = 0; j < sides; ++j) { + int v= fv[j]; + + if(bvh->verts[v].flag & ME_VERT_PBVH_UPDATE) { + /* this seems like it could be very slow but profile + does not show this, so just leave it for now? */ + #pragma omp atomic + vnor[v][0] += fn[0]; + #pragma omp atomic + vnor[v][1] += fn[1]; + #pragma omp atomic + vnor[v][2] += fn[2]; + } + } + + if(face_nors) + copy_v3_v3(face_nors[faces[i]], fn); + } + } + } + + #pragma omp parallel for private(n) schedule(static) + for(n = 0; n < totnode; n++) { + PBVHNode *node= nodes[n]; + + if(node->flag & PBVH_UpdateNormals) { + int i, *verts, totvert; + + verts= node->vert_indices; + totvert= node->uniq_verts; + + for(i = 0; i < totvert; ++i) { + const int v = verts[i]; + MVert *mvert= &bvh->verts[v]; + + if(mvert->flag & ME_VERT_PBVH_UPDATE) { + float no[3]; + + copy_v3_v3(no, vnor[v]); + normalize_v3(no); + + mvert->no[0] = (short)(no[0]*32767.0f); + mvert->no[1] = (short)(no[1]*32767.0f); + mvert->no[2] = (short)(no[2]*32767.0f); + + mvert->flag &= ~ME_VERT_PBVH_UPDATE; + } + } + + node->flag &= ~PBVH_UpdateNormals; + } + } + + MEM_freeN(vnor); +} + +static void pbvh_update_BB_redraw(PBVH *bvh, PBVHNode **nodes, + int totnode, int flag) +{ + int n; + + /* update BB, redraw flag */ + #pragma omp parallel for private(n) schedule(static) + for(n = 0; n < totnode; n++) { + PBVHNode *node= nodes[n]; + + if((flag & PBVH_UpdateBB) && (node->flag & PBVH_UpdateBB)) + /* don't clear flag yet, leave it for flushing later */ + update_node_vb(bvh, node); + + if((flag & PBVH_UpdateOriginalBB) && (node->flag & PBVH_UpdateOriginalBB)) + node->orig_vb= node->vb; + + if((flag & PBVH_UpdateRedraw) && (node->flag & PBVH_UpdateRedraw)) + node->flag &= ~PBVH_UpdateRedraw; + } +} + +static void pbvh_update_draw_buffers(PBVH *bvh, PBVHNode **nodes, int totnode) +{ + PBVHNode *node; + int n; + + /* can't be done in parallel with OpenGL */ + for(n = 0; n < totnode; n++) { + node= nodes[n]; + + if(node->flag & PBVH_UpdateDrawBuffers) { + if(bvh->grids) { + GPU_update_grid_buffers(node->draw_buffers, + bvh->grids, + node->prim_indices, + node->totprim, + bvh->gridsize); + } + else { + GPU_update_mesh_buffers(node->draw_buffers, + bvh->verts, + node->vert_indices, + node->uniq_verts + + node->face_verts); + } + + node->flag &= ~PBVH_UpdateDrawBuffers; + } + } +} + +static int pbvh_flush_bb(PBVH *bvh, PBVHNode *node, int flag) +{ + int update= 0; + + /* difficult to multithread well, we just do single threaded recursive */ + if(node->flag & PBVH_Leaf) { + if(flag & PBVH_UpdateBB) { + update |= (node->flag & PBVH_UpdateBB); + node->flag &= ~PBVH_UpdateBB; + } + + if(flag & PBVH_UpdateOriginalBB) { + update |= (node->flag & PBVH_UpdateOriginalBB); + node->flag &= ~PBVH_UpdateOriginalBB; + } + + return update; + } + else { + update |= pbvh_flush_bb(bvh, bvh->nodes + node->children_offset, flag); + update |= pbvh_flush_bb(bvh, bvh->nodes + node->children_offset + 1, flag); + + if(update & PBVH_UpdateBB) + update_node_vb(bvh, node); + if(update & PBVH_UpdateOriginalBB) + node->orig_vb= node->vb; + } + + return update; +} + +void BLI_pbvh_update(PBVH *bvh, int flag, float (*face_nors)[3]) +{ + PBVHNode **nodes; + int totnode; + + BLI_pbvh_search_gather(bvh, update_search_cb, SET_INT_IN_POINTER(flag), + &nodes, &totnode); + + if(flag & PBVH_UpdateNormals) + pbvh_update_normals(bvh, nodes, totnode, face_nors); + + if(flag & (PBVH_UpdateBB|PBVH_UpdateOriginalBB|PBVH_UpdateRedraw)) + pbvh_update_BB_redraw(bvh, nodes, totnode, flag); + + if(flag & PBVH_UpdateDrawBuffers) + pbvh_update_draw_buffers(bvh, nodes, totnode); + + if(flag & (PBVH_UpdateBB|PBVH_UpdateOriginalBB)) + pbvh_flush_bb(bvh, bvh->nodes, flag); + + if(nodes) MEM_freeN(nodes); +} + +void BLI_pbvh_redraw_BB(PBVH *bvh, float bb_min[3], float bb_max[3]) +{ + PBVHIter iter; + PBVHNode *node; + BB bb; + + BB_reset(&bb); + + pbvh_iter_begin(&iter, bvh, NULL, NULL); + + while((node=pbvh_iter_next(&iter))) + if(node->flag & PBVH_UpdateRedraw) + BB_expand_with_bb(&bb, &node->vb); + + pbvh_iter_end(&iter); + + copy_v3_v3(bb_min, bb.bmin); + copy_v3_v3(bb_max, bb.bmax); +} + +void BLI_pbvh_get_grid_updates(PBVH *bvh, int clear, void ***gridfaces, int *totface) +{ + PBVHIter iter; + PBVHNode *node; + GHashIterator *hiter; + GHash *map; + void *face, **faces; + int i, tot; + + map = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp); + + pbvh_iter_begin(&iter, bvh, NULL, NULL); + + while((node=pbvh_iter_next(&iter))) { + if(node->flag & PBVH_UpdateNormals) { + for(i = 0; i < node->totprim; ++i) { + face= bvh->gridfaces[node->prim_indices[i]]; + if(!BLI_ghash_lookup(map, face)) + BLI_ghash_insert(map, face, face); + } + + if(clear) + node->flag &= ~PBVH_UpdateNormals; + } + } + + pbvh_iter_end(&iter); + + tot= BLI_ghash_size(map); + if(tot == 0) { + *totface= 0; + *gridfaces= NULL; + BLI_ghash_free(map, NULL, NULL); + return; + } + + faces= MEM_callocN(sizeof(void*)*tot, "PBVH Grid Faces"); + + for(hiter = BLI_ghashIterator_new(map), i = 0; + !BLI_ghashIterator_isDone(hiter); + BLI_ghashIterator_step(hiter), ++i) + faces[i]= BLI_ghashIterator_getKey(hiter); + + BLI_ghash_free(map, NULL, NULL); + + *totface= tot; + *gridfaces= faces; +} + +/***************************** Node Access ***********************************/ + +void BLI_pbvh_node_mark_update(PBVHNode *node) +{ + node->flag |= PBVH_UpdateNormals|PBVH_UpdateBB|PBVH_UpdateOriginalBB|PBVH_UpdateDrawBuffers|PBVH_UpdateRedraw; +} + +void BLI_pbvh_node_get_verts(PBVHNode *node, int **vert_indices, int *totvert, int *allvert) +{ + if(vert_indices) *vert_indices= node->vert_indices; + if(totvert) *totvert= node->uniq_verts; + if(allvert) *allvert= node->uniq_verts + node->face_verts; +} + +void BLI_pbvh_node_num_verts(PBVH *bvh, PBVHNode *node, int *uniquevert, int *totvert) +{ + if(bvh->grids) { + if(totvert) *totvert= node->totprim*bvh->gridsize*bvh->gridsize; + if(uniquevert) *uniquevert= *totvert; + } + else { + if(totvert) *totvert= node->uniq_verts + node->face_verts; + if(uniquevert) *uniquevert= node->uniq_verts; + } +} + +void BLI_pbvh_node_get_grids(PBVH *bvh, PBVHNode *node, int **grid_indices, int *totgrid, int *maxgrid, int *gridsize, DMGridData ***griddata, DMGridAdjacency **gridadj) +{ + if(bvh->grids) { + if(grid_indices) *grid_indices= node->prim_indices; + if(totgrid) *totgrid= node->totprim; + if(maxgrid) *maxgrid= bvh->totgrid; + if(gridsize) *gridsize= bvh->gridsize; + if(griddata) *griddata= bvh->grids; + if(gridadj) *gridadj= bvh->gridadj; + } + else { + if(grid_indices) *grid_indices= NULL; + if(totgrid) *totgrid= 0; + if(maxgrid) *maxgrid= 0; + if(gridsize) *gridsize= 0; + if(griddata) *griddata= NULL; + if(gridadj) *gridadj= NULL; + } +} + +void BLI_pbvh_node_get_BB(PBVHNode *node, float bb_min[3], float bb_max[3]) +{ + copy_v3_v3(bb_min, node->vb.bmin); + copy_v3_v3(bb_max, node->vb.bmax); +} + +void BLI_pbvh_node_get_original_BB(PBVHNode *node, float bb_min[3], float bb_max[3]) +{ + copy_v3_v3(bb_min, node->orig_vb.bmin); + copy_v3_v3(bb_max, node->orig_vb.bmax); +} + +/********************************* Raycast ***********************************/ + +typedef struct { + /* Ray */ + float start[3]; + int sign[3]; + float inv_dir[3]; + int original; +} RaycastData; + +/* Adapted from here: http://www.gamedev.net/community/forums/topic.asp?topic_id=459973 */ +static int ray_aabb_intersect(PBVHNode *node, void *data_v) +{ + RaycastData *ray = data_v; + float bb_min[3], bb_max[3], bbox[2][3]; + float tmin, tmax, tymin, tymax, tzmin, tzmax; + + if(ray->original) + BLI_pbvh_node_get_original_BB(node, bb_min, bb_max); + else + BLI_pbvh_node_get_BB(node, bb_min, bb_max); + + copy_v3_v3(bbox[0], bb_min); + copy_v3_v3(bbox[1], bb_max); + + tmin = (bbox[ray->sign[0]][0] - ray->start[0]) * ray->inv_dir[0]; + tmax = (bbox[1-ray->sign[0]][0] - ray->start[0]) * ray->inv_dir[0]; + + tymin = (bbox[ray->sign[1]][1] - ray->start[1]) * ray->inv_dir[1]; + tymax = (bbox[1-ray->sign[1]][1] - ray->start[1]) * ray->inv_dir[1]; + + if((tmin > tymax) || (tymin > tmax)) + return 0; + if(tymin > tmin) + tmin = tymin; + if(tymax < tmax) + tmax = tymax; + + tzmin = (bbox[ray->sign[2]][2] - ray->start[2]) * ray->inv_dir[2]; + tzmax = (bbox[1-ray->sign[2]][2] - ray->start[2]) * ray->inv_dir[2]; + + if((tmin > tzmax) || (tzmin > tmax)) + return 0; + + return 1; + + /* XXX: Not sure about this? + if(tzmin > tmin) + tmin = tzmin; + if(tzmax < tmax) + tmax = tzmax; + return ((tmin < t1) && (tmax > t0)); + */ + +} + +void BLI_pbvh_raycast(PBVH *bvh, BLI_pbvh_HitCallback cb, void *data, + float ray_start[3], float ray_normal[3], int original) +{ + RaycastData rcd; + + copy_v3_v3(rcd.start, ray_start); + rcd.inv_dir[0] = 1.0f / ray_normal[0]; + rcd.inv_dir[1] = 1.0f / ray_normal[1]; + rcd.inv_dir[2] = 1.0f / ray_normal[2]; + rcd.sign[0] = rcd.inv_dir[0] < 0; + rcd.sign[1] = rcd.inv_dir[1] < 0; + rcd.sign[2] = rcd.inv_dir[2] < 0; + rcd.original = original; + + BLI_pbvh_search_callback(bvh, ray_aabb_intersect, &rcd, cb, data); +} + +/* XXX: Code largely copied from bvhutils.c, could be unified */ +/* Returns 1 if a better intersection has been found */ +static int ray_face_intersection(float ray_start[3], float ray_normal[3], + float *t0, float *t1, float *t2, float *t3, + float *fdist) +{ + int hit = 0; + + do + { + float dist = FLT_MAX; + + if(!isect_ray_tri_threshold_v3(ray_start, ray_normal, t0, t1, t2, + &dist, NULL, 0.001f)) + dist = FLT_MAX; + + if(dist >= 0 && dist < *fdist) { + hit = 1; + *fdist = dist; + } + + t1 = t2; + t2 = t3; + t3 = NULL; + + } while(t2); + + return hit; +} + +int BLI_pbvh_node_raycast(PBVH *bvh, PBVHNode *node, float (*origco)[3], + float ray_start[3], float ray_normal[3], float *dist) +{ + int hit= 0; + + if(bvh->faces) { + MVert *vert = bvh->verts; + int *faces= node->prim_indices; + int *face_verts= node->face_vert_indices; + int totface= node->totprim; + int i; + + for(i = 0; i < totface; ++i) { + MFace *f = bvh->faces + faces[i]; + + if(origco) { + /* intersect with backuped original coordinates */ + hit |= ray_face_intersection(ray_start, ray_normal, + origco[face_verts[i*4+0]], + origco[face_verts[i*4+1]], + origco[face_verts[i*4+2]], + f->v4? origco[face_verts[i*4+3]]: NULL, + dist); + } + else { + /* intersect with current coordinates */ + hit |= ray_face_intersection(ray_start, ray_normal, + vert[f->v1].co, + vert[f->v2].co, + vert[f->v3].co, + f->v4 ? vert[f->v4].co : NULL, + dist); + } + } + } + else { + int totgrid= node->totprim; + int gridsize= bvh->gridsize; + int i, x, y; + + for(i = 0; i < totgrid; ++i) { + DMGridData *grid= bvh->grids[node->prim_indices[i]]; + + for(y = 0; y < gridsize-1; ++y) { + for(x = 0; x < gridsize-1; ++x) { + if(origco) { + hit |= ray_face_intersection(ray_start, ray_normal, + origco[y*gridsize + x], + origco[y*gridsize + x+1], + origco[(y+1)*gridsize + x+1], + origco[(y+1)*gridsize + x], + dist); + } + else { + hit |= ray_face_intersection(ray_start, ray_normal, + grid[y*gridsize + x].co, + grid[y*gridsize + x+1].co, + grid[(y+1)*gridsize + x+1].co, + grid[(y+1)*gridsize + x].co, + dist); + } + } + } + + if(origco) + origco += gridsize*gridsize; + } + } + + return hit; +} + +//#include <GL/glew.h> + +void BLI_pbvh_node_draw(PBVHNode *node, void *data) +{ +#if 0 + /* XXX: Just some quick code to show leaf nodes in different colors */ + float col[3]; int i; + + if(0) { //is_partial) { + col[0] = (rand() / (float)RAND_MAX); col[1] = col[2] = 0.6; + } + else { + srand((long long)node); + for(i = 0; i < 3; ++i) + col[i] = (rand() / (float)RAND_MAX) * 0.3 + 0.7; + } + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col); + + glColor3f(1, 0, 0); +#endif + GPU_draw_buffers(node->draw_buffers); +} + +/* Adapted from: + http://www.gamedev.net/community/forums/topic.asp?topic_id=512123 + Returns true if the AABB is at least partially within the frustum + (ok, not a real frustum), false otherwise. +*/ +int BLI_pbvh_node_planes_contain_AABB(PBVHNode *node, void *data) +{ + float (*planes)[4] = data; + int i, axis; + float vmin[3], vmax[3], bb_min[3], bb_max[3]; + + BLI_pbvh_node_get_BB(node, bb_min, bb_max); + + for(i = 0; i < 4; ++i) { + for(axis = 0; axis < 3; ++axis) { + if(planes[i][axis] > 0) { + vmin[axis] = bb_min[axis]; + vmax[axis] = bb_max[axis]; + } + else { + vmin[axis] = bb_max[axis]; + vmax[axis] = bb_min[axis]; + } + } + + if(dot_v3v3(planes[i], vmin) + planes[i][3] > 0) + return 0; + } + + return 1; +} + +void BLI_pbvh_draw(PBVH *bvh, float (*planes)[4], float (*face_nors)[3]) +{ + BLI_pbvh_update(bvh, PBVH_UpdateNormals|PBVH_UpdateDrawBuffers, face_nors); + + if(planes) { + BLI_pbvh_search_callback(bvh, BLI_pbvh_node_planes_contain_AABB, + planes, BLI_pbvh_node_draw, NULL); + } + else { + BLI_pbvh_search_callback(bvh, NULL, NULL, BLI_pbvh_node_draw, NULL); + } +} + +void BLI_pbvh_node_verts_iter_init(PBVH *bvh, PBVHNode *node, PBVHVertexIter *vi, int mode) +{ + memset(vi, 0, sizeof(PBVHVertexIter)); + vi->grids= bvh->grids; + vi->grid_indices= node->prim_indices; + vi->totgrid= (bvh->grids)? node->totprim: 1; + vi->gridsize= bvh->gridsize; + + vi->totvert= node->uniq_verts; + if(mode == PBVH_ITER_ALL) + vi->totvert += node->face_verts; + vi->vert_indices= node->vert_indices; + vi->mverts= bvh->verts; +} + diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index eada820ba89..2937b7b22e7 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -3325,14 +3325,14 @@ static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts) } } -static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps) +static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int external) { if(mdisps) { int i; for(i = 0; i < count; ++i) { mdisps[i].disps = newdataadr(fd, mdisps[i].disps); - if(!mdisps[i].disps) + if(!external && !mdisps[i].disps) mdisps[i].totdisp = 0; } } @@ -3343,14 +3343,18 @@ static void direct_link_customdata(FileData *fd, CustomData *data, int count) int i = 0; data->layers= newdataadr(fd, data->layers); + data->external= newdataadr(fd, data->external); while (i < data->totlayer) { CustomDataLayer *layer = &data->layers[i]; + if(layer->flag & CD_FLAG_EXTERNAL) + layer->flag &= ~CD_FLAG_IN_MEMORY; + if (CustomData_verify_versions(data, i)) { layer->data = newdataadr(fd, layer->data); if(layer->type == CD_MDISPS) - direct_link_mdisps(fd, count, layer->data); + direct_link_mdisps(fd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL); i++; } } @@ -3938,12 +3942,6 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb) SWITCH_INT(mmd->dynverts[a]) } } - else if (md->type==eModifierType_Multires) { - MultiresModifierData *mmd = (MultiresModifierData*) md; - - mmd->undo_verts = newdataadr(fd, mmd->undo_verts); - mmd->undo_signal = !!mmd->undo_verts; - } } } @@ -9635,7 +9633,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main) ToolSettings *ts; //PTCacheID *pid; //ListBase pidlist; - int i, a; + int a; for(ob = main->object.first; ob; ob = ob->id.next) { //BKE_ptcache_ids_from_object(&pidlist, ob); @@ -9652,58 +9650,8 @@ static void do_versions(FileData *fd, Library *lib, Main *main) void *olddata = ob->data; ob->data = me; - if(me && me->id.lib==NULL && me->mr) { /* XXX - library meshes crash on loading most yoFrankie levels, the multires pointer gets invalid - Campbell */ - MultiresLevel *lvl; - ModifierData *md; - MultiresModifierData *mmd; - DerivedMesh *dm, *orig; - - /* Load original level into the mesh */ - lvl = me->mr->levels.first; - CustomData_free_layers(&me->vdata, CD_MVERT, lvl->totvert); - CustomData_free_layers(&me->edata, CD_MEDGE, lvl->totedge); - CustomData_free_layers(&me->fdata, CD_MFACE, lvl->totface); - me->totvert = lvl->totvert; - me->totedge = lvl->totedge; - me->totface = lvl->totface; - me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, me->totvert); - me->medge = CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, NULL, me->totedge); - me->mface = CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, me->totface); - memcpy(me->mvert, me->mr->verts, sizeof(MVert) * me->totvert); - for(i = 0; i < me->totedge; ++i) { - me->medge[i].v1 = lvl->edges[i].v[0]; - me->medge[i].v2 = lvl->edges[i].v[1]; - } - for(i = 0; i < me->totface; ++i) { - me->mface[i].v1 = lvl->faces[i].v[0]; - me->mface[i].v2 = lvl->faces[i].v[1]; - me->mface[i].v3 = lvl->faces[i].v[2]; - me->mface[i].v4 = lvl->faces[i].v[3]; - } - - /* Add a multires modifier to the object */ - md = ob->modifiers.first; - while(md && modifierType_getInfo(md->type)->type == eModifierTypeType_OnlyDeform) - md = md->next; - mmd = (MultiresModifierData*)modifier_new(eModifierType_Multires); - BLI_insertlinkbefore(&ob->modifiers, md, mmd); - - multiresModifier_subdivide(mmd, ob, me->mr->level_count - 1, 1, 0); - - mmd->lvl = mmd->totlvl; - orig = CDDM_from_mesh(me, NULL); - dm = multires_dm_create_from_derived(mmd, 0, orig, ob, 0, 0); - - multires_load_old(dm, me->mr); - - MultiresDM_mark_as_modified(dm); - dm->release(dm); - orig->release(orig); - - /* Remove the old multires */ - multires_free(me->mr); - me->mr = NULL; - } + if(me && me->id.lib==NULL && me->mr) /* XXX - library meshes crash on loading most yoFrankie levels, the multires pointer gets invalid - Campbell */ + multires_load_old(ob, me); ob->data = olddata; } @@ -10175,15 +10123,34 @@ static void do_versions(FileData *fd, Library *lib, Main *main) } } - /* put 2.50 compatibility code here until next subversion bump */ + if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 9)) { - Scene *sce= main->scene.first; + Scene *sce; + Mesh *me; + Object *ob; for(sce=main->scene.first; sce; sce=sce->id.next) if(!sce->toolsettings->particle.selectmode) sce->toolsettings->particle.selectmode= SCE_SELECT_PATH; + + if (main->versionfile == 250 && main->subversionfile > 1) { + for(me=main->mesh.first; me; me=me->id.next) + multires_load_old_250(me); + + for(ob=main->object.first; ob; ob=ob->id.next) { + MultiresModifierData *mmd = (MultiresModifierData *)modifiers_findByType(ob, eModifierType_Multires); + + if(mmd) { + mmd->totlvl--; + mmd->lvl--; + mmd->sculptlvl= mmd->lvl; + mmd->renderlvl= mmd->lvl; + } + } + } } + /* put 2.50 compatibility code here until next subversion bump */ { Object *ob; @@ -10199,6 +10166,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main) } } } + /* WATCH IT!!!: pointers from libdata have not been converted yet here! */ /* WATCH IT 2!: Userdef struct init has to be in src/usiblender.c! */ diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c index 3d654f5ebc9..280b9ffc182 100644 --- a/source/blender/blenloader/intern/writefile.c +++ b/source/blender/blenloader/intern/writefile.c @@ -1222,12 +1222,6 @@ static void write_modifiers(WriteData *wd, ListBase *modbase) writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences); writedata(wd, DATA, sizeof(int)*mmd->totvert, mmd->dynverts); } - else if (md->type==eModifierType_Multires) { - MultiresModifierData *mmd = (MultiresModifierData*) md; - - if(mmd->undo_verts) - writestruct(wd, DATA, "MVert", mmd->undo_verts_tot, mmd->undo_verts); - } } } @@ -1448,23 +1442,29 @@ static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist) } } -static void write_mdisps(WriteData *wd, int count, MDisps *mdlist) +static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external) { if(mdlist) { int i; writestruct(wd, DATA, "MDisps", count, mdlist); - for(i = 0; i < count; ++i) { - if(mdlist[i].disps) - writedata(wd, DATA, sizeof(float)*3*mdlist[i].totdisp, mdlist[i].disps); + if(!external) { + for(i = 0; i < count; ++i) { + if(mdlist[i].disps) + writedata(wd, DATA, sizeof(float)*3*mdlist[i].totdisp, mdlist[i].disps); + } } } } -static void write_customdata(WriteData *wd, int count, CustomData *data, int partial_type, int partial_count) +static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data, int partial_type, int partial_count) { int i; + /* write external customdata (not for undo) */ + if(data->external && !wd->current) + CustomData_external_write(data, id, CD_MASK_MESH, count, 0); + writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers); for (i=0; i<data->totlayer; i++) { @@ -1477,7 +1477,7 @@ static void write_customdata(WriteData *wd, int count, CustomData *data, int par write_dverts(wd, count, layer->data); } else if (layer->type == CD_MDISPS) { - write_mdisps(wd, count, layer->data); + write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL); } else { CustomData_file_write_info(layer->type, &structname, &structnum); @@ -1494,6 +1494,9 @@ static void write_customdata(WriteData *wd, int count, CustomData *data, int par printf("error: this CustomDataLayer must not be written to file\n"); } } + + if(data->external) + writestruct(wd, DATA, "CustomDataExternal", 1, data->external); } static void write_meshs(WriteData *wd, ListBase *idbase) @@ -1512,16 +1515,16 @@ static void write_meshs(WriteData *wd, ListBase *idbase) writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat); if(mesh->pv) { - write_customdata(wd, mesh->pv->totvert, &mesh->vdata, -1, 0); - write_customdata(wd, mesh->pv->totedge, &mesh->edata, + write_customdata(wd, &mesh->id, mesh->pv->totvert, &mesh->vdata, -1, 0); + write_customdata(wd, &mesh->id, mesh->pv->totedge, &mesh->edata, CD_MEDGE, mesh->totedge); - write_customdata(wd, mesh->pv->totface, &mesh->fdata, + write_customdata(wd, &mesh->id, mesh->pv->totface, &mesh->fdata, CD_MFACE, mesh->totface); } else { - write_customdata(wd, mesh->totvert, &mesh->vdata, -1, 0); - write_customdata(wd, mesh->totedge, &mesh->edata, -1, 0); - write_customdata(wd, mesh->totface, &mesh->fdata, -1, 0); + write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0); + write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0); + write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, -1, 0); } /* PMV data */ diff --git a/source/blender/editors/include/ED_sculpt.h b/source/blender/editors/include/ED_sculpt.h index 764efb4ef0c..aae79e9a1de 100644 --- a/source/blender/editors/include/ED_sculpt.h +++ b/source/blender/editors/include/ED_sculpt.h @@ -28,18 +28,28 @@ #ifndef ED_SCULPT_H #define ED_SCULPT_H +struct ARegion; struct bContext; +struct Object; +struct RegionView3D; struct wmKeyConfig; +struct wmWindowManager; /* sculpt.c */ void ED_operatortypes_sculpt(void); +void sculpt_get_redraw_planes(float planes[4][4], struct ARegion *ar, + struct RegionView3D *rv3d, struct Object *ob); +void ED_sculpt_force_update(struct bContext *C); /* paint_ops.c */ void ED_operatortypes_paint(void); void ED_keymap_paint(struct wmKeyConfig *keyconf); -/* paint_image.c */ -void undo_imagepaint_step(int step); -void undo_imagepaint_clear(void); +/* paint_undo.c */ +#define UNDO_PAINT_IMAGE 0 +#define UNDO_PAINT_MESH 1 + +void ED_undo_paint_step(struct bContext *C, int type, int step); +void ED_undo_paint_free(void); #endif diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h index 0e7f55bef8d..13ef235d8fe 100644 --- a/source/blender/editors/include/ED_view3d.h +++ b/source/blender/editors/include/ED_view3d.h @@ -30,6 +30,7 @@ /* ********* exports for space_view3d/ module ********** */ struct ARegion; +struct BoundBox; struct View3D; struct RegionView3D; struct ViewContext; @@ -44,6 +45,7 @@ struct ImBuf; struct Scene; struct bContext; struct Main; +struct rcti; /* for derivedmesh drawing callbacks, for view3d_select, .... */ typedef struct ViewContext { @@ -80,6 +82,8 @@ void request_depth_update(struct RegionView3D *rv3d); /* Projection */ #define IS_CLIPPED 12000 +void view3d_calculate_clipping(struct BoundBox *bb, float planes[4][4], struct bglMats *mats, struct rcti *rect); + void project_short(struct ARegion *ar, float *vec, short *adr); void project_short_noclip(struct ARegion *ar, float *vec, short *adr); @@ -131,7 +135,7 @@ short view3d_opengl_select(struct ViewContext *vc, unsigned int *buffer, unsigne void view3d_set_viewcontext(struct bContext *C, struct ViewContext *vc); void view3d_operator_needs_opengl(const struct bContext *C); void view3d_get_view_aligned_coordinate(struct ViewContext *vc, float *fp, short mval[2]); -void view3d_get_transformation(struct ViewContext *vc, struct Object *ob, struct bglMats *mats); +void view3d_get_transformation(struct ARegion *ar, struct RegionView3D *rv3d, struct Object *ob, struct bglMats *mats); /* XXX should move to arithb.c */ int edge_inside_circle(short centx, short centy, short rad, short x1, short y1, short x2, short y2); diff --git a/source/blender/editors/mesh/editface.c b/source/blender/editors/mesh/editface.c index d034fe4a783..d7df018acd7 100644 --- a/source/blender/editors/mesh/editface.c +++ b/source/blender/editors/mesh/editface.c @@ -99,15 +99,17 @@ void object_facesel_flush_dm(Object *ob) int totface; int i; - - if(me==NULL || dm==NULL || !CustomData_has_layer( &dm->faceData, CD_ORIGINDEX)) + if(me==NULL || dm==NULL) + return; + + index_array = dm->getFaceDataArray(dm, CD_ORIGINDEX); + + if(!index_array) return; faces = dm->getFaceArray(dm); totface = dm->getNumFaces(dm); - index_array = dm->getFaceDataArray(dm, CD_ORIGINDEX); - mf= faces; for (i= 0; i<totface; i++, mf++) { /* loop over derived mesh faces */ diff --git a/source/blender/editors/object/object_intern.h b/source/blender/editors/object/object_intern.h index c87210d6070..848855da1b0 100644 --- a/source/blender/editors/object/object_intern.h +++ b/source/blender/editors/object/object_intern.h @@ -143,6 +143,8 @@ void OBJECT_OT_modifier_convert(struct wmOperatorType *ot); void OBJECT_OT_modifier_copy(struct wmOperatorType *ot); void OBJECT_OT_multires_subdivide(struct wmOperatorType *ot); void OBJECT_OT_multires_higher_levels_delete(struct wmOperatorType *ot); +void OBJECT_OT_multires_save_external(struct wmOperatorType *ot); +void OBJECT_OT_multires_pack_external(struct wmOperatorType *ot); void OBJECT_OT_meshdeform_bind(struct wmOperatorType *ot); void OBJECT_OT_explode_refresh(struct wmOperatorType *ot); diff --git a/source/blender/editors/object/object_modifier.c b/source/blender/editors/object/object_modifier.c index 14c342d4ecd..a1e70c011fe 100644 --- a/source/blender/editors/object/object_modifier.c +++ b/source/blender/editors/object/object_modifier.c @@ -44,6 +44,7 @@ #include "BLI_math.h" #include "BLI_listbase.h" #include "BLI_string.h" +#include "BLI_util.h" #include "BKE_action.h" #include "BKE_curve.h" @@ -187,6 +188,12 @@ int ED_object_modifier_remove(ReportList *reports, Scene *scene, Object *ob, Mod else if(md->type == eModifierType_Smoke) { ob->dt = OB_TEXTURE; } + else if(md->type == eModifierType_Multires) { + Mesh *me= ob->data; + + CustomData_external_remove(&me->fdata, &me->id, CD_MDISPS, me->totface); + CustomData_free_layer_active(&me->fdata, CD_MDISPS, me->totface); + } BLI_remlink(&ob->modifiers, md); modifier_free(md); @@ -414,7 +421,7 @@ static int modifier_apply_obdata(ReportList *reports, Scene *scene, Object *ob, BKE_report(reports, RPT_INFO, "Applied modifier only changed CV points, not tesselated/bevel vertices"); - if (!(md->mode&eModifierMode_Realtime) || (mti->isDisabled && mti->isDisabled(md))) { + if (!(md->mode&eModifierMode_Realtime) || (mti->isDisabled && mti->isDisabled(md, 0))) { BKE_report(reports, RPT_ERROR, "Modifier is disabled, skipping apply"); return 0; } @@ -745,6 +752,13 @@ void OBJECT_OT_modifier_copy(wmOperatorType *ot) /************* multires delete higher levels operator ****************/ +static int multires_poll(bContext *C) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_MultiresModifier); + ID *id= ptr.id.data; + return (ptr.data && id && !id->lib); +} + static int multires_higher_levels_delete_exec(bContext *C, wmOperator *op) { PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_MultiresModifier); @@ -763,8 +777,8 @@ void OBJECT_OT_multires_higher_levels_delete(wmOperatorType *ot) { ot->name= "Delete Higher Levels"; ot->idname= "OBJECT_OT_multires_higher_levels_delete"; - ot->poll= ED_operator_object_active_editable; + ot->poll= multires_poll; ot->exec= multires_higher_levels_delete_exec; /* flags */ @@ -779,27 +793,113 @@ static int multires_subdivide_exec(bContext *C, wmOperator *op) Object *ob= ptr.id.data; MultiresModifierData *mmd= ptr.data; - multiresModifier_subdivide(mmd, ob, 1, 0, mmd->simple); + multiresModifier_subdivide(mmd, ob, 0, mmd->simple); + + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob); return OPERATOR_FINISHED; } -static int multires_subdivide_poll(bContext *C) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_MultiresModifier); - ID *id= ptr.id.data; - return (ptr.data && id && !id->lib); -} - void OBJECT_OT_multires_subdivide(wmOperatorType *ot) { ot->name= "Multires Subdivide"; ot->description= "Add a new level of subdivision."; ot->idname= "OBJECT_OT_multires_subdivide"; + ot->poll= multires_poll; ot->exec= multires_subdivide_exec; - ot->poll= multires_subdivide_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/****************** multires save external operator *********************/ + +static int multires_save_external_exec(bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_MultiresModifier); + Object *ob= ptr.id.data; + Mesh *me= (ob)? ob->data: op->customdata; + char path[FILE_MAX]; + + if(CustomData_external_test(&me->fdata, CD_MDISPS)) + return OPERATOR_CANCELLED; + + RNA_string_get(op->ptr, "path", path); + if(G.save_over) + BLI_makestringcode(G.sce, path); /* make relative */ + + CustomData_external_add(&me->fdata, &me->id, CD_MDISPS, me->totface, path); + CustomData_external_write(&me->fdata, &me->id, CD_MASK_MESH, me->totface, 0); + + return OPERATOR_FINISHED; +} + +static int multires_save_external_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_MultiresModifier); + Object *ob= ptr.id.data; + Mesh *me= ob->data; + char path[FILE_MAX]; + + if(CustomData_external_test(&me->fdata, CD_MDISPS)) + return OPERATOR_CANCELLED; + + if(RNA_property_is_set(op->ptr, "path")) + return multires_save_external_exec(C, op); + + op->customdata= me; + + BLI_snprintf(path, sizeof(path), "//%s.btx", me->id.name+2); + RNA_string_set(op->ptr, "path", path); + + WM_event_add_fileselect(C, op); + + return OPERATOR_RUNNING_MODAL; +} + +void OBJECT_OT_multires_save_external(wmOperatorType *ot) +{ + ot->name= "Multires Save External"; + ot->description= "Save displacements to an external file."; + ot->idname= "OBJECT_OT_multires_save_external"; + + ot->poll= multires_poll; + ot->exec= multires_save_external_exec; + ot->invoke= multires_save_external_invoke; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + WM_operator_properties_filesel(ot, FOLDERFILE|BTXFILE, FILE_SPECIAL); +} + +/****************** multires pack operator *********************/ + +static int multires_pack_external_exec(bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_MultiresModifier); + Object *ob= ptr.id.data; + Mesh *me= ob->data; + + if(!CustomData_external_test(&me->fdata, CD_MDISPS)) + return OPERATOR_CANCELLED; + + // XXX don't remove.. + CustomData_external_remove(&me->fdata, &me->id, CD_MDISPS, me->totface); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_multires_pack_external(wmOperatorType *ot) +{ + ot->name= "Multires Pack External"; + ot->description= "Pack displacements from an external file."; + ot->idname= "OBJECT_OT_multires_pack_external"; + + ot->poll= multires_poll; + ot->exec= multires_pack_external_exec; /* flags */ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; diff --git a/source/blender/editors/object/object_ops.c b/source/blender/editors/object/object_ops.c index 66a03d4aec5..8def741ed2b 100644 --- a/source/blender/editors/object/object_ops.c +++ b/source/blender/editors/object/object_ops.c @@ -136,6 +136,8 @@ void ED_operatortypes_object(void) WM_operatortype_append(OBJECT_OT_modifier_copy); WM_operatortype_append(OBJECT_OT_multires_subdivide); WM_operatortype_append(OBJECT_OT_multires_higher_levels_delete); + WM_operatortype_append(OBJECT_OT_multires_save_external); + WM_operatortype_append(OBJECT_OT_multires_pack_external); WM_operatortype_append(OBJECT_OT_meshdeform_bind); WM_operatortype_append(OBJECT_OT_explode_refresh); @@ -227,6 +229,7 @@ void ED_keymap_object(wmKeyConfig *keyconf) { wmKeyMap *keymap; wmKeyMapItem *kmi; + int i; /* Objects, Regardless of Mode -------------------------------------------------- */ keymap= WM_keymap_find(keyconf, "Object Non-modal", 0, 0); @@ -314,16 +317,10 @@ void ED_keymap_object(wmKeyConfig *keyconf) WM_keymap_verify_item(keymap, "GROUP_OT_objects_add_active", GKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0); WM_keymap_verify_item(keymap, "GROUP_OT_objects_remove_active", GKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0); - kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subsurf_set", ONEKEY, KM_PRESS, KM_CTRL, 0); - RNA_int_set(kmi->ptr, "level", 1); - kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subsurf_set", TWOKEY, KM_PRESS, KM_CTRL, 0); - RNA_int_set(kmi->ptr, "level", 2); - kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subsurf_set", THREEKEY, KM_PRESS, KM_CTRL, 0); - RNA_int_set(kmi->ptr, "level", 3); - kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subsurf_set", FOURKEY, KM_PRESS, KM_CTRL, 0); - RNA_int_set(kmi->ptr, "level", 4); - kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subsurf_set", FIVEKEY, KM_PRESS, KM_CTRL, 0); - RNA_int_set(kmi->ptr, "level", 5); + for(i=1; i<=5; i++) { + kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY+i, KM_PRESS, KM_CTRL, 0); + RNA_int_set(kmi->ptr, "level", i); + } /* Lattice -------------------------------------------------------------------- */ keymap= WM_keymap_find(keyconf, "Lattice", 0, 0); diff --git a/source/blender/editors/physics/particle_edit.c b/source/blender/editors/physics/particle_edit.c index 7c6b3a5ee26..ac986ba7df6 100644 --- a/source/blender/editors/physics/particle_edit.c +++ b/source/blender/editors/physics/particle_edit.c @@ -362,7 +362,7 @@ static void PE_set_view3d_data(bContext *C, PEData *data) PE_set_data(C, data); view3d_set_viewcontext(C, &data->vc); - view3d_get_transformation(&data->vc, data->ob, &data->mats); + view3d_get_transformation(data->vc.ar, data->vc.rv3d, data->ob, &data->mats); if((data->vc.v3d->drawtype>OB_WIRE) && (data->vc.v3d->flag & V3D_ZBUF_SELECT)) view3d_validate_backbuf(&data->vc); diff --git a/source/blender/editors/sculpt_paint/paint_image.c b/source/blender/editors/sculpt_paint/paint_image.c index 55f7a6a23b9..1f0d158ece9 100644 --- a/source/blender/editors/sculpt_paint/paint_image.c +++ b/source/blender/editors/sculpt_paint/paint_image.c @@ -86,6 +86,7 @@ #include "ED_image.h" #include "ED_object.h" #include "ED_screen.h" +#include "ED_sculpt.h" #include "ED_view3d.h" #include "WM_api.h" @@ -113,8 +114,6 @@ #define IMAPAINT_TILE_SIZE (1 << IMAPAINT_TILE_BITS) #define IMAPAINT_TILE_NUMBER(size) (((size)+IMAPAINT_TILE_SIZE-1) >> IMAPAINT_TILE_BITS) -#define MAXUNDONAME 64 - static void imapaint_image_update(SpaceImage *sima, Image *image, ImBuf *ibuf, short texpaint); @@ -204,7 +203,7 @@ typedef struct ProjPaintImage { Image *ima; ImBuf *ibuf; ImagePaintPartialRedraw *partRedrawRect; - struct UndoTile **undoRect; /* only used to build undo tiles after painting */ + void **undoRect; /* only used to build undo tiles after painting */ int touch; } ProjPaintImage; @@ -332,32 +331,20 @@ typedef struct ProjPixelClone { /* Finish projection painting structs */ +typedef struct UndoImageTile { + struct UndoImageTile *next, *prev; + + char idname[MAX_ID_NAME]; /* name instead of pointer*/ -typedef struct UndoTile { - struct UndoTile *next, *prev; - ID id; void *rect; int x, y; -} UndoTile; - -typedef struct UndoElem { - struct UndoElem *next, *prev; - char name[MAXUNDONAME]; - uintptr_t undosize; +} UndoImageTile; - ImBuf *ibuf; - ListBase tiles; -} UndoElem; - -static ListBase undobase = {NULL, NULL}; -static UndoElem *curundo = NULL; static ImagePaintPartialRedraw imapaintpartial = {0, 0, 0, 0, 0}; /* UNDO */ -/* internal functions */ - -static void undo_copy_tile(UndoTile *tile, ImBuf *tmpibuf, ImBuf *ibuf, int restore) +static void undo_copy_tile(UndoImageTile *tile, ImBuf *tmpibuf, ImBuf *ibuf, int restore) { /* copy or swap contents of tile->rect and region in ibuf->rect */ IMB_rectcpy(tmpibuf, ibuf, 0, 0, tile->x*IMAPAINT_TILE_SIZE, @@ -374,49 +361,52 @@ static void undo_copy_tile(UndoTile *tile, ImBuf *tmpibuf, ImBuf *ibuf, int rest tile->y*IMAPAINT_TILE_SIZE, 0, 0, IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE); } -static UndoTile *undo_init_tile(ID *id, ImBuf *ibuf, ImBuf **tmpibuf, int x_tile, int y_tile) +static void *image_undo_push_tile(Image *ima, ImBuf *ibuf, ImBuf **tmpibuf, int x_tile, int y_tile) { - UndoTile *tile; + ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_IMAGE); + UndoImageTile *tile; int allocsize; + + for(tile=lb->first; tile; tile=tile->next) + if(tile->x == x_tile && tile->y == y_tile && strcmp(tile->idname, ima->id.name)==0) + return tile->rect; if (*tmpibuf==NULL) *tmpibuf = IMB_allocImBuf(IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE, 32, IB_rectfloat|IB_rect, 0); - tile= MEM_callocN(sizeof(UndoTile), "ImaUndoTile"); - tile->id= *id; + tile= MEM_callocN(sizeof(UndoImageTile), "UndoImageTile"); + strcpy(tile->idname, ima->id.name); tile->x= x_tile; tile->y= y_tile; allocsize= IMAPAINT_TILE_SIZE*IMAPAINT_TILE_SIZE*4; allocsize *= (ibuf->rect_float)? sizeof(float): sizeof(char); - tile->rect= MEM_mapallocN(allocsize, "ImaUndoRect"); + tile->rect= MEM_mapallocN(allocsize, "UndeImageTile.rect"); undo_copy_tile(tile, *tmpibuf, ibuf, 0); - curundo->undosize += allocsize; + undo_paint_push_count_alloc(UNDO_PAINT_IMAGE, allocsize); - BLI_addtail(&curundo->tiles, tile); + BLI_addtail(lb, tile); - return tile; + return tile->rect; } -static void undo_restore(UndoElem *undo) +static void image_undo_restore(bContext *C, ListBase *lb) { + Main *bmain= CTX_data_main(C); Image *ima = NULL; ImBuf *ibuf, *tmpibuf; - UndoTile *tile; - - if(!undo) - return; + UndoImageTile *tile; tmpibuf= IMB_allocImBuf(IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE, 32, IB_rectfloat|IB_rect, 0); - for(tile=undo->tiles.first; tile; tile=tile->next) { + for(tile=lb->first; tile; tile=tile->next) { /* find image based on name, pointer becomes invalid with global undo */ - if(ima && strcmp(tile->id.name, ima->id.name)==0); + if(ima && strcmp(tile->idname, ima->id.name)==0); else { - for(ima=G.main->image.first; ima; ima=ima->id.next) - if(strcmp(tile->id.name, ima->id.name)==0) + for(ima=bmain->image.first; ima; ima=ima->id.next) + if(strcmp(tile->idname, ima->id.name)==0) break; } @@ -435,117 +425,12 @@ static void undo_restore(UndoElem *undo) IMB_freeImBuf(tmpibuf); } -static void undo_free(UndoElem *undo) +static void image_undo_free(ListBase *lb) { - UndoTile *tile; + UndoImageTile *tile; - for(tile=undo->tiles.first; tile; tile=tile->next) + for(tile=lb->first; tile; tile=tile->next) MEM_freeN(tile->rect); - BLI_freelistN(&undo->tiles); -} - -static void undo_imagepaint_push_begin(char *name) -{ - UndoElem *uel; - int nr; - - /* Undo push is split up in begin and end, the reason is that as painting - * happens more tiles are added to the list, and at the very end we know - * how much memory the undo used to remove old undo elements */ - - /* remove all undos after (also when curundo==NULL) */ - while(undobase.last != curundo) { - uel= undobase.last; - undo_free(uel); - BLI_freelinkN(&undobase, uel); - } - - /* make new */ - curundo= uel= MEM_callocN(sizeof(UndoElem), "undo file"); - BLI_addtail(&undobase, uel); - - /* name can be a dynamic string */ - strncpy(uel->name, name, MAXUNDONAME-1); - - /* limit amount to the maximum amount*/ - nr= 0; - uel= undobase.last; - while(uel) { - nr++; - if(nr==U.undosteps) break; - uel= uel->prev; - } - if(uel) { - while(undobase.first!=uel) { - UndoElem *first= undobase.first; - undo_free(first); - BLI_freelinkN(&undobase, first); - } - } -} - -static void undo_imagepaint_push_end() -{ - UndoElem *uel; - uintptr_t totmem, maxmem; - - if(U.undomemory != 0) { - /* limit to maximum memory (afterwards, we can't know in advance) */ - totmem= 0; - maxmem= ((uintptr_t)U.undomemory)*1024*1024; - - uel= undobase.last; - while(uel) { - totmem+= uel->undosize; - if(totmem>maxmem) break; - uel= uel->prev; - } - - if(uel) { - while(undobase.first!=uel) { - UndoElem *first= undobase.first; - undo_free(first); - BLI_freelinkN(&undobase, first); - } - } - } -} - -void undo_imagepaint_step(int step) -{ - UndoElem *undo; - - if(step==1) { - if(curundo==NULL); - else { - if(G.f & G_DEBUG) printf("undo %s\n", curundo->name); - undo_restore(curundo); - curundo= curundo->prev; - } - } - else if(step==-1) { - if((curundo!=NULL && curundo->next==NULL) || undobase.first==NULL); - else { - undo= (curundo && curundo->next)? curundo->next: undobase.first; - undo_restore(undo); - curundo= undo; - if(G.f & G_DEBUG) printf("redo %s\n", undo->name); - } - } -} - -void undo_imagepaint_clear(void) -{ - UndoElem *uel; - - uel= undobase.first; - while(uel) { - undo_free(uel); - uel= uel->next; - } - - BLI_freelistN(&undobase); - curundo= NULL; } /* fast projection bucket array lookup, use the safe version for bound checking */ @@ -3315,7 +3200,7 @@ static void project_paint_end(ProjPaintState *ps) ProjPixel *projPixel; ImBuf *tmpibuf = NULL, *tmpibuf_float = NULL; LinkNode *pixel_node; - UndoTile *tile; + void *tilerect; MemArena *arena = ps->arena_mt[0]; /* threaded arena re-used for non threaded case */ int bucket_tot = (ps->buckets_x * ps->buckets_y); /* we could get an X/Y but easier to loop through all possible buckets */ @@ -3331,8 +3216,8 @@ static void project_paint_end(ProjPaintState *ps) int last_tile_width=0; for(a=0, last_projIma=ps->projImages; a < ps->image_tot; a++, last_projIma++) { - int size = sizeof(UndoTile **) * IMAPAINT_TILE_NUMBER(last_projIma->ibuf->x) * IMAPAINT_TILE_NUMBER(last_projIma->ibuf->y); - last_projIma->undoRect = (UndoTile **) BLI_memarena_alloc(arena, size); + int size = sizeof(void **) * IMAPAINT_TILE_NUMBER(last_projIma->ibuf->x) * IMAPAINT_TILE_NUMBER(last_projIma->ibuf->y); + last_projIma->undoRect = (void **) BLI_memarena_alloc(arena, size); memset(last_projIma->undoRect, 0, size); last_projIma->ibuf->userflags |= IB_BITMAPDIRTY; } @@ -3372,21 +3257,21 @@ static void project_paint_end(ProjPaintState *ps) if (last_projIma->undoRect[tile_index]==NULL) { /* add the undo tile from the modified image, then write the original colors back into it */ - tile = last_projIma->undoRect[tile_index] = undo_init_tile(&last_projIma->ima->id, last_projIma->ibuf, is_float ? (&tmpibuf_float):(&tmpibuf) , x_tile, y_tile); + tilerect = last_projIma->undoRect[tile_index] = image_undo_push_tile(last_projIma->ima, last_projIma->ibuf, is_float ? (&tmpibuf_float):(&tmpibuf) , x_tile, y_tile); } else { - tile = last_projIma->undoRect[tile_index]; + tilerect = last_projIma->undoRect[tile_index]; } /* This is a BIT ODD, but overwrite the undo tiles image info with this pixels original color * because allocating the tiles allong the way slows down painting */ if (is_float) { - float *rgba_fp = (float *)tile->rect + (((projPixel->x_px - x_round) + (projPixel->y_px - y_round) * IMAPAINT_TILE_SIZE)) * 4; + float *rgba_fp = (float *)tilerect + (((projPixel->x_px - x_round) + (projPixel->y_px - y_round) * IMAPAINT_TILE_SIZE)) * 4; QUATCOPY(rgba_fp, projPixel->origColor.f); } else { - ((unsigned int *)tile->rect)[ (projPixel->x_px - x_round) + (projPixel->y_px - y_round) * IMAPAINT_TILE_SIZE ] = projPixel->origColor.uint; + ((unsigned int *)tilerect)[ (projPixel->x_px - x_round) + (projPixel->y_px - y_round) * IMAPAINT_TILE_SIZE ] = projPixel->origColor.uint; } } } @@ -3957,7 +3842,6 @@ static void imapaint_clear_partial_redraw() static void imapaint_dirty_region(Image *ima, ImBuf *ibuf, int x, int y, int w, int h) { ImBuf *tmpibuf = NULL; - UndoTile *tile; int srcx= 0, srcy= 0, origx; IMB_rectclip(ibuf, NULL, &x, &y, &srcx, &srcy, &w, &h); @@ -3984,17 +3868,9 @@ static void imapaint_dirty_region(Image *ima, ImBuf *ibuf, int x, int y, int w, origx = (x >> IMAPAINT_TILE_BITS); y = (y >> IMAPAINT_TILE_BITS); - for (; y <= h; y++) { - for (x=origx; x <= w; x++) { - for(tile=curundo->tiles.first; tile; tile=tile->next) - if(tile->x == x && tile->y == y && strcmp(tile->id.name, ima->id.name)==0) - break; - - if(!tile) { - undo_init_tile(&ima->id, ibuf, &tmpibuf, x, y); - } - } - } + for (; y <= h; y++) + for (x=origx; x <= w; x++) + image_undo_push_tile(ima, ibuf, &tmpibuf, x, y); ibuf->userflags |= IB_BITMAPDIRTY; @@ -4592,7 +4468,8 @@ static int texture_paint_init(bContext *C, wmOperator *op) } settings->imapaint.flag |= IMAGEPAINT_DRAWING; - undo_imagepaint_push_begin("Image Paint"); + undo_paint_push_begin(UNDO_PAINT_IMAGE, "Image Paint", + image_undo_restore, image_undo_free); /* create painter */ pop->painter= brush_painter_new(pop->s.brush); @@ -4656,7 +4533,7 @@ static void paint_exit(bContext *C, wmOperator *op) } paint_redraw(C, &pop->s, 1); - undo_imagepaint_push_end(); + undo_paint_push_end(UNDO_PAINT_IMAGE); if(pop->s.warnmultifile) BKE_reportf(op->reports, RPT_WARNING, "Image requires 4 color channels to paint: %s", pop->s.warnmultifile); @@ -5255,3 +5132,4 @@ int facemask_paint_poll(bContext *C) { return paint_facesel_test(CTX_data_active_object(C)); } + diff --git a/source/blender/editors/sculpt_paint/paint_intern.h b/source/blender/editors/sculpt_paint/paint_intern.h index f86e1077ef3..108be50267d 100644 --- a/source/blender/editors/sculpt_paint/paint_intern.h +++ b/source/blender/editors/sculpt_paint/paint_intern.h @@ -41,13 +41,16 @@ struct wmOperator; struct wmOperatorType; struct ARegion; struct VPaint; +struct ListBase; /* paint_stroke.c */ +typedef int (*StrokeGetLocation)(struct bContext *C, struct PaintStroke *stroke, float location[3], float mouse[2]); typedef int (*StrokeTestStart)(struct bContext *C, struct wmOperator *op, struct wmEvent *event); typedef void (*StrokeUpdateStep)(struct bContext *C, struct PaintStroke *stroke, struct PointerRNA *itemptr); typedef void (*StrokeDone)(struct bContext *C, struct PaintStroke *stroke); -struct PaintStroke *paint_stroke_new(struct bContext *C, StrokeTestStart test_start, +struct PaintStroke *paint_stroke_new(struct bContext *C, + StrokeGetLocation get_location, StrokeTestStart test_start, StrokeUpdateStep update_step, StrokeDone done); int paint_stroke_modal(struct bContext *C, struct wmOperator *op, struct wmEvent *event); int paint_stroke_exec(struct bContext *C, struct wmOperator *op); @@ -100,5 +103,14 @@ void PAINT_OT_face_select_all(struct wmOperatorType *ot); int facemask_paint_poll(struct bContext *C); +/* paint_undo.c */ +typedef void (*UndoRestoreCb)(struct bContext *C, struct ListBase *lb); +typedef void (*UndoFreeCb)(struct ListBase *lb); + +void undo_paint_push_begin(int type, char *name, UndoRestoreCb restore, UndoFreeCb free); +struct ListBase *undo_paint_push_get_list(int type); +void undo_paint_push_count_alloc(int type, int size); +void undo_paint_push_end(int type); + #endif /* ED_PAINT_INTERN_H */ diff --git a/source/blender/editors/sculpt_paint/paint_ops.c b/source/blender/editors/sculpt_paint/paint_ops.c index 92812c2ab08..ae694ed863b 100644 --- a/source/blender/editors/sculpt_paint/paint_ops.c +++ b/source/blender/editors/sculpt_paint/paint_ops.c @@ -179,6 +179,7 @@ static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *path) void ED_keymap_paint(wmKeyConfig *keyconf) { wmKeyMap *keymap; + int i; /* Sculpt mode */ keymap= WM_keymap_find(keyconf, "Sculpt", 0, 0); @@ -193,6 +194,9 @@ void ED_keymap_paint(wmKeyConfig *keyconf) ed_keymap_paint_brush_switch(keymap, "tool_settings.sculpt.active_brush_index"); + for(i=1; i<=5; i++) + RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY+i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i); + /* Vertex Paint mode */ keymap= WM_keymap_find(keyconf, "Vertex Paint", 0, 0); keymap->poll= vertex_paint_poll; diff --git a/source/blender/editors/sculpt_paint/paint_stroke.c b/source/blender/editors/sculpt_paint/paint_stroke.c index 8e795c5542e..e26b1945e7f 100644 --- a/source/blender/editors/sculpt_paint/paint_stroke.c +++ b/source/blender/editors/sculpt_paint/paint_stroke.c @@ -73,6 +73,7 @@ typedef struct PaintStroke { passes over the mesh */ int stroke_started; + StrokeGetLocation get_location; StrokeTestStart test_start; StrokeUpdateStep update_step; StrokeDone done; @@ -100,7 +101,11 @@ static void paint_draw_smooth_stroke(bContext *C, int x, int y, void *customdata static void paint_draw_cursor(bContext *C, int x, int y, void *customdata) { - Brush *brush = paint_brush(paint_get_active(CTX_data_scene(C))); + Paint *paint = paint_get_active(CTX_data_scene(C)); + Brush *brush = paint_brush(paint); + + if(!(paint->flags & PAINT_SHOW_BRUSH)) + return; glColor4ubv(paint_get_active(CTX_data_scene(C))->paint_cursor_col); glEnable(GL_LINE_SMOOTH); @@ -118,13 +123,14 @@ static void paint_draw_cursor(bContext *C, int x, int y, void *customdata) static void paint_brush_stroke_add_step(bContext *C, wmOperator *op, wmEvent *event, float mouse[2]) { PointerRNA itemptr; - float cur_depth, pressure = 1; - float center[3]; + float pressure = 1; + float center[3] = {0, 0, 0}; int flip= event->shift?1:0; PaintStroke *stroke = op->customdata; - cur_depth = read_cached_depth(&stroke->vc, mouse[0], mouse[1]); - view3d_unproject(&stroke->mats, center, mouse[0], mouse[1], cur_depth); + /* XXX: can remove the if statement once all modes have this */ + if(stroke->get_location) + stroke->get_location(C, stroke, center, mouse); /* Tablet */ if(event->custom == EVT_DATA_TABLET) { @@ -211,15 +217,19 @@ static int paint_space_stroke(bContext *C, wmOperator *op, wmEvent *event, const /**** Public API ****/ -PaintStroke *paint_stroke_new(bContext *C, StrokeTestStart test_start, - StrokeUpdateStep update_step, StrokeDone done) +PaintStroke *paint_stroke_new(bContext *C, + StrokeGetLocation get_location, + StrokeTestStart test_start, + StrokeUpdateStep update_step, + StrokeDone done) { PaintStroke *stroke = MEM_callocN(sizeof(PaintStroke), "PaintStroke"); stroke->brush = paint_brush(paint_get_active(CTX_data_scene(C))); view3d_set_viewcontext(C, &stroke->vc); - view3d_get_transformation(&stroke->vc, stroke->vc.obact, &stroke->mats); + view3d_get_transformation(stroke->vc.ar, stroke->vc.rv3d, stroke->vc.obact, &stroke->mats); + stroke->get_location = get_location; stroke->test_start = test_start; stroke->update_step = update_step; stroke->done = done; @@ -229,12 +239,9 @@ PaintStroke *paint_stroke_new(bContext *C, StrokeTestStart test_start, int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event) { - ARegion *ar = CTX_wm_region(C); PaintStroke *stroke = op->customdata; float mouse[2]; - - if(event->type == TIMER && (event->customdata != stroke->timer)) - return OPERATOR_RUNNING_MODAL; + int first= 0; if(!stroke->stroke_started) { stroke->last_mouse_position[0] = event->x; @@ -249,26 +256,13 @@ int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event) stroke->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, stroke->brush->rate); } - ED_region_tag_redraw(ar); - } - - if(stroke->stroke_started) { - if(paint_smooth_stroke(stroke, mouse, event)) { - if(paint_space_stroke_enabled(stroke->brush)) { - if(!paint_space_stroke(C, op, event, mouse)) - ED_region_tag_redraw(ar); - } - else - paint_brush_stroke_add_step(C, op, event, mouse); - } - else - ED_region_tag_redraw(ar); + first= 1; + //ED_region_tag_redraw(ar); } - /* TODO: fix hardcoded event here */ + /* TODO: fix hardcoded events here */ if(event->type == LEFTMOUSE && event->val == KM_RELEASE) { - /* Exit stroke, free data */ - + /* exit stroke, free data */ if(stroke->smooth_stroke_cursor) WM_paint_cursor_end(CTX_wm_manager(C), stroke->smooth_stroke_cursor); @@ -279,8 +273,22 @@ int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event) MEM_freeN(stroke); return OPERATOR_FINISHED; } - else - return OPERATOR_RUNNING_MODAL; + else if(first || event->type == MOUSEMOVE || (event->type == TIMER && (event->customdata == stroke->timer))) { + if(stroke->stroke_started) { + if(paint_smooth_stroke(stroke, mouse, event)) { + if(paint_space_stroke_enabled(stroke->brush)) { + if(!paint_space_stroke(C, op, event, mouse)) + ;//ED_region_tag_redraw(ar); + } + else + paint_brush_stroke_add_step(C, op, event, mouse); + } + else + ;//ED_region_tag_redraw(ar); + } + } + + return OPERATOR_RUNNING_MODAL; } int paint_stroke_exec(bContext *C, wmOperator *op) diff --git a/source/blender/editors/sculpt_paint/paint_undo.c b/source/blender/editors/sculpt_paint/paint_undo.c new file mode 100644 index 00000000000..05f2b565e82 --- /dev/null +++ b/source/blender/editors/sculpt_paint/paint_undo.c @@ -0,0 +1,235 @@ +/** + * $Id$ + * + * Undo system for painting and sculpting. + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include <stdlib.h> +#include <string.h> + +#include "MEM_guardedalloc.h" + +#include "DNA_userdef_types.h" + +#include "BLI_listbase.h" + +#include "BKE_context.h" +#include "BKE_global.h" + +#include "ED_sculpt.h" + +#include "paint_intern.h" + +#define MAXUNDONAME 64 + +typedef struct UndoElem { + struct UndoElem *next, *prev; + char name[MAXUNDONAME]; + uintptr_t undosize; + + ListBase elems; + + UndoRestoreCb restore; + UndoFreeCb free; +} UndoElem; + +typedef struct UndoStack { + int type; + ListBase elems; + UndoElem *current; +} UndoStack; + +static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL}; +static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL}; + +/* Generic */ + +static void undo_restore(bContext *C, UndoStack *stack, UndoElem *uel) +{ + if(uel && uel->restore) + uel->restore(C, &uel->elems); +} + +static void undo_elem_free(UndoStack *stack, UndoElem *uel) +{ + if(uel && uel->free) { + uel->free(&uel->elems); + BLI_freelistN(&uel->elems); + } +} + +static void undo_stack_push_begin(UndoStack *stack, char *name, UndoRestoreCb restore, UndoFreeCb free) +{ + UndoElem *uel; + int nr; + + /* Undo push is split up in begin and end, the reason is that as painting + * happens more tiles/nodes are added to the list, and at the very end we + * know how much memory the undo used to remove old undo elements */ + + /* remove all undos after (also when stack->current==NULL) */ + while(stack->elems.last != stack->current) { + uel= stack->elems.last; + undo_elem_free(stack, uel); + BLI_freelinkN(&stack->elems, uel); + } + + /* make new */ + stack->current= uel= MEM_callocN(sizeof(UndoElem), "undo file"); + uel->restore= restore; + uel->free= free; + BLI_addtail(&stack->elems, uel); + + /* name can be a dynamic string */ + strncpy(uel->name, name, MAXUNDONAME-1); + + /* limit amount to the maximum amount*/ + nr= 0; + uel= stack->elems.last; + while(uel) { + nr++; + if(nr==U.undosteps) break; + uel= uel->prev; + } + if(uel) { + while(stack->elems.first!=uel) { + UndoElem *first= stack->elems.first; + undo_elem_free(stack, first); + BLI_freelinkN(&stack->elems, first); + } + } +} + +static void undo_stack_push_end(UndoStack *stack) +{ + UndoElem *uel; + uintptr_t totmem, maxmem; + + if(U.undomemory != 0) { + /* limit to maximum memory (afterwards, we can't know in advance) */ + totmem= 0; + maxmem= ((uintptr_t)U.undomemory)*1024*1024; + + uel= stack->elems.last; + while(uel) { + totmem+= uel->undosize; + if(totmem>maxmem) break; + uel= uel->prev; + } + + if(uel) { + while(stack->elems.first!=uel) { + UndoElem *first= stack->elems.first; + undo_elem_free(stack, first); + BLI_freelinkN(&stack->elems, first); + } + } + } +} + +static void undo_stack_step(bContext *C, UndoStack *stack, int step) +{ + UndoElem *undo; + + if(step==1) { + if(stack->current==NULL); + else { + if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name); + undo_restore(C, stack, stack->current); + stack->current= stack->current->prev; + } + } + else if(step==-1) { + if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL); + else { + undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first; + undo_restore(C, stack, undo); + stack->current= undo; + if(G.f & G_DEBUG) printf("redo %s\n", undo->name); + } + } +} + +static void undo_stack_free(UndoStack *stack) +{ + UndoElem *uel; + + for(uel=stack->elems.first; uel; uel=uel->next) + undo_elem_free(stack, uel); + + BLI_freelistN(&stack->elems); + stack->current= NULL; +} + +/* Exported Functions */ + +void undo_paint_push_begin(int type, char *name, UndoRestoreCb restore, UndoFreeCb free) +{ + if(type == UNDO_PAINT_IMAGE) + undo_stack_push_begin(&ImageUndoStack, name, restore, free); + else if(type == UNDO_PAINT_MESH) + undo_stack_push_begin(&MeshUndoStack, name, restore, free); +} + +ListBase *undo_paint_push_get_list(int type) +{ + if(type == UNDO_PAINT_IMAGE) { + if(ImageUndoStack.current) + return &ImageUndoStack.current->elems; + } + else if(type == UNDO_PAINT_MESH) { + if(MeshUndoStack.current) + return &MeshUndoStack.current->elems; + } + + return NULL; +} + +void undo_paint_push_count_alloc(int type, int size) +{ + if(type == UNDO_PAINT_IMAGE) + ImageUndoStack.current->undosize += size; + else if(type == UNDO_PAINT_MESH) + MeshUndoStack.current->undosize += size; +} + +void undo_paint_push_end(int type) +{ + if(type == UNDO_PAINT_IMAGE) + undo_stack_push_end(&ImageUndoStack); + else if(type == UNDO_PAINT_MESH) + undo_stack_push_end(&MeshUndoStack); +} + +void ED_undo_paint_step(bContext *C, int type, int step) +{ + if(type == UNDO_PAINT_IMAGE) + undo_stack_step(C, &ImageUndoStack, step); + else if(type == UNDO_PAINT_MESH) + undo_stack_step(C, &MeshUndoStack, step); +} + +void ED_undo_paint_free(void) +{ + undo_stack_free(&ImageUndoStack); + undo_stack_free(&MeshUndoStack); +} + diff --git a/source/blender/editors/sculpt_paint/paint_utils.c b/source/blender/editors/sculpt_paint/paint_utils.c index 496d15d793f..b968930128a 100644 --- a/source/blender/editors/sculpt_paint/paint_utils.c +++ b/source/blender/editors/sculpt_paint/paint_utils.c @@ -96,7 +96,7 @@ void imapaint_pick_uv(Scene *scene, Object *ob, Mesh *mesh, unsigned int faceind DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH); int *index = dm->getFaceDataArray(dm, CD_ORIGINDEX); MTFace *tface = dm->getFaceDataArray(dm, CD_MTFACE), *tf; - int numfaces = dm->getNumFaces(dm), a; + int numfaces = dm->getNumFaces(dm), a, findex; float p[2], w[3], absw, minabsw; MFace mf; MVert mv[4]; @@ -106,7 +106,9 @@ void imapaint_pick_uv(Scene *scene, Object *ob, Mesh *mesh, unsigned int faceind /* test all faces in the derivedmesh with the original index of the picked face */ for(a = 0; a < numfaces; a++) { - if(index[a] == faceindex) { + findex= (index)? index[a]: a; + + if(findex == faceindex) { dm->getFace(dm, a, &mf); dm->getVert(dm, mf.v1, &mv[0]); diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index 2512ecbc248..7021d76b1c4 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -1629,7 +1629,7 @@ static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke) static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event) { - op->customdata = paint_stroke_new(C, wpaint_stroke_test_start, + op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start, wpaint_stroke_update_step, wpaint_stroke_done); @@ -1931,7 +1931,7 @@ static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke) static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event) { - op->customdata = paint_stroke_new(C, vpaint_stroke_test_start, + op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start, vpaint_stroke_update_step, vpaint_stroke_done); diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c index f4006e63dc0..8b705ff5b5e 100644 --- a/source/blender/editors/sculpt_paint/sculpt.c +++ b/source/blender/editors/sculpt_paint/sculpt.c @@ -35,6 +35,9 @@ #include "BLI_math.h" #include "BLI_blenlib.h" #include "BLI_dynstr.h" +#include "BLI_ghash.h" +#include "BLI_pbvh.h" +#include "BLI_threads.h" #include "DNA_armature_types.h" #include "DNA_brush_types.h" @@ -52,6 +55,7 @@ #include "DNA_color_types.h" #include "BKE_brush.h" +#include "BKE_cdderivedmesh.h" #include "BKE_context.h" #include "BKE_customdata.h" #include "BKE_DerivedMesh.h" @@ -106,15 +110,6 @@ * */ -/* ActiveData stores an Index into the mvert array of Mesh, plus Fade, which - stores how far the vertex is from the brush center, scaled to the range [0,1]. */ -typedef struct ActiveData { - struct ActiveData *next, *prev; - unsigned int Index; - float Fade; - float dist; -} ActiveData; - typedef enum StrokeFlags { CLIP_X = 1, CLIP_Y = 2, @@ -133,7 +128,6 @@ typedef struct StrokeCache { int flag; float clip_tolerance[3]; float initial_mouse[2]; - float depth; /* Variants */ float radius; @@ -142,6 +136,7 @@ typedef struct StrokeCache { float flip; float pressure; float mouse[2]; + float bstrength; float tex_mouse[2]; /* The rest is temporary storage that isn't saved as a property */ @@ -150,32 +145,24 @@ typedef struct StrokeCache { bglMats *mats; - short (*orig_norms)[3]; /* Copy of the mesh vertices' normals */ + /* Clean this up! */ + ViewContext *vc; + Brush *brush; + float (*face_norms)[3]; /* Copy of the mesh faces' normals */ float rotation; /* Texture rotation (radians) for anchored and rake modes */ int pixel_radius, previous_pixel_radius; - ListBase grab_active_verts[8]; /* The same list of verts is used throught grab stroke */ + PBVHNode **grab_active_nodes[8]; /* The same list of nodes is used throught grab stroke */ + int grab_active_totnode[8]; + float grab_active_location[8][3]; float grab_delta[3], grab_delta_symmetry[3]; - float old_grab_location[3]; + float old_grab_location[3], orig_grab_location[3]; int symmetry; /* Symmetry index between 0 and 7 */ float view_normal[3], view_normal_symmetry[3]; int last_rake[2]; /* Last location of updating rake rotation */ + int original; } StrokeCache; -typedef struct RectNode { - struct RectNode *next, *prev; - rcti r; -} RectNode; - -/* Used to store to 2D screen coordinates of each vertex in the mesh. */ -typedef struct ProjVert { - short co[2]; - - /* Used to mark whether a vertex is inside a rough bounding box - containing the brush. */ - char inside; -} ProjVert; - /* ===== OPENGL ===== * * Simple functions to get data from the GL @@ -192,7 +179,7 @@ static void projectf(bglMats *mats, const float v[3], float p[2]) p[1]= uy; } -static void project(bglMats *mats, const float v[3], short p[2]) +/*XXX: static void project(bglMats *mats, const float v[3], short p[2]) { float f[2]; projectf(mats, v, f); @@ -200,6 +187,337 @@ static void project(bglMats *mats, const float v[3], short p[2]) p[0]= f[0]; p[1]= f[1]; } +*/ + +/*** BVH Tree ***/ + +/* Get a screen-space rectangle of the modified area */ +int sculpt_get_redraw_rect(ARegion *ar, RegionView3D *rv3d, + Object *ob, rcti *rect) +{ + float bb_min[3], bb_max[3], pmat[4][4]; + int i, j, k; + + view3d_get_object_project_mat(rv3d, ob, pmat); + + BLI_pbvh_redraw_BB(ob->sculpt->tree, bb_min, bb_max); + + rect->xmin = rect->ymin = INT_MAX; + rect->xmax = rect->ymax = INT_MIN; + + if(bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2]) + return 0; + + for(i = 0; i < 2; ++i) { + for(j = 0; j < 2; ++j) { + for(k = 0; k < 2; ++k) { + float vec[3], proj[2]; + vec[0] = i ? bb_min[0] : bb_max[0]; + vec[1] = j ? bb_min[1] : bb_max[1]; + vec[2] = k ? bb_min[2] : bb_max[2]; + view3d_project_float(ar, vec, proj, pmat); + rect->xmin = MIN2(rect->xmin, proj[0]); + rect->xmax = MAX2(rect->xmax, proj[0]); + rect->ymin = MIN2(rect->ymin, proj[1]); + rect->ymax = MAX2(rect->ymax, proj[1]); + } + } + } + + return rect->xmin < rect->xmax && rect->ymin < rect->ymax; +} + +void sculpt_get_redraw_planes(float planes[4][4], ARegion *ar, + RegionView3D *rv3d, Object *ob) +{ + BoundBox *bb = MEM_callocN(sizeof(BoundBox), "sculpt boundbox"); + bglMats mats; + int i; + rcti rect; + + view3d_get_transformation(ar, rv3d, ob, &mats); + sculpt_get_redraw_rect(ar, rv3d,ob, &rect); + +#if 1 + /* use some extra space just in case */ + rect.xmin -= 2; + rect.xmax += 2; + rect.ymin -= 2; + rect.ymax += 2; +#else + /* it was doing this before, allows to redraw a smaller + part of the screen but also gives artifaces .. */ + rect.xmin += 2; + rect.xmax -= 2; + rect.ymin += 2; + rect.ymax -= 2; +#endif + + view3d_calculate_clipping(bb, planes, &mats, &rect); + + for(i = 0; i < 16; ++i) + ((float*)planes)[i] = -((float*)planes)[i]; + + MEM_freeN(bb); + + /* clear redraw flag from nodes */ + BLI_pbvh_update(ob->sculpt->tree, PBVH_UpdateRedraw, NULL); +} + +/************************** Undo *************************/ + +typedef struct SculptUndoNode { + struct SculptUndoNode *next, *prev; + + char idname[MAX_ID_NAME]; /* name instead of pointer*/ + void *node; /* only during push, not valid afterwards! */ + + float (*co)[3]; + short (*no)[3]; + int totvert; + + /* non-multires */ + int maxvert; /* to verify if totvert it still the same */ + int *index; /* to restore into right location */ + + /* multires */ + int maxgrid; /* same for grid */ + int gridsize; /* same for grid */ + int totgrid; /* to restore into right location */ + int *grids; /* to restore into right location */ +} SculptUndoNode; + +static void update_cb(PBVHNode *node, void *data) +{ + BLI_pbvh_node_mark_update(node); +} + +static void sculpt_undo_restore(bContext *C, ListBase *lb) +{ + Scene *scene = CTX_data_scene(C); + Object *ob = CTX_data_active_object(C); + DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0); + SculptSession *ss = ob->sculpt; + SculptUndoNode *unode; + MVert *mvert; + MultiresModifierData *mmd; + int *index; + int i, j, update= 0; + + sculpt_update_mesh_elements(scene, ob, 0); + + for(unode=lb->first; unode; unode=unode->next) { + if(!(strcmp(unode->idname, ob->id.name)==0)) + continue; + + if(unode->maxvert) { + /* regular mesh restore */ + if(ss->totvert != unode->maxvert) + continue; + + index= unode->index; + mvert= ss->mvert; + + for(i=0; i<unode->totvert; i++) { + swap_v3_v3(mvert[index[i]].co, unode->co[i]); + mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE; + } + } + else if(unode->maxgrid) { + /* multires restore */ + DMGridData **grids, *grid; + float (*co)[3]; + int gridsize; + + if(dm->getNumGrids(dm) != unode->maxgrid) + continue; + if(dm->getGridSize(dm) != unode->gridsize) + continue; + + grids= dm->getGridData(dm); + gridsize= dm->getGridSize(dm); + + co = unode->co; + for(j=0; j<unode->totgrid; j++) { + grid= grids[unode->grids[j]]; + + for(i=0; i<gridsize*gridsize; i++, co++) + swap_v3_v3(grid[i].co, co[0]); + } + } + + update= 1; + } + + if(update) { + if(ss->kb) sculpt_mesh_to_key(ss->ob, ss->kb); + if(ss->refkb) sculpt_key_to_mesh(ss->refkb, ob); + + /* we update all nodes still, should be more clever, but also + needs to work correct when exiting/entering sculpt mode and + the nodes get recreated, though in that case it could do all */ + BLI_pbvh_search_callback(ss->tree, NULL, NULL, update_cb, NULL); + BLI_pbvh_update(ss->tree, PBVH_UpdateBB|PBVH_UpdateOriginalBB|PBVH_UpdateRedraw, NULL); + + if((mmd=sculpt_multires_active(ob))) + multires_mark_as_modified(ob); + } +} + +static void sculpt_undo_free(ListBase *lb) +{ + SculptUndoNode *unode; + + for(unode=lb->first; unode; unode=unode->next) { + if(unode->co) + MEM_freeN(unode->co); + if(unode->no) + MEM_freeN(unode->no); + if(unode->index) + MEM_freeN(unode->index); + if(unode->grids) + MEM_freeN(unode->grids); + } +} + +static SculptUndoNode *sculpt_undo_get_node(SculptSession *ss, PBVHNode *node) +{ + ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH); + SculptUndoNode *unode; + + if(!lb) + return NULL; + + for(unode=lb->first; unode; unode=unode->next) + if(unode->node == node) + return unode; + + return NULL; +} + +static SculptUndoNode *sculpt_undo_push_node(SculptSession *ss, PBVHNode *node) +{ + ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH); + Object *ob= ss->ob; + SculptUndoNode *unode; + int totvert, allvert, totgrid, maxgrid, gridsize, *grids; + + /* list is manipulated by multiple threads, so we lock */ + BLI_lock_thread(LOCK_CUSTOM1); + + if((unode= sculpt_undo_get_node(ss, node))) { + BLI_unlock_thread(LOCK_CUSTOM1); + return unode; + } + + unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode"); + strcpy(unode->idname, ob->id.name); + unode->node= node; + + BLI_pbvh_node_num_verts(ss->tree, node, &totvert, &allvert); + BLI_pbvh_node_get_grids(ss->tree, node, &grids, &totgrid, + &maxgrid, &gridsize, NULL, NULL); + + unode->totvert= totvert; + /* we will use this while sculpting, is mapalloc slow to access then? */ + unode->co= MEM_mapallocN(sizeof(float)*3*allvert, "SculptUndoNode.co"); + unode->no= MEM_mapallocN(sizeof(short)*3*allvert, "SculptUndoNode.no"); + undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(short)*3 + sizeof(int))*allvert); + BLI_addtail(lb, unode); + + if(maxgrid) { + /* multires */ + unode->maxgrid= maxgrid; + unode->totgrid= totgrid; + unode->gridsize= gridsize; + unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids"); + } + else { + /* regular mesh */ + unode->maxvert= ss->totvert; + unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index"); + } + + BLI_unlock_thread(LOCK_CUSTOM1); + + /* copy threaded, hopefully this is the performance critical part */ + { + PBVHVertexIter vd; + + BLI_pbvh_vertex_iter_begin(ss->tree, node, vd, PBVH_ITER_ALL) { + copy_v3_v3(unode->co[vd.i], vd.co); + if(vd.no) VECCOPY(unode->no[vd.i], vd.no) + else normal_float_to_short_v3(unode->no[vd.i], vd.fno); + if(vd.vert_indices) unode->index[vd.i]= vd.vert_indices[vd.i]; + } + BLI_pbvh_vertex_iter_end; + } + + if(unode->grids) + memcpy(unode->grids, grids, sizeof(int)*totgrid); + + return unode; +} + +static void sculpt_undo_push_begin(SculptSession *ss, char *name) +{ + undo_paint_push_begin(UNDO_PAINT_MESH, name, + sculpt_undo_restore, sculpt_undo_free); +} + +static void sculpt_undo_push_end(SculptSession *ss) +{ + ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH); + SculptUndoNode *unode; + + /* we don't need normals in the undo stack */ + for(unode=lb->first; unode; unode=unode->next) { + if(unode->no) { + MEM_freeN(unode->no); + unode->no= NULL; + } + } + + undo_paint_push_end(UNDO_PAINT_MESH); +} + +void ED_sculpt_force_update(bContext *C) +{ + Object *ob= CTX_data_active_object(C); + + if(ob && (ob->mode & OB_MODE_SCULPT)) + multires_force_update(ob); +} + +/************************ Brush Testing *******************/ + +typedef struct SculptBrushTest { + float radius_squared; + float location[3]; + + float dist; +} SculptBrushTest; + +static void sculpt_brush_test_init(SculptSession *ss, SculptBrushTest *test) +{ + test->radius_squared= ss->cache->radius*ss->cache->radius; + copy_v3_v3(test->location, ss->cache->location); +} + +static int sculpt_brush_test(SculptBrushTest *test, float co[3]) +{ + float distsq, delta[3]; + + sub_v3_v3v3(delta, co, test->location); + distsq = INPR(delta, delta); + + if(distsq < test->radius_squared) { + test->dist = sqrt(distsq); + return 1; + } + + return 0; +} /* ===== Sculpting ===== * @@ -239,6 +557,169 @@ static float brush_strength(Sculpt *sd, StrokeCache *cache) } } +/* Uses symm to selectively flip any axis of a coordinate. */ +static void flip_coord(float out[3], float in[3], const char symm) +{ + if(symm & SCULPT_SYMM_X) + out[0]= -in[0]; + else + out[0]= in[0]; + if(symm & SCULPT_SYMM_Y) + out[1]= -in[1]; + else + out[1]= in[1]; + if(symm & SCULPT_SYMM_Z) + out[2]= -in[2]; + else + out[2]= in[2]; +} + +/* Get a pixel from the texcache at (px, py) */ +static unsigned char get_texcache_pixel(const SculptSession *ss, int px, int py) +{ + unsigned *p; + p = ss->texcache + py * ss->texcache_side + px; + return ((unsigned char*)(p))[0]; +} + +static float get_texcache_pixel_bilinear(const SculptSession *ss, float u, float v) +{ + int x, y, x2, y2; + const int tc_max = ss->texcache_side - 1; + float urat, vrat, uopp; + + if(u < 0) u = 0; + else if(u >= ss->texcache_side) u = tc_max; + if(v < 0) v = 0; + else if(v >= ss->texcache_side) v = tc_max; + + x = floor(u); + y = floor(v); + x2 = x + 1; + y2 = y + 1; + + if(x2 > ss->texcache_side) x2 = tc_max; + if(y2 > ss->texcache_side) y2 = tc_max; + + urat = u - x; + vrat = v - y; + uopp = 1 - urat; + + return ((get_texcache_pixel(ss, x, y) * uopp + + get_texcache_pixel(ss, x2, y) * urat) * (1 - vrat) + + (get_texcache_pixel(ss, x, y2) * uopp + + get_texcache_pixel(ss, x2, y2) * urat) * vrat) / 255.0; +} + +/* Return a multiplier for brush strength on a particular vertex. */ +static float tex_strength(SculptSession *ss, Brush *br, float *point, const float len) +{ + MTex *tex = NULL; + float avg= 1; + + if(br->texact >= 0) + tex = br->mtex[br->texact]; + + if(!tex) { + avg= 1; + } + else if(tex->brush_map_mode == MTEX_MAP_MODE_3D) { + float jnk; + + /* Get strength by feeding the vertex + location directly into a texture */ + externtex(tex, point, &avg, + &jnk, &jnk, &jnk, &jnk); + } + else if(ss->texcache) { + const float bsize= ss->cache->pixel_radius * 2; + const float rot= tex->rot + ss->cache->rotation; + int px, py; + float flip[3], point_2d[2]; + + /* If the active area is being applied for symmetry, flip it + across the symmetry axis in order to project it. This insures + that the brush texture will be oriented correctly. */ + copy_v3_v3(flip, point); + flip_coord(flip, flip, ss->cache->symmetry); + projectf(ss->cache->mats, flip, point_2d); + + /* For Tile and Drag modes, get the 2D screen coordinates of the + and scale them up or down to the texture size. */ + if(tex->brush_map_mode == MTEX_MAP_MODE_TILED) { + const int sx= (const int)tex->size[0]; + const int sy= (const int)tex->size[1]; + + float fx= point_2d[0]; + float fy= point_2d[1]; + + float angle= atan2(fy, fx) - rot; + float flen= sqrtf(fx*fx + fy*fy); + + if(rot<0.001 && rot>-0.001) { + px= point_2d[0]; + py= point_2d[1]; + } else { + px= flen * cos(angle) + 2000; + py= flen * sin(angle) + 2000; + } + if(sx != 1) + px %= sx-1; + if(sy != 1) + py %= sy-1; + avg= get_texcache_pixel_bilinear(ss, ss->texcache_side*px/sx, ss->texcache_side*py/sy); + } + else if(tex->brush_map_mode == MTEX_MAP_MODE_FIXED) { + float fx= (point_2d[0] - ss->cache->tex_mouse[0]) / bsize; + float fy= (point_2d[1] - ss->cache->tex_mouse[1]) / bsize; + + float angle= atan2(fy, fx) - rot; + float flen= sqrtf(fx*fx + fy*fy); + + fx = flen * cos(angle) + 0.5; + fy = flen * sin(angle) + 0.5; + + avg= get_texcache_pixel_bilinear(ss, fx * ss->texcache_side, fy * ss->texcache_side); + } + } + + avg*= brush_curve_strength(br, len, ss->cache->radius); /* Falloff curve */ + + return avg; +} + +typedef struct { + Sculpt *sd; + SculptSession *ss; + float radius_squared; + ListBase *active_verts; + float area_normal[3]; +} SculptSearchSphereData; + +/* Test AABB against sphere */ +static int sculpt_search_sphere_cb(PBVHNode *node, void *data_v) +{ + SculptSearchSphereData *data = data_v; + float *center = data->ss->cache->location, nearest[3]; + float t[3], bb_min[3], bb_max[3]; + int i; + + BLI_pbvh_node_get_BB(node, bb_min, bb_max); + + for(i = 0; i < 3; ++i) { + if(bb_min[i] > center[i]) + nearest[i] = bb_min[i]; + else if(bb_max[i] < center[i]) + nearest[i] = bb_max[i]; + else + nearest[i] = center[i]; + } + + sub_v3_v3v3(t, center, nearest); + + return t[0] * t[0] + t[1] * t[1] + t[2] * t[2] < data->radius_squared; +} + /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */ static void sculpt_clip(Sculpt *sd, SculptSession *ss, float *co, const float val[3]) { @@ -255,12 +736,8 @@ static void sculpt_clip(Sculpt *sd, SculptSession *ss, float *co, const float va } } -static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], const short no[3]) +static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], float fno[3]) { - float fno[3] = {no[0], no[1], no[2]}; - - normalize_v3(fno); - if((dot_v3v3(view_vec, fno)) > 0) { add_v3_v3v3(out, out, fno); } else { @@ -268,74 +745,119 @@ static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], cons } } -/* Currently only for the draw brush; finds average normal for all active - vertices */ -static void calc_area_normal(Sculpt *sd, SculptSession *ss, float out[3], const ListBase* active_verts) +/* For draw/layer/flatten; finds average normal for all active vertices */ +static void calc_area_normal(Sculpt *sd, SculptSession *ss, float area_normal[3], PBVHNode **nodes, int totnode) { - Brush *brush = paint_brush(&sd->paint); + PBVH *bvh= ss->tree; StrokeCache *cache = ss->cache; - ActiveData *node = active_verts->first; const int view = 0; /* XXX: should probably be a flag, not number: brush_type==SCULPT_TOOL_DRAW ? sculptmode_brush()->view : 0; */ - float out_flip[3]; - float *out_dir = cache->view_normal_symmetry; - - out[0]=out[1]=out[2] = out_flip[0]=out_flip[1]=out_flip[2] = 0; + float out[3] = {0.0f, 0.0f, 0.0f}; + float out_flip[3] = {0.0f, 0.0f, 0.0f}; + float out_dir[3]; + int n; + + copy_v3_v3(out_dir, cache->view_normal_symmetry); + + /* threaded loop over nodes */ + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; + SculptUndoNode *unode; + float fno[3]; + float nout[3] = {0.0f, 0.0f, 0.0f}; + float nout_flip[3] = {0.0f, 0.0f, 0.0f}; + + // XXX push instead of get for thread safety in draw + // brush .. lame, but also not harmful really + unode= sculpt_undo_push_node(ss, nodes[n]); + sculpt_brush_test_init(ss, &test); + + if(ss->cache->original) { + BLI_pbvh_vertex_iter_begin(bvh, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, unode->co[vd.i])) { + normal_short_to_float_v3(fno, unode->no[vd.i]); + add_norm_if(out_dir, nout, nout_flip, fno); + } + } + BLI_pbvh_vertex_iter_end; + } + else { + BLI_pbvh_vertex_iter_begin(bvh, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + if(vd.no) { + normal_short_to_float_v3(fno, vd.no); + add_norm_if(out_dir, nout, nout_flip, fno); + } + else + add_norm_if(out_dir, nout, nout_flip, vd.fno); + } + } + BLI_pbvh_vertex_iter_end; + } - if(brush->flag & BRUSH_ANCHORED) { - for(; node; node = node->next) - add_norm_if(out_dir, out, out_flip, cache->orig_norms[node->Index]); - } - else { - for(; node; node = node->next) - add_norm_if(out_dir, out, out_flip, ss->mvert[node->Index].no); + #pragma omp critical + { + /* we sum per node and add together later for threads */ + add_v3_v3v3(out, out, nout); + add_v3_v3v3(out_flip, out_flip, nout_flip); + } } if (out[0]==0.0 && out[1]==0.0 && out[2]==0.0) { - VECCOPY(out, out_flip); + copy_v3_v3(out, out_flip); } normalize_v3(out); - if(out_dir) { - out[0] = out_dir[0] * view + out[0] * (10-view); - out[1] = out_dir[1] * view + out[1] * (10-view); - out[2] = out_dir[2] * view + out[2] * (10-view); - } + out[0] = out_dir[0] * view + out[0] * (10-view); + out[1] = out_dir[1] * view + out[1] * (10-view); + out[2] = out_dir[2] * view + out[2] * (10-view); normalize_v3(out); + copy_v3_v3(area_normal, out); } -static void do_draw_brush(Sculpt *sd, SculptSession *ss, const ListBase* active_verts) +static void do_draw_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode) { - float area_normal[3]; - ActiveData *node= active_verts->first; - float* buffer; - - calc_area_normal(sd, ss, area_normal, active_verts); - - buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->vertices ):0; - - while(node){ - float *co= ss->mvert[node->Index].co; - - const float val[3]= {co[0]+area_normal[0]*ss->cache->radius*node->Fade*ss->cache->scale[0], - co[1]+area_normal[1]*ss->cache->radius*node->Fade*ss->cache->scale[1], - co[2]+area_normal[2]*ss->cache->radius*node->Fade*ss->cache->scale[2]}; - - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[node->Index]; - while( cur != 0 && cur->element != -1 ) { - sculpt_clip(sd, ss, &buffer[cur->element*3], val); - cur = cur->next; + Brush *brush = paint_brush(&sd->paint); + float offset[3], area_normal[3]; + float bstrength= ss->cache->bstrength; + int n; + + /* area normal */ + calc_area_normal(sd, ss, area_normal, nodes, totnode); + + /* offset with as much as possible factored in already */ + offset[0]= area_normal[0]*ss->cache->radius*ss->cache->scale[0]*bstrength; + offset[1]= area_normal[1]*ss->cache->radius*ss->cache->scale[1]*bstrength; + offset[2]= area_normal[2]*ss->cache->radius*ss->cache->scale[2]*bstrength; + + /* threaded loop over nodes */ + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; + + sculpt_undo_push_node(ss, nodes[n]); + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + /* offset vertex */ + float fade = tex_strength(ss, brush, vd.co, test.dist); + float val[3]= {vd.co[0] + offset[0]*fade, + vd.co[1] + offset[1]*fade, + vd.co[2] + offset[2]*fade}; + + sculpt_clip(sd, ss, vd.co, val); + if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE; } } + BLI_pbvh_vertex_iter_end; - sculpt_clip(sd, ss, co, val); - - node= node->next; + BLI_pbvh_node_mark_update(nodes[n]); } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->vertices ); } /* For the smooth brush, uses the neighboring vertices around vert to calculate @@ -382,195 +904,340 @@ static void neighbor_average(SculptSession *ss, float avg[3], const int vert) copy_v3_v3(avg, ss->mvert[vert].co); } -static void do_smooth_brush(Sculpt *s, SculptSession *ss, const ListBase* active_verts) +static void do_mesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node) { - ActiveData *node= active_verts->first; - float *buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->vertices ):0; - int i; + Brush *brush = paint_brush(&sd->paint); + float bstrength= ss->cache->bstrength; + PBVHVertexIter vd; + SculptBrushTest test; - for(i = 0; i < 2; ++i) { - while(node){ - float *co= ss->mvert[node->Index].co; + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, node, vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength; float avg[3], val[3]; - neighbor_average(ss, avg, node->Index); - val[0] = co[0]+(avg[0]-co[0])*node->Fade; - val[1] = co[1]+(avg[1]-co[1])*node->Fade; - val[2] = co[2]+(avg[2]-co[2])*node->Fade; + neighbor_average(ss, avg, vd.vert_indices[vd.i]); + val[0] = vd.co[0]+(avg[0]-vd.co[0])*fade; + val[1] = vd.co[1]+(avg[1]-vd.co[1])*fade; + val[2] = vd.co[2]+(avg[2]-vd.co[2])*fade; - sculpt_clip(s, ss, co, val); - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[node->Index]; - while( cur != 0 && cur->element != -1 ) { - sculpt_clip(s, ss, &buffer[cur->element*3], val); - cur = cur->next; + sculpt_clip(sd, ss, vd.co, val); + if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE; + } + } + BLI_pbvh_vertex_iter_end; +} + +static void do_multires_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node) +{ + Brush *brush = paint_brush(&sd->paint); + SculptBrushTest test; + DMGridData **griddata, *data; + DMGridAdjacency *gridadj, *adj; + float bstrength= ss->cache->bstrength; + float co[3], (*tmpgrid)[3]; + int v1, v2, v3, v4; + int *grid_indices, totgrid, gridsize, i, x, y; + + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_node_get_grids(ss->tree, node, &grid_indices, &totgrid, + NULL, &gridsize, &griddata, &gridadj); + + #pragma omp critical + tmpgrid= MEM_mallocN(sizeof(float)*3*gridsize*gridsize, "tmpgrid"); + + for(i = 0; i < totgrid; ++i) { + data = griddata[grid_indices[i]]; + adj = &gridadj[grid_indices[i]]; + + memset(tmpgrid, 0, sizeof(float)*3*gridsize*gridsize); + + /* average grid values */ + for(y = 0; y < gridsize-1; ++y) { + for(x = 0; x < gridsize-1; ++x) { + v1 = x + y*gridsize; + v2 = (x + 1) + y*gridsize; + v3 = (x + 1) + (y + 1)*gridsize; + v4 = x + (y + 1)*gridsize; + + cent_quad_v3(co, data[v1].co, data[v2].co, data[v3].co, data[v4].co); + mul_v3_fl(co, 0.25f); + + add_v3_v3(tmpgrid[v1], co); + add_v3_v3(tmpgrid[v2], co); + add_v3_v3(tmpgrid[v3], co); + add_v3_v3(tmpgrid[v4], co); + } + } + + /* blend with existing coordinates */ + for(y = 0; y < gridsize; ++y) { + for(x = 0; x < gridsize; ++x) { + if(x == 0 && adj->index[0] == -1) continue; + if(x == gridsize - 1 && adj->index[2] == -1) continue; + if(y == 0 && adj->index[3] == -1) continue; + if(y == gridsize - 1 && adj->index[1] == -1) continue; + + copy_v3_v3(co, data[x + y*gridsize].co); + + if(sculpt_brush_test(&test, co)) { + float fade = tex_strength(ss, brush, co, test.dist)*bstrength; + float avg[3], val[3]; + + copy_v3_v3(avg, tmpgrid[x + y*gridsize]); + if(x == 0 || x == gridsize - 1) + mul_v3_fl(avg, 2.0f); + if(y == 0 || y == gridsize - 1) + mul_v3_fl(avg, 2.0f); + + val[0] = co[0]+(avg[0]-co[0])*fade; + val[1] = co[1]+(avg[1]-co[1])*fade; + val[2] = co[2]+(avg[2]-co[2])*fade; + + sculpt_clip(sd, ss, data[x + y*gridsize].co, val); } } - node= node->next; } } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->vertices ); + + #pragma omp critical + MEM_freeN(tmpgrid); } -static void do_pinch_brush(Sculpt *s, SculptSession *ss, const ListBase* active_verts) +static void do_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode) { - ActiveData *node= active_verts->first; - float *buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->vertices ):0; + int iteration, n; + + for(iteration = 0; iteration < 2; ++iteration) { + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + sculpt_undo_push_node(ss, nodes[n]); + + if(ss->fmap) + do_mesh_smooth_brush(sd, ss, nodes[n]); + else + do_multires_smooth_brush(sd, ss, nodes[n]); + + BLI_pbvh_node_mark_update(nodes[n]); + } + + if(!ss->fmap) + multires_stitch_grids(ss->ob); + } +} - while(node) { - float *co= ss->mvert[node->Index].co; - const float val[3]= {co[0]+(ss->cache->location[0]-co[0])*node->Fade, - co[1]+(ss->cache->location[1]-co[1])*node->Fade, - co[2]+(ss->cache->location[2]-co[2])*node->Fade}; +static void do_pinch_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode) +{ + Brush *brush = paint_brush(&sd->paint); + float bstrength= ss->cache->bstrength; + int n; - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[node->Index]; - while( cur != 0 && cur->element != -1 ) { - sculpt_clip(s, ss, &buffer[cur->element*3], val); - cur = cur->next; + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; + + sculpt_undo_push_node(ss, nodes[n]); + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength; + float val[3]= {vd.co[0]+(test.location[0]-vd.co[0])*fade, + vd.co[1]+(test.location[1]-vd.co[1])*fade, + vd.co[2]+(test.location[2]-vd.co[2])*fade}; + + sculpt_clip(sd, ss, vd.co, val); + if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE; } } + BLI_pbvh_vertex_iter_end; - sculpt_clip(s, ss, co, val); - node= node->next; + BLI_pbvh_node_mark_update(nodes[n]); } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->vertices ); } -static void do_grab_brush(Sculpt *sd, SculptSession *ss) +static void do_grab_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode) { - ActiveData *node= ss->cache->grab_active_verts[ss->cache->symmetry].first; - float add[3]; + Brush *brush = paint_brush(&sd->paint); + float bstrength= ss->cache->bstrength; float grab_delta[3]; - float *buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->vertices ):0; + int n; copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry); - - while(node) { - float *co= ss->mvert[node->Index].co; + + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; + float (*origco)[3]; - copy_v3_v3(add, grab_delta); - mul_v3_fl(add, node->Fade); - add_v3_v3v3(add, add, co); - - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[node->Index]; - while( cur != 0 && cur->element != -1 ) { - sculpt_clip(sd, ss, &buffer[cur->element*3], add); - cur = cur->next; + origco= sculpt_undo_push_node(ss, nodes[n])->co; + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, origco[vd.i])) { + float fade = tex_strength(ss, brush, origco[vd.i], test.dist)*bstrength; + float add[3]= {vd.co[0]+fade*grab_delta[0], + vd.co[1]+fade*grab_delta[1], + vd.co[2]+fade*grab_delta[2]}; + + sculpt_clip(sd, ss, vd.co, add); + if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE; } } + BLI_pbvh_vertex_iter_end; - sculpt_clip(sd, ss, co, add); - - node= node->next; + BLI_pbvh_node_mark_update(nodes[n]); } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->vertices ); - } -static void do_layer_brush(Sculpt *sd, SculptSession *ss, const ListBase *active_verts) +static void do_layer_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode) { - float area_normal[3]; - ActiveData *node= active_verts->first; - float *buffer; + Brush *brush = paint_brush(&sd->paint); + float bstrength= ss->cache->bstrength; + float area_normal[3], offset[3]; float lim= ss->cache->radius / 4; + int n; + + /* XXX not working yet for multires */ + if(!ss->mvert) + return; if(ss->cache->flip) lim = -lim; - calc_area_normal(sd, ss, area_normal, active_verts); + calc_area_normal(sd, ss, area_normal, nodes, totnode); - buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->vertices ):0; - while(node){ - float *disp= &ss->layer_disps[node->Index]; - float *co= ss->mvert[node->Index].co; - float val[3]; - - *disp+= node->Fade; - - /* Don't let the displacement go past the limit */ - if((lim < 0 && *disp < lim) || (lim > 0 && *disp > lim)) - *disp = lim; + offset[0]= ss->cache->scale[0]*area_normal[0]; + offset[1]= ss->cache->scale[1]*area_normal[1]; + offset[2]= ss->cache->scale[2]*area_normal[2]; + + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; + float (*origco)[3]; - val[0] = ss->mesh_co_orig[node->Index][0]+area_normal[0] * *disp*ss->cache->scale[0]; - val[1] = ss->mesh_co_orig[node->Index][1]+area_normal[1] * *disp*ss->cache->scale[1]; - val[2] = ss->mesh_co_orig[node->Index][2]+area_normal[2] * *disp*ss->cache->scale[2]; - - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[node->Index]; - while( cur != 0 && cur->element != -1 ) { - sculpt_clip(sd, ss, &buffer[cur->element*3], val); - cur = cur->next; + origco= sculpt_undo_push_node(ss, nodes[n])->co; + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength; + int index= vd.vert_indices[vd.i]; + float *disp= &ss->layer_disps[index]; + float val[3]; + + *disp+= fade; + + /* Don't let the displacement go past the limit */ + if((lim < 0 && *disp < lim) || (lim > 0 && *disp > lim)) + *disp = lim; + + if(ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) { + /* persistent base */ + val[0] = ss->layer_co[index][0] + (*disp)*offset[0]; + val[1] = ss->layer_co[index][1] + (*disp)*offset[1]; + val[2] = ss->layer_co[index][2] + (*disp)*offset[2]; + } + else { + val[0] = origco[vd.i][0] + (*disp)*offset[0]; + val[1] = origco[vd.i][1] + (*disp)*offset[1]; + val[2] = origco[vd.i][2] + (*disp)*offset[2]; + } + + sculpt_clip(sd, ss, vd.co, val); + if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE; } } + BLI_pbvh_vertex_iter_end; - sculpt_clip(sd, ss, co, val); - - node= node->next; + BLI_pbvh_node_mark_update(nodes[n]); } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->vertices ); } -static void do_inflate_brush(Sculpt *s, SculptSession *ss, const ListBase *active_verts) +static void do_inflate_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode) { - ActiveData *node= active_verts->first; - float add[3]; - float *buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->vertices ):0; - - while(node) { - float *co= ss->mvert[node->Index].co; - short *no= ss->mvert[node->Index].no; + Brush *brush = paint_brush(&sd->paint); + float bstrength= ss->cache->bstrength; + int n; - add[0]= no[0]/ 32767.0f; - add[1]= no[1]/ 32767.0f; - add[2]= no[2]/ 32767.0f; - mul_v3_fl(add, node->Fade * ss->cache->radius); - add[0]*= ss->cache->scale[0]; - add[1]*= ss->cache->scale[1]; - add[2]*= ss->cache->scale[2]; - add_v3_v3v3(add, add, co); + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[node->Index]; - while( cur != 0 && cur->element != -1 ) { - sculpt_clip(s, ss, &buffer[cur->element*3], add); - cur = cur->next; + sculpt_undo_push_node(ss, nodes[n]); + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength; + float add[3]; + + if(vd.fno) copy_v3_v3(add, vd.fno); + else normal_short_to_float_v3(add, vd.no); + + mul_v3_fl(add, fade * ss->cache->radius); + add[0]*= ss->cache->scale[0]; + add[1]*= ss->cache->scale[1]; + add[2]*= ss->cache->scale[2]; + add_v3_v3v3(add, add, vd.co); + + sculpt_clip(sd, ss, vd.co, add); + if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE; } } + BLI_pbvh_vertex_iter_end; - sculpt_clip(s, ss, co, add); - - node= node->next; + BLI_pbvh_node_mark_update(nodes[n]); } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->vertices ); } -static void calc_flatten_center(SculptSession *ss, ActiveData *node, float co[3]) +static void calc_flatten_center(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, float co[3]) { - ActiveData *outer[FLATTEN_SAMPLE_SIZE]; - int i; + float outer_dist[FLATTEN_SAMPLE_SIZE]; + float outer_co[FLATTEN_SAMPLE_SIZE][3]; + int i, n; - for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) - outer[i] = node; + for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) { + zero_v3(outer_co[i]); + outer_dist[i]= -1.0f; + } + + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; + int j; - for(; node; node = node->next) { - for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) { - if(node->dist > outer[i]->dist) { - outer[i] = node; - break; + sculpt_undo_push_node(ss, nodes[n]); + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + for(j = 0; j < FLATTEN_SAMPLE_SIZE; ++j) { + if(test.dist > outer_dist[j]) { + copy_v3_v3(outer_co[j], vd.co); + outer_dist[j] = test.dist; + break; + } + } } } + BLI_pbvh_vertex_iter_end; + + BLI_pbvh_node_mark_update(nodes[n]); } co[0] = co[1] = co[2] = 0.0f; for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) - add_v3_v3v3(co, co, ss->mvert[outer[i]->Index].co); + if(outer_dist[i] >= 0.0f) + add_v3_v3v3(co, co, outer_co[i]); mul_v3_fl(co, 1.0f / FLATTEN_SAMPLE_SIZE); } @@ -602,16 +1269,17 @@ static int plane_point_side(float co[3], float plane_normal[3], float plane_cent return d <= 0.0f; } -static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, const ListBase *active_verts, int clay) +static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, int clay) { - ActiveData *node= active_verts->first; /* area_normal and cntr define the plane towards which vertices are squashed */ + Brush *brush = paint_brush(&sd->paint); + float bstrength= ss->cache->bstrength; float area_normal[3]; float cntr[3], cntr2[3], bstr = 0; - int flip = 0; - float *buffer; - calc_area_normal(sd, ss, area_normal, active_verts); - calc_flatten_center(ss, node, cntr); + int n, flip = 0; + + calc_area_normal(sd, ss, area_normal, nodes, totnode); + calc_flatten_center(sd, ss, nodes, totnode, cntr); if(clay) { bstr= brush_strength(sd, ss->cache); @@ -622,295 +1290,123 @@ static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, const ListBase flip = bstr < 0; } - buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->vertices ):0; - - while(node){ - float *co= ss->mvert[node->Index].co; - float intr[3], val[3]; + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + PBVHVertexIter vd; + SculptBrushTest test; - if(!clay || plane_point_side(co, area_normal, cntr2, flip)) { - /* Find the intersection between squash-plane and vertex (along the area normal) */ - point_plane_project(intr, co, area_normal, cntr); - - sub_v3_v3v3(val, intr, co); - - if(clay) { - if(bstr > FLT_EPSILON) - mul_v3_fl(val, node->Fade / bstr); - else - mul_v3_fl(val, node->Fade); - /* Clay displacement */ - val[0]+=area_normal[0] * ss->cache->scale[0]*node->Fade; - val[1]+=area_normal[1] * ss->cache->scale[1]*node->Fade; - val[2]+=area_normal[2] * ss->cache->scale[2]*node->Fade; - } - else - mul_v3_fl(val, fabs(node->Fade)); + sculpt_undo_push_node(ss, nodes[n]); + sculpt_brush_test_init(ss, &test); + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + if(sculpt_brush_test(&test, vd.co)) { + float intr[3], val[3]; + + if(!clay || plane_point_side(vd.co, area_normal, cntr2, flip)) { + const float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength; + + /* Find the intersection between squash-plane and vertex (along the area normal) */ + point_plane_project(intr, vd.co, area_normal, cntr); + + sub_v3_v3v3(val, intr, vd.co); + + if(clay) { + if(bstr > FLT_EPSILON) + mul_v3_fl(val, fade / bstr); + else + mul_v3_fl(val, fade); + /* Clay displacement */ + val[0]+=area_normal[0] * ss->cache->scale[0]*fade; + val[1]+=area_normal[1] * ss->cache->scale[1]*fade; + val[2]+=area_normal[2] * ss->cache->scale[2]*fade; + } + else + mul_v3_fl(val, fabs(fade)); - add_v3_v3v3(val, val, co); + add_v3_v3v3(val, val, vd.co); - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[node->Index]; - while( cur != 0 && cur->element != -1 ) { - sculpt_clip(sd, ss, &buffer[cur->element*3], val); - cur = cur->next; + sculpt_clip(sd, ss, vd.co, val); + if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE; } - } - sculpt_clip(sd, ss, co, val); - - } - - node= node->next; - } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->vertices ); -} - -/* Uses symm to selectively flip any axis of a coordinate. */ -static void flip_coord(float out[3], float in[3], const char symm) -{ - if(symm & SCULPT_SYMM_X) - out[0]= -in[0]; - else - out[0]= in[0]; - if(symm & SCULPT_SYMM_Y) - out[1]= -in[1]; - else - out[1]= in[1]; - if(symm & SCULPT_SYMM_Z) - out[2]= -in[2]; - else - out[2]= in[2]; -} - -/* Get a pixel from the texcache at (px, py) */ -static unsigned char get_texcache_pixel(const SculptSession *ss, int px, int py) -{ - unsigned *p; - p = ss->texcache + py * ss->texcache_side + px; - return ((unsigned char*)(p))[0]; -} - -static float get_texcache_pixel_bilinear(const SculptSession *ss, float u, float v) -{ - int x, y, x2, y2; - const int tc_max = ss->texcache_side - 1; - float urat, vrat, uopp; - - if(u < 0) u = 0; - else if(u >= ss->texcache_side) u = tc_max; - if(v < 0) v = 0; - else if(v >= ss->texcache_side) v = tc_max; - - x = floor(u); - y = floor(v); - x2 = x + 1; - y2 = y + 1; - - if(x2 > ss->texcache_side) x2 = tc_max; - if(y2 > ss->texcache_side) y2 = tc_max; - - urat = u - x; - vrat = v - y; - uopp = 1 - urat; - - return ((get_texcache_pixel(ss, x, y) * uopp + - get_texcache_pixel(ss, x2, y) * urat) * (1 - vrat) + - (get_texcache_pixel(ss, x, y2) * uopp + - get_texcache_pixel(ss, x2, y2) * urat) * vrat) / 255.0; -} - -/* Return a multiplier for brush strength on a particular vertex. */ -static float tex_strength(Sculpt *sd, SculptSession *ss, float *point, const float len) -{ - Brush *br = paint_brush(&sd->paint); - MTex *tex = NULL; - float avg= 1; - - if(br->texact >= 0) - tex = br->mtex[br->texact]; - - if(!tex) { - avg= 1; - } - else if(tex->brush_map_mode == MTEX_MAP_MODE_3D) { - float jnk; - - /* Get strength by feeding the vertex - location directly into a texture */ - externtex(tex, point, &avg, - &jnk, &jnk, &jnk, &jnk); - } - else if(ss->texcache) { - const float bsize= ss->cache->pixel_radius * 2; - const float rot= tex->rot + ss->cache->rotation; - int px, py; - float flip[3], point_2d[2]; - - /* If the active area is being applied for symmetry, flip it - across the symmetry axis in order to project it. This insures - that the brush texture will be oriented correctly. */ - copy_v3_v3(flip, point); - flip_coord(flip, flip, ss->cache->symmetry); - projectf(ss->cache->mats, flip, point_2d); - - /* For Tile and Drag modes, get the 2D screen coordinates of the - and scale them up or down to the texture size. */ - if(tex->brush_map_mode == MTEX_MAP_MODE_TILED) { - const int sx= (const int)tex->size[0]; - const int sy= (const int)tex->size[1]; - - float fx= point_2d[0]; - float fy= point_2d[1]; - - float angle= atan2(fy, fx) - rot; - float flen= sqrtf(fx*fx + fy*fy); - - if(rot<0.001 && rot>-0.001) { - px= point_2d[0]; - py= point_2d[1]; - } else { - px= flen * cos(angle) + 2000; - py= flen * sin(angle) + 2000; } - if(sx != 1) - px %= sx-1; - if(sy != 1) - py %= sy-1; - avg= get_texcache_pixel_bilinear(ss, ss->texcache_side*px/sx, ss->texcache_side*py/sy); - } - else if(tex->brush_map_mode == MTEX_MAP_MODE_FIXED) { - float fx= (point_2d[0] - ss->cache->tex_mouse[0]) / bsize; - float fy= (point_2d[1] - ss->cache->tex_mouse[1]) / bsize; - - float angle= atan2(fy, fx) - rot; - float flen= sqrtf(fx*fx + fy*fy); - - fx = flen * cos(angle) + 0.5; - fy = flen * sin(angle) + 0.5; - - avg= get_texcache_pixel_bilinear(ss, fx * ss->texcache_side, fy * ss->texcache_side); } - } - - avg*= brush_curve_strength(br, len, ss->cache->radius); /* Falloff curve */ - - return avg; -} + BLI_pbvh_vertex_iter_end; -/* Mark area around the brush as damaged. projverts are marked if they are - inside the area and the damaged rectangle in 2D screen coordinates is - added to damaged_rects. */ -static void sculpt_add_damaged_rect(SculptSession *ss) -{ - short p[2]; - RectNode *rn= MEM_mallocN(sizeof(RectNode),"RectNode"); - const float radius = MAX2(ss->cache->pixel_radius, ss->cache->previous_pixel_radius); - unsigned i; - - /* Find center */ - project(ss->cache->mats, ss->cache->location, p); - rn->r.xmin= p[0] - radius; - rn->r.ymin= p[1] - radius; - rn->r.xmax= p[0] + radius; - rn->r.ymax= p[1] + radius; - - BLI_addtail(&ss->damaged_rects, rn); - - /* Update insides */ - for(i=0; i<ss->totvert; ++i) { - if(!ss->projverts[i].inside) { - if(ss->projverts[i].co[0] > rn->r.xmin && ss->projverts[i].co[1] > rn->r.ymin && - ss->projverts[i].co[0] < rn->r.xmax && ss->projverts[i].co[1] < rn->r.ymax) { - ss->projverts[i].inside= 1; - } - } - // XXX: remember to fix this! - // temporary pass - ss->projverts[i].inside = 1; + BLI_pbvh_node_mark_update(nodes[n]); } } static void do_brush_action(Sculpt *sd, SculptSession *ss, StrokeCache *cache) { + SculptSearchSphereData data; Brush *brush = paint_brush(&sd->paint); - float av_dist; - ListBase active_verts={0,0}; - ListBase *grab_active_verts = &ss->cache->grab_active_verts[ss->cache->symmetry]; - ActiveData *adata= 0; - float *vert; - const float bstrength= brush_strength(sd, cache); - Brush *b = brush; - int i; + PBVHNode **nodes= NULL; + int totnode; - sculpt_add_damaged_rect(ss); - - /* Build a list of all vertices that are potentially within the brush's - area of influence. Only do this once for the grab brush. */ - if((b->sculpt_tool != SCULPT_TOOL_GRAB) || cache->first_time) { - for(i=0; i<ss->totvert; ++i) { - /* Projverts.inside provides a rough bounding box */ - if(ss->multires || ss->projverts[i].inside) { - //vert= ss->vertexcosnos ? &ss->vertexcosnos[i*6] : a->verts[i].co; - vert= ss->mvert[i].co; - av_dist= len_v3v3(ss->cache->location, vert); - if(av_dist < cache->radius) { - adata= (ActiveData*)MEM_mallocN(sizeof(ActiveData), "ActiveData"); - - adata->Index = i; - /* Fade is used to store the final strength at which the brush - should modify a particular vertex. */ - adata->Fade= tex_strength(sd, ss, vert, av_dist) * bstrength; - adata->dist = av_dist; - - if(b->sculpt_tool == SCULPT_TOOL_GRAB && cache->first_time) - BLI_addtail(grab_active_verts, adata); - else - BLI_addtail(&active_verts, adata); - } - } + data.ss = ss; + data.sd = sd; + data.radius_squared = ss->cache->radius * ss->cache->radius; + + /* Build a list of all nodes that are potentially within the brush's + area of influence */ + if(brush->sculpt_tool == SCULPT_TOOL_GRAB) { + if(cache->first_time) { + /* For the grab tool we store these nodes once in the beginning + and then reuse them. */ + BLI_pbvh_search_gather(ss->tree, sculpt_search_sphere_cb, &data, + &nodes, &totnode); + + ss->cache->grab_active_nodes[ss->cache->symmetry]= nodes; + ss->cache->grab_active_totnode[ss->cache->symmetry]= totnode; + copy_v3_v3(ss->cache->grab_active_location[ss->cache->symmetry], ss->cache->location); + } + else { + nodes= ss->cache->grab_active_nodes[ss->cache->symmetry]; + totnode= ss->cache->grab_active_totnode[ss->cache->symmetry]; + copy_v3_v3(ss->cache->location, ss->cache->grab_active_location[ss->cache->symmetry]); } } + else { + BLI_pbvh_search_gather(ss->tree, sculpt_search_sphere_cb, &data, + &nodes, &totnode); + } /* Only act if some verts are inside the brush area */ - if(active_verts.first || (b->sculpt_tool == SCULPT_TOOL_GRAB && grab_active_verts->first)) { + if(totnode) { /* Apply one type of brush action */ - switch(b->sculpt_tool){ + switch(brush->sculpt_tool){ case SCULPT_TOOL_DRAW: - do_draw_brush(sd, ss, &active_verts); + do_draw_brush(sd, ss, nodes, totnode); break; case SCULPT_TOOL_SMOOTH: - do_smooth_brush(sd, ss, &active_verts); + do_smooth_brush(sd, ss, nodes, totnode); break; case SCULPT_TOOL_PINCH: - do_pinch_brush(sd, ss, &active_verts); + do_pinch_brush(sd, ss, nodes, totnode); break; case SCULPT_TOOL_INFLATE: - do_inflate_brush(sd, ss, &active_verts); + do_inflate_brush(sd, ss, nodes, totnode); break; case SCULPT_TOOL_GRAB: - do_grab_brush(sd, ss); + do_grab_brush(sd, ss, nodes, totnode); break; case SCULPT_TOOL_LAYER: - do_layer_brush(sd, ss, &active_verts); + do_layer_brush(sd, ss, nodes, totnode); break; case SCULPT_TOOL_FLATTEN: - do_flatten_clay_brush(sd, ss, &active_verts, 0); + do_flatten_clay_brush(sd, ss, nodes, totnode, 0); break; case SCULPT_TOOL_CLAY: - do_flatten_clay_brush(sd, ss, &active_verts, 1); + do_flatten_clay_brush(sd, ss, nodes, totnode, 1); + break; } - /* Copy the modified vertices from mesh to the active key */ + /* copy the modified vertices from mesh to the active key */ if(ss->kb) mesh_to_key(ss->ob->data, ss->kb); - - if(ss->vertexcosnos && !ss->multires) - BLI_freelistN(&active_verts); - else { - if(b->sculpt_tool != SCULPT_TOOL_GRAB) - addlisttolist(&ss->damaged_verts, &active_verts); - } - } + + if((brush->sculpt_tool != SCULPT_TOOL_GRAB) && nodes) + MEM_freeN(nodes); + } } /* Flip all the editdata across the axis/axes specified by symm. Used to @@ -932,6 +1428,7 @@ static void do_symmetrical_brush_actions(Sculpt *sd, SculptSession *ss) copy_v3_v3(cache->location, cache->true_location); copy_v3_v3(cache->grab_delta_symmetry, cache->grab_delta); cache->symmetry = 0; + cache->bstrength = brush_strength(sd, cache); do_brush_action(sd, ss, cache); for(i = 1; i <= symm; ++i) { @@ -944,110 +1441,6 @@ static void do_symmetrical_brush_actions(Sculpt *sd, SculptSession *ss) cache->first_time = 0; } -static void add_face_normal(vec3f *norm, MVert *mvert, const MFace* face, float *fn) -{ - vec3f c= {mvert[face->v1].co[0],mvert[face->v1].co[1],mvert[face->v1].co[2]}; - vec3f b= {mvert[face->v2].co[0],mvert[face->v2].co[1],mvert[face->v2].co[2]}; - vec3f a= {mvert[face->v3].co[0],mvert[face->v3].co[1],mvert[face->v3].co[2]}; - vec3f s1, s2; - float final[3]; - - sub_v3_v3v3(&s1.x,&a.x,&b.x); - sub_v3_v3v3(&s2.x,&c.x,&b.x); - - final[0] = s1.y * s2.z - s1.z * s2.y; - final[1] = s1.z * s2.x - s1.x * s2.z; - final[2] = s1.x * s2.y - s1.y * s2.x; - - if(fn) - copy_v3_v3(fn, final); - - norm->x+= final[0]; - norm->y+= final[1]; - norm->z+= final[2]; -} - -static void update_damaged_vert(SculptSession *ss, ListBase *lb) -{ - ActiveData *vert; - - float *buffer = ss->drawobject!=0?(float *)GPU_buffer_lock( ss->drawobject->normals ):0; - for(vert= lb->first; vert; vert= vert->next) { - vec3f norm= {0,0,0}; - IndexNode *face= ss->fmap[vert->Index].first; - - while(face){ - float *fn = NULL; - if(ss->face_normals) - fn = &ss->face_normals[face->index*3]; - add_face_normal(&norm, ss->mvert, &ss->mface[face->index], fn); - face= face->next; - } - normalize_v3(&norm.x); - - ss->mvert[vert->Index].no[0]=norm.x*32767; - ss->mvert[vert->Index].no[1]=norm.y*32767; - ss->mvert[vert->Index].no[2]=norm.z*32767; - - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[vert->Index]; - while( cur != 0 && cur->element != -1 ) { - int i = ss->drawobject->faceRemap[cur->element/3]; - if( ss->mface[i].flag & ME_SMOOTH ) { - VECCOPY(&buffer[cur->element*3],ss->mvert[vert->Index].no); - } - else { - float norm[3]; - if( ss->mface[i].v4 ) - normal_quad_v3( norm,ss->mvert[ss->mface[i].v1].co, ss->mvert[ss->mface[i].v2].co, ss->mvert[ss->mface[i].v3].co, ss->mvert[ss->mface[i].v4].co); - else - normal_tri_v3( norm,ss->mvert[ss->mface[i].v1].co, ss->mvert[ss->mface[i].v2].co, ss->mvert[ss->mface[i].v3].co); - VECCOPY(&buffer[(cur->element-cur->element%3)*3],norm); - VECCOPY(&buffer[(cur->element-cur->element%3+1)*3],norm); - VECCOPY(&buffer[(cur->element-cur->element%3+2)*3],norm); - - /* maybe this was a quad - need to update the other triangle of the quad */ - if( ss->drawobject->faceRemap[cur->element/3-1] == i ) { - VECCOPY(&buffer[(cur->element-cur->element%3-3)*3],norm); - VECCOPY(&buffer[(cur->element-cur->element%3-2)*3],norm); - VECCOPY(&buffer[(cur->element-cur->element%3-1)*3],norm); - } - if( ss->drawobject->faceRemap[cur->element/3+1] == i ) { - VECCOPY(&buffer[(cur->element-cur->element%3+3)*3],norm); - VECCOPY(&buffer[(cur->element-cur->element%3+4)*3],norm); - VECCOPY(&buffer[(cur->element-cur->element%3+5)*3],norm); - } - } - - //VECCOPY(&buffer[cur->element*3],ss->mvert[vert->Index].no); - cur = cur->next; - } - } - } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->normals ); -} - -static void calc_damaged_verts(SculptSession *ss) -{ - int i; - - for(i=0; i<8; ++i) - update_damaged_vert(ss, &ss->cache->grab_active_verts[i]); - update_damaged_vert(ss, &ss->damaged_verts); - BLI_freelistN(&ss->damaged_verts); - ss->damaged_verts.first = ss->damaged_verts.last = NULL; -} - -#if 0 -static void projverts_clear_inside(SculptSession *ss) -{ - int i; - for(i = 0; i < ss->totvert; ++i) - ss->projverts[i].inside = 0; -} -#endif - static void sculpt_update_tex(Sculpt *sd, SculptSession *ss) { Brush *brush = paint_brush(&sd->paint); @@ -1065,28 +1458,15 @@ static void sculpt_update_tex(Sculpt *sd, SculptSession *ss) } } -static void sculptmode_update_all_projverts(SculptSession *ss) -{ - unsigned i; - - if(!ss->projverts) - ss->projverts = MEM_mallocN(sizeof(ProjVert)*ss->totvert,"ProjVerts"); - - for(i=0; i<ss->totvert; ++i) { - project(ss->cache->mats, ss->vertexcosnos ? &ss->vertexcosnos[i * 6] : ss->mvert[i].co, - ss->projverts[i].co); - ss->projverts[i].inside= 0; - } -} - /* Checks whether full update mode (slower) needs to be used to work with modifiers */ char sculpt_modifiers_active(Object *ob) { ModifierData *md; for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) { - if(modifier_isEnabled(md, eModifierMode_Realtime) && md->type != eModifierType_Multires) - return 1; + if(modifier_isEnabled(md, eModifierMode_Realtime)) + if(!ELEM(md->type, eModifierType_Multires, eModifierType_ShapeKey)) + return 1; } return 0; @@ -1094,7 +1474,7 @@ char sculpt_modifiers_active(Object *ob) /* Sculpt mode handles multires differently from regular meshes, but only if it's the last modifier on the stack and it is not on the first level */ -static struct MultiresModifierData *sculpt_multires_active(Object *ob) +struct MultiresModifierData *sculpt_multires_active(Object *ob) { ModifierData *md, *nmd; @@ -1116,22 +1496,46 @@ static struct MultiresModifierData *sculpt_multires_active(Object *ob) return NULL; } -static void sculpt_update_mesh_elements(bContext *C) +void sculpt_key_to_mesh(KeyBlock *kb, Object *ob) { - Object *ob = CTX_data_active_object(C); - SculptSession *ss = ob->sculpt; - int oldtotvert = ss->totvert; - DerivedMesh *dm = mesh_get_derived_final(CTX_data_scene(C), ob, CD_MASK_BAREMESH); + Mesh *me= ob->data; + + key_to_mesh(kb, me); + mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL); +} + +void sculpt_mesh_to_key(Object *ob, KeyBlock *kb) +{ + Mesh *me= ob->data; + + mesh_to_key(me, kb); +} +void sculpt_update_mesh_elements(Scene *scene, Object *ob, int need_fmap) +{ + DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0); + SculptSession *ss = ob->sculpt; + ss->ob= ob; + if((ob->shapeflag & OB_SHAPE_LOCK) && !sculpt_multires_active(ob)) { + ss->kb= ob_get_keyblock(ob); + ss->refkb= ob_get_reference_keyblock(ob); + } + else { + ss->kb= NULL; + ss->refkb= NULL; + } + + /* need to make PBVH with shape key coordinates */ + if(ss->kb) sculpt_key_to_mesh(ss->kb, ss->ob); + if((ss->multires = sculpt_multires_active(ob))) { - //DerivedMesh *dm = mesh_get_derived_final(CTX_data_scene(C), ob, CD_MASK_BAREMESH); ss->totvert = dm->getNumVerts(dm); ss->totface = dm->getNumFaces(dm); - ss->mvert = dm->getVertDataArray(dm, CD_MVERT); - ss->mface = dm->getFaceDataArray(dm, CD_MFACE); - ss->face_normals = dm->getFaceDataArray(dm, CD_NORMAL); + ss->mvert= NULL; + ss->mface= NULL; + ss->face_normals= NULL; } else { Mesh *me = get_mesh(ob); @@ -1141,31 +1545,9 @@ static void sculpt_update_mesh_elements(bContext *C) ss->mface = me->mface; ss->face_normals = NULL; } - if( GPU_buffer_legacy( dm ) ) { - ss->drawobject = 0; - } - else { - ss->drawobject = dm->drawObject; - } - if(ss->totvert != oldtotvert) { - if(ss->projverts) MEM_freeN(ss->projverts); - ss->projverts = NULL; - - if(ss->fmap) MEM_freeN(ss->fmap); - if(ss->fmap_mem) MEM_freeN(ss->fmap_mem); - create_vert_face_map(&ss->fmap, &ss->fmap_mem, ss->mface, ss->totvert, ss->totface); - ss->fmap_size = ss->totvert; - } - - if((ob->shapeflag & OB_SHAPE_LOCK) && !sculpt_multires_active(ob)) { - ss->kb= ob_get_keyblock(ob); - ss->refkb= ob_get_reference_keyblock(ob); - } - else { - ss->kb= NULL; - ss->refkb= NULL; - } + ss->tree = dm->getPBVH(ob, dm); + ss->fmap = (need_fmap && dm->getFaceMap)? dm->getFaceMap(dm): NULL; } static int sculpt_mode_poll(bContext *C) @@ -1179,27 +1561,27 @@ int sculpt_poll(bContext *C) return sculpt_mode_poll(C) && paint_poll(C); } -static void sculpt_undo_push(bContext *C, Sculpt *sd) +static char *sculpt_tool_name(Sculpt *sd) { Brush *brush = paint_brush(&sd->paint); switch(brush->sculpt_tool) { case SCULPT_TOOL_DRAW: - ED_undo_push(C, "Draw Brush"); break; + return "Draw Brush"; break; case SCULPT_TOOL_SMOOTH: - ED_undo_push(C, "Smooth Brush"); break; + return "Smooth Brush"; break; case SCULPT_TOOL_PINCH: - ED_undo_push(C, "Pinch Brush"); break; + return "Pinch Brush"; break; case SCULPT_TOOL_INFLATE: - ED_undo_push(C, "Inflate Brush"); break; + return "Inflate Brush"; break; case SCULPT_TOOL_GRAB: - ED_undo_push(C, "Grab Brush"); break; + return "Grab Brush"; break; case SCULPT_TOOL_LAYER: - ED_undo_push(C, "Layer Brush"); break; + return "Layer Brush"; break; case SCULPT_TOOL_FLATTEN: - ED_undo_push(C, "Flatten Brush"); break; + return "Flatten Brush"; break; default: - ED_undo_push(C, "Sculpting"); break; + return "Sculpting"; break; } } @@ -1251,28 +1633,26 @@ static void SCULPT_OT_radial_control(wmOperatorType *ot) /**** Operator for applying a stroke (various attributes including mouse path) using the current brush. ****/ -static float unproject_brush_radius(SculptSession *ss, float offset) +static float unproject_brush_radius(ViewContext *vc, float center[3], float offset) { - float brush_edge[3]; - - /* In anchored mode, brush size changes with mouse loc, otherwise it's fixed using the brush radius */ - view3d_unproject(ss->cache->mats, brush_edge, ss->cache->initial_mouse[0] + offset, - ss->cache->initial_mouse[1], ss->cache->depth); + float delta[3]; - return len_v3v3(ss->cache->true_location, brush_edge); + initgrabz(vc->rv3d, center[0], center[1], center[2]); + window_to_3d_delta(vc->ar, delta, offset, 0); + return len_v3(delta); } static void sculpt_cache_free(StrokeCache *cache) { int i; - if(cache->orig_norms) - MEM_freeN(cache->orig_norms); if(cache->face_norms) MEM_freeN(cache->face_norms); if(cache->mats) MEM_freeN(cache->mats); - for(i = 0; i < 8; ++i) - BLI_freelistN(&cache->grab_active_verts[i]); + for(i = 0; i < 8; ++i) { + if(cache->grab_active_nodes[i]) + MEM_freeN(cache->grab_active_nodes[i]); + } MEM_freeN(cache); } @@ -1290,70 +1670,63 @@ static void sculpt_update_cache_invariants(Sculpt *sd, SculptSession *ss, bConte cache->flag = RNA_int_get(op->ptr, "flag"); RNA_float_get_array(op->ptr, "clip_tolerance", cache->clip_tolerance); RNA_float_get_array(op->ptr, "initial_mouse", cache->initial_mouse); - cache->depth = RNA_float_get(op->ptr, "depth"); copy_v2_v2(cache->mouse, cache->initial_mouse); copy_v2_v2(cache->tex_mouse, cache->initial_mouse); /* Truly temporary data that isn't stored in properties */ - cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats"); - view3d_get_transformation(vc, vc->obact, cache->mats); + cache->vc = vc; + cache->brush = brush; - sculpt_update_mesh_elements(C); - - /* Initialize layer brush displacements */ - if(brush->sculpt_tool == SCULPT_TOOL_LAYER && - (!ss->layer_disps || !(brush->flag & BRUSH_PERSISTENT))) { - if(ss->layer_disps) - MEM_freeN(ss->layer_disps); - ss->layer_disps = MEM_callocN(sizeof(float) * ss->totvert, "layer brush displacements"); - } - - /* Make copies of the mesh vertex locations and normals for some tools */ - if(brush->sculpt_tool == SCULPT_TOOL_LAYER || (brush->flag & BRUSH_ANCHORED)) { - if(brush->sculpt_tool != SCULPT_TOOL_LAYER || - !ss->mesh_co_orig || !(brush->flag & BRUSH_PERSISTENT)) { - if(!ss->mesh_co_orig) - ss->mesh_co_orig= MEM_mallocN(sizeof(float) * 3 * ss->totvert, + cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats"); + view3d_get_transformation(vc->ar, vc->rv3d, vc->obact, cache->mats); + + /* Initialize layer brush displacements and persistent coords */ + if(brush->sculpt_tool == SCULPT_TOOL_LAYER) { + if(!ss->layer_disps || !(brush->flag & BRUSH_PERSISTENT)) { + if(ss->layer_disps) + MEM_freeN(ss->layer_disps); + ss->layer_disps = MEM_callocN(sizeof(float) * ss->totvert, "layer brush displacements"); + } + if(!ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) { + if(!ss->layer_co) + ss->layer_co= MEM_mallocN(sizeof(float) * 3 * ss->totvert, "sculpt mesh vertices copy"); for(i = 0; i < ss->totvert; ++i) - copy_v3_v3(ss->mesh_co_orig[i], ss->mvert[i].co); + copy_v3_v3(ss->layer_co[i], ss->mvert[i].co); } + } - if(brush->flag & BRUSH_ANCHORED) { - cache->orig_norms= MEM_mallocN(sizeof(short) * 3 * ss->totvert, "Sculpt orig norm"); - for(i = 0; i < ss->totvert; ++i) { - cache->orig_norms[i][0] = ss->mvert[i].no[0]; - cache->orig_norms[i][1] = ss->mvert[i].no[1]; - cache->orig_norms[i][2] = ss->mvert[i].no[2]; - } - - if(ss->face_normals) { - float *fn = ss->face_normals; - cache->face_norms= MEM_mallocN(sizeof(float) * 3 * ss->totface, "Sculpt face norms"); - for(i = 0; i < ss->totface; ++i, fn += 3) - copy_v3_v3(cache->face_norms[i], fn); - } + /* Make copies of the mesh vertex locations and normals for some tools */ + if(brush->flag & BRUSH_ANCHORED) { + if(ss->face_normals) { + float *fn = ss->face_normals; + cache->face_norms= MEM_mallocN(sizeof(float) * 3 * ss->totface, "Sculpt face norms"); + for(i = 0; i < ss->totface; ++i, fn += 3) + copy_v3_v3(cache->face_norms[i], fn); } + + cache->original = 1; } - view3d_unproject(cache->mats, cache->true_location, cache->initial_mouse[0], cache->initial_mouse[1], cache->depth); - cache->initial_radius = unproject_brush_radius(ss, brush->size); + if(ELEM3(brush->sculpt_tool, SCULPT_TOOL_DRAW, SCULPT_TOOL_LAYER, SCULPT_TOOL_INFLATE)) + if(!(brush->flag & BRUSH_ACCUMULATE)) + cache->original = 1; + cache->rotation = 0; cache->first_time = 1; } /* Initialize the stroke cache variants from operator properties */ -static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, PointerRNA *ptr) +static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, struct PaintStroke *stroke, PointerRNA *ptr) { StrokeCache *cache = ss->cache; Brush *brush = paint_brush(&sd->paint); - float grab_location[3]; int dx, dy; - if(!(brush->flag & BRUSH_ANCHORED)) + if(!(brush->flag & BRUSH_ANCHORED) || cache->first_time) RNA_float_get_array(ptr, "location", cache->true_location); cache->flip = RNA_boolean_get(ptr, "flip"); RNA_float_get_array(ptr, "mouse", cache->mouse); @@ -1364,6 +1737,9 @@ static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, PointerR cache->previous_pixel_radius = cache->pixel_radius; cache->pixel_radius = brush->size; + if(cache->first_time) + cache->initial_radius = unproject_brush_radius(cache->vc, cache->true_location, brush->size); + if(brush->flag & BRUSH_SIZE_PRESSURE) { cache->pixel_radius *= cache->pressure; cache->radius = cache->initial_radius * cache->pressure; @@ -1378,7 +1754,8 @@ static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, PointerR dx = cache->mouse[0] - cache->initial_mouse[0]; dy = cache->mouse[1] - cache->initial_mouse[1]; cache->pixel_radius = sqrt(dx*dx + dy*dy); - cache->radius = unproject_brush_radius(ss, cache->pixel_radius); + cache->radius = unproject_brush_radius(paint_stroke_view_context(stroke), + cache->true_location, cache->pixel_radius); cache->rotation = atan2(dy, dx); } else if(brush->flag & BRUSH_RAKE) { @@ -1401,17 +1778,91 @@ static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, PointerR /* Find the grab delta */ if(brush->sculpt_tool == SCULPT_TOOL_GRAB) { - view3d_unproject(cache->mats, grab_location, cache->mouse[0], cache->mouse[1], cache->depth); + float grab_location[3]; + + if(cache->first_time) + copy_v3_v3(cache->orig_grab_location, cache->true_location); + + initgrabz(cache->vc->rv3d, cache->orig_grab_location[0], cache->orig_grab_location[1], cache->orig_grab_location[2]); + window_to_3d_delta(cache->vc->ar, grab_location, cache->mouse[0], cache->mouse[1]); + if(!cache->first_time) sub_v3_v3v3(cache->grab_delta, grab_location, cache->old_grab_location); copy_v3_v3(cache->old_grab_location, grab_location); } } +static void sculpt_stroke_modifiers_check(bContext *C, SculptSession *ss) +{ + if(sculpt_modifiers_active(ss->ob)) + sculpt_update_mesh_elements(CTX_data_scene(C), ss->ob, 0); // XXX brush->sculpt_tool == SCULPT_TOOL_SMOOTH); +} + +typedef struct { + SculptSession *ss; + float *ray_start, *ray_normal; + int hit; + float dist; + int original; +} SculptRaycastData; + +void sculpt_raycast_cb(PBVHNode *node, void *data_v) +{ + SculptRaycastData *srd = data_v; + float (*origco)[3]= NULL; + + if(srd->original && srd->ss->cache) { + /* intersect with coordinates from before we started stroke */ + SculptUndoNode *unode= sculpt_undo_get_node(srd->ss, node); + origco= (unode)? unode->co: NULL; + } + + srd->hit |= BLI_pbvh_node_raycast(srd->ss->tree, node, origco, + srd->ray_start, srd->ray_normal, &srd->dist); +} + +/* Do a raycast in the tree to find the 3d brush location + (This allows us to ignore the GL depth buffer) + Returns 0 if the ray doesn't hit the mesh, non-zero otherwise + */ +int sculpt_stroke_get_location(bContext *C, struct PaintStroke *stroke, float out[3], float mouse[2]) +{ + ViewContext *vc = paint_stroke_view_context(stroke); + SculptSession *ss= vc->obact->sculpt; + StrokeCache *cache= ss->cache; + float ray_start[3], ray_normal[3]; + float obimat[4][4]; + float mval[2] = {mouse[0] - vc->ar->winrct.xmin, + mouse[1] - vc->ar->winrct.ymin}; + SculptRaycastData srd; + + sculpt_stroke_modifiers_check(C, ss); + + viewray(vc->ar, vc->v3d, mval, ray_start, ray_normal); + + invert_m4_m4(obimat, ss->ob->obmat); + mul_m4_v3(obimat, ray_start); + mul_mat3_m4_v3(obimat, ray_normal); + + srd.ss = vc->obact->sculpt; + srd.ray_start = ray_start; + srd.ray_normal = ray_normal; + srd.dist = FLT_MAX; + srd.hit = 0; + srd.original = (cache)? cache->original: 0; + BLI_pbvh_raycast(ss->tree, sculpt_raycast_cb, &srd, + ray_start, ray_normal, srd.original); + + copy_v3_v3(out, ray_normal); + mul_v3_fl(out, srd.dist); + add_v3_v3v3(out, out, ray_start); + + return srd.hit; +} + /* Initialize stroke operator properties */ static void sculpt_brush_stroke_init_properties(bContext *C, wmOperator *op, wmEvent *event, SculptSession *ss) { - Sculpt *sd = CTX_data_tool_settings(C)->sculpt; Object *ob= CTX_data_active_object(C); ModifierData *md; float scale[3], clip_tolerance[3] = {0,0,0}; @@ -1444,18 +1895,15 @@ static void sculpt_brush_stroke_init_properties(bContext *C, wmOperator *op, wmE mouse[0] = event->x; mouse[1] = event->y; RNA_float_set_array(op->ptr, "initial_mouse", mouse); - - /* Initial screen depth under the mouse */ - RNA_float_set(op->ptr, "depth", read_cached_depth(paint_stroke_view_context(op->customdata), event->x, event->y)); - - sculpt_update_cache_invariants(sd, ss, C, op); } static int sculpt_brush_stroke_init(bContext *C, ReportList *reports) { + Scene *scene= CTX_data_scene(C); Object *ob= CTX_data_active_object(C); Sculpt *sd = CTX_data_tool_settings(C)->sculpt; SculptSession *ss = CTX_data_active_object(C)->sculpt; + Brush *brush = paint_brush(&sd->paint); if(ob_get_key(ob) && !(ob->shapeflag & OB_SHAPE_LOCK)) { BKE_report(reports, RPT_ERROR, "Shape key sculpting requires a locked shape."); @@ -1469,9 +1917,7 @@ static int sculpt_brush_stroke_init(bContext *C, ReportList *reports) changes are made to the texture. */ sculpt_update_tex(sd, ss); - sculpt_update_mesh_elements(C); - - if(ss->kb) key_to_mesh(ss->kb, ss->ob->data); + sculpt_update_mesh_elements(scene, ob, brush->sculpt_tool == SCULPT_TOOL_SMOOTH); return 1; } @@ -1480,30 +1926,31 @@ static void sculpt_restore_mesh(Sculpt *sd, SculptSession *ss) { StrokeCache *cache = ss->cache; Brush *brush = paint_brush(&sd->paint); - float *buffer= NULL; int i; /* Restore the mesh before continuing with anchored stroke */ - if((brush->flag & BRUSH_ANCHORED) && ss->mesh_co_orig) { - - if(ss->drawobject) - buffer= (float *)GPU_buffer_lock(ss->drawobject->normals); - - for(i = 0; i < ss->totvert; ++i) { - copy_v3_v3(ss->mvert[i].co, ss->mesh_co_orig[i]); - ss->mvert[i].no[0] = cache->orig_norms[i][0]; - ss->mvert[i].no[1] = cache->orig_norms[i][1]; - ss->mvert[i].no[2] = cache->orig_norms[i][2]; - if( buffer != 0 ) { - IndexLink *cur = &ss->drawobject->indices[i]; - while( cur != 0 && cur->element != -1 ) { - VECCOPY(&buffer[cur->element*3],cache->orig_norms[i]); - cur = cur->next; + if(brush->flag & BRUSH_ANCHORED) { + PBVHNode **nodes; + int n, totnode; + + BLI_pbvh_search_gather(ss->tree, NULL, NULL, &nodes, &totnode); + + #pragma omp parallel for private(n) schedule(static) + for(n=0; n<totnode; n++) { + SculptUndoNode *unode; + + unode= sculpt_undo_get_node(ss, nodes[n]); + if(unode) { + PBVHVertexIter vd; + + BLI_pbvh_vertex_iter_begin(ss->tree, nodes[n], vd, PBVH_ITER_UNIQUE) { + copy_v3_v3(vd.co, unode->co[vd.i]); + if(vd.no) VECCOPY(vd.no, unode->no[vd.i]) + else normal_short_to_float_v3(vd.fno, unode->no[vd.i]); } + BLI_pbvh_vertex_iter_end; } } - if( buffer != 0 ) - GPU_buffer_unlock( ss->drawobject->normals ); if(ss->face_normals) { float *fn = ss->face_normals; @@ -1516,50 +1963,59 @@ static void sculpt_restore_mesh(Sculpt *sd, SculptSession *ss) } } -static void sculpt_post_stroke_free(SculptSession *ss) -{ - BLI_freelistN(&ss->damaged_rects); - BLI_freelistN(&ss->damaged_verts); -} - static void sculpt_flush_update(bContext *C) { Object *ob = CTX_data_active_object(C); SculptSession *ss = ob->sculpt; ARegion *ar = CTX_wm_region(C); MultiresModifierData *mmd = ss->multires; + int redraw = 0; - calc_damaged_verts(ss); - - if(mmd) { - if(mmd->undo_verts && mmd->undo_verts != ss->mvert) - MEM_freeN(mmd->undo_verts); - - mmd->undo_verts = ss->mvert; - mmd->undo_verts_tot = ss->totvert; + if(mmd) multires_mark_as_modified(ob); - } - if(sculpt_modifiers_active(ob)) + if(sculpt_modifiers_active(ob)) { DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + ED_region_tag_redraw(ar); + } + else { + rcti r; - ED_region_tag_redraw(ar); + BLI_pbvh_update(ss->tree, PBVH_UpdateBB, NULL); + redraw = sculpt_get_redraw_rect(ar, CTX_wm_region_view3d(C), ob, &r); + + if(redraw) { + r.xmin += ar->winrct.xmin + 1; + r.xmax += ar->winrct.xmin - 1; + r.ymin += ar->winrct.ymin + 1; + r.ymax += ar->winrct.ymin - 1; + + ss->partial_redraw = 1; + ED_region_tag_redraw_partial(ar, &r); + } + } } static int sculpt_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *event) { - ViewContext vc; - float cur_depth; - - view3d_set_viewcontext(C, &vc); - cur_depth = read_cached_depth(&vc, event->x, event->y); + float mouse[2] = {event->x, event->y}, location[3]; + int over_mesh; - /* Don't start the stroke until a valid depth is found */ - if(cur_depth < 1.0 - FLT_EPSILON) { - SculptSession *ss = CTX_data_active_object(C)->sculpt; + over_mesh = sculpt_stroke_get_location(C, op->customdata, location, mouse); + + /* Don't start the stroke until mouse goes over the mesh */ + if(over_mesh) { + Object *ob = CTX_data_active_object(C); + SculptSession *ss = ob->sculpt; + Sculpt *sd = CTX_data_tool_settings(C)->sculpt; + + ED_view3d_init_mats_rv3d(ob, CTX_wm_region_view3d(C)); sculpt_brush_stroke_init_properties(C, op, event, ss); - sculptmode_update_all_projverts(ss); + + sculpt_update_cache_invariants(sd, ss, C, op); + + sculpt_undo_push_begin(ss, sculpt_tool_name(sd)); return 1; } @@ -1572,13 +2028,13 @@ static void sculpt_stroke_update_step(bContext *C, struct PaintStroke *stroke, P Sculpt *sd = CTX_data_tool_settings(C)->sculpt; SculptSession *ss = CTX_data_active_object(C)->sculpt; - sculpt_update_cache_variants(sd, ss, itemptr); + sculpt_stroke_modifiers_check(C, ss); + sculpt_update_cache_variants(sd, ss, stroke, itemptr); sculpt_restore_mesh(sd, ss); do_symmetrical_brush_actions(sd, ss); /* Cleanup */ sculpt_flush_update(C); - sculpt_post_stroke_free(ss); } static void sculpt_stroke_done(bContext *C, struct PaintStroke *stroke) @@ -1588,14 +2044,18 @@ static void sculpt_stroke_done(bContext *C, struct PaintStroke *stroke) /* Finished */ if(ss->cache) { - Sculpt *sd = CTX_data_tool_settings(C)->sculpt; - - if(ss->refkb) key_to_mesh(ss->refkb, ob->data); + sculpt_stroke_modifiers_check(C, ss); - request_depth_update(paint_stroke_view_context(stroke)->rv3d); sculpt_cache_free(ss->cache); ss->cache = NULL; - sculpt_undo_push(C, sd); + + sculpt_undo_push_end(ss); + + BLI_pbvh_update(ss->tree, PBVH_UpdateOriginalBB, NULL); + + if(ss->refkb) sculpt_key_to_mesh(ss->refkb, ob); + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); } } @@ -1604,7 +2064,8 @@ static int sculpt_brush_stroke_invoke(bContext *C, wmOperator *op, wmEvent *even if(!sculpt_brush_stroke_init(C, op->reports)) return OPERATOR_CANCELLED; - op->customdata = paint_stroke_new(C, sculpt_stroke_test_start, + op->customdata = paint_stroke_new(C, sculpt_stroke_get_location, + sculpt_stroke_test_start, sculpt_stroke_update_step, sculpt_stroke_done); @@ -1624,18 +2085,16 @@ static int sculpt_brush_stroke_exec(bContext *C, wmOperator *op) if(!sculpt_brush_stroke_init(C, op->reports)) return OPERATOR_CANCELLED; - op->customdata = paint_stroke_new(C, sculpt_stroke_test_start, sculpt_stroke_update_step, sculpt_stroke_done); + op->customdata = paint_stroke_new(C, sculpt_stroke_get_location, sculpt_stroke_test_start, + sculpt_stroke_update_step, sculpt_stroke_done); sculpt_update_cache_invariants(sd, ss, C, op); - sculptmode_update_all_projverts(ss); paint_stroke_exec(C, op); sculpt_flush_update(C); sculpt_cache_free(ss->cache); - sculpt_undo_push(C, sd); - return OPERATOR_FINISHED; } @@ -1670,9 +2129,6 @@ static void SCULPT_OT_brush_stroke(wmOperatorType *ot) /* The initial 2D location of the mouse */ RNA_def_float_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX); - - /* The initial screen depth of the mouse */ - RNA_def_float(ot->srna, "depth", 0.0f, 0.0f, FLT_MAX, "depth", "", 0.0f, FLT_MAX); } /**** Reset the copy of the mesh that is being sculpted on (currently just for the layer brush) ****/ @@ -1686,9 +2142,9 @@ static int sculpt_set_persistent_base(bContext *C, wmOperator *op) MEM_freeN(ss->layer_disps); ss->layer_disps = NULL; - if(ss->mesh_co_orig) - MEM_freeN(ss->mesh_co_orig); - ss->mesh_co_orig = NULL; + if(ss->layer_co) + MEM_freeN(ss->layer_co); + ss->layer_co = NULL; } return OPERATOR_FINISHED; @@ -1709,15 +2165,30 @@ static void SCULPT_OT_set_persistent_base(wmOperatorType *ot) /**** Toggle operator for turning sculpt mode on or off ****/ +static void sculpt_init_session(Scene *scene, Object *ob) +{ + ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session"); + + sculpt_update_mesh_elements(scene, ob, 0); + + if(ob->sculpt->refkb) + sculpt_key_to_mesh(ob->sculpt->refkb, ob); +} + static int sculpt_toggle_mode(bContext *C, wmOperator *op) { + Scene *scene = CTX_data_scene(C); ToolSettings *ts = CTX_data_tool_settings(C); Object *ob = CTX_data_active_object(C); + MultiresModifierData *mmd = sculpt_multires_active(ob); if(ob->mode & OB_MODE_SCULPT) { if(sculpt_multires_active(ob)) multires_force_update(ob); + if(mmd && mmd->sculptlvl != mmd->lvl) + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + /* Leave sculptmode */ ob->mode &= ~OB_MODE_SCULPT; @@ -1725,8 +2196,10 @@ static int sculpt_toggle_mode(bContext *C, wmOperator *op) } else { /* Enter sculptmode */ - ob->mode |= OB_MODE_SCULPT; + + if(mmd && mmd->sculptlvl != mmd->lvl) + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); /* Create persistent sculpt mode data */ if(!ts->sculpt) @@ -1735,7 +2208,8 @@ static int sculpt_toggle_mode(bContext *C, wmOperator *op) /* Create sculpt mode session data */ if(ob->sculpt) free_sculptsession(&ob->sculpt); - ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session"); + + sculpt_init_session(scene, ob); paint_init(&ts->sculpt->paint, PAINT_CURSOR_SCULPT); @@ -1767,3 +2241,4 @@ void ED_operatortypes_sculpt() WM_operatortype_append(SCULPT_OT_sculptmode_toggle); WM_operatortype_append(SCULPT_OT_set_persistent_base); } + diff --git a/source/blender/editors/sculpt_paint/sculpt_intern.h b/source/blender/editors/sculpt_paint/sculpt_intern.h index 15ccacc294a..0e1c0510902 100644 --- a/source/blender/editors/sculpt_paint/sculpt_intern.h +++ b/source/blender/editors/sculpt_paint/sculpt_intern.h @@ -35,7 +35,9 @@ struct bContext; struct Brush; +struct KeyBlock; struct Mesh; +struct MultiresModifierData; struct Object; struct Scene; struct Sculpt; @@ -47,6 +49,7 @@ void sculptmode_draw_mesh(int); void sculpt_paint_brush(char clear); void sculpt_stroke_draw(struct SculptStroke *); void sculpt_radialcontrol_start(int mode); +struct MultiresModifierData *sculpt_multires_active(struct Object *ob); struct Brush *sculptmode_brush(void); //void do_symmetrical_brush_actions(struct Sculpt *sd, struct wmOperator *wm, struct BrushAction *a, short *, short *); @@ -55,6 +58,9 @@ char sculpt_modifiers_active(struct Object *ob); void sculpt(Sculpt *sd); int sculpt_poll(struct bContext *C); +void sculpt_update_mesh_elements(struct Scene *scene, struct Object *ob, int need_fmap); +void sculpt_key_to_mesh(struct KeyBlock *kb, struct Object *ob); +void sculpt_mesh_to_key(struct Object *ob, struct KeyBlock *kb); /* Stroke */ struct SculptStroke *sculpt_stroke_new(const int max); diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c index a1f9a9fd250..93bedd0b866 100644 --- a/source/blender/editors/space_file/file_draw.c +++ b/source/blender/editors/space_file/file_draw.c @@ -310,6 +310,8 @@ static int get_file_icon(struct direntry *file) return ICON_FILE_SOUND; else if (file->flags & FTFONTFILE) return ICON_FILE_FONT; + else if (file->flags & BTXFILE) + return ICON_FILE_BLANK; else return ICON_FILE_BLANK; } diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c index 640f365b073..81aa63dcad9 100644 --- a/source/blender/editors/space_file/filelist.c +++ b/source/blender/editors/space_file/filelist.c @@ -871,6 +871,8 @@ void filelist_setfiletypes(struct FileList* filelist, short has_quicktime) || BLI_testextensie(file->relname, ".otf") || BLI_testextensie(file->relname, ".otc")) { file->flags |= FTFONTFILE; + } else if(BLI_testextensie(file->relname, ".btx")) { + file->flags |= BTXFILE; } else if (has_quicktime){ if( BLI_testextensie(file->relname, ".int") || BLI_testextensie(file->relname, ".inta") diff --git a/source/blender/editors/space_file/filesel.c b/source/blender/editors/space_file/filesel.c index d681a58b276..bf580545455 100644 --- a/source/blender/editors/space_file/filesel.c +++ b/source/blender/editors/space_file/filesel.c @@ -161,6 +161,8 @@ short ED_fileselect_set_params(SpaceFile *sfile) params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0; if(RNA_struct_find_property(op->ptr, "filter_folder")) params->filter |= RNA_boolean_get(op->ptr, "filter_folder") ? FOLDERFILE : 0; + if(RNA_struct_find_property(op->ptr, "filter_btx")) + params->filter |= RNA_boolean_get(op->ptr, "filter_btx") ? BTXFILE : 0; if (params->filter != 0) params->flag |= FILE_FILTER; diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c index 31c12285293..edb91f2b0c9 100644 --- a/source/blender/editors/space_view3d/drawobject.c +++ b/source/blender/editors/space_view3d/drawobject.c @@ -108,6 +108,7 @@ #include "ED_mesh.h" #include "ED_particle.h" #include "ED_screen.h" +#include "ED_sculpt.h" #include "ED_types.h" #include "ED_util.h" @@ -2703,7 +2704,7 @@ static void draw_mesh_object_outline(View3D *v3d, Object *ob, DerivedMesh *dm) drawFacesSolid() doesn't draw the transparent faces */ if(ob->dtx & OB_DRAWTRANSP) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - dm->drawFacesSolid(dm, GPU_enable_material); + dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); GPU_disable_material(); } @@ -2722,7 +2723,7 @@ static int wpaint__setSolidDrawOptions(void *userData, int index, int *drawSmoot return 1; } -static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag) +static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag) { Object *ob= base->object; Mesh *me = ob->data; @@ -2795,7 +2796,7 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base } } else if(dt==OB_SOLID) { - if((v3d->flag&V3D_SELECT_OUTLINE) && (base->flag&SELECT) && !draw_wire) + if((v3d->flag&V3D_SELECT_OUTLINE) && (base->flag&SELECT) && !draw_wire && !ob->sculpt) draw_mesh_object_outline(v3d, ob, dm); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, me->flag & ME_TWOSIDED ); @@ -2803,7 +2804,23 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base glEnable(GL_LIGHTING); glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW); - dm->drawFacesSolid(dm, GPU_enable_material); + if(ob->sculpt) { + Paint *p = paint_get_active(scene); + float planes[4][4]; + float (*fpl)[4] = NULL; + int fast= (p->flags & PAINT_FAST_NAVIGATE) && (rv3d->rflag & RV3D_NAVIGATING); + + if(ob->sculpt->partial_redraw) { + sculpt_get_redraw_planes(planes, ar, rv3d, ob); + fpl = planes; + ob->sculpt->partial_redraw = 0; + } + + dm->drawFacesSolid(dm, fpl, fast, GPU_enable_material); + } + else + dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material); + GPU_disable_material(); glFrontFace(GL_CCW); @@ -2814,7 +2831,8 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base } else { UI_ThemeColor(TH_WIRE); } - dm->drawLooseEdges(dm); + if(!ob->sculpt) + dm->drawLooseEdges(dm); } else if(dt==OB_SHADED) { int do_draw= 1; /* to resolve all G.f settings below... */ @@ -2932,7 +2950,7 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base } /* returns 1 if nothing was drawn, for detecting to draw an object center */ -static int draw_mesh_object(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag) +static int draw_mesh_object(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag) { Object *ob= base->object; Object *obedit= scene->obedit; @@ -2967,10 +2985,6 @@ static int draw_mesh_object(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base if (obedit!=ob && finalDM) finalDM->release(finalDM); } -// else if(!em && (G.f & G_SCULPTMODE) &&(scene->sculptdata.flags & SCULPT_DRAW_FAST) && -// OBACT==ob && !sculpt_modifiers_active(ob)) { -// XXX sculptmode_draw_mesh(0); -// } else { /* don't create boundbox here with mesh_get_bb(), the derived system will make it, puts deformed bb's OK */ if(me->totface<=4 || boundbox_clip(rv3d, ob->obmat, (ob->bb)? ob->bb: me->bb)) { @@ -2982,7 +2996,7 @@ static int draw_mesh_object(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base (check_alpha)? &do_alpha_pass: NULL); } - draw_mesh_fancy(scene, v3d, rv3d, base, dt, flag); + draw_mesh_fancy(scene, ar, v3d, rv3d, base, dt, flag); GPU_end_object_materials(); @@ -5744,7 +5758,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag) switch( ob->type) { case OB_MESH: - empty_object= draw_mesh_object(scene, v3d, rv3d, base, dt, flag); + empty_object= draw_mesh_object(scene, ar, v3d, rv3d, base, dt, flag); if(flag!=DRAW_CONSTCOLOR) dtx &= ~OB_DRAWWIRE; // mesh draws wire itself break; @@ -6333,10 +6347,8 @@ static void bbs_mesh_solid(Scene *scene, View3D *v3d, Object *ob) int ind; colors = MEM_mallocN(dm->getNumFaces(dm)*sizeof(MCol)*4,"bbs_mesh_solid"); for(i=0;i<dm->getNumFaces(dm);i++) { - if( index != 0 ) - ind = index[i]; - else - ind = i; + ind= ( index )? index[i]: i; + if (face_sel_mode==0 || !(me->mface[ind].flag&ME_HIDE)) { unsigned int fbindex = index_to_framebuffer(ind+1); for(j=0;j<4;j++) { @@ -6463,7 +6475,7 @@ static void draw_object_mesh_instance(Scene *scene, View3D *v3d, RegionView3D *r glEnable(GL_LIGHTING); if(dm) { - dm->drawFacesSolid(dm, GPU_enable_material); + dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material); GPU_end_object_materials(); } else if(edm) diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c index 5a2040a44aa..19667beaaf9 100644 --- a/source/blender/editors/space_view3d/view3d_draw.c +++ b/source/blender/editors/space_view3d/view3d_draw.c @@ -1643,37 +1643,6 @@ void view3d_update_depths(ARegion *ar, View3D *v3d) } } -/* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */ -static void draw_sculpt_depths(Scene *scene, ARegion *ar, View3D *v3d) -{ - Object *ob = OBACT; - - int dt= MIN2(v3d->drawtype, ob->dt); - if(v3d->zbuf==0 && dt>OB_WIRE) - dt= OB_WIRE; - if(dt == OB_WIRE) { - GLboolean depth_on; - int orig_vdt = v3d->drawtype; - int orig_zbuf = v3d->zbuf; - int orig_odt = ob->dt; - - glGetBooleanv(GL_DEPTH_TEST, &depth_on); - v3d->drawtype = ob->dt = OB_SOLID; - v3d->zbuf = 1; - - glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); - glEnable(GL_DEPTH_TEST); - draw_object(scene, ar, v3d, BASACT, 0); - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - if(!depth_on) - glDisable(GL_DEPTH_TEST); - - v3d->drawtype = orig_vdt; - v3d->zbuf = orig_zbuf; - ob->dt = orig_odt; - } -} - void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *)) { RegionView3D *rv3d= ar->regiondata; @@ -1891,8 +1860,6 @@ static CustomDataMask get_viewedit_datamask(bScreen *screen, Scene *scene, Objec mask |= CD_MASK_MCOL; if(ob->mode & OB_MODE_WEIGHT_PAINT) mask |= CD_MASK_WEIGHT_MCOL; - if(ob->mode & OB_MODE_SCULPT) - mask |= CD_MASK_MDISPS; } return mask; @@ -2137,7 +2104,7 @@ void view3d_main_area_draw(const bContext *C, ARegion *ar) } // retopo= retopo_mesh_check() || retopo_curve_check(); - sculptparticle= (obact && obact->mode & (OB_MODE_SCULPT|OB_MODE_PARTICLE_EDIT)) && !scene->obedit; + sculptparticle= (obact && obact->mode & (OB_MODE_PARTICLE_EDIT)) && !scene->obedit; if(retopo) view3d_update_depths(ar, v3d); @@ -2150,8 +2117,6 @@ void view3d_main_area_draw(const bContext *C, ARegion *ar) } if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) { - if(obact && obact->mode & OB_MODE_SCULPT) - draw_sculpt_depths(scene, ar, v3d); view3d_update_depths(ar, v3d); } @@ -2166,8 +2131,6 @@ void view3d_main_area_draw(const bContext *C, ARegion *ar) view3d_draw_xray(scene, ar, v3d, 1); // clears zbuffer if it is used! if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) { - if(obact && obact->mode & OB_MODE_SCULPT) - draw_sculpt_depths(scene, ar, v3d); view3d_update_depths(ar, v3d); } @@ -2189,8 +2152,6 @@ void view3d_main_area_draw(const bContext *C, ARegion *ar) ED_region_pixelspace(ar); - /* Draw Sculpt Mode brush XXX (removed) */ - // retopo_paint_view_update(v3d); // retopo_draw_paint_lines(); diff --git a/source/blender/editors/space_view3d/view3d_edit.c b/source/blender/editors/space_view3d/view3d_edit.c index fd20b534dd7..6a58e3c2e8d 100644 --- a/source/blender/editors/space_view3d/view3d_edit.c +++ b/source/blender/editors/space_view3d/view3d_edit.c @@ -278,7 +278,7 @@ static void calctrackballvec(rcti *rect, int mx, int my, float *vec) } -static void viewops_data(bContext *C, wmOperator *op, wmEvent *event) +static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event) { static float lastofs[3] = {0,0,0}; View3D *v3d = CTX_wm_view3d(C); @@ -359,6 +359,21 @@ static void viewops_data(bContext *C, wmOperator *op, wmEvent *event) if (rv3d->persmat[2][1] < 0.0f) vod->reverse= -1.0f; + rv3d->rflag |= RV3D_NAVIGATING; +} + +static void viewops_data_free(bContext *C, wmOperator *op) +{ + Paint *p = paint_get_active(CTX_data_scene(C)); + ViewOpsData *vod= op->customdata; + + vod->rv3d->rflag &= ~RV3D_NAVIGATING; + + if(p && (p->flags & PAINT_FAST_NAVIGATE)) + ED_region_tag_redraw(vod->ar); + + MEM_freeN(vod); + op->customdata= NULL; } /* ************************** viewrotate **********************************/ @@ -622,9 +637,7 @@ static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event) } else if (event_code==VIEW_CONFIRM) { request_depth_update(CTX_wm_region_view3d(C)); - - MEM_freeN(vod); - op->customdata= NULL; + viewops_data_free(C, op); return OPERATOR_FINISHED; } @@ -641,7 +654,7 @@ static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event) return OPERATOR_CANCELLED; /* makes op->customdata */ - viewops_data(C, op, event); + viewops_data_create(C, op, event); vod= op->customdata; /* switch from camera view when: */ @@ -762,8 +775,7 @@ static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event) else if (event_code==VIEW_CONFIRM) { request_depth_update(CTX_wm_region_view3d(C)); - MEM_freeN(vod); - op->customdata= NULL; + viewops_data_free(C, op); return OPERATOR_FINISHED; } @@ -774,7 +786,7 @@ static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event) static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event) { /* makes op->customdata */ - viewops_data(C, op, event); + viewops_data_create(C, op, event); /* add temp handler */ WM_event_add_modal_handler(C, op); @@ -966,9 +978,7 @@ static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event) } else if (event_code==VIEW_CONFIRM) { request_depth_update(CTX_wm_region_view3d(C)); - - MEM_freeN(vod); - op->customdata= NULL; + viewops_data_free(C, op); return OPERATOR_FINISHED; } @@ -1029,7 +1039,7 @@ static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event) } else { /* makes op->customdata */ - viewops_data(C, op, event); + viewops_data_create(C, op, event); /* add temp handler */ WM_event_add_modal_handler(C, op); @@ -1971,12 +1981,9 @@ void ED_view3d_local_clipping(RegionView3D *rv3d, float mat[][4]) static int view3d_clipping_exec(bContext *C, wmOperator *op) { RegionView3D *rv3d= CTX_wm_region_view3d(C); + ViewContext vc; + bglMats mats; rcti rect; - double mvmatrix[16]; - double projmatrix[16]; - double xs, ys, p[3]; - GLint viewport[4]; - short val; rect.xmin= RNA_int_get(op->ptr, "xmin"); rect.ymin= RNA_int_get(op->ptr, "ymin"); @@ -1989,36 +1996,9 @@ static int view3d_clipping_exec(bContext *C, wmOperator *op) /* note; otherwise opengl won't work */ view3d_operator_needs_opengl(C); - /* Get the matrices needed for gluUnProject */ - glGetIntegerv(GL_VIEWPORT, viewport); - glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix); - glGetDoublev(GL_PROJECTION_MATRIX, projmatrix); - - /* near zero floating point values can give issues with gluUnProject - in side view on some implementations */ - if(fabs(mvmatrix[0]) < 1e-6) mvmatrix[0]= 0.0; - if(fabs(mvmatrix[5]) < 1e-6) mvmatrix[5]= 0.0; - - /* Set up viewport so that gluUnProject will give correct values */ - viewport[0] = 0; - viewport[1] = 0; - - /* four clipping planes and bounding volume */ - /* first do the bounding volume */ - for(val=0; val<4; val++) { - - xs= (val==0||val==3)?rect.xmin:rect.xmax; - ys= (val==0||val==1)?rect.ymin:rect.ymax; - - gluUnProject(xs, ys, 0.0, mvmatrix, projmatrix, viewport, &p[0], &p[1], &p[2]); - VECCOPY(rv3d->clipbb->vec[val], p); - - gluUnProject(xs, ys, 1.0, mvmatrix, projmatrix, viewport, &p[0], &p[1], &p[2]); - VECCOPY(rv3d->clipbb->vec[4+val], p); - } - - /* then plane equations */ - calc_clipping_plane(rv3d->clip, rv3d->clipbb); + view3d_set_viewcontext(C, &vc); + view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats); + view3d_calculate_clipping(rv3d->clipbb, rv3d->clip, &mats, &rect); return OPERATOR_FINISHED; } diff --git a/source/blender/editors/space_view3d/view3d_select.c b/source/blender/editors/space_view3d/view3d_select.c index e7ebcbdb849..a1135bf6986 100644 --- a/source/blender/editors/space_view3d/view3d_select.c +++ b/source/blender/editors/space_view3d/view3d_select.c @@ -124,24 +124,24 @@ void view3d_get_view_aligned_coordinate(ViewContext *vc, float *fp, short mval[2 } } -void view3d_get_transformation(ViewContext *vc, Object *ob, bglMats *mats) +void view3d_get_transformation(ARegion *ar, RegionView3D *rv3d, Object *ob, bglMats *mats) { float cpy[4][4]; int i, j; - mul_m4_m4m4(cpy, ob->obmat, vc->rv3d->viewmat); + mul_m4_m4m4(cpy, ob->obmat, rv3d->viewmat); for(i = 0; i < 4; ++i) { for(j = 0; j < 4; ++j) { - mats->projection[i*4+j] = vc->rv3d->winmat[i][j]; + mats->projection[i*4+j] = rv3d->winmat[i][j]; mats->modelview[i*4+j] = cpy[i][j]; } } - mats->viewport[0] = vc->ar->winrct.xmin; - mats->viewport[1] = vc->ar->winrct.ymin; - mats->viewport[2] = vc->ar->winx; - mats->viewport[3] = vc->ar->winy; + mats->viewport[0] = ar->winrct.xmin; + mats->viewport[1] = ar->winrct.ymin; + mats->viewport[2] = ar->winx; + mats->viewport[3] = ar->winy; } /* ********************** view3d_select: selection manipulations ********************* */ diff --git a/source/blender/editors/space_view3d/view3d_view.c b/source/blender/editors/space_view3d/view3d_view.c index 9cd9d12b975..a569eff1ebe 100644 --- a/source/blender/editors/space_view3d/view3d_view.c +++ b/source/blender/editors/space_view3d/view3d_view.c @@ -287,6 +287,8 @@ void smooth_view(bContext *C, Object *oldcamera, Object *camera, float *ofs, flo /* ensure it shows correct */ if(sms.to_camera) rv3d->persp= RV3D_PERSP; + + rv3d->rflag |= RV3D_NAVIGATING; /* keep track of running timer! */ if(rv3d->sms==NULL) @@ -349,6 +351,7 @@ static int view3d_smoothview_invoke(bContext *C, wmOperator *op, wmEvent *event) WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer); rv3d->smooth_timer= NULL; + rv3d->rflag &= ~RV3D_NAVIGATING; } else { int i; @@ -486,6 +489,44 @@ void VIEW3D_OT_setobjectascamera(wmOperatorType *ot) /* ********************************** */ +void view3d_calculate_clipping(BoundBox *bb, float planes[4][4], bglMats *mats, rcti *rect) +{ + double xs, ys, p[3]; + short val; + + /* near zero floating point values can give issues with gluUnProject + in side view on some implementations */ + if(fabs(mats->modelview[0]) < 1e-6) mats->modelview[0]= 0.0; + if(fabs(mats->modelview[5]) < 1e-6) mats->modelview[5]= 0.0; + + /* Set up viewport so that gluUnProject will give correct values */ + mats->viewport[0] = 0; + mats->viewport[1] = 0; + + /* four clipping planes and bounding volume */ + /* first do the bounding volume */ + for(val=0; val<4; val++) { + xs= (val==0||val==3)?rect->xmin:rect->xmax; + ys= (val==0||val==1)?rect->ymin:rect->ymax; + + gluUnProject(xs, ys, 0.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]); + VECCOPY(bb->vec[val], p); + + gluUnProject(xs, ys, 1.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]); + VECCOPY(bb->vec[4+val], p); + } + + /* then plane equations */ + for(val=0; val<4; val++) { + + normal_tri_v3(planes[val], bb->vec[val], bb->vec[val==3?0:val+1], bb->vec[val+4]); + + planes[val][3]= - planes[val][0]*bb->vec[val][0] + - planes[val][1]*bb->vec[val][1] + - planes[val][2]*bb->vec[val][2]; + } +} + /* create intersection coordinates in view Z direction at mouse coordinates */ void viewline(ARegion *ar, View3D *v3d, float mval[2], float ray_start[3], float ray_end[3]) { @@ -501,8 +542,8 @@ void viewline(ARegion *ar, View3D *v3d, float mval[2], float ray_start[3], float mul_m4_v4(rv3d->persinv, vec); mul_v3_fl(vec, 1.0f / vec[3]); - VECCOPY(ray_start, rv3d->viewinv[3]); - VECSUB(vec, vec, ray_start); + copy_v3_v3(ray_start, rv3d->viewinv[3]); + sub_v3_v3v3(vec, vec, ray_start); normalize_v3(vec); VECADDFAC(ray_start, rv3d->viewinv[3], vec, v3d->near); @@ -1920,7 +1961,7 @@ int initFlyInfo (bContext *C, FlyInfo *fly, wmOperator *op, wmEvent *event) fly->time_lastdraw= fly->time_lastwheel= PIL_check_seconds_timer(); - fly->rv3d->rflag |= RV3D_FLYMODE; /* so we draw the corner margins */ + fly->rv3d->rflag |= RV3D_FLYMODE|RV3D_NAVIGATING; /* so we draw the corner margins */ /* detect weather to start with Z locking */ upvec[0]=1.0f; upvec[1]=0.0f; upvec[2]=0.0f; @@ -2019,7 +2060,7 @@ static int flyEnd(bContext *C, FlyInfo *fly) /*Done with correcting for the dist */ } - rv3d->rflag &= ~RV3D_FLYMODE; + rv3d->rflag &= ~(RV3D_FLYMODE|RV3D_NAVIGATING); //XXX2.5 BIF_view3d_previewrender_signal(fly->sa, PR_DBASE|PR_DISPRECT); /* not working at the moment not sure why */ diff --git a/source/blender/editors/util/ed_util.c b/source/blender/editors/util/ed_util.c index ae1e932bb81..fc2576eef5d 100644 --- a/source/blender/editors/util/ed_util.c +++ b/source/blender/editors/util/ed_util.c @@ -60,7 +60,7 @@ void ED_editors_exit(bContext *C) /* frees all editmode undos */ undo_editmode_clear(); - undo_imagepaint_clear(); + ED_undo_paint_free(); for(sce=G.main->scene.first; sce; sce= sce->id.next) { if(sce->obedit) { diff --git a/source/blender/editors/util/undo.c b/source/blender/editors/util/undo.c index d26f7a7a484..e20c88ba41f 100644 --- a/source/blender/editors/util/undo.c +++ b/source/blender/editors/util/undo.c @@ -124,7 +124,7 @@ static int ed_undo_step(bContext *C, int step, const char *undoname) SpaceImage *sima= (SpaceImage *)sa->spacedata.first; if((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) { - undo_imagepaint_step(step); + ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step); WM_event_add_notifier(C, NC_WINDOW, NULL); return OPERATOR_FINISHED; @@ -146,7 +146,9 @@ static int ed_undo_step(bContext *C, int step, const char *undoname) int do_glob_undo= 0; if(obact && obact->mode & OB_MODE_TEXTURE_PAINT) - undo_imagepaint_step(step); + ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step); + else if(obact && obact->mode & OB_MODE_SCULPT) + ED_undo_paint_step(C, UNDO_PAINT_MESH, step); else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) { if(step==1) PE_undo(CTX_data_scene(C)); diff --git a/source/blender/gpu/gpu_buffers.h b/source/blender/gpu/gpu_buffers.h index 662912f9c19..5531ccc813b 100644 --- a/source/blender/gpu/gpu_buffers.h +++ b/source/blender/gpu/gpu_buffers.h @@ -43,6 +43,7 @@ #endif struct DerivedMesh; +struct GHash; /* V - vertex, N - normal, T - uv, C - color F - float, UB - unsigned byte */ @@ -124,6 +125,20 @@ void GPU_buffer_free( GPUBuffer *buffer, GPUBufferPool *pool ); GPUDrawObject *GPU_drawobject_new( struct DerivedMesh *dm ); void GPU_drawobject_free( struct DerivedMesh *dm ); +/* Buffers for non-DerivedMesh drawing */ +void *GPU_build_mesh_buffers(struct GHash *map, struct MVert *mvert, + struct MFace *mface, int *face_indices, + int totface, int *vert_indices, int uniq_verts, + int totvert); +void GPU_update_mesh_buffers(void *buffers, struct MVert *mvert, + int *vert_indices, int totvert); +void *GPU_build_grid_buffers(struct DMGridData **grids, + int *grid_indices, int totgrid, int gridsize); +void GPU_update_grid_buffers(void *buffers_v, struct DMGridData **grids, + int *grid_indices, int totgrid, int gridsize); +void GPU_draw_buffers(void *buffers); +void GPU_free_buffers(void *buffers); + /* called before drawing */ void GPU_vertex_setup( struct DerivedMesh *dm ); void GPU_normal_setup( struct DerivedMesh *dm ); diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c index 3c178b10561..33aa4168ff8 100644 --- a/source/blender/gpu/intern/gpu_buffers.c +++ b/source/blender/gpu/intern/gpu_buffers.c @@ -30,6 +30,8 @@ * ***** END GPL LICENSE BLOCK ***** */ +#include <limits.h> +#include <stddef.h> #include <string.h> #include "GL/glew.h" @@ -37,6 +39,7 @@ #include "MEM_guardedalloc.h" #include "BLI_math.h" +#include "BLI_ghash.h" #include "DNA_meshdata_types.h" @@ -376,6 +379,360 @@ void GPU_drawobject_free( DerivedMesh *dm ) dm->drawObject = 0; } +/* Convenience struct for building the VBO. */ +typedef struct { + float co[3]; + short no[3]; +} VertexBufferFormat; + +typedef struct { + /* opengl buffer handles */ + GLuint vert_buf, index_buf; + GLenum index_type; + + /* mesh pointers in case buffer allocation fails */ + MFace *mface; + MVert *mvert; + int *face_indices; + int totface; + + /* grid pointers */ + DMGridData **grids; + int *grid_indices; + int totgrid; + int gridsize; + + unsigned int tot_tri, tot_quad; +} GPU_Buffers; + +void GPU_update_mesh_buffers(void *buffers_v, MVert *mvert, + int *vert_indices, int totvert) +{ + GPU_Buffers *buffers = buffers_v; + VertexBufferFormat *vert_data; + int i; + + if(buffers->vert_buf) { + /* Build VBO */ + glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffers->vert_buf); + glBufferDataARB(GL_ARRAY_BUFFER_ARB, + sizeof(VertexBufferFormat) * totvert, + NULL, GL_STATIC_DRAW_ARB); + vert_data = glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); + + if(vert_data) { + for(i = 0; i < totvert; ++i) { + MVert *v = mvert + vert_indices[i]; + VertexBufferFormat *out = vert_data + i; + + copy_v3_v3(out->co, v->co); + memcpy(out->no, v->no, sizeof(short) * 3); + } + + glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); + } + else { + glDeleteBuffersARB(1, &buffers->vert_buf); + buffers->vert_buf = 0; + } + + glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + } + + buffers->mvert = mvert; +} + +void *GPU_build_mesh_buffers(GHash *map, MVert *mvert, MFace *mface, + int *face_indices, int totface, + int *vert_indices, int tot_uniq_verts, + int totvert) +{ + GPU_Buffers *buffers; + unsigned short *tri_data; + int i, j, k, tottri; + + buffers = MEM_callocN(sizeof(GPU_Buffers), "GPU_Buffers"); + buffers->index_type = GL_UNSIGNED_SHORT; + + /* Count the number of triangles */ + for(i = 0, tottri = 0; i < totface; ++i) + tottri += mface[face_indices[i]].v4 ? 2 : 1; + + if(GL_ARB_vertex_buffer_object) + glGenBuffersARB(1, &buffers->index_buf); + + if(buffers->index_buf) { + /* Generate index buffer object */ + glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, buffers->index_buf); + glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, + sizeof(unsigned short) * tottri * 3, NULL, GL_STATIC_DRAW_ARB); + + /* Fill the triangle buffer */ + tri_data = glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); + if(tri_data) { + for(i = 0; i < totface; ++i) { + MFace *f = mface + face_indices[i]; + int v[3] = {f->v1, f->v2, f->v3}; + + for(j = 0; j < (f->v4 ? 2 : 1); ++j) { + for(k = 0; k < 3; ++k) { + void *value, *key = SET_INT_IN_POINTER(v[k]); + int vbo_index; + + value = BLI_ghash_lookup(map, key); + vbo_index = GET_INT_FROM_POINTER(value); + + if(vbo_index < 0) { + vbo_index = -vbo_index + + tot_uniq_verts - 1; + } + + *tri_data = vbo_index; + ++tri_data; + } + v[0] = f->v4; + v[1] = f->v1; + v[2] = f->v3; + } + } + glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB); + } + else { + glDeleteBuffersARB(1, &buffers->index_buf); + buffers->index_buf = 0; + } + + glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + } + + if(buffers->index_buf) + glGenBuffersARB(1, &buffers->vert_buf); + GPU_update_mesh_buffers(buffers, mvert, vert_indices, totvert); + + buffers->tot_tri = tottri; + + buffers->mface = mface; + buffers->face_indices = face_indices; + buffers->totface = totface; + + return buffers; +} + +void GPU_update_grid_buffers(void *buffers_v, DMGridData **grids, + int *grid_indices, int totgrid, int gridsize) +{ + GPU_Buffers *buffers = buffers_v; + DMGridData *vert_data; + int i, totvert; + + totvert= gridsize*gridsize*totgrid; + + /* Build VBO */ + if(buffers->vert_buf) { + glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffers->vert_buf); + glBufferDataARB(GL_ARRAY_BUFFER_ARB, + sizeof(DMGridData) * totvert, + NULL, GL_STATIC_DRAW_ARB); + vert_data = glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); + if(vert_data) { + for(i = 0; i < totgrid; ++i) { + DMGridData *grid= grids[grid_indices[i]]; + memcpy(vert_data, grid, sizeof(DMGridData)*gridsize*gridsize); + vert_data += gridsize*gridsize; + } + glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); + } + else { + glDeleteBuffersARB(1, &buffers->vert_buf); + buffers->vert_buf = 0; + } + glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + } + + buffers->grids = grids; + buffers->grid_indices = grid_indices; + buffers->totgrid = totgrid; + buffers->gridsize = gridsize; + + //printf("node updated %p\n", buffers_v); +} + +void *GPU_build_grid_buffers(DMGridData **grids, + int *grid_indices, int totgrid, int gridsize) +{ + GPU_Buffers *buffers; + int i, j, k, totquad, offset= 0; + + buffers = MEM_callocN(sizeof(GPU_Buffers), "GPU_Buffers"); + + /* Count the number of quads */ + totquad= (gridsize-1)*(gridsize-1)*totgrid; + + /* Generate index buffer object */ + if(GL_ARB_vertex_buffer_object) + glGenBuffersARB(1, &buffers->index_buf); + + if(buffers->index_buf) { + glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, buffers->index_buf); + + if(totquad < USHRT_MAX) { + unsigned short *quad_data; + + buffers->index_type = GL_UNSIGNED_SHORT; + glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, + sizeof(unsigned short) * totquad * 4, NULL, GL_STATIC_DRAW_ARB); + + /* Fill the quad buffer */ + quad_data = glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); + if(quad_data) { + for(i = 0; i < totgrid; ++i) { + for(j = 0; j < gridsize-1; ++j) { + for(k = 0; k < gridsize-1; ++k) { + *(quad_data++)= offset + j*gridsize + k; + *(quad_data++)= offset + (j+1)*gridsize + k; + *(quad_data++)= offset + (j+1)*gridsize + k+1; + *(quad_data++)= offset + j*gridsize + k+1; + } + } + + offset += gridsize*gridsize; + } + glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB); + } + else { + glDeleteBuffersARB(1, &buffers->index_buf); + buffers->index_buf = 0; + } + } + else { + unsigned int *quad_data; + + buffers->index_type = GL_UNSIGNED_INT; + glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, + sizeof(unsigned int) * totquad * 4, NULL, GL_STATIC_DRAW_ARB); + + /* Fill the quad buffer */ + quad_data = glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); + + if(quad_data) { + for(i = 0; i < totgrid; ++i) { + for(j = 0; j < gridsize-1; ++j) { + for(k = 0; k < gridsize-1; ++k) { + *(quad_data++)= offset + j*gridsize + k; + *(quad_data++)= offset + (j+1)*gridsize + k; + *(quad_data++)= offset + (j+1)*gridsize + k+1; + *(quad_data++)= offset + j*gridsize + k+1; + } + } + + offset += gridsize*gridsize; + } + glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB); + } + else { + glDeleteBuffersARB(1, &buffers->index_buf); + buffers->index_buf = 0; + } + } + + glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + } + + /* Build VBO */ + if(buffers->index_buf) + glGenBuffersARB(1, &buffers->vert_buf); + GPU_update_grid_buffers(buffers, grids, grid_indices, totgrid, gridsize); + + buffers->tot_quad = totquad; + + return buffers; +} + +void GPU_draw_buffers(void *buffers_v) +{ + GPU_Buffers *buffers = buffers_v; + + if(buffers->vert_buf && buffers->index_buf) { + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffers->vert_buf); + glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, buffers->index_buf); + + if(buffers->tot_quad) { + glVertexPointer(3, GL_FLOAT, sizeof(DMGridData), (void*)offsetof(DMGridData, co)); + glNormalPointer(GL_FLOAT, sizeof(DMGridData), (void*)offsetof(DMGridData, no)); + + glDrawElements(GL_QUADS, buffers->tot_quad * 4, buffers->index_type, 0); + } + else { + glVertexPointer(3, GL_FLOAT, sizeof(VertexBufferFormat), (void*)offsetof(VertexBufferFormat, co)); + glNormalPointer(GL_SHORT, sizeof(VertexBufferFormat), (void*)offsetof(VertexBufferFormat, no)); + + glDrawElements(GL_TRIANGLES, buffers->tot_tri * 3, buffers->index_type, 0); + } + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + } + else if(buffers->totface) { + /* fallback if we are out of memory */ + int i; + + for(i = 0; i < buffers->totface; ++i) { + MFace *f = buffers->mface + buffers->face_indices[i]; + + glBegin((f->v4)? GL_QUADS: GL_TRIANGLES); + glNormal3sv(buffers->mvert[f->v1].no); + glVertex3fv(buffers->mvert[f->v1].co); + glNormal3sv(buffers->mvert[f->v2].no); + glVertex3fv(buffers->mvert[f->v2].co); + glNormal3sv(buffers->mvert[f->v3].no); + glVertex3fv(buffers->mvert[f->v3].co); + if(f->v4) { + glNormal3sv(buffers->mvert[f->v4].no); + glVertex3fv(buffers->mvert[f->v4].co); + } + glEnd(); + } + } + else if(buffers->totgrid) { + int i, x, y, gridsize = buffers->gridsize; + + for(i = 0; i < buffers->totgrid; ++i) { + DMGridData *grid = buffers->grids[buffers->grid_indices[i]]; + + for(y = 0; y < gridsize-1; y++) { + glBegin(GL_QUAD_STRIP); + for(x = 0; x < gridsize; x++) { + DMGridData *a = &grid[y*gridsize + x]; + DMGridData *b = &grid[(y+1)*gridsize + x]; + + glNormal3fv(a->no); + glVertex3fv(a->co); + glNormal3fv(b->no); + glVertex3fv(b->co); + } + glEnd(); + } + } + } +} + +void GPU_free_buffers(void *buffers_v) +{ + if(buffers_v) { + GPU_Buffers *buffers = buffers_v; + + if(buffers->vert_buf) + glDeleteBuffersARB(1, &buffers->vert_buf); + if(buffers->index_buf) + glDeleteBuffersARB(1, &buffers->index_buf); + + MEM_freeN(buffers); + } +} + GPUBuffer *GPU_buffer_setup( DerivedMesh *dm, GPUDrawObject *object, int size, GLenum target, void *user, void (*copy_f)(DerivedMesh *, float *, int *, int *, void *) ) { GPUBuffer *buffer; diff --git a/source/blender/makesdna/DNA_brush_types.h b/source/blender/makesdna/DNA_brush_types.h index 1bbccd20486..c8fc14ca0f0 100644 --- a/source/blender/makesdna/DNA_brush_types.h +++ b/source/blender/makesdna/DNA_brush_types.h @@ -79,12 +79,13 @@ typedef struct Brush { #define BRUSH_JITTER_PRESSURE 16 /* was BRUSH_RAD_PRESSURE */ #define BRUSH_SPACING_PRESSURE 32 #define BRUSH_FIXED_TEX 64 -#define BRUSH_RAKE 128 -#define BRUSH_ANCHORED 256 -#define BRUSH_DIR_IN 512 -#define BRUSH_SPACE 1024 -#define BRUSH_SMOOTH_STROKE 2048 -#define BRUSH_PERSISTENT 4096 +#define BRUSH_RAKE 128 +#define BRUSH_ANCHORED 256 +#define BRUSH_DIR_IN 512 +#define BRUSH_SPACE 1024 +#define BRUSH_SMOOTH_STROKE 2048 +#define BRUSH_PERSISTENT 4096 +#define BRUSH_ACCUMULATE 8192 /* Brush.sculpt_tool */ #define SCULPT_TOOL_DRAW 1 diff --git a/source/blender/makesdna/DNA_customdata_types.h b/source/blender/makesdna/DNA_customdata_types.h index ad686e37097..91e4cefce80 100644 --- a/source/blender/makesdna/DNA_customdata_types.h +++ b/source/blender/makesdna/DNA_customdata_types.h @@ -44,14 +44,19 @@ typedef struct CustomDataLayer { void *data; /* layer data */ } CustomDataLayer; +typedef struct CustomDataExternal { + char filename[240]; /* FILE_MAX */ +} CustomDataExternal; + /* structure which stores custom element data associated with mesh elements * (vertices, edges or faces). The custom data is organised into a series of * layers, each with a data type (e.g. MTFace, MDeformVert, etc.). */ typedef struct CustomData { - CustomDataLayer *layers; /* CustomDataLayers, ordered by type */ - int totlayer, maxlayer; /* number of layers, size of layers array */ - int totsize, pad; /* in editmode, total size of all data layers */ - void *pool; /* for Bmesh: Memory pool for allocation of blocks*/ + CustomDataLayer *layers; /* CustomDataLayers, ordered by type */ + int totlayer, maxlayer; /* number of layers, size of layers array */ + int totsize, pad; /* in editmode, total size of all data layers */ + void *pool; /* Bmesh: Memory pool for allocation of blocks */ + CustomDataExternal *external; /* external file storing customdata layers */ } CustomData; /* CustomData.type */ @@ -115,6 +120,10 @@ typedef struct CustomData { #define CD_FLAG_NOFREE (1<<1) /* indicates the layer is only temporary, also implies no copy */ #define CD_FLAG_TEMPORARY ((1<<2)|CD_FLAG_NOCOPY) +/* indicates the layer is stored in an external file */ +#define CD_FLAG_EXTERNAL (1<<3) +/* indicates external data is read into memory */ +#define CD_FLAG_IN_MEMORY (1<<4) /* Limits */ #define MAX_MTFACE 8 diff --git a/source/blender/makesdna/DNA_meshdata_types.h b/source/blender/makesdna/DNA_meshdata_types.h index d53a7833d0e..48e361afdae 100644 --- a/source/blender/makesdna/DNA_meshdata_types.h +++ b/source/blender/makesdna/DNA_meshdata_types.h @@ -182,10 +182,11 @@ typedef struct PartialVisibility { } PartialVisibility; /* mvert->flag (1=SELECT) */ -#define ME_SPHERETEST 2 -#define ME_SPHERETEMP 4 -#define ME_HIDE 16 +#define ME_SPHERETEST 2 +#define ME_SPHERETEMP 4 +#define ME_HIDE 16 #define ME_VERT_MERGED (1<<6) +#define ME_VERT_PBVH_UPDATE (1<<7) /* medge->flag (1=SELECT)*/ #define ME_EDGEDRAW (1<<1) diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h index 040dc4dbd78..c599c8a7e0f 100644 --- a/source/blender/makesdna/DNA_modifier_types.h +++ b/source/blender/makesdna/DNA_modifier_types.h @@ -585,14 +585,14 @@ typedef struct ExplodeModifierData { typedef struct MultiresModifierData { ModifierData modifier; - struct MVert *undo_verts; /* Store DerivedMesh vertices for multires undo */ - int undo_verts_tot; /* Length of undo_verts array */ - char undo_signal; /* If true, signals to replace verts with undo verts */ - - char lvl, totlvl; - char simple; + char lvl, sculptlvl, renderlvl, totlvl; + char simple, flags, pad[2]; } MultiresModifierData; +typedef enum { + eMultiresModifierFlag_ControlEdges = (1<<0), +} MultiresModifierFlag; + typedef struct FluidsimModifierData { ModifierData modifier; diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h index 111a90ed389..32143015885 100644 --- a/source/blender/makesdna/DNA_scene_types.h +++ b/source/blender/makesdna/DNA_scene_types.h @@ -499,7 +499,7 @@ typedef struct Paint { void *paint_cursor; unsigned char paint_cursor_col[4]; - int pad; + int flags; } Paint; typedef struct ImagePaintSettings { @@ -1061,14 +1061,18 @@ typedef struct Scene { #define FFMPEG_MULTIPLEX_AUDIO 1 #define FFMPEG_AUTOSPLIT_OUTPUT 2 +/* Paint.flags */ +typedef enum { + PAINT_SHOW_BRUSH = 1, + PAINT_FAST_NAVIGATE = 2 +} PaintFlags; + /* Sculpt.flags */ +/* These can eventually be moved to paint flags? */ typedef enum SculptFlags { SCULPT_SYMM_X = 1, SCULPT_SYMM_Y = 2, SCULPT_SYMM_Z = 4, - SCULPT_INPUT_SMOOTH = 8, - SCULPT_DRAW_FAST = 16, - SCULPT_DRAW_BRUSH = 32, SCULPT_LOCK_X = 64, SCULPT_LOCK_Y = 128, SCULPT_LOCK_Z = 256 diff --git a/source/blender/makesdna/DNA_space_types.h b/source/blender/makesdna/DNA_space_types.h index 49893e9c015..40d59b666bf 100644 --- a/source/blender/makesdna/DNA_space_types.h +++ b/source/blender/makesdna/DNA_space_types.h @@ -667,6 +667,7 @@ enum FileSortTypeE { #define TEXTFILE 512 #define MOVIEFILE_ICON 1024 /* movie file that preview can't load */ #define FOLDERFILE 2048 /* represents folders for filtering */ +#define BTXFILE 4096 /* SpaceImage->dt_uv */ #define SI_UVDT_OUTLINE 0 diff --git a/source/blender/makesdna/DNA_view3d_types.h b/source/blender/makesdna/DNA_view3d_types.h index 74d9fa59e50..d8a8ec063d0 100644 --- a/source/blender/makesdna/DNA_view3d_types.h +++ b/source/blender/makesdna/DNA_view3d_types.h @@ -211,6 +211,7 @@ typedef struct View3D { /* RegionView3d->rflag */ #define RV3D_FLYMODE 2 #define RV3D_CLIPPING 4 +#define RV3D_NAVIGATING 8 /* RegionView3d->viewlock */ #define RV3D_LOCKED 1 diff --git a/source/blender/makesrna/intern/rna_brush.c b/source/blender/makesrna/intern/rna_brush.c index 2049618628b..7f7126c659a 100644 --- a/source/blender/makesrna/intern/rna_brush.c +++ b/source/blender/makesrna/intern/rna_brush.c @@ -273,6 +273,11 @@ static void rna_def_brush(BlenderRNA *brna) RNA_def_property_boolean_sdna(prop, NULL, "flag", BRUSH_PERSISTENT); RNA_def_property_ui_text(prop, "Persistent", "Sculpts on a persistent layer of the mesh."); RNA_def_property_update(prop, 0, "rna_Brush_update"); + + prop= RNA_def_property(srna, "use_accumulate", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flag", BRUSH_ACCUMULATE); + RNA_def_property_ui_text(prop, "Accumulate", "Accumulate stroke dabs on top of each other."); + RNA_def_property_update(prop, 0, "rna_Brush_update"); /* not exposed in the interface yet prop= RNA_def_property(srna, "fixed_tex", PROP_BOOLEAN, PROP_NONE); diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c index f2683e39c54..aee2b048f6b 100644 --- a/source/blender/makesrna/intern/rna_modifier.c +++ b/source/blender/makesrna/intern/rna_modifier.c @@ -344,10 +344,43 @@ static void rna_MultiresModifier_level_range(PointerRNA *ptr, int *min, int *max { MultiresModifierData *mmd = (MultiresModifierData*)ptr->data; - *min = 1; + *min = 0; *max = mmd->totlvl; } +static int rna_MultiresModifier_external_get(PointerRNA *ptr) +{ + Object *ob= (Object*)ptr->id.data; + Mesh *me= ob->data; + + return CustomData_external_test(&me->fdata, CD_MDISPS); +} + +static void rna_MultiresModifier_filename_get(PointerRNA *ptr, char *value) +{ + Object *ob= (Object*)ptr->id.data; + CustomDataExternal *external= ((Mesh*)ob->data)->fdata.external; + + BLI_strncpy(value, (external)? external->filename: "", sizeof(external->filename)); +} + +static void rna_MultiresModifier_filename_set(PointerRNA *ptr, const char *value) +{ + Object *ob= (Object*)ptr->id.data; + CustomDataExternal *external= ((Mesh*)ob->data)->fdata.external; + + if(external) + BLI_strncpy(external->filename, value, sizeof(external->filename)); +} + +static int rna_MultiresModifier_filename_length(PointerRNA *ptr) +{ + Object *ob= (Object*)ptr->id.data; + CustomDataExternal *external= ((Mesh*)ob->data)->fdata.external; + + return strlen((external)? external->filename: ""); +} + static void modifier_object_set(Object *self, Object **ob_p, int type, PointerRNA value) { Object *ob= value.data; @@ -496,22 +529,20 @@ static void rna_def_modifier_subsurf(BlenderRNA *brna) rna_def_property_subdivision_common(srna, "subdivType"); - prop= RNA_def_property(srna, "levels", PROP_INT, PROP_NONE); + prop= RNA_def_property(srna, "levels", PROP_INT, PROP_UNSIGNED); RNA_def_property_int_sdna(prop, NULL, "levels"); - RNA_def_property_range(prop, 1, 6); - RNA_def_property_ui_range(prop, 1, 6, 1, 0); + RNA_def_property_ui_range(prop, 0, 6, 1, 0); RNA_def_property_ui_text(prop, "Levels", "Number of subdivisions to perform."); RNA_def_property_update(prop, 0, "rna_Modifier_update"); - prop= RNA_def_property(srna, "render_levels", PROP_INT, PROP_NONE); + prop= RNA_def_property(srna, "render_levels", PROP_INT, PROP_UNSIGNED); RNA_def_property_int_sdna(prop, NULL, "renderLevels"); - RNA_def_property_range(prop, 1, 6); - RNA_def_property_ui_range(prop, 1, 6, 1, 0); + RNA_def_property_ui_range(prop, 0, 6, 1, 0); RNA_def_property_ui_text(prop, "Render Levels", "Number of subdivisions to perform when rendering."); - prop= RNA_def_property(srna, "optimal_draw", PROP_BOOLEAN, PROP_NONE); + prop= RNA_def_property(srna, "optimal_display", PROP_BOOLEAN, PROP_NONE); RNA_def_property_boolean_sdna(prop, NULL, "flags", eSubsurfModifierFlag_ControlEdges); - RNA_def_property_ui_text(prop, "Optimal Draw", "Skip drawing/rendering of interior subdivided edges"); + RNA_def_property_ui_text(prop, "Optimal Display", "Skip drawing/rendering of interior subdivided edges"); RNA_def_property_update(prop, 0, "rna_Modifier_update"); prop= RNA_def_property(srna, "subsurf_uv", PROP_BOOLEAN, PROP_NONE); @@ -532,11 +563,42 @@ static void rna_def_modifier_multires(BlenderRNA *brna) rna_def_property_subdivision_common(srna, "simple"); - prop= RNA_def_property(srna, "level", PROP_INT, PROP_NONE); + prop= RNA_def_property(srna, "levels", PROP_INT, PROP_UNSIGNED); RNA_def_property_int_sdna(prop, NULL, "lvl"); - RNA_def_property_ui_text(prop, "Level", ""); + RNA_def_property_ui_text(prop, "Levels", "Number of subdivisions to use in the viewport."); RNA_def_property_int_funcs(prop, NULL, NULL, "rna_MultiresModifier_level_range"); RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop= RNA_def_property(srna, "sculpt_levels", PROP_INT, PROP_UNSIGNED); + RNA_def_property_int_sdna(prop, NULL, "sculptlvl"); + RNA_def_property_ui_text(prop, "Sculpt Levels", "Number of subdivisions to use in sculpt mode."); + RNA_def_property_int_funcs(prop, NULL, NULL, "rna_MultiresModifier_level_range"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop= RNA_def_property(srna, "render_levels", PROP_INT, PROP_UNSIGNED); + RNA_def_property_int_sdna(prop, NULL, "renderlvl"); + RNA_def_property_ui_text(prop, "Render Levels", ""); + RNA_def_property_int_funcs(prop, NULL, NULL, "rna_MultiresModifier_level_range"); + + prop= RNA_def_property(srna, "total_levels", PROP_INT, PROP_UNSIGNED); + RNA_def_property_int_sdna(prop, NULL, "totlvl"); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_ui_text(prop, "Total Levels", "Number of subdivisions for which displacements are stored."); + + prop= RNA_def_property(srna, "external", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_boolean_funcs(prop, "rna_MultiresModifier_external_get", NULL); + RNA_def_property_ui_text(prop, "External", "Store multires displacements outside the .blend file, to save memory."); + + prop= RNA_def_property(srna, "filename", PROP_STRING, PROP_FILEPATH); + RNA_def_property_string_funcs(prop, "rna_MultiresModifier_filename_get", "rna_MultiresModifier_filename_length", "rna_MultiresModifier_filename_set"); + RNA_def_property_ui_text(prop, "Filename", "Path to external displacements file."); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop= RNA_def_property(srna, "optimal_display", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flags", eMultiresModifierFlag_ControlEdges); + RNA_def_property_ui_text(prop, "Optimal Display", "Skip drawing/rendering of interior subdivided edges"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); } static void rna_def_modifier_lattice(BlenderRNA *brna) diff --git a/source/blender/makesrna/intern/rna_sculpt_paint.c b/source/blender/makesrna/intern/rna_sculpt_paint.c index 9e13ae1191d..c8fb11ced95 100644 --- a/source/blender/makesrna/intern/rna_sculpt_paint.c +++ b/source/blender/makesrna/intern/rna_sculpt_paint.c @@ -190,6 +190,14 @@ static void rna_def_paint(BlenderRNA *brna) RNA_def_property_flag(prop, PROP_EDITABLE); RNA_def_property_ui_text(prop, "Brush", "Active paint brush."); RNA_def_property_update(prop, NC_BRUSH|NA_EDITED, NULL); + + prop= RNA_def_property(srna, "show_brush", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flags", PAINT_SHOW_BRUSH); + RNA_def_property_ui_text(prop, "Show Brush", ""); + + prop= RNA_def_property(srna, "fast_navigate", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flags", PAINT_FAST_NAVIGATE); + RNA_def_property_ui_text(prop, "Fast Navigate", "For multires, show low resolution while navigating the view."); } static void rna_def_sculpt(BlenderRNA *brna) @@ -223,14 +231,6 @@ static void rna_def_sculpt(BlenderRNA *brna) prop= RNA_def_property(srna, "lock_z", PROP_BOOLEAN, PROP_NONE); RNA_def_property_boolean_sdna(prop, NULL, "flags", SCULPT_LOCK_Z); RNA_def_property_ui_text(prop, "Lock Z", "Disallow changes to the Z axis of vertices."); - - prop= RNA_def_property(srna, "show_brush", PROP_BOOLEAN, PROP_NONE); - RNA_def_property_boolean_sdna(prop, NULL, "flags", SCULPT_DRAW_BRUSH); - RNA_def_property_ui_text(prop, "Show Brush", ""); - - prop= RNA_def_property(srna, "partial_redraw", PROP_BOOLEAN, PROP_NONE); - RNA_def_property_boolean_sdna(prop, NULL, "flags", SCULPT_DRAW_FAST); - RNA_def_property_ui_text(prop, "Partial Redraw", "Optimize sculpting by only refreshing modified faces."); } static void rna_def_vertex_paint(BlenderRNA *brna) diff --git a/source/blender/render/intern/source/convertblender.c b/source/blender/render/intern/source/convertblender.c index 8f8e083523b..d0f7b2d60a5 100644 --- a/source/blender/render/intern/source/convertblender.c +++ b/source/blender/render/intern/source/convertblender.c @@ -1710,8 +1710,12 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem if(origindex) { for(a=0; a<totface; a++) strandbuf->totbound= MAX2(strandbuf->totbound, origindex[a]); - strandbuf->totbound++; } + else { + for(a=0; a<totface; a++) + strandbuf->totbound= MAX2(strandbuf->totbound, a); + } + strandbuf->totbound++; strandbuf->bound= MEM_callocN(sizeof(StrandBound)*strandbuf->totbound, "StrandBound"); sbound= strandbuf->bound; diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c index 81666fbe023..87e4048971b 100644 --- a/source/blender/windowmanager/intern/wm_files.c +++ b/source/blender/windowmanager/intern/wm_files.c @@ -81,6 +81,7 @@ #include "ED_datafiles.h" #include "ED_object.h" #include "ED_screen.h" +#include "ED_sculpt.h" #include "ED_util.h" #include "GHOST_C-api.h" @@ -497,6 +498,7 @@ void WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports } ED_object_exit_editmode(C, EM_DO_UNDO); + ED_sculpt_force_update(C); do_history(di, reports); diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c index 6d028a63c1d..d1e004a0a20 100644 --- a/source/blender/windowmanager/intern/wm_operators.c +++ b/source/blender/windowmanager/intern/wm_operators.c @@ -683,6 +683,8 @@ void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type) RNA_def_property_flag(prop, PROP_HIDDEN); prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", ""); RNA_def_property_flag(prop, PROP_HIDDEN); + prop= RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", ""); + RNA_def_property_flag(prop, PROP_HIDDEN); prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", ""); RNA_def_property_flag(prop, PROP_HIDDEN); |