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