2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2007 by Janne Karhu.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/particle.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_particle_types.h"
46 #include "DNA_smoke_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_dynamicpaint_types.h"
50 #include "BLI_blenlib.h"
52 #include "BLI_utildefines.h"
53 #include "BLI_kdtree.h"
55 #include "BLI_threads.h"
56 #include "BLI_linklist.h"
57 #include "BLI_bpath.h"
60 #include "BKE_animsys.h"
62 #include "BKE_boids.h"
63 #include "BKE_cloth.h"
64 #include "BKE_effect.h"
65 #include "BKE_global.h"
66 #include "BKE_group.h"
68 #include "BKE_lattice.h"
70 #include "BKE_displist.h"
71 #include "BKE_particle.h"
72 #include "BKE_object.h"
73 #include "BKE_material.h"
75 #include "BKE_library.h"
76 #include "BKE_depsgraph.h"
77 #include "BKE_modifier.h"
79 #include "BKE_cdderivedmesh.h"
80 #include "BKE_pointcache.h"
81 #include "BKE_scene.h"
83 #include "RE_render_ext.h"
85 static void get_child_modifier_parameters(ParticleSettings *part, ParticleThreadContext *ctx,
86 ChildParticle *cpa, short cpa_from, int cpa_num, float *cpa_fuv, float *orco, ParticleTexture *ptex);
87 static void do_child_modifiers(ParticleSimulationData *sim,
88 ParticleTexture *ptex, ParticleKey *par, float *par_rot, ChildParticle *cpa,
89 float *orco, float mat[4][4], ParticleKey *state, float t);
91 /* few helpers for countall etc. */
92 int count_particles(ParticleSystem *psys){
93 ParticleSettings *part=psys->part;
97 LOOP_SHOWN_PARTICLES {
98 if(pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN)==0);
99 else if(pa->alive == PARS_DEAD && (part->flag & PART_DIED)==0);
104 int count_particles_mod(ParticleSystem *psys, int totgr, int cur){
105 ParticleSettings *part=psys->part;
109 LOOP_SHOWN_PARTICLES {
110 if(pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN)==0);
111 else if(pa->alive == PARS_DEAD && (part->flag & PART_DIED)==0);
112 else if(p%totgr==cur) tot++;
116 /* we allocate path cache memory in chunks instead of a big continguous
117 * chunk, windows' memory allocater fails to find big blocks of memory often */
119 #define PATH_CACHE_BUF_SIZE 1024
121 static ParticleCacheKey **psys_alloc_path_cache_buffers(ListBase *bufs, int tot, int steps)
124 ParticleCacheKey **cache;
125 int i, totkey, totbufkey;
129 cache = MEM_callocN(tot*sizeof(void*), "PathCacheArray");
131 while(totkey < tot) {
132 totbufkey= MIN2(tot-totkey, PATH_CACHE_BUF_SIZE);
133 buf= MEM_callocN(sizeof(LinkData), "PathCacheLinkData");
134 buf->data= MEM_callocN(sizeof(ParticleCacheKey)*totbufkey*steps, "ParticleCacheKey");
136 for(i=0; i<totbufkey; i++)
137 cache[totkey+i] = ((ParticleCacheKey*)buf->data) + i*steps;
140 BLI_addtail(bufs, buf);
146 static void psys_free_path_cache_buffers(ParticleCacheKey **cache, ListBase *bufs)
153 for(buf= bufs->first; buf; buf=buf->next)
154 MEM_freeN(buf->data);
158 /************************************************/
160 /************************************************/
161 /* get object's active particle system safely */
162 ParticleSystem *psys_get_current(Object *ob)
164 ParticleSystem *psys;
165 if(ob==NULL) return NULL;
167 for(psys=ob->particlesystem.first; psys; psys=psys->next){
168 if(psys->flag & PSYS_CURRENT)
174 short psys_get_current_num(Object *ob)
176 ParticleSystem *psys;
179 if(ob==NULL) return 0;
181 for(psys=ob->particlesystem.first, i=0; psys; psys=psys->next, i++)
182 if(psys->flag & PSYS_CURRENT)
187 void psys_set_current_num(Object *ob, int index)
189 ParticleSystem *psys;
194 for(psys=ob->particlesystem.first, i=0; psys; psys=psys->next, i++) {
196 psys->flag |= PSYS_CURRENT;
198 psys->flag &= ~PSYS_CURRENT;
201 Object *psys_find_object(Scene *scene, ParticleSystem *psys)
204 ParticleSystem *tpsys;
206 for(base = scene->base.first; base; base = base->next) {
207 for(tpsys = base->object->particlesystem.first; psys; psys=psys->next) {
215 Object *psys_get_lattice(ParticleSimulationData *sim)
217 Object *lattice=NULL;
219 if(psys_in_edit_mode(sim->scene, sim->psys)==0){
221 ModifierData *md = (ModifierData*)psys_get_modifier(sim->ob, sim->psys);
223 for(; md; md=md->next){
224 if(md->type==eModifierType_Lattice){
225 LatticeModifierData *lmd = (LatticeModifierData *)md;
231 init_latt_deform(lattice, NULL);
236 void psys_disable_all(Object *ob)
238 ParticleSystem *psys=ob->particlesystem.first;
240 for(; psys; psys=psys->next)
241 psys->flag |= PSYS_DISABLED;
243 void psys_enable_all(Object *ob)
245 ParticleSystem *psys=ob->particlesystem.first;
247 for(; psys; psys=psys->next)
248 psys->flag &= ~PSYS_DISABLED;
250 int psys_in_edit_mode(Scene *scene, ParticleSystem *psys)
252 return (scene->basact && (scene->basact->object->mode & OB_MODE_PARTICLE_EDIT) && psys==psys_get_current((scene->basact)->object) && (psys->edit || psys->pointcache->edit) && !psys->renderdata);
254 static void psys_create_frand(ParticleSystem *psys)
257 float *rand = psys->frand = MEM_callocN(PSYS_FRAND_COUNT * sizeof(float), "particle randoms");
259 BLI_srandom(psys->seed);
261 for(i=0; i<1024; i++, rand++)
264 int psys_check_enabled(Object *ob, ParticleSystem *psys)
266 ParticleSystemModifierData *psmd;
268 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE || !psys->part)
271 psmd= psys_get_modifier(ob, psys);
272 if(psys->renderdata || G.rendering) {
273 if(!(psmd->modifier.mode & eModifierMode_Render))
276 else if(!(psmd->modifier.mode & eModifierMode_Realtime))
279 /* perhaps not the perfect place, but we have to be sure the rands are there before usage */
281 psys_create_frand(psys);
282 else if(psys->recalc & PSYS_RECALC_RESET) {
283 MEM_freeN(psys->frand);
284 psys_create_frand(psys);
290 int psys_check_edited(ParticleSystem *psys)
292 if(psys->part && psys->part->type==PART_HAIR)
293 return (psys->flag & PSYS_EDITED || (psys->edit && psys->edit->edited));
295 return (psys->pointcache->edit && psys->pointcache->edit->edited);
298 void psys_check_group_weights(ParticleSettings *part)
300 ParticleDupliWeight *dw, *tdw;
304 if(part->ren_as == PART_DRAW_GR && part->dup_group && part->dup_group->gobject.first) {
305 /* first remove all weights that don't have an object in the group */
306 dw = part->dupliweights.first;
308 if(!object_in_group(dw->ob, part->dup_group)) {
310 BLI_freelinkN(&part->dupliweights, dw);
317 /* then add objects in the group to new list */
318 go = part->dup_group->gobject.first;
320 dw = part->dupliweights.first;
321 while(dw && dw->ob != go->ob)
325 dw = MEM_callocN(sizeof(ParticleDupliWeight), "ParticleDupliWeight");
328 BLI_addtail(&part->dupliweights, dw);
334 dw = part->dupliweights.first;
335 for(; dw; dw=dw->next) {
336 if(dw->flag & PART_DUPLIW_CURRENT) {
343 dw = part->dupliweights.first;
345 dw->flag |= PART_DUPLIW_CURRENT;
349 BLI_freelistN(&part->dupliweights);
352 int psys_uses_gravity(ParticleSimulationData *sim)
354 return sim->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY && sim->psys->part && sim->psys->part->effector_weights->global_gravity != 0.0f;
356 /************************************************/
358 /************************************************/
359 static void fluid_free_settings(SPHFluidSettings *fluid)
365 void psys_free_settings(ParticleSettings *part)
369 BKE_free_animdata(&part->id);
370 free_partdeflect(part->pd);
371 free_partdeflect(part->pd2);
373 if(part->effector_weights)
374 MEM_freeN(part->effector_weights);
376 BLI_freelistN(&part->dupliweights);
378 boid_free_settings(part->boids);
379 fluid_free_settings(part->fluid);
381 for(a=0; a<MAX_MTEX; a++) {
383 if(mtex && mtex->tex) mtex->tex->id.us--;
384 if(mtex) MEM_freeN(mtex);
388 void free_hair(Object *UNUSED(ob), ParticleSystem *psys, int dynamics)
399 psys->flag &= ~PSYS_HAIR_DONE;
403 BKE_ptcache_free_list(&psys->ptcaches);
404 psys->clmd->point_cache = psys->pointcache = NULL;
405 psys->clmd->ptcaches.first = psys->clmd->ptcaches.last = NULL;
407 modifier_free((ModifierData*)psys->clmd);
410 psys->pointcache = BKE_ptcache_add(&psys->ptcaches);
413 cloth_free_modifier(psys->clmd);
418 psys->hair_in_dm->release(psys->hair_in_dm);
419 psys->hair_in_dm = NULL;
421 if(psys->hair_out_dm)
422 psys->hair_out_dm->release(psys->hair_out_dm);
423 psys->hair_out_dm = NULL;
425 void free_keyed_keys(ParticleSystem *psys)
429 if(psys->part->type == PART_HAIR)
432 if(psys->particles && psys->particles->keys) {
433 MEM_freeN(psys->particles->keys);
443 static void free_child_path_cache(ParticleSystem *psys)
445 psys_free_path_cache_buffers(psys->childcache, &psys->childcachebufs);
446 psys->childcache = NULL;
447 psys->totchildcache = 0;
449 void psys_free_path_cache(ParticleSystem *psys, PTCacheEdit *edit)
452 psys_free_path_cache_buffers(edit->pathcache, &edit->pathcachebufs);
453 edit->pathcache= NULL;
457 psys_free_path_cache_buffers(psys->pathcache, &psys->pathcachebufs);
458 psys->pathcache= NULL;
461 free_child_path_cache(psys);
464 void psys_free_children(ParticleSystem *psys)
467 MEM_freeN(psys->child);
472 free_child_path_cache(psys);
474 void psys_free_particles(ParticleSystem *psys)
478 if(psys->particles) {
479 if(psys->part->type==PART_HAIR) {
486 if(psys->particles->keys)
487 MEM_freeN(psys->particles->keys);
489 if(psys->particles->boid)
490 MEM_freeN(psys->particles->boid);
492 MEM_freeN(psys->particles);
493 psys->particles= NULL;
497 void psys_free_pdd(ParticleSystem *psys)
501 MEM_freeN(psys->pdd->cdata);
502 psys->pdd->cdata = NULL;
505 MEM_freeN(psys->pdd->vdata);
506 psys->pdd->vdata = NULL;
509 MEM_freeN(psys->pdd->ndata);
510 psys->pdd->ndata = NULL;
512 if(psys->pdd->vedata)
513 MEM_freeN(psys->pdd->vedata);
514 psys->pdd->vedata = NULL;
516 psys->pdd->totpoint = 0;
517 psys->pdd->tot_vec_size = 0;
520 /* free everything */
521 void psys_free(Object *ob, ParticleSystem * psys)
525 ParticleSystem * tpsys;
527 psys_free_path_cache(psys, NULL);
529 free_hair(ob, psys, 1);
531 psys_free_particles(psys);
533 if(psys->edit && psys->free_edit)
534 psys->free_edit(psys->edit);
537 MEM_freeN(psys->child);
542 // check if we are last non-visible particle system
543 for(tpsys=ob->particlesystem.first; tpsys; tpsys=tpsys->next){
546 if(ELEM(tpsys->part->ren_as,PART_DRAW_OB,PART_DRAW_GR))
553 // clear do-not-draw-flag
555 ob->transflag &= ~OB_DUPLIPARTS;
562 BKE_ptcache_free_list(&psys->ptcaches);
563 psys->pointcache = NULL;
565 BLI_freelistN(&psys->targets);
567 BLI_bvhtree_free(psys->bvhtree);
568 BLI_kdtree_free(psys->tree);
570 if(psys->fluid_springs)
571 MEM_freeN(psys->fluid_springs);
573 pdEndEffectors(&psys->effectors);
576 MEM_freeN(psys->frand);
580 MEM_freeN(psys->pdd);
587 /************************************************/
589 /************************************************/
590 /* these functions move away particle data and bring it back after
591 * rendering, to make different render settings possible without
592 * removing the previous data. this should be solved properly once */
594 typedef struct ParticleRenderElem {
595 int curchild, totchild, reduce;
596 float lambda, t, scalemin, scalemax;
597 } ParticleRenderElem;
599 typedef struct ParticleRenderData {
600 ChildParticle *child;
601 ParticleCacheKey **pathcache;
602 ParticleCacheKey **childcache;
603 ListBase pathcachebufs, childcachebufs;
604 int totchild, totcached, totchildcache;
606 int totdmvert, totdmedge, totdmface;
609 float viewmat[4][4], winmat[4][4];
614 ParticleRenderElem *elems;
616 } ParticleRenderData;
618 static float psys_render_viewport_falloff(double rate, float dist, float width)
620 return pow(rate, dist/width);
623 static float psys_render_projected_area(ParticleSystem *psys, const float center[3], float area, double vprate, float *viewport)
625 ParticleRenderData *data= psys->renderdata;
626 float co[4], view[3], ortho1[3], ortho2[3], w, dx, dy, radius;
628 /* transform to view space */
629 copy_v3_v3(co, center);
631 mul_m4_v4(data->viewmat, co);
633 /* compute two vectors orthogonal to view vector */
634 normalize_v3_v3(view, co);
635 ortho_basis_v3v3_v3( ortho1, ortho2,view);
637 /* compute on screen minification */
638 w= co[2]*data->winmat[2][3] + data->winmat[3][3];
639 dx= data->winx*ortho2[0]*data->winmat[0][0];
640 dy= data->winy*ortho2[1]*data->winmat[1][1];
641 w= sqrtf(dx*dx + dy*dy)/w;
643 /* w squared because we are working with area */
646 /* viewport of the screen test */
648 /* project point on screen */
649 mul_m4_v4(data->winmat, co);
651 co[0]= 0.5f*data->winx*(1.0f + co[0]/co[3]);
652 co[1]= 0.5f*data->winy*(1.0f + co[1]/co[3]);
655 /* screen space radius */
656 radius= sqrt(area/(float)M_PI);
658 /* make smaller using fallof once over screen edge */
661 if(co[0]+radius < 0.0f)
662 *viewport *= psys_render_viewport_falloff(vprate, -(co[0]+radius), data->winx);
663 else if(co[0]-radius > data->winx)
664 *viewport *= psys_render_viewport_falloff(vprate, (co[0]-radius) - data->winx, data->winx);
666 if(co[1]+radius < 0.0f)
667 *viewport *= psys_render_viewport_falloff(vprate, -(co[1]+radius), data->winy);
668 else if(co[1]-radius > data->winy)
669 *viewport *= psys_render_viewport_falloff(vprate, (co[1]-radius) - data->winy, data->winy);
674 void psys_render_set(Object *ob, ParticleSystem *psys, float viewmat[][4], float winmat[][4], int winx, int winy, int timeoffset)
676 ParticleRenderData*data;
677 ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
684 data= MEM_callocN(sizeof(ParticleRenderData), "ParticleRenderData");
686 data->child= psys->child;
687 data->totchild= psys->totchild;
688 data->pathcache= psys->pathcache;
689 data->pathcachebufs.first = psys->pathcachebufs.first;
690 data->pathcachebufs.last = psys->pathcachebufs.last;
691 data->totcached= psys->totcached;
692 data->childcache= psys->childcache;
693 data->childcachebufs.first = psys->childcachebufs.first;
694 data->childcachebufs.last = psys->childcachebufs.last;
695 data->totchildcache= psys->totchildcache;
698 data->dm= CDDM_copy(psmd->dm);
699 data->totdmvert= psmd->totdmvert;
700 data->totdmedge= psmd->totdmedge;
701 data->totdmface= psmd->totdmface;
704 psys->pathcache= NULL;
705 psys->childcache= NULL;
706 psys->totchild= psys->totcached= psys->totchildcache= 0;
707 psys->pathcachebufs.first = psys->pathcachebufs.last = NULL;
708 psys->childcachebufs.first = psys->childcachebufs.last = NULL;
710 copy_m4_m4(data->winmat, winmat);
711 mul_m4_m4m4(data->viewmat, ob->obmat, viewmat);
712 mul_m4_m4m4(data->mat, data->viewmat, winmat);
716 data->timeoffset= timeoffset;
718 psys->renderdata= data;
720 /* Hair can and has to be recalculated if everything isn't displayed. */
721 if(psys->part->disp != 100 && psys->part->type == PART_HAIR)
722 psys->recalc |= PSYS_RECALC_RESET;
725 void psys_render_restore(Object *ob, ParticleSystem *psys)
727 ParticleRenderData*data;
728 ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
730 data= psys->renderdata;
735 MEM_freeN(data->elems);
738 psmd->dm->needsFree= 1;
739 psmd->dm->release(psmd->dm);
742 psys_free_path_cache(psys, NULL);
745 MEM_freeN(psys->child);
750 psys->child= data->child;
751 psys->totchild= data->totchild;
752 psys->pathcache= data->pathcache;
753 psys->pathcachebufs.first = data->pathcachebufs.first;
754 psys->pathcachebufs.last = data->pathcachebufs.last;
755 psys->totcached= data->totcached;
756 psys->childcache= data->childcache;
757 psys->childcachebufs.first = data->childcachebufs.first;
758 psys->childcachebufs.last = data->childcachebufs.last;
759 psys->totchildcache= data->totchildcache;
762 psmd->totdmvert= data->totdmvert;
763 psmd->totdmedge= data->totdmedge;
764 psmd->totdmface= data->totdmface;
765 psmd->flag &= ~eParticleSystemFlag_psys_updated;
768 psys_calc_dmcache(ob, psmd->dm, psys);
771 psys->renderdata= NULL;
774 int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot)
776 DerivedMesh *dm= ctx->dm;
777 Mesh *me= (Mesh*)(ctx->sim.ob->data);
780 ParticleRenderData *data;
781 ParticleRenderElem *elems, *elem;
782 ParticleSettings *part= ctx->sim.psys->part;
783 float *facearea, (*facecenter)[3], size[3], fac, powrate, scaleclamp;
784 float co1[3], co2[3], co3[3], co4[3], lambda, arearatio, t, area, viewport;
786 int *origindex, *facetotvert;
787 int a, b, totorigface, totface, newtot, skipped;
789 if(part->ren_as!=PART_DRAW_PATH || !(part->draw & PART_DRAW_REN_STRAND))
791 if(!ctx->sim.psys->renderdata)
794 data= ctx->sim.psys->renderdata;
797 if(!(part->simplify_flag & PART_SIMPLIFY_ENABLE))
800 mvert= dm->getVertArray(dm);
801 mface= dm->getFaceArray(dm);
802 origindex= dm->getFaceDataArray(dm, CD_ORIGINDEX);
803 totface= dm->getNumFaces(dm);
804 totorigface= me->totface;
806 if(totface == 0 || totorigface == 0)
809 facearea= MEM_callocN(sizeof(float)*totorigface, "SimplifyFaceArea");
810 facecenter= MEM_callocN(sizeof(float[3])*totorigface, "SimplifyFaceCenter");
811 facetotvert= MEM_callocN(sizeof(int)*totorigface, "SimplifyFaceArea");
812 elems= MEM_callocN(sizeof(ParticleRenderElem)*totorigface, "SimplifyFaceElem");
815 MEM_freeN(data->elems);
819 data->origindex= origindex;
821 /* compute number of children per original face */
822 for(a=0; a<tot; a++) {
823 b= (origindex)? origindex[ctx->index[a]]: ctx->index[a];
828 /* compute areas and centers of original faces */
829 for(mf=mface, a=0; a<totface; a++, mf++) {
830 b= (origindex)? origindex[a]: a;
833 copy_v3_v3(co1, mvert[mf->v1].co);
834 copy_v3_v3(co2, mvert[mf->v2].co);
835 copy_v3_v3(co3, mvert[mf->v3].co);
837 add_v3_v3(facecenter[b], co1);
838 add_v3_v3(facecenter[b], co2);
839 add_v3_v3(facecenter[b], co3);
842 copy_v3_v3(co4, mvert[mf->v4].co);
843 add_v3_v3(facecenter[b], co4);
844 facearea[b] += area_quad_v3(co1, co2, co3, co4);
848 facearea[b] += area_tri_v3(co1, co2, co3);
854 for(a=0; a<totorigface; a++)
855 if(facetotvert[a] > 0)
856 mul_v3_fl(facecenter[a], 1.0f/facetotvert[a]);
858 /* for conversion from BU area / pixel area to reference screen size */
859 mesh_get_texspace(me, 0, 0, size);
860 fac= ((size[0] + size[1] + size[2])/3.0f)/part->simplify_refsize;
863 powrate= log(0.5f)/log(part->simplify_rate*0.5f);
864 if(part->simplify_flag & PART_SIMPLIFY_VIEWPORT)
865 vprate= pow(1.0f - part->simplify_viewport, 5.0);
869 /* set simplification parameters per original face */
870 for(a=0, elem=elems; a<totorigface; a++, elem++) {
871 area = psys_render_projected_area(ctx->sim.psys, facecenter[a], facearea[a], vprate, &viewport);
872 arearatio= fac*area/facearea[a];
874 if((arearatio < 1.0f || viewport < 1.0f) && elem->totchild) {
875 /* lambda is percentage of elements to keep */
876 lambda= (arearatio < 1.0f)? powf(arearatio, powrate): 1.0f;
879 lambda= MAX2(lambda, 1.0f/elem->totchild);
881 /* compute transition region */
882 t= part->simplify_transition;
883 elem->t= (lambda-t < 0.0f)? lambda: (lambda+t > 1.0f)? 1.0f-lambda: t;
886 /* scale at end and beginning of the transition region */
887 elem->scalemax= (lambda+t < 1.0f)? 1.0f/lambda: 1.0f/(1.0f - elem->t*elem->t/t);
888 elem->scalemin= (lambda+t < 1.0f)? 0.0f: elem->scalemax*(1.0f-elem->t/t);
890 elem->scalemin= sqrt(elem->scalemin);
891 elem->scalemax= sqrt(elem->scalemax);
894 scaleclamp= MIN2(elem->totchild, 10.0f);
895 elem->scalemin= MIN2(scaleclamp, elem->scalemin);
896 elem->scalemax= MIN2(scaleclamp, elem->scalemax);
898 /* extend lambda to include transition */
899 lambda= lambda + elem->t;
906 elem->scalemax= 1.0f; //sqrt(lambda);
907 elem->scalemin= 1.0f; //sqrt(lambda);
911 elem->lambda= lambda;
912 elem->scalemin= sqrt(elem->scalemin);
913 elem->scalemax= sqrt(elem->scalemax);
918 MEM_freeN(facecenter);
919 MEM_freeN(facetotvert);
921 /* move indices and set random number skipping */
922 ctx->skip= MEM_callocN(sizeof(int)*tot, "SimplificationSkip");
925 for(a=0, newtot=0; a<tot; a++) {
926 b= (origindex)? origindex[ctx->index[a]]: ctx->index[a];
928 if(elems[b].curchild++ < ceil(elems[b].lambda*elems[b].totchild)) {
929 ctx->index[newtot]= ctx->index[a];
930 ctx->skip[newtot]= skipped;
939 for(a=0, elem=elems; a<totorigface; a++, elem++)
945 int psys_render_simplify_params(ParticleSystem *psys, ChildParticle *cpa, float *params)
947 ParticleRenderData *data;
948 ParticleRenderElem *elem;
949 float x, w, scale, alpha, lambda, t, scalemin, scalemax;
952 if(!(psys->renderdata && (psys->part->simplify_flag & PART_SIMPLIFY_ENABLE)))
955 data= psys->renderdata;
956 if(!data->dosimplify)
959 b= (data->origindex)? data->origindex[cpa->num]: cpa->num;
963 elem= &data->elems[b];
965 lambda= elem->lambda;
967 scalemin= elem->scalemin;
968 scalemax= elem->scalemax;
975 x= (elem->curchild+0.5f)/elem->totchild;
980 else if(x >= lambda+t) {
985 w= (lambda+t - x)/(2.0f*t);
986 scale= scalemin + (scalemax - scalemin)*w;
999 /************************************************/
1001 /************************************************/
1002 static float interpolate_particle_value(float v1, float v2, float v3, float v4, const float w[4], int four)
1006 value= w[0]*v1 + w[1]*v2 + w[2]*v3;
1010 CLAMP(value, 0.f, 1.f);
1015 void psys_interpolate_particle(short type, ParticleKey keys[4], float dt, ParticleKey *result, int velocity)
1020 interp_cubic_v3( result->co, result->vel,keys[1].co, keys[1].vel, keys[2].co, keys[2].vel, dt);
1023 key_curve_position_weights(dt, t, type);
1025 interp_v3_v3v3v3v3(result->co, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
1031 key_curve_position_weights(dt-0.001f, t, type);
1032 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
1033 sub_v3_v3v3(result->vel, result->co, temp);
1036 key_curve_position_weights(dt+0.001f, t, type);
1037 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
1038 sub_v3_v3v3(result->vel, temp, result->co);
1046 typedef struct ParticleInterpolationData {
1053 ParticleKey *kkey[2];
1058 PTCacheEditPoint *epoint;
1059 PTCacheEditKey *ekey[2];
1061 float birthtime, dietime;
1063 } ParticleInterpolationData;
1064 /* Assumes pointcache->mem_cache exists, so for disk cached particles call psys_make_temp_pointcache() before use */
1065 /* It uses ParticleInterpolationData->pm to store the current memory cache frame so it's thread safe. */
1066 static void get_pointcache_keys_for_time(Object *UNUSED(ob), PointCache *cache, PTCacheMem **cur, int index, float t, ParticleKey *key1, ParticleKey *key2)
1068 static PTCacheMem *pm = NULL;
1071 if(index < 0) { /* initialize */
1072 *cur = cache->mem_cache.first;
1075 *cur = (*cur)->next;
1079 while(*cur && (*cur)->next && (float)(*cur)->frame < t)
1080 *cur = (*cur)->next;
1084 index2 = BKE_ptcache_mem_index_find(pm, index);
1085 index1 = BKE_ptcache_mem_index_find(pm->prev, index);
1087 BKE_ptcache_make_particle_key(key2, index2, pm->data, (float)pm->frame);
1089 copy_particle_key(key1, key2, 1);
1091 BKE_ptcache_make_particle_key(key1, index1, pm->prev->data, (float)pm->prev->frame);
1093 else if(cache->mem_cache.first) {
1094 pm = cache->mem_cache.first;
1095 index2 = BKE_ptcache_mem_index_find(pm, index);
1096 BKE_ptcache_make_particle_key(key2, index2, pm->data, (float)pm->frame);
1097 copy_particle_key(key1, key2, 1);
1101 static int get_pointcache_times_for_particle(PointCache *cache, int index, float *start, float *end)
1106 for(pm=cache->mem_cache.first; pm; pm=pm->next) {
1107 if(BKE_ptcache_mem_index_find(pm, index) >= 0) {
1114 for(pm=cache->mem_cache.last; pm; pm=pm->prev) {
1115 if(BKE_ptcache_mem_index_find(pm, index) >= 0) {
1125 float psys_get_dietime_from_cache(PointCache *cache, int index) {
1127 int dietime = 10000000; /* some max value so that we can default to pa->time+lifetime */
1129 for(pm=cache->mem_cache.last; pm; pm=pm->prev) {
1130 if(BKE_ptcache_mem_index_find(pm, index) >= 0)
1131 return (float)pm->frame;
1134 return (float)dietime;
1137 static void init_particle_interpolation(Object *ob, ParticleSystem *psys, ParticleData *pa, ParticleInterpolationData *pind)
1141 PTCacheEditPoint *point = pind->epoint;
1143 pind->ekey[0] = point->keys;
1144 pind->ekey[1] = point->totkey > 1 ? point->keys + 1 : NULL;
1146 pind->birthtime = *(point->keys->time);
1147 pind->dietime = *((point->keys + point->totkey - 1)->time);
1149 else if(pind->keyed) {
1150 ParticleKey *key = pa->keys;
1151 pind->kkey[0] = key;
1152 pind->kkey[1] = pa->totkey > 1 ? key + 1 : NULL;
1154 pind->birthtime = key->time;
1155 pind->dietime = (key + pa->totkey - 1)->time;
1157 else if(pind->cache) {
1158 float start=0.0f, end=0.0f;
1159 get_pointcache_keys_for_time(ob, pind->cache, &pind->pm, -1, 0.0f, NULL, NULL);
1160 pind->birthtime = pa ? pa->time : pind->cache->startframe;
1161 pind->dietime = pa ? pa->dietime : pind->cache->endframe;
1163 if(get_pointcache_times_for_particle(pind->cache, pa - psys->particles, &start, &end)) {
1164 pind->birthtime = MAX2(pind->birthtime, start);
1165 pind->dietime = MIN2(pind->dietime, end);
1169 HairKey *key = pa->hair;
1170 pind->hkey[0] = key;
1171 pind->hkey[1] = key + 1;
1173 pind->birthtime = key->time;
1174 pind->dietime = (key + pa->totkey - 1)->time;
1177 pind->mvert[0] = CDDM_get_vert(pind->dm, pa->hair_index);
1178 pind->mvert[1] = pind->mvert[0] + 1;
1182 static void edit_to_particle(ParticleKey *key, PTCacheEditKey *ekey)
1184 copy_v3_v3(key->co, ekey->co);
1186 copy_v3_v3(key->vel, ekey->vel);
1188 key->time = *(ekey->time);
1190 static void hair_to_particle(ParticleKey *key, HairKey *hkey)
1192 copy_v3_v3(key->co, hkey->co);
1193 key->time = hkey->time;
1196 static void mvert_to_particle(ParticleKey *key, MVert *mvert, HairKey *hkey)
1198 copy_v3_v3(key->co, mvert->co);
1199 key->time = hkey->time;
1202 static void do_particle_interpolation(ParticleSystem *psys, int p, ParticleData *pa, float t, ParticleInterpolationData *pind, ParticleKey *result)
1204 PTCacheEditPoint *point = pind->epoint;
1205 ParticleKey keys[4];
1206 int point_vel = (point && point->keys->vel);
1207 float real_t, dfra, keytime, invdt = 1.f;
1209 /* billboards wont fill in all of these, so start cleared */
1210 memset(keys, 0, sizeof(keys));
1212 /* interpret timing and find keys */
1214 if(result->time < 0.0f)
1215 real_t = -result->time;
1217 real_t = *(pind->ekey[0]->time) + t * (*(pind->ekey[0][point->totkey-1].time) - *(pind->ekey[0]->time));
1219 while(*(pind->ekey[1]->time) < real_t)
1222 pind->ekey[0] = pind->ekey[1] - 1;
1224 else if(pind->keyed) {
1225 /* we have only one key, so let's use that */
1226 if(pind->kkey[1]==NULL) {
1227 copy_particle_key(result, pind->kkey[0], 1);
1231 if(result->time < 0.0f)
1232 real_t = -result->time;
1234 real_t = pind->kkey[0]->time + t * (pind->kkey[0][pa->totkey-1].time - pind->kkey[0]->time);
1236 if(psys->part->phystype==PART_PHYS_KEYED && psys->flag & PSYS_KEYED_TIMING) {
1237 ParticleTarget *pt = psys->targets.first;
1241 while(pt && pa->time + pt->time < real_t)
1247 if(pa->time + pt->time + pt->duration > real_t)
1248 real_t = pa->time + pt->time;
1251 real_t = pa->time + ((ParticleTarget*)psys->targets.last)->time;
1254 CLAMP(real_t, pa->time, pa->dietime);
1256 while(pind->kkey[1]->time < real_t)
1259 pind->kkey[0] = pind->kkey[1] - 1;
1261 else if(pind->cache) {
1262 if(result->time < 0.0f) /* flag for time in frames */
1263 real_t = -result->time;
1265 real_t = pa->time + t * (pa->dietime - pa->time);
1268 if(result->time < 0.0f)
1269 real_t = -result->time;
1271 real_t = pind->hkey[0]->time + t * (pind->hkey[0][pa->totkey-1].time - pind->hkey[0]->time);
1273 while(pind->hkey[1]->time < real_t) {
1278 pind->hkey[0] = pind->hkey[1] - 1;
1281 /* set actual interpolation keys */
1283 edit_to_particle(keys + 1, pind->ekey[0]);
1284 edit_to_particle(keys + 2, pind->ekey[1]);
1287 pind->mvert[0] = pind->mvert[1] - 1;
1288 mvert_to_particle(keys + 1, pind->mvert[0], pind->hkey[0]);
1289 mvert_to_particle(keys + 2, pind->mvert[1], pind->hkey[1]);
1291 else if(pind->keyed) {
1292 memcpy(keys + 1, pind->kkey[0], sizeof(ParticleKey));
1293 memcpy(keys + 2, pind->kkey[1], sizeof(ParticleKey));
1295 else if(pind->cache) {
1296 get_pointcache_keys_for_time(NULL, pind->cache, &pind->pm, p, real_t, keys+1, keys+2);
1299 hair_to_particle(keys + 1, pind->hkey[0]);
1300 hair_to_particle(keys + 2, pind->hkey[1]);
1303 /* set secondary interpolation keys for hair */
1304 if(!pind->keyed && !pind->cache && !point_vel) {
1306 if(pind->ekey[0] != point->keys)
1307 edit_to_particle(keys, pind->ekey[0] - 1);
1309 edit_to_particle(keys, pind->ekey[0]);
1312 if(pind->hkey[0] != pa->hair)
1313 mvert_to_particle(keys, pind->mvert[0] - 1, pind->hkey[0] - 1);
1315 mvert_to_particle(keys, pind->mvert[0], pind->hkey[0]);
1318 if(pind->hkey[0] != pa->hair)
1319 hair_to_particle(keys, pind->hkey[0] - 1);
1321 hair_to_particle(keys, pind->hkey[0]);
1325 if(pind->ekey[1] != point->keys + point->totkey - 1)
1326 edit_to_particle(keys + 3, pind->ekey[1] + 1);
1328 edit_to_particle(keys + 3, pind->ekey[1]);
1331 if(pind->hkey[1] != pa->hair + pa->totkey - 1)
1332 mvert_to_particle(keys + 3, pind->mvert[1] + 1, pind->hkey[1] + 1);
1334 mvert_to_particle(keys + 3, pind->mvert[1], pind->hkey[1]);
1337 if(pind->hkey[1] != pa->hair + pa->totkey - 1)
1338 hair_to_particle(keys + 3, pind->hkey[1] + 1);
1340 hair_to_particle(keys + 3, pind->hkey[1]);
1344 dfra = keys[2].time - keys[1].time;
1345 keytime = (real_t - keys[1].time) / dfra;
1347 /* convert velocity to timestep size */
1348 if(pind->keyed || pind->cache || point_vel){
1349 invdt = dfra * 0.04f * (psys ? psys->part->timetweak : 1.f);
1350 mul_v3_fl(keys[1].vel, invdt);
1351 mul_v3_fl(keys[2].vel, invdt);
1352 interp_qt_qtqt(result->rot,keys[1].rot,keys[2].rot,keytime);
1355 /* 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)*/
1356 psys_interpolate_particle((pind->keyed || pind->cache || point_vel) ? -1 /* signal for cubic interpolation */
1357 : (pind->bspline ? KEY_BSPLINE : KEY_CARDINAL)
1358 ,keys, keytime, result, 1);
1360 /* the velocity needs to be converted back from cubic interpolation */
1361 if(pind->keyed || pind->cache || point_vel)
1362 mul_v3_fl(result->vel, 1.f/invdt);
1364 /************************************************/
1365 /* Particles on a dm */
1366 /************************************************/
1367 /* interpolate a location on a face based on face coordinates */
1368 void psys_interpolate_face(MVert *mvert, MFace *mface, MTFace *tface, float (*orcodata)[3], float *w, float *vec, float *nor, float *utan, float *vtan, float *orco,float *ornor){
1369 float *v1=0, *v2=0, *v3=0, *v4=0;
1370 float e1[3],e2[3],s1,s2,t1,t2;
1371 float *uv1, *uv2, *uv3, *uv4;
1372 float n1[3], n2[3], n3[3], n4[3];
1374 float *o1, *o2, *o3, *o4;
1376 v1= mvert[mface->v1].co;
1377 v2= mvert[mface->v2].co;
1378 v3= mvert[mface->v3].co;
1380 normal_short_to_float_v3(n1, mvert[mface->v1].no);
1381 normal_short_to_float_v3(n2, mvert[mface->v2].no);
1382 normal_short_to_float_v3(n3, mvert[mface->v3].no);
1385 v4= mvert[mface->v4].co;
1386 normal_short_to_float_v3(n4, mvert[mface->v4].no);
1388 interp_v3_v3v3v3v3(vec, v1, v2, v3, v4, w);
1391 if(mface->flag & ME_SMOOTH)
1392 interp_v3_v3v3v3v3(nor, n1, n2, n3, n4, w);
1394 normal_quad_v3(nor,v1,v2,v3,v4);
1398 interp_v3_v3v3v3(vec, v1, v2, v3, w);
1401 if(mface->flag & ME_SMOOTH)
1402 interp_v3_v3v3v3(nor, n1, n2, n3, w);
1404 normal_tri_v3(nor,v1,v2,v3);
1408 /* calculate tangent vectors */
1417 uv1= tuv[0]; uv2= tuv[1]; uv3= tuv[2]; uv4= tuv[3];
1418 map_to_sphere( uv1, uv1+1,v1[0], v1[1], v1[2]);
1419 map_to_sphere( uv2, uv2+1,v2[0], v2[1], v2[2]);
1420 map_to_sphere( uv3, uv3+1,v3[0], v3[1], v3[2]);
1422 map_to_sphere( uv4, uv4+1,v4[0], v4[1], v4[2]);
1426 s1= uv3[0] - uv1[0];
1427 s2= uv4[0] - uv1[0];
1429 t1= uv3[1] - uv1[1];
1430 t2= uv4[1] - uv1[1];
1432 sub_v3_v3v3(e1, v3, v1);
1433 sub_v3_v3v3(e2, v4, v1);
1436 s1= uv2[0] - uv1[0];
1437 s2= uv3[0] - uv1[0];
1439 t1= uv2[1] - uv1[1];
1440 t2= uv3[1] - uv1[1];
1442 sub_v3_v3v3(e1, v2, v1);
1443 sub_v3_v3v3(e2, v3, v1);
1446 vtan[0] = (s1*e2[0] - s2*e1[0]);
1447 vtan[1] = (s1*e2[1] - s2*e1[1]);
1448 vtan[2] = (s1*e2[2] - s2*e1[2]);
1450 utan[0] = (t1*e2[0] - t2*e1[0]);
1451 utan[1] = (t1*e2[1] - t2*e1[1]);
1452 utan[2] = (t1*e2[2] - t2*e1[2]);
1457 o1= orcodata[mface->v1];
1458 o2= orcodata[mface->v2];
1459 o3= orcodata[mface->v3];
1462 o4= orcodata[mface->v4];
1464 interp_v3_v3v3v3v3(orco, o1, o2, o3, o4, w);
1467 normal_quad_v3( ornor,o1, o2, o3, o4);
1470 interp_v3_v3v3v3(orco, o1, o2, o3, w);
1473 normal_tri_v3( ornor,o1, o2, o3);
1477 copy_v3_v3(orco, vec);
1479 copy_v3_v3(ornor, nor);
1483 void psys_interpolate_uvs(const MTFace *tface, int quad, const float w[4], float uvco[2])
1485 float v10= tface->uv[0][0];
1486 float v11= tface->uv[0][1];
1487 float v20= tface->uv[1][0];
1488 float v21= tface->uv[1][1];
1489 float v30= tface->uv[2][0];
1490 float v31= tface->uv[2][1];
1494 v40= tface->uv[3][0];
1495 v41= tface->uv[3][1];
1497 uvco[0]= w[0]*v10 + w[1]*v20 + w[2]*v30 + w[3]*v40;
1498 uvco[1]= w[0]*v11 + w[1]*v21 + w[2]*v31 + w[3]*v41;
1501 uvco[0]= w[0]*v10 + w[1]*v20 + w[2]*v30;
1502 uvco[1]= w[0]*v11 + w[1]*v21 + w[2]*v31;
1506 void psys_interpolate_mcol(const MCol *mcol, int quad, const float w[4], MCol *mc)
1508 char *cp, *cp1, *cp2, *cp3, *cp4;
1511 cp1= (char *)&mcol[0];
1512 cp2= (char *)&mcol[1];
1513 cp3= (char *)&mcol[2];
1516 cp4= (char *)&mcol[3];
1518 cp[0]= (int)(w[0]*cp1[0] + w[1]*cp2[0] + w[2]*cp3[0] + w[3]*cp4[0]);
1519 cp[1]= (int)(w[0]*cp1[1] + w[1]*cp2[1] + w[2]*cp3[1] + w[3]*cp4[1]);
1520 cp[2]= (int)(w[0]*cp1[2] + w[1]*cp2[2] + w[2]*cp3[2] + w[3]*cp4[2]);
1521 cp[3]= (int)(w[0]*cp1[3] + w[1]*cp2[3] + w[2]*cp3[3] + w[3]*cp4[3]);
1524 cp[0]= (int)(w[0]*cp1[0] + w[1]*cp2[0] + w[2]*cp3[0]);
1525 cp[1]= (int)(w[0]*cp1[1] + w[1]*cp2[1] + w[2]*cp3[1]);
1526 cp[2]= (int)(w[0]*cp1[2] + w[1]*cp2[2] + w[2]*cp3[2]);
1527 cp[3]= (int)(w[0]*cp1[3] + w[1]*cp2[3] + w[2]*cp3[3]);
1531 static float psys_interpolate_value_from_verts(DerivedMesh *dm, short from, int index, const float fw[4], const float *values)
1533 if(values==0 || index==-1)
1537 case PART_FROM_VERT:
1538 return values[index];
1539 case PART_FROM_FACE:
1540 case PART_FROM_VOLUME:
1542 MFace *mf=dm->getFaceData(dm,index,CD_MFACE);
1543 return interpolate_particle_value(values[mf->v1],values[mf->v2],values[mf->v3],values[mf->v4],fw,mf->v4);
1550 /* conversion of pa->fw to origspace layer coordinates */
1551 static void psys_w_to_origspace(const float w[4], float uv[2])
1557 /* conversion of pa->fw to weights in face from origspace */
1558 static void psys_origspace_to_w(OrigSpaceFace *osface, int quad, const float w[4], float neww[4])
1560 float v[4][3], co[3];
1562 v[0][0]= osface->uv[0][0]; v[0][1]= osface->uv[0][1]; v[0][2]= 0.0f;
1563 v[1][0]= osface->uv[1][0]; v[1][1]= osface->uv[1][1]; v[1][2]= 0.0f;
1564 v[2][0]= osface->uv[2][0]; v[2][1]= osface->uv[2][1]; v[2][2]= 0.0f;
1566 psys_w_to_origspace(w, co);
1570 v[3][0]= osface->uv[3][0]; v[3][1]= osface->uv[3][1]; v[3][2]= 0.0f;
1571 interp_weights_poly_v3(neww, v, 4, co);
1574 interp_weights_poly_v3(neww, v, 3, co);
1579 /* find the derived mesh face for a particle, set the mf passed. this is slow
1580 * and can be optimized but only for many lookups. returns the face index. */
1581 int psys_particle_dm_face_lookup(Object *ob, DerivedMesh *dm, int index, const float fw[4], struct LinkNode *node)
1583 Mesh *me= (Mesh*)ob->data;
1585 OrigSpaceFace *osface;
1587 int quad, findex, totface;
1588 float uv[2], (*faceuv)[2];
1590 mface = dm->getFaceDataArray(dm, CD_MFACE);
1591 origindex = dm->getFaceDataArray(dm, CD_ORIGINDEX);
1592 osface = dm->getFaceDataArray(dm, CD_ORIGSPACE);
1594 totface = dm->getNumFaces(dm);
1596 if(osface==NULL || origindex==NULL) {
1597 /* Assume we dont need osface data */
1598 if (index <totface) {
1599 //printf("\tNO CD_ORIGSPACE, assuming not needed\n");
1602 printf("\tNO CD_ORIGSPACE, error out of range\n");
1603 return DMCACHE_NOTFOUND;
1606 else if(index >= me->totface)
1607 return DMCACHE_NOTFOUND; /* index not in the original mesh */
1609 psys_w_to_origspace(fw, uv);
1611 if(node) { /* we have a linked list of faces that we use, faster! */
1612 for(;node; node=node->next) {
1613 findex= GET_INT_FROM_POINTER(node->link);
1614 faceuv= osface[findex].uv;
1615 quad= mface[findex].v4;
1617 /* check that this intersects - Its possible this misses :/ -
1618 * could also check its not between */
1620 if(isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3]))
1623 else if(isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2]))
1627 else { /* if we have no node, try every face */
1628 for(findex=0; findex<totface; findex++) {
1629 if(origindex[findex] == index) {
1630 faceuv= osface[findex].uv;
1631 quad= mface[findex].v4;
1633 /* check that this intersects - Its possible this misses :/ -
1634 * could also check its not between */
1636 if(isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3]))
1639 else if(isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2]))
1645 return DMCACHE_NOTFOUND;
1648 static int psys_map_index_on_dm(DerivedMesh *dm, int from, int index, int index_dmcache, const float fw[4], float UNUSED(foffset), int *mapindex, float mapfw[4])
1653 if (dm->deformedOnly || index_dmcache == DMCACHE_ISCHILD) {
1654 /* for meshes that are either only defined or for child particles, the
1655 * index and fw do not require any mapping, so we can directly use it */
1656 if(from == PART_FROM_VERT) {
1657 if(index >= dm->getNumVerts(dm))
1662 else { /* FROM_FACE/FROM_VOLUME */
1663 if(index >= dm->getNumFaces(dm))
1667 copy_v4_v4(mapfw, fw);
1670 /* for other meshes that have been modified, we try to map the particle
1671 * to their new location, which means a different index, and for faces
1672 * also a new face interpolation weights */
1673 if(from == PART_FROM_VERT) {
1674 if (index_dmcache == DMCACHE_NOTFOUND || index_dmcache > dm->getNumVerts(dm))
1677 *mapindex = index_dmcache;
1679 else { /* FROM_FACE/FROM_VOLUME */
1680 /* find a face on the derived mesh that uses this face */
1682 OrigSpaceFace *osface;
1687 if(i== DMCACHE_NOTFOUND || i >= dm->getNumFaces(dm))
1692 /* modify the original weights to become
1693 * weights for the derived mesh face */
1694 osface= dm->getFaceDataArray(dm, CD_ORIGSPACE);
1695 mface= dm->getFaceData(dm, i, CD_MFACE);
1698 mapfw[0]= mapfw[1]= mapfw[2]= mapfw[3]= 0.0f;
1700 psys_origspace_to_w(&osface[i], mface->v4, fw, mapfw);
1707 /* interprets particle data to get a point on a mesh in object space */
1708 void psys_particle_on_dm(DerivedMesh *dm, int from, int index, int index_dmcache, const float fw[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3], float ornor[3])
1710 float tmpnor[3], mapfw[4];
1711 float (*orcodata)[3];
1714 if(!psys_map_index_on_dm(dm, from, index, index_dmcache, fw, foffset, &mapindex, mapfw)) {
1715 if(vec) { vec[0]=vec[1]=vec[2]=0.0; }
1716 if(nor) { nor[0]=nor[1]=0.0; nor[2]=1.0; }
1717 if(orco) { orco[0]=orco[1]=orco[2]=0.0; }
1718 if(ornor) { ornor[0]=ornor[1]=0.0; ornor[2]=1.0; }
1719 if(utan) { utan[0]=utan[1]=utan[2]=0.0; }
1720 if(vtan) { vtan[0]=vtan[1]=vtan[2]=0.0; }
1725 orcodata= dm->getVertDataArray(dm, CD_ORCO);
1727 if(from == PART_FROM_VERT) {
1728 dm->getVertCo(dm,mapindex,vec);
1731 dm->getVertNo(dm,mapindex,nor);
1736 copy_v3_v3(orco, orcodata[mapindex]);
1739 dm->getVertNo(dm,mapindex,nor);
1744 utan[0]= utan[1]= utan[2]= 0.0f;
1745 vtan[0]= vtan[1]= vtan[2]= 0.0f;
1748 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
1753 mface=dm->getFaceData(dm,mapindex,CD_MFACE);
1754 mvert=dm->getVertDataArray(dm,CD_MVERT);
1755 mtface=CustomData_get_layer(&dm->faceData,CD_MTFACE);
1760 if(from==PART_FROM_VOLUME) {
1761 psys_interpolate_face(mvert,mface,mtface,orcodata,mapfw,vec,tmpnor,utan,vtan,orco,ornor);
1763 copy_v3_v3(nor,tmpnor);
1765 normalize_v3(tmpnor);
1766 mul_v3_fl(tmpnor,-foffset);
1767 add_v3_v3(vec, tmpnor);
1770 psys_interpolate_face(mvert,mface,mtface,orcodata,mapfw,vec,nor,utan,vtan,orco,ornor);
1774 float psys_particle_value_from_verts(DerivedMesh *dm, short from, ParticleData *pa, float *values)
1779 if(!psys_map_index_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, &mapindex, mapfw))
1782 return psys_interpolate_value_from_verts(dm, from, mapindex, mapfw, values);
1785 ParticleSystemModifierData *psys_get_modifier(Object *ob, ParticleSystem *psys)
1788 ParticleSystemModifierData *psmd;
1790 for(md=ob->modifiers.first; md; md=md->next){
1791 if(md->type==eModifierType_ParticleSystem){
1792 psmd= (ParticleSystemModifierData*) md;
1793 if(psmd->psys==psys){
1800 /************************************************/
1801 /* Particles on a shape */
1802 /************************************************/
1803 /* ready for future use */
1804 static void psys_particle_on_shape(int UNUSED(distr), int UNUSED(index), float *UNUSED(fuv), float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor)
1807 float zerovec[3]={0.0f,0.0f,0.0f};
1809 copy_v3_v3(vec,zerovec);
1812 copy_v3_v3(nor,zerovec);
1815 copy_v3_v3(utan,zerovec);
1818 copy_v3_v3(vtan,zerovec);
1821 copy_v3_v3(orco,zerovec);
1824 copy_v3_v3(ornor,zerovec);
1827 /************************************************/
1828 /* Particles on emitter */
1829 /************************************************/
1830 void psys_particle_on_emitter(ParticleSystemModifierData *psmd, int from, int index, int index_dmcache, float *fuv, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor){
1832 if(psmd->psys->part->distr==PART_DISTR_GRID && psmd->psys->part->from != PART_FROM_VERT){
1834 copy_v3_v3(vec,fuv);
1837 copy_v3_v3(orco, fuv);
1840 /* we cant use the num_dmcache */
1841 psys_particle_on_dm(psmd->dm,from,index,index_dmcache,fuv,foffset,vec,nor,utan,vtan,orco,ornor);
1844 psys_particle_on_shape(from,index,fuv,vec,nor,utan,vtan,orco,ornor);
1847 /************************************************/
1849 /************************************************/
1850 static float vert_weight(MDeformVert *dvert, int group)
1857 for(i= dvert->totweight; i>0; i--, dw++) {
1858 if(dw->def_nr == group) return dw->weight;
1859 if(i==1) break; /*otherwise dw will point to somewhere it shouldn't*/
1865 static void do_kink(ParticleKey *state, ParticleKey *par, float *par_rot, float time, float freq, float shape, float amplitude, float flat, short type, short axis, float obmat[][4], int smooth_start)
1867 float kink[3]={1.f,0.f,0.f}, par_vec[3], q1[4]={1.f,0.f,0.f,0.f};
1868 float t, dt=1.f, result[3];
1870 if(par == NULL || type == PART_KINK_NO)
1873 CLAMP(time, 0.f, 1.f);
1875 if(shape!=0.0f && type!=PART_KINK_BRAID) {
1877 time= (float)pow(time, 1.f+shape);
1879 time= (float)pow(time, 1.f/(1.f-shape));
1882 t = time * freq *(float)M_PI;
1886 /* smooth the beginning of kink */
1887 CLAMP(dt, 0.f, (float)M_PI);
1891 if(type != PART_KINK_RADIAL) {
1897 mul_mat3_m4_v3(obmat, kink);
1900 mul_qt_v3(par_rot, kink);
1902 /* make sure kink is normal to strand */
1903 project_v3_v3v3(temp, kink, par->vel);
1904 sub_v3_v3(kink, temp);
1908 copy_v3_v3(result, state->co);
1909 sub_v3_v3v3(par_vec, par->co, state->co);
1912 case PART_KINK_CURL:
1914 mul_v3_fl(par_vec, -1.f);
1918 project_v3_v3v3(proj, par_vec, par->vel);
1919 madd_v3_v3fl(par_vec, proj, -flat);
1921 project_v3_v3v3(proj, par_vec, kink);
1922 madd_v3_v3fl(par_vec, proj, -flat);
1925 axis_angle_to_quat(q1, kink, (float)M_PI/2.f);
1927 mul_qt_v3(q1, par_vec);
1929 madd_v3_v3fl(par_vec, kink, amplitude);
1931 /* rotate kink vector around strand tangent */
1933 axis_angle_to_quat(q1, par->vel, t);
1934 mul_qt_v3(q1, par_vec);
1937 add_v3_v3v3(result, par->co, par_vec);
1940 case PART_KINK_RADIAL:
1944 /* flatten along strand */
1945 project_v3_v3v3(proj, par_vec, par->vel);
1946 madd_v3_v3fl(result, proj, flat);
1949 madd_v3_v3fl(result, par_vec, -amplitude*(float)sin(t));
1952 case PART_KINK_WAVE:
1954 madd_v3_v3fl(result, kink, amplitude*(float)sin(t));
1958 /* flatten along wave */
1959 project_v3_v3v3(proj, par_vec, kink);
1960 madd_v3_v3fl(result, proj, flat);
1962 /* flatten along strand */
1963 project_v3_v3v3(proj, par_vec, par->vel);
1964 madd_v3_v3fl(result, proj, flat);
1968 case PART_KINK_BRAID:
1970 float y_vec[3]={0.f,1.f,0.f};
1971 float z_vec[3]={0.f,0.f,1.f};
1972 float vec_one[3], state_co[3];
1973 float inp_y, inp_z, length;
1976 mul_qt_v3(par_rot, y_vec);
1977 mul_qt_v3(par_rot, z_vec);
1980 mul_v3_fl(par_vec, -1.f);
1981 normalize_v3_v3(vec_one, par_vec);
1983 inp_y=dot_v3v3(y_vec, vec_one);
1984 inp_z=dot_v3v3(z_vec, vec_one);
1987 copy_v3_v3(state_co, y_vec);
1989 mul_v3_fl(y_vec, amplitude*(float)cos(t));
1990 mul_v3_fl(z_vec, amplitude/2.f*(float)sin(2.f*t));
1992 else if(inp_z > 0.0f){
1993 mul_v3_v3fl(state_co, z_vec, (float)sin((float)M_PI/3.f));
1994 madd_v3_v3fl(state_co, y_vec, -0.5f);
1996 mul_v3_fl(y_vec, -amplitude * (float)cos(t + (float)M_PI/3.f));
1997 mul_v3_fl(z_vec, amplitude/2.f * (float)cos(2.f*t + (float)M_PI/6.f));
2000 mul_v3_v3fl(state_co, z_vec, -(float)sin((float)M_PI/3.f));
2001 madd_v3_v3fl(state_co, y_vec, -0.5f);
2003 mul_v3_fl(y_vec, amplitude * (float)-sin(t + (float)M_PI/6.f));
2004 mul_v3_fl(z_vec, amplitude/2.f * (float)-sin(2.f*t + (float)M_PI/3.f));
2007 mul_v3_fl(state_co, amplitude);
2008 add_v3_v3(state_co, par->co);
2009 sub_v3_v3v3(par_vec, state->co, state_co);
2011 length = normalize_v3(par_vec);
2012 mul_v3_fl(par_vec, MIN2(length, amplitude/2.f));
2014 add_v3_v3v3(state_co, par->co, y_vec);
2015 add_v3_v3(state_co, z_vec);
2016 add_v3_v3(state_co, par_vec);
2018 shape = 2.f*(float)M_PI * (1.f+shape);
2022 shape = (float)sqrt((double)shape);
2023 interp_v3_v3v3(result, result, state_co, shape);
2026 copy_v3_v3(result, state_co);
2032 /* blend the start of the kink */
2034 interp_v3_v3v3(state->co, state->co, result, dt);
2036 copy_v3_v3(state->co, result);
2039 static float do_clump(ParticleKey *state, ParticleKey *par, float time, float clumpfac, float clumppow, float pa_clump)
2043 if(par && clumpfac!=0.0f){
2049 cpow=1.0f+9.0f*clumppow;
2051 if(clumpfac < 0.0f) /* clump roots instead of tips */
2052 clump = -clumpfac*pa_clump*(float)pow(1.0-(double)time,(double)cpow);
2054 clump = clumpfac*pa_clump*(float)pow((double)time,(double)cpow);
2056 interp_v3_v3v3(state->co,state->co,par->co,clump);
2061 void precalc_guides(ParticleSimulationData *sim, ListBase *effectors)
2063 EffectedPoint point;
2067 ParticleSystem *psys = sim->psys;
2068 EffectorWeights *weights = sim->psys->part->effector_weights;
2069 GuideEffectorData *data;
2076 psys_particle_on_emitter(sim->psmd,sim->psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,state.co,0,0,0,0,0);
2078 mul_m4_v3(sim->ob->obmat, state.co);
2079 mul_mat3_m4_v3(sim->ob->obmat, state.vel);
2081 pd_point_from_particle(sim, pa, &state, &point);
2083 for(eff = effectors->first; eff; eff=eff->next) {
2084 if(eff->pd->forcefield != PFIELD_GUIDE)
2087 if(!eff->guide_data)
2088 eff->guide_data = MEM_callocN(sizeof(GuideEffectorData)*psys->totpart, "GuideEffectorData");
2090 data = eff->guide_data + p;
2092 sub_v3_v3v3(efd.vec_to_point, state.co, eff->guide_loc);
2093 copy_v3_v3(efd.nor, eff->guide_dir);
2094 efd.distance = len_v3(efd.vec_to_point);
2096 copy_v3_v3(data->vec_to_point, efd.vec_to_point);
2097 data->strength = effector_falloff(eff, &efd, &point, weights);
2101 int do_guides(ListBase *effectors, ParticleKey *state, int index, float time)
2106 ParticleKey key, par;
2107 GuideEffectorData *data;
2109 float effect[3] = {0.0f, 0.0f, 0.0f}, veffect[3] = {0.0f, 0.0f, 0.0f};
2110 float guidevec[4], guidedir[3], rot2[4], temp[3];
2111 float guidetime, radius, weight, angle, totstrength = 0.0f;
2112 float vec_to_point[3];
2114 if(effectors) for(eff = effectors->first; eff; eff=eff->next) {
2117 if(pd->forcefield != PFIELD_GUIDE)
2120 data = eff->guide_data + index;
2122 if(data->strength <= 0.0f)
2125 guidetime = time / (1.0f - pd->free_end);
2130 cu = (Curve*)eff->ob->data;
2132 if(pd->flag & PFIELD_GUIDE_PATH_ADD) {
2133 if(where_on_path(eff->ob, data->strength * guidetime, guidevec, guidedir, NULL, &radius, &weight)==0)
2137 if(where_on_path(eff->ob, guidetime, guidevec, guidedir, NULL, &radius, &weight)==0)
2141 mul_m4_v3(eff->ob->obmat, guidevec);
2142 mul_mat3_m4_v3(eff->ob->obmat, guidedir);
2144 normalize_v3(guidedir);
2146 copy_v3_v3(vec_to_point, data->vec_to_point);
2148 if(guidetime != 0.0f) {
2149 /* curve direction */
2150 cross_v3_v3v3(temp, eff->guide_dir, guidedir);
2151 angle = dot_v3v3(eff->guide_dir, guidedir)/(len_v3(eff->guide_dir));
2152 angle = saacos(angle);
2153 axis_angle_to_quat( rot2,temp, angle);
2154 mul_qt_v3(rot2, vec_to_point);
2157 axis_angle_to_quat( rot2,guidedir, guidevec[3] - eff->guide_loc[3]);
2158 mul_qt_v3(rot2, vec_to_point);
2163 mul_v3_fl(vec_to_point, calc_taper(eff->scene, cu->taperobj, (int)(data->strength*guidetime*100.0f), 100));
2165 else{ /* curve size*/
2166 if(cu->flag & CU_PATH_RADIUS) {
2167 mul_v3_fl(vec_to_point, radius);
2170 par.co[0] = par.co[1] = par.co[2] = 0.0f;
2171 copy_v3_v3(key.co, vec_to_point);
2172 do_kink(&key, &par, 0, guidetime, pd->kink_freq, pd->kink_shape, pd->kink_amp, 0.f, pd->kink, pd->kink_axis, 0, 0);
2173 do_clump(&key, &par, guidetime, pd->clump_fac, pd->clump_pow, 1.0f);
2174 copy_v3_v3(vec_to_point, key.co);
2176 add_v3_v3(vec_to_point, guidevec);
2178 //sub_v3_v3v3(pa_loc,pa_loc,pa_zero);
2179 madd_v3_v3fl(effect, vec_to_point, data->strength);
2180 madd_v3_v3fl(veffect, guidedir, data->strength);
2181 totstrength += data->strength;
2183 if(pd->flag & PFIELD_GUIDE_PATH_WEIGHT)
2184 totstrength *= weight;
2187 if(totstrength != 0.0f){
2188 if(totstrength > 1.0f)
2189 mul_v3_fl(effect, 1.0f / totstrength);
2190 CLAMP(totstrength, 0.0f, 1.0f);
2191 //add_v3_v3(effect,pa_zero);
2192 interp_v3_v3v3(state->co, state->co, effect, totstrength);
2194 normalize_v3(veffect);
2195 mul_v3_fl(veffect, len_v3(state->vel));
2196 copy_v3_v3(state->vel, veffect);
2201 static void do_rough(float *loc, float mat[4][4], float t, float fac, float size, float thres, ParticleKey *state)
2207 if((float)fabs((float)(-1.5f+loc[0]+loc[1]+loc[2]))<1.5f*thres) return;
2209 copy_v3_v3(rco,loc);
2211 rough[0]=-1.0f+2.0f*BLI_gTurbulence(size, rco[0], rco[1], rco[2], 2,0,2);
2212 rough[1]=-1.0f+2.0f*BLI_gTurbulence(size, rco[1], rco[2], rco[0], 2,0,2);
2213 rough[2]=-1.0f+2.0f*BLI_gTurbulence(size, rco[2], rco[0], rco[1], 2,0,2);
2215 madd_v3_v3fl(state->co, mat[0], fac * rough[0]);
2216 madd_v3_v3fl(state->co, mat[1], fac * rough[1]);
2217 madd_v3_v3fl(state->co, mat[2], fac * rough[2]);
2219 static void do_rough_end(float *loc, float mat[4][4], float t, float fac, float shape, ParticleKey *state)
2224 roughfac=fac*(float)pow((double)t,shape);
2225 copy_v2_v2(rough,loc);
2226 rough[0]=-1.0f+2.0f*rough[0];
2227 rough[1]=-1.0f+2.0f*rough[1];
2228 mul_v2_fl(rough,roughfac);
2230 madd_v3_v3fl(state->co, mat[0], rough[0]);
2231 madd_v3_v3fl(state->co, mat[1], rough[1]);
2233 static void do_path_effectors(ParticleSimulationData *sim, int i, ParticleCacheKey *ca, int k, int steps, float *UNUSED(rootco), float effector, float UNUSED(dfra), float UNUSED(cfra), float *length, float *vec)
2235 float force[3] = {0.0f,0.0f,0.0f};
2236 ParticleKey eff_key;
2237 EffectedPoint epoint;
2239 /* Don't apply effectors for dynamic hair, otherwise the effectors don't get applied twice. */
2240 if(sim->psys->flag & PSYS_HAIR_DYNAMICS)
2243 copy_v3_v3(eff_key.co,(ca-1)->co);
2244 copy_v3_v3(eff_key.vel,(ca-1)->vel);
2245 copy_qt_qt(eff_key.rot,(ca-1)->rot);
2247 pd_point_from_particle(sim, sim->psys->particles+i, &eff_key, &epoint);
2248 pdDoEffectors(sim->psys->effectors, sim->colliders, sim->psys->part->effector_weights, &epoint, force, NULL);
2250 mul_v3_fl(force, effector*powf((float)k / (float)steps, 100.0f * sim->psys->part->eff_hair) / (float)steps);
2252 add_v3_v3(force, vec);
2254 normalize_v3(force);
2257 sub_v3_v3v3(vec, (ca+1)->co, ca->co);
2259 madd_v3_v3v3fl(ca->co, (ca-1)->co, force, *length);
2262 *length = len_v3(vec);
2264 static int check_path_length(int k, ParticleCacheKey *keys, ParticleCacheKey *state, float max_length, float *cur_length, float length, float *dvec)
2266 if(*cur_length + length > max_length){
2267 mul_v3_fl(dvec, (max_length - *cur_length) / length);
2268 add_v3_v3v3(state->co, (state - 1)->co, dvec);
2270 /* something over the maximum step value */
2274 *cur_length+=length;
2278 static void offset_child(ChildParticle *cpa, ParticleKey *par, float *par_rot, ParticleKey *child, float flat, float radius)
2280 copy_v3_v3(child->co, cpa->fuv);
2281 mul_v3_fl(child->co, radius);
2285 copy_v3_v3(child->vel, par->vel);
2288 mul_qt_v3(par_rot, child->co);
2289 copy_qt_qt(child->rot, par_rot);
2292 unit_qt(child->rot);
2294 add_v3_v3(child->co, par->co);
2296 float *psys_cache_vgroup(DerivedMesh *dm, ParticleSystem *psys, int vgroup)
2301 /* hair dynamics pinning vgroup */
2304 else if(psys->vgroup[vgroup]){
2305 MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
2307 int totvert=dm->getNumVerts(dm), i;
2308 vg=MEM_callocN(sizeof(float)*totvert, "vg_cache");
2309 if(psys->vg_neg&(1<<vgroup)){
2310 for(i=0; i<totvert; i++)
2311 vg[i]=1.0f-vert_weight(dvert+i,psys->vgroup[vgroup]-1);
2314 for(i=0; i<totvert; i++)
2315 vg[i]=vert_weight(dvert+i,psys->vgroup[vgroup]-1);
2321 void psys_find_parents(ParticleSimulationData *sim)
2323 ParticleSettings *part=sim->psys->part;
2326 int p, totparent,totchild=sim->psys->totchild;
2327 float co[3], orco[3];
2328 int from=PART_FROM_FACE;
2329 totparent=(int)(totchild*part->parents*0.3f);
2331 if(G.rendering && part->child_nbr && part->ren_child_nbr)
2332 totparent*=(float)part->child_nbr/(float)part->ren_child_nbr;
2334 tree=BLI_kdtree_new(totparent);
2336 for(p=0,cpa=sim->psys->child; p<totparent; p++,cpa++){
2337 psys_particle_on_emitter(sim->psmd,from,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co,0,0,0,orco,0);
2338 BLI_kdtree_insert(tree, p, orco, NULL);
2341 BLI_kdtree_balance(tree);
2343 for(; p<totchild; p++,cpa++){
2344 psys_particle_on_emitter(sim->psmd,from,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co,0,0,0,orco,0);
2345 cpa->parent=BLI_kdtree_find_nearest(tree, orco, NULL, NULL);
2348 BLI_kdtree_free(tree);
2351 static void get_strand_normal(Material *ma, float *surfnor, float surfdist, float *nor)
2353 float cross[3], nstrand[3], vnor[3], blend;
2355 if(!((ma->mode & MA_STR_SURFDIFF) || (ma->strand_surfnor > 0.0f)))
2358 if(ma->mode & MA_STR_SURFDIFF) {
2359 cross_v3_v3v3(cross, surfnor, nor);
2360 cross_v3_v3v3(nstrand, nor, cross);
2362 blend= INPR(nstrand, surfnor);
2363 CLAMP(blend, 0.0f, 1.0f);
2365 interp_v3_v3v3(vnor, nstrand, surfnor, blend);
2369 copy_v3_v3(vnor, nor);
2372 if(ma->strand_surfnor > 0.0f) {
2373 if(ma->strand_surfnor > surfdist) {
2374 blend= (ma->strand_surfnor - surfdist)/ma->strand_surfnor;
2375 interp_v3_v3v3(vnor, vnor, surfnor, blend);
2380 copy_v3_v3(nor, vnor);
2383 static int psys_threads_init_path(ParticleThread *threads, Scene *scene, float cfra, int editupdate)
2385 ParticleThreadContext *ctx= threads[0].ctx;
2386 /* Object *ob= ctx->sim.ob; */
2387 ParticleSystem *psys= ctx->sim.psys;
2388 ParticleSettings *part = psys->part;
2389 /* ParticleEditSettings *pset = &scene->toolsettings->particle; */
2390 int totparent=0, between=0;
2391 int steps = (int)pow(2.0, (double)part->draw_step);
2392 int totchild = psys->totchild;
2393 int i, seed, totthread= threads[0].tot;
2395 /*---start figuring out what is actually wanted---*/
2396 if(psys_in_edit_mode(scene, psys)) {
2397 ParticleEditSettings *pset = &scene->toolsettings->particle;
2399 if(psys->renderdata==0 && (psys->edit==NULL || pset->flag & PE_DRAW_PART)==0)
2402 steps = (int)pow(2.0, (double)pset->draw_step);
2405 if(totchild && part->childtype==PART_CHILD_FACES){
2406 totparent=(int)(totchild*part->parents*0.3f);
2408 if(G.rendering && part->child_nbr && part->ren_child_nbr)
2409 totparent*=(float)part->child_nbr/(float)part->ren_child_nbr;
2411 /* part->parents could still be 0 so we can't test with totparent */
2415 if(psys->renderdata)
2416 steps=(int)pow(2.0,(double)part->ren_step);
2418 totchild=(int)((float)totchild*(float)part->disp/100.0f);
2419 totparent=MIN2(totparent,totchild);
2422 if(totchild==0) return 0;
2424 /* init random number generator */
2425 seed= 31415926 + ctx->sim.psys->seed;
2427 if(ctx->editupdate || totchild < 10000)
2430 for(i=0; i<totthread; i++) {
2431 threads[i].rng_path= rng_new(seed);
2432 threads[i].tot= totthread;
2435 /* fill context values */
2436 ctx->between= between;
2438 ctx->totchild= totchild;
2439 ctx->totparent= totparent;
2440 ctx->parent_pass= 0;
2442 ctx->editupdate= editupdate;
2444 psys->lattice = psys_get_lattice(&ctx->sim);
2446 /* cache all relevant vertex groups if they exist */
2447 ctx->vg_length = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_LENGTH);
2448 ctx->vg_clump = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_CLUMP);
2449 ctx->vg_kink = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_KINK);
2450 ctx->vg_rough1 = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_ROUGH1);
2451 ctx->vg_rough2 = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_ROUGH2);
2452 ctx->vg_roughe = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_ROUGHE);
2453 if(psys->part->flag & PART_CHILD_EFFECT)
2454 ctx->vg_effector = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_EFFECTOR);
2456 /* set correct ipo timing */
2457 #if 0 // XXX old animation system
2458 if(part->flag&PART_ABS_TIME && part->ipo){
2459 calc_ipo(part->ipo, cfra);
2460 execute_ipo((ID *)part, part->ipo);
2462 #endif // XXX old animation system
2467 /* note: this function must be thread safe, except for branching! */
2468 static void psys_thread_create_path(ParticleThread *thread, struct ChildParticle *cpa, ParticleCacheKey *child_keys, int i)
2470 ParticleThreadContext *ctx= thread->ctx;
2471 Object *ob= ctx->sim.ob;
2472 ParticleSystem *psys = ctx->sim.psys;
2473 ParticleSettings *part = psys->part;
2474 ParticleCacheKey **cache= psys->childcache;
2475 ParticleCacheKey **pcache= psys_in_edit_mode(ctx->sim.scene, psys) ? psys->edit->pathcache : psys->pathcache;
2476 ParticleCacheKey *child, *par = NULL, *key[4];
2477 ParticleTexture ptex;
2478 float *cpa_fuv=0, *par_rot=0, rot[4];
2479 float orco[3], ornor[3], hairmat[4][4], t, dvec[3], off1[4][3], off2[4][3];
2480 float length, max_length = 1.0f, cur_length = 0.0f;
2481 float eff_length, eff_vec[3], weight[4];
2489 ParticleData *pa = psys->particles + cpa->pa[0];
2491 float foffset, wsum=0.f;
2493 float p_min = part->parting_min;
2494 float p_max = part->parting_max;
2495 /* Virtual parents don't work nicely with parting. */
2496 float p_fac = part->parents > 0.f ? 0.f : part->parting_fac;
2498 if(ctx->editupdate) {
2501 while(w<4 && cpa->pa[w]>=0) {
2502 if(psys->edit->points[cpa->pa[w]].flag & PEP_EDIT_RECALC) {
2512 memset(child_keys, 0, sizeof(*child_keys)*(ctx->steps+1));
2515 /* get parent paths */
2516 for(w=0; w<4; w++) {
2517 if(cpa->pa[w] >= 0) {
2518 key[w] = pcache[cpa->pa[w]];
2519 weight[w] = cpa->w[w];
2527 /* modify weights to create parting */
2529 for(w=0; w<4; w++) {
2530 if(w && weight[w] > 0.f) {
2532 if(part->flag & PART_CHILD_LONG_HAIR) {
2533 /* For long hair use tip distance/root distance as parting factor instead of root to tip angle. */
2534 float d1 = len_v3v3(key[0]->co, key[w]->co);
2535 float d2 = len_v3v3((key[0]+key[0]->steps-1)->co, (key[w]+key[w]->steps-1)->co);
2537 d = d1 > 0.f ? d2/d1 - 1.f : 10000.f;
2541 sub_v3_v3v3(v1, (key[0]+key[0]->steps-1)->co, key[0]->co);
2542 sub_v3_v3v3(v2, (key[w]+key[w]->steps-1)->co, key[w]->co);
2546 d = RAD2DEGF(saacos(dot_v3v3(v1, v2)));
2550 d = (d - p_min)/(p_max - p_min);
2552 d = (d - p_min) <= 0.f ? 0.f : 1.f;
2557 weight[w] *= (1.f - d);
2564 interp_v4_v4v4(weight, cpa->w, weight, p_fac);
2567 /* get the original coordinates (orco) for texture usage */
2570 foffset = cpa->foffset;
2572 cpa_from = PART_FROM_FACE;
2574 psys_particle_on_emitter(ctx->sim.psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa->fuv,foffset,co,ornor,0,0,orco,0);
2576 mul_m4_v3(ob->obmat, co);
2579 sub_v3_v3v3(off1[w], co, key[w]->co);
2581 psys_mat_hair_to_global(ob, ctx->sim.psmd->dm, psys->part->from, pa, hairmat);
2584 ParticleData *pa = psys->particles + cpa->parent;
2586 if(ctx->editupdate) {
2587 if(!(psys->edit->points[cpa->parent].flag & PEP_EDIT_RECALC))
2590 memset(child_keys, 0, sizeof(*child_keys)*(ctx->steps+1));
2593 /* get the parent path */
2594 key[0] = pcache[cpa->parent];
2596 /* get the original coordinates (orco) for texture usage */
2597 cpa_from = part->from;
2601 psys_particle_on_emitter(ctx->sim.psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa_fuv,pa->foffset,co,ornor,0,0,orco,0);
2603 psys_mat_hair_to_global(ob, ctx->sim.psmd->dm, psys->part->from, pa, hairmat);
2606 child_keys->steps = ctx->steps;
2608 /* get different child parameters from textures & vgroups */
2609 get_child_modifier_parameters(part, ctx, cpa, cpa_from, cpa_num, cpa_fuv, orco, &ptex);
2611 if(ptex.exist < PSYS_FRAND(i + 24)) {
2612 child_keys->steps = -1;
2616 /* create the child path */
2617 for(k=0,child=child_keys; k<=ctx->steps; k++,child++){
2622 zero_v3(child->vel);
2623 unit_qt(child->rot);
2625 for(w=0; w<4; w++) {
2626 copy_v3_v3(off2[w], off1[w]);
2628 if(part->flag & PART_CHILD_LONG_HAIR) {
2629 /* Use parent rotation (in addition to emission location) to determine child offset. */
2631 mul_qt_v3((key[w]+k)->rot, off2[w]);
2633 /* Fade the effect of rotation for even lengths in the end */
2634 project_v3_v3v3(dvec, off2[w], (key[w]+k)->vel);
2635 madd_v3_v3fl(off2[w], dvec, -(float)k/(float)ctx->steps);
2638 add_v3_v3(off2[w], (key[w]+k)->co);
2641 /* child position is the weighted sum of parent positions */
2642 interp_v3_v3v3v3v3(child->co, off2[0], off2[1], off2[2], off2[3], weight);
2643 interp_v3_v3v3v3v3(child->vel, (key[0]+k)->vel, (key[1]+k)->vel, (key[2]+k)->vel, (key[3]+k)->vel, weight);
2645 copy_qt_qt(child->rot, (key[0]+k)->rot);
2649 mul_qt_qtqt(rot, (key[0]+k)->rot, key[0]->rot);
2653 par_rot = key[0]->rot;
2655 /* offset the child from the parent position */
2656 offset_child(cpa, (ParticleKey*)(key[0]+k), par_rot, (ParticleKey*)child, part->childflat, part->childrad);
2660 /* apply effectors */
2661 if(part->flag & PART_CHILD_EFFECT) {
2662 for(k=0,child=child_keys; k<=ctx->steps; k++,child++) {
2664 do_path_effectors(&ctx->sim, cpa->pa[0], child, k, ctx->steps, child_keys->co, ptex.effector, 0.0f, ctx->cfra, &eff_length, eff_vec);
2667 sub_v3_v3v3(eff_vec, (child+1)->co, child->co);
2668 eff_length = len_v3(eff_vec);
2673 for(k=0,child=child_keys; k<=ctx->steps; k++,child++){
2674 t = (float)k/(float)ctx->steps;
2677 /* this is now threadsafe, virtual parents are calculated before rest of children */
2678 par = (i >= ctx->totparent) ? cache[cpa->parent] : NULL;
2679 else if(cpa->parent >= 0)
2680 par = pcache[cpa->parent];
2684 mul_qt_qtqt(rot, (par+k)->rot, par->rot);
2693 /* apply different deformations to the child path */
2694 do_child_modifiers(&ctx->sim, &ptex, (ParticleKey *)par, par_rot, cpa, orco, hairmat, (ParticleKey *)child, t);
2696 /* we have to correct velocity because of kink & clump */
2698 sub_v3_v3v3((child-1)->vel, child->co, (child-2)->co);
2699 mul_v3_fl((child-1)->vel, 0.5);
2701 if(ctx->ma && (part->draw_col == PART_DRAW_COL_MAT))
2702 get_strand_normal(ctx->ma, ornor, cur_length, (child-1)->vel);
2706 sub_v3_v3v3(child->vel, child->co, (child-1)->co);
2708 /* check if path needs to be cut before actual end of data points */
2710 sub_v3_v3v3(dvec, child->co, (child-1)->co);
2711 length = 1.0f/(float)ctx->steps;
2712 k = check_path_length(k, child_keys, child, max_length, &cur_length, length, dvec);
2715 /* initialize length calculation */
2716 max_length = ptex.length;
2720 if(ctx->ma && (part->draw_col == PART_DRAW_COL_MAT)) {
2721 copy_v3_v3(child->col, &ctx->ma->r);
2722 get_strand_normal(ctx->ma, ornor, cur_length, child->vel);
2726 /* Hide virtual parents */
2727 if(i < ctx->totparent)
2728 child_keys->steps = -1;
2731 static void *exec_child_path_cache(void *data)
2733 ParticleThread *thread= (ParticleThread*)data;
2734 ParticleThreadContext *ctx= thread->ctx;
2735 ParticleSystem *psys= ctx->sim.psys;
2736 ParticleCacheKey **cache= psys->childcache;
2738 int i, totchild= ctx->totchild, first= 0;
2740 if(thread->tot > 1){
2741 first= ctx->parent_pass? 0 : ctx->totparent;
2742 totchild= ctx->parent_pass? ctx->totparent : ctx->totchild;
2745 cpa= psys->child + first + thread->num;
2746 for(i=first+thread->num; i<totchild; i+=thread->tot, cpa+=thread->tot)
2747 psys_thread_create_path(thread, cpa, cache[i], i);
2752 void psys_cache_child_paths(ParticleSimulationData *sim, float cfra, int editupdate)
2754 ParticleThread *pthreads;
2755 ParticleThreadContext *ctx;
2757 int i, totchild, totparent, totthread;
2759 if(sim->psys->flag & PSYS_GLOBAL_HAIR)
2762 pthreads= psys_threads_create(sim);
2764 if(!psys_threads_init_path(pthreads, sim->scene, cfra, editupdate)) {
2765 psys_threads_free(pthreads);
2769 ctx= pthreads[0].ctx;
2770 totchild= ctx->totchild;
2771 totparent= ctx->totparent;
2773 if(editupdate && sim->psys->childcache && totchild == sim->psys->totchildcache) {
2774 ; /* just overwrite the existing cache */
2777 /* clear out old and create new empty path cache */
2778 free_child_path_cache(sim->psys);
2779 sim->psys->childcache= psys_alloc_path_cache_buffers(&sim->psys->childcachebufs, totchild, ctx->steps+1);
2780 sim->psys->totchildcache = totchild;
2783 totthread= pthreads[0].tot;
2787 /* make virtual child parents thread safe by calculating them first */
2789 BLI_init_threads(&threads, exec_child_path_cache, totthread);
2791 for(i=0; i<totthread; i++) {
2792 pthreads[i].ctx->parent_pass = 1;
2793 BLI_insert_thread(&threads, &pthreads[i]);
2796 BLI_end_threads(&threads);
2798 for(i=0; i<totthread; i++)
2799 pthreads[i].ctx->parent_pass = 0;
2802 BLI_init_threads(&threads, exec_child_path_cache, totthread);
2804 for(i=0; i<totthread; i++)
2805 BLI_insert_thread(&threads, &pthreads[i]);
2807 BLI_end_threads(&threads);
2810 exec_child_path_cache(&pthreads[0]);
2812 psys_threads_free(pthreads);
2814 /* figure out incremental rotations along path starting from unit quat */
2815 static void cache_key_incremental_rotation(ParticleCacheKey *key0, ParticleCacheKey *key1, ParticleCacheKey *key2, float *prev_tangent, int i)
2817 float cosangle, angle, tangent[3], normal[3], q[4];
2821 /* start from second key */
2824 /* calculate initial tangent for incremental rotations */
2825 sub_v3_v3v3(prev_tangent, key0->co, key1->co);
2826 normalize_v3(prev_tangent);
2830 sub_v3_v3v3(tangent, key0->co, key1->co);
2831 normalize_v3(tangent);
2833 cosangle= dot_v3v3(tangent, prev_tangent);
2835 /* note we do the comparison on cosangle instead of
2836 * angle, since floating point accuracy makes it give
2837 * different results across platforms */
2838 if(cosangle > 0.999999f) {
2839 copy_v4_v4(key1->rot, key2->rot);
2842 angle= saacos(cosangle);
2843 cross_v3_v3v3(normal, prev_tangent, tangent);
2844 axis_angle_to_quat( q,normal, angle);
2845 mul_qt_qtqt(key1->rot, q, key2->rot);
2848 copy_v3_v3(prev_tangent, tangent);
2851 /* Calculates paths ready for drawing/rendering. */
2852 /* -Usefull for making use of opengl vertex arrays for super fast strand drawing. */
2853 /* -Makes child strands possible and creates them too into the cache. */
2854 /* -Cached path data is also used to determine cut position for the editmode tool. */
2855 void psys_cache_paths(ParticleSimulationData *sim, float cfra)
2858 ParticleEditSettings *pset = &sim->scene->toolsettings->particle;
2859 ParticleSystem *psys = sim->psys;
2860 ParticleSettings *part = psys->part;
2861 ParticleCacheKey *ca, **cache;
2863 DerivedMesh *hair_dm = (psys->part->type==PART_HAIR && psys->flag & PSYS_HAIR_DYNAMICS) ? psys->hair_out_dm : NULL;
2868 ParticleInterpolationData pind;
2869 ParticleTexture ptex;
2873 float birthtime = 0.0, dietime = 0.0;
2874 float t, time = 0.0, dfra = 1.0 /* , frs_sec = sim->scene->r.frs_sec*/ /*UNUSED*/;
2875 float col[4] = {0.5f, 0.5f, 0.5f, 1.0f};
2876 float prev_tangent[3] = {0.0f, 0.0f, 0.0f}, hairmat[4][4];
2879 int steps = (int)pow(2.0, (double)(psys->renderdata ? part->ren_step : part->draw_step));
2880 int totpart = psys->totpart;
2881 float length, vec[3];
2882 float *vg_effector= NULL;
2883 float *vg_length= NULL, pa_length=1.0f;
2886 /* we don't have anything valid to create paths from so let's quit here */
2887 if((psys->flag & PSYS_HAIR_DONE || psys->flag & PSYS_KEYED || psys->pointcache)==0)
2890 if(psys_in_edit_mode(sim->scene, psys))
2891 if(psys->renderdata==0 && (psys->edit==NULL || pset->flag & PE_DRAW_PART)==0)
2894 keyed = psys->flag & PSYS_KEYED;
2895 baked = psys->pointcache->mem_cache.first && psys->part->type != PART_HAIR;
2897 /* clear out old and create new empty path cache */
2898 psys_free_path_cache(psys, psys->edit);
2899 cache= psys->pathcache= psys_alloc_path_cache_buffers(&psys->pathcachebufs, totpart, steps+1);
2901 psys->lattice = psys_get_lattice(sim);
2902 ma= give_current_material(sim->ob, psys->part->omat);
2903 if(ma && (psys->part->draw_col == PART_DRAW_COL_MAT))
2904 copy_v3_v3(col, &ma->r);
2906 if((psys->flag & PSYS_GLOBAL_HAIR)==0) {
2907 if((psys->part->flag & PART_CHILD_EFFECT)==0)
2908 vg_effector = psys_cache_vgroup(psmd->dm, psys, PSYS_VG_EFFECTOR);
2911 vg_length = psys_cache_vgroup(psmd->dm, psys, PSYS_VG_LENGTH);
2914 /*---first main loop: create all actual particles' paths---*/
2915 LOOP_SHOWN_PARTICLES {
2916 if(!psys->totchild) {
2917 psys_get_texture(sim, pa, &ptex, PAMAP_LENGTH, 0.f);
2918 pa_length = ptex.length * (1.0f - part->randlength * PSYS_FRAND(psys->seed + p));
2920 pa_length *= psys_particle_value_from_verts(psmd->dm,part->from,pa,vg_length);
2924 pind.cache = baked ? psys->pointcache : NULL;
2926 pind.bspline = (psys->part->flag & PART_HAIR_BSPLINE);
2929 memset(cache[p], 0, sizeof(*cache[p])*(steps+1));
2931 cache[p]->steps = steps;
2933 /*--get the first data points--*/
2934 init_particle_interpolation(sim->ob, sim->psys, pa, &pind);
2936 /* hairmat is needed for for non-hair particle too so we get proper rotations */
2937 psys_mat_hair_to_global(sim->ob, psmd->dm, psys->part->from, pa, hairmat);
2938 copy_v3_v3(rotmat[0], hairmat[2]);
2939 copy_v3_v3(rotmat[1], hairmat[1]);
2940 copy_v3_v3(rotmat[2], hairmat[0]);
2942 if(part->draw & PART_ABS_PATH_TIME) {
2943 birthtime = MAX2(pind.birthtime, part->path_start);
2944 dietime = MIN2(pind.dietime, part->path_end);
2947 float tb = pind.birthtime;
2948 birthtime = tb + part->path_start * (pind.dietime - tb);
2949 dietime = tb + part->path_end * (pind.dietime - tb);
2952 if(birthtime >= dietime) {
2953 cache[p]->steps = -1;
2957 dietime = birthtime + pa_length * (dietime - birthtime);
2959 /*--interpolate actual path from data points--*/
2960 for(k=0, ca=cache[p]; k<=steps; k++, ca++){
2961 time = (float)k / (float)steps;
2962 t = birthtime + time * (dietime - birthtime);
2964 do_particle_interpolation(psys, p, pa, t, &pind, &result);
2965 copy_v3_v3(ca->co, result.co);
2967 /* dynamic hair is in object space */
2968 /* keyed and baked are already in global space */
2970 mul_m4_v3(sim->ob->obmat, ca->co);
2971 else if(!keyed && !baked && !(psys->flag & PSYS_GLOBAL_HAIR))
2972 mul_m4_v3(hairmat, ca->co);
2974 copy_v3_v3(ca->col, col);
2977 /*--modify paths and calculate rotation & velocity--*/
2979 if(!(psys->flag & PSYS_GLOBAL_HAIR)) {
2980 /* apply effectors */
2981 if((psys->part->flag & PART_CHILD_EFFECT) == 0) {
2982 float effector= 1.0f;
2984 effector*= psys_particle_value_from_verts(psmd->dm,psys->part->from,pa,vg_effector);
2986 sub_v3_v3v3(vec,(cache[p]+1)->co,cache[p]->co);
2987 length = len_v3(vec);
2989 for(k=1, ca=cache[p]+1; k<=steps; k++, ca++)
2990 do_path_effectors(sim, p, ca, k, steps, cache[p]->co, effector, dfra, cfra, &length, vec);
2993 /* apply guide curves to path data */
2994 if(sim->psys->effectors && (psys->part->flag & PART_CHILD_EFFECT)==0) {
2995 for(k=0, ca=cache[p]; k<=steps; k++, ca++)
2996 /* ca is safe to cast, since only co and vel are used */
2997 do_guides(sim->psys->effectors, (ParticleKey*)ca, p, (float)k/(float)steps);
3000 /* lattices have to be calculated separately to avoid mixups between effector calculations */
3002 for(k=0, ca=cache[p]; k<=steps; k++, ca++)
3003 calc_latt_deform(psys->lattice, ca->co, 1.0f);
3007 /* finally do rotation & velocity */
3008 for(k=1, ca=cache[p]+1; k<=steps; k++, ca++) {
3009 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k);
3012 copy_qt_qt(ca->rot, (ca - 1)->rot);
3015 sub_v3_v3v3(ca->vel, ca->co, (ca-1)->co);
3018 copy_v3_v3((ca-1)->vel, ca->vel);
3020 /* First rotation is based on emitting face orientation.
3021 * This is way better than having flipping rotations resulting
3022 * from using a global axis as a rotation pole (vec_to_quat()).
3023 * It's not an ideal solution though since it disregards the
3024 * initial tangent, but taking that in to account will allow
3025 * the possibility of flipping again. -jahka
3027 mat3_to_quat_is_ok(cache[p]->rot, rotmat);
3030 psys->totcached = totpart;
3033 end_latt_deform(psys->lattice);
3034 psys->lattice= NULL;
3038 MEM_freeN(vg_effector);
3041 MEM_freeN(vg_length);
3043 void psys_cache_edit_paths(Scene *scene, Object *ob, PTCacheEdit *edit, float cfra)
3045 ParticleCacheKey *ca, **cache= edit->pathcache;
3046 ParticleEditSettings *pset = &scene->toolsettings->particle;
3048 PTCacheEditPoint *point = NULL;
3049 PTCacheEditKey *ekey = NULL;
3051 ParticleSystem *psys = edit->psys;
3052 ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
3053 ParticleData *pa = psys ? psys->particles : NULL;
3055 ParticleInterpolationData pind;
3058 float birthtime = 0.0f, dietime = 0.0f;
3059 float t, time = 0.0f, keytime = 0.0f /*, frs_sec */;
3060 float hairmat[4][4], rotmat[3][3], prev_tangent[3] = {0.0f, 0.0f, 0.0f};
3062 int steps = (int)pow(2.0, (double)pset->draw_step);
3063 int totpart = edit->totpoint, recalc_set=0;
3067 steps = MAX2(steps, 4);
3069 if(!cache || edit->totpoint != edit->totcached) {
3070 /* clear out old and create new empty path cache */
3071 psys_free_path_cache(edit->psys, edit);
3072 cache= edit->pathcache= psys_alloc_path_cache_buffers(&edit->pathcachebufs, totpart, steps+1);
3074 /* set flag for update (child particles check this too) */
3075 for(i=0, point=edit->points; i<totpart; i++, point++)
3076 point->flag |= PEP_EDIT_RECALC;
3080 /* frs_sec = (psys || edit->pid.flag & PTCACHE_VEL_PER_SEC) ? 25.0f : 1.0f; */ /* UNUSED */
3082 if(pset->brushtype == PE_BRUSH_WEIGHT) {
3083 ;/* use weight painting colors now... */
3086 sel_col[0] = (float)edit->sel_col[0] / 255.0f;
3087 sel_col[1] = (float)edit->sel_col[1] / 255.0f;
3088 sel_col[2] = (float)edit->sel_col[2] / 255.0f;
3089 nosel_col[0] = (float)edit->nosel_col[0] / 255.0f;
3090 nosel_col[1] = (float)edit->nosel_col[1] / 255.0f;
3091 nosel_col[2] = (float)edit->nosel_col[2] / 255.0f;
3094 /*---first main loop: create all actual particles' paths---*/
3095 for(i=0, point=edit->points; i<totpart; i++, pa+=pa?1:0, point++){
3096 if(edit->totcached && !(point->flag & PEP_EDIT_RECALC))
3103 pind.epoint = point;
3104 pind.bspline = psys ? (psys->part->flag & PART_HAIR_BSPLINE) : 0;
3108 /* should init_particle_interpolation set this ? */
3109 if(pset->brushtype==PE_BRUSH_WEIGHT){
3110 pind.hkey[0] = NULL;
3111 /* pa != NULL since the weight brush is only available for hair */
3112 pind.hkey[1] = pa->hair;
3116 memset(cache[i], 0, sizeof(*cache[i])*(steps+1));
3118 cache[i]->steps = steps;
3120 /*--get the first data points--*/
3121 init_particle_interpolation(ob, psys, pa, &pind);
3124 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, hairmat);
3125 copy_v3_v3(rotmat[0], hairmat[2]);
3126 copy_v3_v3(rotmat[1], hairmat[1]);
3127 copy_v3_v3(rotmat[2], hairmat[0]);
3130 birthtime = pind.birthtime;
3131 dietime = pind.dietime;
3133 if(birthtime >= dietime) {
3134 cache[i]->steps = -1;
3138 /*--interpolate actual path from data points--*/
3139 for(k=0, ca=cache[i]; k<=steps; k++, ca++){
3140 time = (float)k / (float)steps;
3141 t = birthtime + time * (dietime - birthtime);
3143 do_particle_interpolation(psys, i, pa, t, &pind, &result);
3144 copy_v3_v3(ca->co, result.co);
3146 /* non-hair points are already in global space */
3147 if(psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
3148 mul_m4_v3(hairmat, ca->co);
3151 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k);
3154 copy_qt_qt(ca->rot, (ca - 1)->rot);
3157 sub_v3_v3v3(ca->vel, ca->co, (ca - 1)->co);
3160 copy_v3_v3((ca - 1)->vel, ca->vel);
3164 ca->vel[0] = ca->vel[1] = 0.0f;
3168 /* selection coloring in edit mode */
3169 if(pset->brushtype==PE_BRUSH_WEIGHT){
3173 weight_to_rgb(pind.hkey[1]->weight, ca->col, ca->col+1, ca->col+2);
3176 keytime = (t - (*pind.ekey[0]->time))/((*pind.ekey[1]->time) - (*pind.ekey[0]->time));
3178 weight_to_rgb(pind.hkey[0]->weight, w1, w1+1, w1+2);
3179 weight_to_rgb(pind.hkey[1]->weight, w2, w2+1, w2+2);
3181 interp_v3_v3v3(ca->col, w1, w2, keytime);
3184 /* at the moment this is only used for weight painting.
3185 * will need to move out of this check if its used