| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
#include <QColor> |
| 4 |
#include <QString> |
| 5 |
#include <QtOpenGL/QOpenGLShader> |
| 6 |
|
| 7 |
namespace src_gui_qopenglshaderprogram { |
| 8 |
QString code; |
| 9 |
QObject *context = nullptr; |
| 10 |
QRectF rect() { return QRectF(); } |
| 11 |
|
| 12 |
void wrapper() { |
| 13 |
//! [0] |
| 14 |
QOpenGLShader shader(QOpenGLShader::Vertex); |
| 15 |
shader.compileSourceCode(code); |
| 16 |
|
| 17 |
QOpenGLShaderProgram program(context); |
| 18 |
program.addShader(&shader); |
| 19 |
program.link(); |
| 20 |
|
| 21 |
program.bind(); |
| 22 |
//! [0] |
| 23 |
|
| 24 |
//! [1] |
| 25 |
program.addShaderFromSourceCode(QOpenGLShader::Vertex, |
| 26 |
"attribute highp vec4 vertex;\n" |
| 27 |
"uniform highp mat4 matrix;\n" |
| 28 |
"void main(void)\n" |
| 29 |
"{\n" |
| 30 |
" gl_Position = matrix * vertex;\n" |
| 31 |
"}"); |
| 32 |
program.addShaderFromSourceCode(QOpenGLShader::Fragment, |
| 33 |
"uniform mediump vec4 color;\n" |
| 34 |
"void main(void)\n" |
| 35 |
"{\n" |
| 36 |
" gl_FragColor = color;\n" |
| 37 |
"}"); |
| 38 |
program.link(); |
| 39 |
program.bind(); |
| 40 |
|
| 41 |
int vertexLocation = program.attributeLocation("vertex"); |
| 42 |
int matrixLocation = program.uniformLocation("matrix"); |
| 43 |
int colorLocation = program.uniformLocation("color"); |
| 44 |
//! [1] |
| 45 |
|
| 46 |
//! [2] |
| 47 |
static GLfloat const triangleVertices[] = { |
| 48 |
60.0f, 10.0f, 0.0f, |
| 49 |
110.0f, 110.0f, 0.0f, |
| 50 |
10.0f, 110.0f, 0.0f |
| 51 |
}; |
| 52 |
|
| 53 |
QColor color(0, 255, 0, 255); |
| 54 |
|
| 55 |
QMatrix4x4 pmvMatrix; |
| 56 |
pmvMatrix.ortho(rect()); |
| 57 |
|
| 58 |
program.enableAttributeArray(vertexLocation); |
| 59 |
program.setAttributeArray(vertexLocation, triangleVertices, 3); |
| 60 |
program.setUniformValue(matrixLocation, pmvMatrix); |
| 61 |
program.setUniformValue(colorLocation, color); |
| 62 |
|
| 63 |
glDrawArrays(GL_TRIANGLES, 0, 3); |
| 64 |
|
| 65 |
program.disableAttributeArray(vertexLocation); |
| 66 |
//! [2] |
| 67 |
|
| 68 |
} // wrapper |
| 69 |
} // src_gui_qopenglshaderprogram |