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