Note
This class was introduced in Qt 4.6
The PySide.QtOpenGL.QGLShaderProgram class allows OpenGL shader programs to be linked and used.
This class supports shader programs written in the OpenGL Shading Language (GLSL) and in the OpenGL/ES Shading Language (GLSL/ES).
PySide.QtOpenGL.QGLShader and PySide.QtOpenGL.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 PySide.QtOpenGL.QGLContext by calling QGLShaderProgram.bind() :
shader = QGLShader(QGLShader.Vertex) shader.compileSourceCode(code) program = QGLShaderProgram(context) program.addShader(shader) program.link() program.bind()
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 PySide.QtOpenGL.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 lowpThis 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.
program.addShaderFromSourceCode(QGLShader.Vertex, "attribute highp vec4 vertex\n" \ "attribute mediump 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() vertexLocation = program.attributeLocation("vertex") matrixLocation = program.attributeLocation("matrix") colorLocation = program.uniformLocation("color")With the above shader program active, we can draw a green triangle as follows:
triangleVertices = ( 60.0f, 10.0f, 0.0f, 110.0f, 110.0f, 0.0f, 10.0f, 110.0f, 0.0f) color = QColor(0, 255, 0, 255) pmvMatrix = QMatrix4x4() pmvMatrix.ortho(self.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 may be specified using glShaderBinary() on the return value from QGLShader.shaderId() . The PySide.QtOpenGL.QGLShader instance containing the binary can then be added to the shader program with PySide.QtOpenGL.QGLShaderProgram.addShader() and linked in the usual fashion with PySide.QtOpenGL.QGLShaderProgram.link() .
Binary programs may be specified using glProgramBinaryOES() on the return value from PySide.QtOpenGL.QGLShaderProgram.programId() . Then the application should call PySide.QtOpenGL.QGLShaderProgram.link() , which will notice that the program has already been specified and linked, allowing other operations to be performed on the shader program.
See also
Parameters: |
|
---|
Constructs a new shader program and attaches it to parent . The program will be invalid until PySide.QtOpenGL.QGLShaderProgram.addShader() is called.
The shader program will be associated with the current PySide.QtOpenGL.QGLContext .
Constructs a new shader program and attaches it to parent . The program will be invalid until PySide.QtOpenGL.QGLShaderProgram.addShader() is called.
The shader program will be associated with context .
Parameters: | shader – PySide.QtOpenGL.QGLShader |
---|---|
Return type: | PySide.QtCore.bool |
Adds a compiled shader to this shader program. Returns true if the shader could be added, or false otherwise.
Ownership of the shader object remains with the caller. It will not be deleted when this PySide.QtOpenGL.QGLShaderProgram instance is deleted. This allows the caller to add the same shader to multiple shader programs.
Parameters: |
|
---|---|
Return type: | PySide.QtCore.bool |
Parameters: |
|
---|---|
Return type: | PySide.QtCore.bool |
Parameters: |
|
---|---|
Return type: | PySide.QtCore.bool |
Parameters: |
|
---|---|
Return type: | PySide.QtCore.bool |
Parameters: | name – str |
---|---|
Return type: | PySide.QtCore.int |
Returns the location of the attribute name within this shader program’s parameter list. Returns -1 if name is not a valid attribute for this shader program.
Parameters: | name – unicode |
---|---|
Return type: | PySide.QtCore.int |
This is an overloaded function.
Returns the location of the attribute name within this shader program’s parameter list. Returns -1 if name is not a valid attribute for this shader program.
Parameters: | name – PySide.QtCore.QByteArray |
---|---|
Return type: | PySide.QtCore.int |
This is an overloaded function.
Returns the location of the attribute name within this shader program’s parameter list. Returns -1 if name is not a valid attribute for this shader program.
Return type: | PySide.QtCore.bool |
---|
Binds this shader program to the active PySide.QtOpenGL.QGLContext and makes it the current shader program. Any previously bound shader program is released. This is equivalent to calling glUseProgram() on PySide.QtOpenGL.QGLShaderProgram.programId() . Returns true if the program was successfully bound; false otherwise. If the shader program has not yet been linked, or it needs to be re-linked, this function will call PySide.QtOpenGL.QGLShaderProgram.link() .
Parameters: |
|
---|
Binds the attribute name to the specified location . This function can be called before or after the program has been linked. Any attributes that have not been explicitly bound when the program is linked will be assigned locations automatically.
When this function is called after the program has been linked, the program will need to be relinked for the change to take effect.
Parameters: |
|
---|
This is an overloaded function.
Binds the attribute name to the specified location . This function can be called before or after the program has been linked. Any attributes that have not been explicitly bound when the program is linked will be assigned locations automatically.
When this function is called after the program has been linked, the program will need to be relinked for the change to take effect.
Parameters: |
|
---|
This is an overloaded function.
Binds the attribute name to the specified location . This function can be called before or after the program has been linked. Any attributes that have not been explicitly bound when the program is linked will be assigned locations automatically.
When this function is called after the program has been linked, the program will need to be relinked for the change to take effect.
Parameters: | location – PySide.QtCore.int |
---|
Disables the vertex array at location in this shader program that was enabled by a previous call to PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() .
Parameters: | name – str |
---|
This is an overloaded function.
Disables the vertex array called name in this shader program that was enabled by a previous call to PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() .
Parameters: | location – PySide.QtCore.int |
---|
Enables the vertex array at location in this shader program so that the value set by PySide.QtOpenGL.QGLShaderProgram.setAttributeArray() on location will be used by the shader program.
Parameters: | name – str |
---|
This is an overloaded function.
Enables the vertex array called name in this shader program so that the value set by PySide.QtOpenGL.QGLShaderProgram.setAttributeArray() on name will be used by the shader program.
Return type: | long |
---|
Returns the geometry shader input type, if active.
This parameter takes effect the next time the program is linked.
Return type: | long |
---|
Returns the geometry shader output type, if active.
This parameter takes effect the next time the program is linked.
Return type: | PySide.QtCore.int |
---|
Returns the maximum number of vertices the current geometry shader program will produce, if active.
This parameter takes effect the ntext time the program is linked.
Parameters: | context – PySide.QtOpenGL.QGLContext |
---|---|
Return type: | PySide.QtCore.bool |
Returns true if shader programs written in the OpenGL Shading Language (GLSL) are supported on this system; false otherwise.
The context is used to resolve the GLSL extensions. If context is null, then QGLContext.currentContext() is used.
Return type: | PySide.QtCore.bool |
---|
Return type: | PySide.QtCore.bool |
---|
Returns true if this shader program has been linked; false otherwise.
Return type: | PySide.QtCore.bool |
---|
Links together the shaders that were added to this program with PySide.QtOpenGL.QGLShaderProgram.addShader() . Returns true if the link was successful or false otherwise. If the link failed, the error messages can be retrieved with PySide.QtOpenGL.QGLShaderProgram.log() .
Subclasses can override this function to initialize attributes and uniform variables for use in specific shader programs.
If the shader program was already linked, calling this function again will force it to be re-linked.
Return type: | unicode |
---|
Returns the errors and warnings that occurred during the last PySide.QtOpenGL.QGLShaderProgram.link() or PySide.QtOpenGL.QGLShaderProgram.addShader() with explicitly specified source code.
Return type: | PySide.QtCore.int |
---|
Returns the hardware limit for how many vertices a geometry shader can output.
Return type: | long |
---|
Returns the OpenGL identifier associated with this shader program.
See also
Releases the active shader program from the current PySide.QtOpenGL.QGLContext . This is equivalent to calling glUseProgram(0) .
Removes all of the shaders that were added to this program previously. The PySide.QtOpenGL.QGLShader objects for the shaders will not be deleted if they were constructed externally. PySide.QtOpenGL.QGLShader objects that are constructed internally by PySide.QtOpenGL.QGLShaderProgram will be deleted.
Parameters: | shader – PySide.QtOpenGL.QGLShader |
---|
Removes shader from this shader program. The object is not deleted.
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
This is an overloaded function.
Sets an array of 2D vertex values on the attribute called name in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values .
The array will become active when PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() is called on name . Otherwise the value specified with PySide.QtOpenGL.QGLShaderProgram.setAttributeValue() for name will be used.
Parameters: |
|
---|
Sets an array of 2D vertex values on the attribute at location in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values .
The array will become active when PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() is called on the location . Otherwise the value specified with PySide.QtOpenGL.QGLShaderProgram.setAttributeValue() for location will be used.
Parameters: |
|
---|
Sets an array of 3D vertex values on the attribute at location in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values .
The array will become active when PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() is called on the location . Otherwise the value specified with PySide.QtOpenGL.QGLShaderProgram.setAttributeValue() for location will be used.
Parameters: |
|
---|
This is an overloaded function.
Sets an array of 3D vertex values on the attribute called name in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values .
The array will become active when PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() is called on name . Otherwise the value specified with PySide.QtOpenGL.QGLShaderProgram.setAttributeValue() for name will be used.
Parameters: |
|
---|
This is an overloaded function.
Sets an array of 4D vertex values on the attribute called name in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values .
The array will become active when PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() is called on name . Otherwise the value specified with PySide.QtOpenGL.QGLShaderProgram.setAttributeValue() for name will be used.
Parameters: |
|
---|
Sets an array of 4D vertex values on the attribute at location in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values .
The array will become active when PySide.QtOpenGL.QGLShaderProgram.enableAttributeArray() is called on the location . Otherwise the value specified with PySide.QtOpenGL.QGLShaderProgram.setAttributeValue() for location will be used.
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Sets the attribute at location in the current context to value .
Parameters: |
|
---|
Sets the attribute at location in the current context to value .
Parameters: |
|
---|
Sets the attribute at location in the current context to value .
Parameters: |
|
---|
Parameters: |
|
---|
Sets the attribute at location in the current context to value .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
This is an overloaded function.
Sets the attribute called name in the current context to value .
Parameters: |
|
---|
This is an overloaded function.
Sets the attribute called name in the current context to value .
Parameters: |
|
---|
This is an overloaded function.
Sets the attribute called name in the current context to value .
Parameters: |
|
---|
This is an overloaded function.
Sets the attribute called name in the current context to value .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: | inputType – long |
---|
Parameters: | outputType – long |
---|
Parameters: | count – PySide.QtCore.int |
---|
Sets the maximum number of vertices the current geometry shader program will produce, if active, to count .
This parameter takes effect the next time the program is linked.
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 3x3 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 3x4 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 4x2 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 4x3 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 3x2 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 2x3 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 2x4 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 2x2 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 4x4 matrix value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to the red, green, blue, and alpha components of color .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to the x and y coordinates of point .
Parameters: |
|
---|
Parameters: |
|
---|
Sets the uniform variable at location in the current context to the x and y coordinates of point .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Sets the uniform variable at location in the current context to value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to the width and height of the given size .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to a 3x3 transformation matrix value that is specified as a PySide.QtGui.QTransform value.
To set a PySide.QtGui.QTransform value as a 4x4 matrix in a shader, use setUniformValue(location, QMatrix4x4(value)) .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to value .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to the width and height of the given size .
Parameters: |
|
---|
Sets the uniform variable at location in the current context to value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 3x2 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 4x4 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 3x3 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 4x2 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 4x3 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 3x4 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 2x4 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 2x3 matrix value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to the red, green, blue, and alpha components of color .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 2x2 matrix value .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to value .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable associated with name in the current context to the x and y coordinates of point .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable associated with name in the current context to the x and y coordinates of point .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable associated with name in the current context to the width and height of the given size .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable associated with name in the current context to the width and height of the given size .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to value .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to a 3x3 transformation matrix value that is specified as a PySide.QtGui.QTransform value.
To set a PySide.QtGui.QTransform value as a 4x4 matrix in a shader, use setUniformValue(name, QMatrix4x4(value)) .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable called name in the current context to value .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 2D vector elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 2D vector elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 2x2 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 2x2 matrix elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 2x3 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 2x3 matrix elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 2x4 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 2x4 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 3D vector elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 3D vector elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 3x2 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 3x2 matrix elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 3x3 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 3x3 matrix elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 3x4 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 3x4 matrix elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 4D vector elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 4D vector elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 4x2 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 4x2 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 4x3 matrix elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 4x3 matrix elements of values .
Parameters: |
|
---|
Sets the uniform variable array at location in the current context to the count 4x4 matrix elements of values .
Parameters: |
|
---|
This is an overloaded function.
Sets the uniform variable array called name in the current context to the count 4x4 matrix elements of values .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Return type: |
---|
Returns a list of all shaders that have been added to this shader program using PySide.QtOpenGL.QGLShaderProgram.addShader() .
Parameters: | name – str |
---|---|
Return type: | PySide.QtCore.int |
Returns the location of the uniform variable name within this shader program’s parameter list. Returns -1 if name is not a valid uniform variable for this shader program.
Parameters: | name – unicode |
---|---|
Return type: | PySide.QtCore.int |
This is an overloaded function.
Returns the location of the uniform variable name within this shader program’s parameter list. Returns -1 if name is not a valid uniform variable for this shader program.
Parameters: | name – PySide.QtCore.QByteArray |
---|---|
Return type: | PySide.QtCore.int |
This is an overloaded function.
Returns the location of the uniform variable name within this shader program’s parameter list. Returns -1 if name is not a valid uniform variable for this shader program.