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