QGLShaderProgram Class

The QGLShaderProgram class allows OpenGL shader programs to be linked and used. More...

Header: #include <QGLShaderProgram>
qmake: QT += opengl
Since: Qt 4.6
Inherits: QObject

This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

This class was introduced in Qt 4.6.

Detailed Description

Introduction

This class supports shader programs written in the OpenGL Shading Language (GLSL) and in the OpenGL/ES Shading Language (GLSL/ES).

QGLShader and QGLShaderProgram shelter the programmer from the details of compiling and linking vertex and fragment shaders.

The following example creates a vertex shader program using the supplied source code. Once compiled and linked, the shader program is activated in the current QGLContext by calling QGLShaderProgram::bind():

 QGLShader shader(QGLShader::Vertex);
 shader.compileSourceCode(code);

 QGLShaderProgram program(context);
 program.addShader(shader);
 program.link();

 program.bind();

Writing Portable Shaders

Shader programs can be difficult to reuse across OpenGL implementations because of varying levels of support for standard vertex attributes and uniform variables. In particular, GLSL/ES lacks all of the standard variables that are present on desktop OpenGL systems: gl_Vertex, gl_Normal, gl_Color, and so on. Desktop OpenGL lacks the variable qualifiers highp, mediump, and lowp.

The QGLShaderProgram class makes the process of writing portable shaders easier by prefixing all shader programs with the following lines on desktop OpenGL:

 #define highp
 #define mediump
 #define lowp

This makes it possible to run most GLSL/ES shader programs on desktop systems. The programmer should restrict themselves to just features that are present in GLSL/ES, and avoid standard variable names that only work on the desktop.

Simple Shader Example

 program.addShaderFromSourceCode(QGLShader::Vertex,
     "attribute highp vec4 vertex;\n"
     "uniform highp mat4 matrix;\n"
     "void main(void)\n"
     "{\n"
     "   gl_Position = matrix * vertex;\n"
     "}");
 program.addShaderFromSourceCode(QGLShader::Fragment,
     "uniform mediump vec4 color;\n"
     "void main(void)\n"
     "{\n"
     "   gl_FragColor = color;\n"
     "}");
 program.link();
 program.bind();

 int vertexLocation = program.attributeLocation("vertex");
 int matrixLocation = program.uniformLocation("matrix");
 int colorLocation = program.uniformLocation("color");

With the above shader program active, we can draw a green triangle as follows:

 static GLfloat const triangleVertices[] = {
     60.0f,  10.0f,  0.0f,
     110.0f, 110.0f, 0.0f,
     10.0f,  110.0f, 0.0f
 };

 QColor color(0, 255, 0, 255);

 QMatrix4x4 pmvMatrix;
 pmvMatrix.ortho(rect());

 program.enableAttributeArray(vertexLocation);
 program.setAttributeArray(vertexLocation, triangleVertices, 3);
 program.setUniformValue(matrixLocation, pmvMatrix);
 program.setUniformValue(colorLocation, color);

 glDrawArrays(GL_TRIANGLES, 0, 3);

 program.disableAttributeArray(vertexLocation);

Binary Shaders and Programs

Binary shaders may be specified using glShaderBinary() on the return value from QGLShader::shaderId(). The QGLShader instance containing the binary can then be added to the shader program with addShader() and linked in the usual fashion with link().

Binary programs may be specified using glProgramBinaryOES() on the return value from programId(). Then the application should call link(), which will notice that the program has already been specified and linked, allowing other operations to be performed on the shader program.

Note: This class has been deprecated in favor of QOpenGLShaderProgram.

See also QGLShader.