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
|
#include "drone_controller.hh"
#include "opengl_widget.hh"
#include "load_obj.hh"
#include <QJsonArray>
#include <QDebug>
#include <QFile>
Waypoint::Waypoint(unsigned frame, QVector3D pos)
:frame(frame),
pos(pos) {}
Waypoint::Waypoint(const QJsonObject &json)
:Waypoint(json["frame"].toInt(),
QVector3D(json["position"]["lng_X"].toInt() / 100.0,
json["position"]["alt_Y"].toInt() / 100.0,
json["position"]["lat_Z"].toInt() / 100.0)) {}
bool Drone::mesh_initialized = false;
OpenGLMesh *Drone::mesh = nullptr;
Drone::Drone() {
if (!mesh_initialized) {
QVector<GLfloat> verts = load_obj(":/mdl/dji600.obj", LOAD_OBJ_NORMALS | LOAD_OBJ_UVS);
QOpenGLTexture *texture = new QOpenGLTexture(QImage(":/img/dji600.jpg").mirrored());
// texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
// texture->setMagnificationFilter(QOpenGLTexture::Linear);
mesh = new OpenGLMesh(verts, texture);
mesh_initialized = true;
}
OpenGLWidget::instance->meshes.append(*mesh);
mesh_id = OpenGLWidget::instance->meshes.size() - 1;
}
Drone::Drone(const QJsonObject &json)
:Drone() {
QJsonArray ja = json["waypoints"].toArray();
waypoints.reserve(ja.size());
for (const QJsonValue &o : ja) {
waypoints.append(Waypoint(o.toObject()));
}
}
const QVector<Waypoint> Drone::getWaypoints() const {
return waypoints;
}
void Drone::setTo(int frame) {
int prev = -1, next = -1;
const Waypoint *prev_wp, *next_wp;
for (const Waypoint &wp : waypoints) { // TODO: this can be optimized
if (wp.frame < frame) {
prev = wp.frame;
prev_wp = ℘
} else {
next = wp.frame;
next_wp = ℘
break;
}
}
OpenGLMesh &mesh = OpenGLWidget::instance->meshes[mesh_id];
mesh.mat = QMatrix4x4();
if (next > -1 && prev == -1) {
mesh.mat.translate(next_wp->pos);
} else if (prev > -1 && next == -1) {
mesh.mat.translate(prev_wp->pos);
} else {
mesh.mat.translate(lerp(prev_wp->pos, next_wp->pos, (double) (frame-prev) / (next-prev)));
}
}
DroneController::DroneController(const QJsonObject &json)
:framerate(json["framerate"].toInt()) {
QJsonArray ja = json["drones"].toArray();
drones.reserve(ja.size());
for (const QJsonValue &o : ja) {
drones.append(Drone(o.toObject()));
}
for (const Drone &d : drones) {
for (const Waypoint &wp : d.getWaypoints()) {
if (wp.frame > duration) duration = wp.frame;
}
}
OpenGLWidget::instance->makeCurrent();
QOpenGLTexture *ground_tex = new QOpenGLTexture(QImage(":/img/ground.jpg").mirrored());
OpenGLMesh *ground = new OpenGLMesh({
-100, 0, -100, 0, 1, 0, 0, 0,
100, 0, -100, 0, 1, 0, 1, 0,
-100, 0, 100, 0, 1, 0, 0, 1,
100, 0, -100, 0, 1, 0, 1, 0,
-100, 0, 100, 0, 1, 0, 0, 1,
100, 0, 100, 0, 1, 0, 1, 1,
}, ground_tex);
OpenGLWidget::instance->meshes.append(*ground);
OpenGLWidget::instance->doneCurrent();
connect(&timer, &QTimer::timeout, this, &DroneController::step);
}
int DroneController::getDuration() const {
return duration;
}
void DroneController::step() {
for (Drone d : drones) {
d.setTo(frame);
}
OpenGLWidget::instance->update();
emit frameChanged(frame);
if (frame == duration) {
pause();
} else {
frame++;
}
}
void DroneController::play() {
if (!paused) return;
paused = false;
timer.start(1000. / framerate);
emit playing();
}
void DroneController::pause() {
if (paused) return;
paused = true;
timer.stop();
emit pausing();
}
void DroneController::suspend() {
if (!paused) {
pause();
paused = false;
}
}
void DroneController::resume() {
if (!paused) {
paused = true;
play();
}
}
void DroneController::seek(int frame) {
if (this->frame == frame) return;
this->frame = frame;
step();
}
|