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