/
main.cpp
115 lines (107 loc) · 3.36 KB
/
main.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
#include <inttypes.h>
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include <cmath>
#include <graphic.h>
int fd;
uint32_t *scrMap;
void init(void)
{
display = new FrameBuffer("/dev/fb0", 800, 600);
}
int main(void)
{
using namespace std;
init();
// Generate arrays
const float coords[6][4][3] = {
{{+0.5, +0.5, +0.5}, {+0.5, -0.5, +0.5}, {+0.5, -0.5, -0.5}, {+0.5, +0.5, -0.5}},
{{-0.5, +0.5, -0.5}, {-0.5, +0.5, +0.5}, {+0.5, +0.5, +0.5}, {+0.5, +0.5, -0.5}},
{{-0.5, +0.5, +0.5}, {-0.5, -0.5, +0.5}, {+0.5, -0.5, +0.5}, {+0.5, +0.5, +0.5}},
{{-0.5, +0.5, -0.5}, {-0.5, -0.5, -0.5}, {-0.5, -0.5, +0.5}, {-0.5, +0.5, +0.5}},
{{-0.5, -0.5, +0.5}, {-0.5, -0.5, -0.5}, {+0.5, -0.5, -0.5}, {+0.5, -0.5, +0.5}},
{{+0.5, +0.5, -0.5}, {+0.5, -0.5, -0.5}, {-0.5, -0.5, -0.5}, {-0.5, +0.5, -0.5}},
};
const float colours[6][3] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
{0, 1, 1},
{1, 0, 1},
{1, 1, 0},
};
for (int i = 0; i < 6; i++)
for (int j = 0; j < 4; j++) {
display->vertices().push_back(Vector3D(coords[i][j][0], coords[i][j][1], coords[i][j][2]));
display->colours().push_back(Vector3D(colours[i][0], colours[i][1], colours[i][2]));
}
float len = 2.f;
display->vertices().push_back(Vector3D());
display->vertices().push_back(Vector3D(len, 0.f, 0.f));
display->colours().push_back(Vector3D(1.f, 0.f, 0.f));
display->colours().push_back(Vector3D(1.f, 0.f, 0.f));
display->vertices().push_back(Vector3D());
display->vertices().push_back(Vector3D(0.f, len, 0.f));
display->colours().push_back(Vector3D(0.f, 1.f, 0.f));
display->colours().push_back(Vector3D(0.f, 1.f, 0.f));
display->vertices().push_back(Vector3D());
display->vertices().push_back(Vector3D(0.f, 0.f, len));
display->colours().push_back(Vector3D(0.f, 0.f, 1.f));
display->colours().push_back(Vector3D(0.f, 0.f, 1.f));
// Projection
Matrix4x4 mp;
mp.perspective(45.f, 800.f / 600.f, 0.1, 1000.0);
mp.lookAt(Vector3D(0, 0, 3), Vector3D(0, 0, 0), Vector3D(0, 1, 0));
//mp.rotate(PI * 36.f / 180.f, Vector3D(1, 0, 0));
Vector3D vec(0.5, -0.599661, 6.37471);
cout << vec.x() << ", " << vec.y() << ", " << vec.z() << endl;
vec = mp * vec;
cout << vec.x() << ", " << vec.y() << ", " << vec.z() << endl;
//return 0;
start:
int ro = 360;
for (int of = 0; of < ro; of += 1) {
display->clear();
// Projection rotation
Matrix4x4 p;
p.rotate(PI * 2.0f * (float)of / (float)ro, Vector3D(1, 0, 0));
display->setProjection(mp * p);
// Model-View
Matrix4x4 m;
display->setModelView(m);
// Axis
display->drawArray(Display::Lines, 4 * 6, 6);
#if 1
// Draw cube
for (int i = 0; i < 6; i++)
display->drawArray(Display::TriangleFan, i * 4, 4);
// Another cube
m.scale(Vector3D(-1.f / 3.f, -1.2, -1.2));
m.translate(Vector3D(1.5f / 6.f, 0, 0));
display->setModelView(m);
for (int i = 0; i < 6; i++)
display->drawArray(Display::TriangleFan, i * 4, 4);
// Third cube
m.translate(Vector3D(-3.f / 6.f, 0, 0));
display->setModelView(m);
for (int i = 0; i < 6; i++)
display->drawArray(Display::TriangleFan, i * 4, 4);
#endif
// Far away cube
m = Matrix4x4();
//m.scale(Vector3D(0.5, 0.5, 0.5));
m.translate(Vector3D(0.f, 0.f, 6.f));
display->setModelView(m);
for (int i = 0; i < 6; i++)
display->drawArray(Display::TriangleFan, i * 4, 4);
// Redraw
display->update();
usleep(20000);
}
goto start;
goto finish;
finish:
delete display;
return 0;
}