diff options
Diffstat (limited to 'source/blender/blenkernel/intern')
-rw-r--r-- | source/blender/blenkernel/intern/blender.c | 1 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/depsgraph.c | 18 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/exotic.c | 593 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/multires.c | 6 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/particle.c | 48 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/particle_system.c | 187 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/pointcache.c | 753 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/scene.c | 2 |
8 files changed, 877 insertions, 731 deletions
diff --git a/source/blender/blenkernel/intern/blender.c b/source/blender/blenkernel/intern/blender.c index 5fc7d18689d..d3d21018c1c 100644 --- a/source/blender/blenkernel/intern/blender.c +++ b/source/blender/blenkernel/intern/blender.c @@ -221,7 +221,6 @@ static void clear_global(void) { // extern short winqueue_break; /* screen.c */ -// XXX freeAllRad(); fastshade_free_render(); /* lamps hang otherwise */ free_main(G.main); /* free all lib data */ diff --git a/source/blender/blenkernel/intern/depsgraph.c b/source/blender/blenkernel/intern/depsgraph.c index 8bb34bde122..b57b8b7a6da 100644 --- a/source/blender/blenkernel/intern/depsgraph.c +++ b/source/blender/blenkernel/intern/depsgraph.c @@ -1831,7 +1831,7 @@ static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime) } /* node was checked to have lasttime != curtime , and is of type ID_OB */ -static void flush_pointcache_reset(DagNode *node, int curtime, int reset) +static void flush_pointcache_reset(Scene *scene, DagNode *node, int curtime, int reset) { DagAdjList *itA; Object *ob; @@ -1844,13 +1844,13 @@ static void flush_pointcache_reset(DagNode *node, int curtime, int reset) ob= (Object*)(node->ob); if(reset || (ob->recalc & OB_RECALC)) { - if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH)) + if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH)) ob->recalc |= OB_RECALC_DATA; - flush_pointcache_reset(itA->node, curtime, 1); + flush_pointcache_reset(scene, itA->node, curtime, 1); } else - flush_pointcache_reset(itA->node, curtime, 0); + flush_pointcache_reset(scene, itA->node, curtime, 0); } } } @@ -1908,13 +1908,13 @@ void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time) ob= (Object*)(itA->node->ob); if(ob->recalc & OB_RECALC) { - if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH)) + if(BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH)) ob->recalc |= OB_RECALC_DATA; - flush_pointcache_reset(itA->node, lasttime, 1); + flush_pointcache_reset(sce, itA->node, lasttime, 1); } else - flush_pointcache_reset(itA->node, lasttime, 0); + flush_pointcache_reset(sce, itA->node, lasttime, 0); } } } @@ -2132,7 +2132,7 @@ void DAG_object_flush_update(Scene *sce, Object *ob, short flag) if(ob==NULL || sce->theDag==NULL) return; ob->recalc |= flag; - BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH); + BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH); /* all users of this ob->data should be checked */ /* BUT! displists for curves are still only on cu */ @@ -2147,7 +2147,7 @@ void DAG_object_flush_update(Scene *sce, Object *ob, short flag) for (obt=G.main->object.first; obt; obt= obt->id.next) { if (obt != ob && obt->data==ob->data) { obt->recalc |= OB_RECALC_DATA; - BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH); + BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH); } } } diff --git a/source/blender/blenkernel/intern/exotic.c b/source/blender/blenkernel/intern/exotic.c index 929d3f942dc..f15e1c24949 100644 --- a/source/blender/blenkernel/intern/exotic.c +++ b/source/blender/blenkernel/intern/exotic.c @@ -27,47 +27,7 @@ * * - Blender Foundation * - * ***** END GPL LICENSE BLOCK ***** - * - * eigen videoscape formaat: - * - * - * lamp: - * 3DG2 - aantal_lampen - - type - spsi spbl - r, g, b, energy - locx, locy, locz - vecx, vecy, vecz - - - curve / nurbs: - 3DG3 - 5 of 11 (curve of surf) - aantal_nurbs - extr1 extr2 - - mat[0][0] mat[0][1] mat[0][2] mat[0][3] - mat[1][0] mat[1][1] mat[1][2] mat[1][3] - ... - - type - pntsu, pntsv - resolu, resolv - orderu, orderv - flagu, flagv - - (als type==nurb) x y z w - x y z w - ... - (als type==bez) xyz xyz xyz h1 h2 h3 - xyz xyz xyz h1 h2 h3 - ... - * - * - */ + * ***** END GPL LICENSE BLOCK *****/ #include <ctype.h> /* isdigit, isspace */ @@ -482,385 +442,6 @@ static void read_stl_mesh_ascii(Scene *scene, char *str) #undef STLREADLINE #undef STLREADVERT -static void read_videoscape_mesh(Scene *scene, char *str) -{ - Object *ob; - Mesh *me; - MVert *mvert; - MFace *mface; - Material *ma; - FILE *fp; - float *vertdata, *vd, min[3], max[3], cent[3], ftemp; - unsigned int color[32], col; - int totcol, a, b, verts, tottria=0, totquad=0, totedge=0, poly, nr0, nr, first; - int end; - char s[50]; - - fp= fopen(str, "rb"); - if(fp==NULL) { - //XXX error("Can't read file"); - return; - } - - fscanf(fp, "%40s", s); - - fscanf(fp, "%d\n", &verts); - if(verts<=0) { - fclose(fp); - //XXX error("Read error"); - return; - } - - if(verts>MESH_MAX_VERTS) { - //XXX error("too many vertices"); - fclose(fp); - return; - } - - INIT_MINMAX(min, max); - vd= vertdata= MEM_mallocN(sizeof(float)*3*verts, "videoscapelezer"); - - for(a=0; a<verts; a++) { - fscanf(fp, "%f %f %f", vd, vd+1, vd+2); - DO_MINMAX(vd, min, max); - vd+=3; - } - - /* count faces and colors */ - for(a=0; a<32; a++) color[a]= 0; - totcol= 0; - end= 1; - while(end>0) { - end= fscanf(fp,"%d", &poly); - if(end<=0) break; - - if(poly==3) tottria++; - else if(poly==4) totquad++; - else totedge+= poly; - - for(a=0;a<poly;a++) { - end= fscanf(fp,"%d", &nr); - if(end<=0) break; - } - if(end<=0) break; - - end= fscanf(fp,"%i\n", &col); - col &= 0xF0F0F0; - for(a=0; a<totcol; a++) { - if(color[a]==col) break; - } - if(a>=totcol && totcol<32) { - color[totcol]= col; - totcol++; - } - } - - /* new object */ - ob= add_object(scene, OB_MESH); - me= ob->data; - me->totvert= verts; - me->totface= totedge+tottria+totquad; - - me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, - NULL, me->totvert); - me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, - NULL, me->totface); - - /* colors */ - if(totcol) { - ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat"); - me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat"); - me->totcol= totcol; - ob->totcol= (unsigned char) me->totcol; - ob->actcol= 1; - } - - /* materials */ - for(a=0; a<totcol; a++) { - ma= G.main->mat.first; - while(ma) { - if(ma->mtex[0]==0) { - col= rgb_to_cpack(ma->r, ma->g, ma->b); - if(color[a]==col) { - me->mat[a]= ma; - ma->id.us++; - break; - } - } - ma= ma->id.next; - } - if(ma==0) { - ma= add_material("ext"); - me->mat[a]= ma; - cpack_to_rgb(color[a], cent, cent+1, cent+2); - ma->r= cent[0]; - ma->g= cent[1]; - ma->b= cent[2]; - automatname(ma); - } - } - - /* verts */ - - cent[0]= (min[0]+max[0])/2.0f; - cent[1]= (min[1]+max[1])/2.0f; - cent[2]= (min[2]+max[2])/2.0f; - VECCOPY(ob->loc, cent); - - a= me->totvert; - vd= vertdata; - mvert= me->mvert; - while(a--) { - VecSubf(mvert->co, vd, cent); - mvert++; - vd+= 3; - } - - /* faces */ - if(me->totface) { - rewind(fp); - - fscanf(fp, "%40s", s); - fscanf(fp, "%d\n", &verts); - /* fake read */ - for(a=0;a<verts;a++) { - fscanf(fp, "%f %f %f", &ftemp, &ftemp, &ftemp); - } - - a= me->totface; - mface= me->mface; - while(a--) { - end= fscanf(fp,"%d", &poly); - if(end<=0) break; - - if(poly==3 || poly==4) { - fscanf(fp,"%d", &nr); - mface->v1= MIN2(nr, me->totvert-1); - fscanf(fp,"%d", &nr); - mface->v2= MIN2(nr, me->totvert-1); - fscanf(fp,"%d", &nr); - mface->v3= MIN2(nr, me->totvert-1); - if(poly==4) { - if( fscanf(fp,"%d", &nr) <=0 ) break; - mface->v4= MIN2(nr, me->totvert-1); - } - - test_index_face(mface, NULL, 0, poly); - - mface++; - } - else { - if( fscanf(fp,"%d", &nr0) <=0) break; - first= nr0; - for(b=1; b<poly; b++) { - end= fscanf(fp,"%d", &nr); - if(end<=0) break; - nr= MIN2(nr, me->totvert-1); - mface->v1= nr; - mface->v2= nr0; - nr0= nr; - mface++; - a--; - } - mface->v1= first; - mface->v2= nr; - mface++; - if(end<=0) break; - } - end= fscanf(fp,"%i", &col); - col &= 0xF0F0F0; - if(end<=0) break; - - for(b=0; b<totcol; b++) { - if(color[b]==col) { - (mface-1)->mat_nr= b; - break; - } - } - } - } - - fclose(fp); - MEM_freeN(vertdata); - - mesh_add_normals_flags(me); - make_edges(me, 0); - - //XXX waitcursor(1); -} - -static void read_videoscape_lamp(Scene *scene, char *str) -{ - Object *ob; - Lamp *la; - FILE *fp; - float vec[3], q1[4]; - int tot, val; - char s[50]; - - fp= fopen(str, "rb"); - if(fp==NULL) { - //XXX error("Can't read file"); - return; - } - - fscanf(fp, "%40s", s); - fscanf(fp, "%d\n", &tot); - - while(tot--) { - ob= add_object(scene, OB_LAMP); - la= ob->data; - - fscanf(fp, "%d\n", &val); - la->type= val; - if(la->type==1) la->type= LA_SPOT; - else if(la->type==2) la->type= LA_SUN; - - fscanf(fp, "%f %f\n", &la->spotsize, &la->spotblend); - - fscanf(fp, "%f %f %f %f\n", &la->r, &la->g, &la->b, &la->energy); - - fscanf(fp, "%f %f %f\n", ob->loc, ob->loc+1, ob->loc+2); - val= fscanf(fp, "%f %f %f\n", vec, vec+1, vec+2); - vectoquat(vec, 5, 2, q1); - QuatToEul(q1, ob->rot); - - if(val<=0) break; - - } - fclose(fp); -} - -static void read_videoscape_nurbs(Scene *scene, char *str) -{ - Object *ob; - Curve *cu; - Nurb *nu; - BezTriple *bezt; - BPoint *bp; - FILE *fp; - float tmat[4][4], omat[3][3], imat[3][3], mat[3][3]; - int a, tot, type, val; - char s[50]; - - fp= fopen(str, "rb"); - if(fp==NULL) { - //XXX error("Can't read file"); - return; - } - - fscanf(fp, "%40s", s); - fscanf(fp, "%d\n", &type); - - if(type==5) ob= add_object(scene, OB_SURF); - else ob= add_object(scene, OB_CURVE); - cu= ob->data; - - fscanf(fp, "%d\n", &tot); - fscanf(fp, "%d %d\n", &type, &val); - - cu->ext1= 0.002f*type; - cu->ext2= 0.002f*val; - - for(a=0; a<4; a++) fscanf(fp, "%e %e %e %e\n", tmat[a], tmat[a]+1, tmat[a]+2, tmat[a]+3); - - VECCOPY(ob->loc, tmat[3]); - - Mat3CpyMat4(omat, tmat); - Mat3ToEul(omat, ob->rot); - EulToMat3(ob->rot, mat); - Mat3Inv(imat, mat); - Mat3MulMat3((float ( * )[3])tmat, imat, omat); - - while(tot--) { - nu= (Nurb*)MEM_callocN(sizeof(Nurb),"nu from exotic"); - BLI_addtail(&cu->nurb, nu); - - fscanf(fp, "%d\n", &type); - nu->type= type; - - fscanf(fp, "%d %d\n", &type, &val); - nu->pntsu= type; nu->pntsv= val; - fscanf(fp, "%d %d\n", &type, &val); - nu->resolu= type; nu->resolv= val; - fscanf(fp, "%d %d\n", &type, &val); - nu->orderu= type; nu->orderv= val; - fscanf(fp, "%d %d\n", &type, &val); - nu->flagu= type; nu->flagv= val; - - if( (nu->type & 7)==CU_BEZIER) { - a= nu->pntsu; - nu->bezt= bezt= MEM_callocN(a*sizeof(BezTriple), "bezt from exotic"); - while(a--) { - fscanf(fp, "%f %f %f ", bezt->vec[0], bezt->vec[0]+1, bezt->vec[0]+2); - Mat4MulVecfl(tmat, bezt->vec[0]); - fscanf(fp, "%f %f %f ", bezt->vec[1], bezt->vec[1]+1, bezt->vec[1]+2); - Mat4MulVecfl(tmat, bezt->vec[1]); - fscanf(fp, "%f %f %f ", bezt->vec[2], bezt->vec[2]+1, bezt->vec[2]+2); - Mat4MulVecfl(tmat, bezt->vec[2]); - fscanf(fp, "%d %d\n", &type, &val); - bezt->h1= type; - bezt->h2= val; - bezt++; - } - } - else { - a= nu->pntsu*nu->pntsv; - if(a) { - nu->bp= bp= MEM_callocN(a*sizeof(BPoint), "bp from exotic"); - while(a--) { - fscanf(fp, "%f %f %f %f\n", bp->vec, bp->vec+1, bp->vec+2, bp->vec+3); - Mat4MulVecfl(tmat, bp->vec); - bp++; - } - - val= KNOTSU(nu); - nu->knotsu= MEM_mallocN(sizeof(float)*val, "knots"); - for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsu+a); - - if(nu->pntsv>1) { - val= KNOTSV(nu); - nu->knotsv= MEM_mallocN(sizeof(float)*val, "knots"); - for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsv+a); - } - } - else { - BLI_remlink(&cu->nurb, nu); - MEM_freeN(nu); - } - } - } - fclose(fp); -} - -static void read_videoscape(Scene *scene, char *str) -{ - int file, type; - unsigned int val; - unsigned short numlen; - char name[FILE_MAXDIR+FILE_MAXFILE], head[FILE_MAXDIR+FILE_MAXFILE], tail[FILE_MAXFILE]; - - strcpy(name, str); - - while( TRUE ) { - file= open(name, O_BINARY|O_RDONLY); - if(file<=0) break; - else { - read(file, &type, 4); - close(file); - - if(type==DDG1) read_videoscape_mesh(scene, name); - else if(type==DDG2) read_videoscape_lamp(scene, name); - else if(type==DDG3) read_videoscape_nurbs(scene, name); - } - - val = BLI_stringdec(name, head, tail, &numlen); - BLI_stringenc(name, head, tail, numlen, val + 1); - - } -} - - /* ***************** INVENTOR ******************* */ @@ -2204,16 +1785,7 @@ int BKE_read_exotic(Scene *scene, char *name) if ((*s0 != FORM) && (strncmp(str, "BLEN", 4) != 0) && !BLI_testextensie(name,".blend.gz")) { //XXX waitcursor(1); - - if(ELEM4(*s0, DDG1, DDG2, DDG3, DDG4)) { - if(0) { // XXX obedit) { - //XXX error("Unable to perform function in EditMode"); - } else { - read_videoscape(scene, name); - retval = 1; - } - } - else if(strncmp(str, "#Inventor V1.0", 14)==0) { + if(strncmp(str, "#Inventor V1.0", 14)==0) { if( strncmp(str+15, "ascii", 5)==0) { read_inventor(scene, name, &lbase); displist_to_objects(scene, &lbase); @@ -2385,167 +1957,6 @@ void write_stl(Scene *scene, char *str) //XXX waitcursor(0); } -static void write_videoscape_mesh(Scene *scene, Object *ob, char *str) -{ - Mesh *me= ob->data; - EditMesh *em = BKE_mesh_get_editmesh(me); - Material *ma; - MFace *mface; - FILE *fp; - EditVert *eve; - EditFace *evl; - unsigned int kleur[32]; - float co[3]; - int a; - intptr_t tot; - char *cp; - - if(ob && ob->type==OB_MESH); - else { - return; - } - - kleur[0]= 0x00C0C0C0; - - cp= (char *)kleur; - for(a=0; a<ob->totcol; a++, cp+=4) { - - ma= give_current_material(ob, a+1); - if(ma) { - cp[0]= (unsigned char) (255.0*ma->emit); - cp[1]= (unsigned char) (255.0*ma->b); - cp[2]= (unsigned char) (255.0*ma->g); - cp[3]= (unsigned char) (255.0*ma->r); - if(ENDIAN_ORDER==L_ENDIAN) SWITCH_INT(kleur[a]); - } - else kleur[a]= 0x00C0C0C0; - - if(a>30) break; - } - - fp= fopen(str, "wb"); - if(fp==NULL) return; - - fprintf(fp,"3DG1\n"); - - if(em) { - - fprintf(fp, "%d\n", em->totvert); - - tot= 0; - eve= em->verts.first; - while(eve) { - VECCOPY(co, eve->co); - Mat4MulVecfl(ob->obmat, co); - fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] ); - eve->tmp.l = tot; - tot++; - eve= eve->next; - } - evl= em->faces.first; - while(evl) { - - if(evl->v4==0) { - fprintf(fp, "3 %ld %ld %ld 0x%x\n", - (intptr_t) evl->v1->tmp.l, - (intptr_t) evl->v2->tmp.l, - (intptr_t) evl->v3->tmp.l, - kleur[evl->mat_nr]); - } - else { - fprintf(fp, "4 %ld %ld %ld %ld 0x%x\n", - (intptr_t) evl->v1->tmp.l, - (intptr_t) evl->v2->tmp.l, - (intptr_t) evl->v3->tmp.l, - (intptr_t) evl->v4->tmp.l, - kleur[evl->mat_nr]); - } - evl= evl->next; - } - } - else { - DerivedMesh *dm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH); - - me= ob->data; - - fprintf(fp, "%d\n", me->totvert); - - mface= me->mface; - for(a=0; a<me->totvert; a++) { - dm->getVertCo(dm, a, co); - Mat4MulVecfl(ob->obmat, co); - fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] ); - } - for(a=0; a<me->totface; a++, mface++) { - if(mface->v4==0) { - fprintf(fp, "3 %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, kleur[(int)mface->mat_nr]); - } - else { - fprintf(fp, "4 %d %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, mface->v4, kleur[(int)mface->mat_nr]); - } - } - - dm->release(dm); - } - - fclose(fp); - - if (em) BKE_mesh_end_editmesh(me, em); - -} - - -void write_videoscape(Scene *scene, char *str) -{ - Base *base; - int file, val, lampdone=0; - unsigned short numlen; - char head[FILE_MAXFILE], tail[FILE_MAXFILE]; - - if(BLI_testextensie(str,".trace")) str[ strlen(str)-6]= 0; - if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0; - if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0; - if(BLI_testextensie(str,".obj")==0) strcat(str, ".obj"); - - file= open(str,O_BINARY|O_RDONLY); - close(file); - //XXX saveover() - // if(file>-1) if(!during_script() && saveover(str)==0) return; - - strcpy(temp_dir, str); - - base= scene->base.first; - while(base) { - if((base->flag & SELECT) && (base->lay & scene->lay)) { - if(base->object->type==OB_MESH) { - write_videoscape_mesh(scene, base->object, str); - val = BLI_stringdec(str, head, tail, &numlen); - BLI_stringenc(str, head, tail, numlen, val + 1); - } - else if(base->object->type==OB_CURVE || base->object->type==OB_SURF) { - /* write_videoscape_nurbs(base->object, str); */ - /* val = stringdec(str, head, tail, &numlen); */ - /* stringenc(str, head, tail, numlen, val + 1); */ - } - else if(lampdone==0 && base->object->type==OB_LAMP) { - /* lampdone= 1; */ - /* write_videoscape_lamps(str); */ - /* val = stringdec(str, head, tail, &numlen); */ - /* stringenc(str, head, tail, numlen, val + 1); */ - } - } - base= base->next; - } - - - /* remove when higher numbers exist */ - while(remove(str)==0) { - - val = BLI_stringdec(str, head, tail, &numlen); - BLI_stringenc(str, head, tail, numlen, val + 1); - } -} - /* ******************************* WRITE VRML ***************************** */ static void replace_chars(char *str1, char *str2) diff --git a/source/blender/blenkernel/intern/multires.c b/source/blender/blenkernel/intern/multires.c index b1387281cf5..5def910ddef 100644 --- a/source/blender/blenkernel/intern/multires.c +++ b/source/blender/blenkernel/intern/multires.c @@ -1270,8 +1270,10 @@ void multires_free(Multires *mr) if(lvl) { CustomData_free(&mr->vdata, lvl->totvert); CustomData_free(&mr->fdata, lvl->totface); - MEM_freeN(mr->edge_flags); - MEM_freeN(mr->edge_creases); + if(mr->edge_flags) + MEM_freeN(mr->edge_flags); + if(mr->edge_creases) + MEM_freeN(mr->edge_creases); } while(lvl) { diff --git a/source/blender/blenkernel/intern/particle.c b/source/blender/blenkernel/intern/particle.c index 34e69b2d736..5b3720cd6b0 100644 --- a/source/blender/blenkernel/intern/particle.c +++ b/source/blender/blenkernel/intern/particle.c @@ -222,6 +222,45 @@ short psys_get_current_num(Object *ob) return i; } +Object *psys_find_object(Scene *scene, ParticleSystem *psys) +{ + Base *base = scene->base.first; + ParticleSystem *tpsys; + + for(base = scene->base.first; base; base = base->next) { + for(tpsys = base->object->particlesystem.first; psys; psys=psys->next) { + if(tpsys == psys) + return base->object; + } + } + + return NULL; +} +int psys_count_autocache(Scene *scene, ParticleSettings *part) +{ + Base *base = scene->base.first; + ParticleSystem *psys; + PTCacheID pid; + int autocache_count= 0; + + for(base = scene->base.first; base; base = base->next) { + for(psys = base->object->particlesystem.first; psys; psys=psys->next) { + if(part && psys->part != part) + continue; + + BKE_ptcache_id_from_particles(&pid, base->object, psys); + + if((psys->pointcache->flag & PTCACHE_BAKED) + || (psys->pointcache->flag & PTCACHE_AUTOCACHE)==0) + continue; + + if((psys->pointcache->flag & PTCACHE_OUTDATED) + || BKE_ptcache_id_exist(&pid, CFRA)==0) + autocache_count++; + } + } + return autocache_count; +} /* change object's active particle system */ void psys_change_act(void *ob_v, void *act_v) { @@ -864,7 +903,7 @@ static void weighted_particle_vector(float *v1, float *v2, float *v3, float *v4, vec[1]= weights[0]*v1[1] + weights[1]*v2[1] + weights[2]*v3[1] + weights[3]*v4[1]; vec[2]= weights[0]*v1[2] + weights[1]*v2[2] + weights[2]*v3[2] + weights[3]*v4[2]; } -static void interpolate_particle(short type, ParticleKey keys[4], float dt, ParticleKey *result, int velocity) +void psys_interpolate_particle(short type, ParticleKey keys[4], float dt, ParticleKey *result, int velocity) { float t[4]; @@ -2569,7 +2608,7 @@ void psys_cache_paths(Scene *scene, Object *ob, ParticleSystem *psys, float cfra } /* now we should have in chronologiacl order k1<=k2<=t<=k3<=k4 with keytime between [0,1]->[k2,k3] (k1 & k4 used for cardinal & bspline interpolation)*/ - interpolate_particle((psys->flag & PSYS_KEYED) ? -1 /* signal for cubic interpolation */ + psys_interpolate_particle((psys->flag & PSYS_KEYED) ? -1 /* signal for cubic interpolation */ : ((psys->part->flag & PART_HAIR_BSPLINE) ? KEY_BSPLINE : KEY_CARDINAL) ,keys, keytime, &result, 0); @@ -3062,7 +3101,6 @@ void make_local_particlesettings(ParticleSettings *part) } } } - void psys_flush_particle_settings(Scene *scene, ParticleSettings *part, int recalc) { Base *base = scene->base.first; @@ -3495,7 +3533,7 @@ void psys_get_particle_on_path(Scene *scene, Object *ob, ParticleSystem *psys, i QuatInterpol(state->rot,keys[1].rot,keys[2].rot,keytime); } - interpolate_particle((psys->flag & PSYS_KEYED) ? -1 /* signal for cubic interpolation */ + psys_interpolate_particle((psys->flag & PSYS_KEYED) ? -1 /* signal for cubic interpolation */ : ((psys->part->flag & PART_HAIR_BSPLINE) ? KEY_BSPLINE : KEY_CARDINAL) ,keys, keytime, state, 1); @@ -3776,7 +3814,7 @@ int psys_get_particle_state(struct Scene *scene, Object *ob, ParticleSystem *psy VecMulf(keys[1].vel, dfra / frs_sec); VecMulf(keys[2].vel, dfra / frs_sec); - interpolate_particle(-1, keys, keytime, state, 1); + psys_interpolate_particle(-1, keys, keytime, state, 1); /* convert back to real velocity */ VecMulf(state->vel, frs_sec / dfra); diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index 52f13eeadb8..fc6413849d1 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -104,7 +104,8 @@ static int get_current_display_percentage(ParticleSystem *psys) { ParticleSettings *part=psys->part; - if(psys->renderdata || (part->child_nbr && part->childtype)) + if(psys->renderdata || (part->child_nbr && part->childtype) + || (psys->pointcache->flag & PTCACHE_BAKING)) return 100; if(part->phystype==PART_PHYS_KEYED){ @@ -2195,57 +2196,80 @@ void psys_get_reactor_target(Object *ob, ParticleSystem *psys, Object **target_o /************************************************/ /* Point Cache */ /************************************************/ - -static void write_particles_to_cache(Object *ob, ParticleSystem *psys, int cfra) +void psys_get_pointcache_start_end(Scene *scene, ParticleSystem *psys, int *sfra, int *efra) { - PTCacheID pid; - PTCacheFile *pf; - ParticleData *pa; - int i, totpart= psys->totpart; + ParticleSettings *part = psys->part; - if(totpart == 0) - return; + *sfra = MAX2(1, (int)part->sta); + *efra = MIN2((int)(part->end + part->lifetime + 1.0), scene->r.efra); +} +static float *particle_state_ptr(int index, ParticleSystem *psys) +{ + return (float *)(&(psys->particles+index)->state); +} +static void particle_read_state(int index, ParticleSystem *psys, float *data) +{ + ParticleData *pa = psys->particles + index; + ParticleKey *key = (ParticleKey *)data; - BKE_ptcache_id_from_particles(&pid, ob, psys); - pf= BKE_ptcache_file_open(&pid, PTCACHE_FILE_WRITE, cfra); - if(!pf) - return; + if(key->time > pa->state.time) + copy_particle_key(&pa->prev_state, &pa->state, 1); - /* assuming struct consists of tightly packed floats */ - for(i=0, pa=psys->particles; i<totpart; i++, pa++) - BKE_ptcache_file_write_floats(pf, (float*)&pa->state, sizeof(ParticleKey)/sizeof(float)); - - BKE_ptcache_file_close(pf); + copy_particle_key(&pa->state, key, 1); } +static void particle_cache_interpolate(int index, ParticleSystem *psys, float frs_sec, float cfra, int cfra1, int cfra2, float *data1, float *data2) +{ + ParticleData *pa = psys->particles + index; + ParticleKey keys[4]; + float dfra; + + keys[1] = *((ParticleKey*)data1); + keys[2] = *((ParticleKey*)data2); + + dfra = keys[2].time - keys[1].time; -static int get_particles_from_cache(Object *ob, ParticleSystem *psys, int cfra) + VecMulf(keys[1].vel, dfra / frs_sec); + VecMulf(keys[2].vel, dfra / frs_sec); + + psys_interpolate_particle(-1, keys, (keys[1].time - cfra) / dfra, &pa->state, 1); + + VecMulf(pa->state.vel, frs_sec / dfra); +} +static void write_particles_to_cache(Object *ob, ParticleSystem *psys, int cfra) { + PTCacheWriter writer; PTCacheID pid; - PTCacheFile *pf; - ParticleData *pa; - int i, totpart= psys->totpart; - - if(totpart == 0) - return 0; BKE_ptcache_id_from_particles(&pid, ob, psys); - pf= BKE_ptcache_file_open(&pid, PTCACHE_FILE_READ, cfra); - if(!pf) - return 0; - /* assuming struct consists of tightly packed floats */ - for(i=0, pa=psys->particles; i<totpart; i++, pa++) { - if(cfra!=pa->state.time) - copy_particle_key(&pa->prev_state,&pa->state,1); - if(!BKE_ptcache_file_read_floats(pf, (float*)&pa->state, sizeof(ParticleKey)/sizeof(float))) { - BKE_ptcache_file_close(pf); - return 0; - } - } + writer.calldata = psys; + writer.cfra = cfra; + writer.elem_ptr = particle_state_ptr; + writer.pid = &pid; + writer.totelem = psys->totpart; - BKE_ptcache_file_close(pf); + BKE_ptcache_write_cache(&writer); +} - return 1; +static int get_particles_from_cache(Scene *scene, Object *ob, ParticleSystem *psys, float cfra, int allow_interpolate, int allow_old, int *old_frame) +{ + PTCacheReader reader; + PTCacheID pid; + + BKE_ptcache_id_from_particles(&pid, ob, psys); + + reader.allow_interpolate = allow_interpolate; + reader.allow_old = allow_old; + reader.calldata = psys; + reader.cfra = cfra; + reader.interpolate_elem = particle_cache_interpolate; + reader.old_frame = old_frame; + reader.pid = &pid; + reader.scene = scene; + reader.set_elem = particle_read_state; + reader.totelem = psys->totpart; + + return BKE_ptcache_read_cache(&reader); } /************************************************/ @@ -4085,7 +4109,7 @@ static void dynamics_step(Scene *scene, Object *ob, ParticleSystem *psys, Partic /* main loop: calculate physics for all particles */ for(p=0, pa=psys->particles; p<totpart; p++,pa++){ - if(pa->flag & PARS_UNEXIST) continue; + if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP)) continue; copy_particle_key(&pa->prev_state,&pa->state,1); @@ -4110,25 +4134,26 @@ static void dynamics_step(Scene *scene, Object *ob, ParticleSystem *psys, Partic if(pa->alive==PARS_UNBORN || pa->alive==PARS_KILLED || ELEM(part->phystype,PART_PHYS_NO,PART_PHYS_KEYED) - || birthtime >= cfra){ + || birthtime >= psys->cfra){ reset_particle(scene, pa,psys,psmd,ob,dtime,cfra,vg_vel,vg_tan,vg_rot); } pa_dfra = dfra; pa_dtime = dtime; - if(birthtime <= cfra && birthtime >= psys->cfra){ + + if(dietime <= cfra && psys->cfra < dietime){ + /* particle dies some time between this and last step */ + pa_dfra = dietime - ((birthtime > psys->cfra) ? birthtime : psys->cfra); + pa_dtime = pa_dfra * timestep; + pa->alive = PARS_DYING; + } + else if(birthtime <= cfra && birthtime >= psys->cfra){ /* particle is born some time between this and last step*/ pa->alive = PARS_ALIVE; pa_dfra = cfra - birthtime; pa_dtime = pa_dfra*timestep; } - else if(dietime <= cfra && psys->cfra < dietime){ - /* particle dies some time between this and last step */ - pa_dfra = dietime - psys->cfra; - pa_dtime = pa_dfra * timestep; - pa->alive = PARS_DYING; - } else if(dietime < cfra){ /* nothing to be done when particle is dead */ } @@ -4520,7 +4545,7 @@ static void system_step(Scene *scene, Object *ob, ParticleSystem *psys, Particle int totpart, oldtotpart, totchild, oldtotchild, p; float disp, *vg_vel= 0, *vg_tan= 0, *vg_rot= 0, *vg_size= 0; int init= 0, distr= 0, alloc= 0, usecache= 0, only_children_changed= 0; - int framenr, framedelta, startframe, endframe; + int framenr, framedelta, startframe, endframe, old_framenr; part= psys->part; cache= psys->pointcache; @@ -4528,6 +4553,10 @@ static void system_step(Scene *scene, Object *ob, ParticleSystem *psys, Particle framenr= (int)scene->r.cfra; framedelta= framenr - cache->simframe; + /* set suitable cache range automatically */ + if((cache->flag & (PTCACHE_BAKING|PTCACHE_BAKED))==0) + psys_get_pointcache_start_end(scene, psys, &cache->startframe, &cache->endframe); + BKE_ptcache_id_from_particles(&pid, ob, psys); BKE_ptcache_id_time(&pid, scene, 0.0f, &startframe, &endframe, NULL); @@ -4600,9 +4629,13 @@ static void system_step(Scene *scene, Object *ob, ParticleSystem *psys, Particle if(init) { if(distr) { - if(alloc) + if(alloc) { realloc_particles(ob, psys, totpart); + if(usecache) + BKE_ptcache_id_clear(&pid, PTCACHE_CLEAR_ALL, 0); + } + distribute_particles(scene, ob, psys, part->from); if((psys->part->type == PART_HAIR) && !(psys->flag & PSYS_HAIR_DONE)) @@ -4616,9 +4649,11 @@ static void system_step(Scene *scene, Object *ob, ParticleSystem *psys, Particle free_keyed_keys(psys); initialize_all_particles(ob, psys, psmd); + - if(alloc) + if(alloc) { reset_all_particles(scene, ob, psys, psmd, 0.0, cfra, oldtotpart); + } } /* flag for possible explode modifiers after this system */ @@ -4627,25 +4662,47 @@ static void system_step(Scene *scene, Object *ob, ParticleSystem *psys, Particle /* try to read from the cache */ if(usecache) { - if(get_particles_from_cache(ob, psys, framenr)) { - if(part->phystype==PART_PHYS_KEYED && psys->flag&PSYS_FIRST_KEYED) { - psys_count_keyed_targets(ob,psys); - set_keyed_keys(scene, ob, psys); - } + int result = get_particles_from_cache(scene, ob, psys, (float)framenr, 0, 1, &old_framenr); + + if(result == PTCACHE_READ_EXACT) { + //if(part->phystype==PART_PHYS_KEYED && psys->flag&PSYS_FIRST_KEYED) { + // psys_count_keyed_targets(ob,psys); + // set_keyed_keys(scene, ob, psys); + //} cached_step(scene, ob, psmd, psys, cfra); psys->cfra=cfra; psys->recalc = 0; - if(part->phystype==PART_PHYS_KEYED && psys->flag&PSYS_FIRST_KEYED) { - psys_update_path_cache(scene, ob, psmd, psys, framenr); - } + //if(part->phystype==PART_PHYS_KEYED && psys->flag&PSYS_FIRST_KEYED) { + // psys_update_path_cache(scene, ob, psmd, psys, framenr); + //} cache->simframe= framenr; cache->flag |= PTCACHE_SIMULATION_VALID; + if(cache->flag & PTCACHE_OUTDATED) + BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_FREE); + return; } + else if((cache->flag & PTCACHE_AUTOCACHE)==0 && result==PTCACHE_READ_OLD) { + /* clear cache after current frame */ + BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_FREE); + + /* set old cfra */ + psys->cfra = (float)old_framenr; + + for(p=0, pa=psys->particles; p<totpart; p++, pa++) { + /* update alive status */ + if(pa->time > psys->cfra) + pa->alive = PARS_UNBORN; + else if(pa->dietime <= psys->cfra) + pa->alive = PARS_DEAD; + else + pa->alive = PARS_ALIVE; + } + } else if(ob->id.lib || (cache->flag & PTCACHE_BAKED)) { psys_reset(psys, PSYS_RESET_CACHE_MISS); psys->cfra=cfra; @@ -4653,8 +4710,10 @@ static void system_step(Scene *scene, Object *ob, ParticleSystem *psys, Particle return; } - if(framenr != startframe && framedelta != 1) { - psys_reset(psys, PSYS_RESET_CACHE_MISS); + if(framenr != startframe && framedelta != 1 && cache->flag & PTCACHE_AUTOCACHE) { + //psys_reset(psys, PSYS_RESET_CACHE_MISS); + /* make sure cache is recalculated */ + BKE_ptcache_id_clear(&pid, PTCACHE_CLEAR_FRAME, (int)cfra); psys->cfra = cfra; psys->recalc = 0; return; @@ -4663,10 +4722,11 @@ static void system_step(Scene *scene, Object *ob, ParticleSystem *psys, Particle else { cache->flag &= ~PTCACHE_SIMULATION_VALID; cache->simframe= 0; + cache->last_exact= 0; } /* if on second frame, write cache for first frame */ - if(usecache && framenr == startframe+1) + if(usecache && psys->cfra == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact==0)) write_particles_to_cache(ob, psys, startframe); if(part->phystype==PART_PHYS_KEYED && psys->flag&PSYS_FIRST_KEYED) @@ -4768,8 +4828,7 @@ static void psys_to_softbody(Scene *scene, Object *ob, ParticleSystem *psys) static int hair_needs_recalc(ParticleSystem *psys) { if((psys->flag & PSYS_EDITED)==0 && - ((psys->flag & PSYS_HAIR_DONE)==0 || psys->recalc & PSYS_RECALC_REDO)) { - psys->recalc &= ~PSYS_RECALC_REDO; + ((psys->flag & PSYS_HAIR_DONE)==0 || psys->recalc & PSYS_RECALC_RESET)) { return 1; } diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c index b00755f7135..f59336518d7 100644 --- a/source/blender/blenkernel/intern/pointcache.c +++ b/source/blender/blenkernel/intern/pointcache.c @@ -51,9 +51,11 @@ #include "BKE_object.h" #include "BKE_particle.h" #include "BKE_pointcache.h" +#include "BKE_scene.h" #include "BKE_softbody.h" #include "BKE_utildefines.h" +#include "BLI_blenlib.h" /* needed for directory lookup */ #ifndef WIN32 @@ -213,21 +215,29 @@ static int BKE_ptcache_id_filename(PTCacheID *pid, char *filename, int cfra, sho filename[0] = '\0'; newname = filename; - /*if (!G.relbase_valid) return 0; *//* save blend file before using pointcache */ + if (!G.relbase_valid) return 0; /* save blend file before using disk pointcache */ /* start with temp dir */ if (do_path) { len = ptcache_path(pid, filename); newname += len; } - idname = (pid->ob->id.name+2); - /* convert chars to hex so they are always a valid filename */ - while('\0' != *idname) { - snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++)); - newname+=2; - len += 2; + if(strcmp(pid->cache->name, "")==0) { + idname = (pid->ob->id.name+2); + /* convert chars to hex so they are always a valid filename */ + while('\0' != *idname) { + snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++)); + newname+=2; + len += 2; + } } - + else { + int temp = strlen(pid->cache->name); + strcpy(newname, pid->cache->name); + newname+=temp; + len += temp; + } + if (do_ext) { snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */ len += 16; @@ -247,7 +257,7 @@ PTCacheFile *BKE_ptcache_file_open(PTCacheID *pid, int mode, int cfra) if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE) return NULL; - /*if (!G.relbase_valid) return NULL; *//* save blend file before using pointcache */ + if (!G.relbase_valid) return NULL; /* save blend file before using disk pointcache */ BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); @@ -286,6 +296,323 @@ int BKE_ptcache_file_write_floats(PTCacheFile *pf, float *f, int tot) return (fwrite(f, sizeof(float), tot, pf->fp) == tot); } +static int ptcache_pid_elemsize(PTCacheID *pid) +{ + if(pid->type==PTCACHE_TYPE_SOFTBODY) + return 0; // TODO + else if(pid->type==PTCACHE_TYPE_PARTICLES) + return sizeof(ParticleKey); + else if(pid->type==PTCACHE_TYPE_CLOTH) + return 0; // TODO + + return 0; +} +static int ptcache_pid_totelem(PTCacheID *pid) +{ + if(pid->type==PTCACHE_TYPE_SOFTBODY) + return 0; // TODO + else if(pid->type==PTCACHE_TYPE_PARTICLES) { + ParticleSystem *psys = pid->data; + return psys->totpart; + } + else if(pid->type==PTCACHE_TYPE_CLOTH) + return 0; // TODO + + return 0; +} + +void ptcache_update_info(PTCacheID *pid) +{ + PointCache *cache = pid->cache; + int totframes = 0; + + if(cache->flag & PTCACHE_DISK_CACHE) { + int cfra = cache->startframe; + + for(; cfra<=cache->endframe; cfra++) { + if(BKE_ptcache_id_exist(pid, cfra)) + totframes++; + } + + sprintf(cache->info, "%i frames on disk.", totframes); + } + else { + PTCacheMem *pm = cache->mem_cache.first; + float framesize = 0.0f, bytes = 0.0f; + int mb; + + if(pm) + framesize = (float)ptcache_pid_elemsize(pid) * (float)pm->totpoint; + + for(; pm; pm=pm->next) + totframes++; + + bytes = totframes * framesize; + + mb = (bytes > 1024.0f * 1024.0f); + + sprintf(cache->info, "%i frames in memory (%.1f %s).", + totframes, + bytes / (mb ? 1024.0f * 1024.0f : 1024.0f), + mb ? "Mb" : "kb"); + } +} +/* reads cache from disk or memory */ +/* possible to get old or interpolated result */ +int BKE_ptcache_read_cache(PTCacheReader *reader) +{ + PTCacheID *pid = reader->pid; + PTCacheFile *pf=NULL, *pf2=NULL; + PTCacheMem *pm=NULL, *pm2=NULL; + int totelem = reader->totelem; + float cfra = reader->cfra; + int cfrai = (int)cfra; + int elemsize = ptcache_pid_elemsize(pid); + int i, incr = elemsize / sizeof(float); + float frs_sec = reader->scene->r.frs_sec; + + if(totelem == 0) + return 0; + + /* first check if we have the actual frame cached */ + if(cfra == (float)cfrai) { + if(pid->cache->flag & PTCACHE_DISK_CACHE) { + pf= BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai); + } + else { + pm = pid->cache->mem_cache.first; + + for(; pm; pm=pm->next) { + if(pm->frame == cfrai) + break; + } + } + } + + /* if found, use exact frame */ + if(pf || pm) { + float *data; + + if(pm) + data = pm->data; + else + data = MEM_callocN(elemsize, "pointcache read data"); + + for(i=0; i<totelem; i++) { + if(pf) { + if(!BKE_ptcache_file_read_floats(pf, data, incr)) { + BKE_ptcache_file_close(pf); + MEM_freeN(data); + return 0; + } + + reader->set_elem(i, reader->calldata, data); + } + else { + reader->set_elem(i, reader->calldata, data); + data += incr; + } + } + + if(pf) { + BKE_ptcache_file_close(pf); + MEM_freeN(data); + } + + return PTCACHE_READ_EXACT; + } + /* no exact cache frame found so try to find cached frames around cfra */ + if(reader->allow_interpolate || reader->allow_old) { + int cfra1, cfra2; + + if(pid->cache->flag & PTCACHE_DISK_CACHE) { + pf=NULL; + while(cfrai > pid->cache->startframe && !pf) { + cfrai--; + pf= BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai); + cfra1 = cfrai; + } + + *(reader->old_frame) = cfrai; + + cfrai = (int)cfra; + while(cfrai < pid->cache->endframe && !pf2) { + cfrai++; + pf2= BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai); + cfra2 = cfrai; + } + } + else if(pid->cache->mem_cache.first){ + pm = pid->cache->mem_cache.first; + + while(pm->next && pm->next->frame < cfra) + pm= pm->next; + + if(pm) { + *(reader->old_frame) = pm->frame; + cfra1 = pm->frame; + } + + pm2 = pid->cache->mem_cache.last; + + while(pm2->prev && pm2->frame > cfra) + pm2= pm2->prev; + + if(pm2) + cfra2 = pm2->frame; + } + + if(reader->allow_interpolate && ((pf && pf2) || (pm && pm2))) { + /* interpolate from nearest frames */ + float *data1, *data2; + + if(pm) { + data1 = pm->data; + data2 = pm2->data; + } + else { + data1 = MEM_callocN(elemsize, "pointcache read data1"); + data2 = MEM_callocN(elemsize, "pointcache read data2"); + } + + for(i=0; i<totelem; i++) { + if(pf && pf2) { + if(!BKE_ptcache_file_read_floats(pf, data1, incr)) { + BKE_ptcache_file_close(pf); + BKE_ptcache_file_close(pf2); + MEM_freeN(data1); + MEM_freeN(data2); + return 0; + } + if(!BKE_ptcache_file_read_floats(pf2, data2, incr)) { + BKE_ptcache_file_close(pf); + BKE_ptcache_file_close(pf2); + MEM_freeN(data1); + MEM_freeN(data2); + return 0; + } + reader->interpolate_elem(i, reader->calldata, frs_sec, cfra, cfra1, cfra2, data1, data2); + } + else { + reader->interpolate_elem(i, reader->calldata, frs_sec, cfra, cfra1, cfra2, data1, data2); + data1 += incr; + data2 += incr; + } + } + + if(pf) { + BKE_ptcache_file_close(pf); + BKE_ptcache_file_close(pf2); + MEM_freeN(data1); + MEM_freeN(data2); + } + + return PTCACHE_READ_INTERPOLATED; + } + else if(reader->allow_old && (pf || pm)) { + /* use last valid cache frame */ + float *data; + + if(pm) + data = pm->data; + else + data = MEM_callocN(elemsize, "pointcache read data"); + + for(i=0; i<totelem; i++) { + if(pf) { + if(!BKE_ptcache_file_read_floats(pf, data, incr)) { + BKE_ptcache_file_close(pf); + if(pf2) + BKE_ptcache_file_close(pf2); + return 0; + } + reader->set_elem(i, reader->calldata, data); + } + else { + reader->set_elem(i, reader->calldata, data); + data += incr; + } + } + + if(pf) { + BKE_ptcache_file_close(pf); + MEM_freeN(data); + } + if(pf2) + BKE_ptcache_file_close(pf2); + + return PTCACHE_READ_OLD; + } + } + + if(pf) + BKE_ptcache_file_close(pf); + if(pf2) + BKE_ptcache_file_close(pf2); + + return 0; +} +/* writes cache to disk or memory */ +int BKE_ptcache_write_cache(PTCacheWriter *writer) +{ + PointCache *cache = writer->pid->cache; + PTCacheFile *pf= NULL; + int elemsize = ptcache_pid_elemsize(writer->pid); + int i, incr = elemsize / sizeof(float); + + if(writer->totelem == 0 || writer->cfra <= 0) + return 0; + + if(cache->flag & PTCACHE_DISK_CACHE) { + pf = BKE_ptcache_file_open(writer->pid, PTCACHE_FILE_WRITE, writer->cfra); + if(!pf) + return 0; + + for(i=0; i<writer->totelem; i++) + BKE_ptcache_file_write_floats(pf, writer->elem_ptr(i, writer->calldata), incr); + } + else { + PTCacheMem *pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem"); + PTCacheMem *pm2; + float *pmdata; + + pm->data = MEM_callocN(elemsize * writer->totelem, "Pointcache mem data"); + pmdata = pm->data; + + for(i=0; i<writer->totelem; i++, pmdata+=incr) + memcpy(pmdata, writer->elem_ptr(i, writer->calldata), elemsize); + + pm->frame = writer->cfra; + pm->totpoint = writer->totelem; + + /* find add location */ + pm2 = cache->mem_cache.first; + if(!pm2) + BLI_addtail(&cache->mem_cache, pm); + else if(pm2->frame == writer->cfra) { + /* overwrite same frame */ + MEM_freeN(pm2->data); + pm2->data = pm->data; + MEM_freeN(pm); + } + else { + while(pm2->next && pm2->next->frame < writer->cfra) + pm2 = pm2->next; + + BLI_insertlinkafter(&cache->mem_cache, pm2, pm); + } + } + + if(writer->cfra - cache->last_exact == 1) + cache->last_exact = writer->cfra; + + if(pf) + BKE_ptcache_file_close(pf); + + ptcache_update_info(writer->pid); + + return 1; +} /* youll need to close yourself after! * mode - PTCACHE_CLEAR_ALL, @@ -317,62 +644,116 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, int cfra) case PTCACHE_CLEAR_ALL: case PTCACHE_CLEAR_BEFORE: case PTCACHE_CLEAR_AFTER: - ptcache_path(pid, path); - - len = BKE_ptcache_id_filename(pid, filename, cfra, 0, 0); /* no path */ - - dir = opendir(path); - if (dir==NULL) - return; - - snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index); - - while ((de = readdir(dir)) != NULL) { - if (strstr(de->d_name, ext)) { /* do we have the right extension?*/ - if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */ - if (mode == PTCACHE_CLEAR_ALL) { - BLI_join_dirfile(path_full, path, de->d_name); - BLI_delete(path_full, 0, 0); - } else { - /* read the number of the file */ - int frame, len2 = strlen(de->d_name); - char num[7]; - - if (len2 > 15) { /* could crash if trying to copy a string out of this range*/ - BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num)); - frame = atoi(num); - - if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) || - (mode==PTCACHE_CLEAR_AFTER && frame > cfra) ) { + if(pid->cache->flag & PTCACHE_DISK_CACHE) { + ptcache_path(pid, path); + + len = BKE_ptcache_id_filename(pid, filename, cfra, 0, 0); /* no path */ + + dir = opendir(path); + if (dir==NULL) + return; + + snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index); + + while ((de = readdir(dir)) != NULL) { + if (strstr(de->d_name, ext)) { /* do we have the right extension?*/ + if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */ + if (mode == PTCACHE_CLEAR_ALL) { + pid->cache->last_exact = 0; + BLI_join_dirfile(path_full, path, de->d_name); + BLI_delete(path_full, 0, 0); + } else { + /* read the number of the file */ + int frame, len2 = strlen(de->d_name); + char num[7]; + + if (len2 > 15) { /* could crash if trying to copy a string out of this range*/ + BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num)); + frame = atoi(num); - BLI_join_dirfile(path_full, path, de->d_name); - BLI_delete(path_full, 0, 0); + if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) || + (mode==PTCACHE_CLEAR_AFTER && frame > cfra) ) { + + BLI_join_dirfile(path_full, path, de->d_name); + BLI_delete(path_full, 0, 0); + } } } } } } + closedir(dir); + } + else { + PTCacheMem *pm= pid->cache->mem_cache.first; + PTCacheMem *link= NULL; + + if(mode == PTCACHE_CLEAR_ALL) { + pid->cache->last_exact = 0; + for(; pm; pm=pm->next) + MEM_freeN(pm->data); + BLI_freelistN(&pid->cache->mem_cache); + } else { + while(pm) { + if((mode==PTCACHE_CLEAR_BEFORE && pm->frame < cfra) || + (mode==PTCACHE_CLEAR_AFTER && pm->frame > cfra) ) { + link = pm; + pm = pm->next; + MEM_freeN(link->data); + BLI_freelinkN(&pid->cache->mem_cache, link); + } + else + pm = pm->next; + } + } } - closedir(dir); break; case PTCACHE_CLEAR_FRAME: - len = BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); /* no path */ - BLI_delete(filename, 0, 0); + if(pid->cache->flag & PTCACHE_DISK_CACHE) { + if(BKE_ptcache_id_exist(pid, cfra)) { + BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); /* no path */ + BLI_delete(filename, 0, 0); + } + } + else { + PTCacheMem *pm = pid->cache->mem_cache.first; + + for(; pm; pm=pm->next) { + if(pm->frame == cfra) { + MEM_freeN(pm->data); + BLI_freelinkN(&pid->cache->mem_cache, pm); + break; + } + } + } break; } + + ptcache_update_info(pid); } int BKE_ptcache_id_exist(PTCacheID *pid, int cfra) { - char filename[MAX_PTCACHE_FILE]; - if(!pid->cache) return 0; - BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); + if(pid->cache->flag & PTCACHE_DISK_CACHE) { + char filename[MAX_PTCACHE_FILE]; + + BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); - return BLI_exists(filename); + return BLI_exists(filename); + } + else { + PTCacheMem *pm = pid->cache->mem_cache.first; + + for(; pm; pm=pm->next) { + if(pm->frame==cfra) + return 1; + } + return 0; + } } void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale) @@ -414,10 +795,10 @@ void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startfra } } -int BKE_ptcache_id_reset(PTCacheID *pid, int mode) +int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode) { PointCache *cache; - int reset, clear; + int reset, clear, current, after; if(!pid->cache) return 0; @@ -425,11 +806,20 @@ int BKE_ptcache_id_reset(PTCacheID *pid, int mode) cache= pid->cache; reset= 0; clear= 0; + current= 0; + after= 0; if(mode == PTCACHE_RESET_DEPSGRAPH) { if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) { - reset= 1; - clear= 1; + if(cache->flag & PTCACHE_AUTOCACHE) { + reset= 1; + clear= 1; + } + else { + current= 1; + after= 1; + cache->flag |= PTCACHE_OUTDATED; + } } else cache->flag |= PTCACHE_OUTDATED; @@ -449,10 +839,19 @@ int BKE_ptcache_id_reset(PTCacheID *pid, int mode) if(!(cache->flag & PTCACHE_BAKED)) clear= 1; } + else if(mode == PTCACHE_RESET_FREE) { + if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) { + if((cache->flag & PTCACHE_AUTOCACHE)==0) { + current= 1; + after= 1; + } + } + } if(reset) { cache->flag &= ~(PTCACHE_OUTDATED|PTCACHE_SIMULATION_VALID); cache->simframe= 0; + cache->last_exact= 0; if(pid->type == PTCACHE_TYPE_CLOTH) cloth_free_modifier(pid->ob, pid->data); @@ -463,11 +862,15 @@ int BKE_ptcache_id_reset(PTCacheID *pid, int mode) } if(clear) BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + if(after) + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA); + if(current) + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, CFRA); - return (reset || clear); + return (reset || clear || current || after); } -int BKE_ptcache_object_reset(Object *ob, int mode) +int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode) { PTCacheID pid; ParticleSystem *psys; @@ -479,7 +882,7 @@ int BKE_ptcache_object_reset(Object *ob, int mode) if(ob->soft) { BKE_ptcache_id_from_softbody(&pid, ob, ob->soft); - reset |= BKE_ptcache_id_reset(&pid, mode); + reset |= BKE_ptcache_id_reset(scene, &pid, mode); } for(psys=ob->particlesystem.first; psys; psys=psys->next) { @@ -488,23 +891,23 @@ int BKE_ptcache_object_reset(Object *ob, int mode) if(psys->soft) { BKE_ptcache_id_from_softbody(&pid, ob, psys->soft); if(mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED))) - reset |= BKE_ptcache_id_reset(&pid, mode); + reset |= BKE_ptcache_id_reset(scene, &pid, mode); else skip = 1; } - else if((psys->recalc & PSYS_RECALC_RESET)==0) + else if(psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD) skip = 1; if(skip == 0) { BKE_ptcache_id_from_particles(&pid, ob, psys); - reset |= BKE_ptcache_id_reset(&pid, mode); + reset |= BKE_ptcache_id_reset(scene, &pid, mode); } } for(md=ob->modifiers.first; md; md=md->next) { if(md->type == eModifierType_Cloth) { BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md); - reset |= BKE_ptcache_id_reset(&pid, mode); + reset |= BKE_ptcache_id_reset(scene, &pid, mode); } } @@ -564,7 +967,7 @@ void BKE_ptcache_set_continue_physics(Scene *scene, int enable) if(CONTINUE_PHYSICS == 0) { for(ob=G.main->object.first; ob; ob=ob->id.next) - if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_OUTDATED)) + if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED)) DAG_object_flush_update(scene, ob, OB_RECALC_DATA); } } @@ -590,6 +993,14 @@ PointCache *BKE_ptcache_add() void BKE_ptcache_free(PointCache *cache) { + PTCacheMem *pm = cache->mem_cache.first; + if(pm) { + for(; pm; pm=pm->next) + MEM_freeN(pm->data); + + BLI_freelistN(&cache->mem_cache); + } + MEM_freeN(cache); } @@ -605,3 +1016,231 @@ PointCache *BKE_ptcache_copy(PointCache *cache) return ncache; } + + +/* Baking */ +void BKE_ptcache_autocache_all(Scene *scene) +{ + PTCacheBaker baker; + + baker.bake=0; + baker.break_data=NULL; + baker.break_test=NULL; + baker.pid=NULL; + baker.progressbar=NULL; + baker.progresscontext=NULL; + baker.render=0; + baker.scene=scene; + + if(psys_count_autocache(scene, NULL)) + BKE_ptcache_make_cache(&baker); +} + +/* if bake is not given run simulations to current frame */ +void BKE_ptcache_make_cache(PTCacheBaker* baker) +{ + Scene *scene = baker->scene; + Base *base; + ListBase pidlist; + PTCacheID *pid = baker->pid; + PointCache *cache; + float frameleno = scene->r.framelen; + int cfrao = CFRA; + int startframe = MAXFRAME; + int endframe = CFRA; + int bake = baker->bake; + int render = baker->render; + int end = 0; + + G.afbreek = 0; + + //printf("Caching physics..."); + + /* set caches to baking mode and figure out start frame */ + if(pid) { + /* cache/bake a single object */ + cache = pid->cache; + if((cache->flag & PTCACHE_BAKED)==0) { + if(pid->type==PTCACHE_TYPE_PARTICLES) + psys_get_pointcache_start_end(scene, pid->data, &cache->startframe, &cache->endframe); + + if(bake || cache->flag & PTCACHE_OUTDATED) + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + + startframe = MAX2(cache->last_exact, cache->startframe); + + if(bake) { + endframe = cache->endframe; + cache->flag |= PTCACHE_BAKING; + } + else + endframe = MIN2(endframe, cache->endframe); + + cache->flag &= ~PTCACHE_BAKED; + } + } + else for(base=scene->base.first; base; base= base->next) { + /* cache/bake everything in the scene */ + BKE_ptcache_ids_from_object(&pidlist, base->object); + + for(pid=pidlist.first; pid; pid=pid->next) { + cache = pid->cache; + if((cache->flag & PTCACHE_BAKED)==0) { + if(pid->type==PTCACHE_TYPE_PARTICLES) + psys_get_pointcache_start_end(scene, pid->data, &cache->startframe, &cache->endframe); + + if(cache->flag & PTCACHE_OUTDATED) + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + + startframe = MIN2(startframe, cache->startframe); + + if(bake) { + endframe = MAX2(endframe, cache->endframe); + cache->flag |= PTCACHE_BAKING; + } + else if(render) + cache->flag |= PTCACHE_BAKING; + + cache->flag &= ~PTCACHE_BAKED; + + } + } + + BLI_freelistN(&pidlist); + } + + CFRA= startframe; + scene->r.framelen = 1.0; + scene_update_for_newframe(scene, scene->lay); + + for(; CFRA <= endframe; CFRA++) { + float prog; + + if(bake) + prog = (int)(100.0 * (float)(CFRA - startframe)/(float)(endframe-startframe)); + else + prog = CFRA; + + /* NOTE: baking should not redraw whole ui as this slows things down */ + if(baker->progressbar) + baker->progressbar(baker->progresscontext, prog); + + scene_update_for_newframe(scene, scene->lay); + + /* NOTE: breaking baking should leave calculated frames in cache, not clear it */ + if(baker->break_test && baker->break_test(baker->break_data)) + break; + } + + /* clear baking flag */ + if(pid) { + cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED); + if(bake) + cache->flag |= PTCACHE_BAKED; + } + else for(base=scene->base.first; base; base= base->next) { + BKE_ptcache_ids_from_object(&pidlist, base->object); + + for(pid=pidlist.first; pid; pid=pid->next) { + cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED); + if(bake) + cache->flag |= PTCACHE_BAKED; + } + } + + //printf("done!\n"); + + scene->r.framelen = frameleno; + CFRA = cfrao; + scene_update_for_newframe(scene, scene->lay); +} + +void BKE_ptcache_toggle_disk_cache(PTCacheID *pid) { + PointCache *cache = pid->cache; + PTCacheFile *pf; + PTCacheMem *pm; + int totelem=0; + int float_count=0; + int tot; + int write_error=0; + + if (!G.relbase_valid){ + cache->flag &= ~PTCACHE_DISK_CACHE; + return; + } + + totelem = ptcache_pid_totelem(pid); + float_count = ptcache_pid_elemsize(pid) / sizeof(float); + + if(totelem==0 || float_count==0) + return; + + tot = totelem*float_count; + + /* MEM -> DISK */ + if(cache->flag & PTCACHE_DISK_CACHE) { + pm = cache->mem_cache.first; + + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + + for(; pm; pm=pm->next) { + pf = BKE_ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame); + + if(pf) { + if(fwrite(pm->data, sizeof(float), tot, pf->fp) != tot) { + printf("Error writing to disk cache\n"); + + cache->flag &= ~PTCACHE_DISK_CACHE; + + BKE_ptcache_file_close(pf); + return; + } + BKE_ptcache_file_close(pf); + } + else + printf("Error creating disk cache file\n"); + } + + cache->flag &= ~PTCACHE_DISK_CACHE; + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + cache->flag |= PTCACHE_DISK_CACHE; + } + /* DISK -> MEM */ + else { + int cfra; + int sfra = cache->startframe; + int efra = cache->endframe; + + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + + for(cfra=sfra; cfra <= efra; cfra++) { + pf = BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfra); + + if(pf) { + pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem"); + pm->data = MEM_callocN(sizeof(float)*tot, "Pointcache mem data"); + + if(fread(pm->data, sizeof(float), tot, pf->fp)!= tot) { + printf("Error reading from disk cache\n"); + + cache->flag |= PTCACHE_DISK_CACHE; + + MEM_freeN(pm->data); + MEM_freeN(pm); + BKE_ptcache_file_close(pf); + return; + } + + pm->frame = cfra; + + BLI_addtail(&pid->cache->mem_cache, pm); + + BKE_ptcache_file_close(pf); + } + } + + cache->flag |= PTCACHE_DISK_CACHE; + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + cache->flag &= ~PTCACHE_DISK_CACHE; + } +}
\ No newline at end of file diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index 156bdae9b00..3b51af57e0b 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -142,8 +142,6 @@ void free_scene(Scene *sce) BLI_freelistN(&sce->base); seq_free_editing(sce->ed); - if(sce->radio) MEM_freeN(sce->radio); - sce->radio= 0; #ifndef DISABLE_PYTHON BPY_free_scriptlink(&sce->scriptlink); |