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>
#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 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<DrawItemImpl>()) {}
DrawItem::DrawItem(OpenGLFunctions& gl) : p(std::make_unique<Impl>(gl)) {}
DrawItem::~DrawItem() = 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<float>(params.width),
0.0f,
static_cast<float>(params.height));
p->gl_.glUniformMatrix4fv(
uMVPMatrixLocation, 1, GL_FALSE, glm::value_ptr(projection));
}
} // namespace draw
} // namespace gl
} // namespace qt

View file

@ -1,7 +1,11 @@
#pragma once
#include <scwx/qt/gl/gl.hpp>
#include <memory>
#include <QMapbox>
namespace scwx
{
namespace qt
@ -11,12 +15,10 @@ namespace gl
namespace draw
{
class DrawItemImpl;
class DrawItem
{
public:
explicit DrawItem();
explicit DrawItem(OpenGLFunctions& gl);
~DrawItem();
DrawItem(const DrawItem&) = delete;
@ -26,11 +28,17 @@ public:
DrawItem& operator=(DrawItem&&) noexcept;
virtual void Initialize() = 0;
virtual void Render() = 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<DrawItemImpl> p;
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace draw

View file

@ -59,7 +59,7 @@ public:
// TODO: OpenGL context with shaders
GeoLine::GeoLine(OpenGLFunctions& gl) :
DrawItem(), p(std::make_unique<Impl>(gl))
DrawItem(gl), p(std::make_unique<Impl>(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_)
{

View file

@ -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;
/**

View file

@ -1,4 +1,5 @@
#include <scwx/qt/gl/draw/rectangle.hpp>
#include <scwx/util/logger.hpp>
#include <optional>
@ -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<GlContext> 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<GlContext> context_;
bool dirty_;
@ -59,15 +63,17 @@ public:
std::optional<boost::gil::rgba8_pixel_t> fillColor_;
std::shared_ptr<ShaderProgram> shaderProgram_;
GLint uMVPMatrixLocation_;
GLuint vao_;
GLuint vbo_;
void Update();
};
// TODO: OpenGL context with shaders
Rectangle::Rectangle(OpenGLFunctions& gl) :
DrawItem(), p(std::make_unique<RectangleImpl>(gl))
Rectangle::Rectangle(std::shared_ptr<GlContext> context) :
DrawItem(context->gl()), p(std::make_unique<Impl>(context))
{
}
Rectangle::~Rectangle() = default;
@ -77,7 +83,17 @@ 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_;

View file

@ -1,6 +1,6 @@
#pragma once
#include <scwx/qt/gl/gl.hpp>
#include <scwx/qt/gl/gl_context.hpp>
#include <scwx/qt/gl/draw/draw_item.hpp>
#include <boost/gil.hpp>
@ -14,12 +14,10 @@ namespace gl
namespace draw
{
class RectangleImpl;
class Rectangle : public DrawItem
{
public:
explicit Rectangle(OpenGLFunctions& gl);
explicit Rectangle(std::shared_ptr<GlContext> context);
~Rectangle();
Rectangle(const Rectangle&) = delete;
@ -29,7 +27,7 @@ public:
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<RectangleImpl> p;
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace draw

View file

@ -2,12 +2,6 @@
#include <scwx/qt/gl/shader_program.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 qt
@ -21,17 +15,9 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class DrawLayerImpl
{
public:
explicit DrawLayerImpl(std::shared_ptr<MapContext> context) :
shaderProgram_ {nullptr}, uMVPMatrixLocation_(GL_INVALID_INDEX)
{
}
explicit DrawLayerImpl(std::shared_ptr<MapContext> context) {}
~DrawLayerImpl() {}
std::shared_ptr<gl::ShaderProgram> shaderProgram_;
GLint uMVPMatrixLocation_;
std::vector<std::shared_ptr<gl::draw::DrawItem>> 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<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_)
{
item->Render();
item->Render(params);
}
}

View file

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