22f7e524a15d37639f8f1792ff311a8b0f5ed519
[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
51 #include "BLI_blenlib.h"
52 #include "BLI_arithb.h"
53 #include "BLI_rand.h"
54
55 #include "BKE_utildefines.h"
56 #include "BKE_bad_level_calls.h"
57 #include "BKE_global.h"
58 #include "BKE_material.h"
59 #include "BKE_effect.h"
60 #include "BKE_key.h"
61 #include "BKE_ipo.h"
62 #include "BKE_screen.h"
63 #include "BKE_blender.h"
64 #include "BKE_object.h"
65 #include "BKE_displist.h"
66 #include "BKE_lattice.h"
67
68
69 #ifdef HAVE_CONFIG_H
70 #include <config.h>
71 #endif
72
73 Effect *add_effect(int type)
74 {
75         Effect *eff=0;
76         BuildEff *bld;
77         PartEff *paf;
78         WaveEff *wav;
79         int a;
80         
81         switch(type) {
82         case EFF_BUILD:
83                 bld= MEM_callocN(sizeof(BuildEff), "neweff");
84                 eff= (Effect *)bld;
85                 
86                 bld->sfra= 1.0;
87                 bld->len= 100.0;
88                 break;
89                 
90         case EFF_PARTICLE:
91                 paf= MEM_callocN(sizeof(PartEff), "neweff");
92                 eff= (Effect *)paf;
93                 
94                 paf->sta= 1.0;
95                 paf->end= 100.0;
96                 paf->lifetime= 50.0;
97                 for(a=0; a<PAF_MAXMULT; a++) {
98                         paf->life[a]= 50.0;
99                         paf->child[a]= 4;
100                         paf->mat[a]= 1;
101                 }
102                 
103                 paf->totpart= 1000;
104                 paf->totkey= 8;
105                 paf->staticstep= 5;
106                 paf->defvec[2]= 1.0f;
107                 paf->nabla= 0.05f;
108                 
109                 break;
110                 
111         case EFF_WAVE:
112                 wav= MEM_callocN(sizeof(WaveEff), "neweff");
113                 eff= (Effect *)wav;
114                 
115                 wav->flag |= (WAV_X+WAV_Y+WAV_CYCL);
116                 
117                 wav->height= 0.5f;
118                 wav->width= 1.5f;
119                 wav->speed= 0.5f;
120                 wav->narrow= 1.5f;
121                 wav->lifetime= 0.0f;
122                 wav->damp= 10.0f;
123                 
124                 break;
125         }
126         
127         eff->type= eff->buttype= type;
128         eff->flag |= SELECT;
129         
130         return eff;
131 }
132
133 void free_effect(Effect *eff)
134 {
135         PartEff *paf;
136         
137         if(eff->type==EFF_PARTICLE) {
138                 paf= (PartEff *)eff;
139                 if(paf->keys) MEM_freeN(paf->keys);
140         }
141         MEM_freeN(eff); 
142 }
143
144
145 void free_effects(ListBase *lb)
146 {
147         Effect *eff;
148         
149         eff= lb->first;
150         while(eff) {
151                 BLI_remlink(lb, eff);
152                 free_effect(eff);
153                 eff= lb->first;
154         }
155 }
156
157 Effect *copy_effect(Effect *eff) 
158 {
159         Effect *effn;
160         
161         effn= MEM_dupallocN(eff);
162         if(effn->type==EFF_PARTICLE) ((PartEff *)effn)->keys= 0;
163
164         return effn;    
165 }
166
167 void copy_act_effect(Object *ob)
168 {
169         /* return a copy of the active effect */
170         Effect *effn, *eff;
171         
172         eff= ob->effect.first;
173         while(eff) {
174                 if(eff->flag & SELECT) {
175                         
176                         effn= copy_effect(eff);
177                         BLI_addtail(&ob->effect, effn);
178                         
179                         eff->flag &= ~SELECT;
180                         return;
181                         
182                 }
183                 eff= eff->next;
184         }
185         
186         /* when it comes here: add new effect */
187         eff= add_effect(EFF_BUILD);
188         BLI_addtail(&ob->effect, eff);
189                         
190 }
191
192 void copy_effects(ListBase *lbn, ListBase *lb)
193 {
194         Effect *eff, *effn;
195
196         lbn->first= lbn->last= 0;
197
198         eff= lb->first;
199         while(eff) {
200                 effn= copy_effect(eff);
201                 BLI_addtail(lbn, effn);
202                 
203                 eff= eff->next;
204         }
205         
206 }
207
208 void deselectall_eff(Object *ob)
209 {
210         Effect *eff= ob->effect.first;
211         
212         while(eff) {
213                 eff->flag &= ~SELECT;
214                 eff= eff->next;
215         }
216 }
217
218 void set_buildvars(Object *ob, int *start, int *end)
219 {
220         BuildEff *bld;
221         float ctime;
222         
223         bld= ob->effect.first;
224         while(bld) {
225                 if(bld->type==EFF_BUILD) {
226                         ctime= bsystem_time(ob, 0, (float)G.scene->r.cfra, bld->sfra-1.0f);
227                         if(ctime < 0.0) {
228                                 *end= *start;
229                         }
230                         else if(ctime < bld->len) {
231                                 *end= *start+ (int)((*end - *start)*ctime/bld->len);
232                         }
233                         
234                         return;
235                 }
236                 bld= bld->next;
237         }
238 }
239
240 /* ***************** PARTICLES ***************** */
241
242 Particle *new_particle(PartEff *paf)
243 {
244         static Particle *pa;
245         static int cur;
246
247         /* we agree: when paf->keys==0: alloc */        
248         if(paf->keys==0) {
249                 pa= paf->keys= MEM_callocN( paf->totkey*paf->totpart*sizeof(Particle), "particlekeys" );
250                 cur= 0;
251         }
252         else {
253                 if(cur && cur<paf->totpart) pa+=paf->totkey;
254                 cur++;
255         }
256         return pa;
257 }
258
259 PartEff *give_parteff(Object *ob)
260 {
261         PartEff *paf;
262         
263         paf= ob->effect.first;
264         while(paf) {
265                 if(paf->type==EFF_PARTICLE) return paf;
266                 paf= paf->next;
267         }
268         return 0;
269 }
270
271 void where_is_particle(PartEff *paf, Particle *pa, float ctime, float *vec)
272 {
273         Particle *p[4];
274         float dt, t[4];
275         int a;
276         
277         if(paf->totkey==1) {
278                 VECCOPY(vec, pa->co);
279                 return;
280         }
281         
282         /* first find the first particlekey */
283         a= (int)((paf->totkey-1)*(ctime-pa->time)/pa->lifetime);
284         if(a>=paf->totkey) a= paf->totkey-1;
285         
286         pa+= a;
287         
288         if(a>0) p[0]= pa-1; else p[0]= pa;
289         p[1]= pa;
290         
291         if(a+1<paf->totkey) p[2]= pa+1; else p[2]= pa;
292         if(a+2<paf->totkey) p[3]= pa+2; else p[3]= p[2];
293         
294         if(p[1]==p[2]) dt= 0.0;
295         else dt= (ctime-p[1]->time)/(p[2]->time - p[1]->time);
296
297         if(paf->flag & PAF_BSPLINE) set_four_ipo(dt, t, KEY_BSPLINE);
298         else set_four_ipo(dt, t, KEY_CARDINAL);
299
300         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];
301         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];
302         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];
303
304 }
305
306
307 void particle_tex(MTex *mtex, PartEff *paf, float *co, float *no)
308 {                               
309         extern float Tin, Tr, Tg, Tb;
310         extern void externtex(struct MTex *mtex, float *vec);
311         float old;
312         
313         externtex(mtex, co);
314         
315         if(paf->texmap==PAF_TEXINT) {
316                 Tin*= paf->texfac;
317                 no[0]+= Tin*paf->defvec[0];
318                 no[1]+= Tin*paf->defvec[1];
319                 no[2]+= Tin*paf->defvec[2];
320         }
321         else if(paf->texmap==PAF_TEXRGB) {
322                 no[0]+= (Tr-0.5f)*paf->texfac;
323                 no[1]+= (Tg-0.5f)*paf->texfac;
324                 no[2]+= (Tb-0.5f)*paf->texfac;
325         }
326         else {  /* PAF_TEXGRAD */
327                 
328                 old= Tin;
329                 co[0]+= paf->nabla;
330                 externtex(mtex, co);
331                 no[0]+= (old-Tin)*paf->texfac;
332                 
333                 co[0]-= paf->nabla;
334                 co[1]+= paf->nabla;
335                 externtex(mtex, co);
336                 no[1]+= (old-Tin)*paf->texfac;
337                 
338                 co[1]-= paf->nabla;
339                 co[2]+= paf->nabla;
340                 externtex(mtex, co);
341                 no[2]+= (old-Tin)*paf->texfac;
342                 
343         }
344 }
345
346 void make_particle_keys(int depth, int nr, PartEff *paf, Particle *part, float *force, int deform, MTex *mtex)
347 {
348         Particle *pa, *opa = NULL;
349         float damp, deltalife;
350         int b, rt1, rt2;
351         
352         damp= 1.0f-paf->damp;
353         pa= part;
354         
355         /* start speed: random */
356         if(paf->randfac!=0.0) {
357                 pa->no[0]+= (float)(paf->randfac*( BLI_drand() -0.5));
358                 pa->no[1]+= (float)(paf->randfac*( BLI_drand() -0.5));
359                 pa->no[2]+= (float)(paf->randfac*( BLI_drand() -0.5));
360         }
361         
362         /* start speed: texture */
363         if(mtex && paf->texfac!=0.0) {
364                 particle_tex(mtex, paf, pa->co, pa->no);
365         }
366         
367         if(paf->totkey>1) deltalife= pa->lifetime/(paf->totkey-1);
368         else deltalife= pa->lifetime;
369
370         opa= pa;
371         pa++;
372                 
373         b= paf->totkey-1;
374         while(b--) {
375                 /* new time */
376                 pa->time= opa->time+deltalife;
377
378                 /* new location */
379                 pa->co[0]= opa->co[0] + deltalife*opa->no[0];
380                 pa->co[1]= opa->co[1] + deltalife*opa->no[1];
381                 pa->co[2]= opa->co[2] + deltalife*opa->no[2];
382
383                 /* new speed */
384                 pa->no[0]= opa->no[0] + deltalife*force[0];
385                 pa->no[1]= opa->no[1] + deltalife*force[1];
386                 pa->no[2]= opa->no[2] + deltalife*force[2];
387
388                 /* speed: texture */
389                 if(mtex && paf->texfac!=0.0) {
390                         particle_tex(mtex, paf, pa->co, pa->no);
391                 }
392                 if(damp!=1.0) {
393                         pa->no[0]*= damp;
394                         pa->no[1]*= damp;
395                         pa->no[2]*= damp;
396                 }
397         
398                 opa= pa;
399                 pa++;
400                 /* opa is used later on too! */
401         }
402
403         if(deform) {
404                 /* deform all keys */
405                 pa= part;
406                 b= paf->totkey;
407                 while(b--) {
408                         calc_latt_deform(pa->co);
409                         pa++;
410                 }
411         }
412         
413         /* the big multiplication */
414         if(depth<PAF_MAXMULT && paf->mult[depth]!=0.0) {
415                 
416                 /* new 'child' emerges from an average 'mult' part from 
417                         the particles */
418                 damp = (float)nr;
419                 rt1= (int)(damp*paf->mult[depth]);
420                 rt2= (int)((damp+1.0)*paf->mult[depth]);
421                 if(rt1!=rt2) {
422                         
423                         for(b=0; b<paf->child[depth]; b++) {
424                                 pa= new_particle(paf);
425                                 *pa= *opa;
426                                 pa->lifetime= paf->life[depth];
427                                 if(paf->randlife!=0.0) {
428                                         pa->lifetime*= 1.0f+ (float)(paf->randlife*( BLI_drand() - 0.5));
429                                 }
430                                 pa->mat_nr= paf->mat[depth];
431                                 
432                                 make_particle_keys(depth+1, b, paf, pa, force, deform, mtex);
433                         }
434                 }
435         }
436 }
437
438 void init_mv_jit(float *jit, int num,float seed2)
439 {
440         float *jit2, x, rad1, rad2, rad3;
441         int i, num2;
442
443         if(num==0) return;
444
445         rad1= (float)(1.0/sqrt((float)num));
446         rad2= (float)(1.0/((float)num));
447         rad3= (float)sqrt((float)num)/((float)num);
448
449         BLI_srand(31415926 + num + seed2);
450         x= 0;
451         num2 = 2 * num;
452         for(i=0; i<num2; i+=2) {
453         
454                 jit[i]= x+ (float)(rad1*(0.5-BLI_drand()));
455                 jit[i+1]= ((float)i/2)/num +(float)(rad1*(0.5-BLI_drand()));
456                 
457                 jit[i]-= (float)floor(jit[i]);
458                 jit[i+1]-= (float)floor(jit[i+1]);
459                 
460                 x+= rad3;
461                 x -= (float)floor(x);
462         }
463
464         jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
465
466         for (i=0 ; i<4 ; i++) {
467                 RE_jitterate1(jit, jit2, num, rad1);
468                 RE_jitterate1(jit, jit2, num, rad1);
469                 RE_jitterate2(jit, jit2, num, rad2);
470         }
471         MEM_freeN(jit2);
472 }
473
474
475 void give_mesh_mvert(Mesh *me, int nr, float *co, short *no, float seed2)
476 {
477         static float *jit=0;
478         static int jitlevel=1;
479         MVert *mvert;
480         MFace *mface;
481         float u, v, *v1, *v2, *v3, *v4;
482         int curface, curjit;
483         short *n1, *n2, *n3, *n4;
484         
485         /* signal */
486         if(me==0) {
487                 if(jit) MEM_freeN(jit);
488                 jit= 0;
489                 return;
490         }
491         
492         if(me->totface==0 || nr<me->totvert) {
493                 mvert= me->mvert + (nr % me->totvert);
494                 VECCOPY(co, mvert->co);
495                 VECCOPY(no, mvert->no);
496         }
497         else {
498                 
499                 nr-= me->totvert;
500                 
501                 if(jit==0) {
502                         jitlevel= nr/me->totface;
503                         if(jitlevel==0) jitlevel= 1;
504                         if(jitlevel>100) jitlevel= 100;
505                         
506                         jit= MEM_callocN(2+ jitlevel*2*sizeof(float), "jit");
507                         init_mv_jit(jit, jitlevel,seed2);
508                         
509                 }
510
511                 curjit= nr/me->totface;
512                 curjit= curjit % jitlevel;
513
514                 curface= nr % me->totface;
515                 
516                 mface= me->mface;
517                 mface+= curface;
518                 
519                 v1= (me->mvert+(mface->v1))->co;
520                 v2= (me->mvert+(mface->v2))->co;
521                 n1= (me->mvert+(mface->v1))->no;
522                 n2= (me->mvert+(mface->v2))->no;
523                 if(mface->v3==0) {
524                         v3= (me->mvert+(mface->v2))->co;
525                         v4= (me->mvert+(mface->v1))->co;
526                         n3= (me->mvert+(mface->v2))->no;
527                         n4= (me->mvert+(mface->v1))->no;
528                 }
529                 else if(mface->v4==0) {
530                         v3= (me->mvert+(mface->v3))->co;
531                         v4= (me->mvert+(mface->v1))->co;
532                         n3= (me->mvert+(mface->v3))->no;
533                         n4= (me->mvert+(mface->v1))->no;
534                 }
535                 else {
536                         v3= (me->mvert+(mface->v3))->co;
537                         v4= (me->mvert+(mface->v4))->co;
538                         n3= (me->mvert+(mface->v3))->no;
539                         n4= (me->mvert+(mface->v4))->no;
540                 }
541
542                 u= jit[2*curjit];
543                 v= jit[2*curjit+1];
544
545                 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]);
546                 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]);
547                 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]);
548                 
549                 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]);
550                 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]);
551                 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]);
552                 
553         }
554 }
555
556
557 void build_particle_system(Object *ob)
558 {
559         Object *par;
560         PartEff *paf;
561         Particle *pa;
562         Mesh *me;
563         MVert *mvert;
564         MTex *mtexmove=0;
565         Material *ma;
566         float framelenont, ftime, dtime, force[3], imat[3][3], vec[3];
567         float fac, prevobmat[4][4], sfraont, co[3];
568         int deform=0, a, cur, cfraont, cfralast, totpart;
569         short no[3];
570         
571         if(ob->type!=OB_MESH) return;
572         me= ob->data;
573         if(me->totvert==0) return;
574         
575         ma= give_current_material(ob, 1);
576         if(ma) {
577                 mtexmove= ma->mtex[7];
578         }
579         
580         paf= give_parteff(ob);
581         if(paf==0) return;
582
583         waitcursor(1);
584         
585         disable_speed_curve(1);
586         
587         /* generate all particles */
588         if(paf->keys) MEM_freeN(paf->keys);
589         paf->keys= 0;
590         new_particle(paf);      
591
592         cfraont= G.scene->r.cfra;
593         cfralast= -1000;
594         framelenont= G.scene->r.framelen;
595         G.scene->r.framelen= 1.0;
596         sfraont= ob->sf;
597         ob->sf= 0.0;
598         
599         /* mult generations? */
600         totpart= paf->totpart;
601         for(a=0; a<PAF_MAXMULT; a++) {
602                 if(paf->mult[a]!=0.0) {
603                         /* interessant formula! this way after 'x' generations the total is paf->totpart */
604                         totpart= (int)(totpart / (1.0+paf->mult[a]*paf->child[a]));
605                 }
606                 else break;
607         }
608
609         ftime= paf->sta;
610         dtime= (paf->end - paf->sta)/totpart;
611         
612         /* remember full hierarchy */
613         par= ob;
614         while(par) {
615                 pushdata(par, sizeof(Object));
616                 par= par->parent;
617         }
618
619         /* set it all at first frame */
620         G.scene->r.cfra= cfralast= (int)floor(ftime);
621         par= ob;
622         while(par) {
623                 /* do_ob_ipo(par); */
624                 do_ob_key(par);
625                 par= par->parent;
626         }
627         do_mat_ipo(ma);
628         
629         if((paf->flag & PAF_STATIC)==0) {
630                 where_is_object(ob);
631                 Mat4CpyMat4(prevobmat, ob->obmat);
632                 Mat4Invert(ob->imat, ob->obmat);
633                 Mat3CpyMat4(imat, ob->imat);
634         }
635         else {
636                 Mat4One(prevobmat);
637                 Mat3One(imat);
638         }
639         
640         BLI_srand(paf->seed);
641         
642         /* otherwise it goes way too fast */
643         force[0]= paf->force[0]*0.05f;
644         force[1]= paf->force[1]*0.05f;
645         force[2]= paf->force[2]*0.05f;
646         
647         deform= (ob->parent && ob->parent->type==OB_LATTICE);
648         if(deform) init_latt_deform(ob->parent, 0);
649         
650         /* init */
651         give_mesh_mvert(me, totpart, co, no,paf->seed);
652         
653         for(a=0; a<totpart; a++, ftime+=dtime) {
654                 
655                 pa= new_particle(paf);
656                 pa->time= ftime;
657                 
658                 /* set ob at correct time */
659                 
660                 if((paf->flag & PAF_STATIC)==0) {
661                 
662                         cur= (int)floor(ftime) + 1 ;            /* + 1 has a reason: (obmat/prevobmat) otherwise comet-tails start too late */
663                         if(cfralast != cur) {
664                                 G.scene->r.cfra= cfralast= cur;
665         
666                                 /* added later: blur? */
667                                 bsystem_time(ob, ob->parent, (float)G.scene->r.cfra, 0.0);
668                                 
669                                 par= ob;
670                                 while(par) {
671                                         /* do_ob_ipo(par); */
672                                         par->ctime= -1234567.0;
673                                         do_ob_key(par);
674                                         par= par->parent;
675                                 }
676                                 do_mat_ipo(ma);
677                                 Mat4CpyMat4(prevobmat, ob->obmat);
678                                 where_is_object(ob);
679                                 Mat4Invert(ob->imat, ob->obmat);
680                                 Mat3CpyMat4(imat, ob->imat);
681                         }
682                 }
683                 /* get coordinates */
684                 if(paf->flag & PAF_FACE) give_mesh_mvert(me, a, co, no,paf->seed);
685                 else {
686                         mvert= me->mvert + (a % me->totvert);
687                         VECCOPY(co, mvert->co);
688                         VECCOPY(no, mvert->no);
689                 }
690                 
691                 VECCOPY(pa->co, co);
692                 
693                 if(paf->flag & PAF_STATIC);
694                 else {
695                         Mat4MulVecfl(ob->obmat, pa->co);
696                 
697                         VECCOPY(vec, co);
698                         Mat4MulVecfl(prevobmat, vec);
699                         
700                         /* first start speed: object */
701                         VecSubf(pa->no, pa->co, vec);
702                         VecMulf(pa->no, paf->obfac);
703                         
704                         /* calculate the correct inter-frame */ 
705                         fac= (ftime- (float)floor(ftime));
706                         pa->co[0]= fac*pa->co[0] + (1.0f-fac)*vec[0];
707                         pa->co[1]= fac*pa->co[1] + (1.0f-fac)*vec[1];
708                         pa->co[2]= fac*pa->co[2] + (1.0f-fac)*vec[2];
709                 }
710
711                 /* start speed: normal */
712                 if(paf->normfac!=0.0) {
713                         /* sp= mvert->no; */
714                                 /* transpose ! */
715                         vec[0]= imat[0][0]*no[0] + imat[0][1]*no[1] + imat[0][2]*no[2];
716                         vec[1]= imat[1][0]*no[0] + imat[1][1]*no[1] + imat[1][2]*no[2];
717                         vec[2]= imat[2][0]*no[0] + imat[2][1]*no[1] + imat[2][2]*no[2];         
718                 
719                         Normalise(vec);
720                         VecMulf(vec, paf->normfac);
721                         VecAddf(pa->no, pa->no, vec);
722                 }
723                 pa->lifetime= paf->lifetime;
724                 if(paf->randlife!=0.0) {
725                         pa->lifetime*= 1.0f+ (float)(paf->randlife*( BLI_drand() - 0.5));
726                 }
727                 pa->mat_nr= 1;
728                 
729                 make_particle_keys(0, a, paf, pa, force, deform, mtexmove);
730         }
731         
732         if(deform) end_latt_deform();
733                 
734         /* restore */
735         G.scene->r.cfra= cfraont;
736         G.scene->r.framelen= framelenont;
737         give_mesh_mvert(0, 0, 0, 0,paf->seed);
738
739
740         /* put hierarchy back */
741         par= ob;
742         while(par) {
743                 popfirst(par);
744                 /* do not do ob->ipo: keep insertkey */
745                 do_ob_key(par);
746                 par= par->parent;
747         }
748
749         /* restore: AFTER popfirst */
750         ob->sf= sfraont;
751
752         disable_speed_curve(0);
753
754         waitcursor(0);
755
756 }
757
758 /* ************* WAVE **************** */
759
760 void calc_wave_deform(WaveEff *wav, float ctime, float *co)
761 {
762         /* co is in local coords */
763         float lifefac, x, y, amplit;
764         
765         /* actually this should not happen */
766         if((wav->flag & (WAV_X+WAV_Y))==0) return;      
767
768         lifefac= wav->height;
769         
770         if( wav->lifetime!=0.0) {
771                 x= ctime - wav->timeoffs;
772                 if(x>wav->lifetime) {
773                         
774                         lifefac= x-wav->lifetime;
775                         
776                         if(lifefac > wav->damp) lifefac= 0.0;
777                         else lifefac= (float)(wav->height*(1.0 - sqrt(lifefac/wav->damp)));
778                 }
779         }
780         if(lifefac==0.0) return;
781
782         x= co[0]-wav->startx;
783         y= co[1]-wav->starty;
784
785         if(wav->flag & WAV_X) {
786                 if(wav->flag & WAV_Y) amplit= (float)sqrt( (x*x + y*y));
787                 else amplit= x;
788         }
789         else amplit= y;
790         
791         /* this way it makes nice circles */
792         amplit-= (ctime-wav->timeoffs)*wav->speed;
793
794         if(wav->flag & WAV_CYCL) {
795                 amplit = (float)fmod(amplit-wav->width, 2.0*wav->width) + wav->width;
796         }
797
798         /* GAUSSIAN */
799         
800         if(amplit> -wav->width && amplit<wav->width) {
801         
802                 amplit = amplit*wav->narrow;
803                 amplit= (float)(1.0/exp(amplit*amplit) - wav->minfac);
804
805                 co[2]+= lifefac*amplit;
806         }
807 }
808
809 void object_wave(Object *ob)
810 {
811         WaveEff *wav;
812         DispList *dl;
813         Mesh *me;
814         MVert *mvert;
815         float *fp, ctime;
816         int a, first;
817         
818         /* is there a wave */
819         wav= ob->effect.first;
820         while(wav) {
821                 if(wav->type==EFF_WAVE) break;
822                 wav= wav->next;
823         }
824         if(wav==0) return;
825         
826         if(ob->type==OB_MESH) {
827
828                 ctime= bsystem_time(ob, 0, (float)G.scene->r.cfra, 0.0);
829                 first= 1;
830                 
831                 me= ob->data;
832                 dl= find_displist_create(&ob->disp, DL_VERTS);
833
834                 if(dl->verts) MEM_freeN(dl->verts);
835                 dl->nr= me->totvert;
836                 dl->verts= MEM_mallocN(3*sizeof(float)*me->totvert, "wave");
837
838                 wav= ob->effect.first;
839                 while(wav) {
840                         if(wav->type==EFF_WAVE) {
841                                 
842                                 /* precalculate */
843                                 wav->minfac= (float)(1.0/exp(wav->width*wav->narrow*wav->width*wav->narrow));
844                                 if(wav->damp==0) wav->damp= 10.0f;
845                                 
846                                 mvert= me->mvert;
847                                 fp= dl->verts;
848                                 
849                                 for(a=0; a<me->totvert; a++, mvert++, fp+=3) {
850                                         if(first) VECCOPY(fp, mvert->co);
851                                         calc_wave_deform(wav, ctime, fp);
852                                 }
853                                 first= 0;
854                         }
855                         wav= wav->next;
856                 }
857         }
858 }