d283c5808c69fcde5ab05a4cb5959d761d9e74f7
[blender.git] / source / blender / blenkernel / intern / effect.c
1 /*  effect.c
2  * 
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <math.h>
36 #include <stdlib.h>
37
38 #include "MEM_guardedalloc.h"
39 #include "DNA_listBase.h"
40 #include "DNA_effect_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_key_types.h"
47 #include "DNA_texture_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_lattice_types.h"
50 #include "DNA_ipo_types.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_arithb.h"
54 #include "BLI_rand.h"
55
56 #include "BKE_action.h"
57 #include "BKE_bad_level_calls.h"
58 #include "BKE_blender.h"
59 #include "BKE_constraint.h"
60 #include "BKE_deform.h"
61 #include "BKE_displist.h"
62 #include "BKE_DerivedMesh.h"
63 #include "BKE_effect.h"
64 #include "BKE_global.h"
65 #include "BKE_ipo.h"
66 #include "BKE_key.h"
67 #include "BKE_lattice.h"
68 #include "BKE_mesh.h"
69 #include "BKE_material.h"
70 #include "BKE_main.h"
71 #include "BKE_object.h"
72 #include "BKE_screen.h"
73 #include "BKE_utildefines.h"
74
75 #include "render.h"  // externtex, bad level call (ton)
76
77
78 #ifdef HAVE_CONFIG_H
79 #include <config.h>
80 #endif
81
82 Effect *add_effect(int type)
83 {
84         Effect *eff=0;
85         BuildEff *bld;
86         PartEff *paf;
87         WaveEff *wav;
88         int a;
89         
90         switch(type) {
91         case EFF_BUILD:
92                 bld= MEM_callocN(sizeof(BuildEff), "neweff");
93                 eff= (Effect *)bld;
94                 
95                 bld->sfra= 1.0;
96                 bld->len= 100.0;
97                 break;
98                 
99         case EFF_PARTICLE:
100                 paf= MEM_callocN(sizeof(PartEff), "neweff");
101                 eff= (Effect *)paf;
102                 
103                 paf->sta= 1.0;
104                 paf->end= 100.0;
105                 paf->lifetime= 50.0;
106                 for(a=0; a<PAF_MAXMULT; a++) {
107                         paf->life[a]= 50.0;
108                         paf->child[a]= 4;
109                         paf->mat[a]= 1;
110                 }
111                 
112                 paf->totpart= 1000;
113                 paf->totkey= 8;
114                 paf->staticstep= 5;
115                 paf->defvec[2]= 1.0f;
116                 paf->nabla= 0.05f;
117                 
118                 break;
119                 
120         case EFF_WAVE:
121                 wav= MEM_callocN(sizeof(WaveEff), "neweff");
122                 eff= (Effect *)wav;
123                 
124                 wav->flag |= (WAV_X+WAV_Y+WAV_CYCL);
125                 
126                 wav->height= 0.5f;
127                 wav->width= 1.5f;
128                 wav->speed= 0.5f;
129                 wav->narrow= 1.5f;
130                 wav->lifetime= 0.0f;
131                 wav->damp= 10.0f;
132                 
133                 break;
134         }
135         
136         eff->type= eff->buttype= type;
137         eff->flag |= SELECT;
138         
139         return eff;
140 }
141
142 void free_effect(Effect *eff)
143 {
144         PartEff *paf;
145         
146         if(eff->type==EFF_PARTICLE) {
147                 paf= (PartEff *)eff;
148                 if(paf->keys) MEM_freeN(paf->keys);
149         }
150         MEM_freeN(eff); 
151 }
152
153
154 void free_effects(ListBase *lb)
155 {
156         Effect *eff;
157         
158         eff= lb->first;
159         while(eff) {
160                 BLI_remlink(lb, eff);
161                 free_effect(eff);
162                 eff= lb->first;
163         }
164 }
165
166 Effect *copy_effect(Effect *eff) 
167 {
168         Effect *effn;
169
170         effn= MEM_dupallocN(eff);
171         if(effn->type==EFF_PARTICLE) ((PartEff *)effn)->keys= 0;
172
173         return effn;    
174 }
175
176 void copy_act_effect(Object *ob)
177 {
178         /* return a copy of the active effect */
179         Effect *effn, *eff;
180         
181         eff= ob->effect.first;
182         while(eff) {
183                 if(eff->flag & SELECT) {
184                         
185                         effn= copy_effect(eff);
186                         BLI_addtail(&ob->effect, effn);
187                         
188                         eff->flag &= ~SELECT;
189                         return;
190                         
191                 }
192                 eff= eff->next;
193         }
194         
195         /* when it comes here: add new effect */
196         eff= add_effect(EFF_BUILD);
197         BLI_addtail(&ob->effect, eff);
198                         
199 }
200
201 void copy_effects(ListBase *lbn, ListBase *lb)
202 {
203         Effect *eff, *effn;
204
205         lbn->first= lbn->last= 0;
206
207         eff= lb->first;
208         while(eff) {
209                 effn= copy_effect(eff);
210                 BLI_addtail(lbn, effn);
211                 
212                 eff= eff->next;
213         }
214         
215 }
216
217 void deselectall_eff(Object *ob)
218 {
219         Effect *eff= ob->effect.first;
220         
221         while(eff) {
222                 eff->flag &= ~SELECT;
223                 eff= eff->next;
224         }
225 }
226
227 void set_buildvars(Object *ob, int *start, int *end)
228 {
229         BuildEff *bld;
230         float ctime;
231         
232         bld= ob->effect.first;
233         while(bld) {
234                 if(bld->type==EFF_BUILD) {
235                         ctime= bsystem_time(ob, 0, (float)G.scene->r.cfra, bld->sfra-1.0f);
236                         if(ctime < 0.0) {
237                                 *end= *start;
238                         }
239                         else if(ctime < bld->len) {
240                                 *end= *start+ (int)((*end - *start)*ctime/bld->len);
241                         }
242                         
243                         return;
244                 }
245                 bld= bld->next;
246         }
247 }
248
249 /* ***************** PARTICLES ***************** */
250
251 Particle *new_particle(PartEff *paf)
252 {
253         static Particle *pa;
254         static int cur;
255
256         /* we agree: when paf->keys==0: alloc */        
257         if(paf->keys==0) {
258                 pa= paf->keys= MEM_callocN( paf->totkey*paf->totpart*sizeof(Particle), "particlekeys" );
259                 cur= 0;
260         }
261         else {
262                 if(cur && cur<paf->totpart) pa+=paf->totkey;
263                 cur++;
264         }
265         return pa;
266 }
267
268 PartEff *give_parteff(Object *ob)
269 {
270         PartEff *paf;
271         
272         paf= ob->effect.first;
273         while(paf) {
274                 if(paf->type==EFF_PARTICLE) return paf;
275                 paf= paf->next;
276         }
277         return 0;
278 }
279
280 void where_is_particle(PartEff *paf, Particle *pa, float ctime, float *vec)
281 {
282         Particle *p[4];
283         float dt, t[4];
284         int a;
285         
286         if(paf->totkey==1) {
287                 VECCOPY(vec, pa->co);
288                 return;
289         }
290         
291         /* first find the first particlekey */
292         a= (int)((paf->totkey-1)*(ctime-pa->time)/pa->lifetime);
293         if(a>=paf->totkey) a= paf->totkey-1;
294         else if(a<0) a= 0;
295         
296         pa+= a;
297         
298         if(a>0) p[0]= pa-1; else p[0]= pa;
299         p[1]= pa;
300         
301         if(a+1<paf->totkey) p[2]= pa+1; else p[2]= pa;
302         if(a+2<paf->totkey) p[3]= pa+2; else p[3]= p[2];
303         
304         if(p[1]==p[2]) dt= 0.0;
305         else dt= (ctime-p[1]->time)/(p[2]->time - p[1]->time);
306
307         if(paf->flag & PAF_BSPLINE) set_four_ipo(dt, t, KEY_BSPLINE);
308         else set_four_ipo(dt, t, KEY_CARDINAL);
309
310         vec[0]= t[0]*p[0]->co[0] + t[1]*p[1]->co[0] + t[2]*p[2]->co[0] + t[3]*p[3]->co[0];
311         vec[1]= t[0]*p[0]->co[1] + t[1]*p[1]->co[1] + t[2]*p[2]->co[1] + t[3]*p[3]->co[1];
312         vec[2]= t[0]*p[0]->co[2] + t[1]*p[1]->co[2] + t[2]*p[2]->co[2] + t[3]*p[3]->co[2];
313
314 }
315
316 void particle_tex(MTex *mtex, PartEff *paf, float *co, float *no)
317 {                               
318         float tin, tr, tg, tb, ta;
319         float old;
320         
321         externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
322
323         if(paf->texmap==PAF_TEXINT) {
324                 tin*= paf->texfac;
325                 no[0]+= tin*paf->defvec[0];
326                 no[1]+= tin*paf->defvec[1];
327                 no[2]+= tin*paf->defvec[2];
328         }
329         else if(paf->texmap==PAF_TEXRGB) {
330                 no[0]+= (tr-0.5f)*paf->texfac;
331                 no[1]+= (tg-0.5f)*paf->texfac;
332                 no[2]+= (tb-0.5f)*paf->texfac;
333         }
334         else {  /* PAF_TEXGRAD */
335                 
336                 old= tin;
337                 co[0]+= paf->nabla;
338                 externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
339                 no[0]+= (old-tin)*paf->texfac;
340                 
341                 co[0]-= paf->nabla;
342                 co[1]+= paf->nabla;
343                 externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
344                 no[1]+= (old-tin)*paf->texfac;
345                 
346                 co[1]-= paf->nabla;
347                 co[2]+= paf->nabla;
348                 externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
349                 no[2]+= (old-tin)*paf->texfac;
350                 
351         }
352 }
353
354 static int linetriangle(float p1[3], float p2[3], float v0[3], float v1[3], float v2[3], float *labda)
355 {
356         float p[3], s[3], d[3], e1[3], e2[3], q[3];
357         float a, f, u, v;
358         
359         VECSUB(e1, v1, v0);
360         VECSUB(e2, v2, v0);
361         VECSUB(d, p2, p1);
362         
363         Crossf(p, d, e2);
364         a = INPR(e1, p);
365         if ((a > -0.000001) && (a < 0.000001)) return 0;
366         f = 1.0f/a;
367         
368         VECSUB(s, p1, v0);
369         
370         Crossf(q, s, e1);
371         *labda = f * INPR(e2, q);
372         if ((*labda < 0.0)||(*labda > 1.0)) return 0;
373         
374         u = f * INPR(s, p);
375         if ((u < 0.0)||(u > 1.0)) return 0;
376         
377         v = f * INPR(d, q);
378         if ((v < 0.0)||((u + v) > 1.0)) return 0;
379         
380         return 1;
381 }
382
383 static void get_effector(float opco[], float force[], float speed[], float cur_time, unsigned int par_layer)
384 {
385 /*
386         Particle effector field code
387
388         Modifies the force on a particle according to its
389         relation with the effector object
390         Different kind of effectors include:
391                 Forcefields: Gravity-like attractor
392                 (force power is related to the inverse of distance to the power of a falloff value)
393                 Vortex fields: swirling effectors
394                 (particles rotate around Z-axis of the object. otherwise, same relation as)
395                 (Forcefields, but this is not done through a force/acceleration)
396         
397 */
398         Object *ob;
399         Base *base;
400         float vect_to_vert[3];
401         float force_vec[3];
402         float f_force, distance;
403         float obloc[3];
404         float force_val, ffall_val;
405         short cur_frame;
406
407         /* Cycle through objects, get total of (1/(gravity_strength * dist^gravity_power)) */
408         /* Check for min distance here? */
409         base = G.scene->base.first;
410         while (base) {
411                 if(base->lay & par_layer) {
412                         ob= base->object;
413                         if(ob->pd && ob->pd->forcefield == PFIELD_FORCE) {
414
415                                 /* Need to set r.cfra for paths (investigate, ton) */
416                                 cur_frame = G.scene->r.cfra;
417                                 G.scene->r.cfra = (short)cur_time;
418                                 where_is_object_time(ob, cur_time);
419                                 G.scene->r.cfra = cur_frame;
420                                 
421                                 /* only use center of object */
422                                 obloc[0] = ob->obmat[3][0];
423                                 obloc[1] = ob->obmat[3][1];
424                                 obloc[2] = ob->obmat[3][2];
425                                 
426                                 /* Get IPO force strength and fall off values here */
427                                 if (has_ipo_code(ob->ipo, OB_PD_FSTR))
428                                         force_val = IPO_GetFloatValue(ob->ipo, OB_PD_FSTR, cur_time);
429                                 else 
430                                         force_val = ob->pd->f_strength;
431
432                                 if (has_ipo_code(ob->ipo, OB_PD_FFALL)) 
433                                         ffall_val = IPO_GetFloatValue(ob->ipo, OB_PD_FFALL, cur_time);
434                                 else 
435                                         ffall_val = ob->pd->f_power;
436
437                                 /* Now calculate the gravitational force */
438                                 VECSUB(vect_to_vert, obloc, opco);
439                                 distance = Normalise(vect_to_vert);
440
441                                 /* Limit minimum distance to vertex so that */
442                                 /* the force is not too big */
443                                 if (distance < 0.001) distance = 0.001f;
444                                 f_force = (force_val)*(1/(1000 * (float)pow((double)distance, (double)ffall_val)));
445                                 force[0] += (vect_to_vert[0] * f_force );
446                                 force[1] += (vect_to_vert[1] * f_force );
447                                 force[2] += (vect_to_vert[2] * f_force );
448
449                         }
450                         else if(ob->pd && ob->pd->forcefield == PFIELD_VORTEX) {
451                                 /* Need to set r.cfra for paths (investigate, ton) */
452                                 cur_frame = G.scene->r.cfra;
453                                 G.scene->r.cfra = (short)cur_time;
454                                 where_is_object_time(ob, cur_time);
455                                 G.scene->r.cfra = cur_frame;
456                                 
457                                 /* only use center of object */
458                                 obloc[0] = ob->obmat[3][0];
459                                 obloc[1] = ob->obmat[3][1];
460                                 obloc[2] = ob->obmat[3][2];
461                                 
462                                 /* Get IPO force strength and fall off values here */
463                                 if (has_ipo_code(ob->ipo, OB_PD_FSTR))
464                                         force_val = IPO_GetFloatValue(ob->ipo, OB_PD_FSTR, cur_time);
465                                 else 
466                                         force_val = ob->pd->f_strength;
467
468                                 if (has_ipo_code(ob->ipo, OB_PD_FFALL)) 
469                                         ffall_val = IPO_GetFloatValue(ob->ipo, OB_PD_FFALL, cur_time);
470                                 else 
471                                         ffall_val = ob->pd->f_power;
472
473                                 /* Now calculate the vortex force */
474                                 VECSUB(vect_to_vert, obloc, opco);
475
476                                 distance = Normalise(vect_to_vert);
477
478                                 Crossf(force_vec, ob->obmat[2], vect_to_vert);
479                                 Normalise(force_vec);
480
481                                 /* Limit minimum distance to vertex so that */
482                                 /* the force is not too big */
483                                 if (distance < 0.001) distance = 0.001f;
484                                 f_force = (force_val)*(1/(100 * (float)pow((double)distance, (double)ffall_val)));
485                                 speed[0] -= (force_vec[0] * f_force );
486                                 speed[1] -= (force_vec[1] * f_force );
487                                 speed[2] -= (force_vec[2] * f_force );
488
489                         }
490                 }
491                 base = base->next;
492         }
493 }
494
495 static void cache_object_vertices(Object *ob)
496 {
497         Mesh *me;
498         MVert *mvert;
499         float *fp;
500         int a;
501         
502         me= ob->data;
503         if(me->totvert==0) return;
504
505         fp= ob->sumohandle= MEM_mallocN(3*sizeof(float)*me->totvert, "cache particles");
506         mvert= me->mvert;
507         a= me->totvert;
508         while(a--) {
509                 VECCOPY(fp, mvert->co);
510                 Mat4MulVecfl(ob->obmat, fp);
511                 mvert++;
512                 fp+= 3;
513         }
514 }
515
516 static int get_deflection(float opco[3], float npco[3], float opno[3],
517         float npno[3], float life, float force[3], int def_depth,
518         float cur_time, unsigned int par_layer, int *last_object,
519                 int *last_face, int *same_face)
520 {
521         /* Particle deflection code */
522         /* The code is in two sections: the first part checks whether a particle has            */
523         /* intersected a face of a deflector mesh, given its old and new co-ords, opco and npco */
524         /* and which face it hit first                                                          */
525         /* The second part calculates the new co-ordinates given that collision and updates     */
526         /* the new co-ordinates accordingly */
527         Base *base;
528         Object *ob, *deflection_object = NULL;
529         Mesh *def_mesh;
530         MFace *mface, *deflection_face = NULL;
531         float *v1, *v2, *v3, *v4, *vcache=NULL;
532         float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3];
533         float dv1[3], dv2[3], dv3[3];
534         float vect_to_int[3], refl_vel[3];
535         float d_intersect_co[3], d_intersect_vect[3], d_nvect[3], d_i_co_above[3];
536         float forcec[3];
537         float k_point3, dist_to_plane;
538         float first_dist, ref_plane_mag;
539         float dk_plane=0, dk_point1=0;
540         float icalctop, icalcbot, n_mag;
541         float mag_iv, x_m,y_m,z_m;
542         float damping, perm_thresh;
543         float perm_val, rdamp_val;
544         int a, deflected=0, deflected_now=0;
545         float t, min_t;
546         float mat[3][3], obloc[3];
547         short cur_frame;
548         float time_before, time_after;
549         float force_mag_norm;
550         int d_object=0, d_face=0, ds_object=0, ds_face=0;
551
552         first_dist = 200000;
553         min_t = 200000;
554
555         /* The first part of the code, finding the first intersected face*/
556         base= G.scene->base.first;
557         while (base) {
558                 /*Only proceed for mesh object in same layer */
559                 if(base->object->type==OB_MESH && (base->lay & par_layer)) {
560                         ob= base->object;
561                         /* only with deflecting set */
562                         if(ob->pd && ob->pd->deflect) {
563                                 def_mesh= ob->data;
564                         
565                                 d_object = d_object + 1;
566
567                                 d_face = d_face + 1;
568                                 mface= def_mesh->mface;
569                                 a = def_mesh->totface;
570                                 
571                                 
572                                 if(ob->parent==NULL && ob->ipo==NULL) { // static
573                                         if(ob->sumohandle==NULL) cache_object_vertices(ob);
574                                         vcache= ob->sumohandle;
575                                 }
576                                 else {
577                                         /*Find out where the object is at this time*/
578                                         cur_frame = G.scene->r.cfra;
579                                         G.scene->r.cfra = (short)cur_time;
580                                         where_is_object_time(ob, cur_time);
581                                         G.scene->r.cfra = cur_frame;
582                                         
583                                         /*Pass the values from ob->obmat to mat*/
584                                         /*and the location values to obloc           */
585                                         Mat3CpyMat4(mat,ob->obmat);
586                                         obloc[0] = ob->obmat[3][0];
587                                         obloc[1] = ob->obmat[3][1];
588                                         obloc[2] = ob->obmat[3][2];
589                                 }
590                                 
591                                 while (a--) {
592
593                                         if(vcache) {
594                                                 v1= vcache+ 3*(mface->v1);
595                                                 VECCOPY(nv1, v1);
596                                                 v1= vcache+ 3*(mface->v2);
597                                                 VECCOPY(nv2, v1);
598                                                 v1= vcache+ 3*(mface->v3);
599                                                 VECCOPY(nv3, v1);
600                                                 v1= vcache+ 3*(mface->v4);
601                                                 VECCOPY(nv4, v1);
602                                         }
603                                         else {
604                                                 /* Calculate the global co-ordinates of the vertices*/
605                                                 v1= (def_mesh->mvert+(mface->v1))->co;
606                                                 v2= (def_mesh->mvert+(mface->v2))->co;
607                                                 v3= (def_mesh->mvert+(mface->v3))->co;
608                                                 v4= (def_mesh->mvert+(mface->v4))->co;
609         
610                                                 VECCOPY(nv1, v1);
611                                                 VECCOPY(nv2, v2);
612                                                 VECCOPY(nv3, v3);
613                                                 VECCOPY(nv4, v4);
614         
615                                                 /*Apply the objects deformation matrix*/
616                                                 Mat3MulVecfl(mat, nv1);
617                                                 Mat3MulVecfl(mat, nv2);
618                                                 Mat3MulVecfl(mat, nv3);
619                                                 Mat3MulVecfl(mat, nv4);
620         
621                                                 VECADD(nv1, nv1, obloc);
622                                                 VECADD(nv2, nv2, obloc);
623                                                 VECADD(nv3, nv3, obloc);
624                                                 VECADD(nv4, nv4, obloc);
625                                         }
626                                         
627                                         deflected_now = 0;
628                                         
629                                         t= 0.5; // this is labda of line, can use it optimize quad intersection
630                                         
631                                         if( linetriangle(opco, npco, nv1, nv2, nv3, &t) ) {
632                                                 if (t < min_t) {
633                                                         deflected = 1;
634                                                         deflected_now = 1;
635                                                 }
636                                         }
637                                         else if (mface->v4 && (t>=0.0 && t<=1.0)) {
638                                                 if( linetriangle(opco, npco, nv1, nv3, nv4, &t) ) {
639                                                         if (t < min_t) {
640                                                                 deflected = 1;
641                                                                 deflected_now = 2;
642                                                         }
643                                                 }
644                                         }
645                                         
646                                         if ((deflected_now > 0) && (t < min_t)) {
647                         min_t = t;
648                         ds_object = d_object;
649                                                 ds_face = d_face;
650                                                 deflection_object = ob;
651                                                 deflection_face = mface;
652                                                 if (deflected_now==1) {
653                                                         VECCOPY(dv1, nv1);
654                                                         VECCOPY(dv2, nv2);
655                                                         VECCOPY(dv3, nv3);
656                                                 }
657                                                 else {
658                                                         VECCOPY(dv1, nv1);
659                                                         VECCOPY(dv2, nv3);
660                                                         VECCOPY(dv3, nv4);
661                                                 }
662                                         }
663                                         mface++;
664                                 }
665                         }
666                 }
667                 base = base->next;
668         }
669
670         /* Here's the point to do the permeability calculation */
671         /* Set deflected to 0 if a random number is below the value */
672         /* Get the permeability IPO here*/
673         if (deflected) {
674                 
675                 if (has_ipo_code(deflection_object->ipo, OB_PD_PERM)) 
676                         perm_val = IPO_GetFloatValue(deflection_object->ipo, OB_PD_PERM, cur_time);
677                 else 
678                         perm_val = deflection_object->pd->pdef_perm;
679
680                 perm_thresh =  (float)BLI_drand() - perm_val;
681                 if (perm_thresh < 0 ) {
682                         deflected = 0;
683                 }
684         }
685
686         /* Now for the second part of the deflection code - work out the new speed */
687         /* and position of the particle if a collision occurred */
688         if (deflected) {
689         VECSUB(edge1, dv1, dv2);
690                 VECSUB(edge2, dv3, dv2);
691                 Crossf(d_nvect, edge2, edge1);
692                 n_mag = Normalise(d_nvect);
693                 dk_plane = INPR(d_nvect, nv1);
694                 dk_point1 = INPR(d_nvect,opco);
695
696                 VECSUB(d_intersect_vect, npco, opco);
697
698                 d_intersect_co[0] = opco[0] + (min_t * (npco[0] - opco[0]));
699                 d_intersect_co[1] = opco[1] + (min_t * (npco[1] - opco[1]));
700                 d_intersect_co[2] = opco[2] + (min_t * (npco[2] - opco[2]));
701                 
702                 d_i_co_above[0] = (d_intersect_co[0] + (0.001f * d_nvect[0]));
703                 d_i_co_above[1] = (d_intersect_co[1] + (0.001f * d_nvect[1]));
704                 d_i_co_above[2] = (d_intersect_co[2] + (0.001f * d_nvect[2]));
705                 mag_iv = Normalise(d_intersect_vect);
706                 VECCOPY(npco, d_intersect_co);
707                 
708                 VECSUB(vect_to_int, opco, d_intersect_co);
709                 first_dist = Normalise(vect_to_int);
710
711                 /* Work out the lengths of time before and after collision*/
712                 time_before = (life*(first_dist / (mag_iv)));
713                 time_after =  (life*((mag_iv - first_dist) / (mag_iv)));
714
715                 /* We have to recalculate what the speed would have been at the */
716                 /* point of collision, not the key frame time */
717                 npno[0]= opno[0] + time_before*force[0];
718                 npno[1]= opno[1] + time_before*force[1];
719                 npno[2]= opno[2] + time_before*force[2];
720
721
722                 /* Reflect the speed vector in the face */
723                 x_m = (2 * npno[0] * d_nvect[0]);
724                 y_m = (2 * npno[1] * d_nvect[1]);
725                 z_m = (2 * npno[2] * d_nvect[2]);
726                 refl_vel[0] = npno[0] - (d_nvect[0] * (x_m + y_m + z_m));
727                 refl_vel[1] = npno[1] - (d_nvect[1] * (x_m + y_m + z_m));
728                 refl_vel[2] = npno[2] - (d_nvect[2] * (x_m + y_m + z_m));
729
730                 /*A random variation in the damping factor........ */
731                 /*Get the IPO values for damping here*/
732                 
733                 if (has_ipo_code(deflection_object->ipo, OB_PD_SDAMP)) 
734                         damping = IPO_GetFloatValue(deflection_object->ipo, OB_PD_SDAMP, cur_time);
735                 else 
736                         damping = deflection_object->pd->pdef_damp;
737                 
738                 if (has_ipo_code(deflection_object->ipo, OB_PD_RDAMP)) 
739                         rdamp_val = IPO_GetFloatValue(deflection_object->ipo, OB_PD_RDAMP, cur_time);
740                 else 
741                         rdamp_val = deflection_object->pd->pdef_rdamp;
742
743                 damping = damping + ((1 - damping) * ((float)BLI_drand()*rdamp_val));
744                 damping = damping * damping;
745         ref_plane_mag = INPR(refl_vel,d_nvect);
746
747                 if (damping > 0.999) damping = 0.999f;
748
749                 /* Now add in the damping force - only damp in the direction of */
750                 /* the faces normal vector */
751                 npno[0] = (refl_vel[0] - (d_nvect[0] * ref_plane_mag * damping));
752                 npno[1] = (refl_vel[1] - (d_nvect[1] * ref_plane_mag * damping));
753                 npno[2] = (refl_vel[2] - (d_nvect[2] * ref_plane_mag * damping));
754
755                 /* Now reset opno */
756                 VECCOPY(opno,npno);
757                 VECCOPY(forcec, force);
758
759                 /* If the particle has bounced more than four times on the same */
760                 /* face within this cycle (depth > 4, same face > 4 )           */
761                 /* Then set the force to be only that component of the force    */
762                 /* in the same direction as the face normal                     */
763                 /* i.e. subtract the component of the force in the direction    */
764                 /* of the face normal from the actual force                     */
765                 if ((ds_object == *last_object) && (ds_face == *last_face)) {
766                         /* Increment same_face */
767                         *same_face = *same_face + 1;
768                         if ((*same_face > 3) && (def_depth > 3)) {
769                 force_mag_norm = INPR(forcec, d_nvect);
770                 forcec[0] = forcec[0] - (d_nvect[0] * force_mag_norm);
771                 forcec[1] = forcec[1] - (d_nvect[1] * force_mag_norm);
772                 forcec[2] = forcec[2] - (d_nvect[2] * force_mag_norm);
773                         }
774                 }
775                 else *same_face = 1;
776
777                 *last_object = ds_object;
778                 *last_face = ds_face;
779
780                 /* We have the particles speed at the point of collision    */
781                 /* Now we want the particles speed at the current key frame */
782
783                 npno[0]= npno[0] + time_after*forcec[0];
784                 npno[1]= npno[1] + time_after*forcec[1];
785                 npno[2]= npno[2] + time_after*forcec[2];
786
787                 /* Now we have to recalculate pa->co for the remainder*/
788                 /* of the time since the intersect*/
789                 npco[0]= npco[0] + time_after*npno[0];
790                 npco[1]= npco[1] + time_after*npno[1];
791                 npco[2]= npco[2] + time_after*npno[2];
792
793                 /* And set the old co-ordinates back to the point just above the intersection */
794                 VECCOPY(opco, d_i_co_above);
795
796                 /* Finally update the time */
797                 life = time_after;
798                 cur_time += time_before;
799
800                 /* The particle may have fallen through the face again by now!!*/
801                 /* So check if the particle has changed sides of the plane compared*/
802                 /* the co-ordinates at the last keyframe*/
803                 /* But only do this as a last resort, if we've got to the end of the */
804                 /* number of collisions allowed */
805                 if (def_depth==9) {
806                         k_point3 = INPR(d_nvect,npco);
807                         if (((dk_plane > k_point3) && (dk_plane < dk_point1))||((dk_plane < k_point3) && (dk_plane > dk_point1))) {
808
809                                 /* Yup, the pesky particle may have fallen through a hole!!! */
810                 /* So we'll cheat a bit and move the particle along the normal vector */
811                 /* until it's just the other side of the plane */
812                 icalctop = (dk_plane - d_nvect[0]*npco[0] - d_nvect[1]*npco[1] - d_nvect[2]*npco[2]);
813                 icalcbot = (d_nvect[0]*d_nvect[0] + d_nvect[1]*d_nvect[1] + d_nvect[2]*d_nvect[2]);
814                 dist_to_plane = icalctop / icalcbot;
815
816                 /*  Now just increase the distance a little to place */
817                 /* the point the other side of the plane */
818                 dist_to_plane *= 1.1f;
819                 npco[0]= npco[0] + (dist_to_plane * d_nvect[0]);
820                 npco[1]= npco[1] + (dist_to_plane * d_nvect[1]);
821                 npco[2]= npco[2] + (dist_to_plane * d_nvect[2]);
822
823                         }
824                 }
825         }
826         return deflected;
827 }
828
829 void make_particle_keys(int depth, int nr, PartEff *paf, Particle *part, float *force, int deform, MTex *mtex, unsigned int par_layer)
830 {
831         Particle *pa, *opa = NULL;
832         float damp, deltalife, life;
833         float cur_time;
834         float opco[3], opno[3], npco[3], npno[3], new_force[3], new_speed[3];
835         int b, rt1, rt2, deflected, deflection, finish_defs, def_count;
836         int last_ob, last_fc, same_fc;
837
838         damp= 1.0f-paf->damp;
839         pa= part;
840
841         /* start speed: random */
842         if(paf->randfac!=0.0) {
843                 pa->no[0]+= (float)(paf->randfac*( BLI_drand() -0.5));
844                 pa->no[1]+= (float)(paf->randfac*( BLI_drand() -0.5));
845                 pa->no[2]+= (float)(paf->randfac*( BLI_drand() -0.5));
846         }
847
848         /* start speed: texture */
849         if(mtex && paf->texfac!=0.0) {
850                 particle_tex(mtex, paf, pa->co, pa->no);
851         }
852
853         if(paf->totkey>1) deltalife= pa->lifetime/(paf->totkey-1);
854         else deltalife= pa->lifetime;
855
856         opa= pa;
857         pa++;
858
859         b= paf->totkey-1;
860         while(b--) {
861                 /* new time */
862                 pa->time= opa->time+deltalife;
863
864                 /* set initial variables                                */
865                 opco[0] = opa->co[0];
866                 opco[1] = opa->co[1];
867                 opco[2] = opa->co[2];
868
869                 new_force[0] = force[0];
870                 new_force[1] = force[1];
871                 new_force[2] = force[2];
872                 new_speed[0] = 0.0;
873                 new_speed[1] = 0.0;
874                 new_speed[2] = 0.0;
875
876                 /* Check force field */
877                 cur_time = pa->time;
878                 get_effector(opco, new_force, new_speed, cur_time, par_layer);
879
880                 /* new location */
881                 pa->co[0]= opa->co[0] + deltalife * (opa->no[0] + new_speed[0] + 0.5f*new_force[0]);
882                 pa->co[1]= opa->co[1] + deltalife * (opa->no[1] + new_speed[1] + 0.5f*new_force[1]);
883                 pa->co[2]= opa->co[2] + deltalife * (opa->no[2] + new_speed[2] + 0.5f*new_force[2]);
884
885                 /* new speed */
886                 pa->no[0]= opa->no[0] + deltalife*new_force[0];
887                 pa->no[1]= opa->no[1] + deltalife*new_force[1];
888                 pa->no[2]= opa->no[2] + deltalife*new_force[2];
889
890                 /* Particle deflection code                             */
891                 deflection = 0;
892                 finish_defs = 1;
893                 def_count = 0;
894
895                 VECCOPY(opno, opa->no);
896                 VECCOPY(npco, pa->co);
897                 VECCOPY(npno, pa->no);
898
899                 life = deltalife;
900                 cur_time -= deltalife;
901
902                 last_ob = -1;
903                 last_fc = -1;
904                 same_fc = 0;
905
906                 /* First call the particle deflection check for the particle moving   */
907                 /* between the old co-ordinates and the new co-ordinates              */
908                 /* If a deflection occurs, call the code again, this time between the */
909                 /* intersection point and the updated new co-ordinates                */
910                 /* Bail out if we've done the calculation 10 times - this seems ok     */
911         /* for most scenes I've tested */
912                 while (finish_defs) {
913                         deflected =  get_deflection(opco, npco, opno, npno, life, new_force,
914                                                         def_count, cur_time, par_layer,
915                                                         &last_ob, &last_fc, &same_fc);
916                         if (deflected) {
917                                 def_count = def_count + 1;
918                                 deflection = 1;
919                                 if (def_count==10) finish_defs = 0;
920                         }
921                         else {
922                                 finish_defs = 0;
923                         }
924                 }
925
926                 /* Only update the particle positions and speed if we had a deflection */
927                 if (deflection) {
928                         pa->co[0] = npco[0];
929                         pa->co[1] = npco[1];
930                         pa->co[2] = npco[2];
931                         pa->no[0] = npno[0];
932                         pa->no[1] = npno[1];
933                         pa->no[2] = npno[2];
934                 }
935
936
937                 /* speed: texture */
938                 if(mtex && paf->texfac!=0.0) {
939                         particle_tex(mtex, paf, pa->co, pa->no);
940                 }
941                 if(damp!=1.0) {
942                         pa->no[0]*= damp;
943                         pa->no[1]*= damp;
944                         pa->no[2]*= damp;
945                 }
946         
947
948
949                 opa= pa;
950                 pa++;
951                 /* opa is used later on too! */
952         }
953
954         if(deform) {
955                 /* deform all keys */
956                 pa= part;
957                 b= paf->totkey;
958                 while(b--) {
959                         calc_latt_deform(pa->co);
960                         pa++;
961                 }
962         }
963         
964         /* the big multiplication */
965         if(depth<PAF_MAXMULT && paf->mult[depth]!=0.0) {
966                 
967                 /* new 'child' emerges from an average 'mult' part from 
968                         the particles */
969                 damp = (float)nr;
970                 rt1= (int)(damp*paf->mult[depth]);
971                 rt2= (int)((damp+1.0)*paf->mult[depth]);
972                 if(rt1!=rt2) {
973                         
974                         for(b=0; b<paf->child[depth]; b++) {
975                                 pa= new_particle(paf);
976                                 *pa= *opa;
977                                 pa->lifetime= paf->life[depth];
978                                 if(paf->randlife!=0.0) {
979                                         pa->lifetime*= 1.0f+ (float)(paf->randlife*( BLI_drand() - 0.5));
980                                 }
981                                 pa->mat_nr= paf->mat[depth];
982
983                                 make_particle_keys(depth+1, b, paf, pa, force, deform, mtex, par_layer);
984                         }
985                 }
986         }
987 }
988
989 void init_mv_jit(float *jit, int num,int seed2)
990 {
991         float *jit2, x, rad1, rad2, rad3;
992         int i, num2;
993
994         if(num==0) return;
995
996         rad1= (float)(1.0/sqrt((float)num));
997         rad2= (float)(1.0/((float)num));
998         rad3= (float)sqrt((float)num)/((float)num);
999
1000         BLI_srand(31415926 + num + seed2);
1001         x= 0;
1002         num2 = 2 * num;
1003         for(i=0; i<num2; i+=2) {
1004         
1005                 jit[i]= x+ (float)(rad1*(0.5-BLI_drand()));
1006                 jit[i+1]= ((float)i/2)/num +(float)(rad1*(0.5-BLI_drand()));
1007                 
1008                 jit[i]-= (float)floor(jit[i]);
1009                 jit[i+1]-= (float)floor(jit[i+1]);
1010                 
1011                 x+= rad3;
1012                 x -= (float)floor(x);
1013         }
1014
1015         jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
1016
1017         for (i=0 ; i<4 ; i++) {
1018                 RE_jitterate1(jit, jit2, num, rad1);
1019                 RE_jitterate1(jit, jit2, num, rad1);
1020                 RE_jitterate2(jit, jit2, num, rad2);
1021         }
1022         MEM_freeN(jit2);
1023 }
1024
1025
1026 static void give_mesh_mvert(Mesh *me, DispListMesh *dlm, int nr, float *co, short *no, int seed2)
1027 {
1028         static float *jit=0;
1029         static int jitlevel=1;
1030         MVert *mvert, *mvertbase=NULL;
1031         MFace *mface, *mfacebase=NULL;
1032         float u, v, *v1, *v2, *v3, *v4;
1033         int totface=0, totvert=0, curface, curjit;
1034         short *n1, *n2, *n3, *n4;
1035         
1036         /* signal */
1037         if(me==0) {
1038                 if(jit) MEM_freeN(jit);
1039                 jit= 0;
1040                 return;
1041         }
1042         
1043         if(dlm) {
1044                 mvertbase= dlm->mvert;
1045                 mfacebase= dlm->mface;
1046                 totface= dlm->totface;
1047                 totvert= dlm->totvert;
1048         }
1049         
1050         if(totvert==0) {
1051                 mvertbase= me->mvert;
1052                 mfacebase= me->mface;
1053                 totface= me->totface;
1054                 totvert= me->totvert;
1055         }
1056         
1057         if(totface==0 || nr<totvert) {
1058                 mvert= mvertbase + (nr % totvert);
1059                 VECCOPY(co, mvert->co);
1060                 VECCOPY(no, mvert->no);
1061         }
1062         else {
1063
1064                 nr-= totvert;
1065                 
1066                 if(jit==0) {
1067                         jitlevel= nr/totface;
1068                         if(jitlevel==0) jitlevel= 1;
1069                         if(jitlevel>100) jitlevel= 100;
1070
1071                         jit= MEM_callocN(2+ jitlevel*2*sizeof(float), "jit");
1072                         init_mv_jit(jit, jitlevel,seed2);
1073                         
1074                 }
1075
1076                 curjit= nr/totface;
1077                 curjit= curjit % jitlevel;
1078
1079                 curface= nr % totface;
1080                 
1081                 mface= mfacebase;
1082                 mface+= curface;
1083
1084                 v1= (mvertbase+(mface->v1))->co;
1085                 v2= (mvertbase+(mface->v2))->co;
1086                 n1= (mvertbase+(mface->v1))->no;
1087                 n2= (mvertbase+(mface->v2))->no;
1088                 if(mface->v3==0) {
1089                         v3= (mvertbase+(mface->v2))->co;
1090                         v4= (mvertbase+(mface->v1))->co;
1091                         n3= (mvertbase+(mface->v2))->no;
1092                         n4= (mvertbase+(mface->v1))->no;
1093                 }
1094                 else if(mface->v4==0) {
1095                         v3= (mvertbase+(mface->v3))->co;
1096                         v4= (mvertbase+(mface->v1))->co;
1097                         n3= (mvertbase+(mface->v3))->no;
1098                         n4= (mvertbase+(mface->v1))->no;
1099                 }
1100                 else {
1101                         v3= (mvertbase+(mface->v3))->co;
1102                         v4= (mvertbase+(mface->v4))->co;
1103                         n3= (mvertbase+(mface->v3))->no;
1104                         n4= (mvertbase+(mface->v4))->no;
1105                 }
1106
1107                 u= jit[2*curjit];
1108                 v= jit[2*curjit+1];
1109
1110                 co[0]= (float)((1.0-u)*(1.0-v)*v1[0] + (1.0-u)*(v)*v2[0] + (u)*(v)*v3[0] + (u)*(1.0-v)*v4[0]);
1111                 co[1]= (float)((1.0-u)*(1.0-v)*v1[1] + (1.0-u)*(v)*v2[1] + (u)*(v)*v3[1] + (u)*(1.0-v)*v4[1]);
1112                 co[2]= (float)((1.0-u)*(1.0-v)*v1[2] + (1.0-u)*(v)*v2[2] + (u)*(v)*v3[2] + (u)*(1.0-v)*v4[2]);
1113                 
1114                 no[0]= (short)((1.0-u)*(1.0-v)*n1[0] + (1.0-u)*(v)*n2[0] + (u)*(v)*n3[0] + (u)*(1.0-v)*n4[0]);
1115                 no[1]= (short)((1.0-u)*(1.0-v)*n1[1] + (1.0-u)*(v)*n2[1] + (u)*(v)*n3[1] + (u)*(1.0-v)*n4[1]);
1116                 no[2]= (short)((1.0-u)*(1.0-v)*n1[2] + (1.0-u)*(v)*n2[2] + (u)*(v)*n3[2] + (u)*(1.0-v)*n4[2]);
1117                 
1118         }
1119 }
1120
1121
1122 void build_particle_system(Object *ob)
1123 {
1124         Base *base;
1125         Object *par;
1126         PartEff *paf;
1127         Particle *pa;
1128         Mesh *me;
1129         MVert *mvert;
1130         MTex *mtexmove=0;
1131         Material *ma;
1132         DispListMesh *dlm;
1133         float framelenont, ftime, dtime, force[3], imat[3][3], vec[3];
1134         float fac, prevobmat[4][4], sfraont, co[3];
1135         int deform=0, a, cur, cfraont, cfralast, totpart;
1136         short no[3];
1137
1138         if(ob->type!=OB_MESH) return;
1139         me= ob->data;
1140         if(me->totvert==0) return;
1141
1142         ma= give_current_material(ob, 1);
1143         if(ma) {
1144                 mtexmove= ma->mtex[7];
1145         }
1146
1147         paf= give_parteff(ob);
1148         if(paf==NULL) return;
1149
1150         waitcursor(1);
1151
1152         disable_speed_curve(1);
1153         
1154         /* warning! we cannot call this when modifier is active! */
1155         mesh_modifier(ob, 's');
1156
1157         /* generate all particles */
1158         if(paf->keys) MEM_freeN(paf->keys);
1159         paf->keys= NULL;
1160         new_particle(paf);
1161
1162         /* reset deflector cache, sumohandle is free, but its still sorta abuse... (ton) */
1163         for(base= G.scene->base.first; base; base= base->next) {
1164                 base->object->sumohandle= NULL;
1165         }
1166
1167         cfraont= G.scene->r.cfra;
1168         cfralast= -1000;
1169         framelenont= G.scene->r.framelen;
1170         G.scene->r.framelen= 1.0;
1171         sfraont= ob->sf;
1172         ob->sf= 0.0;
1173
1174         /* mult generations? */
1175         totpart= paf->totpart;
1176         for(a=0; a<PAF_MAXMULT; a++) {
1177                 if(paf->mult[a]!=0.0) {
1178                         /* interessant formula! this way after 'x' generations the total is paf->totpart */
1179                         totpart= (int)(totpart / (1.0+paf->mult[a]*paf->child[a]));
1180                 }
1181                 else break;
1182         }
1183
1184         ftime= paf->sta;
1185         dtime= (paf->end - paf->sta)/totpart;
1186
1187         /* remember full hierarchy */
1188         par= ob;
1189         while(par) {
1190                 pushdata(par, sizeof(Object));
1191                 par= par->parent;
1192         }
1193         
1194         /* for static particles, calculate system on current frame */
1195         if(ma) do_mat_ipo(ma);
1196
1197         /* set it all at first frame */
1198         G.scene->r.cfra= cfralast= (int)floor(ftime);
1199         par= ob;
1200         while(par) {
1201                 /* do_ob_ipo(par); */
1202                 do_ob_key(par);
1203                 par= par->parent;
1204         }
1205         
1206         if((paf->flag & PAF_STATIC)==0) {
1207                 if(ma) do_mat_ipo(ma);  // nor for static
1208                 
1209                 where_is_object(ob);
1210                 Mat4CpyMat4(prevobmat, ob->obmat);
1211                 Mat4Invert(ob->imat, ob->obmat);
1212                 Mat3CpyMat4(imat, ob->imat);
1213         }
1214         else {
1215                 Mat4One(prevobmat);
1216                 Mat3One(imat);
1217         }
1218         
1219         BLI_srand(paf->seed);
1220         
1221         /* otherwise it goes way too fast */
1222         force[0]= paf->force[0]*0.05f;
1223         force[1]= paf->force[1]*0.05f;
1224         force[2]= paf->force[2]*0.05f;
1225         
1226         if( paf->flag & PAF_STATIC ) deform= 0;
1227         else {
1228                 deform= (ob->parent && ob->parent->type==OB_LATTICE);
1229                 if(deform) init_latt_deform(ob->parent, 0);
1230         }
1231         
1232         /* init */
1233         if (mesh_uses_displist(me)) {
1234                 DerivedMesh *dm = mesh_get_derived(ob);
1235
1236                 dlm = dm->convertToDispListMesh(dm);
1237
1238                 dm->release(dm);
1239         } else {
1240                 dlm = NULL;
1241         }
1242
1243         give_mesh_mvert(me, dlm, totpart, co, no, paf->seed);
1244         
1245         if(G.f & G_DEBUG) {
1246                 printf("\n");
1247                 printf("Calculating particles......... \n");
1248         }
1249         for(a=0; a<totpart; a++, ftime+=dtime) {
1250                 
1251                 pa= new_particle(paf);
1252                 pa->time= ftime;
1253                 
1254                 if(G.f & G_DEBUG) {
1255                         int b, c;
1256                         
1257                         c = totpart/100;
1258                         if (c==0){
1259                                 c = 1;
1260                         }
1261
1262                         b=(a%c);
1263                         if (b==0) {
1264                                 printf("\r Particle: %d / %d ", a, totpart);
1265                                 fflush(stdout);
1266                         }
1267                 }
1268                 /* set ob at correct time */
1269                 
1270                 if((paf->flag & PAF_STATIC)==0) {
1271
1272                         cur= (int)floor(ftime) + 1 ;            /* + 1 has a reason: (obmat/prevobmat) otherwise comet-tails start too late */
1273                         if(cfralast != cur) {
1274                                 G.scene->r.cfra= cfralast= cur;
1275         
1276                                 /* added later: blur? */
1277                                 bsystem_time(ob, ob->parent, (float)G.scene->r.cfra, 0.0);
1278
1279                                 par= ob;
1280                                 while(par) {
1281                                         /* do_ob_ipo(par); */
1282                                         par->ctime= -1234567.0;
1283                                         do_ob_key(par);
1284                                         par= par->parent;
1285                                 }
1286                                 if(ma) do_mat_ipo(ma);
1287                                 Mat4CpyMat4(prevobmat, ob->obmat);
1288                                 where_is_object(ob);
1289                                 Mat4Invert(ob->imat, ob->obmat);
1290                                 Mat3CpyMat4(imat, ob->imat);
1291                         }
1292                 }
1293                 /* get coordinates */
1294                 if(paf->flag & PAF_FACE) give_mesh_mvert(me, dlm, a, co, no,paf->seed);
1295                 else {
1296                         mvert= me->mvert + (a % me->totvert);
1297                         VECCOPY(co, mvert->co);
1298                         VECCOPY(no, mvert->no);
1299                 }
1300                 
1301                 VECCOPY(pa->co, co);
1302                 
1303                 if(paf->flag & PAF_STATIC);
1304                 else {
1305                         Mat4MulVecfl(ob->obmat, pa->co);
1306                 
1307                         VECCOPY(vec, co);
1308                         Mat4MulVecfl(prevobmat, vec);
1309                         
1310                         /* first start speed: object */
1311                         VECSUB(pa->no, pa->co, vec);
1312                         VecMulf(pa->no, paf->obfac);
1313                         
1314                         /* calculate the correct inter-frame */ 
1315                         fac= (ftime- (float)floor(ftime));
1316                         pa->co[0]= fac*pa->co[0] + (1.0f-fac)*vec[0];
1317                         pa->co[1]= fac*pa->co[1] + (1.0f-fac)*vec[1];
1318                         pa->co[2]= fac*pa->co[2] + (1.0f-fac)*vec[2];
1319                 }
1320
1321                 /* start speed: normal */
1322                 if(paf->normfac!=0.0) {
1323                         /* sp= mvert->no; */
1324                                 /* transpose ! */
1325                         vec[0]= imat[0][0]*no[0] + imat[0][1]*no[1] + imat[0][2]*no[2];
1326                         vec[1]= imat[1][0]*no[0] + imat[1][1]*no[1] + imat[1][2]*no[2];
1327                         vec[2]= imat[2][0]*no[0] + imat[2][1]*no[1] + imat[2][2]*no[2];         
1328                 
1329                         Normalise(vec);
1330                         VecMulf(vec, paf->normfac);
1331                         VECADD(pa->no, pa->no, vec);
1332                 }
1333                 pa->lifetime= paf->lifetime;
1334                 if(paf->randlife!=0.0) {
1335                         pa->lifetime*= 1.0f+ (float)(paf->randlife*( BLI_drand() - 0.5));
1336                 }
1337                 pa->mat_nr= 1;
1338                 
1339                 make_particle_keys(0, a, paf, pa, force, deform, mtexmove, ob->lay);
1340         }
1341         
1342         if(G.f & G_DEBUG) {
1343                 printf("\r Particle: %d / %d \n", totpart, totpart);
1344                 fflush(stdout);
1345         }       
1346         if(deform) end_latt_deform();
1347                 
1348         /* restore */
1349         G.scene->r.cfra= cfraont;
1350         G.scene->r.framelen= framelenont;
1351         give_mesh_mvert(0, 0, 0, 0, 0,paf->seed);
1352
1353         /* put hierarchy back */
1354         par= ob;
1355         while(par) {
1356                 popfirst(par);
1357                 /* do not do ob->ipo: keep insertkey */
1358                 do_ob_key(par);
1359                 par= par->parent;
1360         }
1361
1362         /* reset deflector cache */
1363         for(base= G.scene->base.first; base; base= base->next) {
1364                 if(base->object->sumohandle) {
1365                         MEM_freeN(base->object->sumohandle);
1366                         base->object->sumohandle= NULL;
1367                 }
1368         }
1369
1370         /* restore: AFTER popfirst */
1371         ob->sf= sfraont;
1372
1373         if(ma) do_mat_ipo(ma);  // set back on current time
1374         disable_speed_curve(0);
1375         
1376         mesh_modifier(ob, 'e');
1377
1378         waitcursor(0);
1379
1380         if (dlm) {
1381                 displistmesh_free(dlm);
1382         }
1383 }
1384
1385 /* ************* WAVE **************** */
1386
1387 void calc_wave_deform(WaveEff *wav, float ctime, float *co)
1388 {
1389         /* co is in local coords */
1390         float lifefac, x, y, amplit;
1391         
1392         /* actually this should not happen */
1393         if((wav->flag & (WAV_X+WAV_Y))==0) return;      
1394
1395         lifefac= wav->height;
1396         
1397         if( wav->lifetime!=0.0) {
1398                 x= ctime - wav->timeoffs;
1399                 if(x>wav->lifetime) {
1400                         
1401                         lifefac= x-wav->lifetime;
1402                         
1403                         if(lifefac > wav->damp) lifefac= 0.0;
1404                         else lifefac= (float)(wav->height*(1.0 - sqrt(lifefac/wav->damp)));
1405                 }
1406         }
1407         if(lifefac==0.0) return;
1408
1409         x= co[0]-wav->startx;
1410         y= co[1]-wav->starty;
1411
1412         if(wav->flag & WAV_X) {
1413                 if(wav->flag & WAV_Y) amplit= (float)sqrt( (x*x + y*y));
1414                 else amplit= x;
1415         }
1416         else amplit= y;
1417         
1418         /* this way it makes nice circles */
1419         amplit-= (ctime-wav->timeoffs)*wav->speed;
1420
1421         if(wav->flag & WAV_CYCL) {
1422                 amplit = (float)fmod(amplit-wav->width, 2.0*wav->width) + wav->width;
1423         }
1424
1425         /* GAUSSIAN */
1426         
1427         if(amplit> -wav->width && amplit<wav->width) {
1428         
1429                 amplit = amplit*wav->narrow;
1430                 amplit= (float)(1.0/exp(amplit*amplit) - wav->minfac);
1431
1432                 co[2]+= lifefac*amplit;
1433         }
1434 }
1435
1436 /* return 1 if deformed
1437    Note: it works on mvert now, so assumes to be callied in modifier stack \
1438 */
1439 int object_wave(Object *ob)
1440 {
1441         WaveEff *wav;
1442         Mesh *me;
1443         MVert *mvert;
1444         float ctime;
1445         int a;
1446         
1447         /* is there a wave */
1448         wav= ob->effect.first;
1449         while(wav) {
1450                 if(wav->type==EFF_WAVE) break;
1451                 wav= wav->next;
1452         }
1453         if(wav==NULL) return 0;
1454         
1455         if(ob->type==OB_MESH) {
1456
1457                 ctime= bsystem_time(ob, 0, (float)G.scene->r.cfra, 0.0);
1458                 
1459                 me= ob->data;
1460
1461                 wav= ob->effect.first;
1462                 while(wav) {
1463                         if(wav->type==EFF_WAVE) {
1464                                 
1465                                 /* precalculate */
1466                                 wav->minfac= (float)(1.0/exp(wav->width*wav->narrow*wav->width*wav->narrow));
1467                                 if(wav->damp==0) wav->damp= 10.0f;
1468                                 
1469                                 mvert= me->mvert;
1470                                 
1471                                 for(a=0; a<me->totvert; a++, mvert++) {
1472                                         calc_wave_deform(wav, ctime, mvert->co);
1473                                 }
1474                         }
1475                         wav= wav->next;
1476                 }
1477         }
1478         return 1;
1479 }