some small optimizations
[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, *tempbevp;
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                         tempbevp = bevp+1;
129                         x= (tempbevp)->x - bevp->x;
130                         y= (tempbevp)->y - bevp->y;
131                         z= (tempbevp)->z - bevp->z;
132                 }
133                 *fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
134                 
135                 bevp++;
136         }
137         
138         path->totdist= *fp;
139
140                 /* de padpunten in path->data */
141                 /* nu ook met TILT */
142         ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
143         
144         bevp= bevpfirst;
145         bevpn= bevp+1;
146         bevplast= bevpfirst + (bl->nr-1);
147         fp= dist+1;
148         maxdist= dist+tot;
149         fac= 1.0f/((float)path->len-1.0f);
150         fac = fac * path->totdist;
151
152         for(a=0; a<path->len; a++) {
153                 
154                 d= ((float)a)*fac;
155                 
156                 /* we zoeken plek 'd' in het array */
157                 while((d>= *fp) && fp<maxdist) {
158                         fp++;
159                         if(bevp<bevplast) bevp++;
160                         bevpn= bevp+1;
161                         if(bevpn>bevplast) {
162                                 if(cycl) bevpn= bevpfirst;
163                                 else bevpn= bevplast;
164                         }
165                 }
166                 
167                 fac1= *(fp)- *(fp-1);
168                 fac2= *(fp)-d;
169                 fac1= fac2/fac1;
170                 fac2= 1.0f-fac1;
171
172                 ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
173                 ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
174                 ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
175                 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
176                 
177                 ft+= 4;
178
179         }
180         
181         MEM_freeN(dist);
182 }
183
184 int interval_test(int min, int max, int p1, int cycl)
185 {
186         
187         if(cycl) {
188                 if( p1 < min) 
189                         p1=  ((p1 -min) % (max-min+1)) + max+1;
190                 else if(p1 > max)
191                         p1=  ((p1 -min) % (max-min+1)) + min;
192         }
193         else {
194                 if(p1 < min) p1= min;
195                 else if(p1 > max) p1= max;
196         }
197         return p1;
198 }
199
200 int where_on_path(Object *ob, float ctime, float *vec, float *dir)      /* geeft OK terug */
201 {
202         Curve *cu;
203         Nurb *nu;
204         BevList *bl;
205         Path *path;
206         float *fp, *p0, *p1, *p2, *p3, fac;
207         float data[4];
208         int cycl=0, s0, s1, s2, s3;
209
210         
211         if(ob==0 || ob->type != OB_CURVE) return 0;
212         cu= ob->data;
213         if(cu->path==0 || cu->path->data==0) calc_curvepath(ob);
214         path= cu->path;
215         fp= path->data;
216         
217         /* cyclic testen */
218         bl= cu->bev.first;
219         if(bl && bl->poly> -1) cycl= 1;
220
221         /* ctime is van 0.0-1.0 */
222         ctime *= (path->len-1);
223         
224         s1= (int)floor(ctime);
225         fac= (float)(s1+1)-ctime;
226
227         /* path->len is gecorrigeerd voor cyclic, zie boven, is beetje warrig! */
228         s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
229         s1= interval_test(0, path->len-1-cycl, s1, cycl);
230         s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
231         s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
232
233         p0= fp + 4*s0;
234         p1= fp + 4*s1;
235         p2= fp + 4*s2;
236         p3= fp + 4*s3;
237
238         if(cu->flag & CU_FOLLOW) {
239                 
240                 set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
241                 
242                 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
243                 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
244                 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
245                 
246                 /* compatible maken met vectoquat */
247                 dir[0]= -dir[0];
248                 dir[1]= -dir[1];
249                 dir[2]= -dir[2];
250         }
251         
252         nu= cu->nurb.first;
253
254         /* zeker van zijn dat de eerste en laatste frame door de punten gaat */
255         if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
256         else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
257         else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
258         else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
259
260         vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
261         vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
262         vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
263
264         vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
265
266         return 1;
267 }
268
269 void frames_duplilist(Object *ob)
270 {
271         extern int enable_cu_speed;     /* object.c */
272         Object *newob;
273         int cfrao, ok;
274         
275         cfrao= G.scene->r.cfra;
276         if(ob->parent==0 && ob->track==0 && ob->ipo==0) return;
277
278         if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
279
280         /* dit om zeker van te zijn dat er iets gezbufferd wordt: in drawobject.c: dt==wire en boundboxclip */
281         if(G.background==0 && ob->type==OB_MESH) {
282                 Mesh *me= ob->data;
283                 DispList *dl;
284                 if(me->disp.first==0) addnormalsDispList(ob, &me->disp);
285                 if(ob->dt==OB_SHADED) {
286                         dl= ob->disp.first;
287                         if(dl==0 || dl->col1==0) shadeDispList(ob);
288                 }
289         }
290         
291         for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
292
293                 ok= 1;
294                 if(ob->dupoff) {
295                         ok= G.scene->r.cfra - ob->dupsta;
296                         ok= ok % (ob->dupon+ob->dupoff);
297                         if(ok < ob->dupon) ok= 1;
298                         else ok= 0;
299                 }
300                 if(ok) {
301                         newob= MEM_mallocN(sizeof(Object), "newobobj dupli");
302                         memcpy(newob, ob, sizeof(Object));
303                         
304                         /* alleen de basis-ball behoeft een displist */
305                         if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
306
307                         BLI_addtail(&duplilist, newob);
308                         do_ob_ipo(newob);
309                         where_is_object(newob);
310
311                         newob->flag |= OB_FROMDUPLI;
312                         newob->id.newid= (ID *)ob;      /* duplicator bewaren */
313                 }
314         }
315
316         G.scene->r.cfra= cfrao;
317         enable_cu_speed= 1;
318         do_ob_ipo(ob);
319 }
320
321
322 void vertex_duplilist(Scene *sce, Object *par)
323 {
324         Object *ob, *newob;
325         Base *base;
326         MVert *mvert;
327         Mesh *me;
328         float vec[3], pvec[3], pmat[4][4], mat[3][3], tmat[4][4];
329         float *q2;
330         int lay, totvert, a;
331         
332         Mat4CpyMat4(pmat, par->obmat);
333         
334         Mat4One(tmat);
335         
336         lay= G.scene->lay;
337         
338         base= sce->base.first;
339         while(base) {
340
341                 if(base->object->type>0 && (lay & base->lay) && G.obedit!=base->object) {
342                         ob= base->object->parent;
343                         while(ob) {
344                                 if(ob==par) {
345                                 
346                                         ob= base->object;
347                                         /* mballs have a different dupli handling */
348                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
349                                         
350                                         me= par->data;
351                                         mvert= me->mvert;
352                                         mvert+= (me->totvert-1);
353                                         VECCOPY(pvec, mvert->co);
354                                         Mat4MulVecfl(pmat, pvec);
355
356                                         mvert= me->mvert;
357                                         totvert= me->totvert;
358
359                                         for(a=0; a<totvert; a++, mvert++) {
360                                         
361                                                 /* bereken de extra offset (tov. nulpunt parent) die de kinderen krijgen */
362                                                 VECCOPY(vec, mvert->co);
363                                                 Mat4MulVecfl(pmat, vec);
364                                                 VecSubf(vec, vec, pmat[3]);
365                                                 VecAddf(vec, vec, ob->obmat[3]);
366                                                 
367                                                 newob= MEM_mallocN(sizeof(Object), "newobj dupli");
368                                                 memcpy(newob, ob, sizeof(Object));
369                                                 newob->flag |= OB_FROMDUPLI;
370                                                 newob->id.newid= (ID *)par;     /* duplicator bewaren */
371                                                 
372                                                 /* alleen de basis-ball behoeft een displist */
373                                                 if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
374                                                 
375                                                 VECCOPY(newob->obmat[3], vec);
376                                                 
377                                                 if(par->transflag & OB_DUPLIROT) {
378                                                         VECCOPY(vec, mvert->no);
379                                                         vec[0]= -vec[0]; vec[1]= -vec[1]; vec[2]= -vec[2];
380                                                         
381                                                         q2= vectoquat(vec, ob->trackflag, ob->upflag);
382                                 
383                                                         QuatToMat3(q2, mat);
384                                                         Mat4CpyMat4(tmat, newob->obmat);
385                                                         Mat4MulMat43(newob->obmat, tmat, mat);
386                                                 }
387                                                 
388                                                 newob->parent= 0;
389                                                 newob->track= 0;
390                                                 /* newob->borig= base; */
391                                                 
392                                                 BLI_addtail(&duplilist, newob);
393                                                 
394                                                 VECCOPY(pvec, vec);
395                                         
396                                         }
397                                         break;
398                                 }
399                                 ob= ob->parent;
400                         }
401                 }
402                 base= base->next;
403         }
404 }
405
406
407 void particle_duplilist(Scene *sce, Object *par, PartEff *paf)
408 {
409         Object *ob, *newob;
410         Base *base;
411         Particle *pa;
412         float ctime, vec1[3];
413         float vec[3], tmat[4][4], mat[3][3];
414         float *q2;
415         int lay, a;
416         
417         pa= paf->keys;
418         if(pa==0) {
419                 build_particle_system(par);
420                 pa= paf->keys;
421                 if(pa==0) return;
422         }
423         
424         ctime= bsystem_time(par, 0, (float)G.scene->r.cfra, 0.0);
425
426         lay= G.scene->lay;
427
428         base= sce->base.first;
429         while(base) {
430                 
431                 if(base->object->type>0 && (base->lay & lay) && G.obedit!=base->object) {
432                         ob= base->object->parent;
433                         while(ob) {
434                                 if(ob==par) {
435                                 
436                                         ob= base->object;
437                                         
438                                         pa= paf->keys;
439                                         for(a=0; a<paf->totpart; a++, pa+=paf->totkey) {
440                                                 
441                                                 if(ctime > pa->time) {
442                                                         if(ctime < pa->time+pa->lifetime) {
443                                                                         
444                                                                 newob= MEM_mallocN(sizeof(Object), "newobj dupli");
445                                                                 memcpy(newob, ob, sizeof(Object));
446                                                                 newob->flag |= OB_FROMDUPLI;
447                                                                 newob->id.newid= (ID *)par;     /* duplicator bewaren */
448
449                                                                 /* alleen de basis-ball behoeft een displist */
450                                                                 if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
451                                                                 
452                                                                 where_is_particle(paf, pa, ctime, vec);
453                                                                 if(paf->stype==PAF_VECT) {
454                                                                         where_is_particle(paf, pa, ctime+1.0f, vec1);
455                                                                         
456                                                                         VecSubf(vec1, vec1, vec);
457                                                                         q2= vectoquat(vec1, ob->trackflag, ob->upflag);
458                                                 
459                                                                         QuatToMat3(q2, mat);
460                                                                         Mat4CpyMat4(tmat, newob->obmat);
461                                                                         Mat4MulMat43(newob->obmat, tmat, mat);
462                                                                 }
463
464                                                                 VECCOPY(newob->obmat[3], vec);
465                                                         
466                                                                 newob->parent= 0;
467                                                                 newob->track= 0;
468                                                                 
469                                                                 BLI_addtail(&duplilist, newob);
470                                                                 
471                                                         }
472                                                 }                                               
473                                         }
474                                         break;
475                                 }
476                                 ob= ob->parent;
477                         }
478                 }
479                 base= base->next;
480         }
481 }
482
483
484 void free_duplilist()
485 {
486         Object *ob;
487         
488         while( ob= duplilist.first) {
489                 BLI_remlink(&duplilist, ob);
490                 MEM_freeN(ob);
491         }
492         
493 }
494
495 void make_duplilist(Scene *sce, Object *ob)
496 {
497         PartEff *paf;
498         
499         if(ob->transflag & OB_DUPLI) {
500                 if(ob->transflag & OB_DUPLIVERTS) {
501                         if(ob->type==OB_MESH) {
502                                 if(ob->transflag & OB_DUPLIVERTS) {
503                                         if( paf=give_parteff(ob) ) particle_duplilist(sce, ob, paf);
504                                         else vertex_duplilist(sce, ob);
505                                 }
506                         }
507                         else if(ob->type==OB_FONT) {
508                                 font_duplilist(ob);
509                         }
510                 }
511                 else if(ob->transflag & OB_DUPLIFRAMES) frames_duplilist(ob);
512         }
513 }
514