[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [12358] branches/particles/source/blender: PointCache functions for physics solvers to store their data in, includes a pointCache modifier that can read and write animation point cache to the

Campbell Barton cbarton at metavr.com
Tue Oct 23 16:48:32 CEST 2007


Revision: 12358
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=12358
Author:   campbellbarton
Date:     2007-10-23 16:48:32 +0200 (Tue, 23 Oct 2007)

Log Message:
-----------
PointCache functions for physics solvers to store their data in, includes a pointCache modifier that can read and write animation point cache to the 
disk.

main funcs are PTCache_id_filename(...), PTCache_id_fopen(...), PTCache_id_clear(...)

Modified Paths:
--------------
    branches/particles/source/blender/blenkernel/BKE_modifier.h
    branches/particles/source/blender/blenkernel/intern/modifier.c
    branches/particles/source/blender/makesdna/DNA_modifier_types.h
    branches/particles/source/blender/src/buttons_editing.c

Added Paths:
-----------
    branches/particles/source/blender/blenkernel/BKE_pointcache.h
    branches/particles/source/blender/blenkernel/intern/pointcache.c

Modified: branches/particles/source/blender/blenkernel/BKE_modifier.h
===================================================================
--- branches/particles/source/blender/blenkernel/BKE_modifier.h	2007-10-23 13:59:22 UTC (rev 12357)
+++ branches/particles/source/blender/blenkernel/BKE_modifier.h	2007-10-23 14:48:32 UTC (rev 12358)
@@ -288,6 +288,8 @@
 int           modifiers_usesArmature(struct Object *ob, struct bArmature *arm);
 int           modifiers_isDeformed(struct Object *ob);
 
+int           modifiers_indexInObject(struct Object *ob, struct ModifierData *md);
+
 /* Calculates and returns a linked list of CustomDataMasks indicating the
  * data required by each modifier in the stack pointed to by md for correct
  * evaluation, assuming the data indicated by dataMask is required at the

Added: branches/particles/source/blender/blenkernel/BKE_pointcache.h
===================================================================
--- branches/particles/source/blender/blenkernel/BKE_pointcache.h	                        (rev 0)
+++ branches/particles/source/blender/blenkernel/BKE_pointcache.h	2007-10-23 14:48:32 UTC (rev 12358)
@@ -0,0 +1,41 @@
+/*
+*
+* ***** 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.
+*
+* The Original Code is Copyright (C) 2006 Blender Foundation.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): Campbell Barton <ideasman42 at gmail.com>
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#ifndef BKE_POINTCACHE_H
+#define BKE_POINTCACHE_H
+
+#include "DNA_ID.h"
+
+#define PTCache_EXT ".bphys"
+#define PTCache_PATH "//pointcache/"
+
+int		PTCache_id_filename(struct ID *id, char *filename, int cfra, int stack_index, short do_path, short do_ext);
+FILE *	PTCache_id_fopen(struct ID *id, char mode, int cfra, int stack_index);
+void	PTCache_id_clear(struct ID *id, int cfra, int stack_index);
+
+#endif

Modified: branches/particles/source/blender/blenkernel/intern/modifier.c
===================================================================
--- branches/particles/source/blender/blenkernel/intern/modifier.c	2007-10-23 13:59:22 UTC (rev 12357)
+++ branches/particles/source/blender/blenkernel/intern/modifier.c	2007-10-23 14:48:32 UTC (rev 12358)
@@ -101,6 +101,8 @@
 
 #include "RE_shader_ext.h"
 
+#include "BKE_pointcache.h"
+
 /***/
 
 static int noneModifier_isDisabled(ModifierData *md)
@@ -5324,6 +5326,8 @@
 {
 	return particleInstanceModifier_applyModifier(md, ob, derivedData, 0, 1);
 }
+
+
 /* Explode */
 static void explodeModifier_initData(ModifierData *md)
 {
@@ -6087,6 +6091,71 @@
 
 	return explode;
 }
+
+/* PointCache - example DONT USE SERIOUSLY */
+static void pointCacheModifier_initData(ModifierData *md)
+{
+	PointCacheModifierData *emd= (PointCacheModifierData*) md;
+
+	emd->mode= ePointCache_Read; /* read */
+}
+static void pointCacheModifier_freeData(ModifierData *md)
+{
+	PointCacheModifierData *pcm = (PointCacheModifierData*) md;
+}
+static void pointCacheModifier_copyData(ModifierData *md, ModifierData *target)
+{
+	PointCacheModifierData *pcm= (PointCacheModifierData*) md;
+	PointCacheModifierData *tpcm= (PointCacheModifierData*) target;
+
+	tpcm->mode = pcm->mode;
+}
+static int pointCacheModifier_dependsOnTime(ModifierData *md) 
+{
+	return 1;
+}
+CustomDataMask pointCacheModifier_requiredDataMask(ModifierData *md)
+{
+	PointCacheModifierData *emd= (PointCacheModifierData*) md;
+	CustomDataMask dataMask = 0;
+	return dataMask;
+}
+
+static DerivedMesh * pointCacheModifier_applyModifier(
+                 ModifierData *md, Object *ob, DerivedMesh *dm,
+                 int useRenderParams, int isFinalCalc)
+{
+	PointCacheModifierData *pcm = (PointCacheModifierData*) md;
+
+	FILE *fp = NULL;
+	int i;
+	int stack_index = modifiers_indexInObject(ob, md);
+	int totvert = dm->getNumVerts(dm);
+	MVert *mvert, *mv;
+	mvert = mv = dm->getVertArray(dm);
+	
+	if (pcm->mode == ePointCache_Read) {
+		fp = PTCache_id_fopen((ID *)ob, 'w', G.scene->r.cfra, stack_index);
+		if (!fp) return dm;
+		for (mv=mvert, i=0; i<totvert; mv++, i++) {
+			fwrite(&mv->co, sizeof(float), 3, fp);
+		}
+		fclose(fp);
+	} else if (pcm->mode == ePointCache_Write) {
+		float pt[3];
+		fp = PTCache_id_fopen((ID *)ob, 'r', G.scene->r.cfra, stack_index);
+		if (!fp) return dm;
+		for (mv=mvert, i=0; i<totvert; mv++, i++) {
+			if ((fread(&mv->co, sizeof(float), 3, fp)) != 3) {
+				break;
+			}
+		}
+		fclose(fp);
+	}
+	CDDM_calc_normals(dm);
+	return dm;
+}
+
 static ParticleSystemModifierData * explodeModifier_findPrecedingParticlesystem(Object *ob, ModifierData *emd)
 {
 	ModifierData *md;
@@ -6406,6 +6475,17 @@
 		mti->requiredDataMask = explodeModifier_requiredDataMask;
 		mti->applyModifier = explodeModifier_applyModifier;
 
+		mti = INIT_TYPE(PointCache);
+		mti->type = eModifierTypeType_Nonconstructive;
+		mti->flags = eModifierTypeFlag_AcceptsMesh
+					| eModifierTypeFlag_SupportsMapping;
+		mti->initData = pointCacheModifier_initData;
+		mti->freeData = pointCacheModifier_freeData;
+		mti->copyData = pointCacheModifier_copyData;
+		mti->dependsOnTime = pointCacheModifier_dependsOnTime;
+		mti->requiredDataMask = pointCacheModifier_requiredDataMask;
+		mti->applyModifier = pointCacheModifier_applyModifier;
+		
 		typeArrInit = 0;
 #undef INIT_TYPE
 	}
@@ -6780,3 +6860,14 @@
 	}
 	return 0;
 }
+
+int modifiers_indexInObject(Object *ob, ModifierData *md_seek)
+{
+	int i= 0;
+	ModifierData *md;
+	
+	for (md=ob->modifiers.first; (md && md_seek!=md); md=md->next, i++);
+	if (!md) return -1; /* modifier isnt in the object */
+	return i;
+}
+

Added: branches/particles/source/blender/blenkernel/intern/pointcache.c
===================================================================
--- branches/particles/source/blender/blenkernel/intern/pointcache.c	                        (rev 0)
+++ branches/particles/source/blender/blenkernel/intern/pointcache.c	2007-10-23 14:48:32 UTC (rev 12358)
@@ -0,0 +1,161 @@
+/**
+ *
+ * ***** BEGIN GPL/BL DUAL 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. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * 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.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+* Contributor(s): Campbell Barton <ideasman42 at gmail.com>
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+
+#include "BKE_pointcache.h"
+
+#include "BKE_utildefines.h"
+#include "BKE_global.h"
+#include "BKE_library.h"
+
+#include "BLI_blenlib.h"
+#include "BKE_utildefines.h"
+#include "blendef.h"
+
+/* needed for directory lookup */
+#ifndef WIN32
+  #include <dirent.h>
+#else
+  #include "BLI_winstuff.h"
+#endif
+
+/*	Takes an Object ID and returns a unique name
+	- id: object id
+	- cfra: frame for the cache, can be negative
+	- stack_index: index in the modifier stack. we can have cache for more then one stack_index
+*/
+
+int PTCache_path(char *filename)
+{
+	sprintf(filename, PTCache_PATH);
+	BLI_convertstringcode(filename, G.sce, 0);
+	return strlen(filename);
+}
+
+int PTCache_id_filename(struct ID *id, char *filename, int cfra, int stack_index, short do_path, short do_ext)
+{
+	int len=0;
+	char *idname;
+	char *newname;
+	filename[0] = '\0';
+	newname = filename;
+	
+	/* start with temp dir */
+	if (do_path) {
+		len = PTCache_path(filename);
+		newname += len;
+	}
+	idname = (id->name+2);
+	/* convert chars to hex so they are always a valid file */
+	while('\0' != *idname) {
+		sprintf(newname, "%02X", (char)(*idname++));
+		newname+=2;
+		len += 2;
+	}
+	
+	if (do_ext) {
+		sprintf(newname, "_%06d_%02d"PTCache_EXT, cfra, stack_index); /* always 6 chars */
+		len += 16;
+	}
+	
+	return len; /* make sure the above string is always 16 chars */
+}
+
+/* youll need to close yourself after! */
+FILE *PTCache_id_fopen(struct ID *id, char mode, int cfra, int stack_index)
+{
+	/* mode is same as fopen's modes */
+	FILE *fp;
+	char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
+
+	PTCache_id_filename(id, filename, cfra, stack_index, 1, 1);
+
+	if (mode=='r') {
+		if (!BLI_exists(filename)) {
+			printf("Error, file does not exist '%s'\n", filename);
+			return NULL;
+		}
+ 		fp = fopen(filename, "r");
+	} else if (mode=='w') {
+		BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
+		fp = fopen(filename, "wb");
+	}
+
+ 	if (!fp) {
+ 		printf("Error creating file filename '%s'\n", filename);
+ 		return NULL;
+ 	}
+ 	
+ 	return fp;
+}
+
+/* youll need to close yourself after! */
+void PTCache_id_clear(struct ID *id, int cfra, int stack_index)
+{
+	int len; /* store the length of the string */
+
+	/* mode is same as fopen's modes */
+	DIR *dir; 
+	struct dirent *de;
+	struct stat status;
+	char *file_extension;
+	char path[FILE_MAX];
+	char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
+	char path_full[(FILE_MAXDIR+FILE_MAXFILE)*2];
+
+	PTCache_path(path);
+	len = PTCache_id_filename(id, filename, cfra, stack_index, 0, 0); /* no path */
+	

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list