From bb0da810d945d98b25390c65ab3f34e695914fd6 Mon Sep 17 00:00:00 2001 From: ccolin Date: Sat, 2 Jan 2021 01:16:06 +0100 Subject: move Drone and Waypoint declarations to their own files --- src/drone.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/drone.cc (limited to 'src/drone.cc') diff --git a/src/drone.cc b/src/drone.cc new file mode 100644 index 0000000..b7e4007 --- /dev/null +++ b/src/drone.cc @@ -0,0 +1,73 @@ +#include "drone.hh" + +#include "load_obj.hh" +#include "opengl_widget.hh" + +#include + + +bool Drone::mesh_initialized = false; +OpenGLMesh *Drone::mesh = nullptr; + +Drone::Drone(int id) + :id(id) { + if (!mesh_initialized) { + QVector 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())); + } +} + + +const QVector 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) { + 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; +} -- cgit v1.2.3-70-g09d2