-
Notifications
You must be signed in to change notification settings - Fork 3
/
arbre.cpp
1231 lines (1105 loc) · 43.3 KB
/
arbre.cpp
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This is a partial implementation of Jason Weber and Joseph Penn's
// paper, "Creation and Rendering of Realistic Trees", from SIGGRAPH
// 1995. This code should not be treated as an example on how to
// implement that paper properly (even though there are some confusing
// sections in the paper that I've tried to interpret in a useful
// fashion), but more as a basic example on how one could set up
// nested object instance hierarchies to maximize memory efficiency in
// PRMan 17.0 (as described in my User's Group Meeting talk at
// SIGGRAPH 2012).
//
// Sections 4.1 through 4.5 of the Penn/Weber paper are mostly
// implemented, 4.6 through 4.9 are not. I also did not implement
// ternary stem splits, handling of -nCurveV (helical stems), or trunk
// lobes.
//
// To use this plugin, compile it as a DSO. On Linux, this can be done
// using a command resembling:
//
// g++ -I$RMANTREE/include -fPIC -o arbre.so -shared arbre.cpp
//
// The resulting shared object can be loaded with RiProcedural2
// using a recent version of prman:
//
// Procedural2 "DynamicLoad" "SimpleBound" "float[6] bound" [-20 20
// -20 20 0 20] "string dsoname" ["arbre"] "int Seed" [66] "int
// MaxLevel" [5] "int StartLevel" [0] "int InstanceLevel" [4] "int
// Leaves" [1] "int InstanceLeaves" [1]
//
// The useful parameters are:
//
// "string Type": tree type. Currently supported: oak, tupelo, and aspen.
// The parameters are straight out of the paper, slightly modified
// in a few cases
//
// "int Seed": random seed for the tree. If you are growing a forest
// of trees, every tree should have its own seed
//
// "int MaxLevel": maximum level of the tree - this overrides the Levels
// parameter in the paper
//
// "int InstanceLevel": level at which to use instanced branches. For
// example, if InstanceLevel == MaxLevel, the terminal level of the
// tree will use instanced branches. For two levels of terminal instanced
// branches, set InstanceLevel == MaxLevel - 1, and so on. To disable
// instanced branches, set this parameter to 0.
//
// "int Leaves": toggle switch for whether leaves will be grown or not
//
// "int InstanceLeaves": whether leaves will be instanced. If 1,
// the procedural requires a "leaf" ObjectInstance to have
// been previously defined. If 0, the procedural requires a "leaf"
// inline archive to be previously defined.
//
// "int StartLevel": by default, the procedural starts at Level 0, this
// parameter overrides that default. This is mainly useful for
// visualization purposes
//
//
// There are several known bugs with this plugin that ought to be
// addressed before even considering using this for production work.
//
// - If a stem has high curvature, its children may be detached at the
// base. This is "fixed" by switching to polygons. The proper fix for
// subdivision meshes is to interpolate using the cubic b-spline
// rather than just perform linear interpolation as is done here
//
// - The actual formulae for the leaf/branch frequency/stem
// length/stem radius are dependent not only parent properties but
// also grandparent properties. So, the simple-minded instancing being
// used here where we just look for the branch of the right length
// isn't close to correct as far the Penn/Weber model is
// concerned. I've attempted to compensate for this by associating an
// arbitrary parent offset with each stem length, and this seems to be
// "good enough" at a first glance. A more rigorous derivation,
// coupled perhaps by searching for appropriate instances in multiple
// dimensions (e.g. radius and length), may provide more accurate
// results.
//
// - The use of erand48 throughout is completely ad hoc and wacky.
//
// Naturally, there are other useful features that should be implemented
// as well (like switching amongst multiple leaf types).
//
//
// Julian Fong <[email protected]>
#include <vector>
#include <deque>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DLL /* Must be done on Windows to get the correct declspec */
#include "ri.h"
#include "RixInterfaces.h"
#ifndef M_PI
#define M_PI 3.1415926535
#endif
extern "C" {
PRMANEXPORT RtVoid Subdivide2(RtContextHandle ctx, RtFloat detail,
RtInt n, RtToken toks[], RtPointer vals[]);
}
struct arbreParameters {
const struct treeParameters *tree;
float maxStemLengths[5];
int maxLevel;
int startLevel;
int instanceLevel;
int leaves;
int instanceLeaves;
float terminalScale;
};
static void growstem(const struct arbreParameters &tree,
float baseradius, float length, int level, float parentV,
unsigned short xsubi[3]);
static void
rotateMatrix(float angle, float axisx, float axisy, float axisz, RtMatrix m)
{
RtMatrix tm;
float a, b, c, d;
float cost, sint, radians, aa, bb, cc, ab, ac, bc, cost1;
float asint, bsint, csint, abcost1, accost1, bccost1;
d = sqrtf(axisx * axisx + axisy * axisy + axisz * axisz);
d = 1.0f/d;
a = axisx * d;
b = axisy * d;
c = axisz * d;
radians = angle * 0.0174532925f;
cost = cosf(radians);
sint = sinf(radians);
cost1 = 1.0 - cost;
asint = a*sint; bsint = b*sint; csint = c*sint;
aa = a*a; bb = b*b; cc = c*c;
ab = a*b; ac = a*c; bc = b*c;
abcost1 = ab*cost1; accost1 = ac*cost1; bccost1 = bc*cost1;
tm[0][3] = tm[1][3] = tm[2][3] = tm[3][0] = tm[3][1] = tm[3][2] = 0.;
tm[3][3] = 1.0;
tm[0][0] = aa+(1-aa)*cost;
tm[0][1] = abcost1+csint;
tm[0][2] = accost1-bsint;
tm[1][0] = abcost1-csint;
tm[1][1] = bb+(1-bb)*cost;
tm[1][2] = bccost1+asint;
tm[2][0] = accost1+bsint;
tm[2][1] = bccost1-asint;
tm[2][2] = cc+(1-cc)*cost;
RtMatrix t;
float *ap, *bp;
int i, j;
float *rp = t[0];
for(i=0;i!=4;i++)for(j=0;j!=4;j++){
float sum;
int k;
ap = &tm[i][0];
bp = &m[0][j];
sum=0.;
for(k=0;k!=4;k++){
sum += *ap++ * *bp;
bp +=4;
}
*rp++ = sum;
}
memcpy(m, t, sizeof(RtMatrix));
}
struct stemParameters {
float downAngle, downAngleV;
float rotate, rotateV, branches;
float length, lengthV, taper;
float segSplits, splitAngle, splitAngleV;
int curveRes;
float curve, curveBack, curveV;
};
struct leafParameters {
float leaves;
float leafScale, leafScaleX;
float attractionUp;
};
struct treeParameters {
enum Shape {
k_Conical,
k_Spherical,
k_Hemispherical,
k_Cylindrical,
k_TaperedCylindrical,
k_Flame,
k_InverseConical,
k_TendFlame
} shape;
float baseSize;
float scale, scaleV, zScale, zScaleV;
int levels;
float ratio, ratioPower;
int lobes;
float lobeDepth;
float flare;
float baseSplits;
struct stemParameters stemParameters[4];
struct leafParameters leafParameters;
};
static float
shapeRatio(enum treeParameters::Shape shape, float ratio) {
switch (shape) {
case treeParameters::k_Conical:
return 0.2f + 0.8f * ratio;
break;
case treeParameters::k_Spherical:
return 0.2f + 0.8f * sinf(M_PI * ratio);
break;
case treeParameters::k_Hemispherical:
return 0.2f + 0.8f * sinf(0.5 * M_PI * ratio);
break;
case treeParameters::k_Cylindrical:
return 1.0f;
break;
case treeParameters::k_TaperedCylindrical:
return 0.5f + 0.5f * ratio;
break;
case treeParameters::k_Flame:
return (ratio < 0.7f) ? (ratio / 0.7f) : (1.0f - ratio) / 0.3f;
break;
case treeParameters::k_InverseConical:
return 1.0f - 0.8f * ratio;
break;
case treeParameters::k_TendFlame:
return (ratio < 0.7f) ? (0.5 + 0.5 * ratio / 0.7f) : (0.5 + 0.5f * (1.0 - ratio) / 0.3);
break;
}
return 1;
}
const struct treeParameters CA_BlackOakParameters = {
treeParameters::k_Hemispherical,
0.05,
10, 10, 1, 0,
3,
0.018, 1.3,
5, 0.1,
1.2,
2,
{
{
0, 0,
0, 0, 0,
1, 0, 0.95,
0.4, 10, 0,
8,
0, 0, 90
},
{
30, -30,
80, 0, 40,
0.8, 0.1, 1,
0.2, 10, 10,
10,
40, -70, 150
},
{
45, 10,
140, 0, 120,
0.2, 0.05, 1,
0.1, 10, 10,
3,
0, 0, -30
},
{
45, 10,
140, 0, 5, /* paper has 0, which makes no sense. */
0.4, 0, 1,
0, 0, 0,
1,
0, 0, 0
}
},
{
25,
0.12, 0.66,
0.8
}
};
const struct treeParameters BlackTupeloParameters = {
treeParameters::k_TaperedCylindrical,
0.2,
23, 5, 1, 0,
4,
0.015, 1.3,
3, 0.1,
1,
0,
{
{
0, 0,
0, 0, 0,
1, 0, 1.1,
0, 0, 0,
10,
0, 0, 40
},
{
60, -40,
140, 0, 50,
0.3, 0.05, 1,
0, 0, 0,
10,
0, 0, 90
},
{
30, 10,
140, 0, 25,
0.6, 0.1, 1,
0, 0, 0,
10,
-10, 0, 150
},
{
45, 10,
140, 0, 12,
0.4, 0, 1,
0, 0, 0,
1,
0, 0, 0
}
},
{
6,
// The paper has 0.3 here. 30 cm leaves seems way too big - I
// pulled 0.085 from Wikipedia
0.085, 0.5,
0.5
}
};
const struct treeParameters QuakingAspenParameters = {
treeParameters::k_TendFlame,
0.4,
13, 3, 1, 0,
3,
0.015, 1.2,
5, 0.07,
0.6,
0,
{
{
0, 0,
0, 0, 0,
1, 0, 1,
0, 0, 0,
3,
0, 0, 20
},
{
60, -50,
140, 0, 50,
0.3, 0, 1,
0, 0, 0,
5,
-40, 0, 50
},
{
45, 10,
140, 0, 30,
0.6, 0, 1,
0, 0, 0,
3,
-40, 0, 75
},
{
45, 10,
77, 0, 10,
/*0*/ 0.4, 0, 1, // paper has 0 for 3Length - surely a typo
0, 0, 0,
1,
0, 0, 0
}
},
{
25,
// 0.17, 1, // paper has 0.17. Possibly true for young trees, but not true for older aspens
0.09, 1,
0.5
}
};
struct stemData {
stemData() :
nf(0) {
}
int nf;
std::vector<int> nverts;
std::vector<int> verts;
std::vector<float> p;
std::vector<int> corners;
};
static void
extendstem(const struct arbreParameters ¶ms,
const struct stemParameters &stem,
int level, int segment,
float baseradius, float tipradius,
float length, float parentLength, float parentV,
float branchPower, float previousBranchAngle,
RtPoint base, RtMatrix coordSystem,
std::deque<float> &unspreadAngles,
std::deque<float> &unspreadAxes,
int previousRing[4],
unsigned short xsubi[3],
struct stemData &result) {
const unsigned short basexsubi[3] = {
xsubi[0],
xsubi[1],
xsubi[2]
};
int i;
float v = segment / (float) stem.curveRes;
float deltaV = 1.0f / stem.curveRes;
float segmentBaseRadius = baseradius + v * (tipradius - baseradius);
float segmentTipRadius = baseradius + (v + deltaV) * (tipradius - baseradius);
if (params.tree->flare != 0.0f && level == 0) {
float y = 1.0f - 8.0f * v;
if (y < 0.0f) y = 0.0f;
float flare = params.tree->flare * (powf(100.0f, y) - 1.0f) * 0.01f + 1.0f;
segmentBaseRadius *= flare;
y = 1.0f - 8.0f * (v + deltaV);
if (y < 0.0f) y = 0.0f;
flare = params.tree->flare * (powf(100.0f, y) - 1) * 0.01f + 1.0f;
segmentTipRadius *= flare;
}
float segmentLength = length / (float) stem.curveRes;
float branchAngle = previousBranchAngle;
float angle;
RtMatrix nextCoordSystem;
int childlevel = level + 1;
if (childlevel > 3) childlevel = 3;
const struct stemParameters &childStem = params.tree->stemParameters[childlevel];
// Swizzle random seed for branch/leaf growing
unsigned short branchxsubi[3] = {
basexsubi[2] + 37,
basexsubi[1] + 42,
basexsubi[0] + 79,
};
//////////////////////////////////////////////////
// Branch growing
//////////////////////////////////////////////////
if (level < params.maxLevel && childStem.branches != 0) {
// Compute the number of branches to generate for this stem
float maxBranches = 0;
float childLengthMax = childStem.length;
float childLength = 0;
if (level == 0) {
// The paper is *really* confusing here. It seems to
// require the number of branches be a function of the
// length of the child branches, which is a chicken and
// egg problem for the way the code is structured
// here. Solution implemented here is to compute the
// number of branches at v and at v + deltaV and take the
// max.
float ratio = (1 - v) / (1 - params.tree->baseSize);
childLength = childLengthMax * length * shapeRatio(params.tree->shape, ratio);
float maxBranches1 = childStem.branches * (0.2 + 0.8 * (childLength / length)) /
childLengthMax / (float) stem.curveRes;
ratio = (1 - (v + deltaV)) / (1 - params.tree->baseSize);
childLength = childLengthMax * length * shapeRatio(params.tree->shape, ratio);
float maxBranches2 = childStem.branches * (0.2 + 0.8 * (childLength / length)) /
childLengthMax / (float) stem.curveRes;
maxBranches = maxBranches1;
if (maxBranches2 > maxBranches) maxBranches = maxBranches2;
} else {
maxBranches = childStem.branches * (1.0 - 0.5 * parentV)
/ ((float) stem.curveRes);
}
// printf("branches per segment = %f branchPower = %f\n", maxBranches, branchPower);
// Paper: "Any stem that has been cloned or is, itself, a
// clone reduces its propensity to form clones by half." I
// assume this actually meant its propensity to form
// *branches*.
maxBranches *= branchPower;
int nBranches = (int) truncf(maxBranches);
maxBranches -= nBranches;
if (maxBranches > 0) {
if (erand48(branchxsubi) < maxBranches) {
nBranches++;
}
}
for (i = 0; i < nBranches; ++i) {
// branchv is 0 to 1 for the branches in this stem segment
float branchV = i / (float) nBranches;
// branchTotalV is 0 to 1 for the entire stem
float branchTotalV = v + branchV * deltaV;
if (level == 0) {
if (branchTotalV < params.tree->baseSize) {
continue;
} else {
float ratio = (1 - branchTotalV) / (1 - params.tree->baseSize);
childLength = childLengthMax * length * shapeRatio(params.tree->shape, ratio);
}
} else {
childLength = childLengthMax * (length - 0.6 * branchTotalV * length);
}
if (params.terminalScale != 0 && childLength > 0.01f) {
float childRadius = baseradius * powf(childLength / length, params.tree->ratioPower);
// "The maximum radius of a stem is explicitly
// limited to the radius of the parent at the
// point from which it was spawned."
float maxChildRadius = segmentBaseRadius + branchV *
(segmentTipRadius - segmentBaseRadius);
if (childRadius > maxChildRadius) childRadius = maxChildRadius;
RiAttributeBegin();
RiTranslate(base[0] + branchV * segmentLength * coordSystem[2][0],
base[1] + branchV * segmentLength * coordSystem[2][1],
base[2] + branchV * segmentLength * coordSystem[2][2]);
branchAngle += childStem.rotate + (2 * erand48(branchxsubi) - 1) *
childStem.rotateV;
RiRotate(branchAngle, 0, 0, 1);
float downAngle;
if (childStem.downAngleV < 0.0f) {
float ratio = (1 - branchTotalV) / (1 - params.tree->baseSize);
downAngle = childStem.downAngle + (2 * erand48(branchxsubi) - 1) *
childStem.downAngleV * (1 - 2 * shapeRatio(treeParameters::k_Conical, ratio));
} else {
downAngle = childStem.downAngle + (2 * erand48(branchxsubi) - 1) *
childStem.downAngleV;
}
RiRotate(downAngle, 1, 0, 0);
if (params.terminalScale != 1.0f &&
(level + 1 == params.maxLevel)) {
RiScale(params.terminalScale, params.terminalScale, params.terminalScale);
}
RiConcatTransform(coordSystem);
unsigned short newxsubi[3] = {
basexsubi[2] + 37 + i,
basexsubi[1] + 42 - i,
basexsubi[0] + 79
};
if (params.instanceLevel && level + 1 >= params.instanceLevel) {
// Find an instanced branch of appropriate size
float ratio = childLength / params.maxStemLengths[params.instanceLevel];
int bucket = trunc(ratio * 10);
int index = bucket * 10 + (erand48(newxsubi) * 10);
char buf[256];
sprintf(buf, "branch_%d_%d", level + 1, index);
RiObjectInstanceV((RtObjectHandle) buf, 0, NULL, NULL);
} else {
growstem(params, childRadius, childLength, level + 1,
branchTotalV, newxsubi);
}
RiAttributeEnd();
}
}
}
//////////////////////////////////////////////////
// Leaf growing
//////////////////////////////////////////////////
if (params.leaves && level != 0) {/* && level == params.tree->levels*/ // uncomment to grow only on terminal branches
const struct leafParameters &leaf = params.tree->leafParameters;
float maxLeaves = leaf.leaves * shapeRatio(treeParameters::k_TaperedCylindrical, parentV) /
(float) stem.curveRes;
int nLeaves = trunc(maxLeaves);
maxLeaves -= nLeaves;
if (maxLeaves > 0) {
if (erand48(branchxsubi) < maxLeaves) {
nLeaves++;
}
}
for (i = 0; i < nLeaves; ++i) {
// leafV is 0 to 1 for the leaves in this stem segment
float leafV = i / (float) nLeaves;
RiAttributeBegin();
RiTranslate(base[0] + leafV * segmentLength * coordSystem[2][0],
base[1] + leafV * segmentLength * coordSystem[2][1],
base[2] + leafV * segmentLength * coordSystem[2][2]);
branchAngle += childStem.rotate + (2 * erand48(branchxsubi) - 1) *
childStem.rotateV;
RiRotate(branchAngle, 0, 0, 1);
if (params.terminalScale != 1.0f &&
(level + 1 == params.maxLevel)) {
RiScale(params.terminalScale, params.terminalScale, params.terminalScale);
}
// I don't think we ever get negative downAngleV for leaves
float downAngle = childStem.downAngle + (2 * erand48(branchxsubi) - 1) * childStem.downAngleV;
RiRotate(downAngle, 1, 0, 0);
RiConcatTransform(coordSystem);
// Assumes the leaf is modelled with length in Z, width in
// X
RiScale(leaf.leafScaleX * leaf.leafScale,
1.0f,
leaf.leafScale);
if (params.instanceLeaves) {
RiObjectInstanceV((RtObjectHandle) "leaf", 0, NULL, NULL);
} else {
RiReadArchive("leaf", NULL, NULL);
}
RiAttributeEnd();
}
}
//////////////////////////////////////////////////
// Stem growing
//////////////////////////////////////////////////
// Decide whether to split or continue
int nSplits = trunc(stem.segSplits);
float splitf = stem.segSplits - nSplits;
if (splitf > 0) {
if (erand48(xsubi) < splitf) {
nSplits++;
}
}
// Compute the next segment's coordinate system
if (segment != stem.curveRes - 1) {
memcpy(nextCoordSystem, coordSystem, sizeof(RtMatrix));
// Apply curve variation about local x axis
if (stem.curveBack == 0) {
angle = stem.curve / (float) stem.curveRes;
} else {
if (segment < stem.curveRes / 2) {
angle = stem.curve / (0.5f * stem.curveRes);
} else {
angle = stem.curveBack / (0.5f * stem.curveRes);
}
}
angle += ((2 * erand48(xsubi) - 1) * stem.curveV) / (float) stem.curveRes;
rotateMatrix(angle, coordSystem[0][0], coordSystem[0][1],
coordSystem[0][2], nextCoordSystem);
// Apply spread angle compensations back towards parent(s) stem axes
for (int j = 0; j < (int) unspreadAngles.size(); ++j) {
rotateMatrix(unspreadAngles[j], unspreadAxes[j * 3],
unspreadAxes[j * 3 + 1], unspreadAxes[j * 3 + 2],
nextCoordSystem);
}
}
// FIXME: the paper describes ternary splits (nSplits > 1), this
// only implements binary splits
if (nSplits && segment != stem.curveRes - 1) {
// Compute the coordinate systems for the clone and the
// continuation of the original stem. We rotate the splits
// around the x axis to minimize tearing.
// Angle of declination is acosf(local z axis . tree z axis).
// Since local tree z axis is 0, 0, 1, the dot product is just
// coordsys[2][2]
float declination = acosf(coordSystem[2][2]) * 57.2957795131f;
RtMatrix cloneCoordSystem;
memcpy(cloneCoordSystem, nextCoordSystem, sizeof(RtMatrix));
std::deque<float> cloneUnspreadAngles(unspreadAngles);
std::deque<float> cloneUnspreadAxes(unspreadAxes);
angle = stem.splitAngle - declination;
if (angle < 0) {
angle = 0;
}
rotateMatrix(angle, coordSystem[0][0], coordSystem[0][1],
coordSystem[0][2], cloneCoordSystem);
cloneUnspreadAngles.push_front(-angle / (float) (stem.curveRes - segment + 1));
cloneUnspreadAxes.push_back(coordSystem[0][0]);
cloneUnspreadAxes.push_back(coordSystem[0][1]);
cloneUnspreadAxes.push_back(coordSystem[0][2]);
rotateMatrix(-angle, coordSystem[0][0], coordSystem[0][1],
coordSystem[0][2], nextCoordSystem);
unspreadAngles.push_front(angle / (float) (stem.curveRes - segment + 1));
unspreadAxes.push_back(coordSystem[0][0]);
unspreadAxes.push_back(coordSystem[0][1]);
unspreadAxes.push_back(coordSystem[0][2]);
// Additional rotation around the tree axis (which is defined
// to be 0, 0, 1)
float xi = erand48(xsubi);
xi *= xi;
angle = 20 + 0.75 * (30 + fabsf(declination - 90)) * xi;
if (erand48(xsubi) > 0.5) {
angle = -angle;
}
rotateMatrix(angle, 0, 0, 1, nextCoordSystem);
int vertbase = result.p.size() / 3;
// Generate split faces
int newRing[6] = {vertbase, vertbase + 1, vertbase + 2,
vertbase + 3, vertbase + 4, vertbase + 5};
// Move to end of segment in local Z coordinate system
RtPoint clonebase;
clonebase[0] = base[0] + segmentLength * coordSystem[2][0];
clonebase[1] = base[1] + segmentLength * coordSystem[2][1];
clonebase[2] = base[2] + segmentLength * coordSystem[2][2];
base[0] = base[0] + segmentLength * coordSystem[2][0];
base[1] = base[1] + segmentLength * coordSystem[2][1];
base[2] = base[2] + segmentLength * coordSystem[2][2];
for (i = 0; i < 3; ++i) {
result.p.push_back(clonebase[i] -
segmentTipRadius * 0.5 * (cloneCoordSystem[0][i] + coordSystem[0][i]) +
segmentTipRadius * 0.5 * (cloneCoordSystem[1][i] + coordSystem[1][i]));
}
for (i = 0; i < 3; ++i) {
result.p.push_back(0.5 * (clonebase[i] +
segmentTipRadius * 0.5 * (cloneCoordSystem[0][i] + coordSystem[0][i]) +
segmentTipRadius * 0.5 * (cloneCoordSystem[1][i] + coordSystem[1][i])) +
0.5 * (base[i] -
segmentTipRadius * 0.5 * (nextCoordSystem[0][i] + coordSystem[0][i]) +
segmentTipRadius * 0.5 * (nextCoordSystem[1][i] + coordSystem[1][i])));
}
for (i = 0; i < 3; ++i) {
result.p.push_back(base[i] +
segmentTipRadius * 0.5 * (nextCoordSystem[0][i] + coordSystem[0][i]) +
segmentTipRadius * 0.5 * (nextCoordSystem[1][i] + coordSystem[1][i]));
}
for (i = 0; i < 3; ++i) {
result.p.push_back(base[i] +
segmentTipRadius * 0.5 * (nextCoordSystem[0][i] + coordSystem[0][i]) -
segmentTipRadius * 0.5 * (nextCoordSystem[1][i] + coordSystem[1][i]));
}
for (i = 0; i < 3; ++i) {
result.p.push_back(0.5 * (clonebase[i] +
segmentTipRadius * 0.5 * (cloneCoordSystem[0][i] + coordSystem[0][i]) -
segmentTipRadius * 0.5 * (cloneCoordSystem[1][i] + coordSystem[1][i])) +
0.5 * (base[i] -
segmentTipRadius * 0.5 * (nextCoordSystem[0][i] + coordSystem[0][i]) -
segmentTipRadius * 0.5 * (nextCoordSystem[1][i] + coordSystem[1][i])));
}
for (i = 0; i < 3; ++i) {
result.p.push_back(clonebase[i] -
segmentTipRadius * 0.5 * (cloneCoordSystem[0][i] + coordSystem[0][i]) -
segmentTipRadius * 0.5 * (cloneCoordSystem[1][i] + coordSystem[1][i]));
}
// Connect the dots
result.nf += 4;
result.nverts.push_back(5);
result.nverts.push_back(4);
result.nverts.push_back(5);
result.nverts.push_back(4);
result.verts.push_back(previousRing[0]);
result.verts.push_back(newRing[0]);
result.verts.push_back(newRing[1]);
result.verts.push_back(newRing[2]);
result.verts.push_back(previousRing[1]);
result.verts.push_back(previousRing[1]);
result.verts.push_back(newRing[2]);
result.verts.push_back(newRing[3]);
result.verts.push_back(previousRing[2]);
result.verts.push_back(previousRing[2]);
result.verts.push_back(newRing[3]);
result.verts.push_back(newRing[4]);
result.verts.push_back(newRing[5]);
result.verts.push_back(previousRing[3]);
result.verts.push_back(previousRing[3]);
result.verts.push_back(newRing[5]);
result.verts.push_back(newRing[0]);
result.verts.push_back(previousRing[0]);
result.corners.push_back(newRing[1]);
result.corners.push_back(newRing[4]);
// And continue
int newRing1[4] = {vertbase, vertbase + 1,
vertbase + 4, vertbase + 5};
int newRing2[4] = {vertbase + 1, vertbase + 2,
vertbase + 3, vertbase + 4};
unsigned short clonexsubi[3] = {
basexsubi[1] + segment + 137,
basexsubi[2] - segment + 259,
basexsubi[0] + 511
};
extendstem(params, stem, level, segment + 1, baseradius, tipradius,
length, parentLength, v + deltaV, branchPower * 0.5f, branchAngle, clonebase, cloneCoordSystem,
cloneUnspreadAngles, cloneUnspreadAxes, newRing1, clonexsubi, result);
unsigned short newxsubi[3] = {
basexsubi[2] + segment + 389,
basexsubi[1] - segment + 107,
basexsubi[0] + 989
};
extendstem(params, stem, level, segment + 1, baseradius, tipradius,
length, parentLength, v + deltaV, branchPower * 0.5f, branchAngle, base, nextCoordSystem,
unspreadAngles, unspreadAxes, newRing2, newxsubi, result);
} else {
// Move to end of segment in local Z coordinate system
base[0] += segmentLength * coordSystem[2][0];
base[1] += segmentLength * coordSystem[2][1];
base[2] += segmentLength * coordSystem[2][2];
int vertbase = result.p.size() / 3;
for (i = 0; i < 3; ++i) {
result.p.push_back(base[i] - segmentTipRadius * coordSystem[0][i] +
segmentTipRadius * coordSystem[1][i]);
}
for (i = 0; i < 3; ++i) {
result.p.push_back(base[i] + segmentTipRadius * coordSystem[0][i] +
segmentTipRadius * coordSystem[1][i]);
}
for (i = 0; i < 3; ++i) {
result.p.push_back(base[i] + segmentTipRadius * coordSystem[0][i] -
segmentTipRadius * coordSystem[1][i]);
}
for (i = 0; i < 3; ++i) {
result.p.push_back(base[i] - segmentTipRadius * coordSystem[0][i] -
segmentTipRadius * coordSystem[1][i]);
}
int newRing[4] = {vertbase, vertbase + 1, vertbase + 2, vertbase + 3};
// We cap the last segment with one more face
if (segment == stem.curveRes - 1) {
result.nf += 5;
} else {
result.nf += 4;
}
result.nverts.push_back(4);
result.nverts.push_back(4);
result.nverts.push_back(4);
result.nverts.push_back(4);
if (segment == stem.curveRes - 1) {
result.nverts.push_back(4);
}
result.verts.push_back(previousRing[0]);
result.verts.push_back(newRing[0]);
result.verts.push_back(newRing[1]);
result.verts.push_back(previousRing[1]);
result.verts.push_back(previousRing[1]);
result.verts.push_back(newRing[1]);
result.verts.push_back(newRing[2]);
result.verts.push_back(previousRing[2]);
result.verts.push_back(previousRing[2]);
result.verts.push_back(newRing[2]);
result.verts.push_back(newRing[3]);
result.verts.push_back(previousRing[3]);
result.verts.push_back(previousRing[3]);
result.verts.push_back(newRing[3]);
result.verts.push_back(newRing[0]);
result.verts.push_back(previousRing[0]);
if (segment == stem.curveRes - 1) {
result.verts.push_back(newRing[0]);
result.verts.push_back(newRing[3]);
result.verts.push_back(newRing[2]);
result.verts.push_back(newRing[1]);
} else {
unsigned short newxsubi[3] = {
basexsubi[2] + segment + 389,
basexsubi[1] - segment + 107,
basexsubi[0] + 989
};
extendstem(params, stem, level, segment + 1,
baseradius, tipradius, length, parentLength, v + deltaV, branchPower,
branchAngle,
base, nextCoordSystem,
unspreadAngles, unspreadAxes, newRing, newxsubi, result);
}
}
}
static void
growstem(const struct arbreParameters ¶ms,
float baseradius, float length, int level, float parentV,
unsigned short xsubi[3]) {
const struct stemParameters &stem = params.tree->stemParameters[(level > 3) ? 3 : level];
int i;
RtPoint base = { 0, 0, 0 };
RtMatrix coordSystem = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
std::deque<float> unspreadAngles;
std::deque<float> unspreadAxes;
struct stemData result;
// Kick things off by adding a ring at the base
float stemtipradius = params.tree->ratio * params.tree->scale * (1 - stem.taper);
float radius = baseradius;
if (level == 0 && params.tree->flare != 0.0f) {
// Derived from paper equation with z = 0
radius *= (1 + 0.99 * params.tree->flare);
}
result.p.push_back(-radius);
result.p.push_back(radius);
result.p.push_back(0);
result.p.push_back(radius);
result.p.push_back(radius);
result.p.push_back(0);
result.p.push_back(radius);
result.p.push_back(-radius);
result.p.push_back(0);
result.p.push_back(-radius);
result.p.push_back(-radius);
result.p.push_back(0);
int basering[4] = {0, 1, 2, 3};
RiAttributeBegin();
extendstem(params, stem, level, 0,
baseradius, stemtipradius, length, -1 /* no parentLength */,
parentV, 1.0f, 0.0f, base, coordSystem, unspreadAngles,
unspreadAxes, basering, xsubi, result);
RiReadArchive("barkmaterial", NULL, NULL);
RtToken toks[1] = {RI_P};
RtPointer vals[1] = {&result.p[0]};
int ncorners = result.corners.size();
int ncreases = ncorners / 2;
RtToken *tags = (RtToken *) malloc((ncreases + 2) * sizeof(RtToken));
RtInt *nargs = (RtInt *) malloc((ncreases + 2) * 2 * sizeof(RtInt));
RtInt *intargs = (RtInt *) malloc((ncorners + ncreases * 2) * sizeof(RtInt));
RtFloat *floatargs = (RtFloat *) malloc((1 + ncreases) * sizeof(RtFloat));
RtToken *tagsptr = tags;
RtInt *nargsptr = nargs;
RtInt *intargsptr = intargs;
RtFloat *floatargsptr = floatargs;
*tagsptr++ = "interpolateboundary";
*nargsptr++ = 0;
*nargsptr++ = 0;
*tagsptr++ = "corner";
*nargsptr++ = ncorners;
*nargsptr++ = 1;
for (i = 0; i < ncorners; ++i) {
*intargsptr++ = result.corners[i];
}
*floatargsptr++ = 2.0;
for (i = 0; i < ncreases; ++i) {
*tagsptr++ = "crease";
*nargsptr++ = 2;
*nargsptr++ = 1;
*intargsptr++ = result.corners[2 * i];