Merged pointcache from particles branch, but is not used in the moment

This commit is contained in:
Daniel Genrich
2007-11-06 09:44:09 +00:00
parent 47c62453db
commit cbc941e342
5 changed files with 345 additions and 0 deletions

View File

@@ -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@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

View File

@@ -5382,6 +5382,83 @@ static void meshdeformModifier_deformVertsEM(
dm->release(dm);
}
/* PointCache - example DONT USE SERIOUSLY */
static void pointCacheModifier_initData(ModifierData *md)
{
PointCacheModifierData *pcm= (PointCacheModifierData*) md;
pcm->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 *pcm= (PointCacheModifierData*) md;
CustomDataMask dataMask = 0;
return dataMask;
}
static void pointCacheModifier_deformVerts(
ModifierData *md, Object *ob, DerivedMesh *derivedData,
float (*vertexCos)[3], int numVerts)
{
PointCacheModifierData *pcm = (PointCacheModifierData*) md;
FILE *fp = NULL;
int i;
int stack_index = modifiers_indexInObject(ob, md);
int totvert;
MVert *mvert, *mv;
DerivedMesh *dm;
if(derivedData) dm = CDDM_copy(derivedData);
else if(ob->type==OB_MESH) dm = CDDM_from_mesh(ob->data, ob);
else return;
CDDM_apply_vert_coords(dm, vertexCos);
CDDM_calc_normals(dm);
mvert = mv = dm->getVertArray(dm);
totvert = dm->getNumVerts(dm);
if (pcm->mode == ePointCache_Read) {
fp = PTCache_id_fopen((ID *)ob, 'w', G.scene->r.cfra, stack_index);
if (!fp) return;
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;
for (mv=mvert, i=0; i<totvert; mv++, i++) {
float *co = vertexCos[i];
if ((fread(co, sizeof(float), 3, fp)) != 3) {
break;
}
}
fclose(fp);
}
if(!derivedData) dm->release(dm);
}
/***/
static ModifierTypeInfo typeArr[NUM_MODIFIER_TYPES];
@@ -5649,6 +5726,16 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
mti->updateDepgraph = meshdeformModifier_updateDepgraph;
mti->deformVerts = meshdeformModifier_deformVerts;
mti->deformVertsEM = meshdeformModifier_deformVertsEM;
mti = INIT_TYPE(PointCache);
mti->type = eModifierTypeType_OnlyDeform;
mti->flags = eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_RequiresOriginalData;
mti->initData = pointCacheModifier_initData;
mti->freeData = pointCacheModifier_freeData;
mti->copyData = pointCacheModifier_copyData;
mti->dependsOnTime = pointCacheModifier_dependsOnTime;
mti->requiredDataMask = pointCacheModifier_requiredDataMask;
mti->deformVerts = pointCacheModifier_deformVerts;
typeArrInit = 0;
#undef INIT_TYPE
@@ -6032,3 +6119,27 @@ int modifiers_isDeformed(Object *ob)
return 0;
}
/* checks we only have deform modifiers */
int modifiers_isDeformedOnly(Object *ob)
{
ModifierData *md = modifiers_getVirtualModifierList(ob);
ModifierTypeInfo *mti;
for (; md; md=md->next) {
mti = modifierType_getInfo(md->type);
/* TODO - check the modifier is being used! */
if (mti->type != eModifierTypeType_OnlyDeform) {
return 0;
}
}
return 1;
}
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;
}

View File

@@ -0,0 +1,159 @@
/**
*
* ***** 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@gmail.com>
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.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, "rb");
} 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;
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 */
/* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
dir = opendir(path);
if (dir==NULL)
return;
while ((de = readdir(dir)) != NULL) {
//if (S_ISREG(status.st_mode)) { /* is file */
if (strstr(de->d_name, PTCache_EXT)) { /* do we have the right extension?*/
if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
BLI_join_dirfile(path_full, path, de->d_name);
BLI_delete(path_full, 0, 0);
}
}
//}
}
closedir(dir);
return;
}

View File

@@ -31,6 +31,7 @@ typedef enum ModifierType {
eModifierType_Smooth,
eModifierType_Cast,
eModifierType_MeshDeform,
eModifierType_PointCache,
eModifierType_Cloth,
eModifierType_Collision,
NUM_MODIFIER_TYPES
@@ -386,4 +387,14 @@ typedef struct MeshDeformModifierData {
int totvert, totcagevert;
} MeshDeformModifierData;
typedef struct PointCacheModifierData {
ModifierData modifier;
short mode, pad1, pad2, pad3;
} PointCacheModifierData;
typedef enum {
ePointCache_Read = (1<<0),
ePointCache_Write = (1<<1),
} PointCacheFlag;
#endif

View File

@@ -88,6 +88,7 @@
#include "BKE_main.h"
#include "BKE_modifier.h"
#include "BKE_packedFile.h"
#include "BKE_pointcache.h"
#include "BKE_scene.h"
#include "BLI_blenlib.h"
@@ -1398,6 +1399,14 @@ static void modifiers_convertToReal(void *ob_v, void *md_v)
BIF_undo_push("Modifier convert to real");
}
static void modifiers_pointCacheClearModifier(void *ob_v, void *md_v)
{
Object *ob = ob_v;
ModifierData *md = md_v;
int stack_index = modifiers_indexInObject(ob_v, md_v);
PTCache_id_clear((ID *)ob, CFRA, stack_index);
}
static void build_uvlayer_menu_vars(CustomData *data, char **menu_string,
int *uvlayer_tmp, char *uvlayer_name)
{
@@ -1646,6 +1655,8 @@ static void draw_modifier(uiBlock *block, Object *ob, ModifierData *md, int *xco
height = 211;
} else if (md->type==eModifierType_MeshDeform) {
height = 73;
} else if (md->type==eModifierType_PointCache) {
height = 48;
}
/* roundbox 4 free variables: corner-rounding, nop, roundbox type, shade */
@@ -2144,6 +2155,18 @@ static void draw_modifier(uiBlock *block, Object *ob, ModifierData *md, int *xco
uiDefButS(block, NUM, B_NOP, "Precision:", lx+(buttonWidth+1)/2,(cy-=24), buttonWidth/2,19, &mmd->gridsize, 2, 10, 0.5, 0, "The grid size for binding");
}
uiBlockEndAlign(block);
} else if (md->type==eModifierType_PointCache) {
PointCacheModifierData *pcm = (PointCacheModifierData *) md;
uiBut *but;
cy -= 20;
uiBlockEndAlign(block);
uiDefButS(block, ROW,B_MODIFIER_RECALC,"Write Cache", lx, cy, 75, 19, &pcm->mode, 12.0, ePointCache_Read, 0, 0, "");
uiDefButS(block, ROW,B_MODIFIER_RECALC,"Read Cache", lx+75, cy, 75,19, &pcm->mode, 12.0, ePointCache_Write, 0, 0, "");
cy -= 20;
but = uiDefBut(block, BUT, B_NOP, "Clear Cache", lx, cy, 150,19, 0, 0, 0, 0, 0, "");
uiButSetFunc(but, modifiers_pointCacheClearModifier, ob, md);
}
uiBlockEndAlign(block);