SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
glshadersimple.cpp
Go to the documentation of this file.
1 // Copyright (C) 2016 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #include <QOpenGLShaderProgram>
20 #include <QOpenGLFunctions>
21 #include <QOpenGLContext>
22 #include <QMatrix4x4>
23 #include <QVector4D>
24 #include <QDebug>
25 
26 #include "gui/glshadersimple.h"
27 
29  m_program(0),
30  m_matrixLoc(0),
31  m_colorLoc(0)
32 { }
33 
35 {
36  cleanup();
37 }
38 
40 {
41  m_program = new QOpenGLShaderProgram;
42 
43  if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShaderSourceSimple)) {
44  qDebug() << "GLShaderSimple::initializeGL: error in vertex shader: " << m_program->log();
45  }
46 
47  if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSourceColored)) {
48  qDebug() << "GLShaderSimple::initializeGL: error in fragment shader: " << m_program->log();
49  }
50 
51  m_program->bindAttributeLocation("vertex", 0);
52 
53  if (!m_program->link()) {
54  qDebug() << "GLShaderSimple::initializeGL: error linking shader: " << m_program->log();
55  }
56 
57  m_program->bind();
58  m_matrixLoc = m_program->uniformLocation("uMatrix");
59  m_colorLoc = m_program->uniformLocation("uColour");
60  m_program->release();
61 }
62 
63 void GLShaderSimple::drawPoints(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices)
64 {
65  draw(GL_POINTS, transformMatrix, color, vertices, nbVertices);
66 }
67 
68 void GLShaderSimple::drawPolyline(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices)
69 {
70  draw(GL_LINE_STRIP, transformMatrix, color, vertices, nbVertices);
71 }
72 
73 void GLShaderSimple::drawSegments(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices)
74 {
75  draw(GL_LINES, transformMatrix, color, vertices, nbVertices);
76 }
77 
78 void GLShaderSimple::drawContour(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices)
79 {
80  draw(GL_LINE_LOOP, transformMatrix, color, vertices, nbVertices);
81 }
82 
83 void GLShaderSimple::drawSurface(const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices)
84 {
85  draw(GL_TRIANGLE_FAN, transformMatrix, color, vertices, nbVertices);
86 }
87 
88 void GLShaderSimple::draw(unsigned int mode, const QMatrix4x4& transformMatrix, const QVector4D& color, GLfloat *vertices, int nbVertices)
89 {
90  QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
91  m_program->bind();
92  m_program->setUniformValue(m_matrixLoc, transformMatrix);
93  m_program->setUniformValue(m_colorLoc, color);
94  f->glEnable(GL_BLEND);
95  f->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
96  f->glLineWidth(1.0f);
97  f->glEnableVertexAttribArray(0); // vertex
98  f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
99  f->glDrawArrays(mode, 0, nbVertices);
100  f->glDisableVertexAttribArray(0);
101  m_program->release();
102 }
103 
105 {
106  if (m_program)
107  {
108  delete m_program;
109  m_program = 0;
110  }
111 }
112 
113 const QString GLShaderSimple::m_vertexShaderSourceSimple = QString(
114  "uniform highp mat4 uMatrix;\n"
115  "attribute highp vec4 vertex;\n"
116  "void main() {\n"
117  " gl_Position = uMatrix * vertex;\n"
118  "}\n"
119  );
120 
121 const QString GLShaderSimple::m_fragmentShaderSourceColored = QString(
122  "uniform mediump vec4 uColour;\n"
123  "void main() {\n"
124  " gl_FragColor = uColour;\n"
125  "}\n"
126  );
void drawSegments(const QMatrix4x4 &transformMatrix, const QVector4D &color, GLfloat *vertices, int nbVertices)
static const QString m_fragmentShaderSourceColored
void draw(unsigned int mode, const QMatrix4x4 &transformMatrix, const QVector4D &color, GLfloat *vertices, int nbVertices)
void drawSurface(const QMatrix4x4 &transformMatrix, const QVector4D &color, GLfloat *vertices, int nbVertices)
void drawContour(const QMatrix4x4 &transformMatrix, const QVector4D &color, GLfloat *vertices, int nbVertices)
static const QString m_vertexShaderSourceSimple
void drawPolyline(const QMatrix4x4 &transformMatrix, const QVector4D &color, GLfloat *vertices, int nbVertices)
void drawPoints(const QMatrix4x4 &transformMatrix, const QVector4D &color, GLfloat *vertices, int nbVertices)
QOpenGLShaderProgram * m_program