aboutsummaryrefslogtreecommitdiff
path: root/src/drone.cc
blob: 0be11a61405f90a1bcc83c273740bf2811438c53 (plain)
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
#include "drone.hh"

#include "load_obj.hh"
#include "opengl_widget.hh"

#include <QJsonArray>


bool Drone::mesh_initialized = false;
OpenGLMesh *Drone::mesh = nullptr;

Drone::Drone(int id)
	:id(id) {
	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());
		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(json["id"].toInt()) {
	QJsonArray ja = json["waypoints"].toArray();
	waypoints.reserve(ja.size());
	for (const QJsonValue &o : ja) {
		waypoints.append(Waypoint(o.toObject()));
	}
	setTo(0);
}


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 = &wp;
		} else {
			next = wp.frame;
			next_wp = &wp;
			break;
		}
	}
	OpenGLMesh &mesh = OpenGLWidget::instance->meshes[mesh_id];
	mesh.mat = QMatrix4x4();
	if (next > -1 && prev == -1) {
		pos = next_wp->pos;
	} else if (prev > -1 && next == -1) {
		pos = prev_wp->pos;
	} else {
		pos = lerp(prev_wp->pos, next_wp->pos, (double) (frame-prev) / (next-prev));
	}
	mesh.mat.translate(pos);
}


QVector3D Drone::getPos() const {
	return pos;
}


int Drone::getId() const {
	return id;
}