From 224d36bae58d3018acfaaf9e1c598fac7528a290 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Mon, 3 Oct 2022 00:11:39 -0500 Subject: [PATCH] Define draw item shaders in draw items, not the generic draw layer --- scwx-qt/source/scwx/qt/gl/draw/draw_item.cpp | 29 ++++++++++-- scwx-qt/source/scwx/qt/gl/draw/draw_item.hpp | 24 ++++++---- scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp | 4 +- scwx-qt/source/scwx/qt/gl/draw/geo_line.hpp | 2 +- scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp | 48 ++++++++++++++------ scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp | 14 +++--- scwx-qt/source/scwx/qt/map/draw_layer.cpp | 44 +----------------- scwx-qt/source/scwx/qt/map/overlay_layer.cpp | 6 +-- 8 files changed, 88 insertions(+), 83 deletions(-) diff --git a/scwx-qt/source/scwx/qt/gl/draw/draw_item.cpp b/scwx-qt/source/scwx/qt/gl/draw/draw_item.cpp index ca4533b6..f4b37fd0 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/draw_item.cpp +++ b/scwx-qt/source/scwx/qt/gl/draw/draw_item.cpp @@ -2,6 +2,12 @@ #include +#pragma warning(push, 0) +#include +#include +#include +#pragma warning(pop) + namespace scwx { namespace qt @@ -13,20 +19,33 @@ namespace draw static const std::string logPrefix_ = "scwx::qt::gl::draw::draw_item"; -class DrawItemImpl +class DrawItem::Impl { public: - explicit DrawItemImpl() {} + explicit Impl(OpenGLFunctions& gl) : gl_ {gl} {} + ~Impl() {} - ~DrawItemImpl() {} + OpenGLFunctions& gl_; }; -DrawItem::DrawItem() : p(std::make_unique()) {} +DrawItem::DrawItem(OpenGLFunctions& gl) : p(std::make_unique(gl)) {} DrawItem::~DrawItem() = default; -DrawItem::DrawItem(DrawItem&&) noexcept = default; +DrawItem::DrawItem(DrawItem&&) noexcept = default; DrawItem& DrawItem::operator=(DrawItem&&) noexcept = default; +void DrawItem::UseDefaultProjection( + const QMapbox::CustomLayerRenderParameters& params, GLint uMVPMatrixLocation) +{ + glm::mat4 projection = glm::ortho(0.0f, + static_cast(params.width), + 0.0f, + static_cast(params.height)); + + p->gl_.glUniformMatrix4fv( + uMVPMatrixLocation, 1, GL_FALSE, glm::value_ptr(projection)); +} + } // namespace draw } // namespace gl } // namespace qt diff --git a/scwx-qt/source/scwx/qt/gl/draw/draw_item.hpp b/scwx-qt/source/scwx/qt/gl/draw/draw_item.hpp index 09e98092..32a221eb 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/draw_item.hpp +++ b/scwx-qt/source/scwx/qt/gl/draw/draw_item.hpp @@ -1,7 +1,11 @@ #pragma once +#include + #include +#include + namespace scwx { namespace qt @@ -11,26 +15,30 @@ namespace gl namespace draw { -class DrawItemImpl; - class DrawItem { public: - explicit DrawItem(); + explicit DrawItem(OpenGLFunctions& gl); ~DrawItem(); - DrawItem(const DrawItem&) = delete; + DrawItem(const DrawItem&) = delete; DrawItem& operator=(const DrawItem&) = delete; DrawItem(DrawItem&&) noexcept; DrawItem& operator=(DrawItem&&) noexcept; - virtual void Initialize() = 0; - virtual void Render() = 0; - virtual void Deinitialize() = 0; + virtual void Initialize() = 0; + virtual void Render(const QMapbox::CustomLayerRenderParameters& params) = 0; + virtual void Deinitialize() = 0; + +protected: + void UseDefaultProjection(const QMapbox::CustomLayerRenderParameters& params, + GLint uMVPMatrixLocation); private: - std::unique_ptr p; + class Impl; + + std::unique_ptr p; }; } // namespace draw diff --git a/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp b/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp index 7834022f..0c65d3fc 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp +++ b/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp @@ -59,7 +59,7 @@ public: // TODO: OpenGL context with shaders GeoLine::GeoLine(OpenGLFunctions& gl) : - DrawItem(), p(std::make_unique(gl)) + DrawItem(gl), p(std::make_unique(gl)) { } GeoLine::~GeoLine() = default; @@ -118,7 +118,7 @@ void GeoLine::Initialize() p->dirty_ = true; } -void GeoLine::Render() +void GeoLine::Render(const QMapbox::CustomLayerRenderParameters&) { if (p->visible_) { diff --git a/scwx-qt/source/scwx/qt/gl/draw/geo_line.hpp b/scwx-qt/source/scwx/qt/gl/draw/geo_line.hpp index a1b6ec8f..7b5d6284 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/geo_line.hpp +++ b/scwx-qt/source/scwx/qt/gl/draw/geo_line.hpp @@ -27,7 +27,7 @@ public: GeoLine& operator=(GeoLine&&) noexcept; void Initialize() override; - void Render() override; + void Render(const QMapbox::CustomLayerRenderParameters& params) override; void Deinitialize() override; /** diff --git a/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp b/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp index 10988613..f8b5756a 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp +++ b/scwx-qt/source/scwx/qt/gl/draw/rectangle.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -12,6 +13,7 @@ namespace draw { static const std::string logPrefix_ = "scwx::qt::gl::draw::rectangle"; +static const auto logger_ = scwx::util::Logger::Create(logPrefix_); static constexpr size_t NUM_RECTANGLES = 5; static constexpr size_t NUM_TRIANGLES = NUM_RECTANGLES * 2; @@ -21,11 +23,11 @@ static constexpr size_t POINTS_PER_VERTEX = 7; static constexpr size_t BUFFER_LENGTH = NUM_TRIANGLES * VERTICES_PER_TRIANGLE * POINTS_PER_VERTEX; -class RectangleImpl +class Rectangle::Impl { public: - explicit RectangleImpl(OpenGLFunctions& gl) : - gl_ {gl}, + explicit Impl(std::shared_ptr context) : + context_ {context}, dirty_ {false}, visible_ {true}, x_ {0.0f}, @@ -36,14 +38,16 @@ public: borderColor_ {0, 0, 0, 0}, borderWidth_ {0.0f}, fillColor_ {std::nullopt}, + shaderProgram_ {nullptr}, + uMVPMatrixLocation_(GL_INVALID_INDEX), vao_ {GL_INVALID_INDEX}, vbo_ {GL_INVALID_INDEX} { } - ~RectangleImpl() {} + ~Impl() {} - OpenGLFunctions& gl_; + std::shared_ptr context_; bool dirty_; @@ -59,25 +63,37 @@ public: std::optional fillColor_; + std::shared_ptr shaderProgram_; + GLint uMVPMatrixLocation_; + GLuint vao_; GLuint vbo_; void Update(); }; -// TODO: OpenGL context with shaders -Rectangle::Rectangle(OpenGLFunctions& gl) : - DrawItem(), p(std::make_unique(gl)) +Rectangle::Rectangle(std::shared_ptr context) : + DrawItem(context->gl()), p(std::make_unique(context)) { } Rectangle::~Rectangle() = default; -Rectangle::Rectangle(Rectangle&&) noexcept = default; +Rectangle::Rectangle(Rectangle&&) noexcept = default; Rectangle& Rectangle::operator=(Rectangle&&) noexcept = default; void Rectangle::Initialize() { - gl::OpenGLFunctions& gl = p->gl_; + gl::OpenGLFunctions& gl = p->context_->gl(); + + p->shaderProgram_ = + p->context_->GetShaderProgram(":/gl/color.vert", ":/gl/color.frag"); + + p->uMVPMatrixLocation_ = + gl.glGetUniformLocation(p->shaderProgram_->id(), "uMVPMatrix"); + if (p->uMVPMatrixLocation_ == -1) + { + logger_->warn("Could not find uMVPMatrix"); + } gl.glGenVertexArrays(1, &p->vao_); gl.glGenBuffers(1, &p->vbo_); @@ -106,16 +122,18 @@ void Rectangle::Initialize() p->dirty_ = true; } -void Rectangle::Render() +void Rectangle::Render(const QMapbox::CustomLayerRenderParameters& params) { if (p->visible_) { - gl::OpenGLFunctions& gl = p->gl_; + gl::OpenGLFunctions& gl = p->context_->gl(); gl.glBindVertexArray(p->vao_); gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_); p->Update(); + p->shaderProgram_->Use(); + UseDefaultProjection(params, p->uMVPMatrixLocation_); if (p->fillColor_.has_value()) { @@ -133,7 +151,7 @@ void Rectangle::Render() void Rectangle::Deinitialize() { - gl::OpenGLFunctions& gl = p->gl_; + gl::OpenGLFunctions& gl = p->context_->gl(); gl.glDeleteVertexArrays(1, &p->vao_); gl.glDeleteBuffers(1, &p->vbo_); @@ -184,11 +202,11 @@ void Rectangle::SetVisible(bool visible) p->visible_ = visible; } -void RectangleImpl::Update() +void Rectangle::Impl::Update() { if (dirty_) { - gl::OpenGLFunctions& gl = gl_; + gl::OpenGLFunctions& gl = context_->gl(); const float lox = x_; const float rox = x_ + width_; diff --git a/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp b/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp index f1214309..481f7507 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp +++ b/scwx-qt/source/scwx/qt/gl/draw/rectangle.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -14,22 +14,20 @@ namespace gl namespace draw { -class RectangleImpl; - class Rectangle : public DrawItem { public: - explicit Rectangle(OpenGLFunctions& gl); + explicit Rectangle(std::shared_ptr context); ~Rectangle(); - Rectangle(const Rectangle&) = delete; + Rectangle(const Rectangle&) = delete; Rectangle& operator=(const Rectangle&) = delete; Rectangle(Rectangle&&) noexcept; Rectangle& operator=(Rectangle&&) noexcept; void Initialize() override; - void Render() override; + void Render(const QMapbox::CustomLayerRenderParameters& params) override; void Deinitialize() override; void SetBorder(float width, boost::gil::rgba8_pixel_t color); @@ -39,7 +37,9 @@ public: void SetVisible(bool visible); private: - std::unique_ptr p; + class Impl; + + std::unique_ptr p; }; } // namespace draw diff --git a/scwx-qt/source/scwx/qt/map/draw_layer.cpp b/scwx-qt/source/scwx/qt/map/draw_layer.cpp index c9dab283..970bf89c 100644 --- a/scwx-qt/source/scwx/qt/map/draw_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/draw_layer.cpp @@ -2,12 +2,6 @@ #include #include -#pragma warning(push, 0) -#include -#include -#include -#pragma warning(pop) - namespace scwx { namespace qt @@ -21,17 +15,9 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_); class DrawLayerImpl { public: - explicit DrawLayerImpl(std::shared_ptr context) : - shaderProgram_ {nullptr}, uMVPMatrixLocation_(GL_INVALID_INDEX) - { - } - + explicit DrawLayerImpl(std::shared_ptr context) {} ~DrawLayerImpl() {} - std::shared_ptr shaderProgram_; - - GLint uMVPMatrixLocation_; - std::vector> drawList_; }; @@ -43,20 +29,6 @@ DrawLayer::~DrawLayer() = default; void DrawLayer::Initialize() { - gl::OpenGLFunctions& gl = context()->gl(); - - p->shaderProgram_ = - context()->GetShaderProgram(":/gl/color.vert", ":/gl/color.frag"); - - p->uMVPMatrixLocation_ = - gl.glGetUniformLocation(p->shaderProgram_->id(), "uMVPMatrix"); - if (p->uMVPMatrixLocation_ == -1) - { - logger_->warn("Could not find uMVPMatrix"); - } - - p->shaderProgram_->Use(); - for (auto& item : p->drawList_) { item->Initialize(); @@ -65,21 +37,9 @@ void DrawLayer::Initialize() void DrawLayer::Render(const QMapbox::CustomLayerRenderParameters& params) { - gl::OpenGLFunctions& gl = context()->gl(); - - p->shaderProgram_->Use(); - - glm::mat4 projection = glm::ortho(0.0f, - static_cast(params.width), - 0.0f, - static_cast(params.height)); - - gl.glUniformMatrix4fv( - p->uMVPMatrixLocation_, 1, GL_FALSE, glm::value_ptr(projection)); - for (auto& item : p->drawList_) { - item->Render(); + item->Render(params); } } diff --git a/scwx-qt/source/scwx/qt/map/overlay_layer.cpp b/scwx-qt/source/scwx/qt/map/overlay_layer.cpp index e8049e1c..a6e136fa 100644 --- a/scwx-qt/source/scwx/qt/map/overlay_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/overlay_layer.cpp @@ -37,9 +37,9 @@ public: textShader_(context), font_(util::Font::Create(":/res/fonts/din1451alt.ttf")), texture_ {GL_INVALID_INDEX}, - activeBoxOuter_ {std::make_shared(context->gl())}, - activeBoxInner_ {std::make_shared(context->gl())}, - timeBox_ {std::make_shared(context->gl())}, + activeBoxOuter_ {std::make_shared(context)}, + activeBoxInner_ {std::make_shared(context)}, + timeBox_ {std::make_shared(context)}, sweepTimeString_ {}, sweepTimeNeedsUpdate_ {true} {