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