b8178dd98cd07b45945106ed6d81e2025a7887b9
[blender.git] / source / blender / blenkernel / intern / anim.c
1 /** anim.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 <string.h>
37
38 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_arithb.h"
41 #include "DNA_listBase.h"
42
43 #include "DNA_object_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_view3d_types.h"
47 #include "DNA_effect_types.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_meshdata_types.h"
50 #include "DNA_scene_types.h"
51
52 #include "BKE_DerivedMesh.h"
53 #include "BKE_global.h"
54 #include "BKE_utildefines.h"
55 #include "BKE_anim.h"
56 #include "BKE_ipo.h"
57 #include "BKE_object.h"
58 #include "BKE_displist.h"
59 #include "BKE_key.h"
60 #include "BKE_font.h"
61 #include "BKE_effect.h"
62
63 #include "BKE_bad_level_calls.h"
64
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69 ListBase duplilist= {0, 0}; 
70
71 void free_path(Path *path)
72 {
73         if(path->data) MEM_freeN(path->data);
74         MEM_freeN(path);
75 }
76
77
78 void calc_curvepath(Object *ob)
79 {
80         BevList *bl;
81         BevPoint *bevp, *bevpn, *bevpfirst, *bevplast, *tempbevp;
82         Curve *cu;
83         Nurb *nu;
84         Path *path;
85         float *fp, *dist, *maxdist, x, y, z;
86         float fac, d=0, fac1, fac2;
87         int a, tot, cycl=0;
88         float *ft;
89         
90         /* in a path vertices are with equal differences: path->len = number of verts */
91         /* NOW WITH BEVELCURVE!!! */
92         
93         if(ob==NULL || ob->type != OB_CURVE) return;
94         cu= ob->data;
95         if(ob==G.obedit) nu= editNurb.first;
96         else nu= cu->nurb.first;
97         
98         if(cu->path) free_path(cu->path);
99         cu->path= NULL;
100         
101         bl= cu->bev.first;
102         if(bl==NULL) return;
103
104         cu->path=path= MEM_callocN(sizeof(Path), "path");
105         
106         /* if POLY: last vertice != first vertice */
107         cycl= (bl->poly!= -1);
108         
109         if(cycl) tot= bl->nr;
110         else tot= bl->nr-1;
111         
112         path->len= tot+1;
113         /* exception: vector handle paths and polygon paths should be subdivided at least a factor 6 (or more?) */
114         if(path->len<6*nu->pntsu) path->len= 6*nu->pntsu;
115         
116         dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
117
118                 /* all lengths in *dist */
119         bevp= bevpfirst= (BevPoint *)(bl+1);
120         fp= dist;
121         *fp= 0;
122         for(a=0; a<tot; a++) {
123                 fp++;
124                 if(cycl && a==tot-1) {
125                         x= bevpfirst->x - bevp->x;
126                         y= bevpfirst->y - bevp->y;
127                         z= bevpfirst->z - bevp->z;
128                 }
129                 else {
130                         tempbevp = bevp+1;
131                         x= (tempbevp)->x - bevp->x;
132                         y= (tempbevp)->y - bevp->y;
133                         z= (tempbevp)->z - bevp->z;
134                 }
135                 *fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
136                 
137                 bevp++;
138         }
139         
140         path->totdist= *fp;
141
142                 /* the path verts  in path->data */
143                 /* now also with TILT value */
144         ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
145         
146         bevp= bevpfirst;
147         bevpn= bevp+1;
148         bevplast= bevpfirst + (bl->nr-1);
149         fp= dist+1;
150         maxdist= dist+tot;
151         fac= 1.0f/((float)path->len-1.0f);
152         fac = fac * path->totdist;
153
154         for(a=0; a<path->len; a++) {
155                 
156                 d= ((float)a)*fac;
157                 
158                 /* we're looking for location (distance) 'd' in the array */
159                 while((d>= *fp) && fp<maxdist) {
160                         fp++;
161                         if(bevp<bevplast) bevp++;
162                         bevpn= bevp+1;
163                         if(bevpn>bevplast) {
164                                 if(cycl) bevpn= bevpfirst;
165                                 else bevpn= bevplast;
166                         }
167                 }
168                 
169                 fac1= *(fp)- *(fp-1);
170                 fac2= *(fp)-d;
171                 fac1= fac2/fac1;
172                 fac2= 1.0f-fac1;
173
174                 ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
175                 ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
176                 ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
177                 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
178                 
179                 ft+= 4;
180
181         }
182         
183         MEM_freeN(dist);
184 }
185
186 int interval_test(int min, int max, int p1, int cycl)
187 {
188         
189         if(cycl) {
190                 if( p1 < min) 
191                         p1=  ((p1 -min) % (max-min+1)) + max+1;
192                 else if(p1 > max)
193                         p1=  ((p1 -min) % (max-min+1)) + min;
194         }
195         else {
196                 if(p1 < min) p1= min;
197                 else if(p1 > max) p1= max;
198         }
199         return p1;
200 }
201
202 /* warning, *vec needs FOUR items! */
203 /* ctime is normalized range <0-1> */
204 int where_on_path(Object *ob, float ctime, float *vec, float *dir)      /* returns OK */
205 {
206         Curve *cu;
207         Nurb *nu;
208         BevList *bl;
209         Path *path;
210         float *fp, *p0, *p1, *p2, *p3, fac;
211         float data[4];
212         int cycl=0, s0, s1, s2, s3;
213
214         if(ob==NULL || ob->type != OB_CURVE) return 0;
215         cu= ob->data;
216         if(cu->path==NULL || cu->path->data==NULL) {
217                 printf("no path!\n");
218         }
219         path= cu->path;
220         fp= path->data;
221         
222         /* test for cyclic */
223         bl= cu->bev.first;
224         if(bl && bl->poly> -1) cycl= 1;
225
226         ctime *= (path->len-1);
227         
228         s1= (int)floor(ctime);
229         fac= (float)(s1+1)-ctime;
230
231         /* path->len is corected for cyclic */
232         s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
233         s1= interval_test(0, path->len-1-cycl, s1, cycl);
234         s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
235         s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
236
237         p0= fp + 4*s0;
238         p1= fp + 4*s1;
239         p2= fp + 4*s2;
240         p3= fp + 4*s3;
241
242         /* note, commented out for follow constraint */
243         //if(cu->flag & CU_FOLLOW) {
244                 
245                 set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
246                 
247                 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
248                 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
249                 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
250                 
251                 /* make compatible with vectoquat */
252                 dir[0]= -dir[0];
253                 dir[1]= -dir[1];
254                 dir[2]= -dir[2];
255         //}
256         
257         nu= cu->nurb.first;
258
259         /* make sure that first and last frame are included in the vectors here  */
260         if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
261         else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
262         else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
263         else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
264
265         vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
266         vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
267         vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
268
269         vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
270
271         return 1;
272 }
273
274 static Object *new_dupli_object(ListBase *lb, Object *ob, Object *par)
275 {
276         Object *newob;
277         
278         newob= MEM_mallocN(sizeof(Object), "newobj dupli");
279
280         memcpy(newob, ob, sizeof(Object));
281         newob->flag |= OB_FROMDUPLI;
282         newob->id.newid= (ID *)par;     /* store duplicator */
283         
284         /* only basis-ball gets displist */
285         if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= NULL;
286
287         if(ob!=par) {   // dupliverts, particle
288                 newob->parent= NULL;
289                 newob->track= NULL;
290         }       
291         BLI_addtail(lb, newob);
292         
293         return newob;
294 }
295
296 void frames_duplilist(Object *ob)
297 {
298         extern int enable_cu_speed;     /* object.c */
299         Object *newob, copyob;
300         int cfrao, ok;
301         
302         cfrao= G.scene->r.cfra;
303         if(ob->parent==NULL && ob->track==NULL && ob->ipo==NULL && ob->constraints.first==NULL) return;
304
305         if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
306         copyob= *ob;    /* store transform info */
307
308         for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
309
310                 ok= 1;
311                 if(ob->dupoff) {
312                         ok= G.scene->r.cfra - ob->dupsta;
313                         ok= ok % (ob->dupon+ob->dupoff);
314                         if(ok < ob->dupon) ok= 1;
315                         else ok= 0;
316                 }
317                 if(ok) {
318                         newob= new_dupli_object(&duplilist, ob, ob);
319
320                         do_ob_ipo(newob);
321                         where_is_object_time(newob, (float)G.scene->r.cfra);
322                 }
323         }
324
325         *ob= copyob;    /* restore transform info */
326         G.scene->r.cfra= cfrao;
327         enable_cu_speed= 1;
328 }
329
330 struct vertexDupliData {
331         float pmat[4][4];
332         Object *ob, *par;
333 };
334
335 static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
336 {
337         struct vertexDupliData *vdd= userData;
338         Object *newob;
339         float vec[3], *q2, mat[3][3], tmat[4][4];
340         
341         VECCOPY(vec, co);
342         Mat4MulVecfl(vdd->pmat, vec);
343         VecSubf(vec, vec, vdd->pmat[3]);
344         VecAddf(vec, vec, vdd->ob->obmat[3]);
345         
346         newob= new_dupli_object(&duplilist, vdd->ob, vdd->par);
347         VECCOPY(newob->obmat[3], vec);
348         
349         if(vdd->par->transflag & OB_DUPLIROT) {
350                 
351                 vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
352                 
353                 q2= vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag);
354                 
355                 QuatToMat3(q2, mat);
356                 Mat4CpyMat4(tmat, newob->obmat);
357                 Mat4MulMat43(newob->obmat, tmat, mat);
358         }
359         
360 }
361
362 void vertex_duplilist(Scene *sce, Object *par)
363 {
364         Object *ob;
365         Base *base;
366         float vec[3], no[3], pmat[4][4];
367         int lay, totvert, a;
368         int dmNeedsFree;
369         DerivedMesh *dm;
370         
371         Mat4CpyMat4(pmat, par->obmat);
372         
373         lay= G.scene->lay;
374         
375         if(par==G.obedit)
376                 dm= editmesh_get_derived_cage(&dmNeedsFree);
377         else
378                 dm = mesh_get_derived_deform(par, &dmNeedsFree);
379         
380         totvert = dm->getNumVerts(dm);
381
382         base= sce->base.first;
383         while(base) {
384
385                 if(base->object->type>0 && (lay & base->lay) && G.obedit!=base->object) {
386                         ob= base->object->parent;
387                         while(ob) {
388                                 if(ob==par) {
389                                         struct vertexDupliData vdd;
390                                         
391                                         ob= base->object;
392                                         vdd.ob= ob;
393                                         vdd.par= par;
394                                         Mat4CpyMat4(vdd.pmat, pmat);
395                                         
396                                         /* mballs have a different dupli handling */
397                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
398
399                                         if(par==G.obedit) {
400                                                 dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
401                                         }
402                                         else {
403                                                 for(a=0; a<totvert; a++) {
404                                                         dm->getVertCo(dm, a, vec);
405                                                         dm->getVertNo(dm, a, no);
406                                                         
407                                                         vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
408                                                 }
409                                         }
410                                         
411                                         break;
412                                 }
413                                 ob= ob->parent;
414                         }
415                 }
416                 base= base->next;
417         }
418
419         if (dmNeedsFree)
420                 dm->release(dm);
421 }
422
423
424 void particle_duplilist(Scene *sce, Object *par, PartEff *paf)
425 {
426         Object *ob, *newob;
427         Base *base;
428         Particle *pa;
429         float ctime, vec1[3];
430         float vec[3], tmat[4][4], mat[3][3];
431         float *q2;
432         int lay, a;
433         
434         pa= paf->keys;
435         if(pa==0) {
436                 build_particle_system(par);
437                 pa= paf->keys;
438                 if(pa==0) return;
439         }
440         
441         ctime= bsystem_time(par, 0, (float)G.scene->r.cfra, 0.0);
442
443         lay= G.scene->lay;
444
445         base= sce->base.first;
446         while(base) {
447                 
448                 if(base->object->type>0 && (base->lay & lay) && G.obedit!=base->object) {
449                         ob= base->object->parent;
450                         while(ob) {
451                                 if(ob==par) {
452                                 
453                                         ob= base->object;
454                                         
455                                         pa= paf->keys;
456                                         for(a=0; a<paf->totpart; a++, pa+=paf->totkey) {
457                                                 
458                                                 if(paf->flag & PAF_STATIC) {
459                                                         float mtime;
460                                                         
461                                                         where_is_particle(paf, pa, pa->time, vec1);
462                                                         mtime= pa->time+pa->lifetime;
463                                                         
464                                                         for(ctime= pa->time; ctime<mtime; ctime+=paf->staticstep) {
465                                                                 newob= new_dupli_object(&duplilist, ob, par);
466                                                                 
467                                                                 /* make sure hair grows until the end.. */ 
468                                                                 if(ctime>pa->time+pa->lifetime) ctime= pa->time+pa->lifetime;
469                                                                 
470                                                                 /* to give ipos in object correct offset */
471                                                                 where_is_object_time(newob, ctime-pa->time);
472
473                                                                 where_is_particle(paf, pa, ctime, vec); // makes sure there's always a vec
474                                                                 Mat4MulVecfl(par->obmat, vec);
475                                                                 
476                                                                 if(paf->stype==PAF_VECT) {
477                                                                         where_is_particle(paf, pa, ctime+1.0, vec1); // makes sure there's always a vec
478                                                                         Mat4MulVecfl(par->obmat, vec1);
479
480                                                                         VecSubf(vec1, vec1, vec);
481                                                                         q2= vectoquat(vec1, ob->trackflag, ob->upflag);
482                                                                         
483                                                                         QuatToMat3(q2, mat);
484                                                                         Mat4CpyMat4(tmat, newob->obmat);
485                                                                         Mat4MulMat43(newob->obmat, tmat, mat);
486                                                                 }
487                                                                 
488                                                                 VECCOPY(newob->obmat[3], vec);
489                                                         }
490                                                 }
491                                                 else { // non static particles
492                                                            
493                                                         if((paf->flag & PAF_UNBORN)==0 && ctime < pa->time) continue;
494                                                         if((paf->flag & PAF_DIED)==0 && ctime > pa->time+pa->lifetime) continue;
495
496                                                         //if(ctime < pa->time+pa->lifetime) {
497                                                         newob= new_dupli_object(&duplilist, ob, par);
498
499                                                         /* to give ipos in object correct offset */
500                                                         where_is_object_time(newob, ctime-pa->time);
501                                                         
502                                                         where_is_particle(paf, pa, ctime, vec);
503                                                         if(paf->stype==PAF_VECT) {
504                                                                 where_is_particle(paf, pa, ctime+1.0f, vec1);
505                                                                 
506                                                                 VecSubf(vec1, vec1, vec);
507                                                                 q2= vectoquat(vec1, ob->trackflag, ob->upflag);
508                                         
509                                                                 QuatToMat3(q2, mat);
510                                                                 Mat4CpyMat4(tmat, newob->obmat);
511                                                                 Mat4MulMat43(newob->obmat, tmat, mat);
512                                                         }
513
514                                                         VECCOPY(newob->obmat[3], vec);
515                                                 }                                       
516                                         }
517                                         break;
518                                 }
519                                 ob= ob->parent;
520                         }
521                 }
522                 base= base->next;
523         }
524 }
525
526
527 void free_duplilist()
528 {
529         Object *ob;
530         
531         while( (ob= duplilist.first) ) {
532                 BLI_remlink(&duplilist, ob);
533                 MEM_freeN(ob);
534         }
535         
536 }
537
538 void make_duplilist(Scene *sce, Object *ob)
539 {
540         PartEff *paf;
541
542         if(ob->transflag & OB_DUPLI) {
543                 if(ob->transflag & OB_DUPLIVERTS) {
544                         if(ob->type==OB_MESH) {
545                                 if(ob->transflag & OB_DUPLIVERTS) {
546                                         if( (paf=give_parteff(ob)) ) particle_duplilist(sce, ob, paf);
547                                         else vertex_duplilist(sce, ob);
548                                 }
549                         }
550                         else if(ob->type==OB_FONT) {
551                                 font_duplilist(ob);
552                         }
553                 }
554                 else if(ob->transflag & OB_DUPLIFRAMES) frames_duplilist(ob);
555         }
556 }
557
558 int count_duplilist(Object *ob)
559 {
560         if(ob->transflag & OB_DUPLI) {
561                 if(ob->transflag & OB_DUPLIVERTS) {
562                         if(ob->type==OB_MESH) {
563                                 if(ob->transflag & OB_DUPLIVERTS) {
564                                         PartEff *paf;
565                                         if( (paf=give_parteff(ob)) ) {
566                                                 return paf->totpart;
567                                         }
568                                         else {
569                                                 Mesh *me= ob->data;
570                                                 return me->totvert;
571                                         }
572                                 }
573                         }
574                 }
575                 else if(ob->transflag & OB_DUPLIFRAMES) {
576                         int tot= ob->dupend - ob->dupsta; 
577                         tot/= (ob->dupon+ob->dupoff);
578                         return tot*ob->dupon;
579                 }
580         }
581         return 1;
582 }