Repository navigation
Expand file tree
/
Copy pathTreeObject.cpp
More file actions
214 lines (180 loc) · 4.93 KB
/
Copy pathTreeObject.cpp
File metadata and controls
214 lines (180 loc) · 4.93 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
#include "TreeObject.h"
#include "Sphere.h"
#include "Matrix4.h"
#include "cube.h"
#include <string.h>
#include "Cylinder.h"
#include <unordered_map>
#include <vector>
using namespace std;
TreeObject::TreeObject() {
angle = 30;
depth = 0;
maxDepth = 1;
length = 1;
ratioStep = 1;
treeObj = new Group();
}
TreeObject::TreeObject(int maxDepth, double length, double angle, double ratioStep) {
this->angle = angle;
depth = 0;
this->length = length;
this->maxDepth = maxDepth;
this->ratioStep = ratioStep;
treeObj = new Group();
}
//Parsing the string with the input characters
std::vector<char> TreeObject::convertStringtoVector(string input){
return std::vector<char>(input.begin(), input.end());
}
void TreeObject::setAxiom(string ax) {
axiom = ax;
}
void TreeObject::addRule(char ruleLetter, string rule, double length) {
//vector<char> ruleVector = convertStringtoVector(rule);
//string pair = make_pair(ruleVector, length);
rulesMap.insert(make_pair(ruleLetter, rule));
}
int TreeObject::drawRule(string currentRule) {
vector<Matrix4> transformationStack;
Matrix4 iden = Matrix4();
iden.identity();
transformationStack.push_back(iden);
int errors = 0;
int count = 0;
// Iterating though the rule vector
stack<vector<Matrix4>> matrixStack;
double ratio = 1.0;
double savedRatio = 1;
for (string::iterator it = currentRule.begin(); it != currentRule.end(); ++it) {
if (*it == '[') {
matrixStack.push(transformationStack);
savedRatio = ratio;
}
else if (*it == ']') {
transformationStack = matrixStack.top();
matrixStack.pop();
ratio = savedRatio;
}
else if (!drawChar(*it, depth, transformationStack, ratio)) errors += 1;
count++;
}
return errors == 0;
}
string TreeObject::generateFinalRule(string startRule, int depth) {
if (depth == maxDepth) {
return startRule;
}
string tempString;
for (std::string::iterator it = startRule.begin(); it != startRule.end(); ++it) {
std::unordered_map<char, string>::const_iterator got = rulesMap.find(*it);
//Didn't find it
if (got == rulesMap.end()) {
tempString += *it;
}
// Found it
else {
tempString += rulesMap.at(*it);
}
}
return generateFinalRule(tempString, depth + 1);
}
void TreeObject::setMaxDepth(int depth) {
maxDepth = depth;
}
// Returns 0 if error
int TreeObject::drawChar(char drawLetter, int depth, vector<Matrix4>& transformationStack, double& ratio) {
int shouldDraw = depth == maxDepth;
Matrix4 rotationMatrix;
rotationMatrix.identity();
switch (drawLetter) {
// if space, exit
case ' ':
break;
//Turn left by angle amount
case '+':
rotationMatrix.makeRotateX(angle);
transformationStack.push_back(rotationMatrix);
break;
case '-':
rotationMatrix.makeRotateX(-angle);
transformationStack.push_back(rotationMatrix);
break;
case '&':
rotationMatrix.makeRotateY(angle);
transformationStack.push_back(rotationMatrix);
break;
case '^':
rotationMatrix.makeRotateY(-angle);
transformationStack.push_back(rotationMatrix);
break;
case '>':
rotationMatrix.makeRotateZ(angle);
transformationStack.push_back(rotationMatrix);
break;
case '<':
rotationMatrix.makeRotateZ(-angle);
transformationStack.push_back(rotationMatrix);
break;
default:
ratio *= ratioStep;
drawBranch(convertVectorToMatrix(transformationStack), drawLetter, ratio);
Matrix4 shiftMatrix;
shiftMatrix.identity();
//TODO: change length
shiftMatrix.makeTranslate(0, length * ratio, 0);
transformationStack.push_back(shiftMatrix);
break;
}
return 1;
}
Matrix4 TreeObject::convertVectorToMatrix(vector<Matrix4> transformationStack) {
Matrix4 finalMatrix;
finalMatrix.identity();
for (std::vector<Matrix4>::iterator it = transformationStack.begin(); it != transformationStack.end(); ++it) {
finalMatrix = finalMatrix * (*it);
}
return finalMatrix;
}
MatrixTransform TreeObject::drawBranch(Matrix4 trans, char letter, double ratio) {
Matrix4 rotateCyl;
rotateCyl.makeRotateX(90);
double radius = 1;
trans = trans * rotateCyl;
MatrixTransform* transform = new MatrixTransform(trans);
transform->addChild(new Cylinder(length* ratio, radius, ratio, ratioStep));
treeObj->addChild(transform);
Matrix4 tras;
trans.identity();
//treeObj->draw(trans);
return *transform;
}
void TreeObject::prerender() {
glutIndex = glGenLists(1);
glNewList(glutIndex, GL_COMPILE);
Matrix4 ident = Matrix4();
ident.identity();
vector<Matrix4> transformationStack;
transformationStack.push_back(ident);
string finalRule = generateFinalRule(axiom, depth);
drawRule(finalRule);
treeObj->draw(ident); //Since its prerendered, pass in ident
glEndList();
}
/*
* Sort of hacky method to render the tree object, as it doesnt draw the objects in
* draw but draws it in prerender.
*/
void TreeObject::draw(Matrix4 matrix) {
glPushMatrix();
glMultMatrixd(matrix.getPointer());
glCallList(glutIndex);
glPopMatrix();
}
void TreeObject::update(Matrix4 worldMatrix) {
treeObj->update(worldMatrix);
}
TreeObject::~TreeObject()
{
delete treeObj;
}