1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-18 07:35:47 -05:00

OpenGL modernization: draw the spectrum line in the new way. New object GLShaderSimplePolyline

This commit is contained in:
f4exb 2016-03-02 23:55:29 +01:00
parent 79c717862c
commit 691a34cb16
6 changed files with 112 additions and 69 deletions

View File

@ -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

View File

@ -15,19 +15,31 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_GUI_GLSHADERSOURCES_H_
#define INCLUDE_GUI_GLSHADERSOURCES_H_
#ifndef INCLUDE_GUI_GLSHADERSIMPLEPOLYLINE_H_
#define INCLUDE_GUI_GLSHADERSIMPLEPOLYLINE_H_
#include <QString>
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_ */

View File

@ -26,6 +26,7 @@
#include <QMatrix4x4>
#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;

View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions>
#include <QOpenGLContext>
#include <QMatrix4x4>
#include <QVector4D>
#include <gui/glshadersimplepolyline.h>
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"
);

View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#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"
);

View File

@ -23,7 +23,7 @@
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions>
#include "gui/glspectrum.h"
#include "gui/glshadersources.h"
#include <gui/glshadersimplepolyline.h>
#include <QDebug>
@ -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();
}