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
|
#include <iostream>
#include <ncurses.h>
#include <cmath>
#include "chfb.h"
#include "obj3d.h"
#include "math_vector.h"
#include "vertex.h"
#include "vertex_data.h"
#include "tri_vertex.h"
#define MKEY_Z 122
#define MKEY_Q 113
#define MKEY_S 115
#define MKEY_D 100
#define MKEY_ESC 27
#define FPS 60
#define PI 3.1415926535f
int main() {
// init
setlocale(LC_ALL, "");
initscr();
cbreak();
noecho();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
set_escdelay(0);
curs_set(0);
int w, h;
getmaxyx(stdscr, h, w);
CharacterFrameBuffer cfb{static_cast<unsigned int>(w), static_cast<unsigned int>(h)};
MathVector3 a{0.f, 0.f, 0.f};
float dist = 4.f;
while (1) {
//timeout(1000 / FPS);
timeout(10);
int c = getch();
if (c == MKEY_ESC) break;
switch (c) {
case KEY_UP:
// a.x += 0.1f;
dist += .1f;
break;
case KEY_DOWN:
// a.x -= 0.1f;
dist -= .1f;
break;
case KEY_LEFT:
// a.y += 0.1f;
break;
case KEY_RIGHT:
// a.y -= 0.1f;
break;
case MKEY_Q:
// a.z += 0.1f;
break;
case MKEY_D:
// a.z -= 0.1f;
break;
}
a.x += .0050f;
a.y += .0065f;
a.z += .0080f;
getmaxyx(stdscr, h, w);
cfb.resize(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
float rad = 5.f;
MathVector3 ca{std::cos(a.x), std::cos(a.y), std::cos(a.z)};
MathVector3 sa{std::sin(a.x), std::sin(a.y), std::sin(a.z)};
std::array<MathVector3, 3> rot_x = {{
{ 1.f, 0.f, 0.f },
{ 0.f, ca.x, sa.x },
{ 0.f, -sa.x, ca.x },
}};
std::array<MathVector3, 3> rot_y = {{
{ ca.y, 0.f, -sa.y },
{ 0.f, 1.f, 0.f },
{ sa.y, 0.f, ca.y },
}};
std::array<MathVector3, 3> rot_z = {{
{ ca.z, sa.z, 0.f },
{ -sa.z, ca.z, 0.f },
{ 0.f, 0.f, 1.f },
}};
auto [e_x, e_y, e_z] = rot_x;
e_x = e_x.x * rot_y[0] + e_x.y * rot_y[1] + e_x.z * rot_y[2];
e_y = e_y.x * rot_y[0] + e_y.y * rot_y[1] + e_y.z * rot_y[2];
e_z = e_z.x * rot_y[0] + e_z.y * rot_y[1] + e_z.z * rot_y[2];
e_x = e_x.x * rot_z[0] + e_x.y * rot_z[1] + e_x.z * rot_z[2];
e_y = e_y.x * rot_z[0] + e_y.y * rot_z[1] + e_y.z * rot_z[2];
e_z = e_z.x * rot_z[0] + e_z.y * rot_z[1] + e_z.z * rot_z[2];
std::array<Object3D, 2> objs{{
{
{
{
rad * (-e_x + -e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + -e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (-e_x + +e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + +e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (-e_x + -e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + -e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (-e_x + +e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + +e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
}
},
{
// face 1
{ 0, 2, 3 },
{ 0, 3, 1 },
// face 2
{ 0, 4, 6 },
{ 0, 6, 2 },
// face 3
{ 0, 1, 5 },
{ 0, 5, 4 },
// face 4
{ 7, 6, 4 },
{ 7, 4, 5 },
// face 5
{ 7, 3, 2 },
{ 7, 2, 6 },
// face 6
{ 7, 5, 1 },
{ 7, 1, 3 },
}
},
{
{
{
rad * (-e_x + -e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + -e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (-e_x + +e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + +e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (-e_x + -e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + -e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (-e_x + +e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
},
{
rad * (+e_x + +e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
{}
}
},
{
// face 1
{ 0, 2, 3 },
{ 0, 3, 1 },
// face 2
{ 0, 4, 6 },
{ 0, 6, 2 },
// face 3
{ 0, 1, 5 },
{ 0, 5, 4 },
// face 4
{ 7, 6, 4 },
{ 7, 4, 5 },
// face 5
{ 7, 3, 2 },
{ 7, 2, 6 },
// face 6
{ 7, 5, 1 },
{ 7, 1, 3 },
}
}
}};
cfb.clear();
float min_z = 2.f, max_z = 50.f;
float fac_for_aspect_ratio = 2.f * static_cast<float>(cfb.height()) / static_cast<float>(cfb.width());
for (auto obj : objs) {
for (auto triangle : obj) {
TriangleVertex4 t{triangle};
// should be multiplied by a matrix, temporary replacement
t.vertex1.point.x *= fac_for_aspect_ratio;
t.vertex1.point.y = -t.vertex1.point.y;
t.vertex1.point.w = -t.vertex1.point.z;
t.vertex1.point.z = 2.f * (-t.vertex1.point.z - min_z) / (max_z - min_z) - 1.f;
t.vertex2.point.x *= fac_for_aspect_ratio;
t.vertex2.point.y = -t.vertex2.point.y;
t.vertex2.point.w = -t.vertex2.point.z;
t.vertex2.point.z = 2.f * (-t.vertex2.point.z - min_z) / (max_z - min_z) - 1.f;
t.vertex3.point.x *= fac_for_aspect_ratio;
t.vertex3.point.y = -t.vertex3.point.y;
t.vertex3.point.w = -t.vertex3.point.z;
t.vertex3.point.z = 2.f * (-t.vertex3.point.z - min_z) / (max_z - min_z) - 1.f;
cfb.draw_triangle(t);
}
}
mvaddnstr(0, 0, cfb.chars(), cfb.width() * cfb.height());
}
// terminate
endwin();
return EXIT_SUCCESS;
}
|