[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54022] trunk/blender: rigidbody: Add point cache support

Sergej Reich sergej.reich at googlemail.com
Wed Jan 23 06:56:34 CET 2013


Revision: 54022
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54022
Author:   sergof
Date:     2013-01-23 05:56:34 +0000 (Wed, 23 Jan 2013)
Log Message:
-----------
rigidbody: Add point cache support

Add read/write/interpolate functions.

In order to get rigid body point cache id from object it's now required to pass the
scene to BKE_ptcache_ids_from_object().

Rigid body cache is drawn in the orange color of the bullet logo.

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_ui/space_time.py
    trunk/blender/source/blender/blenkernel/BKE_pointcache.h
    trunk/blender/source/blender/blenkernel/intern/pointcache.c
    trunk/blender/source/blender/blenkernel/intern/rigidbody.c
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/blenloader/intern/writefile.c
    trunk/blender/source/blender/editors/object/object_edit.c
    trunk/blender/source/blender/editors/space_time/space_time.c
    trunk/blender/source/blender/makesdna/DNA_rigidbody_types.h
    trunk/blender/source/blender/makesdna/DNA_space_types.h
    trunk/blender/source/blender/makesrna/intern/rna_rigidbody.c
    trunk/blender/source/blender/makesrna/intern/rna_space.c

Modified: trunk/blender/release/scripts/startup/bl_ui/space_time.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_time.py	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/release/scripts/startup/bl_ui/space_time.py	2013-01-23 05:56:34 UTC (rev 54022)
@@ -152,6 +152,7 @@
         col.prop(st, "cache_cloth")
         col.prop(st, "cache_smoke")
         col.prop(st, "cache_dynamicpaint")
+        col.prop(st, "cache_rigidbody")
 
 
 class TIME_MT_frame(Menu):

Modified: trunk/blender/source/blender/blenkernel/BKE_pointcache.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_pointcache.h	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/source/blender/blenkernel/BKE_pointcache.h	2013-01-23 05:56:34 UTC (rev 54022)
@@ -67,6 +67,7 @@
 #define PTCACHE_TYPE_SMOKE_DOMAIN       3
 #define PTCACHE_TYPE_SMOKE_HIGHRES      4
 #define PTCACHE_TYPE_DYNAMICPAINT       5
+#define PTCACHE_TYPE_RIGIDBODY          6
 
 /* high bits reserved for flags that need to be stored in file */
 #define PTCACHE_TYPEFLAG_COMPRESS       (1 << 16)
@@ -91,6 +92,7 @@
 struct Scene;
 struct SmokeModifierData;
 struct SoftBody;
+struct RigidBodyWorld;
 
 /* temp structure for read/write */
 typedef struct PTCacheData {
@@ -260,6 +262,7 @@
 void BKE_ptcache_id_from_cloth(PTCacheID *pid, struct Object *ob, struct ClothModifierData *clmd);
 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd);
 void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, struct Object *ob, struct DynamicPaintSurface *surface);
+void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct RigidBodyWorld *rbw);
 
 void BKE_ptcache_ids_from_object(struct ListBase *lb, struct Object *ob, struct Scene *scene, int duplis);
 

Modified: trunk/blender/source/blender/blenkernel/intern/pointcache.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/pointcache.c	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/source/blender/blenkernel/intern/pointcache.c	2013-01-23 05:56:34 UTC (rev 54022)
@@ -43,6 +43,7 @@
 #include "DNA_object_types.h"
 #include "DNA_object_force.h"
 #include "DNA_particle_types.h"
+#include "DNA_rigidbody_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_smoke_types.h"
 
@@ -72,6 +73,8 @@
 
 #include "BIK_api.h"
 
+#include "RBI_api.h"
+
 /* both in intern */
 #ifdef WITH_SMOKE
 #include "smoke_API.h"
@@ -867,6 +870,97 @@
 	return 1;
 }
 
+/* Rigid Body functions */
+static int  ptcache_rigidbody_write(int index, void *rb_v, void **data, int UNUSED(cfra))
+{
+	RigidBodyWorld *rbw = rb_v;
+	Object *ob = NULL;
+	
+	if (rbw->objects)
+		ob = rbw->objects[index];
+	
+	if (ob && ob->rigidbody_object) {
+		RigidBodyOb *rbo = ob->rigidbody_object;
+		
+		if (rbo->type == RBO_TYPE_ACTIVE) {
+			
+			RB_body_get_position(rbo->physics_object, rbo->pos);
+			RB_body_get_orientation(rbo->physics_object, rbo->orn);
+			
+			PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, rbo->pos);
+			PTCACHE_DATA_FROM(data, BPHYS_DATA_ROTATION, rbo->orn);
+		}
+	}
+
+	return 1;
+}
+static void ptcache_rigidbody_read(int index, void *rb_v, void **data, float UNUSED(cfra), float *old_data)
+{
+	RigidBodyWorld *rbw = rb_v;
+	Object *ob = NULL;
+	
+	if (rbw->objects)
+		ob = rbw->objects[index];
+	
+	if (ob && ob->rigidbody_object) {
+		RigidBodyOb *rbo = ob->rigidbody_object;
+		
+		if (rbo->type == RBO_TYPE_ACTIVE) {
+			
+			if (old_data) {
+				memcpy(rbo->pos, data, 3 * sizeof(float));
+				memcpy(rbo->orn, data + 3, 4 * sizeof(float));
+			}
+			else {
+				PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, rbo->pos);
+				PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, 0, rbo->orn);
+			}
+		}
+	}
+}
+static void ptcache_rigidbody_interpolate(int index, void *rb_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
+{
+	RigidBodyWorld *rbw = rb_v;
+	Object *ob = NULL;
+	ParticleKey keys[4];
+	float dfra;
+	
+	if (rbw->objects)
+		ob = rbw->objects[index];
+	
+	if (ob && ob->rigidbody_object) {
+		RigidBodyOb *rbo = ob->rigidbody_object;
+		
+		if (rbo->type == RBO_TYPE_ACTIVE) {
+			
+			copy_v3_v3(keys[1].co, rbo->pos);
+			copy_v3_v3(keys[1].rot, rbo->orn);
+			
+			if (old_data) {
+				memcpy(keys[2].co, data, 3 * sizeof(float));
+				memcpy(keys[2].rot, data + 3, 4 * sizeof(float));
+			}
+			else {
+				BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
+			}
+			
+			dfra = cfra2 - cfra1;
+		
+			psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
+			interp_qt_qtqt(keys->rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
+			
+			copy_v3_v3(rbo->pos, keys->co);
+			copy_v3_v3(rbo->orn, keys->rot);
+		}
+	}
+}
+static int ptcache_rigidbody_totpoint(void *rb_v, int UNUSED(cfra))
+{
+	RigidBodyWorld *rbw = rb_v;
+	
+	return rbw->numbodies;
+}
+
 /* Creating ID's */
 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
 {
@@ -1072,6 +1166,42 @@
 	pid->max_step = 1;
 }
 
+void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, Object *ob, RigidBodyWorld *rbw)
+{
+	
+	memset(pid, 0, sizeof(PTCacheID));
+	
+	pid->ob= ob;
+	pid->calldata= rbw;
+	pid->type= PTCACHE_TYPE_RIGIDBODY;
+	pid->cache= rbw->pointcache;
+	pid->cache_ptr= &rbw->pointcache;
+	pid->ptcaches= &rbw->ptcaches;
+	pid->totpoint= pid->totwrite= ptcache_rigidbody_totpoint;
+	
+	pid->write_point			= ptcache_rigidbody_write;
+	pid->read_point				= ptcache_rigidbody_read;
+	pid->interpolate_point		= ptcache_rigidbody_interpolate;
+	
+	pid->write_stream			= NULL;
+	pid->read_stream			= NULL;
+	
+	pid->write_extra_data		= NULL;
+	pid->read_extra_data		= NULL;
+	pid->interpolate_extra_data	= NULL;
+	
+	pid->write_header			= ptcache_basic_header_write;
+	pid->read_header			= ptcache_basic_header_read;
+	
+	pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_ROTATION);
+	pid->info_types= 0;
+	
+	pid->stack_index = pid->cache->index;
+	
+	pid->default_step = 1;
+	pid->max_step = 1;
+}
+
 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
 {
 	PTCacheID *pid;
@@ -1133,6 +1263,12 @@
 			}
 		}
 	}
+	
+	if (scene && ob->rigidbody_object && scene->rigidbody_world) {
+		pid = MEM_callocN(sizeof(PTCacheID), "PTCacheID");
+		BKE_ptcache_id_from_rigidbody(pid, ob, scene->rigidbody_world);
+		BLI_addtail(lb, pid);
+	}
 
 	if (scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
 		ListBase *lb_dupli_ob;
@@ -2641,6 +2777,14 @@
 		}
 	}
 
+	if (scene->rigidbody_world && ob->rigidbody_object) {
+		if (ob->rigidbody_object)
+			ob->rigidbody_object->flag |= RBO_FLAG_NEEDS_RESHAPE;
+		BKE_ptcache_id_from_rigidbody(&pid, ob, scene->rigidbody_world);
+		/* only flag as outdated, resetting should happen on start frame */
+		pid.cache->flag |= PTCACHE_OUTDATED;
+	}
+
 	if (ob->type == OB_ARMATURE)
 		BIK_clear_cache(ob->pose);
 

Modified: trunk/blender/source/blender/blenkernel/intern/rigidbody.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/rigidbody.c	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/source/blender/blenkernel/intern/rigidbody.c	2013-01-23 05:56:34 UTC (rev 54022)
@@ -97,6 +97,10 @@
 	if (rbw->objects)
 		free(rbw->objects);
 
+	/* free cache */
+	BKE_ptcache_free_list(&(rbw->ptcaches));
+	rbw->pointcache = NULL;
+
 	/* free effector weights */
 	if (rbw->effector_weights)
 		MEM_freeN(rbw->effector_weights);
@@ -472,6 +476,9 @@
 	rbw->steps_per_second = 60; /* Bullet default (60 Hz) */
 	rbw->num_solver_iterations = 10; /* 10 is bullet default */
 
+	rbw->pointcache = BKE_ptcache_add(&(rbw->ptcaches));
+	rbw->pointcache->step = 1;
+
 	/* return this sim world */
 	return rbw;
 }
@@ -749,7 +756,8 @@
 
 void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw)
 {
-// RB_TODO implement this
+	if (rbw)
+		rbw->pointcache->flag |= PTCACHE_OUTDATED;
 }
 
 /* ------------------ */

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c	2013-01-23 05:56:34 UTC (rev 54022)
@@ -5303,6 +5303,12 @@
 		rbw->effector_weights = newdataadr(fd, rbw->effector_weights);
 		if (!rbw->effector_weights)
 			rbw->effector_weights = BKE_add_effector_weights(NULL);
+
+		/* link cache */
+		direct_link_pointcache_list(fd, &rbw->ptcaches, &rbw->pointcache, FALSE);
+		/* make sure simulation starts from the beginning after loading file */
+		if (rbw->pointcache)
+			rbw->ltime = rbw->pointcache->startframe;
 	}
 }
 

Modified: trunk/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/writefile.c	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/source/blender/blenloader/intern/writefile.c	2013-01-23 05:56:34 UTC (rev 54022)
@@ -2307,6 +2307,7 @@
 		if (sce->rigidbody_world) {
 			writestruct(wd, DATA, "RigidBodyWorld", 1, sce->rigidbody_world);
 			writestruct(wd, DATA, "EffectorWeights", 1, sce->rigidbody_world->effector_weights);
+			write_pointcaches(wd, &(sce->rigidbody_world->ptcaches));
 		}
 		
 		sce= sce->id.next;

Modified: trunk/blender/source/blender/editors/object/object_edit.c
===================================================================
--- trunk/blender/source/blender/editors/object/object_edit.c	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/source/blender/editors/object/object_edit.c	2013-01-23 05:56:34 UTC (rev 54022)
@@ -376,7 +376,7 @@
 		scene->obedit = NULL; // XXX for context
 
 		/* flag object caches as outdated */
-		BKE_ptcache_ids_from_object(&pidlist, obedit, NULL, 0);
+		BKE_ptcache_ids_from_object(&pidlist, obedit, scene, 0);
 		for (pid = pidlist.first; pid; pid = pid->next) {
 			if (pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
 				pid->cache->flag |= PTCACHE_OUTDATED;

Modified: trunk/blender/source/blender/editors/space_time/space_time.c
===================================================================
--- trunk/blender/source/blender/editors/space_time/space_time.c	2013-01-23 05:56:27 UTC (rev 54021)
+++ trunk/blender/source/blender/editors/space_time/space_time.c	2013-01-23 05:56:34 UTC (rev 54022)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list