From c06230ed6c31e1b0a91a23ed3fa803c5929b99e6 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Thu, 29 Jul 2021 21:59:21 -0500 Subject: [PATCH] Color radar image using a color table --- scwx-qt/gl/radar.frag | 17 +++-------- scwx-qt/source/scwx/qt/map/map_widget.cpp | 8 +++++ scwx-qt/source/scwx/qt/map/radar_layer.cpp | 30 +++++++++++++++++++ scwx-qt/source/scwx/qt/map/radar_layer.hpp | 2 ++ scwx-qt/source/scwx/qt/view/radar_view.cpp | 34 +++++++++++++++++++++- scwx-qt/source/scwx/qt/view/radar_view.hpp | 4 +++ 6 files changed, 81 insertions(+), 14 deletions(-) diff --git a/scwx-qt/gl/radar.frag b/scwx-qt/gl/radar.frag index b4fd8e7e..035f5e65 100644 --- a/scwx-qt/gl/radar.frag +++ b/scwx-qt/gl/radar.frag @@ -3,7 +3,7 @@ // Lower the default precision to medium precision mediump float; -uniform sampler2D uTexture; +uniform sampler1D uTexture; flat in uint dataMoment; @@ -11,16 +11,7 @@ layout (location = 0) out vec4 fragColor; void main() { - if (dataMoment < 126u) - { - fragColor = vec4(0.0f, 0.5f, 0.0f, 0.9f); - } - else if (dataMoment < 156u) - { - fragColor = vec4(1.0f, 0.75f, 0.0f, 0.9f); - } - else - { - fragColor = vec4(1.0f, 0.0f, 0.0f, 0.9f); - } + float texCoord = float(dataMoment - 2u) / 253.0f; // TODO: Scale properly + + fragColor = texture(uTexture, texCoord); } diff --git a/scwx-qt/source/scwx/qt/map/map_widget.cpp b/scwx-qt/source/scwx/qt/map/map_widget.cpp index 01ec2d80..db19c703 100644 --- a/scwx-qt/source/scwx/qt/map/map_widget.cpp +++ b/scwx-qt/source/scwx/qt/map/map_widget.cpp @@ -106,6 +106,14 @@ void MapWidget::AddLayers() radarView->Initialize(); + QString colorTableFile = qgetenv("COLOR_TABLE"); + if (!colorTableFile.isEmpty()) + { + std::shared_ptr colorTable = + common::ColorTable::Load(colorTableFile.toUtf8().constData()); + radarView->LoadColorTable(colorTable); + } + // QMapboxGL::addCustomLayer will take ownership of the QScopedPointer QScopedPointer pHost( new RadarLayer(radarView, p->gl_)); diff --git a/scwx-qt/source/scwx/qt/map/radar_layer.cpp b/scwx-qt/source/scwx/qt/map/radar_layer.cpp index 11b7f29d..aa273491 100644 --- a/scwx-qt/source/scwx/qt/map/radar_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/radar_layer.cpp @@ -36,6 +36,7 @@ public: uMapScreenCoordLocation_(GL_INVALID_INDEX), vbo_ {GL_INVALID_INDEX}, vao_ {GL_INVALID_INDEX}, + texture_ {GL_INVALID_INDEX}, numVertices_ {0} { } @@ -49,6 +50,7 @@ public: GLint uMapScreenCoordLocation_; std::array vbo_; GLuint vao_; + GLuint texture_; GLsizeiptr numVertices_; }; @@ -143,6 +145,11 @@ void RadarLayer::initialize() gl.glEnableVertexAttribArray(1); p->numVertices_ = vertices.size() / 2; + + // Create color table + gl.glGenTextures(1, &p->texture_); + UpdateColorTable(); + gl.glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); } void RadarLayer::render(const QMapbox::CustomLayerRenderParameters& params) @@ -170,6 +177,8 @@ void RadarLayer::render(const QMapbox::CustomLayerRenderParameters& params) gl.glUniformMatrix4fv( p->uMVPMatrixLocation_, 1, GL_FALSE, glm::value_ptr(uMVPMatrix)); + gl.glActiveTexture(GL_TEXTURE0); + gl.glBindTexture(GL_TEXTURE_1D, p->texture_); gl.glBindVertexArray(p->vao_); gl.glDrawArrays(GL_TRIANGLES, 0, p->numVertices_); } @@ -186,6 +195,27 @@ void RadarLayer::deinitialize() p->uMVPMatrixLocation_ = GL_INVALID_INDEX; p->vao_ = GL_INVALID_INDEX; p->vbo_ = {GL_INVALID_INDEX}; + p->texture_ = GL_INVALID_INDEX; +} + +void RadarLayer::UpdateColorTable() +{ + OpenGLFunctions& gl = p->gl_; + + const std::vector& colorTable = + p->radarView_->color_table(); + + gl.glActiveTexture(GL_TEXTURE0); + gl.glBindTexture(GL_TEXTURE_1D, p->texture_); + gl.glTexImage1D(GL_TEXTURE_1D, + 0, + GL_RGBA, + (GLsizei) colorTable.size(), + 0, + GL_RGBA, + GL_UNSIGNED_BYTE, + colorTable.data()); + gl.glGenerateMipmap(GL_TEXTURE_1D); } static glm::vec2 diff --git a/scwx-qt/source/scwx/qt/map/radar_layer.hpp b/scwx-qt/source/scwx/qt/map/radar_layer.hpp index c3cb696b..3a8e3088 100644 --- a/scwx-qt/source/scwx/qt/map/radar_layer.hpp +++ b/scwx-qt/source/scwx/qt/map/radar_layer.hpp @@ -28,6 +28,8 @@ public: void render(const QMapbox::CustomLayerRenderParameters&) override final; void deinitialize() override final; + void UpdateColorTable(); + private: std::unique_ptr p; }; diff --git a/scwx-qt/source/scwx/qt/view/radar_view.cpp b/scwx-qt/source/scwx/qt/view/radar_view.cpp index d732772f..f441f51b 100644 --- a/scwx-qt/source/scwx/qt/view/radar_view.cpp +++ b/scwx-qt/source/scwx/qt/view/radar_view.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace scwx @@ -21,7 +22,11 @@ class RadarViewImpl public: explicit RadarViewImpl(std::shared_ptr radarManager, std::shared_ptr map) : - radarManager_(radarManager), map_(map) + radarManager_(radarManager), + map_(map), + colorTable_ {boost::gil::rgba8_pixel_t(0, 128, 0, 255), + boost::gil::rgba8_pixel_t(255, 192, 0, 255), + boost::gil::rgba8_pixel_t(255, 0, 0, 255)} { } ~RadarViewImpl() = default; @@ -32,6 +37,8 @@ public: std::vector vertices_; std::vector dataMoments8_; std::vector dataMoments16_; + + std::vector colorTable_; }; RadarView::RadarView(std::shared_ptr radarManager, @@ -69,6 +76,11 @@ const std::vector& RadarView::vertices() const return p->vertices_; } +const std::vector& RadarView::color_table() const +{ + return p->colorTable_; +} + void RadarView::Initialize() { BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Initialize()"; @@ -297,6 +309,26 @@ void RadarView::Initialize() << logPrefix_ << "Vertices calculated in " << timer.format(6, "%ws"); } +void RadarView::LoadColorTable(std::shared_ptr colorTable) +{ + // TODO: Make size, offset and scale dynamic + const float offset = 66.0f; + const float scale = 2.0f; + + std::vector& lut = p->colorTable_; + lut.resize(254); + + auto dataRange = boost::irange(2, 255); + + std::for_each(std::execution::par_unseq, + dataRange.begin(), + dataRange.end(), + [&](uint16_t i) { + float f = (i - offset) / scale; + lut[i - *dataRange.begin()] = colorTable->Color(f); + }); +} + } // namespace view } // namespace qt } // namespace scwx diff --git a/scwx-qt/source/scwx/qt/view/radar_view.hpp b/scwx-qt/source/scwx/qt/view/radar_view.hpp index 829fe119..51b5b4ce 100644 --- a/scwx-qt/source/scwx/qt/view/radar_view.hpp +++ b/scwx-qt/source/scwx/qt/view/radar_view.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -35,7 +36,10 @@ public: const std::vector& data_moments16() const; const std::vector& vertices() const; + const std::vector& color_table() const; + void Initialize(); + void LoadColorTable(std::shared_ptr colorTable); private: std::unique_ptr p;