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