Define draw item shaders in draw items, not the generic draw layer

This commit is contained in:
Dan Paulat 2022-10-03 00:11:39 -05:00
parent d84a618d3d
commit 224d36bae5
8 changed files with 88 additions and 83 deletions

View file

@ -2,6 +2,12 @@
#include <string> #include <string>
#pragma warning(push, 0)
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#pragma warning(pop)
namespace scwx namespace scwx
{ {
namespace qt namespace qt
@ -13,20 +19,33 @@ namespace draw
static const std::string logPrefix_ = "scwx::qt::gl::draw::draw_item"; static const std::string logPrefix_ = "scwx::qt::gl::draw::draw_item";
class DrawItemImpl class DrawItem::Impl
{ {
public: public:
explicit DrawItemImpl() {} explicit Impl(OpenGLFunctions& gl) : gl_ {gl} {}
~Impl() {}
~DrawItemImpl() {} OpenGLFunctions& gl_;
}; };
DrawItem::DrawItem() : p(std::make_unique<DrawItemImpl>()) {} DrawItem::DrawItem(OpenGLFunctions& gl) : p(std::make_unique<Impl>(gl)) {}
DrawItem::~DrawItem() = default; DrawItem::~DrawItem() = default;
DrawItem::DrawItem(DrawItem&&) noexcept = default; DrawItem::DrawItem(DrawItem&&) noexcept = default;
DrawItem& DrawItem::operator=(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<float>(params.width),
0.0f,
static_cast<float>(params.height));
p->gl_.glUniformMatrix4fv(
uMVPMatrixLocation, 1, GL_FALSE, glm::value_ptr(projection));
}
} // namespace draw } // namespace draw
} // namespace gl } // namespace gl
} // namespace qt } // namespace qt

View file

@ -1,7 +1,11 @@
#pragma once #pragma once
#include <scwx/qt/gl/gl.hpp>
#include <memory> #include <memory>
#include <QMapbox>
namespace scwx namespace scwx
{ {
namespace qt namespace qt
@ -11,26 +15,30 @@ namespace gl
namespace draw namespace draw
{ {
class DrawItemImpl;
class DrawItem class DrawItem
{ {
public: public:
explicit DrawItem(); explicit DrawItem(OpenGLFunctions& gl);
~DrawItem(); ~DrawItem();
DrawItem(const DrawItem&) = delete; DrawItem(const DrawItem&) = delete;
DrawItem& operator=(const DrawItem&) = delete; DrawItem& operator=(const DrawItem&) = delete;
DrawItem(DrawItem&&) noexcept; DrawItem(DrawItem&&) noexcept;
DrawItem& operator=(DrawItem&&) noexcept; DrawItem& operator=(DrawItem&&) noexcept;
virtual void Initialize() = 0; virtual void Initialize() = 0;
virtual void Render() = 0; virtual void Render(const QMapbox::CustomLayerRenderParameters& params) = 0;
virtual void Deinitialize() = 0; virtual void Deinitialize() = 0;
protected:
void UseDefaultProjection(const QMapbox::CustomLayerRenderParameters& params,
GLint uMVPMatrixLocation);
private: private:
std::unique_ptr<DrawItemImpl> p; class Impl;
std::unique_ptr<Impl> p;
}; };
} // namespace draw } // namespace draw

View file

@ -59,7 +59,7 @@ public:
// TODO: OpenGL context with shaders // TODO: OpenGL context with shaders
GeoLine::GeoLine(OpenGLFunctions& gl) : GeoLine::GeoLine(OpenGLFunctions& gl) :
DrawItem(), p(std::make_unique<Impl>(gl)) DrawItem(gl), p(std::make_unique<Impl>(gl))
{ {
} }
GeoLine::~GeoLine() = default; GeoLine::~GeoLine() = default;
@ -118,7 +118,7 @@ void GeoLine::Initialize()
p->dirty_ = true; p->dirty_ = true;
} }
void GeoLine::Render() void GeoLine::Render(const QMapbox::CustomLayerRenderParameters&)
{ {
if (p->visible_) if (p->visible_)
{ {

View file

@ -27,7 +27,7 @@ public:
GeoLine& operator=(GeoLine&&) noexcept; GeoLine& operator=(GeoLine&&) noexcept;
void Initialize() override; void Initialize() override;
void Render() override; void Render(const QMapbox::CustomLayerRenderParameters& params) override;
void Deinitialize() override; void Deinitialize() override;
/** /**

View file

@ -1,4 +1,5 @@
#include <scwx/qt/gl/draw/rectangle.hpp> #include <scwx/qt/gl/draw/rectangle.hpp>
#include <scwx/util/logger.hpp>
#include <optional> #include <optional>
@ -12,6 +13,7 @@ namespace draw
{ {
static const std::string logPrefix_ = "scwx::qt::gl::draw::rectangle"; 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_RECTANGLES = 5;
static constexpr size_t NUM_TRIANGLES = NUM_RECTANGLES * 2; 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 = static constexpr size_t BUFFER_LENGTH =
NUM_TRIANGLES * VERTICES_PER_TRIANGLE * POINTS_PER_VERTEX; NUM_TRIANGLES * VERTICES_PER_TRIANGLE * POINTS_PER_VERTEX;
class RectangleImpl class Rectangle::Impl
{ {
public: public:
explicit RectangleImpl(OpenGLFunctions& gl) : explicit Impl(std::shared_ptr<GlContext> context) :
gl_ {gl}, context_ {context},
dirty_ {false}, dirty_ {false},
visible_ {true}, visible_ {true},
x_ {0.0f}, x_ {0.0f},
@ -36,14 +38,16 @@ public:
borderColor_ {0, 0, 0, 0}, borderColor_ {0, 0, 0, 0},
borderWidth_ {0.0f}, borderWidth_ {0.0f},
fillColor_ {std::nullopt}, fillColor_ {std::nullopt},
shaderProgram_ {nullptr},
uMVPMatrixLocation_(GL_INVALID_INDEX),
vao_ {GL_INVALID_INDEX}, vao_ {GL_INVALID_INDEX},
vbo_ {GL_INVALID_INDEX} vbo_ {GL_INVALID_INDEX}
{ {
} }
~RectangleImpl() {} ~Impl() {}
OpenGLFunctions& gl_; std::shared_ptr<GlContext> context_;
bool dirty_; bool dirty_;
@ -59,25 +63,37 @@ public:
std::optional<boost::gil::rgba8_pixel_t> fillColor_; std::optional<boost::gil::rgba8_pixel_t> fillColor_;
std::shared_ptr<ShaderProgram> shaderProgram_;
GLint uMVPMatrixLocation_;
GLuint vao_; GLuint vao_;
GLuint vbo_; GLuint vbo_;
void Update(); void Update();
}; };
// TODO: OpenGL context with shaders Rectangle::Rectangle(std::shared_ptr<GlContext> context) :
Rectangle::Rectangle(OpenGLFunctions& gl) : DrawItem(context->gl()), p(std::make_unique<Impl>(context))
DrawItem(), p(std::make_unique<RectangleImpl>(gl))
{ {
} }
Rectangle::~Rectangle() = default; Rectangle::~Rectangle() = default;
Rectangle::Rectangle(Rectangle&&) noexcept = default; Rectangle::Rectangle(Rectangle&&) noexcept = default;
Rectangle& Rectangle::operator=(Rectangle&&) noexcept = default; Rectangle& Rectangle::operator=(Rectangle&&) noexcept = default;
void Rectangle::Initialize() 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.glGenVertexArrays(1, &p->vao_);
gl.glGenBuffers(1, &p->vbo_); gl.glGenBuffers(1, &p->vbo_);
@ -106,16 +122,18 @@ void Rectangle::Initialize()
p->dirty_ = true; p->dirty_ = true;
} }
void Rectangle::Render() void Rectangle::Render(const QMapbox::CustomLayerRenderParameters& params)
{ {
if (p->visible_) if (p->visible_)
{ {
gl::OpenGLFunctions& gl = p->gl_; gl::OpenGLFunctions& gl = p->context_->gl();
gl.glBindVertexArray(p->vao_); gl.glBindVertexArray(p->vao_);
gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_); gl.glBindBuffer(GL_ARRAY_BUFFER, p->vbo_);
p->Update(); p->Update();
p->shaderProgram_->Use();
UseDefaultProjection(params, p->uMVPMatrixLocation_);
if (p->fillColor_.has_value()) if (p->fillColor_.has_value())
{ {
@ -133,7 +151,7 @@ void Rectangle::Render()
void Rectangle::Deinitialize() void Rectangle::Deinitialize()
{ {
gl::OpenGLFunctions& gl = p->gl_; gl::OpenGLFunctions& gl = p->context_->gl();
gl.glDeleteVertexArrays(1, &p->vao_); gl.glDeleteVertexArrays(1, &p->vao_);
gl.glDeleteBuffers(1, &p->vbo_); gl.glDeleteBuffers(1, &p->vbo_);
@ -184,11 +202,11 @@ void Rectangle::SetVisible(bool visible)
p->visible_ = visible; p->visible_ = visible;
} }
void RectangleImpl::Update() void Rectangle::Impl::Update()
{ {
if (dirty_) if (dirty_)
{ {
gl::OpenGLFunctions& gl = gl_; gl::OpenGLFunctions& gl = context_->gl();
const float lox = x_; const float lox = x_;
const float rox = x_ + width_; const float rox = x_ + width_;

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <scwx/qt/gl/gl.hpp> #include <scwx/qt/gl/gl_context.hpp>
#include <scwx/qt/gl/draw/draw_item.hpp> #include <scwx/qt/gl/draw/draw_item.hpp>
#include <boost/gil.hpp> #include <boost/gil.hpp>
@ -14,22 +14,20 @@ namespace gl
namespace draw namespace draw
{ {
class RectangleImpl;
class Rectangle : public DrawItem class Rectangle : public DrawItem
{ {
public: public:
explicit Rectangle(OpenGLFunctions& gl); explicit Rectangle(std::shared_ptr<GlContext> context);
~Rectangle(); ~Rectangle();
Rectangle(const Rectangle&) = delete; Rectangle(const Rectangle&) = delete;
Rectangle& operator=(const Rectangle&) = delete; Rectangle& operator=(const Rectangle&) = delete;
Rectangle(Rectangle&&) noexcept; Rectangle(Rectangle&&) noexcept;
Rectangle& operator=(Rectangle&&) noexcept; Rectangle& operator=(Rectangle&&) noexcept;
void Initialize() override; void Initialize() override;
void Render() override; void Render(const QMapbox::CustomLayerRenderParameters& params) override;
void Deinitialize() override; void Deinitialize() override;
void SetBorder(float width, boost::gil::rgba8_pixel_t color); void SetBorder(float width, boost::gil::rgba8_pixel_t color);
@ -39,7 +37,9 @@ public:
void SetVisible(bool visible); void SetVisible(bool visible);
private: private:
std::unique_ptr<RectangleImpl> p; class Impl;
std::unique_ptr<Impl> p;
}; };
} // namespace draw } // namespace draw

View file

@ -2,12 +2,6 @@
#include <scwx/qt/gl/shader_program.hpp> #include <scwx/qt/gl/shader_program.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#pragma warning(push, 0)
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#pragma warning(pop)
namespace scwx namespace scwx
{ {
namespace qt namespace qt
@ -21,17 +15,9 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class DrawLayerImpl class DrawLayerImpl
{ {
public: public:
explicit DrawLayerImpl(std::shared_ptr<MapContext> context) : explicit DrawLayerImpl(std::shared_ptr<MapContext> context) {}
shaderProgram_ {nullptr}, uMVPMatrixLocation_(GL_INVALID_INDEX)
{
}
~DrawLayerImpl() {} ~DrawLayerImpl() {}
std::shared_ptr<gl::ShaderProgram> shaderProgram_;
GLint uMVPMatrixLocation_;
std::vector<std::shared_ptr<gl::draw::DrawItem>> drawList_; std::vector<std::shared_ptr<gl::draw::DrawItem>> drawList_;
}; };
@ -43,20 +29,6 @@ DrawLayer::~DrawLayer() = default;
void DrawLayer::Initialize() 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_) for (auto& item : p->drawList_)
{ {
item->Initialize(); item->Initialize();
@ -65,21 +37,9 @@ void DrawLayer::Initialize()
void DrawLayer::Render(const QMapbox::CustomLayerRenderParameters& params) 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));
gl.glUniformMatrix4fv(
p->uMVPMatrixLocation_, 1, GL_FALSE, glm::value_ptr(projection));
for (auto& item : p->drawList_) for (auto& item : p->drawList_)
{ {
item->Render(); item->Render(params);
} }
} }

View file

@ -37,9 +37,9 @@ public:
textShader_(context), textShader_(context),
font_(util::Font::Create(":/res/fonts/din1451alt.ttf")), font_(util::Font::Create(":/res/fonts/din1451alt.ttf")),
texture_ {GL_INVALID_INDEX}, texture_ {GL_INVALID_INDEX},
activeBoxOuter_ {std::make_shared<gl::draw::Rectangle>(context->gl())}, activeBoxOuter_ {std::make_shared<gl::draw::Rectangle>(context)},
activeBoxInner_ {std::make_shared<gl::draw::Rectangle>(context->gl())}, activeBoxInner_ {std::make_shared<gl::draw::Rectangle>(context)},
timeBox_ {std::make_shared<gl::draw::Rectangle>(context->gl())}, timeBox_ {std::make_shared<gl::draw::Rectangle>(context)},
sweepTimeString_ {}, sweepTimeString_ {},
sweepTimeNeedsUpdate_ {true} sweepTimeNeedsUpdate_ {true}
{ {