ShaderProgramThe ShaderProgram item is derivative class of the more general Effect class in QML/3d. Whereas the Effect class provides support for standard effects under OpenGL, the ShaderProgramEffect supports effects based on custom shader programs for the GPU. More... Inherits Effect ShaderProgram instantiates the C++ class ShaderProgram This type was introduced in Qt 4.8. Detailed DescriptionThe ShaderProgram class provides Qml/3d users with the ability to use a QGLShaderProgram within the logical context of the normal Effect class provided by Qml/3d. If the system does not support shaders, then ShaderProgram will behave the same as Effect, with support for simple lit materials only. AttributesShaderProgram provides a standard set of 8 vertex attributes that can be provided via the geometry Mesh:
These attributes are used in the vertexShader, as in the following example of a simple texture shader: attribute highp vec4 qt_Vertex; attribute highp vec4 qt_MultiTexCoord0; uniform mediump mat4 qt_ModelViewProjectionMatrix; varying highp vec4 texCoord; void main(void) { gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex; texCoord = qt_MultiTexCoord0; } Standard uniform variablesShaderProgram provides a standard set of uniform variables for common values from the environment:
The above variables are usually declared in the shaders as follows (where highp may be replaced with mediump or lowp depending upon the shader's precision requirements): uniform highp mat4 qt_ModelViewProjectionMatrix; uniform highp mat4 qt_ModelViewMatrix; uniform highp mat3 qt_NormalMatrix; uniform sampler2D qt_Texture0; uniform highp vec4 qt_Color; Other lighting and material values, such as the ambient, diffuse, and specular colors, can be passed to the shader program using custom uniform variables, or the standard variable names described in the QGLShaderProgramEffect documentation. Custom uniform variablesMany properties defined on the ShaderProgram are automatically exposed as uniforms for the fragment and vertex shaders under the same name. QML and shader types do not match exactly, so the following table shows how QML properties should be declared in qml compared to shader programs:
Note: The precision hints in this table are just examples. highp, mediump, and lowp do not map directly onto floats, doubles, colors etc. Choose the most appropriate variable type for your qml or javascript, and the most appropriate precision for your shader program. Be aware that variant properties in general and matrices in particular can have significant performance implications. Conversion from variants can be slow, and matrices can consume multiple slots for uniforms, which are usually limited by hardware. String properties are assumed to be urls to images for use in textures. Where these images are remote, they are loaded in the background and bound to the effect when they are ready. See also QGLGraphicsViewportItem. |