[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [27864] trunk/blender: Rest shape key for cloth option, this makes it possible

Brecht Van Lommel brecht at blender.org
Tue Mar 30 13:49:07 CEST 2010


Revision: 27864
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=27864
Author:   blendix
Date:     2010-03-30 13:49:07 +0200 (Tue, 30 Mar 2010)

Log Message:
-----------
Rest shape key for cloth option, this makes it possible
to specify different spring lengths.

Implementation is quite ugly because the shape key has to be pulled
through the modifier stack somehow, need a more flexible data mask
system to solve this properly.

(commits 27773,27775,27778 by Brecht from render25 branch)

Modified Paths:
--------------
    trunk/blender/release/scripts/ui/properties_physics_cloth.py
    trunk/blender/source/blender/blenkernel/BKE_cloth.h
    trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c
    trunk/blender/source/blender/blenkernel/intern/cloth.c
    trunk/blender/source/blender/blenkernel/intern/customdata.c
    trunk/blender/source/blender/blenkernel/intern/modifier.c
    trunk/blender/source/blender/editors/space_image/image_buttons.c
    trunk/blender/source/blender/makesdna/DNA_cloth_types.h
    trunk/blender/source/blender/makesdna/DNA_customdata_types.h
    trunk/blender/source/blender/makesrna/intern/rna_cloth.c
    trunk/blender/source/blender/makesrna/intern/rna_internal.h
    trunk/blender/source/blender/makesrna/intern/rna_key.c

Modified: trunk/blender/release/scripts/ui/properties_physics_cloth.py
===================================================================
--- trunk/blender/release/scripts/ui/properties_physics_cloth.py	2010-03-30 11:38:06 UTC (rev 27863)
+++ trunk/blender/release/scripts/ui/properties_physics_cloth.py	2010-03-30 11:49:07 UTC (rev 27864)
@@ -129,7 +129,12 @@
                 col.prop(cloth, "goal_friction", text="Friction")
             """
 
+            key = ob.data.shape_keys
 
+            if key:
+                col.label(text="Rest Shape Key:")
+                col.prop_object(cloth, "rest_shape_key", key, "keys", text="")
+
 class PHYSICS_PT_cloth_cache(PhysicButtonsPanel):
     bl_label = "Cloth Cache"
     bl_default_closed = True

Modified: trunk/blender/source/blender/blenkernel/BKE_cloth.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_cloth.h	2010-03-30 11:38:06 UTC (rev 27863)
+++ trunk/blender/source/blender/blenkernel/BKE_cloth.h	2010-03-30 11:49:07 UTC (rev 27864)
@@ -126,6 +126,7 @@
 	float 	mass;		/* mass / weight of the vertex		*/
 	float 	goal;		/* goal, from SB			*/
 	float	impulse[3];	/* used in collision.c */
+	float	*xrest;		/* temporary valid for building springs */
 	unsigned int impulse_count; /* same as above */
 	float 	avg_spring_len; /* average length of connected springs */
 	float 	struct_stiff;
@@ -240,6 +241,7 @@
 DerivedMesh *clothModifier_do ( ClothModifierData *clmd, struct Scene *scene, Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc );
 
 void cloth_update_normals ( ClothVertex *verts, int nVerts, MFace *face, int totface );
+int cloth_uses_vgroup(ClothModifierData *clmd);
 
 // needed for collision.c
 void bvhtree_update_from_cloth ( ClothModifierData *clmd, int moving );

Modified: trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c	2010-03-30 11:38:06 UTC (rev 27863)
+++ trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c	2010-03-30 11:49:07 UTC (rev 27864)
@@ -36,6 +36,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "DNA_cloth_types.h"
 #include "DNA_key_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
@@ -49,6 +50,7 @@
 
 #include "BKE_cdderivedmesh.h"
 #include "BKE_displist.h"
+#include "BKE_key.h"
 #include "BKE_modifier.h"
 #include "BKE_mesh.h"
 #include "BKE_object.h"
@@ -1450,55 +1452,88 @@
 
 /* orco custom data layer */
 
-static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, EditMesh *em)
+static void *get_orco_coords_dm(Object *ob, EditMesh *em, int layer, int *free)
 {
+	*free= 0;
+
+	if(layer == CD_ORCO) {
+		/* get original coordinates */
+		*free= 1;
+
+		if(em)
+			return (float(*)[3])get_editmesh_orco_verts(em);
+		else
+			return (float(*)[3])get_mesh_orco_verts(ob);
+	}
+	else if(layer == CD_CLOTH_ORCO) {
+		/* apply shape key for cloth, this should really be solved
+		   by a more flexible customdata system, but not simple */
+		if(!em) {
+			ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
+			KeyBlock *kb= key_get_keyblock(ob_get_key(ob), clmd->sim_parms->shapekey_rest);
+
+			if(kb->data)
+				return kb->data;
+		}
+
+		return NULL;
+	}
+
+	return NULL;
+}
+
+static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, EditMesh *em, int layer)
+{
 	DerivedMesh *dm;
 	float (*orco)[3];
+	int free;
 
-	if(em) {
-		dm= CDDM_from_editmesh(em, me);
-		orco= (float(*)[3])get_editmesh_orco_verts(em);
+	if(em) dm= CDDM_from_editmesh(em, me);
+	else dm= CDDM_from_mesh(me, ob);
+
+	orco= get_orco_coords_dm(ob, em, layer, &free);
+
+	if(orco) {
+		CDDM_apply_vert_coords(dm, orco);
+		if(free) MEM_freeN(orco);
 	}
-	else {
-		dm= CDDM_from_mesh(me, ob);
-		orco= (float(*)[3])get_mesh_orco_verts(ob);
-	}
 
-	CDDM_apply_vert_coords(dm, orco);
 	CDDM_calc_normals(dm);
-	MEM_freeN(orco);
 
 	return dm;
 }
 
-static void add_orco_dm(Object *ob, EditMesh *em, DerivedMesh *dm, DerivedMesh *orcodm)
+static void add_orco_dm(Object *ob, EditMesh *em, DerivedMesh *dm, DerivedMesh *orcodm, int layer)
 {
 	float (*orco)[3], (*layerorco)[3];
-	int totvert;
+	int totvert, free;
 
 	totvert= dm->getNumVerts(dm);
 
 	if(orcodm) {
 		orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
+		free= 1;
 
 		if(orcodm->getNumVerts(orcodm) == totvert)
 			orcodm->getVertCos(orcodm, orco);
 		else
 			dm->getVertCos(dm, orco);
 	}
-	else {
-		if(em) orco= (float(*)[3])get_editmesh_orco_verts(em);
-		else orco= (float(*)[3])get_mesh_orco_verts(ob);
-	}
+	else
+		orco= get_orco_coords_dm(ob, em, layer, &free);
 
-	transform_mesh_orco_verts(ob->data, orco, totvert, 0);
+	if(orco) {
+		if(layer == CD_ORCO)
+			transform_mesh_orco_verts(ob->data, orco, totvert, 0);
 
-	if((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
-		memcpy(layerorco, orco, sizeof(float)*totvert);
-		MEM_freeN(orco);
+		if(!(layerorco = DM_get_vert_data_layer(dm, layer))) {
+			DM_add_vert_layer(dm, layer, CD_CALLOC, NULL);
+			layerorco = DM_get_vert_data_layer(dm, layer);
+		}
+
+		memcpy(layerorco, orco, sizeof(float)*3*totvert);
+		if(free) MEM_freeN(orco);
 	}
-	else
-		DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
 }
 
 /* weight paint colors */
@@ -1604,9 +1639,9 @@
 	Mesh *me = ob->data;
 	ModifierData *firstmd, *md;
 	LinkNode *datamasks, *curr;
-	CustomDataMask mask;
+	CustomDataMask mask, nextmask;
 	float (*deformedVerts)[3] = NULL;
-	DerivedMesh *dm, *orcodm, *finaldm;
+	DerivedMesh *dm, *orcodm, *clothorcodm, *finaldm;
 	int numVerts = me->totvert;
 	int required_mode;
 
@@ -1679,6 +1714,7 @@
 	 */
 	dm = NULL;
 	orcodm = NULL;
+	clothorcodm = NULL;
 
 	for(;md; md = md->next, curr = curr->next) {
 		ModifierTypeInfo *mti = modifierType_getInfo(md->type);
@@ -1695,12 +1731,14 @@
 		if(useDeform < 0 && mti->dependsOnTime && mti->dependsOnTime(md)) continue;
 
 		/* add an orco layer if needed by this modifier */
-		if(dm && mti->requiredDataMask) {
+		if(mti->requiredDataMask)
 			mask = mti->requiredDataMask(ob, md);
-			if(mask & CD_MASK_ORCO)
-				add_orco_dm(ob, NULL, dm, orcodm);
-		}
+		else
+			mask = 0;
 
+		if(dm && (mask & CD_MASK_ORCO))
+			add_orco_dm(ob, NULL, dm, orcodm, CD_ORCO);
+
 		/* How to apply modifier depends on (a) what we already have as
 		 * a result of previous modifiers (could be a DerivedMesh or just
 		 * deformed vertices) and (b) what type the modifier is.
@@ -1766,26 +1804,20 @@
 				}
 			}
 
-			/* create an orco derivedmesh in parallel */
+			/* determine which data layers are needed by following modifiers */
+			if(curr->next)
+				nextmask= (CustomDataMask)GET_INT_FROM_POINTER(curr->next->link);
+			else
+				nextmask= dataMask;
+			
+			/* set the DerivedMesh to only copy needed data */
 			mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
-			if(mask & CD_MASK_ORCO) {
-				if(!orcodm)
-					orcodm= create_orco_dm(ob, me, NULL);
-
-				mask &= ~CD_MASK_ORCO;
-				DM_set_only_copy(orcodm, mask);
-				ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, 0);
-
-				if(ndm) {
-					/* if the modifier returned a new dm, release the old one */
-					if(orcodm && orcodm != ndm) orcodm->release(orcodm);
-					orcodm = ndm;
-				}
-			}
-
-			/* set the DerivedMesh to only copy needed data */
 			DM_set_only_copy(dm, mask);
 			
+			/* add cloth rest shape key if need */
+			if(mask & CD_MASK_CLOTH_ORCO)
+				add_orco_dm(ob, NULL, dm, clothorcodm, CD_CLOTH_ORCO);
+
 			/* add an origspace layer if needed */
 			if(((CustomDataMask)GET_INT_FROM_POINTER(curr->link)) & CD_MASK_ORIGSPACE)
 				if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
@@ -1806,6 +1838,38 @@
 					deformedVerts = NULL;
 				}
 			} 
+
+			/* create an orco derivedmesh in parallel */
+			if(nextmask & CD_MASK_ORCO) {
+				if(!orcodm)
+					orcodm= create_orco_dm(ob, me, NULL, CD_ORCO);
+
+				nextmask &= ~CD_MASK_ORCO;
+				DM_set_only_copy(orcodm, nextmask);
+				ndm = mti->applyModifier(md, ob, orcodm, useRenderParams, 0);
+
+				if(ndm) {
+					/* if the modifier returned a new dm, release the old one */
+					if(orcodm && orcodm != ndm) orcodm->release(orcodm);
+					orcodm = ndm;
+				}
+			}
+
+			/* create cloth orco derivedmesh in parallel */
+			if(nextmask & CD_MASK_CLOTH_ORCO) {
+				if(!clothorcodm)
+					clothorcodm= create_orco_dm(ob, me, NULL, CD_CLOTH_ORCO);
+
+				nextmask &= ~CD_MASK_CLOTH_ORCO;
+				DM_set_only_copy(clothorcodm, nextmask);
+				ndm = mti->applyModifier(md, ob, clothorcodm, useRenderParams, 0);
+
+				if(ndm) {
+					/* if the modifier returned a new dm, release the old one */
+					if(clothorcodm && clothorcodm != ndm) clothorcodm->release(clothorcodm);
+					clothorcodm = ndm;
+				}
+			}
 		}
 		
 		/* grab modifiers until index i */
@@ -1846,16 +1910,18 @@
 
 	/* add an orco layer if needed */
 	if(dataMask & CD_MASK_ORCO) {
-		add_orco_dm(ob, NULL, finaldm, orcodm);
+		add_orco_dm(ob, NULL, finaldm, orcodm, CD_ORCO);
 
 		if(deform_r && *deform_r)
-			add_orco_dm(ob, NULL, *deform_r, NULL);
+			add_orco_dm(ob, NULL, *deform_r, NULL, CD_ORCO);
 	}
 
 	*final_r = finaldm;
 
 	if(orcodm)
 		orcodm->release(orcodm);
+	if(clothorcodm)
+		clothorcodm->release(clothorcodm);
 
 	if(deformedVerts && deformedVerts != inputVertexCos)
 		MEM_freeN(deformedVerts);
@@ -1930,7 +1996,7 @@
 		if(dm && mti->requiredDataMask) {
 			mask = mti->requiredDataMask(ob, md);
 			if(mask & CD_MASK_ORCO)
-				add_orco_dm(ob, em, dm, orcodm);
+				add_orco_dm(ob, em, dm, orcodm, CD_ORCO);
 		}
 
 		/* How to apply modifier depends on (a) what we already have as
@@ -1987,7 +2053,7 @@
 			mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
 			if(mask & CD_MASK_ORCO) {
 				if(!orcodm)
-					orcodm= create_orco_dm(ob, ob->data, em);
+					orcodm= create_orco_dm(ob, ob->data, em, CD_ORCO);
 
 				mask &= ~CD_MASK_ORCO;
 				DM_set_only_copy(orcodm, mask);
@@ -2060,7 +2126,7 @@
 
 	/* add an orco layer if needed */
 	if(dataMask & CD_MASK_ORCO)
-		add_orco_dm(ob, em, *final_r, orcodm);
+		add_orco_dm(ob, em, *final_r, orcodm, CD_ORCO);
 
 	if(orcodm)
 		orcodm->release(orcodm);

Modified: trunk/blender/source/blender/blenkernel/intern/cloth.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/cloth.c	2010-03-30 11:38:06 UTC (rev 27863)
+++ trunk/blender/source/blender/blenkernel/intern/cloth.c	2010-03-30 11:49:07 UTC (rev 27864)
@@ -721,6 +721,15 @@
 }
 
 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list