drone models (broken) and animation

This commit is contained in:
ccolin 2020-12-22 16:35:25 +01:00
parent 89a3f9c402
commit f20a0f978d
5 changed files with 3224 additions and 13 deletions

View File

@ -1,7 +1,12 @@
#include "drone_controller.hh"
#include "opengl_widget.hh"
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#include <QJsonArray>
#include <QDebug>
#include <Qt3DRender/QMesh>
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)) {}
Drone::Drone(const QJsonObject &json) {
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<float>(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() {
QJsonArray ja = json["waypoints"].toArray();
waypoints.reserve(ja.size());
for (const QJsonValue &o : ja) {
@ -30,12 +62,34 @@ const QVector<Waypoint> 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 = &wp;
} else {
next = wp.frame;
next_wp = &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();

View File

@ -1,11 +1,19 @@
#ifndef DRONE_CONTROLLER_HH
#define DRONE_CONTROLLER_HH
#include "opengl_mesh.hh"
#include <QJsonObject>
#include <QVector3D>
#include <QTimer>
template <typename T>
static T lerp(T a, T b, double factor) {
return a + (factor * (b - a));
}
struct Waypoint {
int frame;
QVector3D pos;
@ -16,11 +24,17 @@ struct Waypoint {
class Drone {
static OpenGLMesh *mesh;
static bool mesh_initialized;
QVector<Waypoint> waypoints;
int mesh_id;
public:
Drone();
Drone(const QJsonObject &json);
const QVector<Waypoint> getWaypoints() const;
void setTo(int frame);
};
@ -36,7 +50,6 @@ class DroneController : public QObject {
public:
DroneController(const QJsonObject &json);
~DroneController();
int getDuration() const;
signals:

View File

@ -1,5 +1,7 @@
#include "opengl_widget.hh"
#include <QMouseEvent>
static const GLchar *vertex_shader_source = R"glsl(
#version 330 core
@ -135,9 +137,43 @@ void OpenGLWidget::resizeGL(int w, int h) {
void OpenGLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 trans;
trans.translate(0, 0, -cam_dist);
QMatrix4x4 view = trans * rot;
glUniformMatrix4fv(view_attr, 1, GL_FALSE, view.data());
for (const OpenGLMesh &mesh : meshes) {
glUniformMatrix4fv(model_attr, 1, GL_FALSE, mesh.mat.data());
glBindVertexArray(mesh.vao);
glDrawArrays(GL_TRIANGLES, 0, mesh.nverts);
}
}
void OpenGLWidget::mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton) {
mouse_pos = e->pos();
}
}
void OpenGLWidget::mouseReleaseEvent(QMouseEvent *e) {
(void) e;
rot_start = rot;
}
void OpenGLWidget::mouseMoveEvent(QMouseEvent *e) {
if (e->buttons() & Qt::LeftButton) {
QPoint delta = e->pos() - mouse_pos;
rot = rot_start;
rot.rotate(delta.x() / 5., 0, 1, 0);
rot.rotate(delta.y() / 5., QVector3D(1, 0, 0) * rot);
update();
}
}
void OpenGLWidget::wheelEvent(QWheelEvent *e) {
cam_dist -= e->angleDelta().y() / 1000. * cam_dist;
update();
}

View File

@ -16,6 +16,16 @@ class OpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions_4_4_Core {
GLuint pos_attr, proj_attr, view_attr, model_attr;
QMatrix4x4 rot, rot_start;
GLfloat cam_dist = 1;
QPoint mouse_pos;
protected:
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseReleaseEvent(QMouseEvent *e);
virtual void mouseMoveEvent(QMouseEvent *e);
virtual void wheelEvent(QWheelEvent *e);
public:
static OpenGLWidget *instance;

3098
src/tiny_obj_loader.h Normal file

File diff suppressed because it is too large Load Diff