-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeTree.cpp
More file actions
46 lines (38 loc) · 1.18 KB
/
MakeTree.cpp
File metadata and controls
46 lines (38 loc) · 1.18 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
void drawPolygonPrism(int n, float base, float height) {
glPushMatrix();
glRotatef(-90, 1.0, 0.0, 0.0);
// Cylinder "Cover"
glBegin(GL_QUAD_STRIP);
glColor3f(83 / 255.0, 53 / 255.0, 10 / 255.0);
for (int i = 0; i < 480; i += (360 / n)) {
//degrees to radians
float a = i * M_PI / 180;
glVertex3f(base * cos(a), base * sin(a), 0.0);
glVertex3f(base * cos(a), base * sin(a), height);
}
glEnd();
glPopMatrix();
}
//min height is 1; base can be anything; recursive function until height is < 1;
void makeTree(float height, float base) {
float angleIntensity = height/10;
int branchNumber;
//number of sides; base size; height size;
drawPolygonPrism(10,base,height);
//shift branches up
glTranslatef(0.0, height, 0.0);
height -= 1;
base -= base*0.3;
//random branch number
branchNumber = rand() % 3 + 1;
for (int x = 0; x < branchNumber; x++) {
if (height > 1) {
glPushMatrix();
//x and z rotation of tree branches
glRotatef(((rand() % 60) - 30) * angleIntensity, 1, 0, 0);
glRotatef(((rand() % 60) - 30) * angleIntensity, 0, 0, 1);
makeTree(height, base);
glPopMatrix();
}
}
}