Fix for bug #10894: objects used as particle duplis couldn't be
[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         DupliObject *dob;
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                         dob= new_dupli_object(lb, ob, ob->obmat, ob->lay, G.scene->r.cfra, OB_DUPLIFRAMES, animated);
365                         Mat4CpyMat4(dob->omat, copyob.obmat);
366                 }
367         }
368
369         *ob= copyob;    /* restore transform info */
370         G.scene->r.cfra= cfrao;
371         enable_cu_speed= 1;
372 }
373
374 struct vertexDupliData {
375         ID *id; /* scene or group, for recursive loops */
376         int level;
377         int animated;
378         ListBase *lb;
379         float pmat[4][4];
380         float obmat[4][4]; /* Only used for dupliverts inside dupligroups, where the ob->obmat is modified */
381         Object *ob, *par;
382         float (*orco)[3];
383 };
384
385 static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
386 {
387         DupliObject *dob;
388         struct vertexDupliData *vdd= userData;
389         float vec[3], q2[4], mat[3][3], tmat[4][4], obmat[4][4];
390         
391         VECCOPY(vec, co);
392         Mat4MulVecfl(vdd->pmat, vec);
393         VecSubf(vec, vec, vdd->pmat[3]);
394         VecAddf(vec, vec, vdd->obmat[3]);
395         
396         Mat4CpyMat4(obmat, vdd->obmat);
397         VECCOPY(obmat[3], vec);
398         
399         if(vdd->par->transflag & OB_DUPLIROT) {
400                 if(no_f) {
401                         vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
402                 }
403                 else if(no_s) {
404                         vec[0]= -no_s[0]; vec[1]= -no_s[1]; vec[2]= -no_s[2];
405                 }
406                 
407                 vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag, q2);
408                 
409                 QuatToMat3(q2, mat);
410                 Mat4CpyMat4(tmat, obmat);
411                 Mat4MulMat43(obmat, tmat, mat);
412         }
413         dob= new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index, OB_DUPLIVERTS, vdd->animated);
414         if(vdd->orco)
415                 VECCOPY(dob->orco, vdd->orco[index]);
416         
417         if(vdd->ob->transflag & OB_DUPLI) {
418                 float tmpmat[4][4];
419                 Mat4CpyMat4(tmpmat, vdd->ob->obmat);
420                 Mat4CpyMat4(vdd->ob->obmat, obmat); /* pretend we are really this mat */
421                 object_duplilist_recursive((ID *)vdd->id, vdd->ob, vdd->lb, obmat, vdd->level+1, vdd->animated);
422                 Mat4CpyMat4(vdd->ob->obmat, tmpmat);
423         }
424 }
425
426 static void vertex_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], int level, int animated)
427 {
428         Object *ob, *ob_iter;
429         Mesh *me;
430         Base *base = NULL;
431         float vec[3], no[3], pmat[4][4];
432         int lay, totvert, a, oblay;
433         DerivedMesh *dm;
434         struct vertexDupliData vdd;
435         Scene *sce = NULL;
436         Group *group = NULL;
437         GroupObject * go = NULL;
438         
439         Mat4CpyMat4(pmat, par->obmat);
440         
441         /* simple preventing of too deep nested groups */
442         if(level>MAX_DUPLI_RECUR) return;
443
444         if(par==G.obedit)
445                 dm= editmesh_get_derived_cage(CD_MASK_BAREMESH);
446         else
447                 dm= mesh_get_derived_deform(par, CD_MASK_BAREMESH);
448
449         if(G.rendering) {
450                 me= par->data;
451                 vdd.orco= (float(*)[3])get_mesh_orco_verts(par);
452                 transform_mesh_orco_verts(me, vdd.orco, me->totvert, 0);
453         }
454         else
455                 vdd.orco= NULL;
456         
457         totvert = dm->getNumVerts(dm);
458
459         /* having to loop on scene OR group objects is NOT FUN */
460         if (GS(id->name) == ID_SCE) {
461                 sce = (Scene *)id;
462                 lay= sce->lay;
463                 base= sce->base.first;
464         } else {
465                 group = (Group *)id;
466                 lay= group->layer;
467                 go = group->gobject.first;
468         }
469         
470         /* Start looping on Scene OR Group objects */
471         while (base || go) { 
472                 if (sce) {
473                         ob_iter= base->object;
474                         oblay = base->lay;
475                 } else {
476                         ob_iter= go->ob;
477                         oblay = ob_iter->lay;
478                 }
479                 
480                 if (lay & oblay && G.obedit!=ob_iter) {
481                         ob=ob_iter->parent;
482                         while(ob) {
483                                 if(ob==par) {
484                                         ob = ob_iter;
485         /* End Scene/Group object loop, below is generic */
486                                         
487                                         
488                                         /* par_space_mat - only used for groups so we can modify the space dupli's are in
489                                            when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
490                                         */
491                                         if(par_space_mat)
492                                                 Mat4MulMat4(vdd.obmat, ob->obmat, par_space_mat);
493                                         else
494                                                 Mat4CpyMat4(vdd.obmat, ob->obmat);
495
496                                         vdd.id= id;
497                                         vdd.level= level;
498                                         vdd.animated= animated;
499                                         vdd.lb= lb;
500                                         vdd.ob= ob;
501                                         vdd.par= par;
502                                         Mat4CpyMat4(vdd.pmat, pmat);
503                                         
504                                         /* mballs have a different dupli handling */
505                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
506
507                                         if(par==G.obedit) {
508                                                 dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
509                                         }
510                                         else {
511                                                 for(a=0; a<totvert; a++) {
512                                                         dm->getVertCo(dm, a, vec);
513                                                         dm->getVertNo(dm, a, no);
514                                                         
515                                                         vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
516                                                 }
517                                         }
518                                         
519                                         break;
520                                 }
521                                 ob= ob->parent;
522                         }
523                 }
524                 if (sce)        base= base->next;       /* scene loop */
525                 else            go= go->next;           /* group loop */
526         }
527
528         if(vdd.orco)
529                 MEM_freeN(vdd.orco);
530         dm->release(dm);
531 }
532
533 static void face_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], int level, int animated)
534 {
535         Object *ob, *ob_iter;
536         Base *base = NULL;
537         DupliObject *dob;
538         DerivedMesh *dm;
539         Mesh *me;
540         MTFace *mtface;
541         MFace *mface;
542         MVert *mvert;
543         float pmat[4][4], imat[3][3], (*orco)[3] = NULL, w;
544         int lay, oblay, totface, a;
545         Scene *sce = NULL;
546         Group *group = NULL;
547         GroupObject *go = NULL;
548         float ob__obmat[4][4]; /* needed for groups where the object matrix needs to be modified */
549         
550         /* simple preventing of too deep nested groups */
551         if(level>MAX_DUPLI_RECUR) return;
552         
553         Mat4CpyMat4(pmat, par->obmat);
554
555         if(par==G.obedit) {
556                 int totvert;
557                 dm= editmesh_get_derived_cage(CD_MASK_BAREMESH);
558                 
559                 totface= dm->getNumFaces(dm);
560                 mface= MEM_mallocN(sizeof(MFace)*totface, "mface temp");
561                 dm->copyFaceArray(dm, mface);
562                 totvert= dm->getNumVerts(dm);
563                 mvert= MEM_mallocN(sizeof(MVert)*totvert, "mvert temp");
564                 dm->copyVertArray(dm, mvert);
565         }
566         else {
567                 dm = mesh_get_derived_deform(par, CD_MASK_BAREMESH);
568                 
569                 totface= dm->getNumFaces(dm);
570                 mface= dm->getFaceArray(dm);
571                 mvert= dm->getVertArray(dm);
572         }
573
574         if(G.rendering) {
575                 me= (Mesh*)par->data;
576
577                 orco= (float(*)[3])get_mesh_orco_verts(par);
578                 transform_mesh_orco_verts(me, orco, me->totvert, 0);
579                 mtface= me->mtface;
580         }
581         else {
582                 orco= NULL;
583                 mtface= NULL;
584         }
585         
586         /* having to loop on scene OR group objects is NOT FUN */
587         if (GS(id->name) == ID_SCE) {
588                 sce = (Scene *)id;
589                 lay= sce->lay;
590                 base= sce->base.first;
591         } else {
592                 group = (Group *)id;
593                 lay= group->layer;
594                 go = group->gobject.first;
595         }
596         
597         /* Start looping on Scene OR Group objects */
598         while (base || go) { 
599                 if (sce) {
600                         ob_iter= base->object;
601                         oblay = base->lay;
602                 } else {
603                         ob_iter= go->ob;
604                         oblay = ob_iter->lay;
605                 }
606                 
607                 if (lay & oblay && G.obedit!=ob_iter) {
608                         ob=ob_iter->parent;
609                         while(ob) {
610                                 if(ob==par) {
611                                         ob = ob_iter;
612         /* End Scene/Group object loop, below is generic */
613                                         
614                                         /* par_space_mat - only used for groups so we can modify the space dupli's are in
615                                            when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
616                                         */
617                                         if(par_space_mat)
618                                                 Mat4MulMat4(ob__obmat, ob->obmat, par_space_mat);
619                                         else
620                                                 Mat4CpyMat4(ob__obmat, ob->obmat);
621                                         
622                                         Mat3CpyMat4(imat, ob->parentinv);
623                                                 
624                                         /* mballs have a different dupli handling */
625                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
626
627                                         for(a=0; a<totface; a++) {
628                                                 int mv1 = mface[a].v1;
629                                                 int mv2 = mface[a].v2;
630                                                 int mv3 = mface[a].v3;
631                                                 int mv4 = mface[a].v4;
632                                                 float *v1= mvert[mv1].co;
633                                                 float *v2= mvert[mv2].co;
634                                                 float *v3= mvert[mv3].co;
635                                                 float *v4= (mv4)? mvert[mv4].co: NULL;
636                                                 float cent[3], quat[4], mat[3][3], mat3[3][3], tmat[4][4], obmat[4][4];
637
638                                                 /* translation */
639                                                 if(v4)
640                                                         CalcCent4f(cent, v1, v2, v3, v4);
641                                                 else
642                                                         CalcCent3f(cent, v1, v2, v3);
643                                                 Mat4MulVecfl(pmat, cent);
644                                                 
645                                                 VecSubf(cent, cent, pmat[3]);
646                                                 VecAddf(cent, cent, ob__obmat[3]);
647                                                 
648                                                 Mat4CpyMat4(obmat, ob__obmat);
649                                                 
650                                                 VECCOPY(obmat[3], cent);
651                                                 
652                                                 /* rotation */
653                                                 triatoquat(v1, v2, v3, quat);
654                                                 QuatToMat3(quat, mat);
655                                                 
656                                                 /* scale */
657                                                 if(par->transflag & OB_DUPLIFACES_SCALE) {
658                                                         float size= v4?AreaQ3Dfl(v1, v2, v3, v4):AreaT3Dfl(v1, v2, v3);
659                                                         size= sqrt(size) * par->dupfacesca;
660                                                         Mat3MulFloat(mat[0], size);
661                                                 }
662                                                 
663                                                 Mat3CpyMat3(mat3, mat);
664                                                 Mat3MulMat3(mat, imat, mat3);
665                                                 
666                                                 Mat4CpyMat4(tmat, obmat);
667                                                 Mat4MulMat43(obmat, tmat, mat);
668                                                 
669                                                 dob= new_dupli_object(lb, ob, obmat, lay, a, OB_DUPLIFACES, animated);
670                                                 if(G.rendering) {
671                                                         w= (mv4)? 0.25f: 1.0f/3.0f;
672
673                                                         if(orco) {
674                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv1], w);
675                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv2], w);
676                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv3], w);
677                                                                 if(mv4)
678                                                                         VECADDFAC(dob->orco, dob->orco, orco[mv4], w);
679                                                         }
680
681                                                         if(mtface) {
682                                                                 dob->uv[0] += w*mtface[a].uv[0][0];
683                                                                 dob->uv[1] += w*mtface[a].uv[0][1];
684                                                                 dob->uv[0] += w*mtface[a].uv[1][0];
685                                                                 dob->uv[1] += w*mtface[a].uv[1][1];
686                                                                 dob->uv[0] += w*mtface[a].uv[2][0];
687                                                                 dob->uv[1] += w*mtface[a].uv[2][1];
688
689                                                                 if(mv4) {
690                                                                         dob->uv[0] += w*mtface[a].uv[3][0];
691                                                                         dob->uv[1] += w*mtface[a].uv[3][1];
692                                                                 }
693                                                         }
694                                                 }
695                                                 
696                                                 if(ob->transflag & OB_DUPLI) {
697                                                         float tmpmat[4][4];
698                                                         Mat4CpyMat4(tmpmat, ob->obmat);
699                                                         Mat4CpyMat4(ob->obmat, obmat); /* pretend we are really this mat */
700                                                         object_duplilist_recursive((ID *)id, ob, lb, ob->obmat, level+1, animated);
701                                                         Mat4CpyMat4(ob->obmat, tmpmat);
702                                                 }
703                                         }
704                                         
705                                         break;
706                                 }
707                                 ob= ob->parent;
708                         }
709                 }
710                 if (sce)        base= base->next;       /* scene loop */
711                 else            go= go->next;           /* group loop */
712         }
713         
714         if(par==G.obedit) {
715                 MEM_freeN(mface);
716                 MEM_freeN(mvert);
717         }
718
719         if(orco)
720                 MEM_freeN(orco);
721         
722         dm->release(dm);
723 }
724
725 static void new_particle_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], ParticleSystem *psys, int level, int animated)
726 {
727         GroupObject *go;
728         Object *ob=0, **oblist=0, obcopy, *obcopylist=0;
729         DupliObject *dob;
730         ParticleSettings *part;
731         ParticleData *pa;
732         ChildParticle *cpa=0;
733         ParticleKey state;
734         ParticleCacheKey *cache;
735         ParticleSystemModifierData *psmd;
736         float ctime, pa_time, scale = 1.0f;
737         float tmat[4][4], mat[4][4], obrotmat[4][4], pamat[4][4], size=0.0;
738         float (*obmat)[4], (*oldobmat)[4];
739         float xvec[3] = {-1.0, 0.0, 0.0}, q[4];
740         int lay, a, b, k, step_nbr = 0, counter, hair = 0;
741         int totpart, totchild, totgroup=0, pa_num;
742
743         if(psys==0) return;
744         
745         /* simple preventing of too deep nested groups */
746         if(level>MAX_DUPLI_RECUR) return;
747         
748         part=psys->part;
749         psmd= psys_get_modifier(par, psys);
750
751         if(part==0)
752                 return;
753
754         if(!psys_check_enabled(par, psys))
755                 return;
756
757         ctime = bsystem_time(par, (float)G.scene->r.cfra, 0.0);
758
759         totpart = psys->totpart;
760         totchild = psys->totchild;
761
762         BLI_srandom(31415926 + psys->seed);
763         
764         lay= G.scene->lay;
765         if((part->draw_as == PART_DRAW_OB && part->dup_ob) ||
766                 (part->draw_as == PART_DRAW_GR && part->dup_group && part->dup_group->gobject.first)) {
767
768                 if(psys->flag & (PSYS_HAIR_DONE|PSYS_KEYED) && part->draw & PART_DRAW_KEYS)
769                         step_nbr = part->keys_step;
770                 else
771                         step_nbr = 0;
772
773                 /* if we have a hair particle system, use the path cache */
774                 if(part->type == PART_HAIR) {
775                         if(psys->flag & PSYS_HAIR_DONE)
776                                 hair= (totchild == 0 || psys->childcache) && psys->pathcache;
777                         if(!hair)
778                                 return;
779                 }
780
781                 psys->lattice = psys_get_lattice(par, psys);
782
783                 /* gather list of objects or single object */
784                 if(part->draw_as==PART_DRAW_GR) {
785                         group_handle_recalc_and_update(par, part->dup_group);
786
787                         for(go=part->dup_group->gobject.first; go; go=go->next)
788                                 totgroup++;
789
790                         /* we also copy the actual objects to restore afterwards, since
791                          * where_is_object_time will change the object which breaks transform */
792                         oblist = MEM_callocN(totgroup*sizeof(Object *), "dupgroup object list");
793                         obcopylist = MEM_callocN(totgroup*sizeof(Object), "dupgroup copy list");
794
795                         go = part->dup_group->gobject.first;
796                         for(a=0; a<totgroup; a++, go=go->next) {
797                                 oblist[a] = go->ob;
798                                 obcopylist[a] = *go->ob;
799                         }
800                 }
801                 else {
802                         ob = part->dup_ob;
803                         obcopy = *ob;
804                 }
805
806                 if(totchild==0 || part->draw & PART_DRAW_PARENT)
807                         a = 0;
808                 else
809                         a = totpart;
810
811                 for(pa=psys->particles,counter=0; a<totpart+totchild; a++,pa++,counter++) {
812                         if(a<totpart) {
813                                 /* handle parent particle */
814                                 if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP))
815                                         continue;
816
817                                 pa_num = pa->num;
818                                 pa_time = pa->time;
819                                 size = pa->size;
820                         }
821                         else {
822                                 /* handle child particle */
823                                 cpa = &psys->child[a - totpart];
824
825                                 pa_num = a;
826                                 pa_time = psys->particles[cpa->parent].time;
827                                 size = psys_get_child_size(psys, cpa, ctime, 0);
828                         }
829
830                         if(part->draw_as==PART_DRAW_GR) {
831                                 /* for groups, pick the object based on settings */
832                                 if(part->draw&PART_DRAW_RAND_GR)
833                                         b= BLI_rand() % totgroup;
834                                 else if(part->from==PART_FROM_PARTICLE)
835                                         b= pa_num % totgroup;
836                                 else
837                                         b= a % totgroup;
838
839                                 ob = oblist[b];
840                                 obmat = oblist[b]->obmat;
841                                 oldobmat = obcopylist[b].obmat;
842                         }
843                         else {
844                                 obmat= ob->obmat;
845                                 oldobmat= obcopy.obmat;
846                         }
847
848                         for(k=0; k<=step_nbr; k++, counter++) {
849                                 if(hair) {
850                                         /* hair we handle separate and compute transform based on hair keys */
851                                         if(a < totpart) {
852                                                 cache = psys->pathcache[a];
853                                                 psys_get_dupli_path_transform(par, psys, psmd, pa, 0, cache, pamat, &scale);
854                                         }
855                                         else {
856                                                 cache = psys->childcache[a-totpart];
857                                                 psys_get_dupli_path_transform(par, psys, psmd, 0, cpa, cache, pamat, &scale);
858                                         }
859
860                                         VECCOPY(pamat[3], cache->co);
861                                 }
862                                 else if(step_nbr) {
863                                         /* other keys */
864                                         state.time = (float)k / (float)step_nbr;
865                                         psys_get_particle_on_path(par, psys, a, &state, 0);
866
867                                         QuatToMat4(state.rot, pamat);
868                                         VECCOPY(pamat[3], state.co);
869                                         pamat[3][3]= 1.0f;
870                                 }
871                                 else {
872                                         /* first key */
873                                         state.time = -1.0;
874                                         if(psys_get_particle_state(par, psys, a, &state, 0) == 0)
875                                                 continue;
876
877                                         QuatToMat4(state.rot, pamat);
878                                         VECCOPY(pamat[3], state.co);
879                                         pamat[3][3]= 1.0f;
880                                 }
881
882                                 if(part->draw_as==PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
883                                         for(go= part->dup_group->gobject.first, b=0; go; go= go->next, b++) {
884
885                                                 Mat4MulMat4(tmat, obmat, pamat);
886                                                 Mat4MulFloat3((float *)tmat, size*scale);
887                                                 if(par_space_mat)
888                                                         Mat4MulMat4(mat, tmat, par_space_mat);
889                                                 else
890                                                         Mat4CpyMat4(mat, tmat);
891
892                                                 dob= new_dupli_object(lb, go->ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
893                                                 Mat4CpyMat4(dob->omat, oldobmat);
894                                                 if(G.rendering)
895                                                         psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
896                                         }
897                                 }
898                                 else {
899                                         /* to give ipos in object correct offset */
900                                         where_is_object_time(ob, ctime-pa_time);
901                                         
902                                         if(!hair) {
903                                                 vectoquat(xvec, ob->trackflag, ob->upflag, q);
904                                                 QuatToMat4(q, obrotmat);
905                                                 obrotmat[3][3]= 1.0f;
906                                                 Mat4MulMat4(mat, obrotmat, pamat);
907                                         }
908                                         else
909                                                 Mat4CpyMat4(mat, pamat);
910
911                                         Mat4MulMat4(tmat, obmat, mat);
912                                         Mat4MulFloat3((float *)tmat, size*scale);
913                                         if(par_space_mat)
914                                                 Mat4MulMat4(mat, tmat, par_space_mat);
915                                         else
916                                                 Mat4CpyMat4(mat, tmat);
917
918                                         dob= new_dupli_object(lb, ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
919                                         Mat4CpyMat4(dob->omat, oldobmat);
920                                         if(G.rendering)
921                                                 psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
922                                 }
923                         }
924                 }
925
926                 /* restore objects since they were changed in where_is_object_time */
927                 if(part->draw_as==PART_DRAW_GR) {
928                         for(a=0; a<totgroup; a++)
929                                 *(oblist[a])= obcopylist[a];
930                 }
931                 else
932                         *ob= obcopy;
933         }
934
935         /* clean up */
936         if(oblist)
937                 MEM_freeN(oblist);
938         if(obcopylist)
939                 MEM_freeN(obcopylist);
940
941         if(psys->lattice) {
942                 end_latt_deform();
943                 psys->lattice = 0;
944         }
945 }
946
947 static Object *find_family_object(Object **obar, char *family, char ch)
948 {
949         Object *ob;
950         int flen;
951         
952         if( obar[ch] ) return obar[ch];
953         
954         flen= strlen(family);
955         
956         ob= G.main->object.first;
957         while(ob) {
958                 if( ob->id.name[flen+2]==ch ) {
959                         if( strncmp(ob->id.name+2, family, flen)==0 ) break;
960                 }
961                 ob= ob->id.next;
962         }
963         
964         obar[ch]= ob;
965         
966         return ob;
967 }
968
969
970 static void font_duplilist(ListBase *lb, Object *par, int level, int animated)
971 {
972         Object *ob, *obar[256];
973         Curve *cu;
974         struct chartrans *ct, *chartransdata;
975         float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
976         int slen, a;
977         
978         /* simple preventing of too deep nested groups */
979         if(level>MAX_DUPLI_RECUR) return;
980         
981         Mat4CpyMat4(pmat, par->obmat);
982         
983         /* in par the family name is stored, use this to find the other objects */
984         
985         chartransdata= text_to_curve(par, FO_DUPLI);
986         if(chartransdata==0) return;
987         
988         memset(obar, 0, 256*sizeof(void *));
989         
990         cu= par->data;
991         slen= strlen(cu->str);
992         fsize= cu->fsize;
993         xof= cu->xof;
994         yof= cu->yof;
995         
996         ct= chartransdata;
997         
998         for(a=0; a<slen; a++, ct++) {
999                 
1000                 ob= find_family_object(obar, cu->family, cu->str[a]);
1001                 if(ob) {
1002                         vec[0]= fsize*(ct->xof - xof);
1003                         vec[1]= fsize*(ct->yof - yof);
1004                         vec[2]= 0.0;
1005                         
1006                         Mat4MulVecfl(pmat, vec);
1007                         
1008                         Mat4CpyMat4(obmat, par->obmat);
1009                         VECCOPY(obmat[3], vec);
1010                         
1011                         new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIVERTS, animated);
1012                 }
1013         }
1014         
1015         MEM_freeN(chartransdata);
1016 }
1017
1018 /* ***************************** */
1019 static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated)
1020 {       
1021         if((ob->transflag & OB_DUPLI)==0)
1022                 return;
1023         
1024         /* Should the dupli's be generated for this object? - Respect restrict flags */
1025         if (G.rendering) {
1026                 if (ob->restrictflag & OB_RESTRICT_RENDER) {
1027                         return;
1028                 }
1029         } else {
1030                 if (ob->restrictflag & OB_RESTRICT_VIEW) {
1031                         return;
1032                 }
1033         }
1034
1035         if(ob->transflag & OB_DUPLIPARTS) {
1036                 ParticleSystem *psys = ob->particlesystem.first;
1037                 for(; psys; psys=psys->next)
1038                         new_particle_duplilist(duplilist, id, ob, par_space_mat, psys, level+1, animated);
1039         }
1040         else if(ob->transflag & OB_DUPLIVERTS) {
1041                 if(ob->type==OB_MESH) {
1042                         vertex_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1043                 }
1044                 else if(ob->type==OB_FONT) {
1045                         if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1046                                 font_duplilist(duplilist, ob, level+1, animated);
1047                         }
1048                 }
1049         }
1050         else if(ob->transflag & OB_DUPLIFACES) {
1051                 if(ob->type==OB_MESH)
1052                         face_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1053         }
1054         else if(ob->transflag & OB_DUPLIFRAMES) {
1055                 if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1056                         frames_duplilist(duplilist, ob, level+1, animated);
1057                 }
1058         } else if(ob->transflag & OB_DUPLIGROUP) {
1059                 DupliObject *dob;
1060                 
1061                 group_duplilist(duplilist, ob, level+1, animated); /* now recursive */
1062
1063                 if (level==0) {
1064                         for(dob= duplilist->first; dob; dob= dob->next)
1065                                 if(dob->type == OB_DUPLIGROUP)
1066                                         Mat4CpyMat4(dob->ob->obmat, dob->mat);
1067                 }
1068         }
1069 }
1070
1071 /* note; group dupli's already set transform matrix. see note in group_duplilist() */
1072 ListBase *object_duplilist(Scene *sce, Object *ob)
1073 {
1074         ListBase *duplilist= MEM_mallocN(sizeof(ListBase), "duplilist");
1075         duplilist->first= duplilist->last= NULL;
1076         object_duplilist_recursive((ID *)sce, ob, duplilist, NULL, 0, 0);
1077         return duplilist;
1078 }
1079
1080 void free_object_duplilist(ListBase *lb)
1081 {
1082         DupliObject *dob;
1083         
1084         for(dob= lb->first; dob; dob= dob->next) {
1085                 dob->ob->lay= dob->origlay;
1086                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
1087         }
1088         
1089         BLI_freelistN(lb);
1090         MEM_freeN(lb);
1091 }
1092
1093 int count_duplilist(Object *ob)
1094 {
1095         if(ob->transflag & OB_DUPLI) {
1096                 if(ob->transflag & OB_DUPLIVERTS) {
1097                         if(ob->type==OB_MESH) {
1098                                 if(ob->transflag & OB_DUPLIVERTS) {
1099                                         ParticleSystem *psys = ob->particlesystem.first;
1100                                         int pdup=0;
1101
1102                                         for(; psys; psys=psys->next)
1103                                                 pdup += psys->totpart;
1104
1105                                         if(pdup==0){
1106                                                 Mesh *me= ob->data;
1107                                                 return me->totvert;
1108                                         }
1109                                         else
1110                                                 return pdup;
1111                                 }
1112                         }
1113                 }
1114                 else if(ob->transflag & OB_DUPLIFRAMES) {
1115                         int tot= ob->dupend - ob->dupsta; 
1116                         tot/= (ob->dupon+ob->dupoff);
1117                         return tot*ob->dupon;
1118                 }
1119         }
1120         return 1;
1121 }