diff --git a/CMakeLists.txt b/CMakeLists.txt index 063b46549..7af562762 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,7 +117,7 @@ set(sdrbase_SOURCES sdrbase/gui/colormapper.cpp sdrbase/gui/glscope.cpp sdrbase/gui/glscopegui.cpp - sdrbase/gui/glshadersources.cpp + sdrbase/gui/glshadersimplepolyline.cpp sdrbase/gui/glspectrum.cpp sdrbase/gui/glspectrumgui.cpp sdrbase/gui/indicator.cpp @@ -200,7 +200,7 @@ set(sdrbase_HEADERS include/gui/colormapper.h include/gui/glscope.h include/gui/glscopegui.h - include/gui/glshadersources.h + include/gui/glshadersimplepolyline.h include/gui/glspectrum.h include/gui/glspectrumgui.h include/gui/indicator.h diff --git a/include/gui/glshadersources.h b/include/gui/glshadersimplepolyline.h similarity index 74% rename from include/gui/glshadersources.h rename to include/gui/glshadersimplepolyline.h index 7f2ecdffa..ebaf5c6f3 100644 --- a/include/gui/glshadersources.h +++ b/include/gui/glshadersimplepolyline.h @@ -15,19 +15,31 @@ // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// -#ifndef INCLUDE_GUI_GLSHADERSOURCES_H_ -#define INCLUDE_GUI_GLSHADERSOURCES_H_ +#ifndef INCLUDE_GUI_GLSHADERSIMPLEPOLYLINE_H_ +#define INCLUDE_GUI_GLSHADERSIMPLEPOLYLINE_H_ #include -class GLShaderSources +class QOpenGLShaderProgram; +class QMatrix4x4; +class QVector4D; + +class GLShaderSimplePolyline { public: - static const QString& getVertexShaderSourceSimple() { return m_vertexShaderSourceSimple; } - static const QString& getFragmentShaderSourceColored() { return m_fragmentShaderSourceColored; } + GLShaderSimplePolyline(); + ~GLShaderSimplePolyline(); + + void initializeGL(); + void draw(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices); + void cleanup(); + private: + QOpenGLShaderProgram *m_program; + int m_matrixLoc; + int m_colorLoc; static const QString m_vertexShaderSourceSimple; static const QString m_fragmentShaderSourceColored; }; -#endif /* INCLUDE_GUI_GLSHADERSOURCES_H_ */ +#endif /* INCLUDE_GUI_GLSHADERSIMPLEPOLYLINE_H_ */ diff --git a/include/gui/glspectrum.h b/include/gui/glspectrum.h index e50f05a17..f593b6f91 100644 --- a/include/gui/glspectrum.h +++ b/include/gui/glspectrum.h @@ -26,6 +26,7 @@ #include #include "dsp/dsptypes.h" #include "gui/scaleengine.h" +#include "gui/glshadersimplepolyline.h" #include "dsp/channelmarker.h" #include "util/export.h" @@ -152,6 +153,7 @@ private: int m_histogramStroke; QRectF m_glHistogramRect; QMatrix4x4 m_glHistogramMatrix; + GLShaderSimplePolyline m_glShaderSimplePolyline; bool m_displayHistogram; bool m_displayChanged; diff --git a/sdrbase/gui/glshadersimplepolyline.cpp b/sdrbase/gui/glshadersimplepolyline.cpp new file mode 100644 index 000000000..8dcc48a7c --- /dev/null +++ b/sdrbase/gui/glshadersimplepolyline.cpp @@ -0,0 +1,86 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2016 F4EXB // +// written by Edouard Griffiths // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include + +#include + +GLShaderSimplePolyline::GLShaderSimplePolyline() : + m_program(0) +{ } + +GLShaderSimplePolyline::~GLShaderSimplePolyline() +{ + cleanup(); +} + +void GLShaderSimplePolyline::initializeGL() +{ + m_program = new QOpenGLShaderProgram; + m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShaderSourceSimple); + m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSourceColored); + m_program->bindAttributeLocation("vertex", 0); + m_program->link(); + m_program->bind(); + m_matrixLoc = m_program->uniformLocation("uMatrix"); + m_colorLoc = m_program->uniformLocation("uColour"); + m_program->release(); +} + +void GLShaderSimplePolyline::draw(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices) +{ + QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); + m_program->bind(); + m_program->setUniformValue(m_matrixLoc, transformMatrix); + m_program->setUniformValue(m_colorLoc, color); + f->glEnable(GL_BLEND); + f->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + f->glLineWidth(1.0f); + f->glEnableVertexAttribArray(0); // vertex + f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices); + f->glDrawArrays(GL_LINE_STRIP, 0, nbVertices); + f->glDisableVertexAttribArray(0); + m_program->release(); +} + +void GLShaderSimplePolyline::cleanup() +{ + if (m_program) + { + delete m_program; + m_program = 0; + } +} + +const QString GLShaderSimplePolyline::m_vertexShaderSourceSimple = QString( + "uniform mat4 uMatrix;\n" + "attribute vec4 vertex;\n" + "void main() {\n" + " gl_Position = uMatrix * vertex;\n" + "}\n" + ); + +const QString GLShaderSimplePolyline::m_fragmentShaderSourceColored = QString( + "uniform mediump vec4 uColour;\n" + "void main() {\n" + " gl_FragColor = uColour;\n" + "}\n" + ); diff --git a/sdrbase/gui/glshadersources.cpp b/sdrbase/gui/glshadersources.cpp deleted file mode 100644 index f9ca5ed97..000000000 --- a/sdrbase/gui/glshadersources.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////////// -// Copyright (C) 2016 F4EXB // -// written by Edouard Griffiths // -// // -// This program is free software; you can redistribute it and/or modify // -// it under the terms of the GNU General Public License as published by // -// the Free Software Foundation as version 3 of the License, or // -// // -// This program is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY; without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU General Public License V3 for more details. // -// // -// You should have received a copy of the GNU General Public License // -// along with this program. If not, see . // -/////////////////////////////////////////////////////////////////////////////////// - -#include "gui/glshadersources.h" - -const QString GLShaderSources::m_vertexShaderSourceSimple = QString( - "uniform mat4 uMatrix;\n" - "attribute vec4 vertex;\n" - "void main() {\n" - " gl_Position = uMatrix * vertex;\n" - "}\n" - ); - -const QString GLShaderSources::m_fragmentShaderSourceColored = QString( - "uniform mediump vec4 uColour;\n" - "void main() {\n" - " gl_FragColor = uColour;\n" - "}\n" - ); diff --git a/sdrbase/gui/glspectrum.cpp b/sdrbase/gui/glspectrum.cpp index 44defb983..788f8a1e9 100644 --- a/sdrbase/gui/glspectrum.cpp +++ b/sdrbase/gui/glspectrum.cpp @@ -23,7 +23,7 @@ #include #include #include "gui/glspectrum.h" -#include "gui/glshadersources.h" +#include #include @@ -507,16 +507,7 @@ void GLSpectrum::initializeGL() connect(glCurrentContext, &QOpenGLContext::aboutToBeDestroyed, this, &GLSpectrum::cleanup); // TODO: when migrating to QOpenGLWidget glDisable(GL_DEPTH_TEST); - - m_program = new QOpenGLShaderProgram; - m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, GLShaderSources::getVertexShaderSourceSimple()); - m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, GLShaderSources::getFragmentShaderSourceColored()); - m_program->bindAttributeLocation("vertex", 0); - m_program->link(); - m_program->bind(); - m_matrixLoc = m_program->uniformLocation("uMatrix"); - m_colorLoc = m_program->uniformLocation("uColour"); - m_program->release(); + m_glShaderSimplePolyline.initializeGL(); } void GLSpectrum::resizeGL(int width, int height) @@ -1241,20 +1232,8 @@ void GLSpectrum::paintGL() q3[2*i+1] = v; } - QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); - m_program->bind(); QVector4D color(1.0f, 1.0f, 0.25f, (float) m_displayTraceIntensity / 100.0f); - m_program->setUniformValue(m_matrixLoc, m_glHistogramMatrix); - m_program->setUniformValue(m_colorLoc, color); - f->glEnable(GL_BLEND); - f->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - f->glLineWidth(1.0f); - f->glEnableVertexAttribArray(0); // vertex - f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, q3); - f->glDrawArrays(GL_LINE_STRIP, 0, m_fftSize); - f->glDisableVertexAttribArray(0); - m_program->release(); - //QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao); + m_glShaderSimplePolyline.draw(m_glHistogramMatrix, color, q3, m_fftSize); } #endif } @@ -2109,9 +2088,6 @@ void GLSpectrum::connectTimer(const QTimer& timer) void GLSpectrum::cleanup() { makeCurrent(); - if (m_program) { - delete m_program; - m_program = 0; - } + m_glShaderSimplePolyline.cleanup(); doneCurrent(); }