-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_cpp.cpp
226 lines (170 loc) · 6.52 KB
/
main_cpp.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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "Cube_Unity.h"
#include "bmp.h"
#define PI 3.1415
Cube_Unity ***Cube;
Cube_Unity ***Cube_Perspective;
int Size;
int width;
//funcao chamada continuamente. Deve-se controlar o que desenhar por meio de variaveis
//globais que podem ser setadas pelo metodo keyboard()
void OffSet_Z(double offset){
for(auto i = 0; i < Size; i++){
for(auto j = 0; j < Size; j++){
for(auto k = 0; k < Size; k++){
Cube[i][j][k].z += offset;
}
}
}
}
void Transform_3D_2D(){
double dist_to_screen = 300;
double new_x, new_y;
for(auto i = 0; i < Size; i++){
for(auto j = 0; j < Size; j++){
for(auto k = 0; k < Size; k++){
new_x = Cube[i][j][k].x*dist_to_screen/Cube[i][j][k].z+width/2;
new_y = Cube[i][j][k].y*dist_to_screen/Cube[i][j][k].z+width/2;
Cube_Perspective[i][j][k].set_pos(new_x, new_y, 0, Cube[i][j][k].cor);
}
}
}
}
void Rotate_x(double angulo){
angulo = angulo * PI/180;
double matrix_x[4][4] = {1, 0, 0, 0,
0, cos(angulo), -sin(angulo), 0,
0, sin(angulo), cos(angulo), 0,
0, 0, 0, 1};
double new_x, new_y, new_z;
for(auto i = 0; i < Size; i++){
for(auto j = 0; j < Size; j++){
for(auto k = 0; k < Size; k++){
new_x = Cube[i][j][k].x * matrix_x[0][0] + Cube[i][j][k].y * matrix_x[0][1] + Cube[i][j][k].z * matrix_x[0][2];
new_y = Cube[i][j][k].x * matrix_x[1][0] + Cube[i][j][k].y * matrix_x[1][1] + Cube[i][j][k].z * matrix_x[1][2];
new_z = Cube[i][j][k].x * matrix_x[2][0] + Cube[i][j][k].y * matrix_x[2][1] + Cube[i][j][k].z * matrix_x[2][2];
Cube[i][j][k].x = new_x;
Cube[i][j][k].y = new_y;
Cube[i][j][k].z = new_z;
}
}
}
}
void Rotate_y(double angulo){
angulo = angulo * PI/180;
double matrix_x[4][4] = { cos(angulo), 0, sin(angulo), 0,
0, 1, 0, 0,
-sin(angulo), 0, cos(angulo), 0,
0, 0, 0, 1};
double new_x, new_y, new_z;
for(auto i = 0; i < Size; i++){
for(auto j = 0; j < Size; j++){
for(auto k = 0; k < Size; k++){
new_x = Cube[i][j][k].x * matrix_x[0][0] + Cube[i][j][k].y * matrix_x[0][1] + Cube[i][j][k].z * matrix_x[0][2];
new_y = Cube[i][j][k].x * matrix_x[1][0] + Cube[i][j][k].y * matrix_x[1][1] + Cube[i][j][k].z * matrix_x[1][2];
new_z = Cube[i][j][k].x * matrix_x[2][0] + Cube[i][j][k].y * matrix_x[2][1] + Cube[i][j][k].z * matrix_x[2][2];
Cube[i][j][k].x = new_x;
Cube[i][j][k].y = new_y;
Cube[i][j][k].z = new_z;
}
}
}
}
void Rotate_z(double angulo){
angulo = angulo * PI/180;
double matrix_x[4][4] = {cos(angulo), -sin(angulo), 0, 0,
sin(angulo), cos(angulo), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
double new_x, new_y, new_z;
for(auto i = 0; i < Size; i++){
for(auto j = 0; j < Size; j++){
for(auto k = 0; k < Size; k++){
new_x = Cube[i][j][k].x * matrix_x[0][0] + Cube[i][j][k].y * matrix_x[0][1] + Cube[i][j][k].z * matrix_x[0][2];
new_y = Cube[i][j][k].x * matrix_x[1][0] + Cube[i][j][k].y * matrix_x[1][1] + Cube[i][j][k].z * matrix_x[1][2];
new_z = Cube[i][j][k].x * matrix_x[2][0] + Cube[i][j][k].y * matrix_x[2][1] + Cube[i][j][k].z * matrix_x[2][2];
Cube[i][j][k].x = new_x;
Cube[i][j][k].y = new_y;
Cube[i][j][k].z = new_z;
}
}
}
}
void render(){
double angulo = 1;
Rotate_z(angulo);
Rotate_y(angulo);
Rotate_x(angulo);
OffSet_Z(800);
Transform_3D_2D();
OffSet_Z(-800);
}
int main(void)
{
Size = 10;
width = 512;
int frames = 1;
double spacing_factor = 40;
double inix, iniy, iniz;
double atualx, atualy, atualz;
double spacing;
inix = iniy = iniz = atualx = atualy = atualz = -double(Size)/2 * spacing_factor;
spacing = -inix*2 / double(Size);
Cube = new Cube_Unity**[Size];
Cube_Perspective = new Cube_Unity**[Size];
for(auto i = 0; i < Size; i++){
Cube[i] = new Cube_Unity*[Size];
Cube_Perspective[i] = new Cube_Unity*[Size];
for(auto j = 0; j < Size; j++){
Cube[i][j] = new Cube_Unity[Size];
Cube_Perspective[i][j] = new Cube_Unity[Size];
for(auto k = 0; k < Size; k++){
Cube[i][j][k].set_pos(atualx,atualy,atualz, 255);
atualz += spacing;
}
atualz = iniz;
atualy += spacing;
}
atualy = iniy;
atualx += spacing;
}
// allocate picture array
unsigned char* pic = new unsigned char[frames * width * width];
for (int frame = 0; frame < frames; frame++) {
for (int row = 0; row < width; row++) {
for (int col = 0; col < width; col++) {
unsigned char color = (unsigned char) 255;
pic[frame * width * width + row * width + col] = (unsigned char) color;
}
}
}
// start time
timeval start, end;
gettimeofday(&start, NULL);
for(int frame = 0; frame < frames; frame++){
render();
for(auto i = 0; i < Size; i++){
for(auto j = 0; j < Size; j++){
for(auto k = 0; k < Size; k++){
int row = (int)Cube_Perspective[i][j][k].x;
int col = (int)Cube_Perspective[i][j][k].y;
if(row >= 0 && row < width && col >= 0 && col < width){
unsigned char color = (unsigned char) 0;
pic[frame * width * width + row * width + col] = (unsigned char) color;
}
}
}
}
}
gettimeofday(&end, NULL);
double runtime = end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0;
std::cout << "compute time: " << runtime << " s\n";
for (int frame = 0; frame < frames; frame++) {
char name[32];
sprintf(name, "cube%d.bmp", frame + 1000);
writeBMP(width, width, &pic[frame * width * width], name);
}
}