-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
223 lines (188 loc) · 4.66 KB
/
Copy pathmain.cpp
File metadata and controls
223 lines (188 loc) · 4.66 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
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Branch.h"
#define WINDOW_TITLE "Video"
#define WINDOW_W 1280
#define WINDOW_H 720
#define Z_NEAR 0.001
#define Z_FAR 120
#define C_FOVY 60
#define PI 3.14159265359
#define FULL_SCREEN 1
#define PLOT_AXIS 0
using namespace std;
int width = WINDOW_W;
int height = WINDOW_H;
double z_near = Z_NEAR;
double z_far = Z_FAR;
double c_fovy = C_FOVY;
double c_aspect = (double)WINDOW_W/WINDOW_H;
double dist = 12;
double cam_x = 0;
double cam_y = 0;
double cam_z = dist;
double theta = 90;
double phi = 90;
int aux = 0;
double aux1 = 0;
double aux2 = 0;
double r1 = 0;
double r2 = 0;
double r3 = 0;
double max_angle = 30;
int depth = 10;
int num_childs = 4;
int renderBranch(Branch * branch);
double cosd(double ang);
double sind(double ang);
void mouseMove(int x, int y);
void iniGl();
void updateValues();
void keyPressed(unsigned char key, int x, int y);
void plotAxis();
void RenderScene();
Branch galho;
int main(int argc, char **argv){
galho.init(max_angle,depth,num_childs,0.5);
glutInit(&argc, argv);
iniGl();
glutMainLoop();
galho.clean();
return 1;
}
int renderBranch(Branch* branch){
glPushMatrix();
glRotatef(branch->angle1,0,1,0);
glRotatef(branch->angle2,1,0,0);
glColor3f(branch->r,branch->g,branch->b);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,0,branch->length);
glEnd();
if(!(branch->order < 1)){
for(int i = 0; i < branch->numChilds; i++){
glPushMatrix();
glTranslatef(0,0,branch->length*((branch->child)+i)->spot);
renderBranch((branch->child)+i);
glPopMatrix();
}
}
glPopMatrix();
return 0;
}
double cosd(double ang){
return cos(PI*ang/180);
}
double sind(double ang){
return sin(PI*ang/180);
}
void mouseMove(int x, int y){
theta = 360*((double)x/WINDOW_W-0.5);
phi = 360*((double)y/WINDOW_H-0.5);
if (theta < 0)
theta += 360;
else if(theta > 360)
theta -= 360;
if (phi < 0)
phi += 360;
else if(phi > 360)
phi -= 360;
}
void iniGl(){
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow(WINDOW_TITLE);
glutKeyboardFunc(&keyPressed);
glutDisplayFunc(&RenderScene);
glutPassiveMotionFunc(&mouseMove);
glutIdleFunc(&RenderScene);
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
gluPerspective(c_fovy,c_aspect,z_near,z_far);
glEnable(GL_DEPTH_TEST);
if (FULL_SCREEN)
glutFullScreen();
}
void updateValues(){
cam_x = dist*sind(theta)*sind(phi);
cam_y = dist*cosd(theta)*sind(phi);
cam_z = dist*cosd(phi);
aux = 2*(phi>180)-1;
}
void keyPressed(unsigned char key, int x, int y){
double update = true;
switch (key){
case 27:
exit(0);
break;
case 'a':
max_angle += 1;
break;
case 'z':
max_angle -= 1;
break;
case 's':
depth += 1;
break;
case 'x':
depth -= 1;
break;
case 'd':
num_childs += 1;
break;
case 'c':
num_childs -= 1;
break;
case 'f':
dist *= 1.1;
update = false;
break;
case 'v':
dist /= 1.1;
update = false;
break;
default:
break;
}
if(depth < 1)
depth = 1;
if(num_childs < 1)
num_childs = 1;
if(update){
galho.clean();
galho.init(max_angle,depth,num_childs,0.5);
}
}
void plotAxis(){
glColor3f(1,0.5,0.5);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glEnd();
glColor3f(0.5,1,0.5);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glEnd();
glColor3f(0.5,0.5,1);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
}
void RenderScene(){
updateValues();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(cam_x,cam_y,cam_z,0,0,0, 0,0,aux);
renderBranch(&galho);
if (PLOT_AXIS)
plotAxis();
glPopMatrix();
glutSwapBuffers();
}