Curve cleanup
[blender-staging.git] / source / blender / blenkernel / intern / anim.c
1 /** anim.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <math.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_arithb.h"
38 #include "BLI_rand.h"
39 #include "DNA_listBase.h"
40
41 #include "DNA_curve_types.h"
42 #include "DNA_effect_types.h"
43 #include "DNA_group_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_modifier_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_vfont_types.h"
53
54 #include "BKE_anim.h"
55 #include "BKE_curve.h"
56 #include "BKE_DerivedMesh.h"
57 #include "BKE_displist.h"
58 #include "BKE_effect.h"
59 #include "BKE_font.h"
60 #include "BKE_group.h"
61 #include "BKE_global.h"
62 #include "BKE_ipo.h"
63 #include "BKE_key.h"
64 #include "BKE_lattice.h"
65 #include "BKE_main.h"
66 #include "BKE_mesh.h"
67 #include "BKE_object.h"
68 #include "BKE_particle.h"
69 #include "BKE_utildefines.h"
70
71 #include "BKE_bad_level_calls.h"
72
73 #ifdef HAVE_CONFIG_H
74 #include <config.h>
75 #endif
76
77 static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated);
78
79 void free_path(Path *path)
80 {
81         if(path->data) MEM_freeN(path->data);
82         MEM_freeN(path);
83 }
84
85
86 void calc_curvepath(Object *ob)
87 {
88         BevList *bl;
89         BevPoint *bevp, *bevpn, *bevpfirst, *bevplast;
90         Curve *cu;
91         Nurb *nu;
92         Path *path;
93         float *fp, *dist, *maxdist, xyz[3];
94         float fac, d=0, fac1, fac2;
95         int a, tot, cycl=0;
96         float *ft;
97         
98         /* in a path vertices are with equal differences: path->len = number of verts */
99         /* NOW WITH BEVELCURVE!!! */
100         
101         if(ob==NULL || ob->type != OB_CURVE) return;
102         cu= ob->data;
103         if(ob==G.obedit) nu= editNurb.first;
104         else nu= cu->nurb.first;
105         
106         if(cu->path) free_path(cu->path);
107         cu->path= NULL;
108         
109         bl= cu->bev.first;
110         if(bl==NULL || !bl->nr) return;
111
112         cu->path=path= MEM_callocN(sizeof(Path), "path");
113         
114         /* if POLY: last vertice != first vertice */
115         cycl= (bl->poly!= -1);
116         
117         if(cycl) tot= bl->nr;
118         else tot= bl->nr-1;
119         
120         path->len= tot+1;
121         /* exception: vector handle paths and polygon paths should be subdivided at least a factor resolu */
122         if(path->len<nu->resolu*SEGMENTSU(nu)) path->len= nu->resolu*SEGMENTSU(nu);
123         
124         dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
125
126                 /* all lengths in *dist */
127         bevp= bevpfirst= (BevPoint *)(bl+1);
128         fp= dist;
129         *fp= 0;
130         for(a=0; a<tot; a++) {
131                 fp++;
132                 if(cycl && a==tot-1)
133                         VecSubf(xyz, bevpfirst->vec, bevp->vec);
134                 else
135                         VecSubf(xyz, (bevp+1)->vec, bevp->vec);
136                 
137                 *fp= *(fp-1)+VecLength(xyz);
138                 bevp++;
139         }
140         
141         path->totdist= *fp;
142
143                 /* the path verts  in path->data */
144                 /* now also with TILT value */
145         ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
146         
147         bevp= bevpfirst;
148         bevpn= bevp+1;
149         bevplast= bevpfirst + (bl->nr-1);
150         fp= dist+1;
151         maxdist= dist+tot;
152         fac= 1.0f/((float)path->len-1.0f);
153         fac = fac * path->totdist;
154
155         for(a=0; a<path->len; a++) {
156                 
157                 d= ((float)a)*fac;
158                 
159                 /* we're looking for location (distance) 'd' in the array */
160                 while((d>= *fp) && fp<maxdist) {
161                         fp++;
162                         if(bevp<bevplast) bevp++;
163                         bevpn= bevp+1;
164                         if(bevpn>bevplast) {
165                                 if(cycl) bevpn= bevpfirst;
166                                 else bevpn= bevplast;
167                         }
168                 }
169                 
170                 fac1= *(fp)- *(fp-1);
171                 fac2= *(fp)-d;
172                 fac1= fac2/fac1;
173                 fac2= 1.0f-fac1;
174                 
175                 VecLerpf(ft, bevp->vec, bevpn->vec, fac2);
176                 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
177                 
178                 ft+= 4;
179         }
180         
181         MEM_freeN(dist);
182 }
183
184 int interval_test(int min, int max, int p1, int cycl)
185 {
186         
187         if(cycl) {
188                 if( p1 < min) 
189                         p1=  ((p1 -min) % (max-min+1)) + max+1;
190                 else if(p1 > max)
191                         p1=  ((p1 -min) % (max-min+1)) + min;
192         }
193         else {
194                 if(p1 < min) p1= min;
195                 else if(p1 > max) p1= max;
196         }
197         return p1;
198 }
199
200 /* warning, *vec needs FOUR items! */
201 /* ctime is normalized range <0-1> */
202 int where_on_path(Object *ob, float ctime, float *vec, float *dir)      /* returns OK */
203 {
204         Curve *cu;
205         Nurb *nu;
206         BevList *bl;
207         Path *path;
208         float *fp, *p0, *p1, *p2, *p3, fac;
209         float data[4];
210         int cycl=0, s0, s1, s2, s3;
211
212         if(ob==NULL || ob->type != OB_CURVE) return 0;
213         cu= ob->data;
214         if(cu->path==NULL || cu->path->data==NULL) {
215                 printf("no path!\n");
216                 return 0;
217         }
218         path= cu->path;
219         fp= path->data;
220         
221         /* test for cyclic */
222         bl= cu->bev.first;
223         if (!bl) return 0;
224         if (!bl->nr) return 0;
225         if(bl->poly> -1) cycl= 1;
226
227         ctime *= (path->len-1);
228         
229         s1= (int)floor(ctime);
230         fac= (float)(s1+1)-ctime;
231
232         /* path->len is corected for cyclic */
233         s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
234         s1= interval_test(0, path->len-1-cycl, s1, cycl);
235         s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
236         s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
237
238         p0= fp + 4*s0;
239         p1= fp + 4*s1;
240         p2= fp + 4*s2;
241         p3= fp + 4*s3;
242
243         /* note, commented out for follow constraint */
244         //if(cu->flag & CU_FOLLOW) {
245                 
246                 key_curve_tangent_weights(1.0f-fac, data, KEY_BSPLINE);
247                 
248                 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
249                 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
250                 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
251                 
252                 /* make compatible with vectoquat */
253                 dir[0]= -dir[0];
254                 dir[1]= -dir[1];
255                 dir[2]= -dir[2];
256         //}
257         
258         nu= cu->nurb.first;
259
260         /* make sure that first and last frame are included in the vectors here  */
261         if((nu->type & 7)==CU_POLY) key_curve_position_weights(1.0f-fac, data, KEY_LINEAR);
262         else if((nu->type & 7)==CU_BEZIER) key_curve_position_weights(1.0f-fac, data, KEY_LINEAR);
263         else if(s0==s1 || p2==p3) key_curve_position_weights(1.0f-fac, data, KEY_CARDINAL);
264         else key_curve_position_weights(1.0f-fac, data, KEY_BSPLINE);
265
266         vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
267         vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
268         vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
269
270         vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
271
272         return 1;
273 }
274
275 /* ****************** DUPLICATOR ************** */
276
277 static DupliObject *new_dupli_object(ListBase *lb, Object *ob, float mat[][4], int lay, int index, int type, int animated)
278 {
279         DupliObject *dob= MEM_callocN(sizeof(DupliObject), "dupliobject");
280         
281         BLI_addtail(lb, dob);
282         dob->ob= ob;
283         Mat4CpyMat4(dob->mat, mat);
284         Mat4CpyMat4(dob->omat, ob->obmat);
285         dob->origlay= ob->lay;
286         dob->index= index;
287         dob->type= type;
288         dob->animated= (type == OB_DUPLIGROUP) && animated;
289         ob->lay= lay;
290         
291         return dob;
292 }
293
294 static void group_duplilist(ListBase *lb, Object *ob, int level, int animated)
295 {
296         DupliObject *dob;
297         Group *group;
298         GroupObject *go;
299         float mat[4][4], tmat[4][4];
300         
301         if(ob->dup_group==NULL) return;
302         group= ob->dup_group;
303         
304         /* simple preventing of too deep nested groups */
305         if(level>MAX_DUPLI_RECUR) return;
306         
307         /* handles animated groups, and */
308         /* we need to check update for objects that are not in scene... */
309         group_handle_recalc_and_update(ob, group);
310         animated= animated || group_is_animated(ob, group);
311         
312         for(go= group->gobject.first; go; go= go->next) {
313                 /* note, if you check on layer here, render goes wrong... it still deforms verts and uses parent imat */
314                 if(go->ob!=ob) {
315                         
316                         /* Group Dupli Offset, should apply after everything else */
317                         if (group->dupli_ofs[0] || group->dupli_ofs[1] || group->dupli_ofs[2]) {
318                                 Mat4CpyMat4(tmat, go->ob->obmat);
319                                 VecSubf(tmat[3], tmat[3], group->dupli_ofs);
320                                 Mat4MulMat4(mat, tmat, ob->obmat);
321                         } else {
322                                 Mat4MulMat4(mat, go->ob->obmat, ob->obmat);
323                         }
324                         
325                         dob= new_dupli_object(lb, go->ob, mat, ob->lay, 0, OB_DUPLIGROUP, animated);
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, animated);
331                                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
332                         }
333                 }
334         }
335 }
336
337 static void frames_duplilist(ListBase *lb, Object *ob, int level, int animated)
338 {
339         extern int enable_cu_speed;     /* object.c */
340         Object copyob;
341         DupliObject *dob;
342         int cfrao, ok;
343         
344         /* simple preventing of too deep nested groups */
345         if(level>MAX_DUPLI_RECUR) return;
346         
347         cfrao= G.scene->r.cfra;
348         if(ob->parent==NULL && ob->track==NULL && ob->ipo==NULL && ob->constraints.first==NULL) return;
349
350         if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
351         copyob= *ob;    /* store transform info */
352
353         for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
354
355                 ok= 1;
356                 if(ob->dupoff) {
357                         ok= G.scene->r.cfra - ob->dupsta;
358                         ok= ok % (ob->dupon+ob->dupoff);
359                         if(ok < ob->dupon) ok= 1;
360                         else ok= 0;
361                 }
362                 if(ok) {
363                         do_ob_ipo(ob);
364                         where_is_object_time(ob, (float)G.scene->r.cfra);
365                         dob= new_dupli_object(lb, ob, ob->obmat, ob->lay, G.scene->r.cfra, OB_DUPLIFRAMES, animated);
366                         Mat4CpyMat4(dob->omat, copyob.obmat);
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[4], 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                 vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag, q2);
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, obcopy, *obcopylist=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], pamat[4][4], size=0.0;
739         float (*obmat)[4], (*oldobmat)[4];
740         int lay, a, b, counter, hair = 0;
741         int totpart, totchild, totgroup=0, pa_num;
742
743         if(psys==0) return;
744         
745         /* simple preventing of too deep nested groups */
746         if(level>MAX_DUPLI_RECUR) return;
747         
748         part=psys->part;
749         psmd= psys_get_modifier(par, psys);
750
751         if(part==0)
752                 return;
753
754         if(!psys_check_enabled(par, psys))
755                 return;
756
757         ctime = bsystem_time(par, (float)G.scene->r.cfra, 0.0);
758
759         totpart = psys->totpart;
760         totchild = psys->totchild;
761
762         BLI_srandom(31415926 + psys->seed);
763         
764         lay= G.scene->lay;
765         if((part->draw_as == PART_DRAW_OB && part->dup_ob) ||
766                 (part->draw_as == PART_DRAW_GR && part->dup_group && part->dup_group->gobject.first)) {
767
768                 /* if we have a hair particle system, use the path cache */
769                 if(part->type == PART_HAIR) {
770                         if(psys->flag & PSYS_HAIR_DONE)
771                                 hair= (totchild == 0 || psys->childcache) && psys->pathcache;
772                         if(!hair)
773                                 return;
774                         
775                         /* we use cache, update totchild according to cached data */
776                         totchild = psys->totchildcache;
777                         totpart = psys->totcached;
778                 }
779
780                 psys->lattice = psys_get_lattice(par, psys);
781
782                 /* gather list of objects or single object */
783                 if(part->draw_as==PART_DRAW_GR) {
784                         group_handle_recalc_and_update(par, part->dup_group);
785
786                         for(go=part->dup_group->gobject.first; go; go=go->next)
787                                 totgroup++;
788
789                         /* we also copy the actual objects to restore afterwards, since
790                          * where_is_object_time will change the object which breaks transform */
791                         oblist = MEM_callocN(totgroup*sizeof(Object *), "dupgroup object list");
792                         obcopylist = MEM_callocN(totgroup*sizeof(Object), "dupgroup copy list");
793
794                         go = part->dup_group->gobject.first;
795                         for(a=0; a<totgroup; a++, go=go->next) {
796                                 oblist[a] = go->ob;
797                                 obcopylist[a] = *go->ob;
798                         }
799                 }
800                 else {
801                         ob = part->dup_ob;
802                         obcopy = *ob;
803                 }
804
805                 if(totchild==0 || part->draw & PART_DRAW_PARENT)
806                         a = 0;
807                 else
808                         a = totpart;
809
810                 for(pa=psys->particles,counter=0; a<totpart+totchild; a++,pa++,counter++) {
811                         if(a<totpart) {
812                                 /* handle parent particle */
813                                 if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP))
814                                         continue;
815
816                                 pa_num = pa->num;
817                                 pa_time = pa->time;
818                                 size = pa->size;
819                         }
820                         else {
821                                 /* handle child particle */
822                                 cpa = &psys->child[a - totpart];
823
824                                 pa_num = a;
825                                 pa_time = psys->particles[cpa->parent].time;
826                                 size = psys_get_child_size(psys, cpa, ctime, 0);
827                         }
828
829                         if(part->draw_as==PART_DRAW_GR) {
830                                 /* for groups, pick the object based on settings */
831                                 if(part->draw&PART_DRAW_RAND_GR)
832                                         b= BLI_rand() % totgroup;
833                                 else if(part->from==PART_FROM_PARTICLE)
834                                         b= pa_num % totgroup;
835                                 else
836                                         b= a % totgroup;
837
838                                 ob = oblist[b];
839                                 obmat = oblist[b]->obmat;
840                                 oldobmat = obcopylist[b].obmat;
841                         }
842                         else {
843                                 obmat= ob->obmat;
844                                 oldobmat= obcopy.obmat;
845                         }
846
847                         if(hair) {
848                                 /* hair we handle separate and compute transform based on hair keys */
849                                 if(a < totpart) {
850                                         cache = psys->pathcache[a];
851                                         psys_get_dupli_path_transform(par, psys, psmd, pa, 0, cache, pamat, &scale);
852                                 }
853                                 else {
854                                         cache = psys->childcache[a-totpart];
855                                         psys_get_dupli_path_transform(par, psys, psmd, 0, cpa, cache, pamat, &scale);
856                                 }
857
858                                 VECCOPY(pamat[3], cache->co);
859                                 pamat[3][3]= 1.0f;
860                                 
861                         }
862                         else {
863                                 /* first key */
864                                 state.time = ctime;
865                                 if(psys_get_particle_state(par, psys, a, &state, 0) == 0)
866                                         continue;
867
868                                 QuatToMat4(state.rot, pamat);
869                                 VECCOPY(pamat[3], state.co);
870                                 pamat[3][3]= 1.0f;
871                         }
872
873                         if(part->draw_as==PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
874                                 for(go= part->dup_group->gobject.first, b=0; go; go= go->next, b++) {
875                                         Mat4MulMat4(tmat, oblist[b]->obmat, pamat);
876                                         Mat4MulFloat3((float *)tmat, size*scale);
877                                         if(par_space_mat)
878                                                 Mat4MulMat4(mat, tmat, par_space_mat);
879                                         else
880                                                 Mat4CpyMat4(mat, tmat);
881
882                                         dob= new_dupli_object(lb, go->ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
883                                         Mat4CpyMat4(dob->omat, obcopylist[b].obmat);
884                                         if(G.rendering)
885                                                 psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
886                                 }
887                         }
888                         else {
889                                 /* to give ipos in object correct offset */
890                                 where_is_object_time(ob, ctime-pa_time);
891                                 
892                                 Mat4CpyMat4(mat, pamat);
893
894                                 Mat4MulMat4(tmat, obmat, mat);
895                                 Mat4MulFloat3((float *)tmat, size*scale);
896                                 if(par_space_mat)
897                                         Mat4MulMat4(mat, tmat, par_space_mat);
898                                 else
899                                         Mat4CpyMat4(mat, tmat);
900
901                                 dob= new_dupli_object(lb, ob, mat, ob->lay, counter, OB_DUPLIPARTS, animated);
902                                 Mat4CpyMat4(dob->omat, oldobmat);
903                                 if(G.rendering)
904                                         psys_get_dupli_texture(par, part, psmd, pa, cpa, dob->uv, dob->orco);
905                         }
906                 }
907
908                 /* restore objects since they were changed in where_is_object_time */
909                 if(part->draw_as==PART_DRAW_GR) {
910                         for(a=0; a<totgroup; a++)
911                                 *(oblist[a])= obcopylist[a];
912                 }
913                 else
914                         *ob= obcopy;
915         }
916
917         /* clean up */
918         if(oblist)
919                 MEM_freeN(oblist);
920         if(obcopylist)
921                 MEM_freeN(obcopylist);
922
923         if(psys->lattice) {
924                 end_latt_deform();
925                 psys->lattice = 0;
926         }
927 }
928
929 static Object *find_family_object(Object **obar, char *family, char ch)
930 {
931         Object *ob;
932         int flen;
933         
934         if( obar[ch] ) return obar[ch];
935         
936         flen= strlen(family);
937         
938         ob= G.main->object.first;
939         while(ob) {
940                 if( ob->id.name[flen+2]==ch ) {
941                         if( strncmp(ob->id.name+2, family, flen)==0 ) break;
942                 }
943                 ob= ob->id.next;
944         }
945         
946         obar[ch]= ob;
947         
948         return ob;
949 }
950
951
952 static void font_duplilist(ListBase *lb, Object *par, int level, int animated)
953 {
954         Object *ob, *obar[256];
955         Curve *cu;
956         struct chartrans *ct, *chartransdata;
957         float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
958         int slen, a;
959         
960         /* simple preventing of too deep nested groups */
961         if(level>MAX_DUPLI_RECUR) return;
962         
963         Mat4CpyMat4(pmat, par->obmat);
964         
965         /* in par the family name is stored, use this to find the other objects */
966         
967         chartransdata= text_to_curve(par, FO_DUPLI);
968         if(chartransdata==0) return;
969         
970         memset(obar, 0, 256*sizeof(void *));
971         
972         cu= par->data;
973         slen= strlen(cu->str);
974         fsize= cu->fsize;
975         xof= cu->xof;
976         yof= cu->yof;
977         
978         ct= chartransdata;
979         
980         for(a=0; a<slen; a++, ct++) {
981                 
982                 ob= find_family_object(obar, cu->family, cu->str[a]);
983                 if(ob) {
984                         vec[0]= fsize*(ct->xof - xof);
985                         vec[1]= fsize*(ct->yof - yof);
986                         vec[2]= 0.0;
987                         
988                         Mat4MulVecfl(pmat, vec);
989                         
990                         Mat4CpyMat4(obmat, par->obmat);
991                         VECCOPY(obmat[3], vec);
992                         
993                         new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIVERTS, animated);
994                 }
995         }
996         
997         MEM_freeN(chartransdata);
998 }
999
1000 /* ***************************** */
1001 static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated)
1002 {       
1003         if((ob->transflag & OB_DUPLI)==0)
1004                 return;
1005         
1006         /* Should the dupli's be generated for this object? - Respect restrict flags */
1007         if (G.rendering) {
1008                 if (ob->restrictflag & OB_RESTRICT_RENDER) {
1009                         return;
1010                 }
1011         } else {
1012                 if (ob->restrictflag & OB_RESTRICT_VIEW) {
1013                         return;
1014                 }
1015         }
1016
1017         if(ob->transflag & OB_DUPLIPARTS) {
1018                 ParticleSystem *psys = ob->particlesystem.first;
1019                 for(; psys; psys=psys->next)
1020                         new_particle_duplilist(duplilist, id, ob, par_space_mat, psys, level+1, animated);
1021         }
1022         else if(ob->transflag & OB_DUPLIVERTS) {
1023                 if(ob->type==OB_MESH) {
1024                         vertex_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1025                 }
1026                 else if(ob->type==OB_FONT) {
1027                         if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1028                                 font_duplilist(duplilist, ob, level+1, animated);
1029                         }
1030                 }
1031         }
1032         else if(ob->transflag & OB_DUPLIFACES) {
1033                 if(ob->type==OB_MESH)
1034                         face_duplilist(duplilist, id, ob, par_space_mat, level+1, animated);
1035         }
1036         else if(ob->transflag & OB_DUPLIFRAMES) {
1037                 if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1038                         frames_duplilist(duplilist, ob, level+1, animated);
1039                 }
1040         } else if(ob->transflag & OB_DUPLIGROUP) {
1041                 DupliObject *dob;
1042                 
1043                 group_duplilist(duplilist, ob, level+1, animated); /* now recursive */
1044
1045                 if (level==0) {
1046                         for(dob= duplilist->first; dob; dob= dob->next)
1047                                 if(dob->type == OB_DUPLIGROUP)
1048                                         Mat4CpyMat4(dob->ob->obmat, dob->mat);
1049                 }
1050         }
1051 }
1052
1053 /* Returns a list of DupliObject
1054  * note; group dupli's already set transform matrix. see note in group_duplilist() */
1055 ListBase *object_duplilist(Scene *sce, Object *ob)
1056 {
1057         ListBase *duplilist= MEM_mallocN(sizeof(ListBase), "duplilist");
1058         duplilist->first= duplilist->last= NULL;
1059         object_duplilist_recursive((ID *)sce, ob, duplilist, NULL, 0, 0);
1060         return duplilist;
1061 }
1062
1063 void free_object_duplilist(ListBase *lb)
1064 {
1065         DupliObject *dob;
1066         
1067         for(dob= lb->first; dob; dob= dob->next) {
1068                 dob->ob->lay= dob->origlay;
1069                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
1070         }
1071         
1072         BLI_freelistN(lb);
1073         MEM_freeN(lb);
1074 }
1075
1076 int count_duplilist(Object *ob)
1077 {
1078         if(ob->transflag & OB_DUPLI) {
1079                 if(ob->transflag & OB_DUPLIVERTS) {
1080                         if(ob->type==OB_MESH) {
1081                                 if(ob->transflag & OB_DUPLIVERTS) {
1082                                         ParticleSystem *psys = ob->particlesystem.first;
1083                                         int pdup=0;
1084
1085                                         for(; psys; psys=psys->next)
1086                                                 pdup += psys->totpart;
1087
1088                                         if(pdup==0){
1089                                                 Mesh *me= ob->data;
1090                                                 return me->totvert;
1091                                         }
1092                                         else
1093                                                 return pdup;
1094                                 }
1095                         }
1096                 }
1097                 else if(ob->transflag & OB_DUPLIFRAMES) {
1098                         int tot= ob->dupend - ob->dupsta; 
1099                         tot/= (ob->dupon+ob->dupoff);
1100                         return tot*ob->dupon;
1101                 }
1102         }
1103         return 1;
1104 }