The QSGSimpleMaterialShader class provides a convenient way of building custom OpenGL-based materials for the scene graph. 更多...
头: | #include <QSGSimpleMaterialShader> |
qmake: | QT += quick |
继承: | QSGMaterialShader |
virtual QList<QByteArray> | 属性 () const = 0 |
virtual void | resolveUniforms () |
const char * | uniformMatrixName () const |
const char * | uniformOpacityName () const |
virtual void | updateState (const State * newState , const State * oldState ) = 0 |
QSG_DECLARE_SIMPLE_COMPARABLE_SHADER ( Shader , 状态 ) | |
QSG_DECLARE_SIMPLE_SHADER ( Shader , 状态 ) |
The QSGSimpleMaterialShader class provides a convenient way of building custom OpenGL-based materials for the scene graph.
警告: This utility class is only functional when running with the OpenGL backend of the Qt Quick scenegraph.
Where the QSGMaterial and QSGMaterialShader API requires a bit of boilerplate code to create a functioning material, the QSGSimpleMaterialShader tries to hide some of this through the use of templates.
QSGSimpleMaterialShader::vertexShader () 和 QSGSimpleMaterialShader::fragmentShader () are used to specify the actual shader source code. The names of the vertex attributes should be listed in the QSGSimpleMaterialShader::attributes ()
QSGSimpleMaterialShader::updateState () is used to push the material state to the OpenGL shader program.
The actual OpenGL shader program is accessible through the QSGSimpleMaterialShader::program () 函数。
每个
QSGSimpleMaterialShader
implementation operates on a unique state struct. The state struct must be declared using the
QSG_DECLARE_SIMPLE_SHADER
宏。
Here is a simple example of a custom solid-color:
struct Color { float r, g, b, a; }; class MinimalShader : public QSGSimpleMaterialShader<Color> { QSG_DECLARE_SIMPLE_SHADER(MinimalShader, Color) public: const char *vertexShader() const { return "attribute highp vec4 vertex; \n" "uniform highp mat4 qt_Matrix; \n" "void main() { \n" " gl_Position = qt_Matrix * vertex; \n" "}"; } const char *fragmentShader() const { return "uniform lowp float qt_Opacity; \n" "uniform lowp vec4 color; \n" "void main() { \n" " gl_FragColor = color * qt_Opacity; \n" "}"; } QList<QByteArray> attributes() const { return QList<QByteArray>() << "vertex"; } void updateState(const Color *color, const Color *) { program()->setUniformValue("color", color->r, color->g, color->b, color->a); } };
Instances of materials using this shader can be created using the createMaterial() function which will be defined by the QSG_DECLARE_SIMPLE_SHADER 宏。
QSGSimpleMaterial<Color> *material = MinimalShader::createMaterial(); material->state()->r = 1; material->state()->g = 0; material->state()->b = 0; material->state()->a = 1; node->setMaterial(material);
The scene graph will often try to find materials that have the same or at least similar state so that these can be batched together inside the renderer, which gives better performance. To specify sortable material states, use QSG_DECLARE_SIMPLE_COMPARABLE_SHADER 而不是 QSG_DECLARE_SIMPLE_SHADER . The state struct must then also define the function:
int compare(const Type *other) const;
警告: The QSGSimpleMaterialShader relies on template instantiation to create a QSGMaterialType which the scene graph renderer internally uses to identify this shader. For this reason, the unique QSGSimpleMaterialShader implementation must be instantiated with a unique C++ type.
注意: 所有带有 QSG 前缀的类只应用于场景图形渲染线程。见 场景图形和渲染 了解更多信息。
另请参阅 场景图形 - 简单材质 .
[pure virtual]
QList
<
QByteArray
> QSGSimpleMaterialShader::
属性
() const
Returns a list of names, declaring the vertex attributes in the vertex shader.
[虚拟]
void
QSGSimpleMaterialShader::
resolveUniforms
()
Reimplement this function to resolve the location of named uniforms in the shader program.
This function is called when the material shader is initialized.
Returns the name for the transform matrix uniform of this item. The default value is
qt_Matrix
.
Returns the name for the opacity uniform of this item. The default value is
qt_Opacity
.
[pure virtual]
void
QSGSimpleMaterialShader::
updateState
(const
State
*
newState
, const
State
*
oldState
)
Called whenever the state of this shader should be updated from oldState to newState , typical for each new set of geometries being drawn.
Both the old and the new state are passed in so that the implementation can compare and minimize the state changes when applicable.
This macro is used to declare a
QSGMaterialType
和
createMaterial()
function for
Shader
采用给定
状态
, where the
状态
class must define a compare function on the form:
int compare(const State *other) const;
This macro is used to declare a
QSGMaterialType
和
createMaterial()
function for
Shader
采用给定
状态
.