|
Blender
V2.59
|
00001 /* particle.c 00002 * 00003 * 00004 * $Id: particle.c 39244 2011-08-10 07:36:57Z jesterking $ 00005 * 00006 * ***** BEGIN GPL LICENSE BLOCK ***** 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU General Public License 00010 * as published by the Free Software Foundation; either version 2 00011 * of the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software Foundation, 00020 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00021 * 00022 * The Original Code is Copyright (C) 2007 by Janne Karhu. 00023 * All rights reserved. 00024 * 00025 * The Original Code is: all of this file. 00026 * 00027 * Contributor(s): none yet. 00028 * 00029 * ***** END GPL LICENSE BLOCK ***** 00030 */ 00031 00037 #include <stdlib.h> 00038 #include <math.h> 00039 #include <string.h> 00040 00041 #include "MEM_guardedalloc.h" 00042 00043 #include "DNA_curve_types.h" 00044 #include "DNA_group_types.h" 00045 #include "DNA_key_types.h" 00046 #include "DNA_material_types.h" 00047 #include "DNA_mesh_types.h" 00048 #include "DNA_meshdata_types.h" 00049 #include "DNA_particle_types.h" 00050 #include "DNA_smoke_types.h" 00051 #include "DNA_scene_types.h" 00052 00053 #include "BLI_blenlib.h" 00054 #include "BLI_math.h" 00055 #include "BLI_utildefines.h" 00056 #include "BLI_kdtree.h" 00057 #include "BLI_rand.h" 00058 #include "BLI_threads.h" 00059 00060 #include "BKE_anim.h" 00061 #include "BKE_animsys.h" 00062 00063 #include "BKE_boids.h" 00064 #include "BKE_cloth.h" 00065 #include "BKE_effect.h" 00066 #include "BKE_global.h" 00067 #include "BKE_group.h" 00068 #include "BKE_main.h" 00069 #include "BKE_lattice.h" 00070 00071 #include "BKE_displist.h" 00072 #include "BKE_particle.h" 00073 #include "BKE_object.h" 00074 #include "BKE_material.h" 00075 #include "BKE_key.h" 00076 #include "BKE_library.h" 00077 #include "BKE_depsgraph.h" 00078 #include "BKE_modifier.h" 00079 #include "BKE_mesh.h" 00080 #include "BKE_cdderivedmesh.h" 00081 #include "BKE_pointcache.h" 00082 00083 #include "RE_render_ext.h" 00084 00085 static void get_child_modifier_parameters(ParticleSettings *part, ParticleThreadContext *ctx, 00086 ChildParticle *cpa, short cpa_from, int cpa_num, float *cpa_fuv, float *orco, ParticleTexture *ptex); 00087 static void do_child_modifiers(ParticleSimulationData *sim, 00088 ParticleTexture *ptex, ParticleKey *par, float *par_rot, ChildParticle *cpa, 00089 float *orco, float mat[4][4], ParticleKey *state, float t); 00090 00091 /* few helpers for countall etc. */ 00092 int count_particles(ParticleSystem *psys){ 00093 ParticleSettings *part=psys->part; 00094 PARTICLE_P; 00095 int tot=0; 00096 00097 LOOP_SHOWN_PARTICLES { 00098 if(pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN)==0); 00099 else if(pa->alive == PARS_DEAD && (part->flag & PART_DIED)==0); 00100 else tot++; 00101 } 00102 return tot; 00103 } 00104 int count_particles_mod(ParticleSystem *psys, int totgr, int cur){ 00105 ParticleSettings *part=psys->part; 00106 PARTICLE_P; 00107 int tot=0; 00108 00109 LOOP_SHOWN_PARTICLES { 00110 if(pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN)==0); 00111 else if(pa->alive == PARS_DEAD && (part->flag & PART_DIED)==0); 00112 else if(p%totgr==cur) tot++; 00113 } 00114 return tot; 00115 } 00116 /* we allocate path cache memory in chunks instead of a big continguous 00117 * chunk, windows' memory allocater fails to find big blocks of memory often */ 00118 00119 #define PATH_CACHE_BUF_SIZE 1024 00120 00121 static ParticleCacheKey **psys_alloc_path_cache_buffers(ListBase *bufs, int tot, int steps) 00122 { 00123 LinkData *buf; 00124 ParticleCacheKey **cache; 00125 int i, totkey, totbufkey; 00126 00127 tot= MAX2(tot, 1); 00128 totkey = 0; 00129 cache = MEM_callocN(tot*sizeof(void*), "PathCacheArray"); 00130 00131 while(totkey < tot) { 00132 totbufkey= MIN2(tot-totkey, PATH_CACHE_BUF_SIZE); 00133 buf= MEM_callocN(sizeof(LinkData), "PathCacheLinkData"); 00134 buf->data= MEM_callocN(sizeof(ParticleCacheKey)*totbufkey*steps, "ParticleCacheKey"); 00135 00136 for(i=0; i<totbufkey; i++) 00137 cache[totkey+i] = ((ParticleCacheKey*)buf->data) + i*steps; 00138 00139 totkey += totbufkey; 00140 BLI_addtail(bufs, buf); 00141 } 00142 00143 return cache; 00144 } 00145 00146 static void psys_free_path_cache_buffers(ParticleCacheKey **cache, ListBase *bufs) 00147 { 00148 LinkData *buf; 00149 00150 if(cache) 00151 MEM_freeN(cache); 00152 00153 for(buf= bufs->first; buf; buf=buf->next) 00154 MEM_freeN(buf->data); 00155 BLI_freelistN(bufs); 00156 } 00157 00158 /************************************************/ 00159 /* Getting stuff */ 00160 /************************************************/ 00161 /* get object's active particle system safely */ 00162 ParticleSystem *psys_get_current(Object *ob) 00163 { 00164 ParticleSystem *psys; 00165 if(ob==NULL) return NULL; 00166 00167 for(psys=ob->particlesystem.first; psys; psys=psys->next){ 00168 if(psys->flag & PSYS_CURRENT) 00169 return psys; 00170 } 00171 00172 return NULL; 00173 } 00174 short psys_get_current_num(Object *ob) 00175 { 00176 ParticleSystem *psys; 00177 short i; 00178 00179 if(ob==NULL) return 0; 00180 00181 for(psys=ob->particlesystem.first, i=0; psys; psys=psys->next, i++) 00182 if(psys->flag & PSYS_CURRENT) 00183 return i; 00184 00185 return i; 00186 } 00187 void psys_set_current_num(Object *ob, int index) 00188 { 00189 ParticleSystem *psys; 00190 short i; 00191 00192 if(ob==NULL) return; 00193 00194 for(psys=ob->particlesystem.first, i=0; psys; psys=psys->next, i++) { 00195 if(i == index) 00196 psys->flag |= PSYS_CURRENT; 00197 else 00198 psys->flag &= ~PSYS_CURRENT; 00199 } 00200 } 00201 Object *psys_find_object(Scene *scene, ParticleSystem *psys) 00202 { 00203 Base *base; 00204 ParticleSystem *tpsys; 00205 00206 for(base = scene->base.first; base; base = base->next) { 00207 for(tpsys = base->object->particlesystem.first; psys; psys=psys->next) { 00208 if(tpsys == psys) 00209 return base->object; 00210 } 00211 } 00212 00213 return NULL; 00214 } 00215 Object *psys_get_lattice(ParticleSimulationData *sim) 00216 { 00217 Object *lattice=NULL; 00218 00219 if(psys_in_edit_mode(sim->scene, sim->psys)==0){ 00220 00221 ModifierData *md = (ModifierData*)psys_get_modifier(sim->ob, sim->psys); 00222 00223 for(; md; md=md->next){ 00224 if(md->type==eModifierType_Lattice){ 00225 LatticeModifierData *lmd = (LatticeModifierData *)md; 00226 lattice=lmd->object; 00227 break; 00228 } 00229 } 00230 if(lattice) 00231 init_latt_deform(lattice, NULL); 00232 } 00233 00234 return lattice; 00235 } 00236 void psys_disable_all(Object *ob) 00237 { 00238 ParticleSystem *psys=ob->particlesystem.first; 00239 00240 for(; psys; psys=psys->next) 00241 psys->flag |= PSYS_DISABLED; 00242 } 00243 void psys_enable_all(Object *ob) 00244 { 00245 ParticleSystem *psys=ob->particlesystem.first; 00246 00247 for(; psys; psys=psys->next) 00248 psys->flag &= ~PSYS_DISABLED; 00249 } 00250 int psys_in_edit_mode(Scene *scene, ParticleSystem *psys) 00251 { 00252 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); 00253 } 00254 static void psys_create_frand(ParticleSystem *psys) 00255 { 00256 int i; 00257 float *rand = psys->frand = MEM_callocN(PSYS_FRAND_COUNT * sizeof(float), "particle randoms"); 00258 00259 BLI_srandom(psys->seed); 00260 00261 for(i=0; i<1024; i++, rand++) 00262 *rand = BLI_frand(); 00263 } 00264 int psys_check_enabled(Object *ob, ParticleSystem *psys) 00265 { 00266 ParticleSystemModifierData *psmd; 00267 00268 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE || !psys->part) 00269 return 0; 00270 00271 psmd= psys_get_modifier(ob, psys); 00272 if(psys->renderdata || G.rendering) { 00273 if(!(psmd->modifier.mode & eModifierMode_Render)) 00274 return 0; 00275 } 00276 else if(!(psmd->modifier.mode & eModifierMode_Realtime)) 00277 return 0; 00278 00279 /* perhaps not the perfect place, but we have to be sure the rands are there before usage */ 00280 if(!psys->frand) 00281 psys_create_frand(psys); 00282 else if(psys->recalc & PSYS_RECALC_RESET) { 00283 MEM_freeN(psys->frand); 00284 psys_create_frand(psys); 00285 } 00286 00287 return 1; 00288 } 00289 00290 int psys_check_edited(ParticleSystem *psys) 00291 { 00292 if(psys->part && psys->part->type==PART_HAIR) 00293 return (psys->flag & PSYS_EDITED || (psys->edit && psys->edit->edited)); 00294 else 00295 return (psys->pointcache->edit && psys->pointcache->edit->edited); 00296 } 00297 00298 void psys_check_group_weights(ParticleSettings *part) 00299 { 00300 ParticleDupliWeight *dw, *tdw; 00301 GroupObject *go; 00302 int current = 0; 00303 00304 if(part->ren_as == PART_DRAW_GR && part->dup_group && part->dup_group->gobject.first) { 00305 /* first remove all weights that don't have an object in the group */ 00306 dw = part->dupliweights.first; 00307 while(dw) { 00308 if(!object_in_group(dw->ob, part->dup_group)) { 00309 tdw = dw->next; 00310 BLI_freelinkN(&part->dupliweights, dw); 00311 dw = tdw; 00312 } 00313 else 00314 dw = dw->next; 00315 } 00316 00317 /* then add objects in the group to new list */ 00318 go = part->dup_group->gobject.first; 00319 while(go) { 00320 dw = part->dupliweights.first; 00321 while(dw && dw->ob != go->ob) 00322 dw = dw->next; 00323 00324 if(!dw) { 00325 dw = MEM_callocN(sizeof(ParticleDupliWeight), "ParticleDupliWeight"); 00326 dw->ob = go->ob; 00327 dw->count = 1; 00328 BLI_addtail(&part->dupliweights, dw); 00329 } 00330 00331 go = go->next; 00332 } 00333 00334 dw = part->dupliweights.first; 00335 for(; dw; dw=dw->next) { 00336 if(dw->flag & PART_DUPLIW_CURRENT) { 00337 current = 1; 00338 break; 00339 } 00340 } 00341 00342 if(!current) { 00343 dw = part->dupliweights.first; 00344 if(dw) 00345 dw->flag |= PART_DUPLIW_CURRENT; 00346 } 00347 } 00348 else { 00349 BLI_freelistN(&part->dupliweights); 00350 } 00351 } 00352 int psys_uses_gravity(ParticleSimulationData *sim) 00353 { 00354 return sim->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY && sim->psys->part && sim->psys->part->effector_weights->global_gravity != 0.0f; 00355 } 00356 /************************************************/ 00357 /* Freeing stuff */ 00358 /************************************************/ 00359 static void fluid_free_settings(SPHFluidSettings *fluid) 00360 { 00361 if(fluid) 00362 MEM_freeN(fluid); 00363 } 00364 00365 void psys_free_settings(ParticleSettings *part) 00366 { 00367 MTex *mtex; 00368 int a; 00369 BKE_free_animdata(&part->id); 00370 free_partdeflect(part->pd); 00371 free_partdeflect(part->pd2); 00372 00373 if(part->effector_weights) 00374 MEM_freeN(part->effector_weights); 00375 00376 BLI_freelistN(&part->dupliweights); 00377 00378 boid_free_settings(part->boids); 00379 fluid_free_settings(part->fluid); 00380 00381 for(a=0; a<MAX_MTEX; a++) { 00382 mtex= part->mtex[a]; 00383 if(mtex && mtex->tex) mtex->tex->id.us--; 00384 if(mtex) MEM_freeN(mtex); 00385 } 00386 } 00387 00388 void free_hair(Object *UNUSED(ob), ParticleSystem *psys, int dynamics) 00389 { 00390 PARTICLE_P; 00391 00392 LOOP_PARTICLES { 00393 if(pa->hair) 00394 MEM_freeN(pa->hair); 00395 pa->hair = NULL; 00396 pa->totkey = 0; 00397 } 00398 00399 psys->flag &= ~PSYS_HAIR_DONE; 00400 00401 if(psys->clmd) { 00402 if(dynamics) { 00403 BKE_ptcache_free_list(&psys->ptcaches); 00404 psys->clmd->point_cache = psys->pointcache = NULL; 00405 psys->clmd->ptcaches.first = psys->clmd->ptcaches.last = NULL; 00406 00407 modifier_free((ModifierData*)psys->clmd); 00408 00409 psys->clmd = NULL; 00410 psys->pointcache = BKE_ptcache_add(&psys->ptcaches); 00411 } 00412 else { 00413 cloth_free_modifier(psys->clmd); 00414 } 00415 } 00416 00417 if(psys->hair_in_dm) 00418 psys->hair_in_dm->release(psys->hair_in_dm); 00419 psys->hair_in_dm = NULL; 00420 00421 if(psys->hair_out_dm) 00422 psys->hair_out_dm->release(psys->hair_out_dm); 00423 psys->hair_out_dm = NULL; 00424 } 00425 void free_keyed_keys(ParticleSystem *psys) 00426 { 00427 PARTICLE_P; 00428 00429 if(psys->part->type == PART_HAIR) 00430 return; 00431 00432 if(psys->particles && psys->particles->keys) { 00433 MEM_freeN(psys->particles->keys); 00434 00435 LOOP_PARTICLES { 00436 if(pa->keys) { 00437 pa->keys= NULL; 00438 pa->totkey= 0; 00439 } 00440 } 00441 } 00442 } 00443 static void free_child_path_cache(ParticleSystem *psys) 00444 { 00445 psys_free_path_cache_buffers(psys->childcache, &psys->childcachebufs); 00446 psys->childcache = NULL; 00447 psys->totchildcache = 0; 00448 } 00449 void psys_free_path_cache(ParticleSystem *psys, PTCacheEdit *edit) 00450 { 00451 if(edit) { 00452 psys_free_path_cache_buffers(edit->pathcache, &edit->pathcachebufs); 00453 edit->pathcache= NULL; 00454 edit->totcached= 0; 00455 } 00456 if(psys) { 00457 psys_free_path_cache_buffers(psys->pathcache, &psys->pathcachebufs); 00458 psys->pathcache= NULL; 00459 psys->totcached= 0; 00460 00461 free_child_path_cache(psys); 00462 } 00463 } 00464 void psys_free_children(ParticleSystem *psys) 00465 { 00466 if(psys->child) { 00467 MEM_freeN(psys->child); 00468 psys->child= NULL; 00469 psys->totchild=0; 00470 } 00471 00472 free_child_path_cache(psys); 00473 } 00474 void psys_free_particles(ParticleSystem *psys) 00475 { 00476 PARTICLE_P; 00477 00478 if(psys->particles) { 00479 if(psys->part->type==PART_HAIR) { 00480 LOOP_PARTICLES { 00481 if(pa->hair) 00482 MEM_freeN(pa->hair); 00483 } 00484 } 00485 00486 if(psys->particles->keys) 00487 MEM_freeN(psys->particles->keys); 00488 00489 if(psys->particles->boid) 00490 MEM_freeN(psys->particles->boid); 00491 00492 MEM_freeN(psys->particles); 00493 psys->particles= NULL; 00494 psys->totpart= 0; 00495 } 00496 } 00497 void psys_free_pdd(ParticleSystem *psys) 00498 { 00499 if(psys->pdd) { 00500 if(psys->pdd->cdata) 00501 MEM_freeN(psys->pdd->cdata); 00502 psys->pdd->cdata = NULL; 00503 00504 if(psys->pdd->vdata) 00505 MEM_freeN(psys->pdd->vdata); 00506 psys->pdd->vdata = NULL; 00507 00508 if(psys->pdd->ndata) 00509 MEM_freeN(psys->pdd->ndata); 00510 psys->pdd->ndata = NULL; 00511 00512 if(psys->pdd->vedata) 00513 MEM_freeN(psys->pdd->vedata); 00514 psys->pdd->vedata = NULL; 00515 00516 psys->pdd->totpoint = 0; 00517 psys->pdd->tot_vec_size = 0; 00518 } 00519 } 00520 /* free everything */ 00521 void psys_free(Object *ob, ParticleSystem * psys) 00522 { 00523 if(psys){ 00524 int nr = 0; 00525 ParticleSystem * tpsys; 00526 00527 psys_free_path_cache(psys, NULL); 00528 00529 free_hair(ob, psys, 1); 00530 00531 psys_free_particles(psys); 00532 00533 if(psys->edit && psys->free_edit) 00534 psys->free_edit(psys->edit); 00535 00536 if(psys->child){ 00537 MEM_freeN(psys->child); 00538 psys->child = NULL; 00539 psys->totchild = 0; 00540 } 00541 00542 // check if we are last non-visible particle system 00543 for(tpsys=ob->particlesystem.first; tpsys; tpsys=tpsys->next){ 00544 if(tpsys->part) 00545 { 00546 if(ELEM(tpsys->part->ren_as,PART_DRAW_OB,PART_DRAW_GR)) 00547 { 00548 nr++; 00549 break; 00550 } 00551 } 00552 } 00553 // clear do-not-draw-flag 00554 if(!nr) 00555 ob->transflag &= ~OB_DUPLIPARTS; 00556 00557 if(psys->part){ 00558 psys->part->id.us--; 00559 psys->part=NULL; 00560 } 00561 00562 BKE_ptcache_free_list(&psys->ptcaches); 00563 psys->pointcache = NULL; 00564 00565 BLI_freelistN(&psys->targets); 00566 00567 BLI_bvhtree_free(psys->bvhtree); 00568 BLI_kdtree_free(psys->tree); 00569 00570 if(psys->fluid_springs) 00571 MEM_freeN(psys->fluid_springs); 00572 00573 pdEndEffectors(&psys->effectors); 00574 00575 if(psys->frand) 00576 MEM_freeN(psys->frand); 00577 00578 if(psys->pdd) { 00579 psys_free_pdd(psys); 00580 MEM_freeN(psys->pdd); 00581 } 00582 00583 MEM_freeN(psys); 00584 } 00585 } 00586 00587 /************************************************/ 00588 /* Rendering */ 00589 /************************************************/ 00590 /* these functions move away particle data and bring it back after 00591 * rendering, to make different render settings possible without 00592 * removing the previous data. this should be solved properly once */ 00593 00594 typedef struct ParticleRenderElem { 00595 int curchild, totchild, reduce; 00596 float lambda, t, scalemin, scalemax; 00597 } ParticleRenderElem; 00598 00599 typedef struct ParticleRenderData { 00600 ChildParticle *child; 00601 ParticleCacheKey **pathcache; 00602 ParticleCacheKey **childcache; 00603 ListBase pathcachebufs, childcachebufs; 00604 int totchild, totcached, totchildcache; 00605 DerivedMesh *dm; 00606 int totdmvert, totdmedge, totdmface; 00607 00608 float mat[4][4]; 00609 float viewmat[4][4], winmat[4][4]; 00610 int winx, winy; 00611 00612 int dosimplify; 00613 int timeoffset; 00614 ParticleRenderElem *elems; 00615 int *origindex; 00616 } ParticleRenderData; 00617 00618 static float psys_render_viewport_falloff(double rate, float dist, float width) 00619 { 00620 return pow(rate, dist/width); 00621 } 00622 00623 static float psys_render_projected_area(ParticleSystem *psys, float *center, float area, double vprate, float *viewport) 00624 { 00625 ParticleRenderData *data= psys->renderdata; 00626 float co[4], view[3], ortho1[3], ortho2[3], w, dx, dy, radius; 00627 00628 /* transform to view space */ 00629 VECCOPY(co, center); 00630 co[3]= 1.0f; 00631 mul_m4_v4(data->viewmat, co); 00632 00633 /* compute two vectors orthogonal to view vector */ 00634 normalize_v3_v3(view, co); 00635 ortho_basis_v3v3_v3( ortho1, ortho2,view); 00636 00637 /* compute on screen minification */ 00638 w= co[2]*data->winmat[2][3] + data->winmat[3][3]; 00639 dx= data->winx*ortho2[0]*data->winmat[0][0]; 00640 dy= data->winy*ortho2[1]*data->winmat[1][1]; 00641 w= sqrt(dx*dx + dy*dy)/w; 00642 00643 /* w squared because we are working with area */ 00644 area= area*w*w; 00645 00646 /* viewport of the screen test */ 00647 00648 /* project point on screen */ 00649 mul_m4_v4(data->winmat, co); 00650 if(co[3] != 0.0f) { 00651 co[0]= 0.5f*data->winx*(1.0f + co[0]/co[3]); 00652 co[1]= 0.5f*data->winy*(1.0f + co[1]/co[3]); 00653 } 00654 00655 /* screen space radius */ 00656 radius= sqrt(area/(float)M_PI); 00657 00658 /* make smaller using fallof once over screen edge */ 00659 *viewport= 1.0f; 00660 00661 if(co[0]+radius < 0.0f) 00662 *viewport *= psys_render_viewport_falloff(vprate, -(co[0]+radius), data->winx); 00663 else if(co[0]-radius > data->winx) 00664 *viewport *= psys_render_viewport_falloff(vprate, (co[0]-radius) - data->winx, data->winx); 00665 00666 if(co[1]+radius < 0.0f) 00667 *viewport *= psys_render_viewport_falloff(vprate, -(co[1]+radius), data->winy); 00668 else if(co[1]-radius > data->winy) 00669 *viewport *= psys_render_viewport_falloff(vprate, (co[1]-radius) - data->winy, data->winy); 00670 00671 return area; 00672 } 00673 00674 void psys_render_set(Object *ob, ParticleSystem *psys, float viewmat[][4], float winmat[][4], int winx, int winy, int timeoffset) 00675 { 00676 ParticleRenderData*data; 00677 ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys); 00678 00679 if(!G.rendering) 00680 return; 00681 if(psys->renderdata) 00682 return; 00683 00684 data= MEM_callocN(sizeof(ParticleRenderData), "ParticleRenderData"); 00685 00686 data->child= psys->child; 00687 data->totchild= psys->totchild; 00688 data->pathcache= psys->pathcache; 00689 data->pathcachebufs.first = psys->pathcachebufs.first; 00690 data->pathcachebufs.last = psys->pathcachebufs.last; 00691 data->totcached= psys->totcached; 00692 data->childcache= psys->childcache; 00693 data->childcachebufs.first = psys->childcachebufs.first; 00694 data->childcachebufs.last = psys->childcachebufs.last; 00695 data->totchildcache= psys->totchildcache; 00696 00697 if(psmd->dm) 00698 data->dm= CDDM_copy(psmd->dm); 00699 data->totdmvert= psmd->totdmvert; 00700 data->totdmedge= psmd->totdmedge; 00701 data->totdmface= psmd->totdmface; 00702 00703 psys->child= NULL; 00704 psys->pathcache= NULL; 00705 psys->childcache= NULL; 00706 psys->totchild= psys->totcached= psys->totchildcache= 0; 00707 psys->pathcachebufs.first = psys->pathcachebufs.last = NULL; 00708 psys->childcachebufs.first = psys->childcachebufs.last = NULL; 00709 00710 copy_m4_m4(data->winmat, winmat); 00711 mul_m4_m4m4(data->viewmat, ob->obmat, viewmat); 00712 mul_m4_m4m4(data->mat, data->viewmat, winmat); 00713 data->winx= winx; 00714 data->winy= winy; 00715 00716 data->timeoffset= timeoffset; 00717 00718 psys->renderdata= data; 00719 00720 /* Hair can and has to be recalculated if everything isn't displayed. */ 00721 if(psys->part->disp != 100 && psys->part->type == PART_HAIR) 00722 psys->recalc |= PSYS_RECALC_RESET; 00723 } 00724 00725 void psys_render_restore(Object *ob, ParticleSystem *psys) 00726 { 00727 ParticleRenderData*data; 00728 ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys); 00729 00730 data= psys->renderdata; 00731 if(!data) 00732 return; 00733 00734 if(data->elems) 00735 MEM_freeN(data->elems); 00736 00737 if(psmd->dm) { 00738 psmd->dm->needsFree= 1; 00739 psmd->dm->release(psmd->dm); 00740 } 00741 00742 psys_free_path_cache(psys, NULL); 00743 00744 if(psys->child){ 00745 MEM_freeN(psys->child); 00746 psys->child= 0; 00747 psys->totchild= 0; 00748 } 00749 00750 psys->child= data->child; 00751 psys->totchild= data->totchild; 00752 psys->pathcache= data->pathcache; 00753 psys->pathcachebufs.first = data->pathcachebufs.first; 00754 psys->pathcachebufs.last = data->pathcachebufs.last; 00755 psys->totcached= data->totcached; 00756 psys->childcache= data->childcache; 00757 psys->childcachebufs.first = data->childcachebufs.first; 00758 psys->childcachebufs.last = data->childcachebufs.last; 00759 psys->totchildcache= data->totchildcache; 00760 00761 psmd->dm= data->dm; 00762 psmd->totdmvert= data->totdmvert; 00763 psmd->totdmedge= data->totdmedge; 00764 psmd->totdmface= data->totdmface; 00765 psmd->flag &= ~eParticleSystemFlag_psys_updated; 00766 00767 if(psmd->dm) 00768 psys_calc_dmcache(ob, psmd->dm, psys); 00769 00770 MEM_freeN(data); 00771 psys->renderdata= NULL; 00772 } 00773 00774 int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot) 00775 { 00776 DerivedMesh *dm= ctx->dm; 00777 Mesh *me= (Mesh*)(ctx->sim.ob->data); 00778 MFace *mf, *mface; 00779 MVert *mvert; 00780 ParticleRenderData *data; 00781 ParticleRenderElem *elems, *elem; 00782 ParticleSettings *part= ctx->sim.psys->part; 00783 float *facearea, (*facecenter)[3], size[3], fac, powrate, scaleclamp; 00784 float co1[3], co2[3], co3[3], co4[3], lambda, arearatio, t, area, viewport; 00785 double vprate; 00786 int *origindex, *facetotvert; 00787 int a, b, totorigface, totface, newtot, skipped; 00788 00789 if(part->ren_as!=PART_DRAW_PATH || !(part->draw & PART_DRAW_REN_STRAND)) 00790 return tot; 00791 if(!ctx->sim.psys->renderdata) 00792 return tot; 00793 00794 data= ctx->sim.psys->renderdata; 00795 if(data->timeoffset) 00796 return 0; 00797 if(!(part->simplify_flag & PART_SIMPLIFY_ENABLE)) 00798 return tot; 00799 00800 mvert= dm->getVertArray(dm); 00801 mface= dm->getFaceArray(dm); 00802 origindex= dm->getFaceDataArray(dm, CD_ORIGINDEX); 00803 totface= dm->getNumFaces(dm); 00804 totorigface= me->totface; 00805 00806 if(totface == 0 || totorigface == 0) 00807 return tot; 00808 00809 facearea= MEM_callocN(sizeof(float)*totorigface, "SimplifyFaceArea"); 00810 facecenter= MEM_callocN(sizeof(float[3])*totorigface, "SimplifyFaceCenter"); 00811 facetotvert= MEM_callocN(sizeof(int)*totorigface, "SimplifyFaceArea"); 00812 elems= MEM_callocN(sizeof(ParticleRenderElem)*totorigface, "SimplifyFaceElem"); 00813 00814 if(data->elems) 00815 MEM_freeN(data->elems); 00816 00817 data->dosimplify= 1; 00818 data->elems= elems; 00819 data->origindex= origindex; 00820 00821 /* compute number of children per original face */ 00822 for(a=0; a<tot; a++) { 00823 b= (origindex)? origindex[ctx->index[a]]: ctx->index[a]; 00824 if(b != -1) 00825 elems[b].totchild++; 00826 } 00827 00828 /* compute areas and centers of original faces */ 00829 for(mf=mface, a=0; a<totface; a++, mf++) { 00830 b= (origindex)? origindex[a]: a; 00831 00832 if(b != -1) { 00833 VECCOPY(co1, mvert[mf->v1].co); 00834 VECCOPY(co2, mvert[mf->v2].co); 00835 VECCOPY(co3, mvert[mf->v3].co); 00836 00837 VECADD(facecenter[b], facecenter[b], co1); 00838 VECADD(facecenter[b], facecenter[b], co2); 00839 VECADD(facecenter[b], facecenter[b], co3); 00840 00841 if(mf->v4) { 00842 VECCOPY(co4, mvert[mf->v4].co); 00843 VECADD(facecenter[b], facecenter[b], co4); 00844 facearea[b] += area_quad_v3(co1, co2, co3, co4); 00845 facetotvert[b] += 4; 00846 } 00847 else { 00848 facearea[b] += area_tri_v3(co1, co2, co3); 00849 facetotvert[b] += 3; 00850 } 00851 } 00852 } 00853 00854 for(a=0; a<totorigface; a++) 00855 if(facetotvert[a] > 0) 00856 mul_v3_fl(facecenter[a], 1.0f/facetotvert[a]); 00857 00858 /* for conversion from BU area / pixel area to reference screen size */ 00859 mesh_get_texspace(me, 0, 0, size); 00860 fac= ((size[0] + size[1] + size[2])/3.0f)/part->simplify_refsize; 00861 fac= fac*fac; 00862 00863 powrate= log(0.5f)/log(part->simplify_rate*0.5f); 00864 if(part->simplify_flag & PART_SIMPLIFY_VIEWPORT) 00865 vprate= pow(1.0f - part->simplify_viewport, 5.0); 00866 else 00867 vprate= 1.0; 00868 00869 /* set simplification parameters per original face */ 00870 for(a=0, elem=elems; a<totorigface; a++, elem++) { 00871 area = psys_render_projected_area(ctx->sim.psys, facecenter[a], facearea[a], vprate, &viewport); 00872 arearatio= fac*area/facearea[a]; 00873 00874 if((arearatio < 1.0f || viewport < 1.0f) && elem->totchild) { 00875 /* lambda is percentage of elements to keep */ 00876 lambda= (arearatio < 1.0f)? powf(arearatio, powrate): 1.0f; 00877 lambda *= viewport; 00878 00879 lambda= MAX2(lambda, 1.0f/elem->totchild); 00880 00881 /* compute transition region */ 00882 t= part->simplify_transition; 00883 elem->t= (lambda-t < 0.0f)? lambda: (lambda+t > 1.0f)? 1.0f-lambda: t; 00884 elem->reduce= 1; 00885 00886 /* scale at end and beginning of the transition region */ 00887 elem->scalemax= (lambda+t < 1.0f)? 1.0f/lambda: 1.0f/(1.0f - elem->t*elem->t/t); 00888 elem->scalemin= (lambda+t < 1.0f)? 0.0f: elem->scalemax*(1.0f-elem->t/t); 00889 00890 elem->scalemin= sqrt(elem->scalemin); 00891 elem->scalemax= sqrt(elem->scalemax); 00892 00893 /* clamp scaling */ 00894 scaleclamp= MIN2(elem->totchild, 10.0f); 00895 elem->scalemin= MIN2(scaleclamp, elem->scalemin); 00896 elem->scalemax= MIN2(scaleclamp, elem->scalemax); 00897 00898 /* extend lambda to include transition */ 00899 lambda= lambda + elem->t; 00900 if(lambda > 1.0f) 00901 lambda= 1.0f; 00902 } 00903 else { 00904 lambda= arearatio; 00905 00906 elem->scalemax= 1.0f; //sqrt(lambda); 00907 elem->scalemin= 1.0f; //sqrt(lambda); 00908 elem->reduce= 0; 00909 } 00910 00911 elem->lambda= lambda; 00912 elem->scalemin= sqrt(elem->scalemin); 00913 elem->scalemax= sqrt(elem->scalemax); 00914 elem->curchild= 0; 00915 } 00916 00917 MEM_freeN(facearea); 00918 MEM_freeN(facecenter); 00919 MEM_freeN(facetotvert); 00920 00921 /* move indices and set random number skipping */ 00922 ctx->skip= MEM_callocN(sizeof(int)*tot, "SimplificationSkip"); 00923 00924 skipped= 0; 00925 for(a=0, newtot=0; a<tot; a++) { 00926 b= (origindex)? origindex[ctx->index[a]]: ctx->index[a]; 00927 if(b != -1) { 00928 if(elems[b].curchild++ < ceil(elems[b].lambda*elems[b].totchild)) { 00929 ctx->index[newtot]= ctx->index[a]; 00930 ctx->skip[newtot]= skipped; 00931 skipped= 0; 00932 newtot++; 00933 } 00934 else skipped++; 00935 } 00936 else skipped++; 00937 } 00938 00939 for(a=0, elem=elems; a<totorigface; a++, elem++) 00940 elem->curchild= 0; 00941 00942 return newtot; 00943 } 00944 00945 int psys_render_simplify_params(ParticleSystem *psys, ChildParticle *cpa, float *params) 00946 { 00947 ParticleRenderData *data; 00948 ParticleRenderElem *elem; 00949 float x, w, scale, alpha, lambda, t, scalemin, scalemax; 00950 int b; 00951 00952 if(!(psys->renderdata && (psys->part->simplify_flag & PART_SIMPLIFY_ENABLE))) 00953 return 0; 00954 00955 data= psys->renderdata; 00956 if(!data->dosimplify) 00957 return 0; 00958 00959 b= (data->origindex)? data->origindex[cpa->num]: cpa->num; 00960 if(b == -1) 00961 return 0; 00962 00963 elem= &data->elems[b]; 00964 00965 lambda= elem->lambda; 00966 t= elem->t; 00967 scalemin= elem->scalemin; 00968 scalemax= elem->scalemax; 00969 00970 if(!elem->reduce) { 00971 scale= scalemin; 00972 alpha= 1.0f; 00973 } 00974 else { 00975 x= (elem->curchild+0.5f)/elem->totchild; 00976 if(x < lambda-t) { 00977 scale= scalemax; 00978 alpha= 1.0f; 00979 } 00980 else if(x >= lambda+t) { 00981 scale= scalemin; 00982 alpha= 0.0f; 00983 } 00984 else { 00985 w= (lambda+t - x)/(2.0f*t); 00986 scale= scalemin + (scalemax - scalemin)*w; 00987 alpha= w; 00988 } 00989 } 00990 00991 params[0]= scale; 00992 params[1]= alpha; 00993 00994 elem->curchild++; 00995 00996 return 1; 00997 } 00998 00999 /************************************************/ 01000 /* Interpolation */ 01001 /************************************************/ 01002 static float interpolate_particle_value(float v1, float v2, float v3, float v4, float *w, int four) 01003 { 01004 float value; 01005 01006 value= w[0]*v1 + w[1]*v2 + w[2]*v3; 01007 if(four) 01008 value += w[3]*v4; 01009 01010 CLAMP(value, 0.f, 1.f); 01011 01012 return value; 01013 } 01014 01015 void psys_interpolate_particle(short type, ParticleKey keys[4], float dt, ParticleKey *result, int velocity) 01016 { 01017 float t[4]; 01018 01019 if(type<0) { 01020 interp_cubic_v3( result->co, result->vel,keys[1].co, keys[1].vel, keys[2].co, keys[2].vel, dt); 01021 } 01022 else { 01023 key_curve_position_weights(dt, t, type); 01024 01025 interp_v3_v3v3v3v3(result->co, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t); 01026 01027 if(velocity){ 01028 float temp[3]; 01029 01030 if(dt>0.999f){ 01031 key_curve_position_weights(dt-0.001f, t, type); 01032 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t); 01033 VECSUB(result->vel, result->co, temp); 01034 } 01035 else{ 01036 key_curve_position_weights(dt+0.001f, t, type); 01037 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t); 01038 VECSUB(result->vel, temp, result->co); 01039 } 01040 } 01041 } 01042 } 01043 01044 01045 01046 typedef struct ParticleInterpolationData { 01047 HairKey *hkey[2]; 01048 01049 DerivedMesh *dm; 01050 MVert *mvert[2]; 01051 01052 int keyed; 01053 ParticleKey *kkey[2]; 01054 01055 PointCache *cache; 01056 PTCacheMem *pm; 01057 01058 PTCacheEditPoint *epoint; 01059 PTCacheEditKey *ekey[2]; 01060 01061 float birthtime, dietime; 01062 int bspline; 01063 } ParticleInterpolationData; 01064 /* Assumes pointcache->mem_cache exists, so for disk cached particles call psys_make_temp_pointcache() before use */ 01065 /* It uses ParticleInterpolationData->pm to store the current memory cache frame so it's thread safe. */ 01066 static void get_pointcache_keys_for_time(Object *UNUSED(ob), PointCache *cache, PTCacheMem **cur, int index, float t, ParticleKey *key1, ParticleKey *key2) 01067 { 01068 static PTCacheMem *pm = NULL; 01069 int index1, index2; 01070 01071 if(index < 0) { /* initialize */ 01072 *cur = cache->mem_cache.first; 01073 01074 if(*cur) 01075 *cur = (*cur)->next; 01076 } 01077 else { 01078 if(*cur) { 01079 while(*cur && (*cur)->next && (float)(*cur)->frame < t) 01080 *cur = (*cur)->next; 01081 01082 pm = *cur; 01083 01084 index2 = BKE_ptcache_mem_index_find(pm, index); 01085 index1 = BKE_ptcache_mem_index_find(pm->prev, index); 01086 01087 BKE_ptcache_make_particle_key(key2, index2, pm->data, (float)pm->frame); 01088 if(index1 < 0) 01089 copy_particle_key(key1, key2, 1); 01090 else 01091 BKE_ptcache_make_particle_key(key1, index1, pm->prev->data, (float)pm->prev->frame); 01092 } 01093 else if(cache->mem_cache.first) { 01094 pm = cache->mem_cache.first; 01095 index2 = BKE_ptcache_mem_index_find(pm, index); 01096 BKE_ptcache_make_particle_key(key2, index2, pm->data, (float)pm->frame); 01097 copy_particle_key(key1, key2, 1); 01098 } 01099 } 01100 } 01101 static int get_pointcache_times_for_particle(PointCache *cache, int index, float *start, float *end) 01102 { 01103 PTCacheMem *pm; 01104 int ret = 0; 01105 01106 for(pm=cache->mem_cache.first; pm; pm=pm->next) { 01107 if(BKE_ptcache_mem_index_find(pm, index) >= 0) { 01108 *start = pm->frame; 01109 ret++; 01110 break; 01111 } 01112 } 01113 01114 for(pm=cache->mem_cache.last; pm; pm=pm->prev) { 01115 if(BKE_ptcache_mem_index_find(pm, index) >= 0) { 01116 *end = pm->frame; 01117 ret++; 01118 break; 01119 } 01120 } 01121 01122 return ret == 2; 01123 } 01124 01125 float psys_get_dietime_from_cache(PointCache *cache, int index) { 01126 PTCacheMem *pm; 01127 int dietime = 10000000; /* some max value so that we can default to pa->time+lifetime */ 01128 01129 for(pm=cache->mem_cache.last; pm; pm=pm->prev) { 01130 if(BKE_ptcache_mem_index_find(pm, index) >= 0) 01131 return (float)pm->frame; 01132 } 01133 01134 return (float)dietime; 01135 } 01136 01137 static void init_particle_interpolation(Object *ob, ParticleSystem *psys, ParticleData *pa, ParticleInterpolationData *pind) 01138 { 01139 01140 if(pind->epoint) { 01141 PTCacheEditPoint *point = pind->epoint; 01142 01143 pind->ekey[0] = point->keys; 01144 pind->ekey[1] = point->totkey > 1 ? point->keys + 1 : NULL; 01145 01146 pind->birthtime = *(point->keys->time); 01147 pind->dietime = *((point->keys + point->totkey - 1)->time); 01148 } 01149 else if(pind->keyed) { 01150 ParticleKey *key = pa->keys; 01151 pind->kkey[0] = key; 01152 pind->kkey[1] = pa->totkey > 1 ? key + 1 : NULL; 01153 01154 pind->birthtime = key->time; 01155 pind->dietime = (key + pa->totkey - 1)->time; 01156 } 01157 else if(pind->cache) { 01158 float start=0.0f, end=0.0f; 01159 get_pointcache_keys_for_time(ob, pind->cache, &pind->pm, -1, 0.0f, NULL, NULL); 01160 pind->birthtime = pa ? pa->time : pind->cache->startframe; 01161 pind->dietime = pa ? pa->dietime : pind->cache->endframe; 01162 01163 if(get_pointcache_times_for_particle(pind->cache, pa - psys->particles, &start, &end)) { 01164 pind->birthtime = MAX2(pind->birthtime, start); 01165 pind->dietime = MIN2(pind->dietime, end); 01166 } 01167 } 01168 else { 01169 HairKey *key = pa->hair; 01170 pind->hkey[0] = key; 01171 pind->hkey[1] = key + 1; 01172 01173 pind->birthtime = key->time; 01174 pind->dietime = (key + pa->totkey - 1)->time; 01175 01176 if(pind->dm) { 01177 pind->mvert[0] = CDDM_get_vert(pind->dm, pa->hair_index); 01178 pind->mvert[1] = pind->mvert[0] + 1; 01179 } 01180 } 01181 } 01182 static void edit_to_particle(ParticleKey *key, PTCacheEditKey *ekey) 01183 { 01184 VECCOPY(key->co, ekey->co); 01185 if(ekey->vel) { 01186 VECCOPY(key->vel, ekey->vel); 01187 } 01188 key->time = *(ekey->time); 01189 } 01190 static void hair_to_particle(ParticleKey *key, HairKey *hkey) 01191 { 01192 VECCOPY(key->co, hkey->co); 01193 key->time = hkey->time; 01194 } 01195 01196 static void mvert_to_particle(ParticleKey *key, MVert *mvert, HairKey *hkey) 01197 { 01198 VECCOPY(key->co, mvert->co); 01199 key->time = hkey->time; 01200 } 01201 01202 static void do_particle_interpolation(ParticleSystem *psys, int p, ParticleData *pa, float t, ParticleInterpolationData *pind, ParticleKey *result) 01203 { 01204 PTCacheEditPoint *point = pind->epoint; 01205 ParticleKey keys[4]; 01206 int point_vel = (point && point->keys->vel); 01207 float real_t, dfra, keytime, invdt = 1.f; 01208 01209 /* billboards wont fill in all of these, so start cleared */ 01210 memset(keys, 0, sizeof(keys)); 01211 01212 /* interpret timing and find keys */ 01213 if(point) { 01214 if(result->time < 0.0f) 01215 real_t = -result->time; 01216 else 01217 real_t = *(pind->ekey[0]->time) + t * (*(pind->ekey[0][point->totkey-1].time) - *(pind->ekey[0]->time)); 01218 01219 while(*(pind->ekey[1]->time) < real_t) 01220 pind->ekey[1]++; 01221 01222 pind->ekey[0] = pind->ekey[1] - 1; 01223 } 01224 else if(pind->keyed) { 01225 /* we have only one key, so let's use that */ 01226 if(pind->kkey[1]==NULL) { 01227 copy_particle_key(result, pind->kkey[0], 1); 01228 return; 01229 } 01230 01231 if(result->time < 0.0f) 01232 real_t = -result->time; 01233 else 01234 real_t = pind->kkey[0]->time + t * (pind->kkey[0][pa->totkey-1].time - pind->kkey[0]->time); 01235 01236 if(psys->part->phystype==PART_PHYS_KEYED && psys->flag & PSYS_KEYED_TIMING) { 01237 ParticleTarget *pt = psys->targets.first; 01238 01239 pt=pt->next; 01240 01241 while(pt && pa->time + pt->time < real_t) 01242 pt= pt->next; 01243 01244 if(pt) { 01245 pt=pt->prev; 01246 01247 if(pa->time + pt->time + pt->duration > real_t) 01248 real_t = pa->time + pt->time; 01249 } 01250 else 01251 real_t = pa->time + ((ParticleTarget*)psys->targets.last)->time; 01252 } 01253 01254 CLAMP(real_t, pa->time, pa->dietime); 01255 01256 while(pind->kkey[1]->time < real_t) 01257 pind->kkey[1]++; 01258 01259 pind->kkey[0] = pind->kkey[1] - 1; 01260 } 01261 else if(pind->cache) { 01262 if(result->time < 0.0f) /* flag for time in frames */ 01263 real_t = -result->time; 01264 else 01265 real_t = pa->time + t * (pa->dietime - pa->time); 01266 } 01267 else { 01268 if(result->time < 0.0f) 01269 real_t = -result->time; 01270 else 01271 real_t = pind->hkey[0]->time + t * (pind->hkey[0][pa->totkey-1].time - pind->hkey[0]->time); 01272 01273 while(pind->hkey[1]->time < real_t) { 01274 pind->hkey[1]++; 01275 pind->mvert[1]++; 01276 } 01277 01278 pind->hkey[0] = pind->hkey[1] - 1; 01279 } 01280 01281 /* set actual interpolation keys */ 01282 if(point) { 01283 edit_to_particle(keys + 1, pind->ekey[0]); 01284 edit_to_particle(keys + 2, pind->ekey[1]); 01285 } 01286 else if(pind->dm) { 01287 pind->mvert[0] = pind->mvert[1] - 1; 01288 mvert_to_particle(keys + 1, pind->mvert[0], pind->hkey[0]); 01289 mvert_to_particle(keys + 2, pind->mvert[1], pind->hkey[1]); 01290 } 01291 else if(pind->keyed) { 01292 memcpy(keys + 1, pind->kkey[0], sizeof(ParticleKey)); 01293 memcpy(keys + 2, pind->kkey[1], sizeof(ParticleKey)); 01294 } 01295 else if(pind->cache) { 01296 get_pointcache_keys_for_time(NULL, pind->cache, &pind->pm, p, real_t, keys+1, keys+2); 01297 } 01298 else { 01299 hair_to_particle(keys + 1, pind->hkey[0]); 01300 hair_to_particle(keys + 2, pind->hkey[1]); 01301 } 01302 01303 /* set secondary interpolation keys for hair */ 01304 if(!pind->keyed && !pind->cache && !point_vel) { 01305 if(point) { 01306 if(pind->ekey[0] != point->keys) 01307 edit_to_particle(keys, pind->ekey[0] - 1); 01308 else 01309 edit_to_particle(keys, pind->ekey[0]); 01310 } 01311 else if(pind->dm) { 01312 if(pind->hkey[0] != pa->hair) 01313 mvert_to_particle(keys, pind->mvert[0] - 1, pind->hkey[0] - 1); 01314 else 01315 mvert_to_particle(keys, pind->mvert[0], pind->hkey[0]); 01316 } 01317 else { 01318 if(pind->hkey[0] != pa->hair) 01319 hair_to_particle(keys, pind->hkey[0] - 1); 01320 else 01321 hair_to_particle(keys, pind->hkey[0]); 01322 } 01323 01324 if(point) { 01325 if(pind->ekey[1] != point->keys + point->totkey - 1) 01326 edit_to_particle(keys + 3, pind->ekey[1] + 1); 01327 else 01328 edit_to_particle(keys + 3, pind->ekey[1]); 01329 } 01330 else if(pind->dm) { 01331 if(pind->hkey[1] != pa->hair + pa->totkey - 1) 01332 mvert_to_particle(keys + 3, pind->mvert[1] + 1, pind->hkey[1] + 1); 01333 else 01334 mvert_to_particle(keys + 3, pind->mvert[1], pind->hkey[1]); 01335 } 01336 else { 01337 if(pind->hkey[1] != pa->hair + pa->totkey - 1) 01338 hair_to_particle(keys + 3, pind->hkey[1] + 1); 01339 else 01340 hair_to_particle(keys + 3, pind->hkey[1]); 01341 } 01342 } 01343 01344 dfra = keys[2].time - keys[1].time; 01345 keytime = (real_t - keys[1].time) / dfra; 01346 01347 /* convert velocity to timestep size */ 01348 if(pind->keyed || pind->cache || point_vel){ 01349 invdt = dfra * 0.04f * (psys ? psys->part->timetweak : 1.f); 01350 mul_v3_fl(keys[1].vel, invdt); 01351 mul_v3_fl(keys[2].vel, invdt); 01352 interp_qt_qtqt(result->rot,keys[1].rot,keys[2].rot,keytime); 01353 } 01354 01355 /* 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)*/ 01356 psys_interpolate_particle((pind->keyed || pind->cache || point_vel) ? -1 /* signal for cubic interpolation */ 01357 : (pind->bspline ? KEY_BSPLINE : KEY_CARDINAL) 01358 ,keys, keytime, result, 1); 01359 01360 /* the velocity needs to be converted back from cubic interpolation */ 01361 if(pind->keyed || pind->cache || point_vel) 01362 mul_v3_fl(result->vel, 1.f/invdt); 01363 } 01364 /************************************************/ 01365 /* Particles on a dm */ 01366 /************************************************/ 01367 /* interpolate a location on a face based on face coordinates */ 01368 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){ 01369 float *v1=0, *v2=0, *v3=0, *v4=0; 01370 float e1[3],e2[3],s1,s2,t1,t2; 01371 float *uv1, *uv2, *uv3, *uv4; 01372 float n1[3], n2[3], n3[3], n4[3]; 01373 float tuv[4][2]; 01374 float *o1, *o2, *o3, *o4; 01375 01376 v1= mvert[mface->v1].co; 01377 v2= mvert[mface->v2].co; 01378 v3= mvert[mface->v3].co; 01379 01380 normal_short_to_float_v3(n1, mvert[mface->v1].no); 01381 normal_short_to_float_v3(n2, mvert[mface->v2].no); 01382 normal_short_to_float_v3(n3, mvert[mface->v3].no); 01383 01384 if(mface->v4) { 01385 v4= mvert[mface->v4].co; 01386 normal_short_to_float_v3(n4, mvert[mface->v4].no); 01387 01388 interp_v3_v3v3v3v3(vec, v1, v2, v3, v4, w); 01389 01390 if(nor){ 01391 if(mface->flag & ME_SMOOTH) 01392 interp_v3_v3v3v3v3(nor, n1, n2, n3, n4, w); 01393 else 01394 normal_quad_v3(nor,v1,v2,v3,v4); 01395 } 01396 } 01397 else { 01398 interp_v3_v3v3v3(vec, v1, v2, v3, w); 01399 01400 if(nor){ 01401 if(mface->flag & ME_SMOOTH) 01402 interp_v3_v3v3v3(nor, n1, n2, n3, w); 01403 else 01404 normal_tri_v3(nor,v1,v2,v3); 01405 } 01406 } 01407 01408 /* calculate tangent vectors */ 01409 if(utan && vtan){ 01410 if(tface){ 01411 uv1= tface->uv[0]; 01412 uv2= tface->uv[1]; 01413 uv3= tface->uv[2]; 01414 uv4= tface->uv[3]; 01415 } 01416 else{ 01417 uv1= tuv[0]; uv2= tuv[1]; uv3= tuv[2]; uv4= tuv[3]; 01418 map_to_sphere( uv1, uv1+1,v1[0], v1[1], v1[2]); 01419 map_to_sphere( uv2, uv2+1,v2[0], v2[1], v2[2]); 01420 map_to_sphere( uv3, uv3+1,v3[0], v3[1], v3[2]); 01421 if(v4) 01422 map_to_sphere( uv4, uv4+1,v4[0], v4[1], v4[2]); 01423 } 01424 01425 if(v4){ 01426 s1= uv3[0] - uv1[0]; 01427 s2= uv4[0] - uv1[0]; 01428 01429 t1= uv3[1] - uv1[1]; 01430 t2= uv4[1] - uv1[1]; 01431 01432 sub_v3_v3v3(e1, v3, v1); 01433 sub_v3_v3v3(e2, v4, v1); 01434 } 01435 else{ 01436 s1= uv2[0] - uv1[0]; 01437 s2= uv3[0] - uv1[0]; 01438 01439 t1= uv2[1] - uv1[1]; 01440 t2= uv3[1] - uv1[1]; 01441 01442 sub_v3_v3v3(e1, v2, v1); 01443 sub_v3_v3v3(e2, v3, v1); 01444 } 01445 01446 vtan[0] = (s1*e2[0] - s2*e1[0]); 01447 vtan[1] = (s1*e2[1] - s2*e1[1]); 01448 vtan[2] = (s1*e2[2] - s2*e1[2]); 01449 01450 utan[0] = (t1*e2[0] - t2*e1[0]); 01451 utan[1] = (t1*e2[1] - t2*e1[1]); 01452 utan[2] = (t1*e2[2] - t2*e1[2]); 01453 } 01454 01455 if(orco) { 01456 if(orcodata) { 01457 o1= orcodata[mface->v1]; 01458 o2= orcodata[mface->v2]; 01459 o3= orcodata[mface->v3]; 01460 01461 if(mface->v4) { 01462 o4= orcodata[mface->v4]; 01463 01464 interp_v3_v3v3v3v3(orco, o1, o2, o3, o4, w); 01465 01466 if(ornor) 01467 normal_quad_v3( ornor,o1, o2, o3, o4); 01468 } 01469 else { 01470 interp_v3_v3v3v3(orco, o1, o2, o3, w); 01471 01472 if(ornor) 01473 normal_tri_v3( ornor,o1, o2, o3); 01474 } 01475 } 01476 else { 01477 VECCOPY(orco, vec); 01478 if(ornor && nor) 01479 VECCOPY(ornor, nor); 01480 } 01481 } 01482 } 01483 void psys_interpolate_uvs(MTFace *tface, int quad, float *w, float *uvco) 01484 { 01485 float v10= tface->uv[0][0]; 01486 float v11= tface->uv[0][1]; 01487 float v20= tface->uv[1][0]; 01488 float v21= tface->uv[1][1]; 01489 float v30= tface->uv[2][0]; 01490 float v31= tface->uv[2][1]; 01491 float v40,v41; 01492 01493 if(quad) { 01494 v40= tface->uv[3][0]; 01495 v41= tface->uv[3][1]; 01496 01497 uvco[0]= w[0]*v10 + w[1]*v20 + w[2]*v30 + w[3]*v40; 01498 uvco[1]= w[0]*v11 + w[1]*v21 + w[2]*v31 + w[3]*v41; 01499 } 01500 else { 01501 uvco[0]= w[0]*v10 + w[1]*v20 + w[2]*v30; 01502 uvco[1]= w[0]*v11 + w[1]*v21 + w[2]*v31; 01503 } 01504 } 01505 01506 void psys_interpolate_mcol(MCol *mcol, int quad, float *w, MCol *mc) 01507 { 01508 char *cp, *cp1, *cp2, *cp3, *cp4; 01509 01510 cp= (char *)mc; 01511 cp1= (char *)&mcol[0]; 01512 cp2= (char *)&mcol[1]; 01513 cp3= (char *)&mcol[2]; 01514 01515 if(quad) { 01516 cp4= (char *)&mcol[3]; 01517 01518 cp[0]= (int)(w[0]*cp1[0] + w[1]*cp2[0] + w[2]*cp3[0] + w[3]*cp4[0]); 01519 cp[1]= (int)(w[0]*cp1[1] + w[1]*cp2[1] + w[2]*cp3[1] + w[3]*cp4[1]); 01520 cp[2]= (int)(w[0]*cp1[2] + w[1]*cp2[2] + w[2]*cp3[2] + w[3]*cp4[2]); 01521 cp[3]= (int)(w[0]*cp1[3] + w[1]*cp2[3] + w[2]*cp3[3] + w[3]*cp4[3]); 01522 } 01523 else { 01524 cp[0]= (int)(w[0]*cp1[0] + w[1]*cp2[0] + w[2]*cp3[0]); 01525 cp[1]= (int)(w[0]*cp1[1] + w[1]*cp2[1] + w[2]*cp3[1]); 01526 cp[2]= (int)(w[0]*cp1[2] + w[1]*cp2[2] + w[2]*cp3[2]); 01527 cp[3]= (int)(w[0]*cp1[3] + w[1]*cp2[3] + w[2]*cp3[3]); 01528 } 01529 } 01530 01531 static float psys_interpolate_value_from_verts(DerivedMesh *dm, short from, int index, float *fw, float *values) 01532 { 01533 if(values==0 || index==-1) 01534 return 0.0; 01535 01536 switch(from){ 01537 case PART_FROM_VERT: 01538 return values[index]; 01539 case PART_FROM_FACE: 01540 case PART_FROM_VOLUME: 01541 { 01542 MFace *mf=dm->getFaceData(dm,index,CD_MFACE); 01543 return interpolate_particle_value(values[mf->v1],values[mf->v2],values[mf->v3],values[mf->v4],fw,mf->v4); 01544 } 01545 01546 } 01547 return 0.0; 01548 } 01549 01550 /* conversion of pa->fw to origspace layer coordinates */ 01551 static void psys_w_to_origspace(float *w, float *uv) 01552 { 01553 uv[0]= w[1] + w[2]; 01554 uv[1]= w[2] + w[3]; 01555 } 01556 01557 /* conversion of pa->fw to weights in face from origspace */ 01558 static void psys_origspace_to_w(OrigSpaceFace *osface, int quad, float *w, float *neww) 01559 { 01560 float v[4][3], co[3]; 01561 01562 v[0][0]= osface->uv[0][0]; v[0][1]= osface->uv[0][1]; v[0][2]= 0.0f; 01563 v[1][0]= osface->uv[1][0]; v[1][1]= osface->uv[1][1]; v[1][2]= 0.0f; 01564 v[2][0]= osface->uv[2][0]; v[2][1]= osface->uv[2][1]; v[2][2]= 0.0f; 01565 01566 psys_w_to_origspace(w, co); 01567 co[2]= 0.0f; 01568 01569 if(quad) { 01570 v[3][0]= osface->uv[3][0]; v[3][1]= osface->uv[3][1]; v[3][2]= 0.0f; 01571 interp_weights_poly_v3( neww,v, 4, co); 01572 } 01573 else { 01574 interp_weights_poly_v3( neww,v, 3, co); 01575 neww[3]= 0.0f; 01576 } 01577 } 01578 01579 /* find the derived mesh face for a particle, set the mf passed. this is slow 01580 * and can be optimized but only for many lookups. returns the face index. */ 01581 int psys_particle_dm_face_lookup(Object *ob, DerivedMesh *dm, int index, float *fw, struct LinkNode *node) 01582 { 01583 Mesh *me= (Mesh*)ob->data; 01584 MFace *mface; 01585 OrigSpaceFace *osface; 01586 int *origindex; 01587 int quad, findex, totface; 01588 float uv[2], (*faceuv)[2]; 01589 01590 mface = dm->getFaceDataArray(dm, CD_MFACE); 01591 origindex = dm->getFaceDataArray(dm, CD_ORIGINDEX); 01592 osface = dm->getFaceDataArray(dm, CD_ORIGSPACE); 01593 01594 totface = dm->getNumFaces(dm); 01595 01596 if(osface==NULL || origindex==NULL) { 01597 /* Assume we dont need osface data */ 01598 if (index <totface) { 01599 //printf("\tNO CD_ORIGSPACE, assuming not needed\n"); 01600 return index; 01601 } else { 01602 printf("\tNO CD_ORIGSPACE, error out of range\n"); 01603 return DMCACHE_NOTFOUND; 01604 } 01605 } 01606 else if(index >= me->totface) 01607 return DMCACHE_NOTFOUND; /* index not in the original mesh */ 01608 01609 psys_w_to_origspace(fw, uv); 01610 01611 if(node) { /* we have a linked list of faces that we use, faster! */ 01612 for(;node; node=node->next) { 01613 findex= GET_INT_FROM_POINTER(node->link); 01614 faceuv= osface[findex].uv; 01615 quad= mface[findex].v4; 01616 01617 /* check that this intersects - Its possible this misses :/ - 01618 * could also check its not between */ 01619 if(quad) { 01620 if(isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3])) 01621 return findex; 01622 } 01623 else if(isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2])) 01624 return findex; 01625 } 01626 } 01627 else { /* if we have no node, try every face */ 01628 for(findex=0; findex<totface; findex++) { 01629 if(origindex[findex] == index) { 01630 faceuv= osface[findex].uv; 01631 quad= mface[findex].v4; 01632 01633 /* check that this intersects - Its possible this misses :/ - 01634 * could also check its not between */ 01635 if(quad) { 01636 if(isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3])) 01637 return findex; 01638 } 01639 else if(isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2])) 01640 return findex; 01641 } 01642 } 01643 } 01644 01645 return DMCACHE_NOTFOUND; 01646 } 01647 01648 static int psys_map_index_on_dm(DerivedMesh *dm, int from, int index, int index_dmcache, float *fw, float UNUSED(foffset), int *mapindex, float *mapfw) 01649 { 01650 if(index < 0) 01651 return 0; 01652 01653 if (dm->deformedOnly || index_dmcache == DMCACHE_ISCHILD) { 01654 /* for meshes that are either only defined or for child particles, the 01655 * index and fw do not require any mapping, so we can directly use it */ 01656 if(from == PART_FROM_VERT) { 01657 if(index >= dm->getNumVerts(dm)) 01658 return 0; 01659 01660 *mapindex = index; 01661 } 01662 else { /* FROM_FACE/FROM_VOLUME */ 01663 if(index >= dm->getNumFaces(dm)) 01664 return 0; 01665 01666 *mapindex = index; 01667 QUATCOPY(mapfw, fw); 01668 } 01669 } else { 01670 /* for other meshes that have been modified, we try to map the particle 01671 * to their new location, which means a different index, and for faces 01672 * also a new face interpolation weights */ 01673 if(from == PART_FROM_VERT) { 01674 if (index_dmcache == DMCACHE_NOTFOUND || index_dmcache > dm->getNumVerts(dm)) 01675 return 0; 01676 01677 *mapindex = index_dmcache; 01678 } 01679 else { /* FROM_FACE/FROM_VOLUME */ 01680 /* find a face on the derived mesh that uses this face */ 01681 MFace *mface; 01682 OrigSpaceFace *osface; 01683 int i; 01684 01685 i = index_dmcache; 01686 01687 if(i== DMCACHE_NOTFOUND || i >= dm->getNumFaces(dm)) 01688 return 0; 01689 01690 *mapindex = i; 01691 01692 /* modify the original weights to become 01693 * weights for the derived mesh face */ 01694 osface= dm->getFaceDataArray(dm, CD_ORIGSPACE); 01695 mface= dm->getFaceData(dm, i, CD_MFACE); 01696 01697 if(osface == NULL) 01698 mapfw[0]= mapfw[1]= mapfw[2]= mapfw[3]= 0.0f; 01699 else 01700 psys_origspace_to_w(&osface[i], mface->v4, fw, mapfw); 01701 } 01702 } 01703 01704 return 1; 01705 } 01706 01707 /* interprets particle data to get a point on a mesh in object space */ 01708 void psys_particle_on_dm(DerivedMesh *dm, int from, int index, int index_dmcache, float *fw, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor) 01709 { 01710 float tmpnor[3], mapfw[4]; 01711 float (*orcodata)[3]; 01712 int mapindex; 01713 01714 if(!psys_map_index_on_dm(dm, from, index, index_dmcache, fw, foffset, &mapindex, mapfw)) { 01715 if(vec) { vec[0]=vec[1]=vec[2]=0.0; } 01716 if(nor) { nor[0]=nor[1]=0.0; nor[2]=1.0; } 01717 if(orco) { orco[0]=orco[1]=orco[2]=0.0; } 01718 if(ornor) { ornor[0]=ornor[1]=0.0; ornor[2]=1.0; } 01719 if(utan) { utan[0]=utan[1]=utan[2]=0.0; } 01720 if(vtan) { vtan[0]=vtan[1]=vtan[2]=0.0; } 01721 01722 return; 01723 } 01724 01725 orcodata= dm->getVertDataArray(dm, CD_ORCO); 01726 01727 if(from == PART_FROM_VERT) { 01728 dm->getVertCo(dm,mapindex,vec); 01729 01730 if(nor) { 01731 dm->getVertNo(dm,mapindex,nor); 01732 normalize_v3(nor); 01733 } 01734 01735 if(orco) 01736 VECCOPY(orco, orcodata[mapindex]) 01737 01738 if(ornor) { 01739 dm->getVertNo(dm,mapindex,nor); 01740 normalize_v3(nor); 01741 } 01742 01743 if(utan && vtan) { 01744 utan[0]= utan[1]= utan[2]= 0.0f; 01745 vtan[0]= vtan[1]= vtan[2]= 0.0f; 01746 } 01747 } 01748 else { /* PART_FROM_FACE / PART_FROM_VOLUME */ 01749 MFace *mface; 01750 MTFace *mtface; 01751 MVert *mvert; 01752 01753 mface=dm->getFaceData(dm,mapindex,CD_MFACE); 01754 mvert=dm->getVertDataArray(dm,CD_MVERT); 01755 mtface=CustomData_get_layer(&dm->faceData,CD_MTFACE); 01756 01757 if(mtface) 01758 mtface += mapindex; 01759 01760 if(from==PART_FROM_VOLUME) { 01761 psys_interpolate_face(mvert,mface,mtface,orcodata,mapfw,vec,tmpnor,utan,vtan,orco,ornor); 01762 if(nor) 01763 VECCOPY(nor,tmpnor); 01764 01765 normalize_v3(tmpnor); 01766 mul_v3_fl(tmpnor,-foffset); 01767 VECADD(vec,vec,tmpnor); 01768 } 01769 else 01770 psys_interpolate_face(mvert,mface,mtface,orcodata,mapfw,vec,nor,utan,vtan,orco,ornor); 01771 } 01772 } 01773 01774 float psys_particle_value_from_verts(DerivedMesh *dm, short from, ParticleData *pa, float *values) 01775 { 01776 float mapfw[4]; 01777 int mapindex; 01778 01779 if(!psys_map_index_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, &mapindex, mapfw)) 01780 return 0.0f; 01781 01782 return psys_interpolate_value_from_verts(dm, from, mapindex, mapfw, values); 01783 } 01784 01785 ParticleSystemModifierData *psys_get_modifier(Object *ob, ParticleSystem *psys) 01786 { 01787 ModifierData *md; 01788 ParticleSystemModifierData *psmd; 01789 01790 for(md=ob->modifiers.first; md; md=md->next){ 01791 if(md->type==eModifierType_ParticleSystem){ 01792 psmd= (ParticleSystemModifierData*) md; 01793 if(psmd->psys==psys){ 01794 return psmd; 01795 } 01796 } 01797 } 01798 return NULL; 01799 } 01800 /************************************************/ 01801 /* Particles on a shape */ 01802 /************************************************/ 01803 /* ready for future use */ 01804 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) 01805 { 01806 /* TODO */ 01807 float zerovec[3]={0.0f,0.0f,0.0f}; 01808 if(vec){ 01809 VECCOPY(vec,zerovec); 01810 } 01811 if(nor){ 01812 VECCOPY(nor,zerovec); 01813 } 01814 if(utan){ 01815 VECCOPY(utan,zerovec); 01816 } 01817 if(vtan){ 01818 VECCOPY(vtan,zerovec); 01819 } 01820 if(orco){ 01821 VECCOPY(orco,zerovec); 01822 } 01823 if(ornor){ 01824 VECCOPY(ornor,zerovec); 01825 } 01826 } 01827 /************************************************/ 01828 /* Particles on emitter */ 01829 /************************************************/ 01830 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){ 01831 if(psmd){ 01832 if(psmd->psys->part->distr==PART_DISTR_GRID && psmd->psys->part->from != PART_FROM_VERT){ 01833 if(vec) 01834 copy_v3_v3(vec,fuv); 01835 01836 if(orco) 01837 copy_v3_v3(orco, fuv); 01838 return; 01839 } 01840 /* we cant use the num_dmcache */ 01841 psys_particle_on_dm(psmd->dm,from,index,index_dmcache,fuv,foffset,vec,nor,utan,vtan,orco,ornor); 01842 } 01843 else 01844 psys_particle_on_shape(from,index,fuv,vec,nor,utan,vtan,orco,ornor); 01845 01846 } 01847 /************************************************/ 01848 /* Path Cache */ 01849 /************************************************/ 01850 static float vert_weight(MDeformVert *dvert, int group) 01851 { 01852 MDeformWeight *dw; 01853 int i; 01854 01855 if(dvert) { 01856 dw= dvert->dw; 01857 for(i= dvert->totweight; i>0; i--, dw++) { 01858 if(dw->def_nr == group) return dw->weight; 01859 if(i==1) break; /*otherwise dw will point to somewhere it shouldn't*/ 01860 } 01861 } 01862 return 0.0; 01863 } 01864 01865 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) 01866 { 01867 float kink[3]={1.f,0.f,0.f}, par_vec[3], q1[4]={1.f,0.f,0.f,0.f}; 01868 float t, dt=1.f, result[3]; 01869 01870 if(par == NULL || type == PART_KINK_NO) 01871 return; 01872 01873 CLAMP(time, 0.f, 1.f); 01874 01875 if(shape!=0.0f && type!=PART_KINK_BRAID) { 01876 if(shape<0.0f) 01877 time= (float)pow(time, 1.f+shape); 01878 else 01879 time= (float)pow(time, 1.f/(1.f-shape)); 01880 } 01881 01882 t = time * freq *(float)M_PI; 01883 01884 if(smooth_start) { 01885 dt = fabs(t); 01886 /* smooth the beginning of kink */ 01887 CLAMP(dt, 0.f, (float)M_PI); 01888 dt = sin(dt/2.f); 01889 } 01890 01891 if(type != PART_KINK_RADIAL) { 01892 float temp[3]; 01893 01894 kink[axis]=1.f; 01895 01896 if(obmat) 01897 mul_mat3_m4_v3(obmat, kink); 01898 01899 if(par_rot) 01900 mul_qt_v3(par_rot, kink); 01901 01902 /* make sure kink is normal to strand */ 01903 project_v3_v3v3(temp, kink, par->vel); 01904 sub_v3_v3(kink, temp); 01905 normalize_v3(kink); 01906 } 01907 01908 copy_v3_v3(result, state->co); 01909 sub_v3_v3v3(par_vec, par->co, state->co); 01910 01911 switch(type) { 01912 case PART_KINK_CURL: 01913 { 01914 mul_v3_fl(par_vec, -1.f); 01915 01916 if(flat > 0.f) { 01917 float proj[3]; 01918 project_v3_v3v3(proj, par_vec, par->vel); 01919 madd_v3_v3fl(par_vec, proj, -flat); 01920 01921 project_v3_v3v3(proj, par_vec, kink); 01922 madd_v3_v3fl(par_vec, proj, -flat); 01923 } 01924 01925 axis_angle_to_quat(q1, kink, (float)M_PI/2.f); 01926 01927 mul_qt_v3(q1, par_vec); 01928 01929 madd_v3_v3fl(par_vec, kink, amplitude); 01930 01931 /* rotate kink vector around strand tangent */ 01932 if(t!=0.f) { 01933 axis_angle_to_quat(q1, par->vel, t); 01934 mul_qt_v3(q1, par_vec); 01935 } 01936 01937 add_v3_v3v3(result, par->co, par_vec); 01938 break; 01939 } 01940 case PART_KINK_RADIAL: 01941 { 01942 if(flat > 0.f) { 01943 float proj[3]; 01944 /* flatten along strand */ 01945 project_v3_v3v3(proj, par_vec, par->vel); 01946 madd_v3_v3fl(result, proj, flat); 01947 } 01948 01949 madd_v3_v3fl(result, par_vec, -amplitude*(float)sin(t)); 01950 break; 01951 } 01952 case PART_KINK_WAVE: 01953 { 01954 madd_v3_v3fl(result, kink, amplitude*(float)sin(t)); 01955 01956 if(flat > 0.f) { 01957 float proj[3]; 01958 /* flatten along wave */ 01959 project_v3_v3v3(proj, par_vec, kink); 01960 madd_v3_v3fl(result, proj, flat); 01961 01962 /* flatten along strand */ 01963 project_v3_v3v3(proj, par_vec, par->vel); 01964 madd_v3_v3fl(result, proj, flat); 01965 } 01966 break; 01967 } 01968 case PART_KINK_BRAID: 01969 { 01970 float y_vec[3]={0.f,1.f,0.f}; 01971 float z_vec[3]={0.f,0.f,1.f}; 01972 float vec_one[3], state_co[3]; 01973 float inp_y, inp_z, length; 01974 01975 if(par_rot) { 01976 mul_qt_v3(par_rot, y_vec); 01977 mul_qt_v3(par_rot, z_vec); 01978 } 01979 01980 mul_v3_fl(par_vec, -1.f); 01981 normalize_v3_v3(vec_one, par_vec); 01982 01983 inp_y=dot_v3v3(y_vec, vec_one); 01984 inp_z=dot_v3v3(z_vec, vec_one); 01985 01986 if(inp_y > 0.5f){ 01987 copy_v3_v3(state_co, y_vec); 01988 01989 mul_v3_fl(y_vec, amplitude*(float)cos(t)); 01990 mul_v3_fl(z_vec, amplitude/2.f*(float)sin(2.f*t)); 01991 } 01992 else if(inp_z > 0.0f){ 01993 mul_v3_v3fl(state_co, z_vec, (float)sin((float)M_PI/3.f)); 01994 VECADDFAC(state_co,state_co,y_vec,-0.5f); 01995 01996 mul_v3_fl(y_vec, -amplitude * (float)cos(t + (float)M_PI/3.f)); 01997 mul_v3_fl(z_vec, amplitude/2.f * (float)cos(2.f*t + (float)M_PI/6.f)); 01998 } 01999 else{ 02000 mul_v3_v3fl(state_co, z_vec, -(float)sin((float)M_PI/3.f)); 02001 madd_v3_v3fl(state_co, y_vec, -0.5f); 02002 02003 mul_v3_fl(y_vec, amplitude * (float)-sin(t + (float)M_PI/6.f)); 02004 mul_v3_fl(z_vec, amplitude/2.f * (float)-sin(2.f*t + (float)M_PI/3.f)); 02005 } 02006 02007 mul_v3_fl(state_co, amplitude); 02008 add_v3_v3(state_co, par->co); 02009 sub_v3_v3v3(par_vec, state->co, state_co); 02010 02011 length = normalize_v3(par_vec); 02012 mul_v3_fl(par_vec, MIN2(length, amplitude/2.f)); 02013 02014 add_v3_v3v3(state_co, par->co, y_vec); 02015 add_v3_v3(state_co, z_vec); 02016 add_v3_v3(state_co, par_vec); 02017 02018 shape = 2.f*(float)M_PI * (1.f+shape); 02019 02020 if(t<shape){ 02021 shape = t/shape; 02022 shape = (float)sqrt((double)shape); 02023 interp_v3_v3v3(result, result, state_co, shape); 02024 } 02025 else{ 02026 copy_v3_v3(result, state_co); 02027 } 02028 break; 02029 } 02030 } 02031 02032 /* blend the start of the kink */ 02033 if(dt < 1.f) 02034 interp_v3_v3v3(state->co, state->co, result, dt); 02035 else 02036 copy_v3_v3(state->co, result); 02037 } 02038 02039 static float do_clump(ParticleKey *state, ParticleKey *par, float time, float clumpfac, float clumppow, float pa_clump) 02040 { 02041 float clump = 0.f; 02042 02043 if(par && clumpfac!=0.0f){ 02044 float cpow; 02045 02046 if(clumppow < 0.0f) 02047 cpow=1.0f+clumppow; 02048 else 02049 cpow=1.0f+9.0f*clumppow; 02050 02051 if(clumpfac < 0.0f) /* clump roots instead of tips */ 02052 clump = -clumpfac*pa_clump*(float)pow(1.0-(double)time,(double)cpow); 02053 else 02054 clump = clumpfac*pa_clump*(float)pow((double)time,(double)cpow); 02055 02056 interp_v3_v3v3(state->co,state->co,par->co,clump); 02057 } 02058 02059 return clump; 02060 } 02061 void precalc_guides(ParticleSimulationData *sim, ListBase *effectors) 02062 { 02063 EffectedPoint point; 02064 ParticleKey state; 02065 EffectorData efd; 02066 EffectorCache *eff; 02067 ParticleSystem *psys = sim->psys; 02068 EffectorWeights *weights = sim->psys->part->effector_weights; 02069 GuideEffectorData *data; 02070 PARTICLE_P; 02071 02072 if(!effectors) 02073 return; 02074 02075 LOOP_PARTICLES { 02076 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); 02077 02078 mul_m4_v3(sim->ob->obmat, state.co); 02079 mul_mat3_m4_v3(sim->ob->obmat, state.vel); 02080 02081 pd_point_from_particle(sim, pa, &state, &point); 02082 02083 for(eff = effectors->first; eff; eff=eff->next) { 02084 if(eff->pd->forcefield != PFIELD_GUIDE) 02085 continue; 02086 02087 if(!eff->guide_data) 02088 eff->guide_data = MEM_callocN(sizeof(GuideEffectorData)*psys->totpart, "GuideEffectorData"); 02089 02090 data = eff->guide_data + p; 02091 02092 VECSUB(efd.vec_to_point, state.co, eff->guide_loc); 02093 VECCOPY(efd.nor, eff->guide_dir); 02094 efd.distance = len_v3(efd.vec_to_point); 02095 02096 VECCOPY(data->vec_to_point, efd.vec_to_point); 02097 data->strength = effector_falloff(eff, &efd, &point, weights); 02098 } 02099 } 02100 } 02101 int do_guides(ListBase *effectors, ParticleKey *state, int index, float time) 02102 { 02103 EffectorCache *eff; 02104 PartDeflect *pd; 02105 Curve *cu; 02106 ParticleKey key, par; 02107 GuideEffectorData *data; 02108 02109 float effect[3] = {0.0f, 0.0f, 0.0f}, veffect[3] = {0.0f, 0.0f, 0.0f}; 02110 float guidevec[4], guidedir[3], rot2[4], temp[3]; 02111 float guidetime, radius, weight, angle, totstrength = 0.0f; 02112 float vec_to_point[3]; 02113 02114 if(effectors) for(eff = effectors->first; eff; eff=eff->next) { 02115 pd = eff->pd; 02116 02117 if(pd->forcefield != PFIELD_GUIDE) 02118 continue; 02119 02120 data = eff->guide_data + index; 02121 02122 if(data->strength <= 0.0f) 02123 continue; 02124 02125 guidetime = time / (1.0f - pd->free_end); 02126 02127 if(guidetime>1.0f) 02128 continue; 02129 02130 cu = (Curve*)eff->ob->data; 02131 02132 if(pd->flag & PFIELD_GUIDE_PATH_ADD) { 02133 if(where_on_path(eff->ob, data->strength * guidetime, guidevec, guidedir, NULL, &radius, &weight)==0) 02134 return 0; 02135 } 02136 else { 02137 if(where_on_path(eff->ob, guidetime, guidevec, guidedir, NULL, &radius, &weight)==0) 02138 return 0; 02139 } 02140 02141 mul_m4_v3(eff->ob->obmat, guidevec); 02142 mul_mat3_m4_v3(eff->ob->obmat, guidedir); 02143 02144 normalize_v3(guidedir); 02145 02146 VECCOPY(vec_to_point, data->vec_to_point); 02147 02148 if(guidetime != 0.0f) { 02149 /* curve direction */ 02150 cross_v3_v3v3(temp, eff->guide_dir, guidedir); 02151 angle = dot_v3v3(eff->guide_dir, guidedir)/(len_v3(eff->guide_dir)); 02152 angle = saacos(angle); 02153 axis_angle_to_quat( rot2,temp, angle); 02154 mul_qt_v3(rot2, vec_to_point); 02155 02156 /* curve tilt */ 02157 axis_angle_to_quat( rot2,guidedir, guidevec[3] - eff->guide_loc[3]); 02158 mul_qt_v3(rot2, vec_to_point); 02159 } 02160 02161 /* curve taper */ 02162 if(cu->taperobj) 02163 mul_v3_fl(vec_to_point, calc_taper(eff->scene, cu->taperobj, (int)(data->strength*guidetime*100.0f), 100)); 02164 02165 else{ /* curve size*/ 02166 if(cu->flag & CU_PATH_RADIUS) { 02167 mul_v3_fl(vec_to_point, radius); 02168 } 02169 } 02170 par.co[0] = par.co[1] = par.co[2] = 0.0f; 02171 VECCOPY(key.co, vec_to_point); 02172 do_kink(&key, &par, 0, guidetime, pd->kink_freq, pd->kink_shape, pd->kink_amp, 0.f, pd->kink, pd->kink_axis, 0, 0); 02173 do_clump(&key, &par, guidetime, pd->clump_fac, pd->clump_pow, 1.0f); 02174 VECCOPY(vec_to_point, key.co); 02175 02176 VECADD(vec_to_point, vec_to_point, guidevec); 02177 02178 //VECSUB(pa_loc,pa_loc,pa_zero); 02179 VECADDFAC(effect, effect, vec_to_point, data->strength); 02180 VECADDFAC(veffect, veffect, guidedir, data->strength); 02181 totstrength += data->strength; 02182 02183 if(pd->flag & PFIELD_GUIDE_PATH_WEIGHT) 02184 totstrength *= weight; 02185 } 02186 02187 if(totstrength != 0.0f){ 02188 if(totstrength > 1.0f) 02189 mul_v3_fl(effect, 1.0f / totstrength); 02190 CLAMP(totstrength, 0.0f, 1.0f); 02191 //VECADD(effect,effect,pa_zero); 02192 interp_v3_v3v3(state->co, state->co, effect, totstrength); 02193 02194 normalize_v3(veffect); 02195 mul_v3_fl(veffect, len_v3(state->vel)); 02196 VECCOPY(state->vel, veffect); 02197 return 1; 02198 } 02199 return 0; 02200 } 02201 static void do_rough(float *loc, float mat[4][4], float t, float fac, float size, float thres, ParticleKey *state) 02202 { 02203 float rough[3]; 02204 float rco[3]; 02205 02206 if(thres != 0.0f) 02207 if((float)fabs((float)(-1.5f+loc[0]+loc[1]+loc[2]))<1.5f*thres) return; 02208 02209 VECCOPY(rco,loc); 02210 mul_v3_fl(rco,t); 02211 rough[0]=-1.0f+2.0f*BLI_gTurbulence(size, rco[0], rco[1], rco[2], 2,0,2); 02212 rough[1]=-1.0f+2.0f*BLI_gTurbulence(size, rco[1], rco[2], rco[0], 2,0,2); 02213 rough[2]=-1.0f+2.0f*BLI_gTurbulence(size, rco[2], rco[0], rco[1], 2,0,2); 02214 02215 VECADDFAC(state->co,state->co,mat[0],fac*rough[0]); 02216 VECADDFAC(state->co,state->co,mat[1],fac*rough[1]); 02217 VECADDFAC(state->co,state->co,mat[2],fac*rough[2]); 02218 } 02219 static void do_rough_end(float *loc, float mat[4][4], float t, float fac, float shape, ParticleKey *state) 02220 { 02221 float rough[2]; 02222 float roughfac; 02223 02224 roughfac=fac*(float)pow((double)t,shape); 02225 copy_v2_v2(rough,loc); 02226 rough[0]=-1.0f+2.0f*rough[0]; 02227 rough[1]=-1.0f+2.0f*rough[1]; 02228 mul_v2_fl(rough,roughfac); 02229 02230 VECADDFAC(state->co,state->co,mat[0],rough[0]); 02231 VECADDFAC(state->co,state->co,mat[1],rough[1]); 02232 } 02233 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) 02234 { 02235 float force[3] = {0.0f,0.0f,0.0f}; 02236 ParticleKey eff_key; 02237 EffectedPoint epoint; 02238 02239 /* Don't apply effectors for dynamic hair, otherwise the effectors don't get applied twice. */ 02240 if(sim->psys->flag & PSYS_HAIR_DYNAMICS) 02241 return; 02242 02243 VECCOPY(eff_key.co,(ca-1)->co); 02244 VECCOPY(eff_key.vel,(ca-1)->vel); 02245 QUATCOPY(eff_key.rot,(ca-1)->rot); 02246 02247 pd_point_from_particle(sim, sim->psys->particles+i, &eff_key, &epoint); 02248 pdDoEffectors(sim->psys->effectors, sim->colliders, sim->psys->part->effector_weights, &epoint, force, NULL); 02249 02250 mul_v3_fl(force, effector*powf((float)k / (float)steps, 100.0f * sim->psys->part->eff_hair) / (float)steps); 02251 02252 add_v3_v3(force, vec); 02253 02254 normalize_v3(force); 02255 02256 if(k < steps) 02257 sub_v3_v3v3(vec, (ca+1)->co, ca->co); 02258 02259 madd_v3_v3v3fl(ca->co, (ca-1)->co, force, *length); 02260 02261 if(k < steps) 02262 *length = len_v3(vec); 02263 } 02264 static int check_path_length(int k, ParticleCacheKey *keys, ParticleCacheKey *state, float max_length, float *cur_length, float length, float *dvec) 02265 { 02266 if(*cur_length + length > max_length){ 02267 mul_v3_fl(dvec, (max_length - *cur_length) / length); 02268 VECADD(state->co, (state - 1)->co, dvec); 02269 keys->steps = k; 02270 /* something over the maximum step value */ 02271 return k=100000; 02272 } 02273 else { 02274 *cur_length+=length; 02275 return k; 02276 } 02277 } 02278 static void offset_child(ChildParticle *cpa, ParticleKey *par, float *par_rot, ParticleKey *child, float flat, float radius) 02279 { 02280 copy_v3_v3(child->co, cpa->fuv); 02281 mul_v3_fl(child->co, radius); 02282 02283 child->co[0]*=flat; 02284 02285 copy_v3_v3(child->vel, par->vel); 02286 02287 if(par_rot) { 02288 mul_qt_v3(par_rot, child->co); 02289 copy_qt_qt(child->rot, par_rot); 02290 } 02291 else 02292 unit_qt(child->rot); 02293 02294 add_v3_v3(child->co, par->co); 02295 } 02296 float *psys_cache_vgroup(DerivedMesh *dm, ParticleSystem *psys, int vgroup) 02297 { 02298 float *vg=0; 02299 02300 if(vgroup < 0) { 02301 /* hair dynamics pinning vgroup */ 02302 02303 } 02304 else if(psys->vgroup[vgroup]){ 02305 MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT); 02306 if(dvert){ 02307 int totvert=dm->getNumVerts(dm), i; 02308 vg=MEM_callocN(sizeof(float)*totvert, "vg_cache"); 02309 if(psys->vg_neg&(1<<vgroup)){ 02310 for(i=0; i<totvert; i++) 02311 vg[i]=1.0f-vert_weight(dvert+i,psys->vgroup[vgroup]-1); 02312 } 02313 else{ 02314 for(i=0; i<totvert; i++) 02315 vg[i]=vert_weight(dvert+i,psys->vgroup[vgroup]-1); 02316 } 02317 } 02318 } 02319 return vg; 02320 } 02321 void psys_find_parents(ParticleSimulationData *sim) 02322 { 02323 ParticleSettings *part=sim->psys->part; 02324 KDTree *tree; 02325 ChildParticle *cpa; 02326 int p, totparent,totchild=sim->psys->totchild; 02327 float co[3], orco[3]; 02328 int from=PART_FROM_FACE; 02329 totparent=(int)(totchild*part->parents*0.3f); 02330 02331 if(G.rendering && part->child_nbr && part->ren_child_nbr) 02332 totparent*=(float)part->child_nbr/(float)part->ren_child_nbr; 02333 02334 tree=BLI_kdtree_new(totparent); 02335 02336 for(p=0,cpa=sim->psys->child; p<totparent; p++,cpa++){ 02337 psys_particle_on_emitter(sim->psmd,from,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co,0,0,0,orco,0); 02338 BLI_kdtree_insert(tree, p, orco, NULL); 02339 } 02340 02341 BLI_kdtree_balance(tree); 02342 02343 for(; p<totchild; p++,cpa++){ 02344 psys_particle_on_emitter(sim->psmd,from,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co,0,0,0,orco,0); 02345 cpa->parent=BLI_kdtree_find_nearest(tree, orco, NULL, NULL); 02346 } 02347 02348 BLI_kdtree_free(tree); 02349 } 02350 02351 static void get_strand_normal(Material *ma, float *surfnor, float surfdist, float *nor) 02352 { 02353 float cross[3], nstrand[3], vnor[3], blend; 02354 02355 if(!((ma->mode & MA_STR_SURFDIFF) || (ma->strand_surfnor > 0.0f))) 02356 return; 02357 02358 if(ma->mode & MA_STR_SURFDIFF) { 02359 cross_v3_v3v3(cross, surfnor, nor); 02360 cross_v3_v3v3(nstrand, nor, cross); 02361 02362 blend= INPR(nstrand, surfnor); 02363 CLAMP(blend, 0.0f, 1.0f); 02364 02365 interp_v3_v3v3(vnor, nstrand, surfnor, blend); 02366 normalize_v3(vnor); 02367 } 02368 else 02369 VECCOPY(vnor, nor) 02370 02371 if(ma->strand_surfnor > 0.0f) { 02372 if(ma->strand_surfnor > surfdist) { 02373 blend= (ma->strand_surfnor - surfdist)/ma->strand_surfnor; 02374 interp_v3_v3v3(vnor, vnor, surfnor, blend); 02375 normalize_v3(vnor); 02376 } 02377 } 02378 02379 VECCOPY(nor, vnor); 02380 } 02381 02382 static int psys_threads_init_path(ParticleThread *threads, Scene *scene, float cfra, int editupdate) 02383 { 02384 ParticleThreadContext *ctx= threads[0].ctx; 02385 /* Object *ob= ctx->sim.ob; */ 02386 ParticleSystem *psys= ctx->sim.psys; 02387 ParticleSettings *part = psys->part; 02388 /* ParticleEditSettings *pset = &scene->toolsettings->particle; */ 02389 int totparent=0, between=0; 02390 int steps = (int)pow(2.0, (double)part->draw_step); 02391 int totchild = psys->totchild; 02392 int i, seed, totthread= threads[0].tot; 02393 02394 /*---start figuring out what is actually wanted---*/ 02395 if(psys_in_edit_mode(scene, psys)) { 02396 ParticleEditSettings *pset = &scene->toolsettings->particle; 02397 02398 if(psys->renderdata==0 && (psys->edit==NULL || pset->flag & PE_DRAW_PART)==0) 02399 totchild=0; 02400 02401 steps = (int)pow(2.0, (double)pset->draw_step); 02402 } 02403 02404 if(totchild && part->childtype==PART_CHILD_FACES){ 02405 totparent=(int)(totchild*part->parents*0.3f); 02406 02407 if(G.rendering && part->child_nbr && part->ren_child_nbr) 02408 totparent*=(float)part->child_nbr/(float)part->ren_child_nbr; 02409 02410 /* part->parents could still be 0 so we can't test with totparent */ 02411 between=1; 02412 } 02413 02414 if(psys->renderdata) 02415 steps=(int)pow(2.0,(double)part->ren_step); 02416 else{ 02417 totchild=(int)((float)totchild*(float)part->disp/100.0f); 02418 totparent=MIN2(totparent,totchild); 02419 } 02420 02421 if(totchild==0) return 0; 02422 02423 /* init random number generator */ 02424 seed= 31415926 + ctx->sim.psys->seed; 02425 02426 if(ctx->editupdate || totchild < 10000) 02427 totthread= 1; 02428 02429 for(i=0; i<totthread; i++) { 02430 threads[i].rng_path= rng_new(seed); 02431 threads[i].tot= totthread; 02432 } 02433 02434 /* fill context values */ 02435 ctx->between= between; 02436 ctx->steps= steps; 02437 ctx->totchild= totchild; 02438 ctx->totparent= totparent; 02439 ctx->parent_pass= 0; 02440 ctx->cfra= cfra; 02441 ctx->editupdate= editupdate; 02442 02443 psys->lattice = psys_get_lattice(&ctx->sim); 02444 02445 /* cache all relevant vertex groups if they exist */ 02446 ctx->vg_length = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_LENGTH); 02447 ctx->vg_clump = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_CLUMP); 02448 ctx->vg_kink = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_KINK); 02449 ctx->vg_rough1 = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_ROUGH1); 02450 ctx->vg_rough2 = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_ROUGH2); 02451 ctx->vg_roughe = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_ROUGHE); 02452 if(psys->part->flag & PART_CHILD_EFFECT) 02453 ctx->vg_effector = psys_cache_vgroup(ctx->dm,psys,PSYS_VG_EFFECTOR); 02454 02455 /* set correct ipo timing */ 02456 #if 0 // XXX old animation system 02457 if(part->flag&PART_ABS_TIME && part->ipo){ 02458 calc_ipo(part->ipo, cfra); 02459 execute_ipo((ID *)part, part->ipo); 02460 } 02461 #endif // XXX old animation system 02462 02463 return 1; 02464 } 02465 02466 /* note: this function must be thread safe, except for branching! */ 02467 static void psys_thread_create_path(ParticleThread *thread, struct ChildParticle *cpa, ParticleCacheKey *child_keys, int i) 02468 { 02469 ParticleThreadContext *ctx= thread->ctx; 02470 Object *ob= ctx->sim.ob; 02471 ParticleSystem *psys = ctx->sim.psys; 02472 ParticleSettings *part = psys->part; 02473 ParticleCacheKey **cache= psys->childcache; 02474 ParticleCacheKey **pcache= psys_in_edit_mode(ctx->sim.scene, psys) ? psys->edit->pathcache : psys->pathcache; 02475 ParticleCacheKey *child, *par = NULL, *key[4]; 02476 ParticleTexture ptex; 02477 float *cpa_fuv=0, *par_rot=0, rot[4]; 02478 float orco[3], ornor[3], hairmat[4][4], t, dvec[3], off1[4][3], off2[4][3]; 02479 float length, max_length = 1.0f, cur_length = 0.0f; 02480 float eff_length, eff_vec[3], weight[4]; 02481 int k, cpa_num; 02482 short cpa_from; 02483 02484 if(!pcache) 02485 return; 02486 02487 if(ctx->between){ 02488 ParticleData *pa = psys->particles + cpa->pa[0]; 02489 int w, needupdate; 02490 float foffset, wsum=0.f; 02491 float co[3]; 02492 float p_min = part->parting_min; 02493 float p_max = part->parting_max; 02494 /* Virtual parents don't work nicely with parting. */ 02495 float p_fac = part->parents > 0.f ? 0.f : part->parting_fac; 02496 02497 if(ctx->editupdate) { 02498 needupdate= 0; 02499 w= 0; 02500 while(w<4 && cpa->pa[w]>=0) { 02501 if(psys->edit->points[cpa->pa[w]].flag & PEP_EDIT_RECALC) { 02502 needupdate= 1; 02503 break; 02504 } 02505 w++; 02506 } 02507 02508 if(!needupdate) 02509 return; 02510 else 02511 memset(child_keys, 0, sizeof(*child_keys)*(ctx->steps+1)); 02512 } 02513 02514 /* get parent paths */ 02515 for(w=0; w<4; w++) { 02516 if(cpa->pa[w] >= 0) { 02517 key[w] = pcache[cpa->pa[w]]; 02518 weight[w] = cpa->w[w]; 02519 } 02520 else { 02521 key[w] = pcache[0]; 02522 weight[w] = 0.f; 02523 } 02524 } 02525 02526 /* modify weights to create parting */ 02527 if(p_fac > 0.f) { 02528 for(w=0; w<4; w++) { 02529 if(w && weight[w] > 0.f) { 02530 float d; 02531 if(part->flag & PART_CHILD_LONG_HAIR) { 02532 /* For long hair use tip distance/root distance as parting factor instead of root to tip angle. */ 02533 float d1 = len_v3v3(key[0]->co, key[w]->co); 02534 float d2 = len_v3v3((key[0]+key[0]->steps-1)->co, (key[w]+key[w]->steps-1)->co); 02535 02536 d = d1 > 0.f ? d2/d1 - 1.f : 10000.f; 02537 } 02538 else { 02539 float v1[3], v2[3]; 02540 sub_v3_v3v3(v1, (key[0]+key[0]->steps-1)->co, key[0]->co); 02541 sub_v3_v3v3(v2, (key[w]+key[w]->steps-1)->co, key[w]->co); 02542 normalize_v3(v1); 02543 normalize_v3(v2); 02544 02545 d = saacos(dot_v3v3(v1, v2)) * 180.0f/(float)M_PI; 02546 } 02547 02548 if(p_max > p_min) 02549 d = (d - p_min)/(p_max - p_min); 02550 else 02551 d = (d - p_min) <= 0.f ? 0.f : 1.f; 02552 02553 CLAMP(d, 0.f, 1.f); 02554 02555 if(d > 0.f) 02556 weight[w] *= (1.f - d); 02557 } 02558 wsum += weight[w]; 02559 } 02560 for(w=0; w<4; w++) 02561 weight[w] /= wsum; 02562 02563 interp_v4_v4v4(weight, cpa->w, weight, p_fac); 02564 } 02565 02566 /* get the original coordinates (orco) for texture usage */ 02567 cpa_num = cpa->num; 02568 02569 foffset = cpa->foffset; 02570 cpa_fuv = cpa->fuv; 02571 cpa_from = PART_FROM_FACE; 02572 02573 psys_particle_on_emitter(ctx->sim.psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa->fuv,foffset,co,ornor,0,0,orco,0); 02574 02575 mul_m4_v3(ob->obmat, co); 02576 02577 for(w=0; w<4; w++) 02578 sub_v3_v3v3(off1[w], co, key[w]->co); 02579 02580 psys_mat_hair_to_global(ob, ctx->sim.psmd->dm, psys->part->from, pa, hairmat); 02581 } 02582 else{ 02583 ParticleData *pa = psys->particles + cpa->parent; 02584 float co[3]; 02585 if(ctx->editupdate) { 02586 if(!(psys->edit->points[cpa->parent].flag & PEP_EDIT_RECALC)) 02587 return; 02588 02589 memset(child_keys, 0, sizeof(*child_keys)*(ctx->steps+1)); 02590 } 02591 02592 /* get the parent path */ 02593 key[0] = pcache[cpa->parent]; 02594 02595 /* get the original coordinates (orco) for texture usage */ 02596 cpa_from = part->from; 02597 cpa_num = pa->num; 02598 cpa_fuv = pa->fuv; 02599 02600 psys_particle_on_emitter(ctx->sim.psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa_fuv,pa->foffset,co,ornor,0,0,orco,0); 02601 02602 psys_mat_hair_to_global(ob, ctx->sim.psmd->dm, psys->part->from, pa, hairmat); 02603 } 02604 02605 child_keys->steps = ctx->steps; 02606 02607 /* get different child parameters from textures & vgroups */ 02608 get_child_modifier_parameters(part, ctx, cpa, cpa_from, cpa_num, cpa_fuv, orco, &ptex); 02609 02610 if(ptex.exist < PSYS_FRAND(i + 24)) { 02611 child_keys->steps = -1; 02612 return; 02613 } 02614 02615 /* create the child path */ 02616 for(k=0,child=child_keys; k<=ctx->steps; k++,child++){ 02617 if(ctx->between){ 02618 int w=0; 02619 02620 zero_v3(child->co); 02621 zero_v3(child->vel); 02622 unit_qt(child->rot); 02623 02624 for(w=0; w<4; w++) { 02625 copy_v3_v3(off2[w], off1[w]); 02626 02627 if(part->flag & PART_CHILD_LONG_HAIR) { 02628 /* Use parent rotation (in addition to emission location) to determine child offset. */ 02629 if(k) 02630 mul_qt_v3((key[w]+k)->rot, off2[w]); 02631 02632 /* Fade the effect of rotation for even lengths in the end */ 02633 project_v3_v3v3(dvec, off2[w], (key[w]+k)->vel); 02634 madd_v3_v3fl(off2[w], dvec, -(float)k/(float)ctx->steps); 02635 } 02636 02637 add_v3_v3(off2[w], (key[w]+k)->co); 02638 } 02639 02640 /* child position is the weighted sum of parent positions */ 02641 interp_v3_v3v3v3v3(child->co, off2[0], off2[1], off2[2], off2[3], weight); 02642 interp_v3_v3v3v3v3(child->vel, (key[0]+k)->vel, (key[1]+k)->vel, (key[2]+k)->vel, (key[3]+k)->vel, weight); 02643 02644 copy_qt_qt(child->rot, (key[0]+k)->rot); 02645 } 02646 else{ 02647 if(k) { 02648 mul_qt_qtqt(rot, (key[0]+k)->rot, key[0]->rot); 02649 par_rot = rot; 02650 } 02651 else { 02652 par_rot = key[0]->rot; 02653 } 02654 /* offset the child from the parent position */ 02655 offset_child(cpa, (ParticleKey*)(key[0]+k), par_rot, (ParticleKey*)child, part->childflat, part->childrad); 02656 } 02657 } 02658 02659 /* apply effectors */ 02660 if(part->flag & PART_CHILD_EFFECT) { 02661 for(k=0,child=child_keys; k<=ctx->steps; k++,child++) { 02662 if(k) { 02663 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); 02664 } 02665 else { 02666 sub_v3_v3v3(eff_vec, (child+1)->co, child->co); 02667 eff_length = len_v3(eff_vec); 02668 } 02669 } 02670 } 02671 02672 for(k=0,child=child_keys; k<=ctx->steps; k++,child++){ 02673 t = (float)k/(float)ctx->steps; 02674 02675 if(ctx->totparent) 02676 /* this is now threadsafe, virtual parents are calculated before rest of children */ 02677 par = (i >= ctx->totparent) ? cache[cpa->parent] : NULL; 02678 else if(cpa->parent >= 0) 02679 par = pcache[cpa->parent]; 02680 02681 if(par) { 02682 if(k) { 02683 mul_qt_qtqt(rot, (par+k)->rot, par->rot); 02684 par_rot = rot; 02685 } 02686 else { 02687 par_rot = par->rot; 02688 } 02689 par += k; 02690 } 02691 02692 /* apply different deformations to the child path */ 02693 do_child_modifiers(&ctx->sim, &ptex, (ParticleKey *)par, par_rot, cpa, orco, hairmat, (ParticleKey *)child, t); 02694 02695 /* we have to correct velocity because of kink & clump */ 02696 if(k>1){ 02697 sub_v3_v3v3((child-1)->vel, child->co, (child-2)->co); 02698 mul_v3_fl((child-1)->vel, 0.5); 02699 02700 if(ctx->ma && (part->draw_col == PART_DRAW_COL_MAT)) 02701 get_strand_normal(ctx->ma, ornor, cur_length, (child-1)->vel); 02702 } 02703 02704 if(k == ctx->steps) 02705 sub_v3_v3v3(child->vel, child->co, (child-1)->co); 02706 02707 /* check if path needs to be cut before actual end of data points */ 02708 if(k){ 02709 sub_v3_v3v3(dvec, child->co, (child-1)->co); 02710 length = 1.0f/(float)ctx->steps; 02711 k = check_path_length(k, child_keys, child, max_length, &cur_length, length, dvec); 02712 } 02713 else{ 02714 /* initialize length calculation */ 02715 max_length = ptex.length; 02716 cur_length = 0.0f; 02717 } 02718 02719 if(ctx->ma && (part->draw_col == PART_DRAW_COL_MAT)) { 02720 VECCOPY(child->col, &ctx->ma->r) 02721 get_strand_normal(ctx->ma, ornor, cur_length, child->vel); 02722 } 02723 } 02724 02725 /* Hide virtual parents */ 02726 if(i < ctx->totparent) 02727 child_keys->steps = -1; 02728 } 02729 02730 static void *exec_child_path_cache(void *data) 02731 { 02732 ParticleThread *thread= (ParticleThread*)data; 02733 ParticleThreadContext *ctx= thread->ctx; 02734 ParticleSystem *psys= ctx->sim.psys; 02735 ParticleCacheKey **cache= psys->childcache; 02736 ChildParticle *cpa; 02737 int i, totchild= ctx->totchild, first= 0; 02738 02739 if(thread->tot > 1){ 02740 first= ctx->parent_pass? 0 : ctx->totparent; 02741 totchild= ctx->parent_pass? ctx->totparent : ctx->totchild; 02742 } 02743 02744 cpa= psys->child + first + thread->num; 02745 for(i=first+thread->num; i<totchild; i+=thread->tot, cpa+=thread->tot) 02746 psys_thread_create_path(thread, cpa, cache[i], i); 02747 02748 return 0; 02749 } 02750 02751 void psys_cache_child_paths(ParticleSimulationData *sim, float cfra, int editupdate) 02752 { 02753 ParticleThread *pthreads; 02754 ParticleThreadContext *ctx; 02755 ListBase threads; 02756 int i, totchild, totparent, totthread; 02757 02758 if(sim->psys->flag & PSYS_GLOBAL_HAIR) 02759 return; 02760 02761 pthreads= psys_threads_create(sim); 02762 02763 if(!psys_threads_init_path(pthreads, sim->scene, cfra, editupdate)) { 02764 psys_threads_free(pthreads); 02765 return; 02766 } 02767 02768 ctx= pthreads[0].ctx; 02769 totchild= ctx->totchild; 02770 totparent= ctx->totparent; 02771 02772 if(editupdate && sim->psys->childcache && totchild == sim->psys->totchildcache) { 02773 ; /* just overwrite the existing cache */ 02774 } 02775 else { 02776 /* clear out old and create new empty path cache */ 02777 free_child_path_cache(sim->psys); 02778 sim->psys->childcache= psys_alloc_path_cache_buffers(&sim->psys->childcachebufs, totchild, ctx->steps+1); 02779 sim->psys->totchildcache = totchild; 02780 } 02781 02782 totthread= pthreads[0].tot; 02783 02784 if(totthread > 1) { 02785 02786 /* make virtual child parents thread safe by calculating them first */ 02787 if(totparent) { 02788 BLI_init_threads(&threads, exec_child_path_cache, totthread); 02789 02790 for(i=0; i<totthread; i++) { 02791 pthreads[i].ctx->parent_pass = 1; 02792 BLI_insert_thread(&threads, &pthreads[i]); 02793 } 02794 02795 BLI_end_threads(&threads); 02796 02797 for(i=0; i<totthread; i++) 02798 pthreads[i].ctx->parent_pass = 0; 02799 } 02800 02801 BLI_init_threads(&threads, exec_child_path_cache, totthread); 02802 02803 for(i=0; i<totthread; i++) 02804 BLI_insert_thread(&threads, &pthreads[i]); 02805 02806 BLI_end_threads(&threads); 02807 } 02808 else 02809 exec_child_path_cache(&pthreads[0]); 02810 02811 psys_threads_free(pthreads); 02812 } 02813 /* figure out incremental rotations along path starting from unit quat */ 02814 static void cache_key_incremental_rotation(ParticleCacheKey *key0, ParticleCacheKey *key1, ParticleCacheKey *key2, float *prev_tangent, int i) 02815 { 02816 float cosangle, angle, tangent[3], normal[3], q[4]; 02817 02818 switch(i) { 02819 case 0: 02820 /* start from second key */ 02821 break; 02822 case 1: 02823 /* calculate initial tangent for incremental rotations */ 02824 sub_v3_v3v3(prev_tangent, key0->co, key1->co); 02825 normalize_v3(prev_tangent); 02826 unit_qt(key1->rot); 02827 break; 02828 default: 02829 sub_v3_v3v3(tangent, key0->co, key1->co); 02830 normalize_v3(tangent); 02831 02832 cosangle= dot_v3v3(tangent, prev_tangent); 02833 02834 /* note we do the comparison on cosangle instead of 02835 * angle, since floating point accuracy makes it give 02836 * different results across platforms */ 02837 if(cosangle > 0.999999f) { 02838 QUATCOPY(key1->rot, key2->rot); 02839 } 02840 else { 02841 angle= saacos(cosangle); 02842 cross_v3_v3v3(normal, prev_tangent, tangent); 02843 axis_angle_to_quat( q,normal, angle); 02844 mul_qt_qtqt(key1->rot, q, key2->rot); 02845 } 02846 02847 copy_v3_v3(prev_tangent, tangent); 02848 } 02849 } 02850 /* Calculates paths ready for drawing/rendering. */ 02851 /* -Usefull for making use of opengl vertex arrays for super fast strand drawing. */ 02852 /* -Makes child strands possible and creates them too into the cache. */ 02853 /* -Cached path data is also used to determine cut position for the editmode tool. */ 02854 void psys_cache_paths(ParticleSimulationData *sim, float cfra) 02855 { 02856 PARTICLE_PSMD; 02857 ParticleEditSettings *pset = &sim->scene->toolsettings->particle; 02858 ParticleSystem *psys = sim->psys; 02859 ParticleSettings *part = psys->part; 02860 ParticleCacheKey *ca, **cache; 02861 02862 DerivedMesh *hair_dm = (psys->part->type==PART_HAIR && psys->flag & PSYS_HAIR_DYNAMICS) ? psys->hair_out_dm : NULL; 02863 02864 ParticleKey result; 02865 02866 Material *ma; 02867 ParticleInterpolationData pind; 02868 ParticleTexture ptex; 02869 02870 PARTICLE_P; 02871 02872 float birthtime = 0.0, dietime = 0.0; 02873 float t, time = 0.0, dfra = 1.0 /* , frs_sec = sim->scene->r.frs_sec*/ /*UNUSED*/; 02874 float col[4] = {0.5f, 0.5f, 0.5f, 1.0f}; 02875 float prev_tangent[3] = {0.0f, 0.0f, 0.0f}, hairmat[4][4]; 02876 float rotmat[3][3]; 02877 int k; 02878 int steps = (int)pow(2.0, (double)(psys->renderdata ? part->ren_step : part->draw_step)); 02879 int totpart = psys->totpart; 02880 float length, vec[3]; 02881 float *vg_effector= NULL; 02882 float *vg_length= NULL, pa_length=1.0f; 02883 int keyed, baked; 02884 02885 /* we don't have anything valid to create paths from so let's quit here */ 02886 if((psys->flag & PSYS_HAIR_DONE || psys->flag & PSYS_KEYED || psys->pointcache)==0) 02887 return; 02888 02889 if(psys_in_edit_mode(sim->scene, psys)) 02890 if(psys->renderdata==0 && (psys->edit==NULL || pset->flag & PE_DRAW_PART)==0) 02891 return; 02892 02893 keyed = psys->flag & PSYS_KEYED; 02894 baked = psys->pointcache->mem_cache.first && psys->part->type != PART_HAIR; 02895 02896 /* clear out old and create new empty path cache */ 02897 psys_free_path_cache(psys, psys->edit); 02898 cache= psys->pathcache= psys_alloc_path_cache_buffers(&psys->pathcachebufs, totpart, steps+1); 02899 02900 psys->lattice = psys_get_lattice(sim); 02901 ma= give_current_material(sim->ob, psys->part->omat); 02902 if(ma && (psys->part->draw_col == PART_DRAW_COL_MAT)) 02903 VECCOPY(col, &ma->r) 02904 02905 if((psys->flag & PSYS_GLOBAL_HAIR)==0) { 02906 if((psys->part->flag & PART_CHILD_EFFECT)==0) 02907 vg_effector = psys_cache_vgroup(psmd->dm, psys, PSYS_VG_EFFECTOR); 02908 02909 if(!psys->totchild) 02910 vg_length = psys_cache_vgroup(psmd->dm, psys, PSYS_VG_LENGTH); 02911 } 02912 02913 /*---first main loop: create all actual particles' paths---*/ 02914 LOOP_SHOWN_PARTICLES { 02915 if(!psys->totchild) { 02916 psys_get_texture(sim, pa, &ptex, PAMAP_LENGTH, 0.f); 02917 pa_length = ptex.length * (1.0f - part->randlength * PSYS_FRAND(psys->seed + p)); 02918 if(vg_length) 02919 pa_length *= psys_particle_value_from_verts(psmd->dm,part->from,pa,vg_length); 02920 } 02921 02922 pind.keyed = keyed; 02923 pind.cache = baked ? psys->pointcache : NULL; 02924 pind.epoint = NULL; 02925 pind.bspline = (psys->part->flag & PART_HAIR_BSPLINE); 02926 pind.dm = hair_dm; 02927 02928 memset(cache[p], 0, sizeof(*cache[p])*(steps+1)); 02929 02930 cache[p]->steps = steps; 02931 02932 /*--get the first data points--*/ 02933 init_particle_interpolation(sim->ob, sim->psys, pa, &pind); 02934 02935 /* hairmat is needed for for non-hair particle too so we get proper rotations */ 02936 psys_mat_hair_to_global(sim->ob, psmd->dm, psys->part->from, pa, hairmat); 02937 VECCOPY(rotmat[0], hairmat[2]); 02938 VECCOPY(rotmat[1], hairmat[1]); 02939 VECCOPY(rotmat[2], hairmat[0]); 02940 02941 if(part->draw & PART_ABS_PATH_TIME) { 02942 birthtime = MAX2(pind.birthtime, part->path_start); 02943 dietime = MIN2(pind.dietime, part->path_end); 02944 } 02945 else { 02946 float tb = pind.birthtime; 02947 birthtime = tb + part->path_start * (pind.dietime - tb); 02948 dietime = tb + part->path_end * (pind.dietime - tb); 02949 } 02950 02951 if(birthtime >= dietime) { 02952 cache[p]->steps = -1; 02953 continue; 02954 } 02955 02956 dietime = birthtime + pa_length * (dietime - birthtime); 02957 02958 /*--interpolate actual path from data points--*/ 02959 for(k=0, ca=cache[p]; k<=steps; k++, ca++){ 02960 time = (float)k / (float)steps; 02961 t = birthtime + time * (dietime - birthtime); 02962 result.time = -t; 02963 do_particle_interpolation(psys, p, pa, t, &pind, &result); 02964 copy_v3_v3(ca->co, result.co); 02965 02966 /* dynamic hair is in object space */ 02967 /* keyed and baked are already in global space */ 02968 if(hair_dm) 02969 mul_m4_v3(sim->ob->obmat, ca->co); 02970 else if(!keyed && !baked && !(psys->flag & PSYS_GLOBAL_HAIR)) 02971 mul_m4_v3(hairmat, ca->co); 02972 02973 copy_v3_v3(ca->col, col); 02974 } 02975 02976 /*--modify paths and calculate rotation & velocity--*/ 02977 02978 if(!(psys->flag & PSYS_GLOBAL_HAIR)) { 02979 /* apply effectors */ 02980 if((psys->part->flag & PART_CHILD_EFFECT) == 0) { 02981 float effector= 1.0f; 02982 if(vg_effector) 02983 effector*= psys_particle_value_from_verts(psmd->dm,psys->part->from,pa,vg_effector); 02984 02985 sub_v3_v3v3(vec,(cache[p]+1)->co,cache[p]->co); 02986 length = len_v3(vec); 02987 02988 for(k=1, ca=cache[p]+1; k<=steps; k++, ca++) 02989 do_path_effectors(sim, p, ca, k, steps, cache[p]->co, effector, dfra, cfra, &length, vec); 02990 } 02991 02992 /* apply guide curves to path data */ 02993 if(sim->psys->effectors && (psys->part->flag & PART_CHILD_EFFECT)==0) { 02994 for(k=0, ca=cache[p]; k<=steps; k++, ca++) 02995 /* ca is safe to cast, since only co and vel are used */ 02996 do_guides(sim->psys->effectors, (ParticleKey*)ca, p, (float)k/(float)steps); 02997 } 02998 02999 /* lattices have to be calculated separately to avoid mixups between effector calculations */ 03000 if(psys->lattice) { 03001 for(k=0, ca=cache[p]; k<=steps; k++, ca++) 03002 calc_latt_deform(psys->lattice, ca->co, 1.0f); 03003 } 03004 } 03005 03006 /* finally do rotation & velocity */ 03007 for(k=1, ca=cache[p]+1; k<=steps; k++, ca++) { 03008 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k); 03009 03010 if(k == steps) 03011 copy_qt_qt(ca->rot, (ca - 1)->rot); 03012 03013 /* set velocity */ 03014 sub_v3_v3v3(ca->vel, ca->co, (ca-1)->co); 03015 03016 if(k==1) 03017 copy_v3_v3((ca-1)->vel, ca->vel); 03018 } 03019 /* First rotation is based on emitting face orientation. 03020 * This is way better than having flipping rotations resulting 03021 * from using a global axis as a rotation pole (vec_to_quat()). 03022 * It's not an ideal solution though since it disregards the 03023 * initial tangent, but taking that in to account will allow 03024 * the possibility of flipping again. -jahka 03025 */ 03026 mat3_to_quat_is_ok(cache[p]->rot, rotmat); 03027 } 03028 03029 psys->totcached = totpart; 03030 03031 if(psys->lattice){ 03032 end_latt_deform(psys->lattice); 03033 psys->lattice= NULL; 03034 } 03035 03036 if(vg_effector) 03037 MEM_freeN(vg_effector); 03038 03039 if(vg_length) 03040 MEM_freeN(vg_length); 03041 } 03042 void psys_cache_edit_paths(Scene *scene, Object *ob, PTCacheEdit *edit, float cfra) 03043 { 03044 ParticleCacheKey *ca, **cache= edit->pathcache; 03045 ParticleEditSettings *pset = &scene->toolsettings->particle; 03046 03047 PTCacheEditPoint *point = NULL; 03048 PTCacheEditKey *ekey = NULL; 03049 03050 ParticleSystem *psys = edit->psys; 03051 ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys); 03052 ParticleData *pa = psys ? psys->particles : NULL; 03053 03054 ParticleInterpolationData pind; 03055 ParticleKey result; 03056 03057 float birthtime = 0.0f, dietime = 0.0f; 03058 float t, time = 0.0f, keytime = 0.0f /*, frs_sec */; 03059 float hairmat[4][4], rotmat[3][3], prev_tangent[3] = {0.0f, 0.0f, 0.0f}; 03060 int k, i; 03061 int steps = (int)pow(2.0, (double)pset->draw_step); 03062 int totpart = edit->totpoint, recalc_set=0; 03063 float sel_col[3]; 03064 float nosel_col[3]; 03065 03066 steps = MAX2(steps, 4); 03067 03068 if(!cache || edit->totpoint != edit->totcached) { 03069 /* clear out old and create new empty path cache */ 03070 psys_free_path_cache(edit->psys, edit); 03071 cache= edit->pathcache= psys_alloc_path_cache_buffers(&edit->pathcachebufs, totpart, steps+1); 03072 03073 /* set flag for update (child particles check this too) */ 03074 for(i=0, point=edit->points; i<totpart; i++, point++) 03075 point->flag |= PEP_EDIT_RECALC; 03076 recalc_set = 1; 03077 } 03078 03079 /* frs_sec = (psys || edit->pid.flag & PTCACHE_VEL_PER_SEC) ? 25.0f : 1.0f; */ /* UNUSED */ 03080 03081 if(pset->brushtype == PE_BRUSH_WEIGHT) { 03082 ;/* use weight painting colors now... */ 03083 } 03084 else{ 03085 sel_col[0] = (float)edit->sel_col[0] / 255.0f; 03086 sel_col[1] = (float)edit->sel_col[1] / 255.0f; 03087 sel_col[2] = (float)edit->sel_col[2] / 255.0f; 03088 nosel_col[0] = (float)edit->nosel_col[0] / 255.0f; 03089 nosel_col[1] = (float)edit->nosel_col[1] / 255.0f; 03090 nosel_col[2] = (float)edit->nosel_col[2] / 255.0f; 03091 } 03092 03093 /*---first main loop: create all actual particles' paths---*/ 03094 for(i=0, point=edit->points; i<totpart; i++, pa+=pa?1:0, point++){ 03095 if(edit->totcached && !(point->flag & PEP_EDIT_RECALC)) 03096 continue; 03097 03098 ekey = point->keys; 03099 03100 pind.keyed = 0; 03101 pind.cache = NULL; 03102 pind.epoint = point; 03103 pind.bspline = psys ? (psys->part->flag & PART_HAIR_BSPLINE) : 0; 03104 pind.dm = NULL; 03105 03106 03107 /* should init_particle_interpolation set this ? */ 03108 if(pset->brushtype==PE_BRUSH_WEIGHT){ 03109 pind.hkey[0] = NULL; 03110 /* pa != NULL since the weight brush is only available for hair */ 03111 pind.hkey[1] = pa->hair; 03112 } 03113 03114 03115 memset(cache[i], 0, sizeof(*cache[i])*(steps+1)); 03116 03117 cache[i]->steps = steps; 03118 03119 /*--get the first data points--*/ 03120 init_particle_interpolation(ob, psys, pa, &pind); 03121 03122 if(psys) { 03123 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, hairmat); 03124 copy_v3_v3(rotmat[0], hairmat[2]); 03125 copy_v3_v3(rotmat[1], hairmat[1]); 03126 copy_v3_v3(rotmat[2], hairmat[0]); 03127 } 03128 03129 birthtime = pind.birthtime; 03130 dietime = pind.dietime; 03131 03132 if(birthtime >= dietime) { 03133 cache[i]->steps = -1; 03134 continue; 03135 } 03136 03137 /*--interpolate actual path from data points--*/ 03138 for(k=0, ca=cache[i]; k<=steps; k++, ca++){ 03139 time = (float)k / (float)steps; 03140 t = birthtime + time * (dietime - birthtime); 03141 result.time = -t; 03142 do_particle_interpolation(psys, i, pa, t, &pind, &result); 03143 copy_v3_v3(ca->co, result.co); 03144 03145 /* non-hair points are already in global space */ 03146 if(psys && !(psys->flag & PSYS_GLOBAL_HAIR)) { 03147 mul_m4_v3(hairmat, ca->co); 03148 03149 if(k) { 03150 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k); 03151 03152 if(k == steps) 03153 copy_qt_qt(ca->rot, (ca - 1)->rot); 03154 03155 /* set velocity */ 03156 sub_v3_v3v3(ca->vel, ca->co, (ca - 1)->co); 03157 03158 if(k==1) 03159 copy_v3_v3((ca - 1)->vel, ca->vel); 03160 } 03161 } 03162 else { 03163 ca->vel[0] = ca->vel[1] = 0.0f; 03164 ca->vel[1] = 1.0f; 03165 } 03166 03167 /* selection coloring in edit mode */ 03168 if(pset->brushtype==PE_BRUSH_WEIGHT){ 03169 float t2; 03170 03171 if(k==0) { 03172 weight_to_rgb(pind.hkey[1]->weight, ca->col, ca->col+1, ca->col+2); 03173 } else { 03174 float w1[3], w2[3]; 03175 keytime = (t - (*pind.ekey[0]->time))/((*pind.ekey[1]->time) - (*pind.ekey[0]->time)); 03176 03177 weight_to_rgb(pind.hkey[0]->weight, w1, w1+1, w1+2); 03178 weight_to_rgb(pind.hkey[1]->weight, w2, w2+1, w2+2); 03179 03180 interp_v3_v3v3(ca->col, w1, w2, keytime); 03181 } 03182 03183 /* at the moment this is only used for weight painting. 03184 * will need to move out of this check if its used elsewhere. */ 03185 t2 = birthtime + ((float)k/(float)steps) * (dietime - birthtime); 03186 03187 while (pind.hkey[1]->time < t2) pind.hkey[1]++; 03188 pind.hkey[0] = pind.hkey[1] - 1; 03189 } 03190 else { 03191 if((ekey + (pind.ekey[0] - point->keys))->flag & PEK_SELECT){ 03192 if((ekey + (pind.ekey[1] - point->keys))->flag & PEK_SELECT){ 03193 VECCOPY(ca->col, sel_col); 03194 } 03195 else{ 03196 keytime = (t - (*pind.ekey[0]->time))/((*pind.ekey[1]->time) - (*pind.ekey[0]->time)); 03197 interp_v3_v3v3(ca->col, sel_col, nosel_col, keytime); 03198 } 03199 } 03200 else{ 03201 if((ekey + (pind.ekey[1] - point->keys))->flag & PEK_SELECT){ 03202 keytime = (t - (*pind.ekey[0]->time))/((*pind.ekey[1]->time) - (*pind.ekey[0]->time)); 03203 interp_v3_v3v3(ca->col, nosel_col, sel_col, keytime); 03204 } 03205 else{ 03206 VECCOPY(ca->col, nosel_col); 03207 } 03208 } 03209 } 03210 03211 ca->time = t; 03212 } 03213 if(psys && !(psys->flag & PSYS_GLOBAL_HAIR)) { 03214 /* First rotation is based on emitting face orientation. 03215 * This is way better than having flipping rotations resulting 03216 * from using a global axis as a rotation pole (vec_to_quat()). 03217 * It's not an ideal solution though since it disregards the 03218 * initial tangent, but taking that in to account will allow 03219 * the possibility of flipping again. -jahka 03220 */ 03221 mat3_to_quat_is_ok(cache[i]->rot, rotmat); 03222 } 03223 } 03224 03225 edit->totcached = totpart; 03226 03227 if(psys) { 03228 ParticleSimulationData sim= {0}; 03229 sim.scene= scene; 03230 sim.ob= ob; 03231 sim.psys= psys; 03232 sim.psmd= psys_get_modifier(ob, psys); 03233 03234 psys_cache_child_paths(&sim, cfra, 1); 03235 } 03236 03237 /* clear recalc flag if set here */ 03238 if(recalc_set) { 03239 for(i=0, point=edit->points; i<totpart; i++, point++) 03240 point->flag &= ~PEP_EDIT_RECALC; 03241 } 03242 } 03243 /************************************************/ 03244 /* Particle Key handling */ 03245 /************************************************/ 03246 void copy_particle_key(ParticleKey *to, ParticleKey *from, int time){ 03247 if(time){ 03248 memcpy(to,from,sizeof(ParticleKey)); 03249 } 03250 else{ 03251 float to_time=to->time; 03252 memcpy(to,from,sizeof(ParticleKey)); 03253 to->time=to_time; 03254 } 03255 } 03256 void psys_get_from_key(ParticleKey *key, float *loc, float *vel, float *rot, float *time){ 03257 if(loc) VECCOPY(loc,key->co); 03258 if(vel) VECCOPY(vel,key->vel); 03259 if(rot) QUATCOPY(rot,key->rot); 03260 if(time) *time=key->time; 03261 } 03262 /*-------changing particle keys from space to another-------*/ 03263 #if 0 03264 static void key_from_object(Object *ob, ParticleKey *key){ 03265 float q[4]; 03266 03267 VECADD(key->vel,key->vel,key->co); 03268 03269 mul_m4_v3(ob->obmat,key->co); 03270 mul_m4_v3(ob->obmat,key->vel); 03271 mat4_to_quat(q,ob->obmat); 03272 03273 VECSUB(key->vel,key->vel,key->co); 03274 mul_qt_qtqt(key->rot,q,key->rot); 03275 } 03276 #endif 03277 03278 static void triatomat(float *v1, float *v2, float *v3, float (*uv)[2], float mat[][4]) 03279 { 03280 float det, w1, w2, d1[2], d2[2]; 03281 03282 memset(mat, 0, sizeof(float)*4*4); 03283 mat[3][3]= 1.0f; 03284 03285 /* first axis is the normal */ 03286 normal_tri_v3( mat[2],v1, v2, v3); 03287 03288 /* second axis along (1, 0) in uv space */ 03289 if(uv) { 03290 d1[0]= uv[1][0] - uv[0][0]; 03291 d1[1]= uv[1][1] - uv[0][1]; 03292 d2[0]= uv[2][0] - uv[0][0]; 03293 d2[1]= uv[2][1] - uv[0][1]; 03294 03295 det = d2[0]*d1[1] - d2[1]*d1[0]; 03296 03297 if(det != 0.0f) { 03298 det= 1.0f/det; 03299 w1= -d2[1]*det; 03300 w2= d1[1]*det; 03301 03302 mat[1][0]= w1*(v2[0] - v1[0]) + w2*(v3[0] - v1[0]); 03303 mat[1][1]= w1*(v2[1] - v1[1]) + w2*(v3[1] - v1[1]); 03304 mat[1][2]= w1*(v2[2] - v1[2]) + w2*(v3[2] - v1[2]); 03305 normalize_v3(mat[1]); 03306 } 03307 else 03308 mat[1][0]= mat[1][1]= mat[1][2]= 0.0f; 03309 } 03310 else { 03311 sub_v3_v3v3(mat[1], v2, v1); 03312 normalize_v3(mat[1]); 03313 } 03314 03315 /* third as a cross product */ 03316 cross_v3_v3v3(mat[0], mat[1], mat[2]); 03317 } 03318 03319 static void psys_face_mat(Object *ob, DerivedMesh *dm, ParticleData *pa, float mat[][4], int orco) 03320 { 03321 float v[3][3]; 03322 MFace *mface; 03323 OrigSpaceFace *osface; 03324 float (*orcodata)[3]; 03325 03326 int i = pa->num_dmcache==DMCACHE_NOTFOUND ? pa->num : pa->num_dmcache; 03327 03328 if (i==-1 || i >= dm->getNumFaces(dm)) { unit_m4(mat); return; } 03329 03330 mface=dm->getFaceData(dm,i,CD_MFACE); 03331 osface=dm->getFaceData(dm,i,CD_ORIGSPACE); 03332 03333 if(orco && (orcodata=dm->getVertDataArray(dm, CD_ORCO))) { 03334 VECCOPY(v[0], orcodata[mface->v1]); 03335 VECCOPY(v[1], orcodata[mface->v2]); 03336 VECCOPY(v[2], orcodata[mface->v3]); 03337 03338 /* ugly hack to use non-transformed orcos, since only those 03339 * give symmetric results for mirroring in particle mode */ 03340 if(DM_get_vert_data_layer(dm, CD_ORIGINDEX)) 03341 transform_mesh_orco_verts(ob->data, v, 3, 1); 03342 } 03343 else { 03344 dm->getVertCo(dm,mface->v1,v[0]); 03345 dm->getVertCo(dm,mface->v2,v[1]); 03346 dm->getVertCo(dm,mface->v3,v[2]); 03347 } 03348 03349 triatomat(v[0], v[1], v[2], (osface)? osface->uv: NULL, mat); 03350 } 03351 03352 void psys_mat_hair_to_object(Object *UNUSED(ob), DerivedMesh *dm, short from, ParticleData *pa, float hairmat[][4]) 03353 { 03354 float vec[3]; 03355 03356 psys_face_mat(0, dm, pa, hairmat, 0); 03357 psys_particle_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, vec, 0, 0, 0, 0, 0); 03358 VECCOPY(hairmat[3],vec); 03359 } 03360 03361 void psys_mat_hair_to_orco(Object *ob, DerivedMesh *dm, short from, ParticleData *pa, float hairmat[][4]) 03362 { 03363 float vec[3], orco[3]; 03364 03365 psys_face_mat(ob, dm, pa, hairmat, 1); 03366 psys_particle_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, vec, 0, 0, 0, orco, 0); 03367 03368 /* see psys_face_mat for why this function is called */ 03369 if(DM_get_vert_data_layer(dm, CD_ORIGINDEX)) 03370 transform_mesh_orco_verts(ob->data, &orco, 1, 1); 03371 VECCOPY(hairmat[3],orco); 03372 } 03373 03374 void psys_vec_rot_to_face(DerivedMesh *dm, ParticleData *pa, float *vec) 03375 { 03376 float mat[4][4]; 03377 03378 psys_face_mat(0, dm, pa, mat, 0); 03379 transpose_m4(mat); /* cheap inverse for rotation matrix */ 03380 mul_mat3_m4_v3(mat, vec); 03381 } 03382 03383 void psys_mat_hair_to_global(Object *ob, DerivedMesh *dm, short from, ParticleData *pa, float hairmat[][4]) 03384 { 03385 float facemat[4][4]; 03386 03387 psys_mat_hair_to_object(ob, dm, from, pa, facemat); 03388 03389 mul_m4_m4m4(hairmat, facemat, ob->obmat); 03390 } 03391 03392 /************************************************/ 03393 /* ParticleSettings handling */ 03394 /************************************************/ 03395 ModifierData *object_add_particle_system(Scene *scene, Object *ob, const char *name) 03396 { 03397 ParticleSystem *psys; 03398 ModifierData *md; 03399 ParticleSystemModifierData *psmd; 03400 03401 if(!ob || ob->type != OB_MESH) 03402 return NULL; 03403 03404 psys = ob->particlesystem.first; 03405 for(; psys; psys=psys->next) 03406 psys->flag &= ~PSYS_CURRENT; 03407 03408 psys = MEM_callocN(sizeof(ParticleSystem), "particle_system"); 03409 psys->pointcache = BKE_ptcache_add(&psys->ptcaches); 03410 BLI_addtail(&ob->particlesystem, psys); 03411 03412 psys->part = psys_new_settings("ParticleSettings", NULL); 03413 03414 if(BLI_countlist(&ob->particlesystem)>1) 03415 sprintf(psys->name, "ParticleSystem %i", BLI_countlist(&ob->particlesystem)); 03416 else 03417 strcpy(psys->name, "ParticleSystem"); 03418 03419 md= modifier_new(eModifierType_ParticleSystem); 03420 03421 if(name) BLI_strncpy(md->name, name, sizeof(md->name)); 03422 else sprintf(md->name, "ParticleSystem %i", BLI_countlist(&ob->particlesystem)); 03423 modifier_unique_name(&ob->modifiers, md); 03424 03425 psmd= (ParticleSystemModifierData*) md; 03426 psmd->psys=psys; 03427 BLI_addtail(&ob->modifiers, md); 03428 03429 psys->totpart=0; 03430 psys->flag = PSYS_ENABLED|PSYS_CURRENT; 03431 psys->cfra=bsystem_time(scene,ob,scene->r.cfra+1,0.0); 03432 03433 DAG_scene_sort(G.main, scene); 03434 DAG_id_tag_update(&ob->id, OB_RECALC_DATA); 03435 03436 return md; 03437 } 03438 void object_remove_particle_system(Scene *scene, Object *ob) 03439 { 03440 ParticleSystem *psys = psys_get_current(ob); 03441 ParticleSystemModifierData *psmd; 03442 ModifierData *md; 03443 03444 if(!psys) 03445 return; 03446 03447 /* clear all other appearances of this pointer (like on smoke flow modifier) */ 03448 if((md = modifiers_findByType(ob, eModifierType_Smoke))) 03449 { 03450 SmokeModifierData *smd = (SmokeModifierData *)md; 03451 if((smd->type == MOD_SMOKE_TYPE_FLOW) && smd->flow && smd->flow->psys) 03452 if(smd->flow->psys == psys) 03453 smd->flow->psys = NULL; 03454 } 03455 03456 /* clear modifier */ 03457 psmd= psys_get_modifier(ob, psys); 03458 BLI_remlink(&ob->modifiers, psmd); 03459 modifier_free((ModifierData *)psmd); 03460 03461 /* clear particle system */ 03462 BLI_remlink(&ob->particlesystem, psys); 03463 psys_free(ob,psys); 03464 03465 if(ob->particlesystem.first) 03466 ((ParticleSystem *) ob->particlesystem.first)->flag |= PSYS_CURRENT; 03467 else 03468 ob->mode &= ~OB_MODE_PARTICLE_EDIT; 03469 03470 DAG_scene_sort(G.main, scene); 03471 DAG_id_tag_update(&ob->id, OB_RECALC_DATA); 03472 } 03473 static void default_particle_settings(ParticleSettings *part) 03474 { 03475 part->type= PART_EMITTER; 03476 part->distr= PART_DISTR_JIT; 03477 part->draw_as = PART_DRAW_REND; 03478 part->ren_as = PART_DRAW_HALO; 03479 part->bb_uv_split=1; 03480 part->bb_align=PART_BB_VIEW; 03481 part->bb_split_offset=PART_BB_OFF_LINEAR; 03482 part->flag=PART_EDISTR|PART_TRAND|PART_HIDE_ADVANCED_HAIR; 03483 03484 part->sta= 1.0; 03485 part->end= 200.0; 03486 part->lifetime= 50.0; 03487 part->jitfac= 1.0; 03488 part->totpart= 1000; 03489 part->grid_res= 10; 03490 part->timetweak= 1.0; 03491 03492 part->integrator= PART_INT_MIDPOINT; 03493 part->phystype= PART_PHYS_NEWTON; 03494 part->hair_step= 5; 03495 part->keys_step= 5; 03496 part->draw_step= 2; 03497 part->ren_step= 3; 03498 part->adapt_angle= 5; 03499 part->adapt_pix= 3; 03500 part->kink_axis= 2; 03501 part->kink_amp_clump= 1.f; 03502 part->reactevent= PART_EVENT_DEATH; 03503 part->disp=100; 03504 part->from= PART_FROM_FACE; 03505 03506 part->normfac= 1.0f; 03507 03508 part->mass=1.0; 03509 part->size=0.05; 03510 part->childsize=1.0; 03511 03512 part->rotmode = PART_ROT_VEL; 03513 part->avemode = PART_AVE_SPIN; 03514 03515 part->child_nbr=10; 03516 part->ren_child_nbr=100; 03517 part->childrad=0.2f; 03518 part->childflat=0.0f; 03519 part->clumppow=0.0f; 03520 part->kink_amp=0.2f; 03521 part->kink_freq=2.0; 03522 03523 part->rough1_size=1.0; 03524 part->rough2_size=1.0; 03525 part->rough_end_shape=1.0; 03526 03527 part->clength=1.0f; 03528 part->clength_thres=0.0f; 03529 03530 part->draw= PART_DRAW_EMITTER; 03531 part->draw_line[0]=0.5; 03532 part->path_start = 0.0f; 03533 part->path_end = 1.0f; 03534 03535 part->bb_size[0] = part->bb_size[1] = 1.0f; 03536 03537 part->keyed_loops = 1; 03538 03539 part->color_vec_max = 1.f; 03540 part->draw_col = PART_DRAW_COL_MAT; 03541 03542 part->simplify_refsize= 1920; 03543 part->simplify_rate= 1.0f; 03544 part->simplify_transition= 0.1f; 03545 part->simplify_viewport= 0.8; 03546 03547 if(!part->effector_weights) 03548 part->effector_weights = BKE_add_effector_weights(NULL); 03549 } 03550 03551 03552 ParticleSettings *psys_new_settings(const char *name, Main *main) 03553 { 03554 ParticleSettings *part; 03555 03556 if(main==NULL) 03557 main = G.main; 03558 03559 part= alloc_libblock(&main->particle, ID_PA, name); 03560 03561 default_particle_settings(part); 03562 03563 return part; 03564 } 03565 03566 ParticleSettings *psys_copy_settings(ParticleSettings *part) 03567 { 03568 ParticleSettings *partn; 03569 int a; 03570 03571 partn= copy_libblock(part); 03572 partn->pd= MEM_dupallocN(part->pd); 03573 partn->pd2= MEM_dupallocN(part->pd2); 03574 partn->effector_weights= MEM_dupallocN(part->effector_weights); 03575 partn->fluid= MEM_dupallocN(part->fluid); 03576 03577 partn->boids = boid_copy_settings(part->boids); 03578 03579 for(a=0; a<MAX_MTEX; a++) { 03580 if(part->mtex[a]) { 03581 partn->mtex[a]= MEM_mallocN(sizeof(MTex), "psys_copy_tex"); 03582 memcpy(partn->mtex[a], part->mtex[a], sizeof(MTex)); 03583 id_us_plus((ID *)partn->mtex[a]->tex); 03584 } 03585 } 03586 03587 BLI_duplicatelist(&partn->dupliweights, &part->dupliweights); 03588 03589 return partn; 03590 } 03591 03592 static void expand_local_particlesettings(ParticleSettings *part) 03593 { 03594 int i; 03595 id_lib_extern((ID *)part->dup_group); 03596 03597 for(i=0; i<MAX_MTEX; i++) { 03598 if(part->mtex[i]) id_lib_extern((ID *)part->mtex[i]->tex); 03599 } 03600 } 03601 03602 void make_local_particlesettings(ParticleSettings *part) 03603 { 03604 Main *bmain= G.main; 03605 Object *ob; 03606 int local=0, lib=0; 03607 03608 /* - only lib users: do nothing 03609 * - only local users: set flag 03610 * - mixed: make copy 03611 */ 03612 03613 if(part->id.lib==0) return; 03614 if(part->id.us==1) { 03615 part->id.lib= 0; 03616 part->id.flag= LIB_LOCAL; 03617 new_id(&bmain->particle, (ID *)part, 0); 03618 expand_local_particlesettings(part); 03619 return; 03620 } 03621 03622 /* test objects */ 03623 for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) { 03624 ParticleSystem *psys=ob->particlesystem.first; 03625 for(; psys; psys=psys->next){ 03626 if(psys->part==part) { 03627 if(ob->id.lib) lib= 1; 03628 else local= 1; 03629 } 03630 } 03631 } 03632 03633 if(local && lib==0) { 03634 part->id.lib= 0; 03635 part->id.flag= LIB_LOCAL; 03636 new_id(&bmain->particle, (ID *)part, 0); 03637 expand_local_particlesettings(part); 03638 } 03639 else if(local && lib) { 03640 ParticleSettings *partn= psys_copy_settings(part); 03641 partn->id.us= 0; 03642 03643 /* do objects */ 03644 for(ob= bmain->object.first; ob; ob= ob->id.next) { 03645 ParticleSystem *psys; 03646 for(psys= ob->particlesystem.first; psys; psys=psys->next){ 03647 if(psys->part==part && ob->id.lib==0) { 03648 psys->part= partn; 03649 partn->id.us++; 03650 part->id.us--; 03651 } 03652 } 03653 } 03654 } 03655 } 03656 03657 /************************************************/ 03658 /* Textures */ 03659 /************************************************/ 03660 03661 static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, float *fuv, char *name, float *texco) 03662 { 03663 MFace *mf; 03664 MTFace *tf; 03665 int i; 03666 03667 tf= CustomData_get_layer_named(&dm->faceData, CD_MTFACE, name); 03668 03669 if(tf == NULL) 03670 tf= CustomData_get_layer(&dm->faceData, CD_MTFACE); 03671 03672 if(tf == NULL) 03673 return 0; 03674 03675 if(pa) { 03676 i= (pa->num_dmcache==DMCACHE_NOTFOUND)? pa->num: pa->num_dmcache; 03677 if(i >= dm->getNumFaces(dm)) 03678 i = -1; 03679 } 03680 else 03681 i= face_index; 03682 03683 if (i==-1) { 03684 texco[0]= 0.0f; 03685 texco[1]= 0.0f; 03686 texco[2]= 0.0f; 03687 } 03688 else { 03689 mf= dm->getFaceData(dm, i, CD_MFACE); 03690 03691 psys_interpolate_uvs(&tf[i], mf->v4, fuv, texco); 03692 03693 texco[0]= texco[0]*2.0f - 1.0f; 03694 texco[1]= texco[1]*2.0f - 1.0f; 03695 texco[2]= 0.0f; 03696 } 03697 03698 return 1; 03699 } 03700 03701 #define SET_PARTICLE_TEXTURE(type, pvalue, texfac) if((event & mtex->mapto) & type) {pvalue = texture_value_blend(def, pvalue, value, texfac, blend);} 03702 #define CLAMP_PARTICLE_TEXTURE_POS(type, pvalue) if(event & type) { if(pvalue < 0.f) pvalue = 1.f+pvalue; CLAMP(pvalue, 0.0f, 1.0f); } 03703 #define CLAMP_PARTICLE_TEXTURE_POSNEG(type, pvalue) if(event & type) { CLAMP(pvalue, -1.0f, 1.0f); } 03704 03705 static void get_cpa_texture(DerivedMesh *dm, ParticleSystem *psys, ParticleSettings *part, ParticleData *par, int child_index, int face_index, float *fw, float *orco, ParticleTexture *ptex, int event, float cfra) 03706 { 03707 MTex *mtex, **mtexp = part->mtex; 03708 int m; 03709 float value, rgba[4], texvec[3]; 03710 03711 ptex->ivel = ptex->life = ptex->exist = ptex->size = ptex->damp = 03712 ptex->gravity = ptex->field = ptex->time = ptex->clump = ptex->kink = 03713 ptex->effector = ptex->rough1 = ptex->rough2 = ptex->roughe = 1.f; 03714 03715 ptex->length= 1.0f - part->randlength * PSYS_FRAND(child_index + 26); 03716 ptex->length*= part->clength_thres < PSYS_FRAND(child_index + 27) ? part->clength : 1.0f; 03717 03718 for(m=0; m<MAX_MTEX; m++, mtexp++){ 03719 mtex = *mtexp; 03720 if(mtex && mtex->mapto){ 03721 float def=mtex->def_var; 03722 short blend=mtex->blendtype; 03723 short texco = mtex->texco; 03724 03725 if(ELEM(texco, TEXCO_UV, TEXCO_ORCO) && (ELEM(part->from, PART_FROM_FACE, PART_FROM_VOLUME) == 0 || part->distr == PART_DISTR_GRID)) 03726 texco = TEXCO_GLOB; 03727 03728 switch(texco) { 03729 case TEXCO_GLOB: 03730 copy_v3_v3(texvec, par->state.co); 03731 break; 03732 case TEXCO_OBJECT: 03733 copy_v3_v3(texvec, par->state.co); 03734 if(mtex->object) 03735 mul_m4_v3(mtex->object->imat, texvec); 03736 break; 03737 case TEXCO_UV: 03738 if(fw && get_particle_uv(dm, NULL, face_index, fw, mtex->uvname, texvec)) 03739 break; 03740 /* no break, failed to get uv's, so let's try orco's */ 03741 case TEXCO_ORCO: 03742 copy_v3_v3(texvec, orco); 03743 break; 03744 case TEXCO_PARTICLE: 03745 /* texture coordinates in range [-1,1] */ 03746 texvec[0] = 2.f * (cfra - par->time)/(par->dietime-par->time) - 1.f; 03747 texvec[1] = 0.f; 03748 texvec[2] = 0.f; 03749 break; 03750 } 03751 03752 externtex(mtex, texvec, &value, rgba, rgba+1, rgba+2, rgba+3, 0); 03753 03754 if((event & mtex->mapto) & PAMAP_ROUGH) 03755 ptex->rough1= ptex->rough2= ptex->roughe= texture_value_blend(def,ptex->rough1,value,mtex->roughfac,blend); 03756 03757 SET_PARTICLE_TEXTURE(PAMAP_LENGTH, ptex->length, mtex->lengthfac); 03758 SET_PARTICLE_TEXTURE(PAMAP_CLUMP, ptex->clump, mtex->clumpfac); 03759 SET_PARTICLE_TEXTURE(PAMAP_KINK, ptex->kink, mtex->kinkfac); 03760 SET_PARTICLE_TEXTURE(PAMAP_DENS, ptex->exist, mtex->padensfac); 03761 } 03762 } 03763 03764 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_LENGTH, ptex->length); 03765 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_CLUMP, ptex->clump); 03766 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_KINK, ptex->kink); 03767 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_ROUGH, ptex->rough1); 03768 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_DENS, ptex->exist); 03769 } 03770 void psys_get_texture(ParticleSimulationData *sim, ParticleData *pa, ParticleTexture *ptex, int event, float cfra) 03771 { 03772 ParticleSettings *part = sim->psys->part; 03773 MTex **mtexp = part->mtex; 03774 MTex *mtex; 03775 int m; 03776 float value, rgba[4], co[3], texvec[3]; 03777 int setvars=0; 03778 03779 /* initialize ptex */ 03780 ptex->ivel = ptex->life = ptex->exist = ptex->size = ptex->damp = 03781 ptex->gravity = ptex->field = ptex->length = ptex->clump = ptex->kink = 03782 ptex->effector = ptex->rough1 = ptex->rough2 = ptex->roughe = 1.f; 03783 03784 ptex->time = (float)(pa - sim->psys->particles)/(float)sim->psys->totpart; 03785 03786 for(m=0; m<MAX_MTEX; m++, mtexp++){ 03787 mtex = *mtexp; 03788 if(mtex && mtex->mapto){ 03789 float def=mtex->def_var; 03790 short blend=mtex->blendtype; 03791 short texco = mtex->texco; 03792 03793 if(texco == TEXCO_UV && (ELEM(part->from, PART_FROM_FACE, PART_FROM_VOLUME) == 0 || part->distr == PART_DISTR_GRID)) 03794 texco = TEXCO_GLOB; 03795 03796 switch(texco) { 03797 case TEXCO_GLOB: 03798 copy_v3_v3(texvec, pa->state.co); 03799 break; 03800 case TEXCO_OBJECT: 03801 copy_v3_v3(texvec, pa->state.co); 03802 if(mtex->object) 03803 mul_m4_v3(mtex->object->imat, texvec); 03804 break; 03805 case TEXCO_UV: 03806 if(get_particle_uv(sim->psmd->dm, pa, 0, pa->fuv, mtex->uvname, texvec)) 03807 break; 03808 /* no break, failed to get uv's, so let's try orco's */ 03809 case TEXCO_ORCO: 03810 psys_particle_on_emitter(sim->psmd,sim->psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,0,0,0,texvec, 0); 03811 break; 03812 case TEXCO_PARTICLE: 03813 /* texture coordinates in range [-1,1] */ 03814 texvec[0] = 2.f * (cfra - pa->time)/(pa->dietime-pa->time) - 1.f; 03815 texvec[1] = 0.f; 03816 texvec[2] = 0.f; 03817 break; 03818 } 03819 03820 externtex(mtex, texvec, &value, rgba, rgba+1, rgba+2, rgba+3, 0); 03821 03822 if((event & mtex->mapto) & PAMAP_TIME) { 03823 /* the first time has to set the base value for time regardless of blend mode */ 03824 if((setvars&MAP_PA_TIME)==0){ 03825 int flip= (mtex->timefac < 0.0f); 03826 float timefac= fabsf(mtex->timefac); 03827 ptex->time *= 1.0f - timefac; 03828 ptex->time += timefac * ((flip)? 1.0f - value : value); 03829 setvars |= MAP_PA_TIME; 03830 } 03831 else 03832 ptex->time= texture_value_blend(def,ptex->time,value,mtex->timefac,blend); 03833 } 03834 SET_PARTICLE_TEXTURE(PAMAP_LIFE, ptex->life, mtex->lifefac) 03835 SET_PARTICLE_TEXTURE(PAMAP_DENS, ptex->exist, mtex->padensfac) 03836 SET_PARTICLE_TEXTURE(PAMAP_SIZE, ptex->size, mtex->sizefac) 03837 SET_PARTICLE_TEXTURE(PAMAP_IVEL, ptex->ivel, mtex->ivelfac) 03838 SET_PARTICLE_TEXTURE(PAMAP_FIELD, ptex->field, mtex->fieldfac) 03839 SET_PARTICLE_TEXTURE(PAMAP_GRAVITY, ptex->gravity, mtex->gravityfac) 03840 SET_PARTICLE_TEXTURE(PAMAP_DAMP, ptex->damp, mtex->dampfac) 03841 SET_PARTICLE_TEXTURE(PAMAP_LENGTH, ptex->length, mtex->lengthfac) 03842 } 03843 } 03844 03845 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_TIME, ptex->time) 03846 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_LIFE, ptex->life) 03847 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_DENS, ptex->exist) 03848 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_SIZE, ptex->size) 03849 CLAMP_PARTICLE_TEXTURE_POSNEG(PAMAP_IVEL, ptex->ivel) 03850 CLAMP_PARTICLE_TEXTURE_POSNEG(PAMAP_FIELD, ptex->field) 03851 CLAMP_PARTICLE_TEXTURE_POSNEG(PAMAP_GRAVITY, ptex->gravity) 03852 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_DAMP, ptex->damp) 03853 CLAMP_PARTICLE_TEXTURE_POS(PAMAP_LENGTH, ptex->length) 03854 } 03855 /************************************************/ 03856 /* Particle State */ 03857 /************************************************/ 03858 float psys_get_timestep(ParticleSimulationData *sim) 03859 { 03860 return 0.04f * sim->psys->part->timetweak; 03861 } 03862 float psys_get_child_time(ParticleSystem *psys, ChildParticle *cpa, float cfra, float *birthtime, float *dietime) 03863 { 03864 ParticleSettings *part = psys->part; 03865 float time, life; 03866 03867 if(part->childtype==PART_CHILD_FACES){ 03868 int w=0; 03869 time=0.0; 03870 while(w<4 && cpa->pa[w]>=0){ 03871 time+=cpa->w[w]*(psys->particles+cpa->pa[w])->time; 03872 w++; 03873 } 03874 03875 life = part->lifetime * (1.0f - part->randlife * PSYS_FRAND(cpa - psys->child + 25)); 03876 } 03877 else{ 03878 ParticleData *pa = psys->particles + cpa->parent; 03879 03880 time = pa->time; 03881 life = pa->lifetime; 03882 } 03883 03884 if(birthtime) 03885 *birthtime = time; 03886 if(dietime) 03887 *dietime = time+life; 03888 03889 return (cfra-time)/life; 03890 } 03891 float psys_get_child_size(ParticleSystem *psys, ChildParticle *cpa, float UNUSED(cfra), float *UNUSED(pa_time)) 03892 { 03893 ParticleSettings *part = psys->part; 03894 float size; // time XXX 03895 03896 if(part->childtype==PART_CHILD_FACES) 03897 size=part->size; 03898 else 03899 size=psys->particles[cpa->parent].size; 03900 03901 size*=part->childsize; 03902 03903 if(part->childrandsize != 0.0f) 03904 size *= 1.0f - part->childrandsize * PSYS_FRAND(cpa - psys->child + 26); 03905 03906 return size; 03907 } 03908 static void get_child_modifier_parameters(ParticleSettings *part, ParticleThreadContext *ctx, ChildParticle *cpa, short cpa_from, int cpa_num, float *cpa_fuv, float *orco, ParticleTexture *ptex) 03909 { 03910 ParticleSystem *psys = ctx->sim.psys; 03911 int i = cpa - psys->child; 03912 03913 get_cpa_texture(ctx->dm, psys, part, psys->particles + cpa->pa[0], i, cpa_num, cpa_fuv, orco, ptex, PAMAP_DENS|PAMAP_CHILD, psys->cfra); 03914 03915 03916 if(ptex->exist < PSYS_FRAND(i + 24)) 03917 return; 03918 03919 if(ctx->vg_length) 03920 ptex->length*=psys_interpolate_value_from_verts(ctx->dm,cpa_from,cpa_num,cpa_fuv,ctx->vg_length); 03921 if(ctx->vg_clump) 03922 ptex->clump*=psys_interpolate_value_from_verts(ctx->dm,cpa_from,cpa_num,cpa_fuv,ctx->vg_clump); 03923 if(ctx->vg_kink) 03924 ptex->kink*=psys_interpolate_value_from_verts(ctx->dm,cpa_from,cpa_num,cpa_fuv,ctx->vg_kink); 03925 if(ctx->vg_rough1) 03926 ptex->rough1*=psys_interpolate_value_from_verts(ctx->dm,cpa_from,cpa_num,cpa_fuv,ctx->vg_rough1); 03927 if(ctx->vg_rough2) 03928 ptex->rough2*=psys_interpolate_value_from_verts(ctx->dm,cpa_from,cpa_num,cpa_fuv,ctx->vg_rough2); 03929 if(ctx->vg_roughe) 03930 ptex->roughe*=psys_interpolate_value_from_verts(ctx->dm,cpa_from,cpa_num,cpa_fuv,ctx->vg_roughe); 03931 if(ctx->vg_effector) 03932 ptex->effector*=psys_interpolate_value_from_verts(ctx->dm,cpa_from,cpa_num,cpa_fuv,ctx->vg_effector); 03933 } 03934 static void do_child_modifiers(ParticleSimulationData *sim, ParticleTexture *ptex, ParticleKey *par, float *par_rot, ChildParticle *cpa, float *orco, float mat[4][4], ParticleKey *state, float t) 03935 { 03936 ParticleSettings *part = sim->psys->part; 03937 int i = cpa - sim->psys->child; 03938 int guided = 0; 03939 03940 float kink_freq = part->kink_freq; 03941 float rough1 = part->rough1; 03942 float rough2 = part->rough2; 03943 float rough_end = part->rough_end; 03944 03945 if(ptex) { 03946 kink_freq *= ptex->kink; 03947 rough1 *= ptex->rough1; 03948 rough2 *= ptex->rough2; 03949 rough_end *= ptex->roughe; 03950 } 03951 03952 if(part->flag & PART_CHILD_EFFECT) 03953 /* state is safe to cast, since only co and vel are used */ 03954 guided = do_guides(sim->psys->effectors, (ParticleKey*)state, cpa->parent, t); 03955 03956 if(guided==0){ 03957 float clump = do_clump(state, par, t, part->clumpfac, part->clumppow, ptex ? ptex->clump : 1.f); 03958 03959 if(kink_freq != 0.f) { 03960 float kink_amp = part->kink_amp * (1.f - part->kink_amp_clump * clump); 03961 03962 do_kink(state, par, par_rot, t, kink_freq, part->kink_shape, 03963 kink_amp, part->kink_flat, part->kink, part->kink_axis, 03964 sim->ob->obmat, sim->psys->part->childtype == PART_CHILD_FACES); 03965 } 03966 } 03967 03968 if(rough1 > 0.f) 03969 do_rough(orco, mat, t, rough1, part->rough1_size, 0.0, state); 03970 03971 if(rough2 > 0.f) 03972 do_rough(sim->psys->frand + ((i + 27) % (PSYS_FRAND_COUNT - 3)), mat, t, rough2, part->rough2_size, part->rough2_thres, state); 03973 03974 if(rough_end > 0.f) 03975 do_rough_end(sim->psys->frand + ((i + 27) % (PSYS_FRAND_COUNT - 3)), mat, t, rough_end, part->rough_end_shape, state); 03976 } 03977 /* get's hair (or keyed) particles state at the "path time" specified in state->time */ 03978 void psys_get_particle_on_path(ParticleSimulationData *sim, int p, ParticleKey *state, int vel) 03979 { 03980 PARTICLE_PSMD; 03981 ParticleSystem *psys = sim->psys; 03982 ParticleSettings *part = sim->psys->part; 03983 Material *ma = give_current_material(sim->ob, part->omat); 03984 ParticleData *pa; 03985 ChildParticle *cpa; 03986 ParticleTexture ptex; 03987 ParticleKey *par=0, keys[4], tstate; 03988 ParticleThreadContext ctx; /* fake thread context for child modifiers */ 03989 ParticleInterpolationData pind; 03990 03991 float t; 03992 float co[3], orco[3]; 03993 float hairmat[4][4]; 03994 int totpart = psys->totpart; 03995 int totchild = psys->totchild; 03996 short between = 0, edit = 0; 03997 03998 int keyed = part->phystype & PART_PHYS_KEYED && psys->flag & PSYS_KEYED; 03999 int cached = !keyed && part->type != PART_HAIR; 04000 04001 float *cpa_fuv; int cpa_num; short cpa_from; 04002 04003 /* initialize keys to zero */ 04004 memset(keys, 0, 4*sizeof(ParticleKey)); 04005 04006 t=state->time; 04007 CLAMP(t, 0.0f, 1.0f); 04008 04009 if(p<totpart){ 04010 pa = psys->particles + p; 04011 pind.keyed = keyed; 04012 pind.cache = cached ? psys->pointcache : NULL; 04013 pind.epoint = NULL; 04014 pind.bspline = (psys->part->flag & PART_HAIR_BSPLINE); 04015 /* pind.dm disabled in editmode means we dont get effectors taken into 04016 * account when subdividing for instance */ 04017 pind.dm = psys_in_edit_mode(sim->scene, psys) ? NULL : psys->hair_out_dm; 04018 init_particle_interpolation(sim->ob, psys, pa, &pind); 04019 do_particle_interpolation(psys, p, pa, t, &pind, state); 04020 04021 if(pind.dm) { 04022 mul_m4_v3(sim->ob->obmat, state->co); 04023 mul_mat3_m4_v3(sim->ob->obmat, state->vel); 04024 } 04025 else if(!keyed && !cached && !(psys->flag & PSYS_GLOBAL_HAIR)) { 04026 if((pa->flag & PARS_REKEY)==0) { 04027 psys_mat_hair_to_global(sim->ob, sim->psmd->dm, part->from, pa, hairmat); 04028 mul_m4_v3(hairmat, state->co); 04029 mul_mat3_m4_v3(hairmat, state->vel); 04030 04031 if(sim->psys->effectors && (part->flag & PART_CHILD_GUIDE)==0) { 04032 do_guides(sim->psys->effectors, state, p, state->time); 04033 /* TODO: proper velocity handling */ 04034 } 04035 04036 if(psys->lattice && edit==0) 04037 calc_latt_deform(psys->lattice, state->co,1.0f); 04038 } 04039 } 04040 } 04041 else if(totchild){ 04042 //invert_m4_m4(imat,ob->obmat); 04043 04044 cpa=psys->child+p-totpart; 04045 04046 if(state->time < 0.0f) 04047 t = psys_get_child_time(psys, cpa, -state->time, NULL, NULL); 04048 04049 if(totchild && part->childtype==PART_CHILD_FACES){ 04050 /* part->parents could still be 0 so we can't test with totparent */ 04051 between=1; 04052 } 04053 if(between){ 04054 int w = 0; 04055 float foffset; 04056 04057 /* get parent states */ 04058 while(w<4 && cpa->pa[w]>=0){ 04059 keys[w].time = state->time; 04060 psys_get_particle_on_path(sim, cpa->pa[w], keys+w, 1); 04061 w++; 04062 } 04063 04064 /* get the original coordinates (orco) for texture usage */ 04065 cpa_num=cpa->num; 04066 04067 foffset= cpa->foffset; 04068 cpa_fuv = cpa->fuv; 04069 cpa_from = PART_FROM_FACE; 04070 04071 psys_particle_on_emitter(psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa->fuv,foffset,co,0,0,0,orco,0); 04072 04073 /* we need to save the actual root position of the child for positioning it accurately to the surface of the emitter */ 04074 //VECCOPY(cpa_1st,co); 04075 04076 //mul_m4_v3(ob->obmat,cpa_1st); 04077 04078 pa = psys->particles + cpa->parent; 04079 04080 if(part->type == PART_HAIR) 04081 psys_mat_hair_to_global(sim->ob, sim->psmd->dm, psys->part->from, pa, hairmat); 04082 else 04083 unit_m4(hairmat); 04084 04085 pa=0; 04086 } 04087 else{ 04088 /* get the parent state */ 04089 keys->time = state->time; 04090 psys_get_particle_on_path(sim, cpa->parent, keys,1); 04091 04092 /* get the original coordinates (orco) for texture usage */ 04093 pa=psys->particles+cpa->parent; 04094 04095 cpa_from=part->from; 04096 cpa_num=pa->num; 04097 cpa_fuv=pa->fuv; 04098 04099 04100 04101 if(part->type == PART_HAIR) { 04102 psys_particle_on_emitter(psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa_fuv,pa->foffset,co,0,0,0,orco,0); 04103 psys_mat_hair_to_global(sim->ob, sim->psmd->dm, psys->part->from, pa, hairmat); 04104 } 04105 else { 04106 copy_v3_v3(orco, cpa->fuv); 04107 unit_m4(hairmat); 04108 } 04109 } 04110 04111 /* correct child ipo timing */ 04112 #if 0 // XXX old animation system 04113 if((part->flag&PART_ABS_TIME)==0 && part->ipo){ 04114 calc_ipo(part->ipo, 100.0f*t); 04115 execute_ipo((ID *)part, part->ipo); 04116 } 04117 #endif // XXX old animation system 04118 04119 /* get different child parameters from textures & vgroups */ 04120 memset(&ctx, 0, sizeof(ParticleThreadContext)); 04121 ctx.sim = *sim; 04122 ctx.dm = psmd->dm; 04123 ctx.ma = ma; 04124 /* TODO: assign vertex groups */ 04125 get_child_modifier_parameters(part, &ctx, cpa, cpa_from, cpa_num, cpa_fuv, orco, &ptex); 04126 04127 if(between){ 04128 int w=0; 04129 04130 state->co[0] = state->co[1] = state->co[2] = 0.0f; 04131 state->vel[0] = state->vel[1] = state->vel[2] = 0.0f; 04132 04133 /* child position is the weighted sum of parent positions */ 04134 while(w<4 && cpa->pa[w]>=0){ 04135 state->co[0] += cpa->w[w] * keys[w].co[0]; 04136 state->co[1] += cpa->w[w] * keys[w].co[1]; 04137 state->co[2] += cpa->w[w] * keys[w].co[2]; 04138 04139 state->vel[0] += cpa->w[w] * keys[w].vel[0]; 04140 state->vel[1] += cpa->w[w] * keys[w].vel[1]; 04141 state->vel[2] += cpa->w[w] * keys[w].vel[2]; 04142 w++; 04143 } 04144 /* apply offset for correct positioning */ 04145 //VECADD(state->co,state->co,cpa_1st); 04146 } 04147 else{ 04148 /* offset the child from the parent position */ 04149 offset_child(cpa, keys, keys->rot, state, part->childflat, part->childrad); 04150 } 04151 04152 par = keys; 04153 04154 if(vel) 04155 copy_particle_key(&tstate, state, 1); 04156 04157 /* apply different deformations to the child path */ 04158 do_child_modifiers(sim, &ptex, par, par->rot, cpa, orco, hairmat, state, t); 04159 04160 /* try to estimate correct velocity */ 04161 if(vel){ 04162 ParticleKey tstate; 04163 float length = len_v3(state->vel); 04164 04165 if(t>=0.001f){ 04166 tstate.time=t-0.001f; 04167 psys_get_particle_on_path(sim,p,&tstate,0); 04168 VECSUB(state->vel,state->co,tstate.co); 04169 normalize_v3(state->vel); 04170 } 04171 else{ 04172 tstate.time=t+0.001f; 04173 psys_get_particle_on_path(sim,p,&tstate,0); 04174 VECSUB(state->vel,tstate.co,state->co); 04175 normalize_v3(state->vel); 04176 } 04177 04178 mul_v3_fl(state->vel, length); 04179 } 04180 } 04181 } 04182 /* gets particle's state at a time, returns 1 if particle exists and can be seen and 0 if not */ 04183 int psys_get_particle_state(ParticleSimulationData *sim, int p, ParticleKey *state, int always){ 04184 ParticleSystem *psys = sim->psys; 04185 ParticleSettings *part = psys->part; 04186 ParticleData *pa = NULL; 04187 ChildParticle *cpa = NULL; 04188 float cfra; 04189 int totpart = psys->totpart; 04190 float timestep = psys_get_timestep(sim); 04191 04192 /* negative time means "use current time" */ 04193 cfra = state->time > 0 ? state->time : bsystem_time(sim->scene, 0, (float)sim->scene->r.cfra, 0.0); 04194 04195 if(p>=totpart){ 04196 if(!psys->totchild) 04197 return 0; 04198 04199 if(part->childtype == PART_CHILD_FACES){ 04200 if(!(psys->flag & PSYS_KEYED)) 04201 return 0; 04202 04203 cpa = psys->child + p - totpart; 04204 04205 state->time = psys_get_child_time(psys, cpa, cfra, NULL, NULL); 04206 04207 if(!always) 04208 if((state->time < 0.0f && !(part->flag & PART_UNBORN)) 04209 || (state->time > 1.0f && !(part->flag & PART_DIED))) 04210 return 0; 04211 04212 state->time= (cfra - (part->sta + (part->end - part->sta) * PSYS_FRAND(p + 23))) / (part->lifetime * PSYS_FRAND(p + 24)); 04213 04214 psys_get_particle_on_path(sim, p, state,1); 04215 return 1; 04216 } 04217 else { 04218 cpa = sim->psys->child + p - totpart; 04219 pa = sim->psys->particles + cpa->parent; 04220 } 04221 } 04222 else { 04223 pa = sim->psys->particles + p; 04224 } 04225 04226 if(pa) { 04227 if(!always) 04228 if((cfra < pa->time && (part->flag & PART_UNBORN)==0) 04229 || (cfra > pa->dietime && (part->flag & PART_DIED)==0)) 04230 return 0; 04231 04232 cfra = MIN2(cfra, pa->dietime); 04233 } 04234 04235 if(sim->psys->flag & PSYS_KEYED){ 04236 state->time= -cfra; 04237 psys_get_particle_on_path(sim, p, state,1); 04238 return 1; 04239 } 04240 else{ 04241 if(cpa){ 04242 float mat[4][4]; 04243 ParticleKey *key1; 04244 float t = (cfra - pa->time) / pa->lifetime; 04245 04246 key1=&pa->state; 04247 offset_child(cpa, key1, key1->rot, state, part->childflat, part->childrad); 04248 04249 CLAMP(t, 0.0f, 1.0f); 04250 04251 unit_m4(mat); 04252 do_child_modifiers(sim, NULL, key1, key1->rot, cpa, cpa->fuv, mat, state, t); 04253 04254 if(psys->lattice) 04255 calc_latt_deform(sim->psys->lattice, state->co,1.0f); 04256 } 04257 else{ 04258 if(pa->state.time==cfra || ELEM(part->phystype,PART_PHYS_NO,PART_PHYS_KEYED)) 04259 copy_particle_key(state, &pa->state, 1); 04260 else if(pa->prev_state.time==cfra) 04261 copy_particle_key(state, &pa->prev_state, 1); 04262 else { 04263 float dfra, frs_sec = sim->scene->r.frs_sec; 04264 /* let's interpolate to try to be as accurate as possible */ 04265 if(pa->state.time + 2.f >= state->time && pa->prev_state.time - 2.f <= state->time) { 04266 if(pa->prev_state.time >= pa->state.time || pa->prev_state.time < 0.f) { 04267 /* prev_state is wrong so let's not use it, this can happen at frames 1, 0 or particle birth */ 04268 dfra = state->time - pa->state.time; 04269 04270 copy_particle_key(state, &pa->state, 1); 04271 04272 madd_v3_v3v3fl(state->co, state->co, state->vel, dfra/frs_sec); 04273 } 04274 else { 04275 ParticleKey keys[4]; 04276 float keytime; 04277 04278 copy_particle_key(keys+1, &pa->prev_state, 1); 04279 copy_particle_key(keys+2, &pa->state, 1); 04280 04281 dfra = keys[2].time - keys[1].time; 04282 04283 keytime = (state->time - keys[1].time) / dfra; 04284 04285 /* convert velocity to timestep size */ 04286 mul_v3_fl(keys[1].vel, dfra * timestep); 04287 mul_v3_fl(keys[2].vel, dfra * timestep); 04288 04289 psys_interpolate_particle(-1, keys, keytime, state, 1); 04290 04291 /* convert back to real velocity */ 04292 mul_v3_fl(state->vel, 1.f / (dfra * timestep)); 04293 04294 interp_v3_v3v3(state->ave, keys[1].ave, keys[2].ave, keytime); 04295 interp_qt_qtqt(state->rot, keys[1].rot, keys[2].rot, keytime); 04296 } 04297 } 04298 else if(pa->state.time + 1.f >= state->time && pa->state.time - 1.f <= state->time) { 04299 /* linear interpolation using only pa->state */ 04300 04301 dfra = state->time - pa->state.time; 04302 04303 copy_particle_key(state, &pa->state, 1); 04304 04305 madd_v3_v3v3fl(state->co, state->co, state->vel, dfra/frs_sec); 04306 } 04307 else { 04308 /* extrapolating over big ranges is not accurate so let's just give something close to reasonable back */ 04309 copy_particle_key(state, &pa->state, 0); 04310 } 04311 } 04312 04313 if(sim->psys->lattice) 04314 calc_latt_deform(sim->psys->lattice, state->co,1.0f); 04315 } 04316 04317 return 1; 04318 } 04319 } 04320 04321 void psys_get_dupli_texture(ParticleSystem *psys, ParticleSettings *part, ParticleSystemModifierData *psmd, ParticleData *pa, ChildParticle *cpa, float *uv, float *orco) 04322 { 04323 MFace *mface; 04324 MTFace *mtface; 04325 float loc[3]; 04326 int num; 04327 04328 uv[0] = uv[1] = 0.f; 04329 04330 if(cpa) { 04331 if(part->childtype == PART_CHILD_FACES) { 04332 mtface= CustomData_get_layer(&psmd->dm->faceData, CD_MTFACE); 04333 if(mtface) { 04334 mface= psmd->dm->getFaceData(psmd->dm, cpa->num, CD_MFACE); 04335 mtface += cpa->num; 04336 psys_interpolate_uvs(mtface, mface->v4, cpa->fuv, uv); 04337 } 04338 04339 psys_particle_on_emitter(psmd,PART_FROM_FACE,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,loc,0,0,0,orco,0); 04340 return; 04341 } 04342 else { 04343 pa = psys->particles + cpa->pa[0]; 04344 } 04345 } 04346 04347 if(part->from == PART_FROM_FACE) { 04348 mtface= CustomData_get_layer(&psmd->dm->faceData, CD_MTFACE); 04349 num= pa->num_dmcache; 04350 04351 if(num == DMCACHE_NOTFOUND) 04352 num= pa->num; 04353 04354 if (num >= psmd->dm->getNumFaces(psmd->dm)) { 04355 /* happens when simplify is enabled 04356 * gives invalid coords but would crash otherwise */ 04357 num= DMCACHE_NOTFOUND; 04358 } 04359 04360 if(mtface && num != DMCACHE_NOTFOUND) { 04361 mface= psmd->dm->getFaceData(psmd->dm, num, CD_MFACE); 04362 mtface += num; 04363 psys_interpolate_uvs(mtface, mface->v4, pa->fuv, uv); 04364 } 04365 } 04366 04367 psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,0,0,0,orco,0); 04368 } 04369 04370 void psys_get_dupli_path_transform(ParticleSimulationData *sim, ParticleData *pa, ChildParticle *cpa, ParticleCacheKey *cache, float mat[][4], float *scale) 04371 { 04372 Object *ob = sim->ob; 04373 ParticleSystem *psys = sim->psys; 04374 ParticleSystemModifierData *psmd = sim->psmd; 04375 float loc[3], nor[3], vec[3], side[3], len; 04376 float xvec[3] = {-1.0, 0.0, 0.0}, nmat[3][3]; 04377 04378 sub_v3_v3v3(vec, (cache+cache->steps)->co, cache->co); 04379 len= normalize_v3(vec); 04380 04381 if(pa == NULL && psys->part->childflat != PART_CHILD_FACES) 04382 pa = psys->particles + cpa->pa[0]; 04383 04384 if(pa) 04385 psys_particle_on_emitter(psmd,sim->psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,nor,0,0,0,0); 04386 else 04387 psys_particle_on_emitter(psmd,PART_FROM_FACE,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,loc,nor,0,0,0,0); 04388 04389 copy_m3_m4(nmat, ob->imat); 04390 transpose_m3(nmat); 04391 mul_m3_v3(nmat, nor); 04392 normalize_v3(nor); 04393 04394 /* make sure that we get a proper side vector */ 04395 if(fabs(dot_v3v3(nor,vec))>0.999999) { 04396 if(fabs(dot_v3v3(nor,xvec))>0.999999) { 04397 nor[0] = 0.0f; 04398 nor[1] = 1.0f; 04399 nor[2] = 0.0f; 04400 } 04401 else { 04402 nor[0] = 1.0f; 04403 nor[1] = 0.0f; 04404 nor[2] = 0.0f; 04405 } 04406 } 04407 cross_v3_v3v3(side, nor, vec); 04408 normalize_v3(side); 04409 cross_v3_v3v3(nor, vec, side); 04410 04411 unit_m4(mat); 04412 VECCOPY(mat[0], vec); 04413 VECCOPY(mat[1], side); 04414 VECCOPY(mat[2], nor); 04415 04416 *scale= len; 04417 } 04418 04419 void psys_make_billboard(ParticleBillboardData *bb, float xvec[3], float yvec[3], float zvec[3], float center[3]) 04420 { 04421 float onevec[3] = {0.0f,0.0f,0.0f}, tvec[3], tvec2[3]; 04422 04423 xvec[0] = 1.0f; xvec[1] = 0.0f; xvec[2] = 0.0f; 04424 yvec[0] = 0.0f; yvec[1] = 1.0f; yvec[2] = 0.0f; 04425 04426 /* can happen with bad pointcache or physics calculation 04427 * since this becomes geometry, nan's and inf's crash raytrace code. 04428 * better not allow this. */ 04429 if( !finite(bb->vec[0]) || !finite(bb->vec[1]) || !finite(bb->vec[2]) || 04430 !finite(bb->vel[0]) || !finite(bb->vel[1]) || !finite(bb->vel[2]) ) 04431 { 04432 zero_v3(bb->vec); 04433 zero_v3(bb->vel); 04434 04435 zero_v3(xvec); 04436 zero_v3(yvec); 04437 zero_v3(zvec); 04438 zero_v3(center); 04439 04440 return; 04441 } 04442 04443 if(bb->align < PART_BB_VIEW) 04444 onevec[bb->align]=1.0f; 04445 04446 if(bb->lock && (bb->align == PART_BB_VIEW)) { 04447 normalize_v3_v3(xvec, bb->ob->obmat[0]); 04448 normalize_v3_v3(yvec, bb->ob->obmat[1]); 04449 normalize_v3_v3(zvec, bb->ob->obmat[2]); 04450 } 04451 else if(bb->align == PART_BB_VEL) { 04452 float temp[3]; 04453 04454 normalize_v3_v3(temp, bb->vel); 04455 04456 VECSUB(zvec, bb->ob->obmat[3], bb->vec); 04457 04458 if(bb->lock) { 04459 float fac = -dot_v3v3(zvec, temp); 04460 04461 VECADDFAC(zvec, zvec, temp, fac); 04462 } 04463 normalize_v3(zvec); 04464 04465 cross_v3_v3v3(xvec,temp,zvec); 04466 normalize_v3(xvec); 04467 04468 cross_v3_v3v3(yvec,zvec,xvec); 04469 } 04470 else { 04471 VECSUB(zvec, bb->ob->obmat[3], bb->vec); 04472 if(bb->lock) 04473 zvec[bb->align] = 0.0f; 04474 normalize_v3(zvec); 04475 04476 if(bb->align < PART_BB_VIEW) 04477 cross_v3_v3v3(xvec, onevec, zvec); 04478 else 04479 cross_v3_v3v3(xvec, bb->ob->obmat[1], zvec); 04480 normalize_v3(xvec); 04481 04482 cross_v3_v3v3(yvec,zvec,xvec); 04483 } 04484 04485 VECCOPY(tvec, xvec); 04486 VECCOPY(tvec2, yvec); 04487 04488 mul_v3_fl(xvec, cos(bb->tilt * (float)M_PI)); 04489 mul_v3_fl(tvec2, sin(bb->tilt * (float)M_PI)); 04490 VECADD(xvec, xvec, tvec2); 04491 04492 mul_v3_fl(yvec, cos(bb->tilt * (float)M_PI)); 04493 mul_v3_fl(tvec, -sin(bb->tilt * (float)M_PI)); 04494 VECADD(yvec, yvec, tvec); 04495 04496 mul_v3_fl(xvec, bb->size[0]); 04497 mul_v3_fl(yvec, bb->size[1]); 04498 04499 VECADDFAC(center, bb->vec, xvec, bb->offset[0]); 04500 VECADDFAC(center, center, yvec, bb->offset[1]); 04501 } 04502 04503 04504 void psys_apply_hair_lattice(Scene *scene, Object *ob, ParticleSystem *psys) { 04505 ParticleSimulationData sim= {0}; 04506 sim.scene= scene; 04507 sim.ob= ob; 04508 sim.psys= psys; 04509 sim.psmd= psys_get_modifier(ob, psys); 04510 04511 psys->lattice = psys_get_lattice(&sim); 04512 04513 if(psys->lattice) { 04514 ParticleData *pa = psys->particles; 04515 HairKey *hkey; 04516 int p, h; 04517 float hairmat[4][4], imat[4][4]; 04518 04519 for(p=0; p<psys->totpart; p++, pa++) { 04520 psys_mat_hair_to_global(sim.ob, sim.psmd->dm, psys->part->from, pa, hairmat); 04521 invert_m4_m4(imat, hairmat); 04522 04523 hkey = pa->hair; 04524 for(h=0; h<pa->totkey; h++, hkey++) { 04525 mul_m4_v3(hairmat, hkey->co); 04526 calc_latt_deform(psys->lattice, hkey->co, 1.0f); 04527 mul_m4_v3(imat, hkey->co); 04528 } 04529 } 04530 04531 end_latt_deform(psys->lattice); 04532 psys->lattice= NULL; 04533 04534 /* protect the applied shape */ 04535 psys->flag |= PSYS_EDITED; 04536 } 04537 }