1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
/** anim.c
*
*
* $Id$
*
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. The Blender
* Foundation also sells licenses for use in proprietary software under
* the Blender License. See http://www.blender.org/BL/ for information
* about this.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#include <math.h>
#include <string.h>
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "DNA_listBase.h"
#include "DNA_object_types.h"
#include "DNA_curve_types.h"
#include "DNA_key_types.h"
#include "DNA_view3d_types.h"
#include "DNA_effect_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_scene_types.h"
#include "BKE_DerivedMesh.h"
#include "BKE_global.h"
#include "BKE_utildefines.h"
#include "BKE_anim.h"
#include "BKE_ipo.h"
#include "BKE_object.h"
#include "BKE_displist.h"
#include "BKE_key.h"
#include "BKE_font.h"
#include "BKE_effect.h"
#include "BKE_bad_level_calls.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
ListBase duplilist= {0, 0};
void free_path(Path *path)
{
if(path->data) MEM_freeN(path->data);
MEM_freeN(path);
}
void calc_curvepath(Object *ob)
{
BevList *bl;
BevPoint *bevp, *bevpn, *bevpfirst, *bevplast, *tempbevp;
Curve *cu;
Nurb *nu;
Path *path;
float *fp, *dist, *maxdist, x, y, z;
float fac, d=0, fac1, fac2;
int a, tot, cycl=0;
float *ft;
/* in a path vertices are with equal differences: path->len = number of verts */
/* NOW WITH BEVELCURVE!!! */
if(ob==NULL || ob->type != OB_CURVE) return;
cu= ob->data;
if(ob==G.obedit) nu= editNurb.first;
else nu= cu->nurb.first;
if(cu->path) free_path(cu->path);
cu->path= NULL;
bl= cu->bev.first;
if(bl==NULL) return;
cu->path=path= MEM_callocN(sizeof(Path), "path");
/* if POLY: last vertice != first vertice */
cycl= (bl->poly!= -1);
if(cycl) tot= bl->nr;
else tot= bl->nr-1;
path->len= tot+1;
/* exception: vector handle paths and polygon paths should be subdivided at least a factor 6 (or more?) */
if(path->len<6*nu->pntsu) path->len= 6*nu->pntsu;
dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
/* all lengths in *dist */
bevp= bevpfirst= (BevPoint *)(bl+1);
fp= dist;
*fp= 0;
for(a=0; a<tot; a++) {
fp++;
if(cycl && a==tot-1) {
x= bevpfirst->x - bevp->x;
y= bevpfirst->y - bevp->y;
z= bevpfirst->z - bevp->z;
}
else {
tempbevp = bevp+1;
x= (tempbevp)->x - bevp->x;
y= (tempbevp)->y - bevp->y;
z= (tempbevp)->z - bevp->z;
}
*fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
bevp++;
}
path->totdist= *fp;
/* the path verts in path->data */
/* now also with TILT value */
ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
bevp= bevpfirst;
bevpn= bevp+1;
bevplast= bevpfirst + (bl->nr-1);
fp= dist+1;
maxdist= dist+tot;
fac= 1.0f/((float)path->len-1.0f);
fac = fac * path->totdist;
for(a=0; a<path->len; a++) {
d= ((float)a)*fac;
/* we're looking for location (distance) 'd' in the array */
while((d>= *fp) && fp<maxdist) {
fp++;
if(bevp<bevplast) bevp++;
bevpn= bevp+1;
if(bevpn>bevplast) {
if(cycl) bevpn= bevpfirst;
else bevpn= bevplast;
}
}
fac1= *(fp)- *(fp-1);
fac2= *(fp)-d;
fac1= fac2/fac1;
fac2= 1.0f-fac1;
ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
ft+= 4;
}
MEM_freeN(dist);
}
int interval_test(int min, int max, int p1, int cycl)
{
if(cycl) {
if( p1 < min)
p1= ((p1 -min) % (max-min+1)) + max+1;
else if(p1 > max)
p1= ((p1 -min) % (max-min+1)) + min;
}
else {
if(p1 < min) p1= min;
else if(p1 > max) p1= max;
}
return p1;
}
/* warning, *vec needs FOUR items! */
int where_on_path(Object *ob, float ctime, float *vec, float *dir) /* returns OK */
{
Curve *cu;
Nurb *nu;
BevList *bl;
Path *path;
float *fp, *p0, *p1, *p2, *p3, fac;
float data[4];
int cycl=0, s0, s1, s2, s3;
if(ob==NULL || ob->type != OB_CURVE) return 0;
cu= ob->data;
if(cu->path==NULL || cu->path->data==NULL) {
printf("no path!\n");
}
path= cu->path;
fp= path->data;
/* test for cyclic */
bl= cu->bev.first;
if(bl && bl->poly> -1) cycl= 1;
ctime *= (path->len-1);
s1= (int)floor(ctime);
fac= (float)(s1+1)-ctime;
/* path->len is corected for cyclic */
s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
s1= interval_test(0, path->len-1-cycl, s1, cycl);
s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
p0= fp + 4*s0;
p1= fp + 4*s1;
p2= fp + 4*s2;
p3= fp + 4*s3;
/* note, commented out for follow constraint */
//if(cu->flag & CU_FOLLOW) {
set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
/* make compatible with vectoquat */
dir[0]= -dir[0];
dir[1]= -dir[1];
dir[2]= -dir[2];
//}
nu= cu->nurb.first;
/* make sure that first and last frame are included in the vectors here */
if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
return 1;
}
static Object *new_dupli_object(ListBase *lb, Object *ob, Object *par)
{
Object *newob;
newob= MEM_mallocN(sizeof(Object), "newobj dupli");
memcpy(newob, ob, sizeof(Object));
newob->flag |= OB_FROMDUPLI;
newob->id.newid= (ID *)par; /* store duplicator */
/* only basis-ball gets displist */
if(newob->type==OB_MBALL) newob->disp.first= newob->disp.last= NULL;
if(ob!=par) { // dupliverts, particle
newob->parent= NULL;
newob->track= NULL;
}
BLI_addtail(lb, newob);
return newob;
}
void frames_duplilist(Object *ob)
{
extern int enable_cu_speed; /* object.c */
Object *newob;
int cfrao, ok;
cfrao= G.scene->r.cfra;
if(ob->parent==NULL && ob->track==NULL && ob->ipo==NULL && ob->constraints.first==NULL) return;
if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
ok= 1;
if(ob->dupoff) {
ok= G.scene->r.cfra - ob->dupsta;
ok= ok % (ob->dupon+ob->dupoff);
if(ok < ob->dupon) ok= 1;
else ok= 0;
}
if(ok) {
newob= new_dupli_object(&duplilist, ob, ob);
do_ob_ipo(newob);
where_is_object_time(newob, (float)G.scene->r.cfra);
}
}
G.scene->r.cfra= cfrao;
enable_cu_speed= 1;
do_ob_ipo(ob);
}
struct vertexDupliData {
float pmat[4][4];
Object *ob, *par;
};
static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
{
struct vertexDupliData *vdd= userData;
Object *newob;
float vec[3], *q2, mat[3][3], tmat[4][4];
VECCOPY(vec, co);
Mat4MulVecfl(vdd->pmat, vec);
VecSubf(vec, vec, vdd->pmat[3]);
VecAddf(vec, vec, vdd->ob->obmat[3]);
newob= new_dupli_object(&duplilist, vdd->ob, vdd->par);
VECCOPY(newob->obmat[3], vec);
if(vdd->par->transflag & OB_DUPLIROT) {
vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
q2= vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag);
QuatToMat3(q2, mat);
Mat4CpyMat4(tmat, newob->obmat);
Mat4MulMat43(newob->obmat, tmat, mat);
}
}
void vertex_duplilist(Scene *sce, Object *par)
{
Object *ob;
Base *base;
float vec[3], no[3], pmat[4][4];
int lay, totvert, a;
int dmNeedsFree;
DerivedMesh *dm;
Mat4CpyMat4(pmat, par->obmat);
lay= G.scene->lay;
if(par==G.obedit)
dm= editmesh_get_derived_cage(&dmNeedsFree);
else
dm = mesh_get_derived_deform(par, &dmNeedsFree);
totvert = dm->getNumVerts(dm);
base= sce->base.first;
while(base) {
if(base->object->type>0 && (lay & base->lay) && G.obedit!=base->object) {
ob= base->object->parent;
while(ob) {
if(ob==par) {
struct vertexDupliData vdd;
ob= base->object;
vdd.ob= ob;
vdd.par= par;
Mat4CpyMat4(vdd.pmat, pmat);
/* mballs have a different dupli handling */
if(ob->type!=OB_MBALL) ob->flag |= OB_DONE; /* doesnt render */
if(par==G.obedit) {
dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
}
else {
for(a=0; a<totvert; a++) {
dm->getVertCo(dm, a, vec);
dm->getVertNo(dm, a, no);
vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
}
}
break;
}
ob= ob->parent;
}
}
base= base->next;
}
if (dmNeedsFree)
dm->release(dm);
}
void particle_duplilist(Scene *sce, Object *par, PartEff *paf)
{
Object *ob, *newob;
Base *base;
Particle *pa;
float ctime, vec1[3];
float vec[3], tmat[4][4], mat[3][3];
float *q2;
int lay, a;
pa= paf->keys;
if(pa==0) {
build_particle_system(par);
pa= paf->keys;
if(pa==0) return;
}
ctime= bsystem_time(par, 0, (float)G.scene->r.cfra, 0.0);
lay= G.scene->lay;
base= sce->base.first;
while(base) {
if(base->object->type>0 && (base->lay & lay) && G.obedit!=base->object) {
ob= base->object->parent;
while(ob) {
if(ob==par) {
ob= base->object;
pa= paf->keys;
for(a=0; a<paf->totpart; a++, pa+=paf->totkey) {
if(paf->flag & PAF_STATIC) {
float mtime;
where_is_particle(paf, pa, pa->time, vec1);
mtime= pa->time+pa->lifetime;
for(ctime= pa->time; ctime<mtime; ctime+=paf->staticstep) {
newob= new_dupli_object(&duplilist, ob, par);
/* make sure hair grows until the end.. */
if(ctime>pa->time+pa->lifetime) ctime= pa->time+pa->lifetime;
/* to give ipos in object correct offset */
where_is_object_time(newob, ctime-pa->time);
where_is_particle(paf, pa, ctime, vec); // makes sure there's always a vec
Mat4MulVecfl(par->obmat, vec);
if(paf->stype==PAF_VECT) {
where_is_particle(paf, pa, ctime+1.0, vec1); // makes sure there's always a vec
Mat4MulVecfl(par->obmat, vec1);
VecSubf(vec1, vec1, vec);
q2= vectoquat(vec1, ob->trackflag, ob->upflag);
QuatToMat3(q2, mat);
Mat4CpyMat4(tmat, newob->obmat);
Mat4MulMat43(newob->obmat, tmat, mat);
}
VECCOPY(newob->obmat[3], vec);
}
}
else { // non static particles
if(ctime > pa->time) {
if(ctime < pa->time+pa->lifetime) {
newob= new_dupli_object(&duplilist, ob, par);
/* to give ipos in object correct offset */
where_is_object_time(newob, ctime-pa->time);
where_is_particle(paf, pa, ctime, vec);
if(paf->stype==PAF_VECT) {
where_is_particle(paf, pa, ctime+1.0f, vec1);
VecSubf(vec1, vec1, vec);
q2= vectoquat(vec1, ob->trackflag, ob->upflag);
QuatToMat3(q2, mat);
Mat4CpyMat4(tmat, newob->obmat);
Mat4MulMat43(newob->obmat, tmat, mat);
}
VECCOPY(newob->obmat[3], vec);
}
}
}
}
break;
}
ob= ob->parent;
}
}
base= base->next;
}
}
void free_duplilist()
{
Object *ob;
while( (ob= duplilist.first) ) {
BLI_remlink(&duplilist, ob);
MEM_freeN(ob);
}
}
void make_duplilist(Scene *sce, Object *ob)
{
PartEff *paf;
if(ob->transflag & OB_DUPLI) {
if(ob->transflag & OB_DUPLIVERTS) {
if(ob->type==OB_MESH) {
if(ob->transflag & OB_DUPLIVERTS) {
if( (paf=give_parteff(ob)) ) particle_duplilist(sce, ob, paf);
else vertex_duplilist(sce, ob);
}
}
else if(ob->type==OB_FONT) {
font_duplilist(ob);
}
}
else if(ob->transflag & OB_DUPLIFRAMES) frames_duplilist(ob);
}
}
int count_duplilist(Object *ob)
{
if(ob->transflag & OB_DUPLI) {
if(ob->transflag & OB_DUPLIVERTS) {
if(ob->type==OB_MESH) {
if(ob->transflag & OB_DUPLIVERTS) {
PartEff *paf;
if( (paf=give_parteff(ob)) ) {
return paf->totpart;
}
else {
Mesh *me= ob->data;
return me->totvert;
}
}
}
}
else if(ob->transflag & OB_DUPLIFRAMES) {
int tot= ob->dupend - ob->dupsta;
tot/= (ob->dupon+ob->dupoff);
return tot*ob->dupon;
}
}
return 1;
}
|