4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 #include "MEM_guardedalloc.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_arithb.h"
39 #include "DNA_listBase.h"
41 #include "DNA_object_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_key_types.h"
44 #include "DNA_view3d_types.h"
45 #include "DNA_effect_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_scene_types.h"
49 #include "BKE_global.h"
50 #include "BKE_utildefines.h"
53 #include "BKE_object.h"
54 #include "BKE_displist.h"
57 #include "BKE_effect.h"
59 #include "BKE_bad_level_calls.h"
61 ListBase duplilist= {0, 0};
63 void free_path(Path *path)
65 if(path->data) MEM_freeN(path->data);
70 void calc_curvepath(Object *ob)
73 BevPoint *bevp, *bevpn, *bevpfirst, *bevplast;
77 float *fp, *dist, *maxdist, x, y, z;
78 float fac, d=0, fac1, fac2;
82 /* in een pad zitten allemaal punten met gelijke afstand: path->len = aantal pt */
83 /* NU MET BEVELCURVE!!! */
85 if(ob==0 || ob->type != OB_CURVE) return;
87 if(ob==G.obedit) nu= editNurb.first;
88 else nu= cu->nurb.first;
90 if(cu->path) free_path(cu->path);
93 if((cu->flag & CU_PATH)==0) return;
102 cu->path=path= MEM_callocN(sizeof(Path), "path");
104 /* als POLY: laatste punt != eerste punt */
105 cycl= (bl->poly!= -1);
107 if(cycl) tot= bl->nr;
111 /* exception: vector handle paths and polygon paths should be subdivided at least a factor 6 (or more?) */
112 if(path->len<6*nu->pntsu) path->len= 6*nu->pntsu;
114 dist= (float *)MEM_mallocN((tot+1)*4, "berekenpaddist");
116 /* alle lengtes in *dist */
117 bevp= bevpfirst= (BevPoint *)(bl+1);
120 for(a=0; a<tot; a++) {
122 if(cycl && a==tot-1) {
123 x= bevpfirst->x - bevp->x;
124 y= bevpfirst->y - bevp->y;
125 z= bevpfirst->z - bevp->z;
128 x= (bevp+1)->x - bevp->x;
129 y= (bevp+1)->y - bevp->y;
130 z= (bevp+1)->z - bevp->z;
132 *fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
139 /* de padpunten in path->data */
140 /* nu ook met TILT */
141 ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
145 bevplast= bevpfirst + (bl->nr-1);
148 fac= 1.0f/((float)path->len-1.0f);
150 for(a=0; a<path->len; a++) {
152 d= ((float)a)*fac*path->totdist;
154 /* we zoeken plek 'd' in het array */
155 while((d>= *fp) && fp<maxdist) {
157 if(bevp<bevplast) bevp++;
160 if(cycl) bevpn= bevpfirst;
161 else bevpn= bevplast;
165 fac1= *(fp)- *(fp-1);
170 ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
171 ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
172 ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
173 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
182 int interval_test(int min, int max, int p1, int cycl)
187 p1= ((p1 -min) % (max-min+1)) + max+1;
189 p1= ((p1 -min) % (max-min+1)) + min;
192 if(p1 < min) p1= min;
193 else if(p1 > max) p1= max;
198 int where_on_path(Object *ob, float ctime, float *vec, float *dir) /* geeft OK terug */
204 float *fp, *p0, *p1, *p2, *p3, fac;
206 int cycl=0, s0, s1, s2, s3;
209 if(ob==0 || ob->type != OB_CURVE) return 0;
211 if(cu->path==0 || cu->path->data==0) calc_curvepath(ob);
217 if(bl && bl->poly> -1) cycl= 1;
219 /* ctime is van 0.0-1.0 */
220 ctime *= (path->len-1);
222 s1= (int)floor(ctime);
223 fac= (float)(s1+1)-ctime;
225 /* path->len is gecorrigeerd voor cyclic, zie boven, is beetje warrig! */
226 s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
227 s1= interval_test(0, path->len-1-cycl, s1, cycl);
228 s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
229 s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
236 if(cu->flag & CU_FOLLOW) {
238 set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
240 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
241 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
242 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
244 /* compatible maken met vectoquat */
252 /* zeker van zijn dat de eerste en laatste frame door de punten gaat */
253 if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
254 else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
255 else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
256 else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
258 vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
259 vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
260 vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
262 vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
267 void frames_duplilist(Object *ob)
269 extern int enable_cu_speed; /* object.c */
273 cfrao= G.scene->r.cfra;
274 if(ob->parent==0 && ob->track==0 && ob->ipo==0) return;
276 if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
278 /* dit om zeker van te zijn dat er iets gezbufferd wordt: in drawobject.c: dt==wire en boundboxclip */
279 if(G.background==0 && ob->type==OB_MESH) {
282 if(me->disp.first==0) addnormalsDispList(ob, &me->disp);
283 if(ob->dt==OB_SHADED) {
285 if(dl==0 || dl->col1==0) shadeDispList(ob);
289 for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
293 ok= G.scene->r.cfra - ob->dupsta;
294 ok= ok % (ob->dupon+ob->dupoff);
295 if(ok < ob->dupon) ok= 1;
299 newob= MEM_mallocN(sizeof(Object), "newobobj dupli");
300 memcpy(newob, ob, sizeof(Object));
302 /* alleen de basis-ball behoeft een displist */
303 if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
305 BLI_addtail(&duplilist, newob);
307 where_is_object(newob);
309 newob->flag |= OB_FROMDUPLI;
310 newob->id.newid= (ID *)ob; /* duplicator bewaren */
314 G.scene->r.cfra= cfrao;
320 void vertex_duplilist(Scene *sce, Object *par)
326 float vec[3], pvec[3], pmat[4][4], mat[3][3], tmat[4][4];
330 Mat4CpyMat4(pmat, par->obmat);
336 base= sce->base.first;
339 if(base->object->type>0 && (lay & base->lay) && G.obedit!=base->object) {
340 ob= base->object->parent;
345 /* mballs have a different dupli handling */
346 if(ob->type!=OB_MBALL) ob->flag |= OB_DONE; /* doesnt render */
350 mvert+= (me->totvert-1);
351 VECCOPY(pvec, mvert->co);
352 Mat4MulVecfl(pmat, pvec);
355 totvert= me->totvert;
357 for(a=0; a<totvert; a++, mvert++) {
359 /* bereken de extra offset (tov. nulpunt parent) die de kinderen krijgen */
360 VECCOPY(vec, mvert->co);
361 Mat4MulVecfl(pmat, vec);
362 VecSubf(vec, vec, pmat[3]);
363 VecAddf(vec, vec, ob->obmat[3]);
365 newob= MEM_mallocN(sizeof(Object), "newobj dupli");
366 memcpy(newob, ob, sizeof(Object));
367 newob->flag |= OB_FROMDUPLI;
368 newob->id.newid= (ID *)par; /* duplicator bewaren */
370 /* alleen de basis-ball behoeft een displist */
371 if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
373 VECCOPY(newob->obmat[3], vec);
375 if(par->transflag & OB_DUPLIROT) {
376 VECCOPY(vec, mvert->no);
377 vec[0]= -vec[0]; vec[1]= -vec[1]; vec[2]= -vec[2];
379 q2= vectoquat(vec, ob->trackflag, ob->upflag);
382 Mat4CpyMat4(tmat, newob->obmat);
383 Mat4MulMat43(newob->obmat, tmat, mat);
388 /* newob->borig= base; */
390 BLI_addtail(&duplilist, newob);
405 void particle_duplilist(Scene *sce, Object *par, PartEff *paf)
410 float ctime, vec1[3];
411 float vec[3], tmat[4][4], mat[3][3];
417 build_particle_system(par);
422 ctime= bsystem_time(par, 0, (float)G.scene->r.cfra, 0.0);
426 base= sce->base.first;
429 if(base->object->type>0 && (base->lay & lay) && G.obedit!=base->object) {
430 ob= base->object->parent;
437 for(a=0; a<paf->totpart; a++, pa+=paf->totkey) {
439 if(ctime > pa->time) {
440 if(ctime < pa->time+pa->lifetime) {
442 newob= MEM_mallocN(sizeof(Object), "newobj dupli");
443 memcpy(newob, ob, sizeof(Object));
444 newob->flag |= OB_FROMDUPLI;
445 newob->id.newid= (ID *)par; /* duplicator bewaren */
447 /* alleen de basis-ball behoeft een displist */
448 if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= 0;
450 where_is_particle(paf, pa, ctime, vec);
451 if(paf->stype==PAF_VECT) {
452 where_is_particle(paf, pa, ctime+1.0f, vec1);
454 VecSubf(vec1, vec1, vec);
455 q2= vectoquat(vec1, ob->trackflag, ob->upflag);
458 Mat4CpyMat4(tmat, newob->obmat);
459 Mat4MulMat43(newob->obmat, tmat, mat);
462 VECCOPY(newob->obmat[3], vec);
467 BLI_addtail(&duplilist, newob);
482 void free_duplilist()
486 while( ob= duplilist.first) {
487 BLI_remlink(&duplilist, ob);
493 void make_duplilist(Scene *sce, Object *ob)
497 if(ob->transflag & OB_DUPLI) {
498 if(ob->transflag & OB_DUPLIVERTS) {
499 if(ob->type==OB_MESH) {
500 if(ob->transflag & OB_DUPLIVERTS) {
501 if( paf=give_parteff(ob) ) particle_duplilist(sce, ob, paf);
502 else vertex_duplilist(sce, ob);
505 else if(ob->type==OB_FONT) {
509 else if(ob->transflag & OB_DUPLIFRAMES) frames_duplilist(ob);