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