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