1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-27 02:09:14 -05:00
sdrangel/sdrgui/gui/glshadertvarray.cpp

422 lines
12 KiB
C++
Raw Normal View History

2017-02-23 02:18:56 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 F4HKW //
// for F4EXB / SDRAngel //
// //
// 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 //
// (at your option) any later version. //
2017-02-23 02:18:56 -05:00
// //
// 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 <QOpenGLContext>
#include "gui/glshadertvarray.h"
2017-02-23 02:18:56 -05:00
2022-06-19 18:22:43 -04:00
const QString GLShaderTVArray::m_strVertexShaderSourceArray2 = QString(
"uniform highp mat4 uMatrix;\n"
"attribute highp vec4 vertex;\n"
"attribute highp vec2 texCoord;\n"
"varying mediump vec2 texCoordVar;\n"
"void main() {\n"
" gl_Position = uMatrix * vertex;\n"
" texCoordVar = texCoord;\n"
"}\n");
2018-03-11 08:00:33 -04:00
const QString GLShaderTVArray::m_strVertexShaderSourceArray = QString(
2022-06-19 18:22:43 -04:00
"#version 330\n"
2017-02-23 02:18:56 -05:00
"uniform highp mat4 uMatrix;\n"
2022-06-19 18:22:43 -04:00
"in highp vec4 vertex;\n"
"in highp vec2 texCoord;\n"
"out mediump vec2 texCoordVar;\n"
"void main() {\n"
" gl_Position = uMatrix * vertex;\n"
" texCoordVar = texCoord;\n"
"}\n");
const QString GLShaderTVArray::m_strFragmentShaderSourceColored2 = QString(
"uniform lowp sampler2D uTexture;\n"
"varying mediump vec2 texCoordVar;\n"
"void main() {\n"
" gl_FragColor = texture2D(uTexture, texCoordVar);\n"
"}\n");
2017-02-23 02:18:56 -05:00
2018-03-11 08:00:33 -04:00
const QString GLShaderTVArray::m_strFragmentShaderSourceColored = QString(
2022-06-19 18:22:43 -04:00
"#version 330\n"
2017-02-23 02:18:56 -05:00
"uniform lowp sampler2D uTexture;\n"
2022-06-19 18:22:43 -04:00
"in mediump vec2 texCoordVar;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = texture(uTexture, texCoordVar);\n"
"}\n");
2017-02-23 02:18:56 -05:00
GLShaderTVArray::GLShaderTVArray(bool blnColor) :
m_objProgram(nullptr),
2022-06-19 18:22:43 -04:00
m_vao(nullptr),
m_verticesBuf(nullptr),
m_textureCoordsBuf(nullptr),
m_matrixLoc(0),
m_textureLoc(0),
m_objImage(nullptr),
m_objTexture(nullptr),
m_intCols(0),
m_intRows(0),
m_objCurrentRow(nullptr),
m_blnInitialized(false),
m_blnColor(blnColor),
m_blnAlphaBlend(false),
m_blnAlphaReset(false)
2017-02-23 02:18:56 -05:00
{
}
2018-03-11 08:00:33 -04:00
GLShaderTVArray::~GLShaderTVArray()
2017-02-23 02:18:56 -05:00
{
qDebug("GLShaderTVArray::~GLShaderTVArray");
cleanup();
2017-02-23 02:18:56 -05:00
}
2022-06-19 18:22:43 -04:00
void GLShaderTVArray::initializeGL(int majorVersion, int minorVersion, int intCols, int intRows)
2017-02-23 02:18:56 -05:00
{
QMatrix4x4 objQMatrix;
m_blnInitialized = false;
2017-02-23 02:18:56 -05:00
m_intCols = 0;
m_intRows = 0;
2017-02-23 02:18:56 -05:00
m_objCurrentRow = nullptr;
2017-02-23 02:18:56 -05:00
if (!m_objProgram)
2017-02-23 02:18:56 -05:00
{
m_objProgram = new QOpenGLShaderProgram();
2022-06-19 18:22:43 -04:00
if ((majorVersion > 3) || ((majorVersion == 3) && (minorVersion >= 3)))
2017-02-23 02:18:56 -05:00
{
2022-06-19 18:22:43 -04:00
if (!m_objProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,
m_strVertexShaderSourceArray))
{
qDebug() << "GLShaderArray::initializeGL: error in vertex shader: "
<< m_objProgram->log();
}
2017-02-23 02:18:56 -05:00
2022-06-19 18:22:43 -04:00
if (!m_objProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,
m_strFragmentShaderSourceColored))
{
qDebug()
<< "GLShaderArray::initializeGL: error in fragment shader: "
<< m_objProgram->log();
}
m_vao = new QOpenGLVertexArrayObject();
m_vao->create();
m_vao->bind();
}
else
2017-02-23 02:18:56 -05:00
{
2022-06-19 18:22:43 -04:00
if (!m_objProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,
m_strVertexShaderSourceArray2))
{
qDebug() << "GLShaderArray::initializeGL: error in vertex shader: "
<< m_objProgram->log();
}
if (!m_objProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,
m_strFragmentShaderSourceColored2))
{
qDebug()
<< "GLShaderArray::initializeGL: error in fragment shader: "
<< m_objProgram->log();
}
2017-02-23 02:18:56 -05:00
}
m_objProgram->bindAttributeLocation("vertex", 0);
if (!m_objProgram->link())
{
qDebug() << "GLShaderArray::initializeGL: error linking shader: "
<< m_objProgram->log();
2017-02-23 02:18:56 -05:00
}
m_objProgram->bind();
m_objProgram->setUniformValue(m_matrixLoc, objQMatrix);
m_objProgram->setUniformValue(m_textureLoc, 0);
2022-06-19 18:22:43 -04:00
if (m_vao)
{
m_verticesBuf = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
m_verticesBuf->setUsagePattern(QOpenGLBuffer::DynamicDraw);
m_verticesBuf->create();
m_textureCoordsBuf = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
m_textureCoordsBuf->setUsagePattern(QOpenGLBuffer::DynamicDraw);
m_textureCoordsBuf->create();
m_vao->release();
}
2017-02-23 02:18:56 -05:00
m_objProgram->release();
}
m_matrixLoc = m_objProgram->uniformLocation("uMatrix");
m_textureLoc = m_objProgram->uniformLocation("uTexture");
2017-02-23 02:18:56 -05:00
if (m_objTexture)
2017-02-23 02:18:56 -05:00
{
delete m_objTexture;
m_objTexture = nullptr;
2017-02-23 02:18:56 -05:00
}
//Image container
m_objImage = new QImage(intCols, intRows, QImage::Format_RGBA8888);
m_objImage->fill(QColor(0, 0, 0));
2017-02-23 02:18:56 -05:00
m_objTexture = new QOpenGLTexture(*m_objImage);
2018-03-29 04:41:46 -04:00
//m_objTexture->setFormat(QOpenGLTexture::RGBA8_UNorm); avoids OpenGL warning and in fact is useless
2017-02-23 02:18:56 -05:00
m_objTexture->setMinificationFilter(QOpenGLTexture::Linear);
m_objTexture->setMagnificationFilter(QOpenGLTexture::Linear);
m_objTexture->setWrapMode(QOpenGLTexture::ClampToEdge);
m_intCols = intCols;
m_intRows = intRows;
m_blnInitialized = true;
2017-02-23 02:18:56 -05:00
}
QRgb *GLShaderTVArray::GetRowBuffer(int intRow)
2017-02-23 02:18:56 -05:00
{
if (!m_blnInitialized) {
return nullptr;
2017-02-23 02:18:56 -05:00
}
if (!m_objImage) {
return nullptr;
2017-02-23 02:18:56 -05:00
}
if (intRow > m_intRows) {
return nullptr;
2017-02-23 02:18:56 -05:00
}
return (QRgb *) m_objImage->scanLine(intRow);
2017-02-23 02:18:56 -05:00
}
2018-03-11 08:00:33 -04:00
void GLShaderTVArray::RenderPixels(unsigned char *chrData)
{
2017-02-23 02:18:56 -05:00
QOpenGLFunctions *ptrF;
int intI;
int intJ;
int intNbVertices = 6;
2017-02-23 02:18:56 -05:00
QMatrix4x4 objQMatrix;
GLfloat arrVertices[] =
// 2 3
// 1 4
//1 2 3 3 4 1
{ -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f };
2017-02-23 02:18:56 -05:00
GLfloat arrTextureCoords[] =
// 1 4
// 2 3
//1 2 3 3 4 1
{ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };
2017-02-23 02:18:56 -05:00
QRgb *ptrLine;
int intVal;
if (!m_blnInitialized) {
2017-02-23 02:18:56 -05:00
return;
}
if (!m_objImage) {
return;
2017-02-23 02:18:56 -05:00
}
if (chrData != 0)
2017-02-23 02:18:56 -05:00
{
for (intJ = 0; intJ < m_intRows; intJ++)
2017-02-23 02:18:56 -05:00
{
ptrLine = (QRgb *) m_objImage->scanLine(intJ);
2017-02-23 02:18:56 -05:00
for (intI = 0; intI < m_intCols; intI++)
2017-02-23 02:18:56 -05:00
{
2018-03-11 08:00:33 -04:00
if (m_blnColor)
{
*ptrLine = qRgb((int) (*(chrData+2)), (int) (*(chrData+1)), (int) (*chrData));
chrData+=3;
}
else
{
intVal = (int) (*chrData);
*ptrLine = qRgb(intVal, intVal, intVal);
chrData++;
}
2017-02-23 02:18:56 -05:00
ptrLine++;
2017-02-23 02:18:56 -05:00
}
}
}
//Affichage
ptrF = QOpenGLContext::currentContext()->functions();
m_objProgram->bind();
m_objProgram->setUniformValue(m_matrixLoc, objQMatrix);
m_objProgram->setUniformValue(m_textureLoc, 0);
if (m_blnAlphaReset)
{
ptrF->glClear(GL_COLOR_BUFFER_BIT);
m_blnAlphaReset = false;
}
if (m_blnAlphaBlend)
{
ptrF->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
ptrF->glEnable(GL_BLEND);
}
else
{
ptrF->glDisable(GL_BLEND);
}
2017-02-23 02:18:56 -05:00
m_objTexture->bind();
ptrF->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_intCols, m_intRows, GL_RGBA,
GL_UNSIGNED_BYTE, m_objImage->bits());
2017-02-23 02:18:56 -05:00
2022-06-19 18:22:43 -04:00
if (m_vao)
{
m_vao->bind();
2017-02-23 02:18:56 -05:00
2022-06-19 18:22:43 -04:00
m_verticesBuf->bind();
m_verticesBuf->allocate(arrVertices, intNbVertices * 2 * sizeof(GL_FLOAT));
m_objProgram->enableAttributeArray(0);
m_objProgram->setAttributeBuffer(0, GL_FLOAT, 0, 2);
m_textureCoordsBuf->bind();
m_textureCoordsBuf->allocate(arrTextureCoords, intNbVertices * 2 * sizeof(GL_FLOAT));
m_objProgram->enableAttributeArray(1);
m_objProgram->setAttributeBuffer(1, GL_FLOAT, 0, 2);
}
else
{
ptrF->glEnableVertexAttribArray(0); // vertex
ptrF->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, arrVertices);
ptrF->glEnableVertexAttribArray(1); // texture coordinates
ptrF->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, arrTextureCoords);
}
2017-02-23 02:18:56 -05:00
ptrF->glDrawArrays(GL_TRIANGLES, 0, intNbVertices);
2017-02-23 02:18:56 -05:00
//cleanup
2022-06-19 18:22:43 -04:00
if (m_vao)
{
m_vao->release();
}
else
{
ptrF->glDisableVertexAttribArray(0);
ptrF->glDisableVertexAttribArray(1);
}
2017-02-23 02:18:56 -05:00
//*********************//
m_objTexture->release();
m_objProgram->release();
}
2018-03-11 08:00:33 -04:00
void GLShaderTVArray::ResetPixels()
2017-02-23 02:18:56 -05:00
{
if (m_objImage) {
2017-02-23 02:18:56 -05:00
m_objImage->fill(0);
}
}
void GLShaderTVArray::ResetPixels(int alpha)
{
if (m_objImage) {
m_objImage->fill(qRgba(0, 0, 0, alpha));
}
}
void GLShaderTVArray::cleanup()
2017-02-23 02:18:56 -05:00
{
m_blnInitialized = false;
2017-02-23 02:18:56 -05:00
m_intCols = 0;
m_intRows = 0;
2017-02-23 02:18:56 -05:00
m_objCurrentRow = nullptr;
2017-02-23 02:18:56 -05:00
if (!QOpenGLContext::currentContext()) {
return;
}
2017-02-23 02:18:56 -05:00
if (m_objProgram)
{
delete m_objProgram;
m_objProgram = nullptr;
2017-02-23 02:18:56 -05:00
}
if (m_objTexture)
2017-02-23 02:18:56 -05:00
{
delete m_objTexture;
m_objTexture = nullptr;
2017-02-23 02:18:56 -05:00
}
if (m_objImage)
2017-02-23 02:18:56 -05:00
{
delete m_objImage;
m_objImage = nullptr;
2017-02-23 02:18:56 -05:00
}
2022-06-19 18:22:43 -04:00
delete m_verticesBuf;
m_verticesBuf = nullptr;
delete m_textureCoordsBuf;
m_textureCoordsBuf = nullptr;
delete m_vao;
m_vao = nullptr;
2017-02-23 02:18:56 -05:00
}
2018-03-11 08:00:33 -04:00
bool GLShaderTVArray::SelectRow(int intLine)
2017-02-23 02:18:56 -05:00
{
bool blnRslt = false;
2017-02-23 02:18:56 -05:00
if (m_blnInitialized)
2017-02-23 02:18:56 -05:00
{
if ((intLine < m_intRows) && (intLine >= 0))
2017-02-23 02:18:56 -05:00
{
m_objCurrentRow = (QRgb *) m_objImage->scanLine(intLine);
blnRslt = true;
2017-02-23 02:18:56 -05:00
}
else
{
m_objCurrentRow = nullptr;
2017-02-23 02:18:56 -05:00
}
}
return blnRslt;
}
2018-03-11 08:00:33 -04:00
bool GLShaderTVArray::SetDataColor(int intCol, QRgb objColor)
2017-02-23 02:18:56 -05:00
{
bool blnRslt = false;
2017-02-23 02:18:56 -05:00
if (m_blnInitialized)
2017-02-23 02:18:56 -05:00
{
if ((intCol < m_intCols) && (intCol >= 0) && m_objCurrentRow)
2017-02-23 02:18:56 -05:00
{
m_objCurrentRow[intCol] = objColor;
blnRslt = true;
2017-02-23 02:18:56 -05:00
}
}
return blnRslt;
}