Bugfix for use of unitinialized values when drawing hair dupli particles.
[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_mesh.h"
69 #include "BKE_object.h"
70 #include "BKE_particle.h"
71 #include "BKE_utildefines.h"
72
73 #include "BKE_bad_level_calls.h"
74
75 #ifdef HAVE_CONFIG_H
76 #include <config.h>
77 #endif
78
79 static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated);
80
81 void free_path(Path *path)
82 {
83         if(path->data) MEM_freeN(path->data);
84         MEM_freeN(path);
85 }
86
87
88 void calc_curvepath(Object *ob)
89 {
90         BevList *bl;
91         BevPoint *bevp, *bevpn, *bevpfirst, *bevplast, *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, int animated)
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         dob->animated= (type == OB_DUPLIGROUP) && animated;
299         ob->lay= lay;
300         
301         return dob;
302 }
303
304 static void group_duplilist(ListBase *lb, Object *ob, int level, int animated)
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         animated= animated || group_is_animated(ob, group);
321         
322         for(go= group->gobject.first; go; go= go->next) {
323                 /* note, if you check on layer here, render goes wrong... it still deforms verts and uses parent imat */
324                 if(go->ob!=ob) {
325                         
326                         Mat4MulMat4(mat, go->ob->obmat, ob->obmat);
327                         dob= new_dupli_object(lb, go->ob, mat, ob->lay, 0, OB_DUPLIGROUP, animated);
328                         dob->no_draw= (dob->origlay & group->layer)==0;
329                         
330                         if(go->ob->transflag & OB_DUPLI) {
331                                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
332                                 object_duplilist_recursive((ID *)group, go->ob, lb, ob->obmat, level+1, animated);
333                                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
334                         }
335                 }
336         }
337 }
338
339 static void frames_duplilist(ListBase *lb, Object *ob, int level, int animated)
340 {
341         extern int enable_cu_speed;     /* object.c */
342         Object copyob;
343         int cfrao, ok;
344         
345         /* simple preventing of too deep nested groups */
346         if(level>MAX_DUPLI_RECUR) return;
347         
348         cfrao= G.scene->r.cfra;
349         if(ob->parent==NULL && ob->track==NULL && ob->ipo==NULL && ob->constraints.first==NULL) return;
350
351         if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
352         copyob= *ob;    /* store transform info */
353
354         for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
355
356                 ok= 1;
357                 if(ob->dupoff) {
358                         ok= G.scene->r.cfra - ob->dupsta;
359                         ok= ok % (ob->dupon+ob->dupoff);
360                         if(ok < ob->dupon) ok= 1;
361                         else ok= 0;
362                 }
363                 if(ok) {
364                         do_ob_ipo(ob);
365                         where_is_object_time(ob, (float)G.scene->r.cfra);
366                         new_dupli_object(lb, ob, ob->obmat, ob->lay, G.scene->r.cfra, OB_DUPLIFRAMES, animated);
367                 }
368         }
369
370         *ob= copyob;    /* restore transform info */
371         G.scene->r.cfra= cfrao;
372         enable_cu_speed= 1;
373 }
374
375 struct vertexDupliData {
376         ID *id; /* scene or group, for recursive loops */
377         int level;
378         int animated;
379         ListBase *lb;
380         float pmat[4][4];
381         float obmat[4][4]; /* Only used for dupliverts inside dupligroups, where the ob->obmat is modified */
382         Object *ob, *par;
383         float (*orco)[3];
384 };
385
386 static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
387 {
388         DupliObject *dob;
389         struct vertexDupliData *vdd= userData;
390         float vec[3], *q2, mat[3][3], tmat[4][4], obmat[4][4];
391         
392         VECCOPY(vec, co);
393         Mat4MulVecfl(vdd->pmat, vec);
394         VecSubf(vec, vec, vdd->pmat[3]);
395         VecAddf(vec, vec, vdd->obmat[3]);
396         
397         Mat4CpyMat4(obmat, vdd->obmat);
398         VECCOPY(obmat[3], vec);
399         
400         if(vdd->par->transflag & OB_DUPLIROT) {
401                 if(no_f) {
402                         vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
403                 }
404                 else if(no_s) {
405                         vec[0]= -no_s[0]; vec[1]= -no_s[1]; vec[2]= -no_s[2];
406                 }
407                 
408                 q2= vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag);
409                 
410                 QuatToMat3(q2, mat);
411                 Mat4CpyMat4(tmat, obmat);
412                 Mat4MulMat43(obmat, tmat, mat);
413         }
414         dob= new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index, OB_DUPLIVERTS, vdd->animated);
415         if(vdd->orco)
416                 VECCOPY(dob->orco, vdd->orco[index]);
417         
418         if(vdd->ob->transflag & OB_DUPLI) {
419                 float tmpmat[4][4];
420                 Mat4CpyMat4(tmpmat, vdd->ob->obmat);
421                 Mat4CpyMat4(vdd->ob->obmat, obmat); /* pretend we are really this mat */
422                 object_duplilist_recursive((ID *)vdd->id, vdd->ob, vdd->lb, obmat, vdd->level+1, vdd->animated);
423                 Mat4CpyMat4(vdd->ob->obmat, tmpmat);
424         }
425 }
426
427 static void vertex_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], int level, int animated)
428 {
429         Object *ob, *ob_iter;
430         Mesh *me;
431         Base *base = NULL;
432         float vec[3], no[3], pmat[4][4];
433         int lay, totvert, a, oblay;
434         DerivedMesh *dm;
435         struct vertexDupliData vdd;
436         Scene *sce = NULL;
437         Group *group = NULL;
438         GroupObject * go = NULL;
439         
440         Mat4CpyMat4(pmat, par->obmat);
441         
442         /* simple preventing of too deep nested groups */
443         if(level>MAX_DUPLI_RECUR) return;
444
445         if(par==G.obedit)
446                 dm= editmesh_get_derived_cage(CD_MASK_BAREMESH);
447         else
448                 dm= mesh_get_derived_deform(par, CD_MASK_BAREMESH);
449
450         if(G.rendering) {
451                 me= par->data;
452                 vdd.orco= (float(*)[3])get_mesh_orco_verts(par);
453                 transform_mesh_orco_verts(me, vdd.orco, me->totvert, 0);
454         }
455         else
456                 vdd.orco= NULL;
457         
458         totvert = dm->getNumVerts(dm);
459
460         /* having to loop on scene OR group objects is NOT FUN */
461         if (GS(id->name) == ID_SCE) {
462                 sce = (Scene *)id;
463                 lay= sce->lay;
464                 base= sce->base.first;
465         } else {
466                 group = (Group *)id;
467                 lay= group->layer;
468                 go = group->gobject.first;
469         }
470         
471         /* Start looping on Scene OR Group objects */
472         while (base || go) { 
473                 if (sce) {
474                         ob_iter= base->object;
475                         oblay = base->lay;
476                 } else {
477                         ob_iter= go->ob;
478                         oblay = ob_iter->lay;
479                 }
480                 
481                 if (lay & oblay && G.obedit!=ob_iter) {
482                         ob=ob_iter->parent;
483                         while(ob) {
484                                 if(ob==par) {
485                                         ob = ob_iter;
486         /* End Scene/Group object loop, below is generic */
487                                         
488                                         
489                                         /* par_space_mat - only used for groups so we can modify the space dupli's are in
490                                            when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
491                                         */
492                                         if(par_space_mat)
493                                                 Mat4MulMat4(vdd.obmat, ob->obmat, par_space_mat);
494                                         else
495                                                 Mat4CpyMat4(vdd.obmat, ob->obmat);
496
497                                         vdd.id= id;
498                                         vdd.level= level;
499                                         vdd.animated= animated;
500                                         vdd.lb= lb;
501                                         vdd.ob= ob;
502                                         vdd.par= par;
503                                         Mat4CpyMat4(vdd.pmat, pmat);
504                                         
505                                         /* mballs have a different dupli handling */
506                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
507
508                                         if(par==G.obedit) {
509                                                 dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
510                                         }
511                                         else {
512                                                 for(a=0; a<totvert; a++) {
513                                                         dm->getVertCo(dm, a, vec);
514                                                         dm->getVertNo(dm, a, no);
515                                                         
516                                                         vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
517                                                 }
518                                         }
519                                         
520                                         break;
521                                 }
522                                 ob= ob->parent;
523                         }
524                 }
525                 if (sce)        base= base->next;       /* scene loop */
526                 else            go= go->next;           /* group loop */
527         }
528
529         if(vdd.orco)
530                 MEM_freeN(vdd.orco);
531         dm->release(dm);
532 }
533
534 static void face_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], int level, int animated)
535 {
536         Object *ob, *ob_iter;
537         Base *base = NULL;
538         DupliObject *dob;
539         DerivedMesh *dm;
540         Mesh *me;
541         MTFace *mtface;
542         MFace *mface;
543         MVert *mvert;
544         float pmat[4][4], imat[3][3], (*orco)[3] = NULL, w;
545         int lay, oblay, totface, a;
546         Scene *sce = NULL;
547         Group *group = NULL;
548         GroupObject *go = NULL;
549         float ob__obmat[4][4]; /* needed for groups where the object matrix needs to be modified */
550         
551         /* simple preventing of too deep nested groups */
552         if(level>MAX_DUPLI_RECUR) return;
553         
554         Mat4CpyMat4(pmat, par->obmat);
555
556         if(par==G.obedit) {
557                 int totvert;
558                 dm= editmesh_get_derived_cage(CD_MASK_BAREMESH);
559                 
560                 totface= dm->getNumFaces(dm);
561                 mface= MEM_mallocN(sizeof(MFace)*totface, "mface temp");
562                 dm->copyFaceArray(dm, mface);
563                 totvert= dm->getNumVerts(dm);
564                 mvert= MEM_mallocN(sizeof(MVert)*totvert, "mvert temp");
565                 dm->copyVertArray(dm, mvert);
566         }
567         else {
568                 dm = mesh_get_derived_deform(par, CD_MASK_BAREMESH);
569                 
570                 totface= dm->getNumFaces(dm);
571                 mface= dm->getFaceArray(dm);
572                 mvert= dm->getVertArray(dm);
573         }
574
575         if(G.rendering) {
576                 me= (Mesh*)par->data;
577
578                 orco= (float(*)[3])get_mesh_orco_verts(par);
579                 transform_mesh_orco_verts(me, orco, me->totvert, 0);
580                 mtface= me->mtface;
581         }
582         else {
583                 orco= NULL;
584                 mtface= NULL;
585         }
586         
587         /* having to loop on scene OR group objects is NOT FUN */
588         if (GS(id->name) == ID_SCE) {
589                 sce = (Scene *)id;
590                 lay= sce->lay;
591                 base= sce->base.first;
592         } else {
593                 group = (Group *)id;
594                 lay= group->layer;
595                 go = group->gobject.first;
596         }
597         
598         /* Start looping on Scene OR Group objects */
599         while (base || go) { 
600                 if (sce) {
601                         ob_iter= base->object;
602                         oblay = base->lay;
603                 } else {
604                         ob_iter= go->ob;
605                         oblay = ob_iter->lay;
606                 }
607                 
608                 if (lay & oblay && G.obedit!=ob_iter) {
609                         ob=ob_iter->parent;
610                         while(ob) {
611                                 if(ob==par) {
612                                         ob = ob_iter;
613         /* End Scene/Group object loop, below is generic */
614                                         
615                                         /* par_space_mat - only used for groups so we can modify the space dupli's are in
616                                            when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
617                                         */
618                                         if(par_space_mat)
619                                                 Mat4MulMat4(ob__obmat, ob->obmat, par_space_mat);
620                                         else
621                                                 Mat4CpyMat4(ob__obmat, ob->obmat);
622                                         
623                                         Mat3CpyMat4(imat, ob->parentinv);
624                                                 
625                                         /* mballs have a different dupli handling */
626                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
627
628                                         for(a=0; a<totface; a++) {
629                                                 int mv1 = mface[a].v1;
630                                                 int mv2 = mface[a].v2;
631                                                 int mv3 = mface[a].v3;
632                                                 int mv4 = mface[a].v4;
633                                                 float *v1= mvert[mv1].co;
634                                                 float *v2= mvert[mv2].co;
635                                                 float *v3= mvert[mv3].co;
636                                                 float *v4= (mv4)? mvert[mv4].co: NULL;
637                                                 float cent[3], quat[4], mat[3][3], mat3[3][3], tmat[4][4], obmat[4][4];
638
639                                                 /* translation */
640                                                 if(v4)
641                                                         CalcCent4f(cent, v1, v2, v3, v4);
642                                                 else
643                                                         CalcCent3f(cent, v1, v2, v3);
644                                                 Mat4MulVecfl(pmat, cent);
645                                                 
646                                                 VecSubf(cent, cent, pmat[3]);
647                                                 VecAddf(cent, cent, ob__obmat[3]);
648                                                 
649                                                 Mat4CpyMat4(obmat, ob__obmat);
650                                                 
651                                                 VECCOPY(obmat[3], cent);
652                                                 
653                                                 /* rotation */
654                                                 triatoquat(v1, v2, v3, quat);
655                                                 QuatToMat3(quat, mat);
656                                                 
657                                                 /* scale */
658                                                 if(par->transflag & OB_DUPLIFACES_SCALE) {
659                                                         float size= v4?AreaQ3Dfl(v1, v2, v3, v4):AreaT3Dfl(v1, v2, v3);
660                                                         size= sqrt(size) * par->dupfacesca;
661                                                         Mat3MulFloat(mat[0], size);
662                                                 }
663                                                 
664                                                 Mat3CpyMat3(mat3, mat);
665                                                 Mat3MulMat3(mat, imat, mat3);
666                                                 
667                                                 Mat4CpyMat4(tmat, obmat);
668                                                 Mat4MulMat43(obmat, tmat, mat);
669                                                 
670                                                 dob= new_dupli_object(lb, ob, obmat, lay, a, OB_DUPLIFACES, animated);
671                                                 if(G.rendering) {
672                                                         w= (mv4)? 0.25f: 1.0f/3.0f;
673
674                                                         if(orco) {
675                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv1], w);
676                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv2], w);
677                                                                 VECADDFAC(dob->orco, dob->orco, orco[mv3], w);
678                                                                 if(mv4)
679                                                                         VECADDFAC(dob->orco, dob->orco, orco[mv4], w);
680                                                         }
681
682                                                         if(mtface) {
683                                                                 dob->uv[0] += w*mtface[a].uv[0][0];
684                                                                 dob->uv[1] += w*mtface[a].uv[0][1];
685                                                                 dob->uv[0] += w*mtface[a].uv[1][0];
686                                                                 dob->uv[1] += w*mtface[a].uv[1][1];
687                                                                 dob->uv[0] += w*mtface[a].uv[2][0];
688                                                                 dob->uv[1] += w*mtface[a].uv[2][1];
689
690                                                                 if(mv4) {
691                                                                         dob->uv[0] += w*mtface[a].uv[3][0];
692                                                                         dob->uv[1] += w*mtface[a].uv[3][1];
693                                                                 }
694                                                         }
695                                                 }
696                                                 
697                                                 if(ob->transflag & OB_DUPLI) {
698                                                         float tmpmat[4][4];
699                                                         Mat4CpyMat4(tmpmat, ob->obmat);
700                                                         Mat4CpyMat4(ob->obmat, obmat); /* pretend we are really this mat */
701                                                         object_duplilist_recursive((ID *)id, ob, lb, ob->obmat, level+1, animated);
702                                                         Mat4CpyMat4(ob->obmat, tmpmat);
703                                                 }
704                                         }
705                                         
706                                         break;
707                                 }
708                                 ob= ob->parent;
709                         }
710                 }
711                 if (sce)        base= base->next;       /* scene loop */
712                 else            go= go->next;           /* group loop */
713         }
714         
715         if(par==G.obedit) {
716                 MEM_freeN(mface);
717                 MEM_freeN(mvert);
718         }
719
720         if(orco)
721                 MEM_freeN(orco);
722         
723         dm->release(dm);
724 }
725
726 static void new_particle_duplilist(ListBase *lb, ID *id, Object *par, float par_space_mat[][4], ParticleSystem *psys, int level, int animated)
727 {
728         GroupObject *go;
729         Object *ob=0, **oblist=0;
730         DupliObject *dob;
731         ParticleSettings *part;
732         ParticleData *pa;
733         ChildParticle *cpa=0;
734         ParticleKey state;
735         ParticleCacheKey *cache;
736         ParticleSystemModifierData *psmd;
737         float ctime, pa_time, scale = 1.0f;
738         float tmat[4][4], mat[4][4], obrotmat[4][4], pamat[4][4], size=0.0;
739         float obmat[4][4], (*obmatlist)[4][4]=0;
740         float xvec[3] = {-1.0, 0.0, 0.0}, *q;
741         int lay, a, b, k, step_nbr = 0, counter, hair = 0;
742         int totpart, totchild, totgroup=0, pa_num;
743
744         if(psys==0) return;
745         
746         /* simple preventing of too deep nested groups */
747         if(level>MAX_DUPLI_RECUR) return;
748         
749         part=psys->part;
750         psmd= psys_get_modifier(par, psys);
751
752         if(part==0) return;
753
754         ctime = bsystem_time(par, (float)G.scene->r.cfra, 0.0);
755
756         totpart = psys->totpart;
757         totchild = psys->totchild;
758
759         BLI_srandom(31415926 + psys->seed);
760                 
761         lay= G.scene->lay;
762         if((part->draw_as == PART_DRAW_OB && part->dup_ob) ||
763                 (part->draw_as == PART_DRAW_GR && part->dup_group && part->dup_group->gobject.first)) {
764
765                 if(psys->flag & (PSYS_HAIR_DONE|PSYS_KEYED) && part->draw & PART_DRAW_KEYS)
766                         step_nbr = part->keys_step;
767                 else
768                         step_nbr = 0;
769
770                 /* if we have a hair particle system, use the path cache */
771                 if(part->type == PART_HAIR) {
772                         if(psys->flag & PSYS_HAIR_DONE)
773                                 hair= (totchild == 0 || psys->childcache) && psys->pathcache;
774                         if(!hair)
775                                 return;
776                 }
777
778                 psys->lattice = psys_get_lattice(par, psys);
779
780                 if(part->draw_as==PART_DRAW_GR) {
781                         group_handle_recalc_and_update(par, part->dup_group);
782
783                         go= part->dup_group->gobject.first;
784                         while(go) {
785                                 go=go->next;
786                                 totgroup++;
787                         }
788
789                         oblist= MEM_callocN(totgroup*sizeof(Object *), "dupgroup object list");
790                         obmatlist= MEM_callocN(totgroup*sizeof(float)*4*4, "dupgroup obmat list");
791                         go= part->dup_group->gobject.first;
792                         for(a=0; a<totgroup; a++, go=go->next) {
793                                 oblist[a]=go->ob;
794                                 Mat4CpyMat4(obmatlist[a], go->ob->obmat);
795                         }
796                 }
797                 else {
798                         ob = part->dup_ob;
799                         Mat4CpyMat4(obmat, ob->obmat);
800                 }
801
802                 if(totchild==0 || part->draw & PART_DRAW_PARENT)
803                         a=0;
804                 else
805                         a=totpart;
806
807                 for(pa=psys->particles,counter=0; a<totpart+totchild; a++,pa++,counter++) {
808                         if(a<totpart) {
809                                 if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP))
810                                         continue;
811
812                                 pa_num=pa->num;
813
814                                 pa_time=pa->time;
815
816                                 size=pa->size;
817                         }
818                         else {
819                                 /* TODO: figure these two out */
820                                 cpa= &psys->child[a - totpart];
821                                 pa_num = a;
822                                 pa_time = psys->particles[cpa->parent].time;
823
824                                 size=psys_get_child_size(psys, cpa, ctime, 0);
825                         }
826
827                         if(part->draw_as==PART_DRAW_GR) {
828                                 if(part->draw&PART_DRAW_RAND_GR)
829                                         b= BLI_rand() % totgroup;
830                                 else if(part->from==PART_FROM_PARTICLE)
831                                         b= pa_num % totgroup;
832                                 else
833                                         b= a % totgroup;
834
835                                 ob = oblist[b];
836                                 Mat4CpyMat4(obmat, obmatlist[b]);
837                         }
838
839                         for(k=0; k<=step_nbr; k++, counter++) {
840                                 if(hair) {
841                                         if(a < totpart) {
842                                                 cache = psys->pathcache[a];
843                                                 psys_get_dupli_path_transform(par, part, psmd, pa, 0, cache, pamat, &scale);
844                                         }
845                                         else {
846                                                 cache = psys->childcache[a-totpart];
847                                                 psys_get_dupli_path_transform(par, part, psmd, 0, cpa, cache, pamat, &scale);
848                                         }
849
850                                         VECCOPY(pamat[3], cache->co);
851                                 }
852                                 else if(step_nbr) {
853                                         state.time = (float)k / (float)step_nbr;
854                                         psys_get_particle_on_path(par, psys, a, &state, 0);
855
856                                         QuatToMat4(state.rot, pamat);
857                                         VECCOPY(pamat[3], state.co);
858                                         pamat[3][3]= 1.0f;
859                                 }
860                                 else {
861                                         state.time = -1.0;
862                                         if(psys_get_particle_state(par, psys, a, &state, 0) == 0)
863                                                 continue;
864
865                                         QuatToMat4(state.rot, pamat);
866                                         VECCOPY(pamat[3], state.co);
867                                         pamat[3][3]= 1.0f;
868                                 }
869
870                                 if(part->draw_as==PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
871                                         for(go= part->dup_group->gobject.first, b=0; go; go= go->next, b++) {
872
873                                                 Mat4MulMat4(tmat, obmatlist[b], pamat);
874                                                 Mat4MulFloat3((float *)tmat, size*scale);
875                                                 if(par_space_mat)
876                                                         Mat4MulMat4(mat, tmat, par_space_mat);
877                                                 else
878                                                         Mat4CpyMat4(mat, tmat);
879
880                                                 dob= new_dupli_object(lb, go->ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
881                                                 if(G.rendering)
882                                                         psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
883                                         }
884                                 }
885                                 else {
886                                         /* to give ipos in object correct offset */
887                                         where_is_object_time(ob, ctime-pa_time);
888                                         
889                                         if(!hair) {
890                                                 q = vectoquat(xvec, ob->trackflag, ob->upflag);
891                                                 QuatToMat4(q, obrotmat);
892                                                 obrotmat[3][3]= 1.0f;
893
894                                                 Mat4MulMat4(mat, obrotmat, pamat);
895                                         }
896                                         else
897                                                 Mat4CpyMat4(mat, pamat);
898
899                                         Mat4MulMat4(tmat, obmat, mat);
900                                         Mat4MulFloat3((float *)tmat, size*scale);
901                                         if(par_space_mat)
902                                                 Mat4MulMat4(mat, tmat, par_space_mat);
903                                         else
904                                                 Mat4CpyMat4(mat, tmat);
905
906                                         dob= new_dupli_object(lb, ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
907                                         if(G.rendering)
908                                                 psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
909                                 }
910                         }
911                 }
912         }
913         if(oblist)
914                 MEM_freeN(oblist);
915         if(obmatlist)
916                 MEM_freeN(obmatlist);
917
918         if(psys->lattice) {
919                 end_latt_deform();
920                 psys->lattice = 0;
921         }
922 }
923
924 static Object *find_family_object(Object **obar, char *family, char ch)
925 {
926         Object *ob;
927         int flen;
928         
929         if( obar[ch] ) return obar[ch];
930         
931         flen= strlen(family);
932         
933         ob= G.main->object.first;
934         while(ob) {
935                 if( ob->id.name[flen+2]==ch ) {
936                         if( strncmp(ob->id.name+2, family, flen)==0 ) break;
937                 }
938                 ob= ob->id.next;
939         }
940         
941         obar[ch]= ob;
942         
943         return ob;
944 }
945
946
947 static void font_duplilist(ListBase *lb, Object *par, int level, int animated)
948 {
949         Object *ob, *obar[256];
950         Curve *cu;
951         struct chartrans *ct, *chartransdata;
952         float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
953         int slen, a;
954         
955         /* simple preventing of too deep nested groups */
956         if(level>MAX_DUPLI_RECUR) return;
957         
958         Mat4CpyMat4(pmat, par->obmat);
959         
960         /* in par the family name is stored, use this to find the other objects */
961         
962         chartransdata= text_to_curve(par, FO_DUPLI);
963         if(chartransdata==0) return;
964         
965         memset(obar, 0, 256*sizeof(void *));
966         
967         cu= par->data;
968         slen= strlen(cu->str);
969         fsize= cu->fsize;
970         xof= cu->xof;
971         yof= cu->yof;
972         
973         ct= chartransdata;
974         
975         for(a=0; a<slen; a++, ct++) {
976                 
977                 ob= find_family_object(obar, cu->family, cu->str[a]);
978                 if(ob) {
979                         vec[0]= fsize*(ct->xof - xof);
980                         vec[1]= fsize*(ct->yof - yof);
981                         vec[2]= 0.0;
982                         
983                         Mat4MulVecfl(pmat, vec);
984                         
985                         Mat4CpyMat4(obmat, par->obmat);
986                         VECCOPY(obmat[3], vec);
987                         
988                         new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIVERTS, animated);
989                 }
990         }
991         
992         MEM_freeN(chartransdata);
993 }
994
995 /* ***************************** */
996 static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated)
997 {       
998         if((ob->transflag & OB_DUPLI)==0)
999                 return;
1000         
1001         /* Should the dupli's be generated for this object? - Respect restrict flags */
1002         if (G.rendering) {
1003                 if (ob->restrictflag & OB_RESTRICT_RENDER) {
1004                         return;
1005                 }
1006         } else {
1007                 if (ob->restrictflag & OB_RESTRICT_VIEW) {
1008                         return;
1009                 }
1010         }
1011
1012         if(ob->transflag & OB_DUPLIPARTS) {
1013                 ParticleSystem *psys = ob->particlesystem.first;
1014                 for(; psys; psys=psys->next)
1015                         new_particle_duplilist(duplilist, id, ob, par_space_mat, psys, level+1, animated);
1016         }
1017         else if(ob->transflag & OB_DUPLIVERTS) {
1018                 if(ob->type==OB_MESH) {
1019                         vertex_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1020                 }
1021                 else if(ob->type==OB_FONT) {
1022                         if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1023                                 font_duplilist(duplilist, ob, level+1, animated);
1024                         }
1025                 }
1026         }
1027         else if(ob->transflag & OB_DUPLIFACES) {
1028                 if(ob->type==OB_MESH)
1029                         face_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1030         }
1031         else if(ob->transflag & OB_DUPLIFRAMES) {
1032                 if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1033                         frames_duplilist(duplilist, ob, level+1, animated);
1034                 }
1035         } else if(ob->transflag & OB_DUPLIGROUP) {
1036                 DupliObject *dob;
1037                 
1038                 group_duplilist(duplilist, ob, level+1, animated); /* now recursive */
1039
1040                 if (level==0) {
1041                         for(dob= duplilist->first; dob; dob= dob->next)
1042                                 if(dob->type == OB_DUPLIGROUP)
1043                                         Mat4CpyMat4(dob->ob->obmat, dob->mat);
1044                 }
1045         }
1046 }
1047
1048 /* note; group dupli's already set transform matrix. see note in group_duplilist() */
1049 ListBase *object_duplilist(Scene *sce, Object *ob)
1050 {
1051         ListBase *duplilist= MEM_mallocN(sizeof(ListBase), "duplilist");
1052         duplilist->first= duplilist->last= NULL;
1053         object_duplilist_recursive((ID *)sce, ob, duplilist, NULL, 0, 0);
1054         return duplilist;
1055 }
1056
1057 void free_object_duplilist(ListBase *lb)
1058 {
1059         DupliObject *dob;
1060         
1061         for(dob= lb->first; dob; dob= dob->next) {
1062                 dob->ob->lay= dob->origlay;
1063                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
1064         }
1065         
1066         BLI_freelistN(lb);
1067         MEM_freeN(lb);
1068 }
1069
1070 int count_duplilist(Object *ob)
1071 {
1072         if(ob->transflag & OB_DUPLI) {
1073                 if(ob->transflag & OB_DUPLIVERTS) {
1074                         if(ob->type==OB_MESH) {
1075                                 if(ob->transflag & OB_DUPLIVERTS) {
1076                                         ParticleSystem *psys = ob->particlesystem.first;
1077                                         int pdup=0;
1078
1079                                         for(; psys; psys=psys->next)
1080                                                 pdup += psys->totpart;
1081
1082                                         if(pdup==0){
1083                                                 Mesh *me= ob->data;
1084                                                 return me->totvert;
1085                                         }
1086                                         else
1087                                                 return pdup;
1088                                 }
1089                         }
1090                 }
1091                 else if(ob->transflag & OB_DUPLIFRAMES) {
1092                         int tot= ob->dupend - ob->dupsta; 
1093                         tot/= (ob->dupon+ob->dupoff);
1094                         return tot*ob->dupon;
1095                 }
1096         }
1097         return 1;
1098 }