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