aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/drone_controller.cc36
-rw-r--r--src/drone_controller.hh7
-rw-r--r--src/main_window.cc2
-rw-r--r--src/opengl_mesh.cc6
4 files changed, 45 insertions, 6 deletions
diff --git a/src/drone_controller.cc b/src/drone_controller.cc
index 0be9844..514a166 100644
--- a/src/drone_controller.cc
+++ b/src/drone_controller.cc
@@ -1,14 +1,34 @@
#include "drone_controller.hh"
#include "opengl_widget.hh"
+#include "load_obj.hh"
#include <QJsonArray>
#include <QDebug>
#include <QOpenGLShaderProgram>
-DroneController::DroneController(const QJsonObject &json)
- :framerate(json["framerate"].toInt()) {
+const unsigned char DroneController::sphere_neutral[] = {
+ 166, 166, 166
+};
+
+OpenGLMesh *DroneController::sphere = nullptr;
+
+DroneController::DroneController(const QJsonObject &json)
+ :framerate(json["framerate"].toInt()),
+ sphere_timer(this) {
+ sphere_timer.setSingleShot(true);
+ connect(&sphere_timer, &QTimer::timeout, [&]() {
+ draw_spheres = false;
+ OpenGLWidget::instance->update();
+ });
+ if (sphere == nullptr) {
+ OpenGLWidget::instance->makeCurrent();
+ sphere = new OpenGLMesh(load_obj(":/mdl/sphere.obj", LOAD_OBJ_NORMALS | LOAD_OBJ_UVS),
+ new QOpenGLTexture(QImage(sphere_neutral, 1, 1, QImage::Format_RGB888)),
+ OpenGLWidget::instance->getMainProgram());
+ OpenGLWidget::instance->makeCurrent();
+ }
QJsonArray ja = json["drones"].toArray();
drones.reserve(ja.size());
for (const QJsonValue &o : ja) {
@@ -29,6 +49,10 @@ void DroneController::draw(QOpenGLExtraFunctions *f) const {
QMatrix4x4 mat;
mat.translate(d.getPos());
d.getMesh()->draw(f, mat);
+ if (draw_spheres) {
+ mat.scale(sphere_radius);
+ sphere->draw(f, mat);
+ }
}
}
@@ -111,6 +135,14 @@ void DroneController::computeCollisions(double sphere_radius) {
}
+void DroneController::displaySpheres(double sphere_radius) {
+ draw_spheres = true;
+ this->sphere_radius = sphere_radius;
+ sphere_timer.start(1000);
+ OpenGLWidget::instance->update();
+}
+
+
bool DroneController::collides(const Drone &a, const Drone &b, double sqDist) {
return (b.getPos() - a.getPos()).lengthSquared() < sqDist;
}
diff --git a/src/drone_controller.hh b/src/drone_controller.hh
index bcf1a1a..de9385d 100644
--- a/src/drone_controller.hh
+++ b/src/drone_controller.hh
@@ -19,6 +19,12 @@ class DroneController : public QObject, public Painter {
QVector<Drone> drones;
QTimer timer;
bool paused = true;
+ bool draw_spheres = false;
+ double sphere_radius = 0;
+ QTimer sphere_timer;
+
+ static OpenGLMesh *sphere;
+ static const unsigned char sphere_neutral[];
static bool collides(const Drone &a, const Drone &b, double radius);
@@ -43,6 +49,7 @@ public slots:
void resume();
void seek(int frame);
void computeCollisions(double sphere_radius);
+ void displaySpheres(double sphere_radius);
};
diff --git a/src/main_window.cc b/src/main_window.cc
index cd85ae7..9680d94 100644
--- a/src/main_window.cc
+++ b/src/main_window.cc
@@ -87,6 +87,8 @@ void MainWindow::open(const QString &path) {
[&](double _) { settings_pane->clearCollisions(); });
connect(settings_pane, &SettingsPane::sphereRadiusChanged,
dc, &DroneController::computeCollisions);
+ connect(settings_pane, &SettingsPane::sphereRadiusChanged,
+ dc, &DroneController::displaySpheres);
settings_pane->setEnabled(true);
glw.setPainter(dc);
diff --git a/src/opengl_mesh.cc b/src/opengl_mesh.cc
index a1f5a23..f77a70a 100644
--- a/src/opengl_mesh.cc
+++ b/src/opengl_mesh.cc
@@ -21,8 +21,6 @@ OpenGLMesh::OpenGLMesh(QVector<float> verts, QOpenGLTexture *tex, QOpenGLShaderP
glf->glEnableVertexAttribArray(1);
glf->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof (float), (void *) (6 * sizeof (float)));
glf->glEnableVertexAttribArray(2);
- tex->bind();
- glf->glBindVertexArray(0);
OpenGLWidget::instance->doneCurrent();
}
@@ -31,7 +29,7 @@ void OpenGLMesh::draw(QOpenGLExtraFunctions *f, const QMatrix4x4 &mat) const {
program->bind();
program->setUniformValue("model", mat);
f->glBindVertexArray(vao);
- tex->bind();
+ if (tex) tex->bind();
f->glDrawArrays(GL_TRIANGLES, 0, nverts);
- tex->release();
+ if (tex) tex->release();
}