mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 01:30:05 +00:00 
			
		
		
		
	Implement active box using new draw item on draw layers
This commit is contained in:
		
							parent
							
								
									999d322985
								
							
						
					
					
						commit
						d30b6d4011
					
				
					 13 changed files with 610 additions and 37 deletions
				
			
		
							
								
								
									
										98
									
								
								scwx-qt/source/scwx/qt/map/draw_layer.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								scwx-qt/source/scwx/qt/map/draw_layer.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,98 @@ | |||
| #include <scwx/qt/map/draw_layer.hpp> | ||||
| #include <scwx/qt/gl/shader_program.hpp> | ||||
| 
 | ||||
| #include <boost/log/trivial.hpp> | ||||
| #include <glm/glm.hpp> | ||||
| #include <glm/gtc/matrix_transform.hpp> | ||||
| #include <glm/gtc/type_ptr.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace map | ||||
| { | ||||
| 
 | ||||
| static const std::string logPrefix_ = "[scwx::qt::map::draw_layer] "; | ||||
| 
 | ||||
| class DrawLayerImpl | ||||
| { | ||||
| public: | ||||
|    explicit DrawLayerImpl(std::shared_ptr<MapContext> context) : | ||||
|        shaderProgram_ {context->gl_}, uMVPMatrixLocation_(GL_INVALID_INDEX) | ||||
|    { | ||||
|    } | ||||
| 
 | ||||
|    ~DrawLayerImpl() {} | ||||
| 
 | ||||
|    gl::ShaderProgram shaderProgram_; | ||||
|    GLint             uMVPMatrixLocation_; | ||||
| 
 | ||||
|    std::vector<std::shared_ptr<gl::draw::DrawItem>> drawList_; | ||||
| }; | ||||
| 
 | ||||
| DrawLayer::DrawLayer(std::shared_ptr<MapContext> context) : | ||||
|     GenericLayer(context), p(std::make_unique<DrawLayerImpl>(context)) | ||||
| { | ||||
| } | ||||
| DrawLayer::~DrawLayer() = default; | ||||
| 
 | ||||
| void DrawLayer::Initialize() | ||||
| { | ||||
|    gl::OpenGLFunctions& gl = context()->gl_; | ||||
| 
 | ||||
|    p->shaderProgram_.Load(":/gl/color.vert", ":/gl/color.frag"); | ||||
| 
 | ||||
|    p->uMVPMatrixLocation_ = | ||||
|       gl.glGetUniformLocation(p->shaderProgram_.id(), "uMVPMatrix"); | ||||
|    if (p->uMVPMatrixLocation_ == -1) | ||||
|    { | ||||
|       BOOST_LOG_TRIVIAL(warning) << logPrefix_ << "Could not find uMVPMatrix"; | ||||
|    } | ||||
| 
 | ||||
|    p->shaderProgram_.Use(); | ||||
| 
 | ||||
|    for (auto item : p->drawList_) | ||||
|    { | ||||
|       item->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<float>(params.width), | ||||
|                                      0.0f, | ||||
|                                      static_cast<float>(params.height), | ||||
|                                      -10.0f, | ||||
|                                      10.0f); | ||||
| 
 | ||||
|    gl.glUniformMatrix4fv( | ||||
|       p->uMVPMatrixLocation_, 1, GL_FALSE, glm::value_ptr(projection)); | ||||
| 
 | ||||
|    for (auto item : p->drawList_) | ||||
|    { | ||||
|       item->Render(); | ||||
|    } | ||||
| } | ||||
| 
 | ||||
| void DrawLayer::Deinitialize() | ||||
| { | ||||
|    for (auto item : p->drawList_) | ||||
|    { | ||||
|       item->Deinitialize(); | ||||
|    } | ||||
| } | ||||
| 
 | ||||
| void DrawLayer::AddDrawItem(std::shared_ptr<gl::draw::DrawItem> drawItem) | ||||
| { | ||||
|    p->drawList_.push_back(drawItem); | ||||
| } | ||||
| 
 | ||||
| } // namespace map
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
							
								
								
									
										34
									
								
								scwx-qt/source/scwx/qt/map/draw_layer.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								scwx-qt/source/scwx/qt/map/draw_layer.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,34 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <scwx/qt/gl/draw/draw_item.hpp> | ||||
| #include <scwx/qt/map/generic_layer.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace map | ||||
| { | ||||
| 
 | ||||
| class DrawLayerImpl; | ||||
| 
 | ||||
| class DrawLayer : public GenericLayer | ||||
| { | ||||
| public: | ||||
|    explicit DrawLayer(std::shared_ptr<MapContext> context); | ||||
|    virtual ~DrawLayer(); | ||||
| 
 | ||||
|    virtual void Initialize(); | ||||
|    virtual void Render(const QMapbox::CustomLayerRenderParameters&); | ||||
|    virtual void Deinitialize(); | ||||
| 
 | ||||
| protected: | ||||
|    void AddDrawItem(std::shared_ptr<gl::draw::DrawItem> drawItem); | ||||
| 
 | ||||
| private: | ||||
|    std::unique_ptr<DrawLayerImpl> p; | ||||
| }; | ||||
| 
 | ||||
| } // namespace map
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
|  | @ -304,8 +304,8 @@ void MapWidget::AddLayers() | |||
|    p->map_->addCustomLayer("radar", std::move(pHost), before); | ||||
|    RadarRangeLayer::Add( | ||||
|       p->map_, p->context_->radarProductView_->range(), before); | ||||
|    p->map_->addCustomLayer("overlay", std::move(pOverlayHost)); | ||||
|    p->map_->addCustomLayer("colorTable", std::move(pColorTableHost)); | ||||
|    p->map_->addCustomLayer("overlay", std::move(pOverlayHost)); | ||||
| } | ||||
| 
 | ||||
| void MapWidget::keyPressEvent(QKeyEvent* ev) | ||||
|  |  | |||
|  | @ -1,9 +1,10 @@ | |||
| // Enable chrono formatters
 | ||||
| #ifndef __cpp_lib_format | ||||
| #define __cpp_lib_format 202110L | ||||
| #   define __cpp_lib_format 202110L | ||||
| #endif | ||||
| 
 | ||||
| #include <scwx/qt/map/overlay_layer.hpp> | ||||
| #include <scwx/qt/gl/draw/rectangle.hpp> | ||||
| #include <scwx/qt/gl/shader_program.hpp> | ||||
| #include <scwx/qt/gl/text_shader.hpp> | ||||
| #include <scwx/qt/util/font.hpp> | ||||
|  | @ -42,6 +43,8 @@ public: | |||
|        vbo_ {GL_INVALID_INDEX}, | ||||
|        vao_ {GL_INVALID_INDEX}, | ||||
|        texture_ {GL_INVALID_INDEX}, | ||||
|        activeBoxOuter_ {std::make_shared<gl::draw::Rectangle>(context->gl_)}, | ||||
|        activeBoxInner_ {std::make_shared<gl::draw::Rectangle>(context->gl_)}, | ||||
|        sweepTimeString_ {}, | ||||
|        sweepTimeNeedsUpdate_ {true} | ||||
|    { | ||||
|  | @ -54,24 +57,34 @@ public: | |||
|    gl::ShaderProgram           shaderProgram_; | ||||
|    GLint                       uMVPMatrixLocation_; | ||||
|    GLint                       uColorLocation_; | ||||
|    std::array<GLuint, 2>       vbo_; | ||||
|    GLuint                      vbo_; | ||||
|    GLuint                      vao_; | ||||
|    GLuint                      texture_; | ||||
| 
 | ||||
|    std::shared_ptr<gl::draw::Rectangle> activeBoxOuter_; | ||||
|    std::shared_ptr<gl::draw::Rectangle> activeBoxInner_; | ||||
| 
 | ||||
|    std::string sweepTimeString_; | ||||
|    bool        sweepTimeNeedsUpdate_; | ||||
| }; | ||||
| 
 | ||||
| OverlayLayer::OverlayLayer(std::shared_ptr<MapContext> context) : | ||||
|     GenericLayer(context), p(std::make_unique<OverlayLayerImpl>(context)) | ||||
|     DrawLayer(context), p(std::make_unique<OverlayLayerImpl>(context)) | ||||
| { | ||||
|    AddDrawItem(p->activeBoxOuter_); | ||||
|    AddDrawItem(p->activeBoxInner_); | ||||
| 
 | ||||
|    p->activeBoxInner_->SetPosition(1.0f, 1.0f); | ||||
| } | ||||
| 
 | ||||
| OverlayLayer::~OverlayLayer() = default; | ||||
| 
 | ||||
| void OverlayLayer::Initialize() | ||||
| { | ||||
|    BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Initialize()"; | ||||
| 
 | ||||
|    DrawLayer::Initialize(); | ||||
| 
 | ||||
|    gl::OpenGLFunctions& gl = context()->gl_; | ||||
| 
 | ||||
|    p->textShader_.Initialize(); | ||||
|  | @ -104,20 +117,12 @@ void OverlayLayer::Initialize() | |||
|    gl.glGenVertexArrays(1, &p->vao_); | ||||
| 
 | ||||
|    // Generate vertex buffer objects
 | ||||
|    gl.glGenBuffers(static_cast<GLsizei>(p->vbo_.size()), p->vbo_.data()); | ||||
|    gl.glGenBuffers(1, &p->vbo_); | ||||
| 
 | ||||
|    gl.glBindVertexArray(p->vao_); | ||||
| 
 | ||||
|    // Active box (dynamic sized)
 | ||||
|    gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_[0]); | ||||
|    gl.glBufferData( | ||||
|       GL_ARRAY_BUFFER, sizeof(float) * 5 * 2, nullptr, GL_DYNAMIC_DRAW); | ||||
| 
 | ||||
|    gl.glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, static_cast<void*>(0)); | ||||
|    gl.glEnableVertexAttribArray(0); | ||||
| 
 | ||||
|    // Upper right panel (dynamic sized)
 | ||||
|    gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_[1]); | ||||
|    gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_); | ||||
|    gl.glBufferData( | ||||
|       GL_ARRAY_BUFFER, sizeof(float) * 6 * 2, nullptr, GL_DYNAMIC_DRAW); | ||||
| 
 | ||||
|  | @ -164,25 +169,15 @@ void OverlayLayer::Render(const QMapbox::CustomLayerRenderParameters& params) | |||
|    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_) | ||||
|    { | ||||
|       const float vertexLX       = 1.0f; | ||||
|       const float vertexRX       = static_cast<float>(params.width) - 1.0f; | ||||
|       const float vertexTY       = static_cast<float>(params.height) - 1.0f; | ||||
|       const float vertexBY       = 1.0f; | ||||
|       const float vertices[5][2] = {{vertexLX, vertexTY},  // TL
 | ||||
|                                     {vertexLX, vertexBY},  // BL
 | ||||
|                                     {vertexRX, vertexBY},  // BR
 | ||||
|                                     {vertexRX, vertexTY},  // TR
 | ||||
|                                     {vertexLX, vertexTY}}; // TL
 | ||||
| 
 | ||||
|       // Draw vertices
 | ||||
|       gl.glBindVertexArray(p->vao_); | ||||
|       gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_[0]); | ||||
|       gl.glVertexAttribPointer( | ||||
|          0, 2, GL_FLOAT, GL_FALSE, 0, static_cast<void*>(0)); | ||||
|       gl.glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); | ||||
|       gl.glDrawArrays(GL_LINE_STRIP, 0, 5); | ||||
|       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) | ||||
|  | @ -207,7 +202,7 @@ void OverlayLayer::Render(const QMapbox::CustomLayerRenderParameters& params) | |||
| 
 | ||||
|       // Draw vertices
 | ||||
|       gl.glBindVertexArray(p->vao_); | ||||
|       gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_[1]); | ||||
|       gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_); | ||||
|       gl.glVertexAttribPointer( | ||||
|          0, 2, GL_FLOAT, GL_FALSE, 0, static_cast<void*>(0)); | ||||
|       gl.glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); | ||||
|  | @ -225,6 +220,8 @@ void OverlayLayer::Render(const QMapbox::CustomLayerRenderParameters& params) | |||
|                                 gl::TextAlign::Right); | ||||
|    } | ||||
| 
 | ||||
|    DrawLayer::Render(params); | ||||
| 
 | ||||
|    SCWX_GL_CHECK_ERROR(); | ||||
| } | ||||
| 
 | ||||
|  | @ -232,16 +229,18 @@ void OverlayLayer::Deinitialize() | |||
| { | ||||
|    BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Deinitialize()"; | ||||
| 
 | ||||
|    DrawLayer::Deinitialize(); | ||||
| 
 | ||||
|    gl::OpenGLFunctions& gl = context()->gl_; | ||||
| 
 | ||||
|    gl.glDeleteVertexArrays(1, &p->vao_); | ||||
|    gl.glDeleteBuffers(static_cast<GLsizei>(p->vbo_.size()), p->vbo_.data()); | ||||
|    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->vbo_                = GL_INVALID_INDEX; | ||||
|    p->texture_            = GL_INVALID_INDEX; | ||||
| 
 | ||||
|    disconnect(context()->radarProductView_.get(), | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <scwx/qt/map/generic_layer.hpp> | ||||
| #include <scwx/qt/map/draw_layer.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
|  | @ -11,7 +11,7 @@ namespace map | |||
| 
 | ||||
| class OverlayLayerImpl; | ||||
| 
 | ||||
| class OverlayLayer : public GenericLayer | ||||
| class OverlayLayer : public DrawLayer | ||||
| { | ||||
| public: | ||||
|    explicit OverlayLayer(std::shared_ptr<MapContext> context); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat