Adding custom offset and scaling for color table to fragment shader

This commit is contained in:
Dan Paulat 2021-11-13 01:50:44 -06:00
parent 3e92847901
commit aa48d8610a
6 changed files with 60 additions and 14 deletions

View file

@ -4,6 +4,8 @@
precision mediump float; precision mediump float;
uniform sampler1D uTexture; uniform sampler1D uTexture;
uniform uint uDataMomentOffset;
uniform float uDataMomentScale;
flat in uint dataMoment; flat in uint dataMoment;
@ -11,7 +13,7 @@ layout (location = 0) out vec4 fragColor;
void main() void main()
{ {
float texCoord = float(dataMoment - 2u) / 253.0f; // TODO: Scale properly float texCoord = float(dataMoment - uDataMomentOffset) / uDataMomentScale;
fragColor = texture(uTexture, texCoord); fragColor = texture(uTexture, texCoord);
} }

View file

@ -53,6 +53,8 @@ public:
gl::ShaderProgram shaderProgram_; gl::ShaderProgram shaderProgram_;
GLint uMVPMatrixLocation_; GLint uMVPMatrixLocation_;
GLint uMapScreenCoordLocation_; GLint uMapScreenCoordLocation_;
GLint uDataMomentOffsetLocation_;
GLint uDataMomentScaleLocation_;
std::array<GLuint, 2> vbo_; std::array<GLuint, 2> vbo_;
GLuint vao_; GLuint vao_;
GLuint texture_; GLuint texture_;
@ -95,6 +97,24 @@ void RadarProductLayer::initialize()
<< logPrefix_ << "Could not find uMapScreenCoord"; << logPrefix_ << "Could not find uMapScreenCoord";
} }
p->uDataMomentOffsetLocation_ =
gl.glGetUniformLocation(p->shaderProgram_.id(), "uDataMomentOffset");
if (p->uDataMomentOffsetLocation_ == -1)
{
BOOST_LOG_TRIVIAL(warning)
<< logPrefix_ << "Could not find uDataMomentOffset";
}
p->uDataMomentScaleLocation_ =
gl.glGetUniformLocation(p->shaderProgram_.id(), "uDataMomentScale");
if (p->uDataMomentScaleLocation_ == -1)
{
BOOST_LOG_TRIVIAL(warning)
<< logPrefix_ << "Could not find uDataMomentScale";
}
p->shaderProgram_.Use();
// Generate a vertex array object // Generate a vertex array object
gl.glGenVertexArrays(1, &p->vao_); gl.glGenVertexArrays(1, &p->vao_);
@ -187,6 +207,8 @@ void RadarProductLayer::render(
{ {
gl::OpenGLFunctions& gl = p->gl_; gl::OpenGLFunctions& gl = p->gl_;
p->shaderProgram_.Use();
if (p->colorTableNeedsUpdate_) if (p->colorTableNeedsUpdate_)
{ {
UpdateColorTable(); UpdateColorTable();
@ -197,8 +219,6 @@ void RadarProductLayer::render(
UpdateSweep(); UpdateSweep();
} }
p->shaderProgram_.Use();
const float scale = std::pow(2.0, params.zoom) * 2.0f * const float scale = std::pow(2.0, params.zoom) * 2.0f *
mbgl::util::tileSize / mbgl::util::DEGREES_MAX; mbgl::util::tileSize / mbgl::util::DEGREES_MAX;
const float xScale = scale / params.width; const float xScale = scale / params.width;
@ -252,12 +272,18 @@ void RadarProductLayer::UpdateColorTable()
{ {
BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "UpdateColorTable()"; BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "UpdateColorTable()";
uint16_t rangeMin;
uint16_t rangeMax;
float scale;
p->colorTableNeedsUpdate_ = false; p->colorTableNeedsUpdate_ = false;
gl::OpenGLFunctions& gl = p->gl_; gl::OpenGLFunctions& gl = p->gl_;
const std::vector<boost::gil::rgba8_pixel_t>& colorTable = const std::vector<boost::gil::rgba8_pixel_t>& colorTable =
p->radarProductView_->color_table(); p->radarProductView_->color_table(rangeMin, rangeMax);
scale = rangeMax - rangeMin;
gl.glActiveTexture(GL_TEXTURE0); gl.glActiveTexture(GL_TEXTURE0);
gl.glBindTexture(GL_TEXTURE_1D, p->texture_); gl.glBindTexture(GL_TEXTURE_1D, p->texture_);
@ -270,6 +296,9 @@ void RadarProductLayer::UpdateColorTable()
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
colorTable.data()); colorTable.data());
gl.glGenerateMipmap(GL_TEXTURE_1D); gl.glGenerateMipmap(GL_TEXTURE_1D);
gl.glUniform1ui(p->uDataMomentOffsetLocation_, rangeMin);
gl.glUniform1f(p->uDataMomentScaleLocation_, rangeMax - rangeMin);
} }
void RadarProductLayer::UpdateColorTableNextFrame() void RadarProductLayer::UpdateColorTableNextFrame()

View file

@ -47,7 +47,9 @@ public:
longitude_ {}, longitude_ {},
sweepTime_ {}, sweepTime_ {},
colorTable_ {}, colorTable_ {},
colorTableLut_ {} colorTableLut_ {},
colorTableMin_ {2},
colorTableMax_ {254}
{ {
auto it = blockTypes_.find(product); auto it = blockTypes_.find(product);
@ -81,6 +83,8 @@ public:
std::shared_ptr<common::ColorTable> colorTable_; std::shared_ptr<common::ColorTable> colorTable_;
std::vector<boost::gil::rgba8_pixel_t> colorTableLut_; std::vector<boost::gil::rgba8_pixel_t> colorTableLut_;
uint16_t colorTableMin_;
uint16_t colorTableMax_;
std::shared_ptr<common::ColorTable> savedColorTable_; std::shared_ptr<common::ColorTable> savedColorTable_;
float savedScale_; float savedScale_;
@ -100,14 +104,16 @@ Level2ProductView::Level2ProductView(
Level2ProductView::~Level2ProductView() = default; Level2ProductView::~Level2ProductView() = default;
const std::vector<boost::gil::rgba8_pixel_t>& const std::vector<boost::gil::rgba8_pixel_t>&
Level2ProductView::color_table() const Level2ProductView::color_table(uint16_t& minValue, uint16_t& maxValue) const
{ {
if (p->colorTableLut_.size() == 0) if (p->colorTableLut_.size() == 0)
{ {
return RadarProductView::color_table(); return RadarProductView::color_table(minValue, maxValue);
} }
else else
{ {
minValue = p->colorTableMin_;
maxValue = p->colorTableMax_;
return p->colorTableLut_; return p->colorTableLut_;
} }
} }
@ -216,6 +222,9 @@ void Level2ProductView::UpdateColorTable()
lut[i - *dataRange.begin()] = p->colorTable_->Color(f); lut[i - *dataRange.begin()] = p->colorTable_->Color(f);
}); });
p->colorTableMin_ = rangeMin;
p->colorTableMax_ = rangeMax;
p->savedColorTable_ = p->colorTable_; p->savedColorTable_ = p->colorTable_;
p->savedOffset_ = offset; p->savedOffset_ = offset;
p->savedScale_ = scale; p->savedScale_ = scale;

View file

@ -28,9 +28,10 @@ public:
std::shared_ptr<manager::RadarProductManager> radarProductManager); std::shared_ptr<manager::RadarProductManager> radarProductManager);
~Level2ProductView(); ~Level2ProductView();
const std::vector<boost::gil::rgba8_pixel_t>& color_table() const override; const std::vector<boost::gil::rgba8_pixel_t>&
std::chrono::system_clock::time_point sweep_time() const override; color_table(uint16_t& minValue, uint16_t& maxValue) const override;
const std::vector<float>& vertices() const override; std::chrono::system_clock::time_point sweep_time() const override;
const std::vector<float>& vertices() const override;
void LoadColorTable(std::shared_ptr<common::ColorTable> colorTable) override; void LoadColorTable(std::shared_ptr<common::ColorTable> colorTable) override;

View file

@ -18,6 +18,8 @@ static const std::vector<boost::gil::rgba8_pixel_t> DEFAULT_COLOR_TABLE = {
boost::gil::rgba8_pixel_t(0, 128, 0, 255), boost::gil::rgba8_pixel_t(0, 128, 0, 255),
boost::gil::rgba8_pixel_t(255, 192, 0, 255), boost::gil::rgba8_pixel_t(255, 192, 0, 255),
boost::gil::rgba8_pixel_t(255, 0, 0, 255)}; boost::gil::rgba8_pixel_t(255, 0, 0, 255)};
static const uint16_t DEFAULT_COLOR_TABLE_MIN = 2u;
static const uint16_t DEFAULT_COLOR_TABLE_MAX = 255u;
class RadarProductViewImpl class RadarProductViewImpl
{ {
@ -31,8 +33,10 @@ RadarProductView::RadarProductView() :
RadarProductView::~RadarProductView() = default; RadarProductView::~RadarProductView() = default;
const std::vector<boost::gil::rgba8_pixel_t>& const std::vector<boost::gil::rgba8_pixel_t>&
RadarProductView::color_table() const RadarProductView::color_table(uint16_t& minValue, uint16_t& maxValue) const
{ {
minValue = DEFAULT_COLOR_TABLE_MIN;
maxValue = DEFAULT_COLOR_TABLE_MAX;
return DEFAULT_COLOR_TABLE; return DEFAULT_COLOR_TABLE;
} }

View file

@ -25,9 +25,10 @@ public:
explicit RadarProductView(); explicit RadarProductView();
~RadarProductView(); ~RadarProductView();
virtual const std::vector<boost::gil::rgba8_pixel_t>& color_table() const; virtual const std::vector<boost::gil::rgba8_pixel_t>&
virtual std::chrono::system_clock::time_point sweep_time() const; color_table(uint16_t& minValue, uint16_t& maxValue) const;
virtual const std::vector<float>& vertices() const = 0; virtual std::chrono::system_clock::time_point sweep_time() const;
virtual const std::vector<float>& vertices() const = 0;
void Initialize(); void Initialize();
virtual void virtual void