From ec4017bb0ff221c5c3e192a165c8d60a54f3bedc Mon Sep 17 00:00:00 2001 From: ccolin Date: Sun, 3 Jan 2021 17:10:26 +0100 Subject: [PATCH] highlight colliding drones --- shaders/main.frag | 8 +++++++- src/drone_controller.cc | 9 +++++++++ src/drone_controller.hh | 1 + src/opengl_widget.cc | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/shaders/main.frag b/shaders/main.frag index 7120521..f69789c 100644 --- a/shaders/main.frag +++ b/shaders/main.frag @@ -3,6 +3,7 @@ varying vec2 uv; varying vec3 frag_pos; uniform sampler2D tex; +uniform bool highlight; void main() { vec3 light_col = vec3(1, .964, .783); @@ -12,5 +13,10 @@ void main() { float diff = max(dot(normalize(norm), light_dir), 0.0); vec3 diffuse = diff * light_col; - gl_FragColor = texture2D(tex, uv) * vec4(ambient + diffuse, 1); + vec4 col = texture2D(tex, uv) * vec4(ambient + diffuse, 1); + + if (highlight) { + col = mix(col, vec4(1, 0, 0, 1), .5); + } + gl_FragColor = col; } diff --git a/src/drone_controller.cc b/src/drone_controller.cc index 3284acf..512ad23 100644 --- a/src/drone_controller.cc +++ b/src/drone_controller.cc @@ -45,14 +45,21 @@ DroneController::DroneController(const QJsonObject &json) void DroneController::draw(QOpenGLExtraFunctions *f) const { + const QVector> &col = collisions[frame]; for (const Drone &d : drones) { QMatrix4x4 mat; mat.translate(d.getPos()); + for (const QPair &p : col) { + if (d.getId() == p.first || d.getId() == p.second) { + OpenGLWidget::instance->getMainProgram()->setUniformValue("highlight", true); + } + } d.getMesh()->draw(f, mat); if (draw_spheres) { mat.scale(sphere_radius); sphere->draw(f, mat); } + OpenGLWidget::instance->getMainProgram()->setUniformValue("highlight", false); } } @@ -116,6 +123,7 @@ void DroneController::seek(int frame) { void DroneController::computeCollisions(double sphere_radius) { + collisions.clear(); double sqDist = sphere_radius * sphere_radius * 4; for (int i = 0; i < duration; i++) { for (Drone &a : drones) { @@ -124,6 +132,7 @@ void DroneController::computeCollisions(double sphere_radius) { b.setTo(i); if (&a == &b) continue; if (collides(a, b, sqDist)) { + collisions[i].append(QPair(a.getId(), b.getId())); emit collision(a.getId(), b.getId(), i); } } diff --git a/src/drone_controller.hh b/src/drone_controller.hh index de9385d..11d7395 100644 --- a/src/drone_controller.hh +++ b/src/drone_controller.hh @@ -22,6 +22,7 @@ class DroneController : public QObject, public Painter { bool draw_spheres = false; double sphere_radius = 0; QTimer sphere_timer; + QMap>> collisions; static OpenGLMesh *sphere; static const unsigned char sphere_neutral[]; diff --git a/src/opengl_widget.cc b/src/opengl_widget.cc index 6919086..29361c5 100644 --- a/src/opengl_widget.cc +++ b/src/opengl_widget.cc @@ -186,6 +186,7 @@ void OpenGLWidget::paintGL() { main_program.bind(); main_program.setUniformValue("proj", proj); main_program.setUniformValue("view", view); + main_program.setUniformValue("highlight", false); glActiveTexture(GL_TEXTURE0); ground->draw(this, QMatrix4x4()); if (painter) painter->draw(this);