Skip to content

PythonQt.QtOpenGL.QGLShaderProgram

QGLShaderProgram

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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.

Source: https://doc.qt.io/archives/qt-5.15/qglshaderprogram.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
addShader function (self, shader: Any) -> bool X.addShader(shader) -> bool
addShaderFromSourceCode function (self, type: Any, source: Any) -> bool X.addShaderFromSourceCode(type, source) -> bool
addShaderFromSourceFile function (self, type: Any, fileName: str) -> bool X.addShaderFromSourceFile(type, fileName) -> bool
attributeLocation function (self, name: Any) -> int X.attributeLocation(name) -> int
bind function (self) -> bool X.bind() -> bool
bindAttributeLocation function (self, name: Any, location: int) -> None X.bindAttributeLocation(name, location)
blockSignals function (self, b: Any) -> bool X.blockSignals(b) -> bool (shared captured text)
childEvent function (self, event: Any) -> Any (shared captured text)
children function (self) -> tuple X.children() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
connect function (self, signal: Any, receiver: Any, slot: Any, type: Any) -> bool X.connect(signal, receiver, slot, type) -> bool (shared captured text)
customEvent function (self, event: Any) -> Any (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteLater function (self) -> Any (shared captured text)
destroyed function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtOpenGL.QGLShaderProgram.destroyed; argument details are not documented.
disableAttributeArray function (self, name: Any) -> None Disables the vertex array at location in this shader program that was enabled by a previous call to…
disconnect function (self, signal: Any, receiver: Any, slot: Any) -> bool X.disconnect(signal, receiver, slot) -> bool (shared captured text)
dumpObjectInfo function (self) -> Any (shared captured text)
dumpObjectTree function (self) -> Any (shared captured text)
dynamicPropertyNames function (self) -> Any (shared captured text)
enableAttributeArray function (self, name: Any) -> None Enables the vertex array at location in this shader program so that the value set by [setAttributeArray](https://doc.qt.io/archives/qt-5.15/qglshaderprogram.html#setAttributeArray)() on location…
event function (self, event: Any) -> bool X.event(event) -> bool (shared captured text)
eventFilter function (self, watched: Any, event: Any) -> bool X.eventFilter(watched, event) -> bool (shared captured text)
findChild function (self, type: Any, name: Any) -> Any X.findChild(type, name) -> PythonQt.private.QObject (shared captured text)
findChildren function (self, type: Any, name: Any) -> tuple X.findChildren(type, name) -> tuple (shared captured text)
geometryInputType function (self) -> int X.geometryInputType() -> int
geometryOutputType function (self) -> int X.geometryOutputType() -> int
geometryOutputVertexCount function (self) -> int X.geometryOutputVertexCount() -> int
hasOpenGLShaderPrograms function (self, context: Any) -> bool X.hasOpenGLShaderPrograms(context) -> bool
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
installEventFilter function (self, filterObj: Any) -> Any (shared captured text)
isLinked function (self) -> bool X.isLinked() -> bool
isSignalConnected function (self, signal: Any) -> bool X.isSignalConnected(signal) -> bool (shared captured text)
isWidgetType function (self) -> bool X.isWidgetType() -> bool (shared captured text)
isWindowType function (self) -> bool X.isWindowType() -> bool (shared captured text)
killTimer function (self, id: Any) -> Any (shared captured text)
link function (self) -> bool X.link() -> bool
log function (self) -> str X.log() -> str
maxGeometryOutputVertices function (self) -> int X.maxGeometryOutputVertices() -> int
metaObject function (self) -> Any X.metaObject() -> PythonQt.QtCore.QMetaObject (shared captured text)
moveToThread function (self, thread: Any) -> Any (shared captured text)
objectName attribute Any (shared captured text)
objectNameChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtOpenGL.QGLShaderProgram.objectNameChanged; argument details are not documented.
parent function (self) -> Any X.parent() -> PythonQt.private.QObject (shared captured text)
programId function (self) -> int X.programId() -> int
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
release function (self) -> None Releases the active shader program from the current QGLContext.
removeAllShaders function (self) -> None Removes all of the shaders that were added to this program previously.
removeEventFilter function (self, obj: Any) -> Any (shared captured text)
removeShader function (self, shader: Any) -> None Removes shader from this shader program.
sender function (self) -> Any X.sender() -> PythonQt.private.QObject (shared captured text)
senderSignalIndex function (self) -> int X.senderSignalIndex() -> int (shared captured text)
setAttributeArray function (self, name: Any, type: Any, values: Any, tupleSize: Any, stride: Any) -> Any X.setAttributeArray(name, type, values, tupleSize, stride)
setAttributeBuffer function (self, name: Any, type: int, offset: int, tupleSize: int, stride: int) -> None X.setAttributeBuffer(name, type, offset, tupleSize, stride)
setAttributeValue function (self, name: Any, x: Any, y: Any, z: Any, w: Any) -> None X.setAttributeValue(name, x, y, z, w)
setGeometryInputType function (self, inputType: int) -> None Sets the input type from inputType.
setGeometryOutputType function (self, outputType: int) -> None Sets the output type from the geometry shader, if active, to outputType.
setGeometryOutputVertexCount function (self, count: int) -> None Sets the maximum number of vertices the current geometry shader program will produce, if active, to count.
setObjectName function (self, name: Any) -> Any (shared captured text)
setParent function (self, parent: Any) -> Any (shared captured text)
setProperty function (self, name: Any, value: Any) -> bool X.setProperty(name, value) -> bool (shared captured text)
setUniformValue function (self, name: Any, x: Any, y: Any, z: Any, w: Any) -> None X.setUniformValue(name, x, y, z, w)
setUniformValueArray function (self, name: Any, values: Any, count: Any, tupleSize: Any) -> Any X.setUniformValueArray(name, values, count, tupleSize)
shaders function (self) -> tuple X.shaders() -> tuple
signalsBlocked function (self) -> bool X.signalsBlocked() -> bool (shared captured text)
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
thread function (self) -> Any (shared captured text)
timerEvent function (self, event: Any) -> Any (shared captured text)
tr function (self, text: Any, ambig: Any, n: Any) -> str X.tr(text, ambig, n) -> str (shared captured text)
uniformLocation function (self, name: Any) -> int X.uniformLocation(name) -> int