| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // 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                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:43:33 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +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 <QMatrix4x4>
 | 
					
						
							|  |  |  | #include <QVector4D>
 | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | #include "gui/glshadersimple.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | GLShaderSimple::GLShaderSimple() : | 
					
						
							| 
									
										
										
										
											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_vertexLoc(0), | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  |     m_matrixLoc(0), | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_colorLoc(0) | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | GLShaderSimple::~GLShaderSimple() | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     cleanup(); | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-19 00:11:46 +01:00
										 |  |  | void GLShaderSimple::initializeGL(int majorVersion, int minorVersion) | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program = new QOpenGLShaderProgram; | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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_vertexShaderSourceSimple)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderSimple::initializeGL: error in vertex shader: " << m_program->log(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSourceColored)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderSimple::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_vertexShaderSourceSimple2)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderSimple::initializeGL: error in vertex shader: " << m_program->log(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSourceColored2)) { | 
					
						
							|  |  |  |             qDebug() << "GLShaderSimple::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); | 
					
						
							| 
									
										
										
										
											2016-03-05 06:45:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     if (!m_program->link()) { | 
					
						
							|  |  |  |         qDebug() << "GLShaderSimple::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(); | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     m_vertexLoc = m_program->attributeLocation("vertex"); | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_matrixLoc = m_program->uniformLocation("uMatrix"); | 
					
						
							|  |  |  |     m_colorLoc = m_program->uniformLocation("uColour"); | 
					
						
							| 
									
										
										
										
											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_vao->release(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program->release(); | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderSimple::drawPoints(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2018-05-15 00:30:01 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  |     draw(GL_POINTS, transformMatrix, color, vertices, nbVertices, nbComponents); | 
					
						
							| 
									
										
										
										
											2018-05-15 00:30:01 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderSimple::drawPolyline(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     draw(GL_LINE_STRIP, transformMatrix, color, vertices, nbVertices, nbComponents); | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderSimple::drawSegments(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     draw(GL_LINES, transformMatrix, color, vertices, nbVertices, nbComponents); | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderSimple::drawContour(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     draw(GL_LINE_LOOP, transformMatrix, color, vertices, nbVertices, nbComponents); | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderSimple::drawSurface(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     draw(GL_TRIANGLE_FAN, transformMatrix, color, vertices, nbVertices, nbComponents); | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-22 08:52:48 +01:00
										 |  |  | void GLShaderSimple::drawSurfaceStrip(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     draw(GL_TRIANGLE_STRIP, transformMatrix, color, vertices, nbVertices, nbComponents); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 12:35:44 +01:00
										 |  |  | void GLShaderSimple::draw(unsigned int mode, const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices, int nbComponents) | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); | 
					
						
							|  |  |  |     m_program->bind(); | 
					
						
							|  |  |  |     m_program->setUniformValue(m_matrixLoc, transformMatrix); | 
					
						
							|  |  |  |     m_program->setUniformValue(m_colorLoc, color); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     f->glEnable(GL_BLEND); | 
					
						
							|  |  |  |     f->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
					
						
							|  |  |  |     f->glLineWidth(1.0f); | 
					
						
							|  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2022-06-19 11:22:46 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |     m_program->release(); | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:55 +01:00
										 |  |  | void GLShaderSimple::cleanup() | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | const QString GLShaderSimple::m_vertexShaderSourceSimple2 = QString( | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "uniform highp mat4 uMatrix;\n" | 
					
						
							|  |  |  |         "attribute highp vec4 vertex;\n" | 
					
						
							|  |  |  |         "void main() {\n" | 
					
						
							|  |  |  |         "    gl_Position = uMatrix * vertex;\n" | 
					
						
							|  |  |  |         "}\n" | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2016-03-02 23:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | const QString GLShaderSimple::m_vertexShaderSourceSimple = QString( | 
					
						
							|  |  |  |         "#version 330\n" | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "uniform highp mat4 uMatrix;\n" | 
					
						
							|  |  |  |         "in highp vec4 vertex;\n" | 
					
						
							|  |  |  |         "void main() {\n" | 
					
						
							|  |  |  |         "    gl_Position = uMatrix * vertex;\n" | 
					
						
							|  |  |  |         "}\n" | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const QString GLShaderSimple::m_fragmentShaderSourceColored2 = QString( | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "uniform mediump vec4 uColour;\n" | 
					
						
							|  |  |  |         "void main() {\n" | 
					
						
							|  |  |  |         "    gl_FragColor = uColour;\n" | 
					
						
							|  |  |  |         "}\n" | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-06-18 23:40:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const QString GLShaderSimple::m_fragmentShaderSourceColored = QString( | 
					
						
							|  |  |  |         "#version 330\n" | 
					
						
							|  |  |  |         "out vec4 fragColor;\n" | 
					
						
							| 
									
										
										
										
											2022-06-20 08:50:28 +01:00
										 |  |  |         "uniform mediump vec4 uColour;\n" | 
					
						
							|  |  |  |         "void main() {\n" | 
					
						
							|  |  |  |         "    fragColor = uColour;\n" | 
					
						
							|  |  |  |         "}\n" | 
					
						
							|  |  |  |         ); |