From 689f3dc4641917ee337b0944a54d3adab4e58101 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sat, 18 Dec 2021 22:45:29 -0600 Subject: [PATCH] Change text background to draw item --- scwx-qt/gl/overlay.frag | 9 -- scwx-qt/gl/overlay.vert | 9 -- scwx-qt/scwx-qt.cmake | 2 - scwx-qt/scwx-qt.qrc | 2 - scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp | 6 +- scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp | 2 +- scwx-qt/source/scwx/qt/map/draw_layer.cpp | 4 +- scwx-qt/source/scwx/qt/map/overlay_layer.cpp | 103 ++++--------------- 8 files changed, 24 insertions(+), 113 deletions(-) delete mode 100644 scwx-qt/gl/overlay.frag delete mode 100644 scwx-qt/gl/overlay.vert diff --git a/scwx-qt/gl/overlay.frag b/scwx-qt/gl/overlay.frag deleted file mode 100644 index b2fd78c6..00000000 --- a/scwx-qt/gl/overlay.frag +++ /dev/null @@ -1,9 +0,0 @@ -#version 330 core -uniform vec4 uColor; - -layout (location = 0) out vec4 fragColor; - -void main() -{ - fragColor = uColor; -} diff --git a/scwx-qt/gl/overlay.vert b/scwx-qt/gl/overlay.vert deleted file mode 100644 index 042a1d8a..00000000 --- a/scwx-qt/gl/overlay.vert +++ /dev/null @@ -1,9 +0,0 @@ -#version 330 core -layout (location = 0) in vec2 aVertex; - -uniform mat4 uMVPMatrix; - -void main() -{ - gl_Position = uMVPMatrix * vec4(aVertex, 0.0f, 1.0f); -} diff --git a/scwx-qt/scwx-qt.cmake b/scwx-qt/scwx-qt.cmake index 1568eeef..ca4cd6ed 100644 --- a/scwx-qt/scwx-qt.cmake +++ b/scwx-qt/scwx-qt.cmake @@ -109,8 +109,6 @@ set(RESOURCE_FILES scwx-qt.qrc) set(SHADER_FILES gl/color.frag gl/color.vert - gl/overlay.frag - gl/overlay.vert gl/radar.frag gl/radar.vert gl/text.frag diff --git a/scwx-qt/scwx-qt.qrc b/scwx-qt/scwx-qt.qrc index 7cc85108..614e8554 100644 --- a/scwx-qt/scwx-qt.qrc +++ b/scwx-qt/scwx-qt.qrc @@ -2,8 +2,6 @@ gl/color.frag gl/color.vert - gl/overlay.frag - gl/overlay.vert gl/radar.frag gl/radar.vert gl/text.frag diff --git a/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp b/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp index 2c00b8a5..89fcba66 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp +++ b/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp @@ -67,6 +67,7 @@ public: void Update(); }; +// TODO: OpenGL context with shaders Rectangle::Rectangle(OpenGLFunctions& gl) : DrawItem(), p(std::make_unique(gl)) { @@ -159,12 +160,13 @@ void Rectangle::SetFill(boost::gil::rgba8_pixel_t color) } } -void Rectangle::SetPosition(float x, float y) +void Rectangle::SetPosition(float x, float y, float z) { - if (p->x_ != x || p->y_ != y) + if (p->x_ != x || p->y_ != y || p->z_ != z) { p->x_ = x; p->y_ = y; + p->z_ = z; p->dirty_ = true; } } diff --git a/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp b/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp index 02ca7a91..f1214309 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp +++ b/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp @@ -34,7 +34,7 @@ public: void SetBorder(float width, boost::gil::rgba8_pixel_t color); void SetFill(boost::gil::rgba8_pixel_t color); - void SetPosition(float x, float y); + void SetPosition(float x, float y, float z = 0.0f); void SetSize(float width, float height); void SetVisible(bool visible); diff --git a/scwx-qt/source/scwx/qt/map/draw_layer.cpp b/scwx-qt/source/scwx/qt/map/draw_layer.cpp index 6fb68970..409ddfe2 100644 --- a/scwx-qt/source/scwx/qt/map/draw_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/draw_layer.cpp @@ -67,9 +67,7 @@ void DrawLayer::Render(const QMapbox::CustomLayerRenderParameters& params) glm::mat4 projection = glm::ortho(0.0f, static_cast(params.width), 0.0f, - static_cast(params.height), - -10.0f, - 10.0f); + static_cast(params.height)); gl.glUniformMatrix4fv( p->uMVPMatrixLocation_, 1, GL_FALSE, glm::value_ptr(projection)); diff --git a/scwx-qt/source/scwx/qt/map/overlay_layer.cpp b/scwx-qt/source/scwx/qt/map/overlay_layer.cpp index 16c95859..5f07668e 100644 --- a/scwx-qt/source/scwx/qt/map/overlay_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/overlay_layer.cpp @@ -37,14 +37,10 @@ public: explicit OverlayLayerImpl(std::shared_ptr context) : textShader_(context->gl_), font_(util::Font::Create(":/res/fonts/din1451alt.ttf")), - shaderProgram_(context->gl_), - uMVPMatrixLocation_(GL_INVALID_INDEX), - uColorLocation_(GL_INVALID_INDEX), - vbo_ {GL_INVALID_INDEX}, - vao_ {GL_INVALID_INDEX}, texture_ {GL_INVALID_INDEX}, activeBoxOuter_ {std::make_shared(context->gl_)}, activeBoxInner_ {std::make_shared(context->gl_)}, + timeBox_ {std::make_shared(context->gl_)}, sweepTimeString_ {}, sweepTimeNeedsUpdate_ {true} { @@ -54,15 +50,11 @@ public: gl::TextShader textShader_; std::shared_ptr font_; - gl::ShaderProgram shaderProgram_; - GLint uMVPMatrixLocation_; - GLint uColorLocation_; - GLuint vbo_; - GLuint vao_; GLuint texture_; std::shared_ptr activeBoxOuter_; std::shared_ptr activeBoxInner_; + std::shared_ptr timeBox_; std::string sweepTimeString_; bool sweepTimeNeedsUpdate_; @@ -71,10 +63,15 @@ public: OverlayLayer::OverlayLayer(std::shared_ptr context) : DrawLayer(context), p(std::make_unique(context)) { + AddDrawItem(p->timeBox_); AddDrawItem(p->activeBoxOuter_); AddDrawItem(p->activeBoxInner_); + p->activeBoxOuter_->SetPosition(0.0f, 0.0f); + p->activeBoxOuter_->SetBorder(1.0f, {0, 0, 0, 255}); + p->activeBoxInner_->SetBorder(1.0f, {255, 255, 255, 255}); p->activeBoxInner_->SetPosition(1.0f, 1.0f); + p->timeBox_->SetFill({0, 0, 0, 192}); } OverlayLayer::~OverlayLayer() = default; @@ -89,49 +86,11 @@ void OverlayLayer::Initialize() p->textShader_.Initialize(); - // Load and configure overlay shader - p->shaderProgram_.Load(":/gl/overlay.vert", ":/gl/overlay.frag"); - - p->uMVPMatrixLocation_ = - gl.glGetUniformLocation(p->shaderProgram_.id(), "uMVPMatrix"); - if (p->uMVPMatrixLocation_ == -1) - { - BOOST_LOG_TRIVIAL(warning) << logPrefix_ << "Could not find uMVPMatrix"; - } - - p->uColorLocation_ = - gl.glGetUniformLocation(p->shaderProgram_.id(), "uColor"); - if (p->uColorLocation_ == -1) - { - BOOST_LOG_TRIVIAL(warning) << logPrefix_ << "Could not find uColor"; - } - if (p->texture_ == GL_INVALID_INDEX) { p->texture_ = p->font_->GenerateTexture(gl); } - p->shaderProgram_.Use(); - - // Generate a vertex array object - gl.glGenVertexArrays(1, &p->vao_); - - // Generate vertex buffer objects - gl.glGenBuffers(1, &p->vbo_); - - gl.glBindVertexArray(p->vao_); - - // Upper right panel (dynamic sized) - gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_); - gl.glBufferData( - GL_ARRAY_BUFFER, sizeof(float) * 6 * 2, nullptr, GL_DYNAMIC_DRAW); - - gl.glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, static_cast(0)); - gl.glEnableVertexAttribArray(0); - - // Upper right panel color - gl.glUniform4f(p->uColorLocation_, 0.0f, 0.0f, 0.0f, 0.75f); - connect(context()->radarProductView_.get(), &view::RadarProductView::SweepComputed, this, @@ -140,6 +99,8 @@ void OverlayLayer::Initialize() void OverlayLayer::Render(const QMapbox::CustomLayerRenderParameters& params) { + constexpr float fontSize = 16.0f; + gl::OpenGLFunctions& gl = context()->gl_; if (p->sweepTimeNeedsUpdate_) @@ -164,50 +125,30 @@ void OverlayLayer::Render(const QMapbox::CustomLayerRenderParameters& params) 0.0f, static_cast(params.height)); - p->shaderProgram_.Use(); - - gl.glUniformMatrix4fv( - p->uMVPMatrixLocation_, 1, GL_FALSE, glm::value_ptr(projection)); - // Active Box p->activeBoxOuter_->SetVisible(context()->settings_.isActive_); p->activeBoxInner_->SetVisible(context()->settings_.isActive_); if (context()->settings_.isActive_) { p->activeBoxOuter_->SetSize(params.width, params.height); - p->activeBoxOuter_->SetBorder(1.0f, {0, 0, 0, 255}); p->activeBoxInner_->SetSize(params.width - 2.0f, params.height - 2.0f); - p->activeBoxInner_->SetBorder(1.0f, {255, 255, 255, 255}); } if (p->sweepTimeString_.length() > 0) { - const float fontSize = 16.0f; const float textLength = p->font_->TextLength(p->sweepTimeString_, fontSize); - // Upper right panel vertices - const float vertexLX = - static_cast(params.width) - textLength - 14.0f; - const float vertexRX = static_cast(params.width); - const float vertexTY = static_cast(params.height); - const float vertexBY = static_cast(params.height) - 22.0f; - const float vertices[6][2] = {{vertexLX, vertexTY}, // TL - {vertexLX, vertexBY}, // BL - {vertexRX, vertexTY}, // TR - // - {vertexLX, vertexBY}, // BL - {vertexRX, vertexTY}, // TR - {vertexRX, vertexBY}}; // BR + p->timeBox_->SetPosition(static_cast(params.width) - textLength - + 14.0f, + static_cast(params.height) - 22.0f); + p->timeBox_->SetSize(textLength + 14.0f, 22.0f); + } - // Draw vertices - gl.glBindVertexArray(p->vao_); - gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_); - gl.glVertexAttribPointer( - 0, 2, GL_FLOAT, GL_FALSE, 0, static_cast(0)); - gl.glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); - gl.glDrawArrays(GL_TRIANGLES, 0, 6); + DrawLayer::Render(params); + if (p->sweepTimeString_.length() > 0) + { // Render time p->textShader_.RenderText(p->sweepTimeString_, params.width - 7.0f, @@ -220,8 +161,6 @@ void OverlayLayer::Render(const QMapbox::CustomLayerRenderParameters& params) gl::TextAlign::Right); } - DrawLayer::Render(params); - SCWX_GL_CHECK_ERROR(); } @@ -233,15 +172,9 @@ void OverlayLayer::Deinitialize() gl::OpenGLFunctions& gl = context()->gl_; - gl.glDeleteVertexArrays(1, &p->vao_); - gl.glDeleteBuffers(1, &p->vbo_); gl.glDeleteTextures(1, &p->texture_); - p->uMVPMatrixLocation_ = GL_INVALID_INDEX; - p->uColorLocation_ = GL_INVALID_INDEX; - p->vao_ = GL_INVALID_INDEX; - p->vbo_ = GL_INVALID_INDEX; - p->texture_ = GL_INVALID_INDEX; + p->texture_ = GL_INVALID_INDEX; disconnect(context()->radarProductView_.get(), &view::RadarProductView::SweepComputed,