soc-2008-mxcurioni: merged changes to revision 14967
[blender.git] / source / blender / blenkernel / intern / anim.c
1 /** anim.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL 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.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <math.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_arithb.h"
38 #include "BLI_rand.h"
39 #include "DNA_listBase.h"
40
41 #include "DNA_curve_types.h"
42 #include "DNA_effect_types.h"
43 #include "DNA_group_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_modifier_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_vfont_types.h"
53
54 #include "BKE_anim.h"
55 #include "BKE_DerivedMesh.h"
56 #include "BKE_displist.h"
57 #include "BKE_effect.h"
58 #include "BKE_font.h"
59 #include "BKE_group.h"
60 #include "BKE_global.h"
61 #include "BKE_ipo.h"
62 #include "BKE_key.h"
63 #include "BKE_lattice.h"
64 #include "BKE_main.h"
65 #include "BKE_mesh.h"
66 #include "BKE_object.h"
67 #include "BKE_particle.h"
68 #include "BKE_utildefines.h"
69
70 #include "BKE_bad_level_calls.h"
71
72 #ifdef HAVE_CONFIG_H
73 #include <config.h>
74 #endif
75
76 static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated);
77
78 void free_path(Path *path)
79 {
80         if(path->data) MEM_freeN(path->data);
81         MEM_freeN(path);
82 }
83
84
85 void calc_curvepath(Object *ob)
86 {
87         BevList *bl;
88         BevPoint *bevp, *bevpn, *bevpfirst, *bevplast, *tempbevp;
89         Curve *cu;
90         Nurb *nu;
91         Path *path;
92         float *fp, *dist, *maxdist, x, y, z;
93         float fac, d=0, fac1, fac2;
94         int a, tot, cycl=0;
95         float *ft;
96         
97         /* in a path vertices are with equal differences: path->len = number of verts */
98         /* NOW WITH BEVELCURVE!!! */
99         
100         if(ob==NULL || ob->type != OB_CURVE) return;
101         cu= ob->data;
102         if(ob==G.obedit) nu= editNurb.first;
103         else nu= cu->nurb.first;
104         
105         if(cu->path) free_path(cu->path);
106         cu->path= NULL;
107         
108         bl= cu->bev.first;
109         if(bl==NULL || !bl->nr) return;
110
111         cu->path=path= MEM_callocN(sizeof(Path), "path");
112         
113         /* if POLY: last vertice != first vertice */
114         cycl= (bl->poly!= -1);
115         
116         if(cycl) tot= bl->nr;
117         else tot= bl->nr-1;
118         
119         path->len= tot+1;
120         /* exception: vector handle paths and polygon paths should be subdivided at least a factor resolu */
121         if(path->len<nu->resolu*nu->pntsu) path->len= nu->resolu*nu->pntsu;
122         
123         dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
124
125                 /* all lengths in *dist */
126         bevp= bevpfirst= (BevPoint *)(bl+1);
127         fp= dist;
128         *fp= 0;
129         for(a=0; a<tot; a++) {
130                 fp++;
131                 if(cycl && a==tot-1) {
132                         x= bevpfirst->x - bevp->x;
133                         y= bevpfirst->y - bevp->y;
134                         z= bevpfirst->z - bevp->z;
135                 }
136                 else {
137                         tempbevp = bevp+1;
138                         x= (tempbevp)->x - bevp->x;
139                         y= (tempbevp)->y - bevp->y;
140                         z= (tempbevp)->z - bevp->z;
141                 }
142                 *fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
143                 
144                 bevp++;
145         }
146         
147         path->totdist= *fp;
148
149                 /* the path verts  in path->data */
150                 /* now also with TILT value */
151         ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
152         
153         bevp= bevpfirst;
154         bevpn= bevp+1;
155         bevplast= bevpfirst + (bl->nr-1);
156         fp= dist+1;
157         maxdist= dist+tot;
158         fac= 1.0f/((float)path->len-1.0f);
159         fac = fac * path->totdist;
160
161         for(a=0; a<path->len; a++) {
162                 
163                 d= ((float)a)*fac;
164                 
165                 /* we're looking for location (distance) 'd' in the array */
166                 while((d>= *fp) && fp<maxdist) {
167                         fp++;
168                         if(bevp<bevplast) bevp++;
169                         bevpn= bevp+1;
170                         if(bevpn>bevplast) {
171                                 if(cycl) bevpn= bevpfirst;
172                                 else bevpn= bevplast;
173                         }
174                 }
175                 
176                 fac1= *(fp)- *(fp-1);
177                 fac2= *(fp)-d;
178                 fac1= fac2/fac1;
179                 fac2= 1.0f-fac1;
180
181                 ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
182                 ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
183                 ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
184                 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
185                 
186                 ft+= 4;
187
188         }
189         
190         MEM_freeN(dist);
191 }
192
193 int interval_test(int min, int max, int p1, int cycl)
194 {
195         
196         if(cycl) {
197                 if( p1 < min) 
198                         p1=  ((p1 -min) % (max-min+1)) + max+1;
199                 else if(p1 > max)
200                         p1=  ((p1 -min) % (max-min+1)) + min;
201         }
202         else {
203                 if(p1 < min) p1= min;
204                 else if(p1 > max) p1= max;
205         }
206         return p1;
207 }
208
209 /* warning, *vec needs FOUR items! */
210 /* ctime is normalized range <0-1> */
211 int where_on_path(Object *ob, float ctime, float *vec, float *dir)      /* returns OK */
212 {
213         Curve *cu;
214         Nurb *nu;
215         BevList *bl;
216         Path *path;
217         float *fp, *p0, *p1, *p2, *p3, fac;
218         float data[4];
219         int cycl=0, s0, s1, s2, s3;
220
221         if(ob==NULL || ob->type != OB_CURVE) return 0;
222         cu= ob->data;
223         if(cu->path==NULL || cu->path->data==NULL) {
224                 printf("no path!\n");
225         }
226         path= cu->path;
227         fp= path->data;
228         
229         /* test for cyclic */
230         bl= cu->bev.first;
231         if (!bl->nr) return 0;
232         if(bl && bl->poly> -1) cycl= 1;
233
234         ctime *= (path->len-1);
235         
236         s1= (int)floor(ctime);
237         fac= (float)(s1+1)-ctime;
238
239         /* path->len is corected for cyclic */
240         s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
241         s1= interval_test(0, path->len-1-cycl, s1, cycl);
242         s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
243         s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
244
245         p0= fp + 4*s0;
246         p1= fp + 4*s1;
247         p2= fp + 4*s2;
248         p3= fp + 4*s3;
249
250         /* note, commented out for follow constraint */
251         //if(cu->flag & CU_FOLLOW) {
252                 
253                 set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
254                 
255                 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
256                 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
257                 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
258                 
259                 /* make compatible with vectoquat */
260                 dir[0]= -dir[0];
261                 dir[1]= -dir[1];
262                 dir[2]= -dir[2];
263         //}
264         
265         nu= cu->nurb.first;
266
267         /* make sure that first and last frame are included in the vectors here  */
268         if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
269         else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
270         else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
271         else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
272
273         vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
274         vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
275         vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
276
277         vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
278
279         return 1;
280 }
281
282 /* ****************** DUPLICATOR ************** */
283
284 static DupliObject *new_dupli_object(ListBase *lb, Object *ob, float mat[][4], int lay, int index, int type, int animated)
285 {
286         DupliObject *dob= MEM_callocN(sizeof(DupliObject), "dupliobject");
287         
288         BLI_addtail(lb, dob);
289         dob->ob= ob;
290         Mat4CpyMat4(dob->mat, mat);
291         Mat4CpyMat4(dob->omat, ob->obmat);
292         dob->origlay= ob->lay;
293         dob->index= index;
294         dob->type= type;
295         dob->animated= (type == OB_DUPLIGROUP) && animated;
296         ob->lay= lay;
297         
298         return dob;
299 }
300
301 static void group_duplilist(ListBase *lb, Object *ob, int level, int animated)
302 {
303         DupliObject *dob;
304         Group *group;
305         GroupObject *go;
306         float mat[4][4];
307         
308         if(ob->dup_group==NULL) return;
309         group= ob->dup_group;
310         
311         /* simple preventing of too deep nested groups */
312         if(level>MAX_DUPLI_RECUR) return;
313         
314         /* handles animated groups, and */
315         /* we need to check update for objects that are not in scene... */
316         group_handle_recalc_and_update(ob, group);
317         animated= animated || group_is_animated(ob, group);
318         
319         for(go= group->gobject.first; go; go= go->next) {
320                 /* note, if you check on layer here, render goes wrong... it still deforms verts and uses parent imat */
321                 if(go->ob!=ob) {
322                         
323                         Mat4MulMat4(mat, go->ob->obmat, ob->obmat);
324                         dob= new_dupli_object(lb, go->ob, mat, ob->lay, 0, OB_DUPLIGROUP, animated);
325                         dob->no_draw= (dob->origlay & group->layer)==0;
326                         
327                         if(go->ob->transflag & OB_DUPLI) {
328                                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
329                                 object_duplilist_recursive((ID *)group, go->ob, lb, ob->obmat, level+1, animated);
330                                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
331                         }
332                 }
333         }
334 }
335
336 static void frames_duplilist(ListBase *lb, Object *ob, int level, int animated)
337 {
338         extern int enable_cu_speed;     /* object.c */
339         Object copyob;
340         int cfrao, ok;
341         
342         /* simple preventing of too deep nested groups */
343         if(level>MAX_DUPLI_RECUR) return;
344         
345         cfrao= G.scene->r.cfra;
346         if(ob->parent==NULL && ob->track==NULL && ob->ipo==NULL && ob->constraints.first==NULL) return;
347
348         if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
349         copyob= *ob;    /* store transform info */
350
351         for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
352
353                 ok= 1;
354                 if(ob->dupoff) {
355                         ok= G.scene->r.cfra - ob->dupsta;
356                         ok= ok % (ob->dupon+ob->dupoff);
357                         if(ok < ob->dupon) ok= 1;
358                         else ok= 0;
359                 }
360                 if(ok) {
361                         do_ob_ipo(ob);
362                         where_is_object_time(ob, (float)G.scene->r.cfra);
363                         new_dupli_object(lb, ob, ob->obmat, ob->lay, G.scene->r.cfra, OB_DUPLIFRAMES, animated);
364                 }
365         }
366
367         *ob= copyob;    /* restore transform info */
368         G.scene->r.cfra= cfrao;
369         enable_cu_speed= 1;
370 }
371
372 struct vertexDupliData {
373         ID *id; /* scene or group, for recursive loops */
374         int level;
375         int animated;
376         ListBase *lb;
377         float pmat[4][4];
378         float obmat[4][4]; /* Only used for dupliverts inside dupligroups, where the ob->obmat is modified */
379         Object *ob, *par;
380         float (*orco)[3];
381 };
382
383 static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
384 {
385         DupliObject *dob;
386         struct vertexDupliData *vdd= userData;
387         float vec[3], q2[4], mat[3][3], tmat[4][4], obmat[4][4];
388         
389         VECCOPY(vec, co);
390         Mat4MulVecfl(vdd->pmat, vec);
391         VecSubf(vec, vec, vdd->pmat[3]);
392         VecAddf(vec, vec, vdd->obmat[3]);
393         
394         Mat4CpyMat4(obmat, vdd->obmat);
395         VECCOPY(obmat[3], vec);
396         
397         if(vdd->par->transflag & OB_DUPLIROT) {
398                 if(no_f) {
399                         vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
400                 }
401                 else if(no_s) {
402                         vec[0]= -no_s[0]; vec[1]= -no_s[1]; vec[2]= -no_s[2];
403                 }
404                 
405                 vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag, q2);
406                 
407                 QuatToMat3(q2, mat);
408                 Mat4CpyMat4(tmat, obmat);
409                 Mat4MulMat43(obmat, tmat, mat);
410         }
411         dob= new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index, OB_DUPLIVERTS, vdd->animated);
412         if(vdd->orco)
413                 VECCOPY(dob->orco, vdd->orco[index]);
414         
415         if(vdd->ob->transflag & OB_DUPLI) {
416                 float tmpmat[4][4];
417                 Mat4CpyMat4(tmpmat, vdd->ob->obmat);
418                 Mat4CpyMat4(vdd->ob->obmat, obmat); /* pretend we are really this mat */
419                 object_duplilist_recursive((ID *)vdd->id, vdd->ob, vdd->lb, obmat, vdd->level+1, vdd->animated);
420                 Mat4CpyMat4(vdd->ob->obmat, tmpmat);
421         }
422 }
423
424 static void vertex_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], int level, int animated)
425 {
426         Object *ob, *ob_iter;
427         Mesh *me;
428         Base *base = NULL;
429         float vec[3], no[3], pmat[4][4];
430         int lay, totvert, a, oblay;
431         DerivedMesh *dm;
432         struct vertexDupliData vdd;
433         Scene *sce = NULL;
434         Group *group = NULL;
435         GroupObject * go = NULL;
436         
437         Mat4CpyMat4(pmat, par->obmat);
438         
439         /* simple preventing of too deep nested groups */
440         if(level>MAX_DUPLI_RECUR) return;
441
442         if(par==G.obedit)
443                 dm= editmesh_get_derived_cage(CD_MASK_BAREMESH);
444         else
445                 dm= mesh_get_derived_deform(par, CD_MASK_BAREMESH);
446
447         if(G.rendering) {
448                 me= par->data;
449                 vdd.orco= (float(*)[3])get_mesh_orco_verts(par);
450                 transform_mesh_orco_verts(me, vdd.orco, me->totvert, 0);
451         }
452         else
453                 vdd.orco= NULL;
454         
455         totvert = dm->getNumVerts(dm);
456
457         /* having to loop on scene OR group objects is NOT FUN */
458         if (GS(id->name) == ID_SCE) {
459                 sce = (Scene *)id;
460                 lay= sce->lay;
461                 base= sce->base.first;
462         } else {
463                 group = (Group *)id;
464                 lay= group->layer;
465                 go = group->gobject.first;
466         }
467         
468         /* Start looping on Scene OR Group objects */
469         while (base || go) { 
470                 if (sce) {
471                         ob_iter= base->object;
472                         oblay = base->lay;
473                 } else {
474                         ob_iter= go->ob;
475                         oblay = ob_iter->lay;
476                 }
477                 
478                 if (lay & oblay && G.obedit!=ob_iter) {
479                         ob=ob_iter->parent;
480                         while(ob) {
481                                 if(ob==par) {
482                                         ob = ob_iter;
483         /* End Scene/Group object loop, below is generic */
484                                         
485                                         
486                                         /* par_space_mat - only used for groups so we can modify the space dupli's are in
487                                            when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
488                                         */
489                                         if(par_space_mat)
490                                                 Mat4MulMat4(vdd.obmat, ob->obmat, par_space_mat);
491                                         else
492                                                 Mat4CpyMat4(vdd.obmat, ob->obmat);
493
494                                         vdd.id= id;
495                                         vdd.level= level;
496                                         vdd.animated= animated;
497                                         vdd.lb= lb;
498                                         vdd.ob= ob;
499                                         vdd.par= par;
500                                         Mat4CpyMat4(vdd.pmat, pmat);
501                                         
502                                         /* mballs have a different dupli handling */
503                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
504
505                                         if(par==G.obedit) {
506                                                 dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
507                                         }
508                                         else {
509                                                 for(a=0; a<totvert; a++) {
510                                                         dm->getVertCo(dm, a, vec);
511                                                         dm->getVertNo(dm, a, no);
512                                                         
513                                                         vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
514                                                 }
515                                         }
516                                         
517                                         break;
518                                 }
519                                 ob= ob->parent;
520                         }
521                 }
522                 if (sce)        base= base->next;       /* scene loop */
523                 else            go= go->next;           /* group loop */
524         }
525
526         if(vdd.orco)
527                 MEM_freeN(vdd.orco);
528         dm->release(dm);
529 }
530
531 static void face_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], int level, int animated)
532 {
533         Object *ob, *ob_iter;
534         Base *base = NULL;
535         DupliObject *dob;
536         DerivedMesh *dm;
537         Mesh *me;
538         MTFace *mtface;
539         MFace *mface;
540         MVert *mvert;
541         float pmat[4][4], imat[3][3], (*orco)[3] = NULL, w;
542         int lay, oblay, totface, a;
543         Scene *sce = NULL;
544         Group *group = NULL;
545         GroupObject *go = NULL;
546         float ob__obmat[4][4]; /* needed for groups where the object matrix needs to be modified */
547         
548         /* simple preventing of too deep nested groups */
549         if(level>MAX_DUPLI_RECUR) return;
550         
551         Mat4CpyMat4(pmat, par->obmat);
552
553         if(par==G.obedit) {
554                 int totvert;
555                 dm= editmesh_get_derived_cage(CD_MASK_BAREMESH);
556                 
557                 totface= dm->getNumFaces(dm);
558                 mface= MEM_mallocN(sizeof(MFace)*totface, "mface temp");
559                 dm->copyFaceArray(dm, mface);
560                 totvert= dm->getNumVerts(dm);
561                 mvert= MEM_mallocN(sizeof(MVert)*totvert, "mvert temp");
562                 dm->copyVertArray(dm, mvert);
563         }
564         else {
565                 dm = mesh_get_derived_deform(par, CD_MASK_BAREMESH);
566                 
567                 totface= dm->getNumFaces(dm);
568                 mface= dm->getFaceArray(dm);
569                 mvert= dm->getVertArray(dm);
570         }
571
572         if(G.rendering) {
573                 me= (Mesh*)par->data;
574
575                 orco= (float(*)[3])get_mesh_orco_verts(par);
576                 transform_mesh_orco_verts(me, orco, me->totvert, 0);
577                 mtface= me->mtface;
578         }
579         else {
580                 orco= NULL;
581                 mtface= NULL;
582         }
583         
584         /* having to loop on scene OR group objects is NOT FUN */
585         if (GS(id->name) == ID_SCE) {
586                 sce = (Scene *)id;
587                 lay= sce->lay;
588                 base= sce->base.first;
589         } else {
590                 group = (Group *)id;
591                 lay= group->layer;
592                 go = group->gobject.first;
593         }
594         
595         /* Start looping on Scene OR Group objects */
596         while (base || go) { 
597                 if (sce) {
598                         ob_iter= base->object;
599                         oblay = base->lay;
600                 } else {
601                         ob_iter= go->ob;
602                         oblay = ob_iter->lay;
603                 }
604                 
605                 if (lay & oblay && G.obedit!=ob_iter) {
606                         ob=ob_iter->parent;
607                         while(ob) {
608                                 if(ob==par) {
609                                         ob = ob_iter;
610         /* End Scene/Group object loop, below is generic */
611                                         
612                                         /* par_space_mat - only used for groups so we can modify the space dupli's are in
613                                            when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
614                                         */
615                                         if(par_space_mat)
616                                                 Mat4MulMat4(ob__obmat, ob->obmat, par_space_mat);
617                                         else
618                                                 Mat4CpyMat4(ob__obmat, ob->obmat);
619                                         
620                                         Mat3CpyMat4(imat, ob->parentinv);
621                                                 
622                                         /* mballs have a different dupli handling */
623                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
624
625                                         for(a=0; a<totface; a++) {
626                                                 int mv1 = mface[a].v1;
627                                                 int mv2 = mface[a].v2;
628                                                 int mv3 = mface[a].v3;
629                                                 int mv4 = mface[a].v4;
630                                                 float *v1= mvert[mv1].co;
631                                                 float *v2= mvert[mv2].co;
632                                                 float *v3= mvert[mv3].co;
633                                                 float *v4= (mv4)? mvert[mv4].co: NULL;
634                                                 float cent[3], quat[4], mat[3][3], mat3[3][3], tmat[4][4], obmat[4][4];
635
636                                                 /* translation */
637                                                 if(v4)
638                                                         CalcCent4f(cent, v1, v2, v3, v4);
639                                                 else
640                                                         CalcCent3f(cent, v1, v2, v3);
641                                                 Mat4MulVecfl(pmat, cent);
642                                                 
643                                                 VecSubf(cent, cent, pmat[3]);
644                                                 VecAddf(cent, cent, ob__obmat[3]);
645                                                 
646                                                 Mat4CpyMat4(obmat, ob__obmat);
647                                                 
648                                                 VECCOPY(obmat[3], cent);
649                                                 
650                                                 /* rotation */
651                                                 triatoquat(v1, v2, v3, quat);
652                                                 QuatToMat3(quat, mat);
653                                                 
654                                                 /* scale */
655                                                 if(par->transflag & OB_DUPLIFACES_SCALE) {
656                                                         float size= v4?AreaQ3Dfl(v1, v2, v3, v4):AreaT3Dfl(v1, v2, v3);
657                                                         size= sqrt(size) * par->dupfacesca;
658                                                         Mat3MulFloat(mat[0], size);
659                                                 }
660                                                 
661                                                 Mat3CpyMat3(mat3, mat);
662                                                 Mat3MulMat3(mat, imat, mat3);
663                                                 
664                                                 Mat4CpyMat4(tmat, obmat);
665                                                 Mat4MulMat43(obmat, tmat, mat);
666                                                 
667                                                 dob= new_dupli_object(lb, ob, obmat, lay, a, OB_DUPLIFACES, animated);
668                                                 if(G.rendering) {
669                                                         w= (mv4)? 0.25f: 1.0f/3.0f;
670
671                                                         if(orco) {
672                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv1], w);
673                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv2], w);
674                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv3], w);
675                                                                 if(mv4)
676                                                                         VECADDFAC(dob->orco, dob->orco, orco[mv4], w);
677                                                         }
678
679                                                         if(mtface) {
680                                                                 dob->uv[0] += w*mtface[a].uv[0][0];
681                                                                 dob->uv[1] += w*mtface[a].uv[0][1];
682                                                                 dob->uv[0] += w*mtface[a].uv[1][0];
683                                                                 dob->uv[1] += w*mtface[a].uv[1][1];
684                                                                 dob->uv[0] += w*mtface[a].uv[2][0];
685                                                                 dob->uv[1] += w*mtface[a].uv[2][1];
686
687                                                                 if(mv4) {
688                                                                         dob->uv[0] += w*mtface[a].uv[3][0];
689                                                                         dob->uv[1] += w*mtface[a].uv[3][1];
690                                                                 }
691                                                         }
692                                                 }
693                                                 
694                                                 if(ob->transflag & OB_DUPLI) {
695                                                         float tmpmat[4][4];
696                                                         Mat4CpyMat4(tmpmat, ob->obmat);
697                                                         Mat4CpyMat4(ob->obmat, obmat); /* pretend we are really this mat */
698                                                         object_duplilist_recursive((ID *)id, ob, lb, ob->obmat, level+1, animated);
699                                                         Mat4CpyMat4(ob->obmat, tmpmat);
700                                                 }
701                                         }
702                                         
703                                         break;
704                                 }
705                                 ob= ob->parent;
706                         }
707                 }
708                 if (sce)        base= base->next;       /* scene loop */
709                 else            go= go->next;           /* group loop */
710         }
711         
712         if(par==G.obedit) {
713                 MEM_freeN(mface);
714                 MEM_freeN(mvert);
715         }
716
717         if(orco)
718                 MEM_freeN(orco);
719         
720         dm->release(dm);
721 }
722
723 static void new_particle_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], ParticleSystem *psys, int level, int animated)
724 {
725         GroupObject *go;
726         Object *ob=0, **oblist=0;
727         DupliObject *dob;
728         ParticleSettings *part;
729         ParticleData *pa;
730         ChildParticle *cpa=0;
731         ParticleKey state;
732         ParticleCacheKey *cache;
733         ParticleSystemModifierData *psmd;
734         float ctime, pa_time, scale = 1.0f;
735         float tmat[4][4], mat[4][4], obrotmat[4][4], pamat[4][4], size=0.0;
736         float obmat[4][4], (*obmatlist)[4][4]=0;
737         float xvec[3] = {-1.0, 0.0, 0.0}, q[4];
738         int lay, a, b, k, step_nbr = 0, counter, hair = 0;
739         int totpart, totchild, totgroup=0, pa_num;
740
741         if(psys==0) return;
742         
743         /* simple preventing of too deep nested groups */
744         if(level>MAX_DUPLI_RECUR) return;
745         
746         part=psys->part;
747         psmd= psys_get_modifier(par, psys);
748
749         if(part==0)
750                 return;
751
752         if(!psys_check_enabled(par, psys))
753                 return;
754
755         ctime = bsystem_time(par, (float)G.scene->r.cfra, 0.0);
756
757         totpart = psys->totpart;
758         totchild = psys->totchild;
759
760         BLI_srandom(31415926 + psys->seed);
761                 
762         lay= G.scene->lay;
763         if((part->draw_as == PART_DRAW_OB && part->dup_ob) ||
764                 (part->draw_as == PART_DRAW_GR && part->dup_group && part->dup_group->gobject.first)) {
765
766                 if(psys->flag & (PSYS_HAIR_DONE|PSYS_KEYED) && part->draw & PART_DRAW_KEYS)
767                         step_nbr = part->keys_step;
768                 else
769                         step_nbr = 0;
770
771                 /* if we have a hair particle system, use the path cache */
772                 if(part->type == PART_HAIR) {
773                         if(psys->flag & PSYS_HAIR_DONE)
774                                 hair= (totchild == 0 || psys->childcache) && psys->pathcache;
775                         if(!hair)
776                                 return;
777                 }
778
779                 psys->lattice = psys_get_lattice(par, psys);
780
781                 if(part->draw_as==PART_DRAW_GR) {
782                         group_handle_recalc_and_update(par, part->dup_group);
783
784                         go= part->dup_group->gobject.first;
785                         while(go) {
786                                 go=go->next;
787                                 totgroup++;
788                         }
789
790                         oblist= MEM_callocN(totgroup*sizeof(Object *), "dupgroup object list");
791                         obmatlist= MEM_callocN(totgroup*sizeof(float)*4*4, "dupgroup obmat list");
792                         go= part->dup_group->gobject.first;
793                         for(a=0; a<totgroup; a++, go=go->next) {
794                                 oblist[a]=go->ob;
795                                 Mat4CpyMat4(obmatlist[a], go->ob->obmat);
796                         }
797                 }
798                 else {
799                         ob = part->dup_ob;
800                         Mat4CpyMat4(obmat, ob->obmat);
801                 }
802
803                 if(totchild==0 || part->draw & PART_DRAW_PARENT)
804                         a=0;
805                 else
806                         a=totpart;
807
808                 for(pa=psys->particles,counter=0; a<totpart+totchild; a++,pa++,counter++) {
809                         if(a<totpart) {
810                                 if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP))
811                                         continue;
812
813                                 pa_num=pa->num;
814
815                                 pa_time=pa->time;
816
817                                 size=pa->size;
818                         }
819                         else {
820                                 /* TODO: figure these two out */
821                                 cpa= &psys->child[a - totpart];
822                                 pa_num = a;
823                                 pa_time = psys->particles[cpa->parent].time;
824
825                                 size=psys_get_child_size(psys, cpa, ctime, 0);
826                         }
827
828                         if(part->draw_as==PART_DRAW_GR) {
829                                 if(part->draw&PART_DRAW_RAND_GR)
830                                         b= BLI_rand() % totgroup;
831                                 else if(part->from==PART_FROM_PARTICLE)
832                                         b= pa_num % totgroup;
833                                 else
834                                         b= a % totgroup;
835
836                                 ob = oblist[b];
837                                 Mat4CpyMat4(obmat, obmatlist[b]);
838                         }
839
840                         for(k=0; k<=step_nbr; k++, counter++) {
841                                 if(hair) {
842                                         if(a < totpart) {
843                                                 cache = psys->pathcache[a];
844                                                 psys_get_dupli_path_transform(par, psys, psmd, pa, 0, cache, pamat, &scale);
845                                         }
846                                         else {
847                                                 cache = psys->childcache[a-totpart];
848                                                 psys_get_dupli_path_transform(par, psys, psmd, 0, cpa, cache, pamat, &scale);
849                                         }
850
851                                         VECCOPY(pamat[3], cache->co);
852                                 }
853                                 else if(step_nbr) {
854                                         state.time = (float)k / (float)step_nbr;
855                                         psys_get_particle_on_path(par, psys, a, &state, 0);
856
857                                         QuatToMat4(state.rot, pamat);
858                                         VECCOPY(pamat[3], state.co);
859                                         pamat[3][3]= 1.0f;
860                                 }
861                                 else {
862                                         state.time = -1.0;
863                                         if(psys_get_particle_state(par, psys, a, &state, 0) == 0)
864                                                 continue;
865
866                                         QuatToMat4(state.rot, pamat);
867                                         VECCOPY(pamat[3], state.co);
868                                         pamat[3][3]= 1.0f;
869                                 }
870
871                                 if(part->draw_as==PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
872                                         for(go= part->dup_group->gobject.first, b=0; go; go= go->next, b++) {
873
874                                                 Mat4MulMat4(tmat, obmatlist[b], pamat);
875                                                 Mat4MulFloat3((float *)tmat, size*scale);
876                                                 if(par_space_mat)
877                                                         Mat4MulMat4(mat, tmat, par_space_mat);
878                                                 else
879                                                         Mat4CpyMat4(mat, tmat);
880
881                                                 dob= new_dupli_object(lb, go->ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
882                                                 if(G.rendering)
883                                                         psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
884                                         }
885                                 }
886                                 else {
887                                         /* to give ipos in object correct offset */
888                                         where_is_object_time(ob, ctime-pa_time);
889                                         
890                                         if(!hair) {
891                                                 vectoquat(xvec, ob->trackflag, ob->upflag, q);
892                                                 QuatToMat4(q, obrotmat);
893                                                 obrotmat[3][3]= 1.0f;
894                                                 Mat4MulMat4(mat, obrotmat, pamat);
895                                         }
896                                         else
897                                                 Mat4CpyMat4(mat, pamat);
898
899                                         Mat4MulMat4(tmat, obmat, mat);
900                                         Mat4MulFloat3((float *)tmat, size*scale);
901                                         if(par_space_mat)
902                                                 Mat4MulMat4(mat, tmat, par_space_mat);
903                                         else
904                                                 Mat4CpyMat4(mat, tmat);
905
906                                         dob= new_dupli_object(lb, ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
907                                         if(G.rendering)
908                                                 psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
909                                 }
910                         }
911                 }
912         }
913         if(oblist)
914                 MEM_freeN(oblist);
915         if(obmatlist)
916                 MEM_freeN(obmatlist);
917
918         if(psys->lattice) {
919                 end_latt_deform();
920                 psys->lattice = 0;
921         }
922 }
923
924 static Object *find_family_object(Object **obar, char *family, char ch)
925 {
926         Object *ob;
927         int flen;
928         
929         if( obar[ch] ) return obar[ch];
930         
931         flen= strlen(family);
932         
933         ob= G.main->object.first;
934         while(ob) {
935                 if( ob->id.name[flen+2]==ch ) {
936                         if( strncmp(ob->id.name+2, family, flen)==0 ) break;
937                 }
938                 ob= ob->id.next;
939         }
940         
941         obar[ch]= ob;
942         
943         return ob;
944 }
945
946
947 static void font_duplilist(ListBase *lb, Object *par, int level, int animated)
948 {
949         Object *ob, *obar[256];
950         Curve *cu;
951         struct chartrans *ct, *chartransdata;
952         float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
953         int slen, a;
954         
955         /* simple preventing of too deep nested groups */
956         if(level>MAX_DUPLI_RECUR) return;
957         
958         Mat4CpyMat4(pmat, par->obmat);
959         
960         /* in par the family name is stored, use this to find the other objects */
961         
962         chartransdata= text_to_curve(par, FO_DUPLI);
963         if(chartransdata==0) return;
964         
965         memset(obar, 0, 256*sizeof(void *));
966         
967         cu= par->data;
968         slen= strlen(cu->str);
969         fsize= cu->fsize;
970         xof= cu->xof;
971         yof= cu->yof;
972         
973         ct= chartransdata;
974         
975         for(a=0; a<slen; a++, ct++) {
976                 
977                 ob= find_family_object(obar, cu->family, cu->str[a]);
978                 if(ob) {
979                         vec[0]= fsize*(ct->xof - xof);
980                         vec[1]= fsize*(ct->yof - yof);
981                         vec[2]= 0.0;
982                         
983                         Mat4MulVecfl(pmat, vec);
984                         
985                         Mat4CpyMat4(obmat, par->obmat);
986                         VECCOPY(obmat[3], vec);
987                         
988                         new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIVERTS, animated);
989                 }
990         }
991         
992         MEM_freeN(chartransdata);
993 }
994
995 /* ***************************** */
996 static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated)
997 {       
998         if((ob->transflag & OB_DUPLI)==0)
999                 return;
1000         
1001         /* Should the dupli's be generated for this object? - Respect restrict flags */
1002         if (G.rendering) {
1003                 if (ob->restrictflag & OB_RESTRICT_RENDER) {
1004                         return;
1005                 }
1006         } else {
1007                 if (ob->restrictflag & OB_RESTRICT_VIEW) {
1008                         return;
1009                 }
1010         }
1011
1012         if(ob->transflag & OB_DUPLIPARTS) {
1013                 ParticleSystem *psys = ob->particlesystem.first;
1014                 for(; psys; psys=psys->next)
1015                         new_particle_duplilist(duplilist, id, ob, par_space_mat, psys, level+1, animated);
1016         }
1017         else if(ob->transflag & OB_DUPLIVERTS) {
1018                 if(ob->type==OB_MESH) {
1019                         vertex_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1020                 }
1021                 else if(ob->type==OB_FONT) {
1022                         if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1023                                 font_duplilist(duplilist, ob, level+1, animated);
1024                         }
1025                 }
1026         }
1027         else if(ob->transflag & OB_DUPLIFACES) {
1028                 if(ob->type==OB_MESH)
1029                         face_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1030         }
1031         else if(ob->transflag & OB_DUPLIFRAMES) {
1032                 if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1033                         frames_duplilist(duplilist, ob, level+1, animated);
1034                 }
1035         } else if(ob->transflag & OB_DUPLIGROUP) {
1036                 DupliObject *dob;
1037                 
1038                 group_duplilist(duplilist, ob, level+1, animated); /* now recursive */
1039
1040                 if (level==0) {
1041                         for(dob= duplilist->first; dob; dob= dob->next)
1042                                 if(dob->type == OB_DUPLIGROUP)
1043                                         Mat4CpyMat4(dob->ob->obmat, dob->mat);
1044                 }
1045         }
1046 }
1047
1048 /* note; group dupli's already set transform matrix. see note in group_duplilist() */
1049 ListBase *object_duplilist(Scene *sce, Object *ob)
1050 {
1051         ListBase *duplilist= MEM_mallocN(sizeof(ListBase), "duplilist");
1052         duplilist->first= duplilist->last= NULL;
1053         object_duplilist_recursive((ID *)sce, ob, duplilist, NULL, 0, 0);
1054         return duplilist;
1055 }
1056
1057 void free_object_duplilist(ListBase *lb)
1058 {
1059         DupliObject *dob;
1060         
1061         for(dob= lb->first; dob; dob= dob->next) {
1062                 dob->ob->lay= dob->origlay;
1063                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
1064         }
1065         
1066         BLI_freelistN(lb);
1067         MEM_freeN(lb);
1068 }
1069
1070 int count_duplilist(Object *ob)
1071 {
1072         if(ob->transflag & OB_DUPLI) {
1073                 if(ob->transflag & OB_DUPLIVERTS) {
1074                         if(ob->type==OB_MESH) {
1075                                 if(ob->transflag & OB_DUPLIVERTS) {
1076                                         ParticleSystem *psys = ob->particlesystem.first;
1077                                         int pdup=0;
1078
1079                                         for(; psys; psys=psys->next)
1080                                                 pdup += psys->totpart;
1081
1082                                         if(pdup==0){
1083                                                 Mesh *me= ob->data;
1084                                                 return me->totvert;
1085                                         }
1086                                         else
1087                                                 return pdup;
1088                                 }
1089                         }
1090                 }
1091                 else if(ob->transflag & OB_DUPLIFRAMES) {
1092                         int tot= ob->dupend - ob->dupsta; 
1093                         tot/= (ob->dupon+ob->dupoff);
1094                         return tot*ob->dupon;
1095                 }
1096         }
1097         return 1;
1098 }