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