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