[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24130] branches/sculpt25/source/blender: Moved the PBVH from sculpt session to DerivedMesh/CDDM.

Nicholas Bishop nicholasbishop at gmail.com
Wed Oct 28 07:06:06 CET 2009


Revision: 24130
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24130
Author:   nicholasbishop
Date:     2009-10-28 07:06:05 +0100 (Wed, 28 Oct 2009)

Log Message:
-----------
Moved the PBVH from sculpt session to DerivedMesh/CDDM.

* Multires sculpting appears to work now
* PBVH gets recalculated in some cases where it shouldn't, haven't looked into this yet

Modified Paths:
--------------
    branches/sculpt25/source/blender/blenkernel/BKE_DerivedMesh.h
    branches/sculpt25/source/blender/blenkernel/BKE_paint.h
    branches/sculpt25/source/blender/blenkernel/intern/cdderivedmesh.c
    branches/sculpt25/source/blender/blenkernel/intern/object.c
    branches/sculpt25/source/blender/blenkernel/intern/subsurf_ccg.c
    branches/sculpt25/source/blender/editors/sculpt_paint/sculpt.c
    branches/sculpt25/source/blender/editors/space_view3d/drawobject.c

Modified: branches/sculpt25/source/blender/blenkernel/BKE_DerivedMesh.h
===================================================================
--- branches/sculpt25/source/blender/blenkernel/BKE_DerivedMesh.h	2009-10-27 23:43:20 UTC (rev 24129)
+++ branches/sculpt25/source/blender/blenkernel/BKE_DerivedMesh.h	2009-10-28 06:06:05 UTC (rev 24130)
@@ -59,6 +59,8 @@
 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
@@ -73,6 +75,7 @@
 	int needsFree; /* checked on ->release, is set to 0 for cached results */
 	int deformedOnly; /* set by modifier stack if only deformed from original */
 	BVHCache bvhCache;
+
 	struct GPUDrawObject *drawObject;
 
 	/* Misc. Queries */
@@ -180,6 +183,14 @@
 	/* 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)(DerivedMesh *dm);
+
 	/* Drawing Operations */
 
 	/* Draw all vertices as bgl points (no options) */
@@ -204,7 +215,7 @@
 	 *
 	 * Also called for *final* editmode DerivedMeshes
 	 */
-	void (*drawFacesSolid)(DerivedMesh *dm, void *tree, float (*partial_redraw_planes)[4],
+	void (*drawFacesSolid)(DerivedMesh *dm, float (*partial_redraw_planes)[4],
 	                       int (*setMaterial)(int, void *attribs));
 
 	/* Draw all faces

Modified: branches/sculpt25/source/blender/blenkernel/BKE_paint.h
===================================================================
--- branches/sculpt25/source/blender/blenkernel/BKE_paint.h	2009-10-27 23:43:20 UTC (rev 24129)
+++ branches/sculpt25/source/blender/blenkernel/BKE_paint.h	2009-10-28 06:06:05 UTC (rev 24130)
@@ -69,17 +69,15 @@
 	struct MFace *mface;
 	int totvert, totface;
 	float *face_normals;
+	struct PBVH *tree;
 	
 	/* Mesh connectivity */
 	struct ListBase *fmap;
-	struct IndexNode *fmap_mem;
-	int fmap_size;
 
 	/* Used temporarily per-stroke */
 	float *vertexcosnos;
 
 	/* Partial redraw */
-	struct PBVH *tree;
 	int partial_redraw;
 	
 	/* Used to cache the render of the active texture */

Modified: branches/sculpt25/source/blender/blenkernel/intern/cdderivedmesh.c
===================================================================
--- branches/sculpt25/source/blender/blenkernel/intern/cdderivedmesh.c	2009-10-27 23:43:20 UTC (rev 24129)
+++ branches/sculpt25/source/blender/blenkernel/intern/cdderivedmesh.c	2009-10-28 06:06:05 UTC (rev 24130)
@@ -77,6 +77,12 @@
 	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,82 @@
 	no_r[2] = no[2]/32767.f;
 }
 
+/* Updates all the face and vertex normals in a node
+
+   Note: the correctness of some vertex normals will be a little
+   off, not sure if this will be noticeable or not */
+static void update_node_normals(const int *face_indices,
+				const int *vert_indices,
+				int totface, int totvert, void *data)
+{
+	DerivedMesh *dm = data;
+	CDDerivedMesh *cddm = data;
+	float (*face_nors)[3];
+	int i;
+
+	/* make a face normal layer if not present */
+	face_nors = CustomData_get_layer(&dm->faceData, CD_NORMAL);
+	if(!face_nors)
+		face_nors = CustomData_add_layer(&dm->faceData, CD_NORMAL, CD_CALLOC,
+		                                 NULL, dm->numFaceData);
+
+	/* Update face normals */
+	for(i = 0; i < totface; ++i) {
+		MFace *f = cddm->mface + face_indices[i];
+		float *fn = face_nors[face_indices[i]];
+
+		if(f->v4)
+			CalcNormFloat4(cddm->mvert[f->v1].co, cddm->mvert[f->v2].co,
+			               cddm->mvert[f->v3].co, cddm->mvert[f->v4].co, fn);
+		else
+			CalcNormFloat(cddm->mvert[f->v1].co, cddm->mvert[f->v2].co,
+			              cddm->mvert[f->v3].co, fn);
+	}
+
+	/* Update vertex normals */
+	for(i = 0; i < totvert; ++i) {
+		const int v = vert_indices[i];
+		float no[3] = {0,0,0};
+		IndexNode *face;
+
+		for(face = cddm->fmap[v].first; face; face = face->next)
+			VecAddf(no, no, face_nors[face->index]);
+
+		Normalize(no);
+		
+		cddm->mvert[v].no[0] = no[0] * 32767;
+		cddm->mvert[v].no[1] = no[1] * 32767;
+		cddm->mvert[v].no[2] = no[2] * 32767;
+	}
+}
+
+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));
+		printf("rebuild fmap\n");
+	}
+
+	return cddm->fmap;
+}
+
+static struct PBVH *cdDM_getPBVH(DerivedMesh *dm)
+{
+	CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
+
+	if(!cddm->pbvh) {
+		cddm->pbvh = BLI_pbvh_new(update_node_normals, cddm);
+		BLI_pbvh_build(cddm->pbvh, cddm->mface, cddm->mvert,
+			       dm->getNumFaces(dm), dm->getNumVerts(dm));
+		printf("rebuild pbvh\n");
+	}
+
+	return cddm->pbvh;
+}
+
 static void cdDM_drawVerts(DerivedMesh *dm)
 {
 	CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
@@ -419,7 +501,7 @@
 	return 1;
 }
 
-static void cdDM_drawFacesSolid(DerivedMesh *dm, void *tree,
+static void cdDM_drawFacesSolid(DerivedMesh *dm,
 				float (*partial_redraw_planes)[4],
 				int (*setMaterial)(int, void *attribs))
 {
@@ -437,19 +519,19 @@
 	glVertex3fv(mvert[index].co);	\
 }
 
-	if(tree) {
-		BLI_pbvh_search(tree, BLI_pbvh_update_search_cb,
+	if(cddm->pbvh) {
+		BLI_pbvh_search(cddm->pbvh, BLI_pbvh_update_search_cb,
 				PBVH_NodeData, NULL, NULL,
 				PBVH_SEARCH_UPDATE);
 
 		if(partial_redraw_planes) {
-			BLI_pbvh_search(tree, planes_contain_AABB,
+			BLI_pbvh_search(cddm->pbvh, planes_contain_AABB,
 					partial_redraw_planes,
 					draw_partial_cb, PBVH_DrawData,
 					PBVH_SEARCH_MODIFIED);
 		}
 		else {
-			BLI_pbvh_search(tree, find_all, NULL,
+			BLI_pbvh_search(cddm->pbvh, find_all, NULL,
 					draw_partial_cb, PBVH_DrawData,
 					PBVH_SEARCH_NORMAL);
 
@@ -1376,12 +1458,21 @@
 	}
 }
 
+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 ****************/
@@ -1416,6 +1507,9 @@
 	dm->getVertCo = cdDM_getVertCo;
 	dm->getVertNo = cdDM_getVertNo;
 
+	dm->getPBVH = cdDM_getPBVH;
+	dm->getFaceMap = cdDM_getFaceMap;
+
 	dm->drawVerts = cdDM_drawVerts;
 
 	dm->drawUVEdges = cdDM_drawUVEdges;
@@ -1901,6 +1995,7 @@
 	}
 
 	if(DM_release(dm)) {
+		cdDM_free_internal(&mrdm->cddm);
 		MEM_freeN(mrdm->subco);
 		MEM_freeN(mrdm->orco);
 		if(mrdm->vert_face_map)

Modified: branches/sculpt25/source/blender/blenkernel/intern/object.c
===================================================================
--- branches/sculpt25/source/blender/blenkernel/intern/object.c	2009-10-27 23:43:20 UTC (rev 24129)
+++ branches/sculpt25/source/blender/blenkernel/intern/object.c	2009-10-28 06:06:05 UTC (rev 24130)
@@ -72,8 +72,6 @@
 #include "BLI_blenlib.h"
 #include "BLI_arithb.h"
 #include "BLI_editVert.h"
-#include "BLI_ghash.h"
-#include "BLI_pbvh.h"
 
 #include "BKE_utildefines.h"
 
@@ -231,12 +229,6 @@
 	if(ssp && *ssp) {
 		SculptSession *ss = *ssp;
 
-		if(ss->fmap)
-			MEM_freeN(ss->fmap);
-
-		if(ss->fmap_mem)
-			MEM_freeN(ss->fmap_mem);
-
 		if(ss->texcache)
 			MEM_freeN(ss->texcache);
 
@@ -246,9 +238,6 @@
 		if(ss->mesh_co_orig)
 			MEM_freeN(ss->mesh_co_orig);
 
-		if(ss->tree)
-			BLI_pbvh_free(ss->tree);
-
 		if(ss->face_normals)
 			MEM_freeN(ss->face_normals);
 

Modified: branches/sculpt25/source/blender/blenkernel/intern/subsurf_ccg.c
===================================================================
--- branches/sculpt25/source/blender/blenkernel/intern/subsurf_ccg.c	2009-10-27 23:43:20 UTC (rev 24129)
+++ branches/sculpt25/source/blender/blenkernel/intern/subsurf_ccg.c	2009-10-28 06:06:05 UTC (rev 24130)
@@ -1615,7 +1615,7 @@
 }
 
 	/* Only used by non-editmesh types */
-static void ccgDM_drawFacesSolid(DerivedMesh *dm, void *tree, float (*partial_redraw_planes)[4], int (*setMaterial)(int, void *attribs)) {
+static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes)[4], int (*setMaterial)(int, void *attribs)) {
 	CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
 	CCGSubSurf *ss = ccgdm->ss;
 	CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);

Modified: branches/sculpt25/source/blender/editors/sculpt_paint/sculpt.c
===================================================================
--- branches/sculpt25/source/blender/editors/sculpt_paint/sculpt.c	2009-10-27 23:43:20 UTC (rev 24129)
+++ branches/sculpt25/source/blender/editors/sculpt_paint/sculpt.c	2009-10-28 06:06:05 UTC (rev 24130)
@@ -193,57 +193,6 @@
 
 /*** BVH Tree ***/
 
-/* Updates all the face and vertex normals in a node
-
-   Note: the correctness of some vertex normals will be a little
-   off, not sure if this will be noticeable or not */
-static void sculpt_update_normals(const int *face_indices,
-				  const int *vert_indices,
-				  int totface, int totvert, void *data)
-{
-	SculptSession *ss = data;
-	int i;
-
-	/* Update face normals */
-	for(i = 0; i < totface; ++i) {
-		MFace *f = ss->mface + face_indices[i];
-		float *fn = ss->face_normals + face_indices[i] * 3;
-
-		if(f->v4)
-			CalcNormFloat4(ss->mvert[f->v1].co, ss->mvert[f->v2].co,
-			               ss->mvert[f->v3].co, ss->mvert[f->v4].co, fn);
-		else
-			CalcNormFloat(ss->mvert[f->v1].co, ss->mvert[f->v2].co,
-			              ss->mvert[f->v3].co, fn);
-	}
-
-	/* Update vertex normals */
-	for(i = 0; i < totvert; ++i) {
-		const int v = vert_indices[i];
-		float no[3] = {0,0,0};
-		IndexNode *face;
-
-		for(face = ss->fmap[v].first; face; face = face->next)
-			VecAddf(no, no, ss->face_normals + face->index*3);
-
-		Normalize(no);
-		
-		ss->mvert[v].no[0] = no[0] * 32767;
-		ss->mvert[v].no[1] = no[1] * 32767;
-		ss->mvert[v].no[2] = no[2] * 32767;
-	}
-}
-
-static void sculpt_rebuild_tree(SculptSession *ss)
-{
-	if(ss->tree)
-		BLI_pbvh_free(ss->tree);
-
-	ss->tree = BLI_pbvh_new(sculpt_update_normals, ss);
-	BLI_pbvh_build(ss->tree, ss->mface, ss->mvert, ss->totface,
-		       ss->totvert);
-}
-

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list