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