6 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version. The Blender
12 * Foundation also sells licenses for use in proprietary software under
13 * the Blender License. See http://www.blender.org/BL/ for information
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26 * All rights reserved.
28 * The Original Code is: all of this file.
30 * Contributor(s): none yet.
32 * ***** END GPL/BL DUAL LICENSE BLOCK *****
38 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_arithb.h"
41 #include "DNA_listBase.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_effect_types.h"
45 #include "DNA_group_types.h"
46 #include "DNA_key_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_vfont_types.h"
55 #include "BKE_DerivedMesh.h"
56 #include "BKE_displist.h"
57 #include "BKE_effect.h"
59 #include "BKE_group.h"
60 #include "BKE_global.h"
64 #include "BKE_object.h"
65 #include "BKE_utildefines.h"
67 #include "BKE_bad_level_calls.h"
73 ListBase duplilist= {0, 0};
75 void free_path(Path *path)
77 if(path->data) MEM_freeN(path->data);
82 void calc_curvepath(Object *ob)
85 BevPoint *bevp, *bevpn, *bevpfirst, *bevplast, *tempbevp;
89 float *fp, *dist, *maxdist, x, y, z;
90 float fac, d=0, fac1, fac2;
94 /* in a path vertices are with equal differences: path->len = number of verts */
95 /* NOW WITH BEVELCURVE!!! */
97 if(ob==NULL || ob->type != OB_CURVE) return;
99 if(ob==G.obedit) nu= editNurb.first;
100 else nu= cu->nurb.first;
102 if(cu->path) free_path(cu->path);
108 cu->path=path= MEM_callocN(sizeof(Path), "path");
110 /* if POLY: last vertice != first vertice */
111 cycl= (bl->poly!= -1);
113 if(cycl) tot= bl->nr;
117 /* exception: vector handle paths and polygon paths should be subdivided at least a factor 6 (or more?) */
118 if(path->len<6*nu->pntsu) path->len= 6*nu->pntsu;
120 dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
122 /* all lengths in *dist */
123 bevp= bevpfirst= (BevPoint *)(bl+1);
126 for(a=0; a<tot; a++) {
128 if(cycl && a==tot-1) {
129 x= bevpfirst->x - bevp->x;
130 y= bevpfirst->y - bevp->y;
131 z= bevpfirst->z - bevp->z;
135 x= (tempbevp)->x - bevp->x;
136 y= (tempbevp)->y - bevp->y;
137 z= (tempbevp)->z - bevp->z;
139 *fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
146 /* the path verts in path->data */
147 /* now also with TILT value */
148 ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
152 bevplast= bevpfirst + (bl->nr-1);
155 fac= 1.0f/((float)path->len-1.0f);
156 fac = fac * path->totdist;
158 for(a=0; a<path->len; a++) {
162 /* we're looking for location (distance) 'd' in the array */
163 while((d>= *fp) && fp<maxdist) {
165 if(bevp<bevplast) bevp++;
168 if(cycl) bevpn= bevpfirst;
169 else bevpn= bevplast;
173 fac1= *(fp)- *(fp-1);
178 ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
179 ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
180 ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
181 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
190 int interval_test(int min, int max, int p1, int cycl)
195 p1= ((p1 -min) % (max-min+1)) + max+1;
197 p1= ((p1 -min) % (max-min+1)) + min;
200 if(p1 < min) p1= min;
201 else if(p1 > max) p1= max;
206 /* warning, *vec needs FOUR items! */
207 /* ctime is normalized range <0-1> */
208 int where_on_path(Object *ob, float ctime, float *vec, float *dir) /* returns OK */
214 float *fp, *p0, *p1, *p2, *p3, fac;
216 int cycl=0, s0, s1, s2, s3;
218 if(ob==NULL || ob->type != OB_CURVE) return 0;
220 if(cu->path==NULL || cu->path->data==NULL) {
221 printf("no path!\n");
226 /* test for cyclic */
228 if(bl && bl->poly> -1) cycl= 1;
230 ctime *= (path->len-1);
232 s1= (int)floor(ctime);
233 fac= (float)(s1+1)-ctime;
235 /* path->len is corected for cyclic */
236 s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
237 s1= interval_test(0, path->len-1-cycl, s1, cycl);
238 s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
239 s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
246 /* note, commented out for follow constraint */
247 //if(cu->flag & CU_FOLLOW) {
249 set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
251 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
252 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
253 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
255 /* make compatible with vectoquat */
263 /* make sure that first and last frame are included in the vectors here */
264 if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
265 else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
266 else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
267 else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
269 vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
270 vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
271 vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
273 vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
278 /* ****************** DUPLICATOR ************** */
280 static void new_dupli_object(ListBase *lb, Object *ob, float mat[][4], int lay, int index)
282 DupliObject *dob= MEM_mallocN(sizeof(DupliObject), "dupliobject");
283 BLI_addtail(lb, dob);
285 Mat4CpyMat4(dob->mat, mat);
286 Mat4CpyMat4(dob->omat, ob->obmat);
287 dob->origlay= ob->lay;
292 static void group_duplilist(ListBase *lb, Object *ob)
298 if(ob->dup_group==NULL) return;
300 /* handles animated groups, and */
301 /* we need to check update for objects that are not in scene... */
302 group_handle_recalc_and_update(ob, ob->dup_group);
304 for(go= ob->dup_group->gobject.first; go; go= go->next) {
306 Mat4MulMat4(mat, go->ob->obmat, ob->obmat);
307 new_dupli_object(lb, go->ob, mat, ob->lay, 0);
311 /* make copy already, because in group dupli's deform displists can be makde, requiring parent matrices */
312 for(dob= lb->first; dob; dob= dob->next)
313 Mat4CpyMat4(dob->ob->obmat, dob->mat);
316 static void frames_duplilist(ListBase *lb, Object *ob)
318 extern int enable_cu_speed; /* object.c */
322 cfrao= G.scene->r.cfra;
323 if(ob->parent==NULL && ob->track==NULL && ob->ipo==NULL && ob->constraints.first==NULL) return;
325 if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
326 copyob= *ob; /* store transform info */
328 for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
332 ok= G.scene->r.cfra - ob->dupsta;
333 ok= ok % (ob->dupon+ob->dupoff);
334 if(ok < ob->dupon) ok= 1;
339 where_is_object_time(ob, (float)G.scene->r.cfra);
340 new_dupli_object(lb, ob, ob->obmat, ob->lay, G.scene->r.cfra);
344 *ob= copyob; /* restore transform info */
345 G.scene->r.cfra= cfrao;
349 struct vertexDupliData {
355 static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
357 struct vertexDupliData *vdd= userData;
358 float vec[3], *q2, mat[3][3], tmat[4][4], obmat[4][4];
361 Mat4MulVecfl(vdd->pmat, vec);
362 VecSubf(vec, vec, vdd->pmat[3]);
363 VecAddf(vec, vec, vdd->ob->obmat[3]);
365 Mat4CpyMat4(obmat, vdd->ob->obmat);
366 VECCOPY(obmat[3], vec);
368 if(vdd->par->transflag & OB_DUPLIROT) {
370 vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
372 q2= vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag);
375 Mat4CpyMat4(tmat, obmat);
376 Mat4MulMat43(obmat, tmat, mat);
378 new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index);
381 static void vertex_duplilist(ListBase *lb, Scene *sce, Object *par)
385 float vec[3], no[3], pmat[4][4];
390 Mat4CpyMat4(pmat, par->obmat);
395 dm= editmesh_get_derived_cage(&dmNeedsFree);
397 dm = mesh_get_derived_deform(par, &dmNeedsFree);
399 totvert = dm->getNumVerts(dm);
401 base= sce->base.first;
404 if(base->object->type>0 && (lay & base->lay) && G.obedit!=base->object) {
405 ob= base->object->parent;
408 struct vertexDupliData vdd;
414 Mat4CpyMat4(vdd.pmat, pmat);
416 /* mballs have a different dupli handling */
417 if(ob->type!=OB_MBALL) ob->flag |= OB_DONE; /* doesnt render */
420 dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
423 for(a=0; a<totvert; a++) {
424 dm->getVertCo(dm, a, vec);
425 dm->getVertNo(dm, a, no);
427 vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
443 static void particle_duplilist(ListBase *lb, Scene *sce, Object *par, PartEff *paf)
448 float ctime, vec1[3];
449 float vec[3], tmat[4][4], mat[3][3];
451 int lay, a, counter; /* counter is used to find in render the indexed object */
455 build_particle_system(par);
460 ctime= bsystem_time(par, 0, (float)G.scene->r.cfra, 0.0);
464 for(base= sce->base.first; base; base= base->next) {
465 if(base->object->type>0 && (base->lay & lay) && G.obedit!=base->object) {
466 ob= base->object->parent;
471 /* temp copy, to have ipos etc to work OK */
474 /* don't want parent animation to apply on past object positions */
475 if(!(paf->flag & PAF_STATIC))
478 for(a=0, pa= paf->keys, counter=0; a<paf->totpart; a++, pa+=paf->totkey, counter++) {
480 if(paf->flag & PAF_STATIC) {
483 where_is_particle(paf, pa, pa->time, vec1);
484 mtime= pa->time+pa->lifetime;
486 for(ctime= pa->time; ctime<mtime; ctime+=paf->staticstep, counter++) {
488 /* make sure hair grows until the end.. */
489 if(ctime>pa->time+pa->lifetime) ctime= pa->time+pa->lifetime;
491 /* to give ipos in object correct offset */
492 where_is_object_time(ob, ctime-pa->time);
494 where_is_particle(paf, pa, ctime, vec); // makes sure there's always a vec
495 Mat4MulVecfl(par->obmat, vec);
497 if(paf->stype==PAF_VECT) {
498 where_is_particle(paf, pa, ctime+1.0, vec1); // makes sure there's always a vec
499 Mat4MulVecfl(par->obmat, vec1);
501 VecSubf(vec1, vec1, vec);
502 q2= vectoquat(vec1, ob->trackflag, ob->upflag);
505 Mat4CpyMat4(tmat, ob->obmat);
506 Mat4MulMat43(ob->obmat, tmat, mat);
509 VECCOPY(ob->obmat[3], vec);
510 /* put object back in original state, so it cam be restored OK */
511 Mat4CpyMat4(tmat, ob->obmat);
512 Mat4CpyMat4(ob->obmat, copyob.obmat);
513 new_dupli_object(lb, ob, tmat, par->lay, counter);
516 else { // non static particles
518 if((paf->flag & PAF_UNBORN)==0 && ctime < pa->time) continue;
519 if((paf->flag & PAF_DIED)==0 && ctime > pa->time+pa->lifetime) continue;
521 //if(ctime < pa->time+pa->lifetime) {
523 /* to give ipos in object correct offset, ob->parent is NULLed */
524 where_is_object_time(ob, ctime-pa->time);
526 where_is_particle(paf, pa, ctime, vec);
527 if(paf->stype==PAF_VECT) {
529 /* if particle died, we use previous position */
530 if(ctime > pa->time+pa->lifetime) {
531 where_is_particle(paf, pa, pa->time+pa->lifetime-1.0f, vec1);
532 VecSubf(vec1, vec, vec1);
535 where_is_particle(paf, pa, ctime+1.0f, vec1);
536 VecSubf(vec1, vec1, vec);
538 q2= vectoquat(vec1, ob->trackflag, ob->upflag);
541 Mat4CpyMat4(tmat, ob->obmat);
542 Mat4MulMat43(ob->obmat, tmat, mat);
545 VECCOPY(ob->obmat[3], vec);
547 /* put object back in original state, so it can be restored OK */
548 Mat4CpyMat4(tmat, ob->obmat);
549 Mat4CpyMat4(ob->obmat, copyob.obmat);
550 new_dupli_object(lb, ob, tmat, par->lay, counter);
553 /* temp copy, to have ipos etc to work OK */
564 static Object *find_family_object(Object **obar, char *family, char ch)
569 if( obar[ch] ) return obar[ch];
571 flen= strlen(family);
573 ob= G.main->object.first;
575 if( ob->id.name[flen+2]==ch ) {
576 if( strncmp(ob->id.name+2, family, flen)==0 ) break;
587 static void font_duplilist(ListBase *lb, Object *par)
589 Object *ob, *obar[256];
591 struct chartrans *ct, *chartransdata;
592 float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
595 Mat4CpyMat4(pmat, par->obmat);
597 /* in par the family name is stored, use this to find the other objects */
599 chartransdata= text_to_curve(par, FO_DUPLI);
600 if(chartransdata==0) return;
602 memset(obar, 0, 256*sizeof(void *));
605 slen= strlen(cu->str);
612 for(a=0; a<slen; a++, ct++) {
614 ob= find_family_object(obar, cu->family, cu->str[a]);
616 vec[0]= fsize*(ct->xof - xof);
617 vec[1]= fsize*(ct->yof - yof);
620 Mat4MulVecfl(pmat, vec);
622 Mat4CpyMat4(obmat, par->obmat);
623 VECCOPY(obmat[3], vec);
625 new_dupli_object(lb, ob, obmat, par->lay, a);
630 MEM_freeN(chartransdata);
633 /* ***************************** */
635 /* note; group dupli's already set transform matrix. see note in group_duplilist() */
636 ListBase *object_duplilist(Scene *sce, Object *ob)
638 static ListBase duplilist={NULL, NULL};
640 if(duplilist.first) {
641 printf("wrong call to object_duplilist\n");
644 duplilist.first= duplilist.last= NULL;
646 if(ob->transflag & OB_DUPLI) {
647 if(ob->transflag & OB_DUPLIVERTS) {
648 if(ob->type==OB_MESH) {
649 if(ob->transflag & OB_DUPLIVERTS) {
651 if( (paf=give_parteff(ob)) ) particle_duplilist(&duplilist, sce, ob, paf);
652 else vertex_duplilist(&duplilist, sce, ob);
655 else if(ob->type==OB_FONT) {
656 font_duplilist(&duplilist, ob);
659 else if(ob->transflag & OB_DUPLIFRAMES)
660 frames_duplilist(&duplilist, ob);
661 else if(ob->transflag & OB_DUPLIGROUP)
662 group_duplilist(&duplilist, ob);
668 void free_object_duplilist(ListBase *lb)
672 for(dob= lb->first; dob; dob= dob->next) {
673 dob->ob->lay= dob->origlay;
674 Mat4CpyMat4(dob->ob->obmat, dob->omat);
680 int count_duplilist(Object *ob)
682 if(ob->transflag & OB_DUPLI) {
683 if(ob->transflag & OB_DUPLIVERTS) {
684 if(ob->type==OB_MESH) {
685 if(ob->transflag & OB_DUPLIVERTS) {
687 if( (paf=give_parteff(ob)) ) {
697 else if(ob->transflag & OB_DUPLIFRAMES) {
698 int tot= ob->dupend - ob->dupsta;
699 tot/= (ob->dupon+ob->dupoff);
700 return tot*ob->dupon;