From f20a0f978d4b443a5784c496969f338996d85ca2 Mon Sep 17 00:00:00 2001 From: ccolin Date: Tue, 22 Dec 2020 16:35:25 +0100 Subject: drone models (broken) and animation --- src/drone_controller.cc | 78 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 12 deletions(-) (limited to 'src/drone_controller.cc') diff --git a/src/drone_controller.cc b/src/drone_controller.cc index 2f1364e..9241d30 100644 --- a/src/drone_controller.cc +++ b/src/drone_controller.cc @@ -1,7 +1,12 @@ #include "drone_controller.hh" +#include "opengl_widget.hh" + +#define TINYOBJLOADER_IMPLEMENTATION +#include "tiny_obj_loader.h" #include #include +#include Waypoint::Waypoint(unsigned frame, QVector3D pos) @@ -11,12 +16,39 @@ Waypoint::Waypoint(unsigned frame, QVector3D pos) Waypoint::Waypoint(const QJsonObject &json) :Waypoint(json["frame"].toInt(), - QVector3D(json["position"]["lng_X"].toInt(), - json["position"]["alt_Y"].toInt(), - json["position"]["lat_Z"].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) { + tinyobj::ObjReaderConfig reader_config; + reader_config.mtl_search_path = "../mdl/"; // Path to material files + tinyobj::ObjReader reader; + if (!reader.ParseFromFile("../mdl/dji600.obj", reader_config)) { + if (!reader.Error().empty()) { + qWarning() << "Erreur lors de la lecture de ../mdl/dji600.obj"; + } + exit(1); + } + // if (!reader.Warning().empty()) { + // qWarning() << "TinyObjReader: " << reader.Warning(); + // } + auto& attrib = reader.GetAttrib(); + mesh = new OpenGLMesh(QVector(attrib.vertices.begin(), attrib.vertices.end())); + mesh_initialized = true; + } + OpenGLWidget::instance->meshes.append(*mesh); + mesh_id = OpenGLWidget::instance->meshes.size() - 1; +} -Drone::Drone(const QJsonObject &json) { +Drone::Drone(const QJsonObject &json) + :Drone() { QJsonArray ja = json["waypoints"].toArray(); waypoints.reserve(ja.size()); for (const QJsonValue &o : ja) { @@ -30,12 +62,34 @@ const QVector Drone::getWaypoints() const { } +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()) { - // TODO: REMOVE!! - framerate = 1; - QJsonArray ja = json["drones"].toArray(); drones.reserve(ja.size()); for (const QJsonValue &o : ja) { @@ -52,17 +106,17 @@ DroneController::DroneController(const QJsonObject &json) } -DroneController::~DroneController() { -} - - int DroneController::getDuration() const { return duration; } void DroneController::step() { - qDebug("frame %d (%d%%)", frame, (int) ((double) frame / duration * 100)); + // qDebug("frame %d (%d%%)", frame, (int) ((double) frame / duration * 100)); + for (Drone d : drones) { + d.setTo(frame); + } + OpenGLWidget::instance->update(); emit frameChanged(frame); if (frame == duration) { pause(); -- cgit v1.2.3-70-g09d2