Initial revision
[blender.git] / source / blender / blenkernel / intern / anim.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_arithb.h"
39 #include "DNA_listBase.h"
40
41 #include "DNA_object_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_key_types.h"
44 #include "DNA_view3d_types.h"
45 #include "DNA_effect_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_scene_types.h"
48
49 #include "BKE_global.h"
50 #include "BKE_utildefines.h"
51 #include "BKE_anim.h"
52 #include "BKE_ipo.h"
53 #include "BKE_object.h"
54 #include "BKE_displist.h"
55 #include "BKE_key.h"
56 #include "BKE_font.h"
57 #include "BKE_effect.h"
58
59 #include "BKE_bad_level_calls.h"
60
61 ListBase duplilist= {0, 0}; 
62
63 void free_path(Path *path)
64 {
65         if(path->data) MEM_freeN(path->data);
66         MEM_freeN(path);
67 }
68
69
70 void calc_curvepath(Object *ob)
71 {
72         BevList *bl;
73         BevPoint *bevp, *bevpn, *bevpfirst, *bevplast;
74         Curve *cu;
75         Nurb *nu;
76         Path *path;
77         float *fp, *dist, *maxdist, x, y, z;
78         float fac, d=0, fac1, fac2;
79         int a, tot, cycl=0;
80         float *ft;
81         
82         /* in een pad zitten allemaal punten met gelijke afstand: path->len = aantal pt */
83         /* NU MET BEVELCURVE!!! */
84         
85         if(ob==0 || ob->type != OB_CURVE) return;
86         cu= ob->data;
87         if(ob==G.obedit) nu= editNurb.first;
88         else nu= cu->nurb.first;
89         
90         if(cu->path) free_path(cu->path);
91         cu->path= 0;
92         
93         if((cu->flag & CU_PATH)==0) return;
94         
95         bl= cu->bev.first;
96         if(bl==0) {
97                 makeDispList(ob);
98                 bl= cu->bev.first;
99         }
100         if(bl==0) return;
101
102         cu->path=path= MEM_callocN(sizeof(Path), "path");
103         
104         /* als POLY: laatste punt != eerste punt */
105         cycl= (bl->poly!= -1);
106         
107         if(cycl) tot= bl->nr;
108         else tot= bl->nr-1;
109         
110         path->len= tot+1;
111         /* exception: vector handle paths and polygon paths should be subdivided at least a factor 6 (or more?) */
112         if(path->len<6*nu->pntsu) path->len= 6*nu->pntsu;
113         
114         dist= (float *)MEM_mallocN((tot+1)*4, "berekenpaddist");
115
116                 /* alle lengtes in *dist */
117         bevp= bevpfirst= (BevPoint *)(bl+1);
118         fp= dist;
119         *fp= 0;
120         for(a=0; a<tot; a++) {
121                 fp++;
122                 if(cycl && a==tot-1) {
123                         x= bevpfirst->x - bevp->x;
124                         y= bevpfirst->y - bevp->y;
125                         z= bevpfirst->z - bevp->z;
126                 }
127                 else {
128                         x= (bevp+1)->x - bevp->x;
129                         y= (bevp+1)->y - bevp->y;
130                         z= (bevp+1)->z - bevp->z;
131                 }
132                 *fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
133                 
134                 bevp++;
135         }
136         
137         path->totdist= *fp;
138
139                 /* de padpunten in path->data */
140                 /* nu ook met TILT */
141         ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
142         
143         bevp= bevpfirst;
144         bevpn= bevp+1;
145         bevplast= bevpfirst + (bl->nr-1);
146         fp= dist+1;
147         maxdist= dist+tot;
148         fac= 1.0f/((float)path->len-1.0f);
149
150         for(a=0; a<path->len; a++) {
151                 
152                 d= ((float)a)*fac*path->totdist;
153                 
154                 /* we zoeken plek 'd' in het array */
155                 while((d>= *fp) && fp<maxdist) {
156                         fp++;
157                         if(bevp<bevplast) bevp++;
158                         bevpn= bevp+1;
159                         if(bevpn>bevplast) {
160                                 if(cycl) bevpn= bevpfirst;
161                                 else bevpn= bevplast;
162                         }
163                 }
164                 
165                 fac1= *(fp)- *(fp-1);
166                 fac2= *(fp)-d;
167                 fac1= fac2/fac1;
168                 fac2= 1.0f-fac1;
169
170                 ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
171                 ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
172                 ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
173                 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
174                 
175                 ft+= 4;
176
177         }
178         
179         MEM_freeN(dist);
180 }
181
182 int interval_test(int min, int max, int p1, int cycl)
183 {
184         
185         if(cycl) {
186                 if( p1 < min) 
187                         p1=  ((p1 -min) % (max-min+1)) + max+1;
188                 else if(p1 > max)
189                         p1=  ((p1 -min) % (max-min+1)) + min;
190         }
191         else {
192                 if(p1 < min) p1= min;
193                 else if(p1 > max) p1= max;
194         }
195         return p1;
196 }
197
198 int where_on_path(Object *ob, float ctime, float *vec, float *dir)      /* geeft OK terug */
199 {
200         Curve *cu;
201         Nurb *nu;
202         BevList *bl;
203         Path *path;
204         float *fp, *p0, *p1, *p2, *p3, fac;
205         float data[4];
206         int cycl=0, s0, s1, s2, s3;
207
208         
209         if(ob==0 || ob->type != OB_CURVE) return 0;
210         cu= ob->data;
211         if(cu->path==0 || cu->path->data==0) calc_curvepath(ob);
212         path= cu->path;
213         fp= path->data;
214         
215         /* cyclic testen */
216         bl= cu->bev.first;
217         if(bl && bl->poly> -1) cycl= 1;
218
219         /* ctime is van 0.0-1.0 */
220         ctime *= (path->len-1);
221         
222         s1= (int)floor(ctime);
223         fac= (float)(s1+1)-ctime;
224
225         /* path->len is gecorrigeerd voor cyclic, zie boven, is beetje warrig! */
226         s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
227         s1= interval_test(0, path->len-1-cycl, s1, cycl);
228         s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
229         s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
230
231         p0= fp + 4*s0;
232         p1= fp + 4*s1;
233         p2= fp + 4*s2;
234         p3= fp + 4*s3;
235
236         if(cu->flag & CU_FOLLOW) {
237                 
238                 set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
239                 
240                 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
241                 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
242                 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
243                 
244                 /* compatible maken met vectoquat */
245                 dir[0]= -dir[0];
246                 dir[1]= -dir[1];
247                 dir[2]= -dir[2];
248         }
249         
250         nu= cu->nurb.first;
251
252         /* zeker van zijn dat de eerste en laatste frame door de punten gaat */
253         if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
254         else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
255         else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
256         else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
257
258         vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
259         vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
260         vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
261
262         vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
263
264         return 1;
265 }
266
267 void frames_duplilist(Object *ob)
268 {
269         extern int enable_cu_speed;     /* object.c */
270         Object *newob;
271         int cfrao, ok;
272         
273         cfrao= G.scene->r.cfra;
274         if(ob->parent==0 && ob->track==0 && ob->ipo==0) return;
275
276         if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
277
278         /* dit om zeker van te zijn dat er iets gezbufferd wordt: in drawobject.c: dt==wire en boundboxclip */
279         if(G.background==0 && ob->type==OB_MESH) {
280                 Mesh *me= ob->data;
281                 DispList *dl;
282                 if(me->disp.first==0) addnormalsDispList(ob, &me->disp);
283                 if(ob->dt==OB_SHADED) {
284                         dl= ob->disp.first;
285                         if(dl==0 || dl->col1==0) shadeDispList(ob);
286                 }
287         }
288         
289         for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
290
291                 ok= 1;
292                 if(ob->dupoff) {
293                         ok= G.scene->r.cfra - ob->dupsta;
294                         ok= ok % (ob->dupon+ob->dupoff);
295                         if(ok < ob->dupon) ok= 1;
296                         else ok= 0;
297                 }
298                 if(ok) {
299                         newob= MEM_mallocN(sizeof(Object), "newobobj dupli");
300                         memcpy(newob, ob, sizeof(Object));
301                         
302                         /* alleen de basis-ball behoeft een displist */
303                         if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
304
305                         BLI_addtail(&duplilist, newob);
306                         do_ob_ipo(newob);
307                         where_is_object(newob);
308
309                         newob->flag |= OB_FROMDUPLI;
310                         newob->id.newid= (ID *)ob;      /* duplicator bewaren */
311                 }
312         }
313
314         G.scene->r.cfra= cfrao;
315         enable_cu_speed= 1;
316         do_ob_ipo(ob);
317 }
318
319
320 void vertex_duplilist(Scene *sce, Object *par)
321 {
322         Object *ob, *newob;
323         Base *base;
324         MVert *mvert;
325         Mesh *me;
326         float vec[3], pvec[3], pmat[4][4], mat[3][3], tmat[4][4];
327         float *q2;
328         int lay, totvert, a;
329         
330         Mat4CpyMat4(pmat, par->obmat);
331         
332         Mat4One(tmat);
333         
334         lay= G.scene->lay;
335         
336         base= sce->base.first;
337         while(base) {
338
339                 if(base->object->type>0 && (lay & base->lay) && G.obedit!=base->object) {
340                         ob= base->object->parent;
341                         while(ob) {
342                                 if(ob==par) {
343                                 
344                                         ob= base->object;
345                                         /* mballs have a different dupli handling */
346                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
347                                         
348                                         me= par->data;
349                                         mvert= me->mvert;
350                                         mvert+= (me->totvert-1);
351                                         VECCOPY(pvec, mvert->co);
352                                         Mat4MulVecfl(pmat, pvec);
353
354                                         mvert= me->mvert;
355                                         totvert= me->totvert;
356
357                                         for(a=0; a<totvert; a++, mvert++) {
358                                         
359                                                 /* bereken de extra offset (tov. nulpunt parent) die de kinderen krijgen */
360                                                 VECCOPY(vec, mvert->co);
361                                                 Mat4MulVecfl(pmat, vec);
362                                                 VecSubf(vec, vec, pmat[3]);
363                                                 VecAddf(vec, vec, ob->obmat[3]);
364                                                 
365                                                 newob= MEM_mallocN(sizeof(Object), "newobj dupli");
366                                                 memcpy(newob, ob, sizeof(Object));
367                                                 newob->flag |= OB_FROMDUPLI;
368                                                 newob->id.newid= (ID *)par;     /* duplicator bewaren */
369                                                 
370                                                 /* alleen de basis-ball behoeft een displist */
371                                                 if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
372                                                 
373                                                 VECCOPY(newob->obmat[3], vec);
374                                                 
375                                                 if(par->transflag & OB_DUPLIROT) {
376                                                         VECCOPY(vec, mvert->no);
377                                                         vec[0]= -vec[0]; vec[1]= -vec[1]; vec[2]= -vec[2];
378                                                         
379                                                         q2= vectoquat(vec, ob->trackflag, ob->upflag);
380                                 
381                                                         QuatToMat3(q2, mat);
382                                                         Mat4CpyMat4(tmat, newob->obmat);
383                                                         Mat4MulMat43(newob->obmat, tmat, mat);
384                                                 }
385                                                 
386                                                 newob->parent= 0;
387                                                 newob->track= 0;
388                                                 /* newob->borig= base; */
389                                                 
390                                                 BLI_addtail(&duplilist, newob);
391                                                 
392                                                 VECCOPY(pvec, vec);
393                                         
394                                         }
395                                         break;
396                                 }
397                                 ob= ob->parent;
398                         }
399                 }
400                 base= base->next;
401         }
402 }
403
404
405 void particle_duplilist(Scene *sce, Object *par, PartEff *paf)
406 {
407         Object *ob, *newob;
408         Base *base;
409         Particle *pa;
410         float ctime, vec1[3];
411         float vec[3], tmat[4][4], mat[3][3];
412         float *q2;
413         int lay, a;
414         
415         pa= paf->keys;
416         if(pa==0) {
417                 build_particle_system(par);
418                 pa= paf->keys;
419                 if(pa==0) return;
420         }
421         
422         ctime= bsystem_time(par, 0, (float)G.scene->r.cfra, 0.0);
423
424         lay= G.scene->lay;
425
426         base= sce->base.first;
427         while(base) {
428                 
429                 if(base->object->type>0 && (base->lay & lay) && G.obedit!=base->object) {
430                         ob= base->object->parent;
431                         while(ob) {
432                                 if(ob==par) {
433                                 
434                                         ob= base->object;
435                                         
436                                         pa= paf->keys;
437                                         for(a=0; a<paf->totpart; a++, pa+=paf->totkey) {
438                                                 
439                                                 if(ctime > pa->time) {
440                                                         if(ctime < pa->time+pa->lifetime) {
441                                                                         
442                                                                 newob= MEM_mallocN(sizeof(Object), "newobj dupli");
443                                                                 memcpy(newob, ob, sizeof(Object));
444                                                                 newob->flag |= OB_FROMDUPLI;
445                                                                 newob->id.newid= (ID *)par;     /* duplicator bewaren */
446
447                                                                 /* alleen de basis-ball behoeft een displist */
448                                                                 if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
449                                                                 
450                                                                 where_is_particle(paf, pa, ctime, vec);
451                                                                 if(paf->stype==PAF_VECT) {
452                                                                         where_is_particle(paf, pa, ctime+1.0f, vec1);
453                                                                         
454                                                                         VecSubf(vec1, vec1, vec);
455                                                                         q2= vectoquat(vec1, ob->trackflag, ob->upflag);
456                                                 
457                                                                         QuatToMat3(q2, mat);
458                                                                         Mat4CpyMat4(tmat, newob->obmat);
459                                                                         Mat4MulMat43(newob->obmat, tmat, mat);
460                                                                 }
461
462                                                                 VECCOPY(newob->obmat[3], vec);
463                                                         
464                                                                 newob->parent= 0;
465                                                                 newob->track= 0;
466                                                                 
467                                                                 BLI_addtail(&duplilist, newob);
468                                                                 
469                                                         }
470                                                 }                                               
471                                         }
472                                         break;
473                                 }
474                                 ob= ob->parent;
475                         }
476                 }
477                 base= base->next;
478         }
479 }
480
481
482 void free_duplilist()
483 {
484         Object *ob;
485         
486         while( ob= duplilist.first) {
487                 BLI_remlink(&duplilist, ob);
488                 MEM_freeN(ob);
489         }
490         
491 }
492
493 void make_duplilist(Scene *sce, Object *ob)
494 {
495         PartEff *paf;
496         
497         if(ob->transflag & OB_DUPLI) {
498                 if(ob->transflag & OB_DUPLIVERTS) {
499                         if(ob->type==OB_MESH) {
500                                 if(ob->transflag & OB_DUPLIVERTS) {
501                                         if( paf=give_parteff(ob) ) particle_duplilist(sce, ob, paf);
502                                         else vertex_duplilist(sce, ob);
503                                 }
504                         }
505                         else if(ob->type==OB_FONT) {
506                                 font_duplilist(ob);
507                         }
508                 }
509                 else if(ob->transflag & OB_DUPLIFRAMES) frames_duplilist(ob);
510         }
511 }
512