-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.cpp
More file actions
368 lines (311 loc) · 12.8 KB
/
scene.cpp
File metadata and controls
368 lines (311 loc) · 12.8 KB
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
#include "scene.h"
#include <QDebug>
Scene* Scene::activeScene = 0;
SceneObject::SceneObject(SceneObject *parent) : parent(parent)
{
children = new QList<SceneObject*>;
rotation = new QVector3D;
translation = new QVector3D;
}
BranchSection::BranchSection(SceneObject *parent, double radBottom, double radTop, double length) :
SceneObject(parent), radBottom(radBottom), radTop(radTop), length(length)
{
}
EndSection::EndSection(SceneObject *parent, double radius) : SceneObject(parent), radius(radius)
{
}
Leaf::Leaf(SceneObject *parent, double width, double length) : SceneObject(parent),
width(width), length(length)
{
}
void BranchSection::render()
{
if (this->age <= Plant::activePlant->growthAge) {
GLUquadricObj *qobj;
qobj = gluNewQuadric();
Scene::activeScene->branches++;
// not really needed
// gluQuadricCallback(qobj, GLU_ERROR, NULL);
// gluQuadricDrawStyle(qobj, GLU_FILL);
// gluQuadricNormals(qobj, GLU_SMOOTH);
// build all along the positive z-axis
QColor branch = Plant::activePlant->branchColor;
glColor3f(branch.redF(), branch.greenF(), branch.blueF());
gluCylinder(qobj, radBottom, radTop, length,
Plant::activePlant->slices, Plant::activePlant->segments);
}
}
void EndSection::render()
{
if (this->age <= Plant::activePlant->growthAge) {
GLUquadricObj *qobj;
qobj = gluNewQuadric();
// build an end cap
QColor branch = Plant::activePlant->branchColor;
glColor3f(branch.redF(), branch.greenF(), branch.blueF());
gluSphere(qobj, radius,
Plant::activePlant->slices, Plant::activePlant->segments);
Scene::activeScene->spheres++;
}
}
void Leaf::render()
{
if (this->age <= Plant::activePlant->growthAge) {
QColor prim = Plant::activePlant->primLeafColor;
QColor sec = Plant::activePlant->secLeafColor;
glFrontFace(GL_CW);
int leafStemLength = 0.5f*length;
// the leaf's stem
glColor3f(prim.redF(), prim.greenF(), prim.blueF());
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0, 1, 0);
glVertex3f(-0.1f*width/10, 0.0f, 0.0f);
glNormal3f(0, 1, 0);
glVertex3f(0.0f, 0.0f, leafStemLength);
glNormal3f(0, 1, 0);
glVertex3f(0.1f*width/10, 0.0f, 0.0f);
glEnd();
// left half of leaf
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0, 1, 0);
glColor3f(prim.redF(), prim.greenF(), prim.blueF());
glVertex3f(-0.5f*width, 0.0f, leafStemLength+0.25f*length);
glNormal3f(0, 1, 0);
glColor3f(sec.redF(), sec.greenF(), sec.blueF());
glVertex3f(-0.5f*width, 0.0f, leafStemLength+0.75f*length);
glNormal3f(0, 1, 0);
glColor3f(prim.redF(), prim.greenF(), prim.blueF());
glVertex3f(0.0f, 0.0f, leafStemLength);
glNormal3f(0, 1, 0);
glColor3f(sec.redF(), sec.greenF(), sec.blueF());
glVertex3f(0.0f, 0.0f, leafStemLength+length);
glEnd();
// right half of leaf
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0, 1, 0);
glColor3f(prim.redF(), prim.greenF(), prim.blueF());
glVertex3f(0.5f*width, 0.0f, leafStemLength+0.25f*length);
glNormal3f(0, 1, 0);
glColor3f(prim.redF(), prim.greenF(), prim.blueF());
glVertex3f(0.0f, 0.0f, leafStemLength);
glNormal3f(0, 1, 0);
glColor3f(sec.redF(), sec.greenF(), sec.blueF());
glVertex3f(0.5f*width, 0.0f, leafStemLength+0.75f*length);
glNormal3f(0, 1, 0);
glColor3f(sec.redF(), sec.greenF(), sec.blueF());
glVertex3f(0.0f, 0.0f, leafStemLength+length);
glEnd();
Scene::activeScene->leaves++;
}
}
Scene::Scene(QWidget *oglPanel) : oglPanel(oglPanel) {
initScene();
}
void Scene::initScene() {
// no plant: no scene root
if (Plant::activePlant == 0) {
root = 0;
return;
}
// reset seed before drawing
Plant::activePlant->reseed();
// create a root starting at age 0
SceneObject *root;
int minAge = 0;
root = new BranchSection(0, Plant::activePlant->getBranchThicknessAt(minAge),
Plant::activePlant->getBranchThicknessAt(minAge + 1),
Plant::activePlant->getBranchLengthAt(minAge));
// try creating the successor
QList<SceneObject*> *children = createSceneObject(Plant::activePlant, root, minAge + 1);
// append all children
root->children->append(*children);
// set root for scene
Scene::root = root;
// call redraw
update();
}
void Scene::update() {
((QGLWidget*)oglPanel)->update();
}
QList<SceneObject*> *Scene::createSceneObject(Plant *plant, SceneObject *parent, int age)
{
// end of recursion, max age reached
if (age > plant->maxAge) return new QList<SceneObject*>;
// apply an endcap to top branches
if (age == plant->maxAge) {
QList<SceneObject*> *end = new QList<SceneObject*>;
if (plant->drawCaps) {
SceneObject *cap = constructEndSection(plant, parent, age, plant->drawLeaves);
end->append(cap);
}
return end;
}
// will hold all possible branches of this age in addition to the main branch (if necessary)
QList<SceneObject*> *children = new QList<SceneObject*>;
// is tree branching here?
bool isBranchingAtAll = plant->isBranchingAt(age);
int possibleBranches = plant->getBranchCountAt(age);
int actualBranches = possibleBranches;
if (isBranchingAtAll) {
for (int i=0; i < possibleBranches; i++) {
// should a possible branch actually grow?
if (!plant->isBranchingAt(age)) {
actualBranches--;
continue;
}
// create new branch
int branchAge = age;
bool isGrowthInterrupted = plant->isGrowthInterruptingAt(age);
int delay = plant->getGrowthInterruptionAt(age);
int parentRadius = ((BranchSection*)parent)->radTop;
if (isGrowthInterrupted) {
// growth is delayed. all following objects start with higher age
branchAge += delay;
parentRadius = plant->getBranchThicknessAt(branchAge - 1);
if (branchAge > plant->maxAge) {
// if growth interruption results in branches > maxAge
// then there is no branch as well
actualBranches--;
continue;
}
}
SceneObject *branch = constructBranchSection(plant, parent, parentRadius, branchAge);
// apply off-the-divided-axis rotation to branch:
int randRotationAngle = plant->coinflip() * plant->getBranchingRotationAt(branchAge);
branch->rotation = new QVector3D(0, plant->getBranchingAngle(branchAge),
360/actualBranches * i + randRotationAngle);
// let the branch grow further with possible interruption (recursion)
QList<SceneObject*> *nextBranchChildren = createSceneObject(plant, branch, branchAge + 1);
// append the next branches' children to this branch
branch->children->append(*nextBranchChildren);
// create leaves for this branch if any
if (plant->drawLeaves)
{
QList<SceneObject*> *leaves = createLeaves(plant, branch, branchAge);
branch->children->append(*leaves);
}
// append this branch to the list of children
children->append(branch);
}
}
// continue main branch if needed
if (plant->continueMainBranchAt(age)) {
SceneObject *mainBranch = constructBranchSection(plant, parent, ((BranchSection*)parent)->radTop, age);
// create all possible next children of the continued main branch
QList<SceneObject*> *nextChildren = createSceneObject(plant, mainBranch, age + 1);
mainBranch->children->append(*nextChildren);
// create leaves for the mainbranch if any
if (plant->drawLeaves)
{
QList<SceneObject*> *leaves = createLeaves(plant, mainBranch, age);
mainBranch->children->append(*leaves);
}
// append the continued main branch to the parent's children list
children->append(mainBranch);
// put a cap to the branches end to smooth string wobbliness and branching angles
if (plant->drawConnectors) {
SceneObject *cap = constructEndSection(plant, parent, age, false);
children->append(cap);
}
}
// append only an end cap otherwise
else {
if (plant->drawCaps) {
SceneObject *current = constructEndSection(plant, parent, age, plant->drawLeaves);
children->append(current);
}
}
return children;
}
SceneObject* Scene::constructBranchSection(Plant* plant, SceneObject* parent, int parentRadius, int age)
{
SceneObject *current = new BranchSection(parent, parentRadius,
plant->getBranchThicknessAt(age + 1),
plant->getBranchLengthAt(age));
current->age = age;
// // do not render if age is larger than deserved growth
// if (age <= Scene::activeScene->drawUntilAge) {
// current->isRendered = false;
// } else {
// // for statistical value only
// branches++;
// }
// apply wobbliness
if (plant->isBranchWobblinessAt(age)) {
QVector3D *wobble = new QVector3D(plant->getBranchWobblinessAt(age), 0,
plant->getRandomRotationBetween(1, 360));
current->rotation = wobble;
}
current->translation = new QVector3D(0, 0, ((BranchSection*)parent)->length);
return current;
}
SceneObject* Scene::constructEndSection(Plant *plant, SceneObject* parent, int age, bool hasLeaf)
{
// add a cap to a parent
SceneObject *end = new EndSection(parent, ((BranchSection*)parent)->radTop);
end->age = age;
end->translation = new QVector3D(0, 0, ((BranchSection*)parent)->length);
// // do not render if age is larger than deserved growth
// if (age > plant->growthAge) {
// end->isRendered=false;
// } else {
// // for statistical value only
// spheres++;
// }
// if the cap should sport a single leaf at its top (like at the end of branches, do so
if (hasLeaf) {
// move translation and rotation away from the object itself, to make leaves universally usable
QVector3D *translation = new QVector3D(0, 0, ((EndSection*)end)->radius);
QVector3D *rotation = new QVector3D();
SceneObject *leaf = constructLeaf(plant, end, age, translation, rotation);
end->children->append(leaf);
}
return end;
}
QList<SceneObject*> *Scene::createLeaves(Plant *plant, SceneObject *parent, int age)
{
QList<SceneObject*> *leaves = new QList<SceneObject*>;
int levels = plant->getLeafLevelsAt(age);
int countPerLevel = plant->getLeafCountPerLevelAt(age);
int angle = plant->getLeafAngleAt(age);
// no leaves
if (levels == 0 || countPerLevel == 0) return leaves;
// base rotation around the branch for each level
double baseRotation = 360.0 / (double)levels;
// adjust base rotation by number of leaves per level
baseRotation = baseRotation / (double)countPerLevel;
// how many levels
for (int level=0; level < levels; level++) {
// rotation for this level
double levelRotation = 360.0 / countPerLevel;
// level translation along z-axis
double zTranslation = ((BranchSection*)parent)->length / (levels + 1) * (level + 1);
// a base translation towards outside of the branch
double radius = ((BranchSection*)parent)->radTop;
// how many per level
for (int count=0; count < countPerLevel; count++) {
QVector3D *rotation = new QVector3D(angle, 0, levelRotation * count + baseRotation * level);
QVector3D *translation = new QVector3D(radius * sin(rotation->z() * PI_180), -radius * cos(rotation->z() * PI_180), zTranslation);
SceneObject *leaf = constructLeaf(plant, parent, age, translation, rotation);
leaves->append(leaf);
}
}
// all leaves done
return leaves;
}
SceneObject* Scene::constructLeaf(Plant *plant, SceneObject* parent, int age, QVector3D *translation, QVector3D *rotation)
{
// construct and return a leaf
SceneObject *leaf = new Leaf(parent, plant->getLeafWidthAt(age), plant->getLeafLengthAt(age));
leaf->age = age;
// // do not render if age is larger than deserved growth
// if (age > plant->growthAge) {
// leaf->isRendered=false;
// } else {
// // for statistical value only
// leaves++;
// }
leaf->translation = translation;
leaf->rotation = rotation;
return leaf;
}