Move texture binding to layer, and reference atlas coordinates in draw item

This commit is contained in:
Dan Paulat 2022-10-06 00:35:22 -05:00
parent d3f7347be0
commit 0fa6ef01f0
2 changed files with 34 additions and 13 deletions

View file

@ -1,4 +1,5 @@
#include <scwx/qt/gl/draw/geo_line.hpp> #include <scwx/qt/gl/draw/geo_line.hpp>
#include <scwx/qt/util/texture_atlas.hpp>
#include <scwx/common/geographic.hpp> #include <scwx/common/geographic.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
@ -38,7 +39,7 @@ public:
uMVPMatrixLocation_(GL_INVALID_INDEX), uMVPMatrixLocation_(GL_INVALID_INDEX),
uMapMatrixLocation_(GL_INVALID_INDEX), uMapMatrixLocation_(GL_INVALID_INDEX),
uMapScreenCoordLocation_(GL_INVALID_INDEX), uMapScreenCoordLocation_(GL_INVALID_INDEX),
texture_ {GL_INVALID_INDEX}, texture_ {},
vao_ {GL_INVALID_INDEX}, vao_ {GL_INVALID_INDEX},
vbo_ {GL_INVALID_INDEX} vbo_ {GL_INVALID_INDEX}
{ {
@ -61,7 +62,8 @@ public:
GLint uMapMatrixLocation_; GLint uMapMatrixLocation_;
GLint uMapScreenCoordLocation_; GLint uMapScreenCoordLocation_;
GLuint texture_; util::TextureAttributes texture_;
GLuint vao_; GLuint vao_;
GLuint vbo_; GLuint vbo_;
@ -106,7 +108,7 @@ void GeoLine::Initialize()
} }
p->texture_ = p->texture_ =
p->context_->GetTexture(":/res/textures/lines/default-1x7.png"); util::TextureAtlas::Instance().GetTextureAttributes("lines/default-1x7");
gl.glGenVertexArrays(1, &p->vao_); gl.glGenVertexArrays(1, &p->vao_);
gl.glGenBuffers(1, &p->vbo_); gl.glGenBuffers(1, &p->vbo_);
@ -170,9 +172,6 @@ void GeoLine::Render(const QMapbox::CustomLayerRenderParameters& params)
UseMapProjection( UseMapProjection(
params, p->uMapMatrixLocation_, p->uMapScreenCoordLocation_); params, p->uMapMatrixLocation_, p->uMapScreenCoordLocation_);
gl.glActiveTexture(GL_TEXTURE0);
gl.glBindTexture(GL_TEXTURE_2D, p->texture_);
// Draw line // Draw line
gl.glDrawArrays(GL_TRIANGLES, 0, 6); gl.glDrawArrays(GL_TRIANGLES, 0, 6);
} }
@ -231,17 +230,25 @@ void GeoLine::Impl::Update()
{ {
gl::OpenGLFunctions& gl = context_->gl(); gl::OpenGLFunctions& gl = context_->gl();
// Latitude and longitude coordinates in degrees
const float lx = points_[0].latitude_; const float lx = points_[0].latitude_;
const float rx = points_[1].latitude_; const float rx = points_[1].latitude_;
const float by = points_[0].longitude_; const float by = points_[0].longitude_;
const float ty = points_[1].longitude_; const float ty = points_[1].longitude_;
// Offset x/y in pixels
const double i = points_[1].longitude_ - points_[0].longitude_; const double i = points_[1].longitude_ - points_[0].longitude_;
const double j = points_[1].latitude_ - points_[0].latitude_; const double j = points_[1].latitude_ - points_[0].latitude_;
const double angle = std::atan2(i, j) * 180.0 / M_PI; const double angle = std::atan2(i, j) * 180.0 / M_PI;
const float ox = width_ * 0.5f * std::cosf(angle); const float ox = width_ * 0.5f * std::cosf(angle);
const float oy = width_ * 0.5f * std::sinf(angle); const float oy = width_ * 0.5f * std::sinf(angle);
// Texture coordinates
const float ls = texture_.sLeft_;
const float rs = texture_.sRight_;
const float tt = texture_.tTop_;
const float bt = texture_.tBottom_;
float mc0 = 1.0f; float mc0 = 1.0f;
float mc1 = 1.0f; float mc1 = 1.0f;
float mc2 = 1.0f; float mc2 = 1.0f;
@ -262,12 +269,12 @@ void GeoLine::Impl::Update()
{ // { //
// Line // Line
{ {
{lx, by, -ox, -oy, 0.0f, 1.0f, mc0, mc1, mc2, mc3}, // BL {lx, by, -ox, -oy, ls, bt, mc0, mc1, mc2, mc3}, // BL
{lx, by, +ox, +oy, 0.0f, 0.0f, mc0, mc1, mc2, mc3}, // TL {lx, by, +ox, +oy, ls, tt, mc0, mc1, mc2, mc3}, // TL
{rx, ty, -ox, -oy, 1.0f, 1.0f, mc0, mc1, mc2, mc3}, // BR {rx, ty, -ox, -oy, rs, bt, mc0, mc1, mc2, mc3}, // BR
{rx, ty, -ox, -oy, 1.0f, 1.0f, mc0, mc1, mc2, mc3}, // BR {rx, ty, -ox, -oy, rs, bt, mc0, mc1, mc2, mc3}, // BR
{rx, ty, +ox, +oy, 1.0f, 0.0f, mc0, mc1, mc2, mc3}, // TR {rx, ty, +ox, +oy, rs, tt, mc0, mc1, mc2, mc3}, // TR
{lx, by, +ox, +oy, 0.0f, 0.0f, mc0, mc1, mc2, mc3} // TL {lx, by, +ox, +oy, ls, tt, mc0, mc1, mc2, mc3} // TL
}}; }};
gl.glBufferData(GL_ARRAY_BUFFER, gl.glBufferData(GL_ARRAY_BUFFER,

View file

@ -15,10 +15,15 @@ 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) :
context_ {context}, drawList_ {}, textureAtlas_ {GL_INVALID_INDEX}
{
}
~DrawLayerImpl() {} ~DrawLayerImpl() {}
std::shared_ptr<MapContext> context_;
std::vector<std::shared_ptr<gl::draw::DrawItem>> drawList_; std::vector<std::shared_ptr<gl::draw::DrawItem>> drawList_;
GLuint textureAtlas_;
}; };
DrawLayer::DrawLayer(std::shared_ptr<MapContext> context) : DrawLayer::DrawLayer(std::shared_ptr<MapContext> context) :
@ -29,6 +34,8 @@ DrawLayer::~DrawLayer() = default;
void DrawLayer::Initialize() void DrawLayer::Initialize()
{ {
p->textureAtlas_ = p->context_->GetTextureAtlas();
for (auto& item : p->drawList_) for (auto& item : p->drawList_)
{ {
item->Initialize(); item->Initialize();
@ -37,6 +44,11 @@ void DrawLayer::Initialize()
void DrawLayer::Render(const QMapbox::CustomLayerRenderParameters& params) void DrawLayer::Render(const QMapbox::CustomLayerRenderParameters& params)
{ {
gl::OpenGLFunctions& gl = p->context_->gl();
gl.glActiveTexture(GL_TEXTURE0);
gl.glBindTexture(GL_TEXTURE_2D, p->textureAtlas_);
for (auto& item : p->drawList_) for (auto& item : p->drawList_)
{ {
item->Render(params); item->Render(params);
@ -45,6 +57,8 @@ void DrawLayer::Render(const QMapbox::CustomLayerRenderParameters& params)
void DrawLayer::Deinitialize() void DrawLayer::Deinitialize()
{ {
p->textureAtlas_ = GL_INVALID_INDEX;
for (auto& item : p->drawList_) for (auto& item : p->drawList_)
{ {
item->Deinitialize(); item->Deinitialize();