Skip to content

Commit

Permalink
Spectrum: Enable alpha blending on gradient fill, so channel markers …
Browse files Browse the repository at this point in the history
…are visible
  • Loading branch information
srcejon committed Jun 22, 2022
1 parent b8576cf commit 5c214c0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
13 changes: 10 additions & 3 deletions sdrgui/gui/glshadercolormap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ GLShaderColorMap::GLShaderColorMap() :
m_matrixLoc(0),
m_colorMapLoc(0),
m_scaleLoc(0),
m_alphaLoc(0),
m_useImmutableStorage(true)
{ }

Expand Down Expand Up @@ -86,6 +87,7 @@ void GLShaderColorMap::initializeGL(int majorVersion, int minorVersion)
m_matrixLoc = m_program->uniformLocation("uMatrix");
m_colorMapLoc = m_program->uniformLocation("colorMap");
m_scaleLoc = m_program->uniformLocation("scale");
m_alphaLoc = m_program->uniformLocation("alpha");
if (m_vao)
{
m_verticesBuf = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
Expand Down Expand Up @@ -149,14 +151,15 @@ void GLShaderColorMap::initColorMapTextureMutable(const QString &colorMapName)
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, QOpenGLTexture::Repeat);
}

void GLShaderColorMap::drawSurfaceStrip(const QMatrix4x4& transformMatrix, GLfloat *vertices, int nbVertices, float scale)
void GLShaderColorMap::drawSurfaceStrip(const QMatrix4x4& transformMatrix, GLfloat *vertices, int nbVertices, float scale, float alpha)
{
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
m_program->bind();
m_program->setUniformValue(m_matrixLoc, transformMatrix);
m_colorMapTexture->bind();
m_program->setUniformValue(m_colorMapLoc, 0); // Texture unit 0 for color map
m_program->setUniformValue(m_scaleLoc, scale);
m_program->setUniformValue(m_alphaLoc, alpha);
if (m_vao)
{
m_vao->bind();
Expand All @@ -172,6 +175,8 @@ void GLShaderColorMap::drawSurfaceStrip(const QMatrix4x4& transformMatrix, GLflo
f->glVertexAttribPointer(m_vertexLoc, 2, GL_FLOAT, GL_FALSE, 0, vertices);
}

f->glEnable(GL_BLEND);
f->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
f->glDrawArrays(GL_TRIANGLE_STRIP, 0, nbVertices);

if (m_vao)
Expand Down Expand Up @@ -256,21 +261,23 @@ const QString GLShaderColorMap::m_vertexShaderSourceColorMap = QString(
);

const QString GLShaderColorMap::m_fragmentShaderSourceColorMap2 = QString(
"uniform float alpha;\n"
"uniform float scale;\n"
"uniform highp sampler1D colorMap;\n"
"varying float y;\n"
"void main() {\n"
" gl_FragColor = texture1D(colorMap, 1.0-(y/scale));\n"
" gl_FragColor = vec4(texture1D(colorMap, 1.0-(y/scale)).rgb, alpha);\n"
"}\n"
);

const QString GLShaderColorMap::m_fragmentShaderSourceColorMap = QString(
"#version 330\n"
"uniform float alpha;\n"
"uniform float scale;\n"
"uniform sampler1D colorMap;\n"
"in float y;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = texture(colorMap, 1.0-(y/scale));\n"
" fragColor = vec4(texture(colorMap, 1.0-(y/scale)).rgb, alpha);\n"
"}\n"
);
3 changes: 2 additions & 1 deletion sdrgui/gui/glshadercolormap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SDRGUI_API GLShaderColorMap : protected QOpenGLFunctions

void initializeGL(int majorVersion, int minorVersion);
void initColorMapTexture(const QString &colorMapName);
void drawSurfaceStrip(const QMatrix4x4& transformMatrix, GLfloat *vertices, int nbVertices, float scale);
void drawSurfaceStrip(const QMatrix4x4& transformMatrix, GLfloat *vertices, int nbVertices, float scale, float alpha);
void cleanup();

private:
Expand All @@ -58,6 +58,7 @@ class SDRGUI_API GLShaderColorMap : protected QOpenGLFunctions
int m_matrixLoc;
int m_colorMapLoc;
int m_scaleLoc;
int m_alphaLoc;
bool m_useImmutableStorage;
static const QString m_vertexShaderSourceColorMap2;
static const QString m_vertexShaderSourceColorMap;
Expand Down
2 changes: 1 addition & 1 deletion sdrgui/gui/glspectrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ void GLSpectrum::paintGL()

QVector4D color(1.0f, 1.0f, 0.25f, (float) m_displayTraceIntensity / 100.0f);
if (m_spectrumStyle == SpectrumSettings::Gradient) {
m_glShaderColorMap.drawSurfaceStrip(m_glHistogramSpectrumMatrix, q3, 2*m_nbBins, bottom);
m_glShaderColorMap.drawSurfaceStrip(m_glHistogramSpectrumMatrix, q3, 2*m_nbBins, bottom, 0.75f);
} else {
m_glShaderSimple.drawSurfaceStrip(m_glHistogramSpectrumMatrix, color, q3, 2*m_nbBins);
}
Expand Down

0 comments on commit 5c214c0

Please sign in to comment.