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