| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-19 13:31:45 +01:00
										 |  |  | // Copyright (C) 2016-2017, 2019-2020, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
 | 
					
						
							|  |  |  | // Copyright (C) 2022 Jon Beniston, M7RCE <jon@beniston.com>                     //
 | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:43:33 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01: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 <QOpenGLShaderProgram>
 | 
					
						
							|  |  |  | #include <QOpenGLFunctions>
 | 
					
						
							|  |  |  | #include <QOpenGLContext>
 | 
					
						
							|  |  |  | #include <QImage>
 | 
					
						
							|  |  |  | #include <QMatrix4x4>
 | 
					
						
							|  |  |  | #include <QVector4D>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 14:39:39 +00:00
										 |  |  | #ifdef ANDROID
 | 
					
						
							|  |  |  | #include <GLES3/gl3.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | #include "gui/glshadertextured.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLShaderTextured::GLShaderTextured() : | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program(nullptr), | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     m_vao(nullptr), | 
					
						
							|  |  |  |     m_verticesBuf(nullptr), | 
					
						
							|  |  |  |     m_textureCoordsBuf(nullptr), | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_texture(nullptr), | 
					
						
							| 
									
										
										
										
											2022-06-19 11:39:13 +01:00
										 |  |  |     m_textureId(0), | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     m_vertexLoc(0), | 
					
						
							|  |  |  |     m_texCoordLoc(0), | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_matrixLoc(0), | 
					
						
							|  |  |  |     m_textureLoc(0), | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     m_useImmutableStorage(true) | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLShaderTextured::~GLShaderTextured() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     cleanup(); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-19 00:11:46 +01:00
										 |  |  | void GLShaderTextured::initializeGL(int majorVersion, int minorVersion) | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     initializeOpenGLFunctions(); | 
					
						
							|  |  |  |     m_useImmutableStorage = useImmutableStorage(); | 
					
						
							|  |  |  |     qDebug() << "GLShaderTextured::initializeGL: m_useImmutableStorage: " << m_useImmutableStorage; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  |     m_program = new QOpenGLShaderProgram; | 
					
						
							| 
									
										
										
										
											2022-06-19 00:11:46 +01:00
										 |  |  |     if ((majorVersion > 3) || ((majorVersion == 3) && (minorVersion >= 3))) | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShaderSourceTextured)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderTextured::initializeGL: error in vertex shader: " << m_program->log(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSourceTextured)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderTextured::initializeGL: error in fragment shader: " << m_program->log(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-19 11:39:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         m_vao = new QOpenGLVertexArrayObject(); | 
					
						
							|  |  |  |         m_vao->create(); | 
					
						
							|  |  |  |         m_vao->bind(); | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShaderSourceTextured2)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderTextured::initializeGL: error in vertex shader: " << m_program->log(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSourceTextured2)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderTextured::initializeGL: error in fragment shader: " << m_program->log(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program->bindAttributeLocation("vertex", 0); | 
					
						
							|  |  |  |     m_program->bindAttributeLocation("texCoord", 1); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (!m_program->link()) { | 
					
						
							|  |  |  |         qDebug() << "GLShaderTextured::initializeGL: error linking shader: " << m_program->log(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program->bind(); | 
					
						
							|  |  |  |     m_vertexLoc = m_program->attributeLocation("vertex"); | 
					
						
							|  |  |  |     m_texCoordLoc = m_program->attributeLocation("texCoord"); | 
					
						
							|  |  |  |     m_matrixLoc = m_program->uniformLocation("uMatrix"); | 
					
						
							|  |  |  |     m_textureLoc = m_program->uniformLocation("uTexture"); | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01: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(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program->release(); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-05 14:28:55 +01:00
										 |  |  | void GLShaderTextured::initTexture(const QImage& image, QOpenGLTexture::WrapMode wrapMode) | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_useImmutableStorage) { | 
					
						
							|  |  |  |         initTextureImmutable(image, wrapMode); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         initTextureMutable(image, wrapMode); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GLShaderTextured::initTextureImmutable(const QImage& image, QOpenGLTexture::WrapMode wrapMode) | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (m_texture) { | 
					
						
							|  |  |  |         delete m_texture; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_texture = new QOpenGLTexture(image); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_texture->setMinificationFilter(QOpenGLTexture::Linear); | 
					
						
							|  |  |  |     m_texture->setMagnificationFilter(QOpenGLTexture::Linear); | 
					
						
							|  |  |  |     m_texture->setWrapMode(wrapMode); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | void GLShaderTextured::initTextureMutable(const QImage& image, QOpenGLTexture::WrapMode wrapMode) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (m_textureId) | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         glDeleteTextures(1, &m_textureId); | 
					
						
							|  |  |  |         m_textureId = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     glGenTextures(1, &m_textureId); | 
					
						
							|  |  |  |     glBindTexture(GL_TEXTURE_2D, m_textureId); | 
					
						
							|  |  |  |     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, | 
					
						
							|  |  |  |         image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.constScanLine(0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
					
						
							|  |  |  |     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
					
						
							|  |  |  |     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); | 
					
						
							|  |  |  |     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-05 11:57:53 +01:00
										 |  |  | void GLShaderTextured::subTexture(int xOffset, int yOffset, int width, int height, const void *pixels) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     if (m_useImmutableStorage) { | 
					
						
							|  |  |  |         subTextureImmutable(xOffset, yOffset, width, height, pixels); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         subTextureMutable(xOffset, yOffset, width, height, pixels); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GLShaderTextured::subTextureImmutable(int xOffset, int yOffset, int width, int height, const void *pixels) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (!m_texture) | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         qDebug("GLShaderTextured::subTextureImmutable: no texture defined. Doing nothing"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-05 11:57:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); | 
					
						
							|  |  |  |     m_texture->bind(); | 
					
						
							|  |  |  |     f->glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | 
					
						
							| 
									
										
										
										
											2016-03-05 11:57:53 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | void GLShaderTextured::subTextureMutable(int xOffset, int yOffset, int width, int height, const void *pixels) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (!m_textureId) | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         qDebug("GLShaderTextured::subTextureMutable: no texture defined. Doing nothing"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     glBindTexture(GL_TEXTURE_2D, m_textureId); | 
					
						
							|  |  |  |     glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderTextured::drawSurface(const QMatrix4x4& transformMatrix, GLfloat *textureCoords, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     if (m_useImmutableStorage) { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         draw(GL_TRIANGLE_FAN, transformMatrix, textureCoords, vertices, nbVertices, nbComponents); | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  |         drawMutable(GL_TRIANGLE_FAN, transformMatrix, textureCoords, vertices, nbVertices, nbComponents); | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderTextured::draw(unsigned int mode, const QMatrix4x4& transformMatrix, GLfloat *textureCoords, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (!m_texture) | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         qDebug("GLShaderTextured::draw: no texture defined. Doing nothing"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); | 
					
						
							|  |  |  |     m_program->bind(); | 
					
						
							|  |  |  |     m_program->setUniformValue(m_matrixLoc, transformMatrix); | 
					
						
							|  |  |  |     m_texture->bind(); | 
					
						
							|  |  |  |     m_program->setUniformValue(m_textureLoc, 0); // Use texture unit 0 which magically contains our texture
 | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     if (m_vao) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_vao->bind(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_verticesBuf->bind(); | 
					
						
							|  |  |  |         m_verticesBuf->allocate(vertices, nbVertices * nbComponents * sizeof(GL_FLOAT)); | 
					
						
							|  |  |  |         m_program->enableAttributeArray(m_vertexLoc); | 
					
						
							|  |  |  |         m_program->setAttributeBuffer(m_vertexLoc, GL_FLOAT, 0, nbComponents); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_textureCoordsBuf->bind(); | 
					
						
							|  |  |  |         m_textureCoordsBuf->allocate(textureCoords, nbVertices * 2 * sizeof(GL_FLOAT)); | 
					
						
							|  |  |  |         m_program->enableAttributeArray(m_texCoordLoc); | 
					
						
							|  |  |  |         m_program->setAttributeBuffer(m_texCoordLoc, GL_FLOAT, 0, 2); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         f->glEnableVertexAttribArray(m_vertexLoc); // vertex
 | 
					
						
							|  |  |  |         f->glVertexAttribPointer(m_vertexLoc, nbComponents, GL_FLOAT, GL_FALSE, 0, vertices); | 
					
						
							|  |  |  |         f->glEnableVertexAttribArray(m_texCoordLoc); // texture coordinates
 | 
					
						
							|  |  |  |         f->glVertexAttribPointer(m_texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, textureCoords); | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     f->glDrawArrays(mode, 0, nbVertices); | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_vao) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_vao->release(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |        f->glDisableVertexAttribArray(m_vertexLoc); | 
					
						
							|  |  |  |        f->glDisableVertexAttribArray(m_texCoordLoc); | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program->release(); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderTextured::drawMutable(unsigned int mode, const QMatrix4x4& transformMatrix, GLfloat *textureCoords, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (!m_textureId) | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         qDebug("GLShaderTextured::drawMutable: no texture defined. Doing nothing"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_program->bind(); | 
					
						
							|  |  |  |     m_program->setUniformValue(m_matrixLoc, transformMatrix); | 
					
						
							|  |  |  |     glBindTexture(GL_TEXTURE_2D, m_textureId); | 
					
						
							|  |  |  |     m_program->setUniformValue(m_textureLoc, 0); // Use texture unit 0 which magically contains our texture
 | 
					
						
							|  |  |  |     glEnableVertexAttribArray(m_vertexLoc); // vertex
 | 
					
						
							|  |  |  |     glVertexAttribPointer(m_vertexLoc, nbComponents, GL_FLOAT, GL_FALSE, 0, vertices); | 
					
						
							|  |  |  |     glEnableVertexAttribArray(m_texCoordLoc); // texture coordinates
 | 
					
						
							|  |  |  |     glVertexAttribPointer(m_texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, textureCoords); | 
					
						
							|  |  |  |     glDrawArrays(mode, 0, nbVertices); | 
					
						
							|  |  |  |     glDisableVertexAttribArray(m_vertexLoc); | 
					
						
							|  |  |  |     glDisableVertexAttribArray(m_texCoordLoc); | 
					
						
							|  |  |  |     m_program->release(); | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | void GLShaderTextured::cleanup() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     delete m_program; | 
					
						
							|  |  |  |     m_program = nullptr; | 
					
						
							|  |  |  |     delete m_vao; | 
					
						
							|  |  |  |     m_vao = nullptr; | 
					
						
							|  |  |  |     delete m_verticesBuf; | 
					
						
							|  |  |  |     m_verticesBuf = nullptr; | 
					
						
							|  |  |  |     delete m_textureCoordsBuf; | 
					
						
							|  |  |  |     m_textureCoordsBuf = nullptr; | 
					
						
							|  |  |  |     delete m_texture; | 
					
						
							|  |  |  |     m_texture = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (!QOpenGLContext::currentContext()) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-18 19:15:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |     if (m_textureId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         glDeleteTextures(1, &m_textureId); | 
					
						
							|  |  |  |         m_textureId = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool GLShaderTextured::useImmutableStorage() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QOpenGLContext* ctx = QOpenGLContext::currentContext(); | 
					
						
							|  |  |  |     QSurfaceFormat sf = ctx->format(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sf.version() >= qMakePair(4, 2) | 
					
						
							|  |  |  |         || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_storage")) | 
					
						
							|  |  |  |         || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_storage"))) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         void (QOPENGLF_APIENTRYP glTexStorage2D)( | 
					
						
							|  |  |  |             GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height); | 
					
						
							|  |  |  |         glTexStorage2D = reinterpret_cast<void (QOPENGLF_APIENTRYP)( | 
					
						
							|  |  |  |             GLenum, GLsizei, GLenum, GLsizei, GLsizei)>(ctx->getProcAddress("glTexStorage2D")); | 
					
						
							|  |  |  |         int data = 0; | 
					
						
							|  |  |  |         GLuint textureId; | 
					
						
							|  |  |  |         glGenTextures(1, &textureId); | 
					
						
							|  |  |  |         glBindTexture(GL_TEXTURE_2D, textureId); | 
					
						
							| 
									
										
										
										
											2022-12-20 14:39:39 +00:00
										 |  |  | #ifdef ANDROID
 | 
					
						
							|  |  |  |         glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA, 1, 1); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |         glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1); | 
					
						
							| 
									
										
										
										
											2022-12-20 14:39:39 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-08-03 00:30:13 +02:00
										 |  |  |         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data); | 
					
						
							|  |  |  |         GLenum err = glGetError(); | 
					
						
							|  |  |  |         glDeleteTextures(1, &textureId); | 
					
						
							|  |  |  |         return err == GL_NO_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | const QString GLShaderTextured::m_vertexShaderSourceTextured2 = QString( | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "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" | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | const QString GLShaderTextured::m_vertexShaderSourceTextured = QString( | 
					
						
							|  |  |  |         "#version 330\n" | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "uniform highp mat4 uMatrix;\n" | 
					
						
							|  |  |  |         "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" | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const QString GLShaderTextured::m_fragmentShaderSourceTextured2 = QString( | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "uniform lowp sampler2D uTexture;\n" | 
					
						
							|  |  |  |         "varying mediump vec2 texCoordVar;\n" | 
					
						
							|  |  |  |         "void main() {\n" | 
					
						
							|  |  |  |         "    gl_FragColor = texture2D(uTexture, texCoordVar);\n" | 
					
						
							|  |  |  |         "}\n" | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const QString GLShaderTextured::m_fragmentShaderSourceTextured = QString( | 
					
						
							|  |  |  |         "#version 330\n" | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "uniform lowp sampler2D uTexture;\n" | 
					
						
							|  |  |  |         "in mediump vec2 texCoordVar;\n" | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  |         "out vec4 fragColor;\n" | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "void main() {\n" | 
					
						
							|  |  |  |         "    fragColor = texture(uTexture, texCoordVar);\n" | 
					
						
							|  |  |  |         "}\n" | 
					
						
							|  |  |  |         ); |