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