-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProceduralTree.cpp
544 lines (504 loc) · 18.5 KB
/
ProceduralTree.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
// @Begin License@
// This file is part of Coldest.
//
// Coldest 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 3 of the License, or
// (at your option) any later version.
//
// Coldest 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 Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "ProceduralTree.h"
/*
Ideas:
-Define curves of branches
*/
ProceduralTree::ProceduralTree()
{
numlevels = 2;
numslices = 3;
numbranches[0] = 5;
numbranches[1] = 2;
numbranches[2] = 3;
maxangle = 70;
minangle = 20;
maxbranchangle = 5;
maxtrunkangle = 5.f;
initrad = 5;
radreductionperc = .2;
initheight = 40;
heightreductionperc = .7;
firstleaflevel = 2;
leafsize = 20;
numsegs = 3;
numleaves = 1;
trunkrad = 5;
trunknumslices = 10;
trunktaper = .9;
trunknumsegs = 8;
branchafter = 1;
sidebranches = 0;
minsidebranchangle = -10;
maxsidebranchangle = 10;
sidesizeperc = .75f;
minheightvar = .75;
maxheightvar = 1.25;
sidetaper = .7;
curvecoeff = 0.f;
split = true;
continuebranch = false;
multitrunk = false;
branchwithleaves = false;
leafsegs = 2;
leafcurve = 3.f;
seed = 0;
}
// Returns the number of primitives generated
size_t ProceduralTree::GenTree(Mesh* currmesh, Material* barkmat, Material* leavesmat)
{
mesh = currmesh;
bark = barkmat;
leaves = leavesmat;
totalprims = 0;
random.Seed(seed);
GraphicMatrix m;
Vector3 temp;
vector<Vector3> pts;
Vector3 mrot = mesh->GetRotation();
float sliceangle = 360. / trunknumslices;
for (int j = 0; j < trunknumslices; ++j)
{
m = GraphicMatrix();
temp = Vector3();
m.translate(trunkrad, 0, 0);
m.rotatey(-sliceangle * j);
m.rotatex(mrot.x);
m.rotatey(mrot.y);
m.translate(mesh->GetPosition());
temp.transform(m);
pts.push_back(temp);
}
m = GraphicMatrix();
m.rotatex(mrot.x);
m.rotatey(mrot.y);
m.translate(mesh->GetPosition());
int i = 0;
if (!multitrunk)
i = numbranches[0] - 1;
for (; i < numbranches[0]; ++i)
GenBranch(m, 0, 0, pts, 0);
currmesh->GenTangents();
currmesh->Update();
// Center the mesh location
currmesh->Move(currmesh->GetPosition() + Vector3(0, currmesh->GetHeight() / 2.f, 0));
// Force reset of dimensions
currmesh->Update();
return totalprims;
}
void ProceduralTree::GenBranch(GraphicMatrix trans, int lev, int seg, vector<Vector3> oldpts, int side)
{
if (lev > numlevels) return;
vector<Vector3> newpts;
vector<Vector3> normals;
vector<Vector3> oldnorms;
float anglex, angley, anglez;
float startrad = 10.f, endrad = 10.f;
float locnumsegs;
int savenumslices = numslices;
locnumsegs = numsegs;
if (lev == 0) // Trunk radius and slices are handled differently
{
startrad = trunkrad * pow(trunktaper, seg + 1);
endrad = trunkrad * pow(trunktaper, seg + 2);
numslices = trunknumslices;
locnumsegs = trunknumsegs;
}
else
{
if (!side)
{
startrad = initrad * pow(radreductionperc, lev);
endrad = initrad * pow(radreductionperc, lev + 1);
}
else if (lev == 1) // Won't work for branches not off the trunk
{
startrad = trunkrad * pow(trunktaper, seg + 1);
endrad = trunkrad * pow(trunktaper, seg + 2);
}
}
float radius = ((locnumsegs - seg) * startrad + seg * endrad) / locnumsegs;
float height;
height = initheight * pow(heightreductionperc, lev);
height = random.Random(height * minheightvar, height * maxheightvar);
height /= locnumsegs;
if (side)
{
startrad = radius * sidesizeperc;
endrad = startrad * radreductionperc;
radius = ((locnumsegs - side) * startrad + side * endrad) / locnumsegs;
height *= pow(sidetaper, seg);
}
float sliceangle = 360. / numslices;
int locnumbranches;
locnumbranches = numbranches[lev];
Vector3 temp;
GraphicMatrix m;
anglex = angley = anglez = 0;
if (seg == 0 && lev != 0 && !side) // Angle of new normal branch
{
anglex = random.Random(minangle, maxangle);
if (random.Random(0, 1) > .5) anglex *= -1;
angley = 0;
anglez = random.Random(minangle, maxangle);
if (random.Random(0, 1) > .5) anglez *= -1;
}
else if (side == 1) // Angle of new side branch
{
anglex = 90 + random.Random(minsidebranchangle, maxsidebranchangle);
angley = random.Random(0, 360);
anglez = 0;
}
else if (lev != 0) // Continued angle of current branch
{
anglex = random.Random(-maxbranchangle, maxbranchangle);
angley = 0;
anglez = random.Random(-maxbranchangle, maxbranchangle);
}
else // Trunk angle
{
anglex = random.Random(-maxtrunkangle, maxtrunkangle);
angley = 0;
anglez = random.Random(-maxtrunkangle, maxtrunkangle);
}
if (branchwithleaves || lev < firstleaflevel)
{
for (int j = 0; j < numslices; ++j)
{
m.identity();
temp = Vector3();
m.translate(radius, height, 0);
m.rotatey(-sliceangle * j);
m.rotatex(anglex);
m.rotatey(angley);
m.rotatez(anglez);
m *= trans;
int square = side ? side : seg;
if (lev != 0)
m.translate(0, curvecoeff * (float)(square * square) * height, 0);
temp.transform(m);
newpts.push_back(temp);
Vector3 norm = Vector3();
m.identity();
m.translate(0, height, 0);
m.rotatey(-sliceangle * j);
m.rotatex(anglex);
m.rotatey(angley);
m.rotatez(anglez);
m *= trans;
if (lev != 0)
m.translate(0, curvecoeff * (float)(square * square) * height, 0);
norm.transform(m);
Vector3 tempn = temp - norm;
tempn.normalize();
normals.push_back(tempn);
}
if (side == 1) // Then we don't care about oldpts
{
oldpts.clear();
for (int j = 0; j < numslices; ++j)
{
m.identity();
temp = Vector3();
m.translate(radius, 0, 0);
m.rotatey(-sliceangle * j);
m.rotatex(anglex);
m.rotatey(angley);
m.rotatez(anglez);
m *= trans;
temp.transform(m.members);
oldpts.push_back(temp);
}
}
for (size_t j = 0; j < oldpts.size(); ++j)
{
temp = Vector3();
temp.transform(trans);
Vector3 norm = oldpts[j] - temp;
norm.normalize();
oldnorms.push_back(norm);
}
// Generate primitives from our points
if (lev == 0)
{
Vector3 colstart, colend;
for (size_t j = 0; j < oldpts.size(); ++j)
{
colstart += oldpts[j];
}
colstart /= oldpts.size();
for (size_t j = 0; j < newpts.size(); ++j)
{
colend += newpts[j];
}
colend /= newpts.size();
TrianglePtr coltri = TrianglePtr(new Triangle());
coltri->v[0]->pos = colstart;
coltri->v[1]->pos = (colstart + colend) / 2.f;
coltri->v[2]->pos = colend;
coltri->radmod = radius;
coltri->collide = true;
coltri->material = bark;
mesh->Add(*coltri);
}
int newind, newind1;
for (size_t j = 0; j < oldpts.size(); ++j)
{
Quad tempq;
#ifndef DEDICATED
tempq.SetMaterial(bark);
#endif
newind = (int)(j * ((float)numslices / (float)oldpts.size()));
newind1 = (int)((j + 1) * ((float)numslices / (float)oldpts.size()));
tempq.SetVertex(0, newpts[newind]);
tempq.SetVertex(1, oldpts[j]);
tempq.SetVertex(2, oldpts[(j + 1) % oldpts.size()]);
tempq.SetVertex(3, newpts[newind1 % numslices]);
tempq.SetNormal(0, normals[newind]);
tempq.SetNormal(1, oldnorms[j]);
tempq.SetNormal(2, oldnorms[(j + 1) % oldpts.size()]);
tempq.SetNormal(3, normals[newind1 % numslices]);
floatvec tc(2, 0.f);
int currseg = side ? side - 1 : seg;
tc[0] = float(j) / float(oldpts.size());
tc[1] = 1.f - float(currseg + 1) / float(locnumsegs);
tempq.SetTexCoords(0, 0, tc);
tc[1] = 1.f - float(currseg) / float(locnumsegs);
tempq.SetTexCoords(1, 0, tc);
tc[0] = float(j + 1) / float(oldpts.size());
tempq.SetTexCoords(2, 0, tc);
tc[1] = 1.f - float(currseg + 1) / float(locnumsegs);
tempq.SetTexCoords(3, 0, tc);
//if (lev == 0)
// tempq.SetCollide(true);
mesh->Add(tempq);
++totalprims;
}
}
/* If this was a trunk piece we're done with numslices so set it back
for the branches*/
numslices = savenumslices;
// Generate leaves if necessary
if (lev >= firstleaflevel && (!seg || side == 1))
{
vector<VertexPtrvec> verts(leafsegs + 1, VertexPtrvec(leafsegs + 1));
float leafangle = 0.f;//180. / numleaves + random.Random(-45.f, 45.f);
//float leafscale = 1.5; unused
//float leafheight = height * (locnumsegs - seg) * leafscale; unused
//float overlap = 5; unused
for (int i = 0; i < numleaves; ++i)
{
for (int x = -leafsegs / 2; x < leafsegs / 2; ++x)
{
for (int y = -leafsegs / 2; y < leafsegs / 2; ++y)
{
float x1 = float(x + 1);
float y1 = float(y + 1);
float curveoffset = leafcurve * (float(x * x) + float(y * y));
float curveoffset1 = leafcurve * (float(x1 * x1) + float(y * y));
float curveoffset2 = leafcurve * (float(x * x) + float(y1 * y1));
float curveoffset3 = leafcurve * (float(x1 * x1) + float(y1 * y1));
Quad tempq;
tempq.SetMaterial(leaves);
tempq.SetCollide(false);
VertexPtrvec vertptrs(4);
for (int j = 0; j < 4; ++j)
{
temp = Vector3();
VertexPtr newvert(new Vertex());
m.identity();
switch(j)
{
case 0:
m.translate(-leafsize + (leafsize * 2.f * float(x + leafsegs / 2) / float(leafsegs)),
-leafsize + (leafsize * 2.f * float(y + 1 + leafsegs / 2) / float(leafsegs)),
radius + curveoffset2);
newvert->texcoords[0][0] = 1.f - float(x + leafsegs / 2) / float(leafsegs);
newvert->texcoords[0][1] = 1.f - float(y + 1 + leafsegs / 2) / float(leafsegs);
break;
case 1:
m.translate(-leafsize + (leafsize * 2.f * float(x + leafsegs / 2) / float(leafsegs)),
-leafsize + (leafsize * 2.f * float(y + leafsegs / 2) / float(leafsegs)),
radius + curveoffset);
newvert->texcoords[0][0] = 1.f - float(x + leafsegs / 2) / float(leafsegs);
newvert->texcoords[0][1] = 1.f - float(y + leafsegs / 2) / float(leafsegs);
break;
case 2:
m.translate(-leafsize + (leafsize * 2.f * float(x + 1 + leafsegs / 2) / float(leafsegs)),
-leafsize + (leafsize * 2.f * float(y + leafsegs / 2) / float(leafsegs)),
radius + curveoffset1);
newvert->texcoords[0][0] = 1.f - float(x + 1 + leafsegs / 2) / float(leafsegs);
newvert->texcoords[0][1] = 1.f - float(y + leafsegs / 2) / float(leafsegs);
break;
case 3:
m.translate(-leafsize + (leafsize * 2.f * float(x + 1 + leafsegs / 2) / float(leafsegs)),
-leafsize + (leafsize * 2.f * float(y + 1 + leafsegs / 2) / float(leafsegs)),
radius + curveoffset3);
newvert->texcoords[0][0] = 1.f - float(x + 1 + leafsegs / 2) / float(leafsegs);
newvert->texcoords[0][1] = 1.f - float(y + 1 + leafsegs / 2) / float(leafsegs);
break;
};
m.rotatey(leafangle * i);
m.rotatex(anglex);
m.rotatey(angley);
m.rotatez(anglez);
m *= trans;
temp.transform(m);
newvert->pos = temp;
switch (j)
{
case 0:
if (!verts[x + leafsegs / 2][y + 1 + leafsegs / 2])
verts[x + leafsegs / 2][y + 1 + leafsegs / 2] = newvert;
vertptrs[j] = verts[x + leafsegs / 2][y + 1 + leafsegs / 2];
break;
case 1:
if (!verts[x + leafsegs / 2][y + leafsegs / 2])
verts[x + leafsegs / 2][y + leafsegs / 2] = newvert;
vertptrs[j] = verts[x + leafsegs / 2][y + leafsegs / 2];
break;
case 2:
if (!verts[x + 1 + leafsegs / 2][y + leafsegs / 2])
verts[x + 1 + leafsegs / 2][y + leafsegs / 2] = newvert;
vertptrs[j] = verts[x + 1 + leafsegs / 2][y + leafsegs / 2];
break;
case 3:
if (!verts[x + 1 + leafsegs / 2][y + 1 + leafsegs / 2])
verts[x + 1 + leafsegs / 2][y + 1 + leafsegs / 2] = newvert;
vertptrs[j] = verts[x + 1 + leafsegs / 2][y + 1 + leafsegs / 2];
break;
};
}
for (int j = 0; j < 4; ++j)
{
if (!(x % 2) && !(y % 2))
tempq.SetVertexPtr(j, vertptrs[j]);
else tempq.SetVertexPtr(j, vertptrs[(j + 1) % 4]);
}
// Generate normals - note that this is not completely accurate because
// it forces all of the quads to share a single quads normal rather than
// averaging them, but it's close enough IMHO
Vector3 temp1 = vertptrs[1]->pos - vertptrs[0]->pos;
Vector3 temp2 = vertptrs[2]->pos - vertptrs[0]->pos;
Vector3 tempn = temp1.cross(temp2);
tempn.normalize();
for (int n = 0; n < 4; n++)
{
tempq.SetNormal(n, tempn);
}
mesh->Add(tempq);
++totalprims;
}
}
}
}
if (!side && seg && (seg >= branchafter)) // Side branches
{
for (int i = 0; i < sidebranches; ++i)
{
// Generate branches anywhere along the segment, not just the joint
m.identity();
m.translate(0, random.Random(0.f, height), 0);
m.rotatex(anglex);
m.rotatey(angley);
m.rotatez(anglez);
m *= trans;
GenBranch(m, lev + 1, seg, newpts, 1);
}
}
// Regenerate matrix to point at center of the open end of the cylinder
m.identity();
m.translate(0, height, 0);
m.rotatex(anglex);
m.rotatey(angley);
m.rotatez(anglez);
m *= trans;
int square = side ? side : seg;
if (lev != 0)
m.translate(0, curvecoeff * (float)(square * square) * height, 0);
if (!side)
{
if (seg >= locnumsegs - 1 && split) // Split ends:-)
{
for (int i = 0; i < locnumbranches; ++i)
GenBranch(m, lev + 1, 0, newpts, 0);
}
else if (seg < locnumsegs) // This branch, next segment
GenBranch(m, lev, seg + 1, newpts, 0);
}
else
{
if (side < locnumsegs) // Side branch next segment (?)
GenBranch(m, lev, seg, newpts, side + 1);
else if (split)
{
for (int i = 0; i < locnumbranches; ++i)
GenBranch(m, lev + 1, 0, newpts, 0);
}
}
if (continuebranch && seg >= locnumsegs) // Continue, probably pretty useless
GenBranch(m, lev + 1, 0, newpts, 0);
}
// Reads tree parameters from the designated IniReader
void ProceduralTree::ReadParams(const NTreeReader &get)
{
get.Read(barkfile, "Materials", 0);
get.Read(leavesfile, "Materials", 1);
get.Read(numlevels, "numlevels");
get.Read(numslices, "numslices");
get.Read(numbranches[0], "numbranches0");
get.Read(numbranches[1], "numbranches1");
get.Read(numbranches[2], "numbranches2");
get.Read(numbranches[3], "numbranches3");
get.Read(numbranches[4], "numbranches4");
get.Read(maxangle, "maxangle");
get.Read(minangle, "minangle");
get.Read(maxbranchangle, "maxbranchangle");
get.Read(maxtrunkangle, "maxtrunkangle");
get.Read(initrad, "initrad");
get.Read(radreductionperc, "radreductionperc");
get.Read(initheight, "initheight");
get.Read(heightreductionperc, "heightreductionperc");
get.Read(firstleaflevel, "firstleaflevel");
get.Read(leafsize, "leafsize");
get.Read(numsegs, "numsegs");
get.Read(numleaves, "numleaves");
get.Read(trunkrad, "trunkrad");
get.Read(trunknumslices, "trunknumslices");
get.Read(trunktaper, "trunktaper");
get.Read(trunknumsegs, "trunknumsegs");
get.Read(maxtrunkangle, "maxtrunkangle");
get.Read(branchafter, "branchafter");
get.Read(sidebranches, "sidebranches");
get.Read(minsidebranchangle, "minsidebranchangle");
get.Read(maxsidebranchangle, "maxsidebranchangle");
get.Read(sidesizeperc, "sidesizeperc");
get.Read(split, "split");
get.Read(continuebranch, "continuebranch");
get.Read(multitrunk, "multitrunk");
get.Read(branchwithleaves, "branchwithleaves");
get.Read(sidetaper, "sidetaper");
get.Read(minheightvar, "minheightvar");
get.Read(maxheightvar, "maxheightvar");
get.Read(curvecoeff, "curvecoeff");
get.Read(leafsegs, "leafsegs");
get.Read(leafcurve, "leafcurve");
get.Read(seed, "seed");
}