aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/scenegraph/simplematerial
diff options
context:
space:
mode:
Diffstat (limited to 'examples/quick/scenegraph/simplematerial')
-rw-r--r--examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc26
-rw-r--r--examples/quick/scenegraph/simplematerial/simplematerial.cpp153
-rw-r--r--examples/quick/scenegraph/simplematerial/simplematerial.pro10
-rw-r--r--examples/quick/scenegraph/simplematerial/simplematerialitem.cpp169
-rw-r--r--examples/quick/scenegraph/simplematerial/simplematerialitem.h82
5 files changed, 274 insertions, 166 deletions
diff --git a/examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc b/examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc
index 67ca2d8dbd..d7de1613e4 100644
--- a/examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc
+++ b/examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc
@@ -47,7 +47,7 @@
material state is what we assign to each individual node; in this
case to give them different colors.
- \snippet scenegraph/simplematerial/simplematerial.cpp 1
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 1
The first thing we do when creating custom materials with the
simplified scheme is to create a state class. In this case the
@@ -55,7 +55,7 @@
compare function which the scene graph can use to reorder the node
rendering.
- \snippet scenegraph/simplematerial/simplematerial.cpp 2
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 2
Next we define the material shader, by subclassing a template
instantiation of \l QSGSimpleMaterialShader with our \c State.
@@ -74,21 +74,21 @@
classes. Using the same \c State class in multiple shaders will
will lead to undefined behavior.
- \snippet scenegraph/simplematerial/simplematerial.cpp 3
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 3
Next comes the declaration of the shader source code, where we
define a vertex and fragment shader. The simple material assumes
the presence of \c qt_Matrix in the vertex shader and \c
qt_Opacity in the fragment shader.
- \snippet scenegraph/simplematerial/simplematerial.cpp 4
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 4
We reimplement the \c attributes function to return the name of
the \c aVertex and \c aTexCoord attributes. These attributes
will be mapped to attribute indices 0 and 1 in the node's
geometry.
- \snippet scenegraph/simplematerial/simplematerial.cpp 6
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 6
Uniforms can be accessed either by name or by index, where index
is faster than name. We reimplement the \c resolveUniforms()
@@ -96,7 +96,7 @@
to worry about resolving \c qt_Opacity or \c qt_Matrix as these
are handled by the baseclass.
- \snippet scenegraph/simplematerial/simplematerial.cpp 5
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 5
The \c updateState() function is called once for every unique
state and we use it to update the shader program with the current
@@ -105,7 +105,7 @@
use case, where all the colors are different, the updateState()
function will be called once for every node.
- \snippet scenegraph/simplematerial/simplematerial.cpp 7
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 7
The \c ColorNode class is supposed to draw something, so it needs
to be a subclass of \l QSGGeometryNode.
@@ -129,12 +129,13 @@
Finally, we tell the node to take ownership of the material, so we
do not have to explicitly memory-manage it.
- \snippet scenegraph/simplematerial/simplematerial.cpp 8
+ \snippet scenegraph/simplematerial/simplematerialitem.h 8
Since the Item is providing its own graphics to the scene graph,
- we set the flag \l QQuickItem::ItemHasContents.
+ we set the flag \l QQuickItem::ItemHasContents. We also make sure
+ the item is exposed to QML by adding the QML_ELEMENT macro.
- \snippet scenegraph/simplematerial/simplematerial.cpp 9
+ \snippet scenegraph/simplematerial/simplematerialitem.cpp 9
Whenever the Item has changed graphically, the \l
QQuickItem::updatePaintNode() function is called.
@@ -159,9 +160,8 @@
\snippet scenegraph/simplematerial/simplematerial.cpp 11
- The \c main() function of the application adds the custom QML type
- using \l qmlRegisterType() and opens up a \l QQuickView with our
- QML file.
+ The \c main() function of the application opens up a \l QQuickView
+ with our QML file.
\snippet scenegraph/simplematerial/main.qml 1
diff --git a/examples/quick/scenegraph/simplematerial/simplematerial.cpp b/examples/quick/scenegraph/simplematerial/simplematerial.cpp
index 6773b6fb5a..35866bd6d0 100644
--- a/examples/quick/scenegraph/simplematerial/simplematerial.cpp
+++ b/examples/quick/scenegraph/simplematerial/simplematerial.cpp
@@ -49,162 +49,13 @@
****************************************************************************/
#include <qguiapplication.h>
-
-#include <qsgmaterial.h>
-#include <qsgnode.h>
-
-#include <qquickitem.h>
#include <qquickview.h>
-#include <qsgsimplerectnode.h>
-
-#include <qsgsimplematerial.h>
-
-//! [1]
-struct State
-{
- QColor color;
-
- int compare(const State *other) const {
- uint rgb = color.rgba();
- uint otherRgb = other->color.rgba();
-
- if (rgb == otherRgb) {
- return 0;
- } else if (rgb < otherRgb) {
- return -1;
- } else {
- return 1;
- }
- }
-};
-//! [1]
-
-//! [2]
-class Shader : public QSGSimpleMaterialShader<State>
-{
- QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State);
-//! [2] //! [3]
-public:
-
- const char *vertexShader() const override {
- return
- "attribute highp vec4 aVertex; \n"
- "attribute highp vec2 aTexCoord; \n"
- "uniform highp mat4 qt_Matrix; \n"
- "varying highp vec2 texCoord; \n"
- "void main() { \n"
- " gl_Position = qt_Matrix * aVertex; \n"
- " texCoord = aTexCoord; \n"
- "}";
- }
-
- const char *fragmentShader() const override {
- return
- "uniform lowp float qt_Opacity; \n"
- "uniform lowp vec4 color; \n"
- "varying highp vec2 texCoord; \n"
- "void main () \n"
- "{ \n"
- " gl_FragColor = texCoord.y * texCoord.x * color * qt_Opacity; \n"
- "}";
- }
-//! [3] //! [4]
- QList<QByteArray> attributes() const override
- {
- return QList<QByteArray>() << "aVertex" << "aTexCoord";
- }
-//! [4] //! [5]
- void updateState(const State *state, const State *) override
- {
- program()->setUniformValue(id_color, state->color);
- }
-//! [5] //! [6]
- void resolveUniforms() override
- {
- id_color = program()->uniformLocation("color");
- }
-
-private:
- int id_color;
-//! [6]
-};
-
-
-//! [7]
-class ColorNode : public QSGGeometryNode
-{
-public:
- ColorNode()
- : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
- {
- setGeometry(&m_geometry);
-
- QSGSimpleMaterial<State> *material = Shader::createMaterial();
- material->setFlag(QSGMaterial::Blending);
- setMaterial(material);
- setFlag(OwnsMaterial);
- }
-
- QSGGeometry m_geometry;
-};
-//! [7]
-
-
-//! [8]
-class Item : public QQuickItem
-{
- Q_OBJECT
-
- Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
-
-public:
-
- Item()
- {
- setFlag(ItemHasContents, true);
- }
-
- void setColor(const QColor &color) {
- if (m_color != color) {
- m_color = color;
- emit colorChanged();
- update();
- }
- }
- QColor color() const {
- return m_color;
- }
-
-signals:
- void colorChanged();
-
-private:
- QColor m_color;
-
-//! [8] //! [9]
-public:
- QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override
- {
- ColorNode *n = static_cast<ColorNode *>(node);
- if (!node)
- n = new ColorNode();
-
- QSGGeometry::updateTexturedRectGeometry(n->geometry(), boundingRect(), QRectF(0, 0, 1, 1));
- static_cast<QSGSimpleMaterial<State>*>(n->material())->state()->color = m_color;
-
- n->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
-
- return n;
- }
-};
-//! [9] //! [11]
+//! [11]
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
- qmlRegisterType<Item>("SimpleMaterial", 1, 0, "SimpleMaterialItem");
-
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:///scenegraph/simplematerial/main.qml"));
@@ -212,6 +63,4 @@ int main(int argc, char **argv)
return app.exec();
}
-
-#include "simplematerial.moc"
//! [11]
diff --git a/examples/quick/scenegraph/simplematerial/simplematerial.pro b/examples/quick/scenegraph/simplematerial/simplematerial.pro
index 6ae935f357..131af45974 100644
--- a/examples/quick/scenegraph/simplematerial/simplematerial.pro
+++ b/examples/quick/scenegraph/simplematerial/simplematerial.pro
@@ -1,8 +1,13 @@
QT += quick
+CONFIG += qmltypes
+QML_IMPORT_NAME = SimpleMaterial
+QML_IMPORT_MAJOR_VERSION = 1
+
SOURCES += \
- simplematerial.cpp
+ simplematerial.cpp \
+ simplematerialitem.cpp
RESOURCES += simplematerial.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/quick/scenegraph/simplematerial
@@ -10,3 +15,6 @@ qml.files = main.qml
qml.path = $$[QT_INSTALL_EXAMPLES]/quick/scenegraph/simplematerial
INSTALLS += target qml
+
+HEADERS += \
+ simplematerialitem.h
diff --git a/examples/quick/scenegraph/simplematerial/simplematerialitem.cpp b/examples/quick/scenegraph/simplematerial/simplematerialitem.cpp
new file mode 100644
index 0000000000..c4c7b2e07e
--- /dev/null
+++ b/examples/quick/scenegraph/simplematerial/simplematerialitem.cpp
@@ -0,0 +1,169 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the demonstration applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "simplematerialitem.h"
+
+#include <QtQuick/qsgsimplematerial.h>
+#include <QtQuick/qsggeometry.h>
+#include <QtQuick/qsgnode.h>
+
+//! [1]
+struct State
+{
+ QColor color;
+
+ int compare(const State *other) const {
+ uint rgb = color.rgba();
+ uint otherRgb = other->color.rgba();
+
+ if (rgb == otherRgb) {
+ return 0;
+ } else if (rgb < otherRgb) {
+ return -1;
+ } else {
+ return 1;
+ }
+ }
+};
+//! [1]
+
+//! [2]
+class Shader : public QSGSimpleMaterialShader<State>
+{
+ QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State);
+//! [2] //! [3]
+public:
+
+ const char *vertexShader() const override {
+ return
+ "attribute highp vec4 aVertex; \n"
+ "attribute highp vec2 aTexCoord; \n"
+ "uniform highp mat4 qt_Matrix; \n"
+ "varying highp vec2 texCoord; \n"
+ "void main() { \n"
+ " gl_Position = qt_Matrix * aVertex; \n"
+ " texCoord = aTexCoord; \n"
+ "}";
+ }
+
+ const char *fragmentShader() const override {
+ return
+ "uniform lowp float qt_Opacity; \n"
+ "uniform lowp vec4 color; \n"
+ "varying highp vec2 texCoord; \n"
+ "void main () \n"
+ "{ \n"
+ " gl_FragColor = texCoord.y * texCoord.x * color * qt_Opacity; \n"
+ "}";
+ }
+//! [3] //! [4]
+ QList<QByteArray> attributes() const override
+ {
+ return QList<QByteArray>() << "aVertex" << "aTexCoord";
+ }
+//! [4] //! [5]
+ void updateState(const State *state, const State *) override
+ {
+ program()->setUniformValue(id_color, state->color);
+ }
+//! [5] //! [6]
+ void resolveUniforms() override
+ {
+ id_color = program()->uniformLocation("color");
+ }
+
+private:
+ int id_color;
+//! [6]
+};
+
+
+//! [7]
+class ColorNode : public QSGGeometryNode
+{
+public:
+ ColorNode()
+ : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
+ {
+ setGeometry(&m_geometry);
+
+ QSGSimpleMaterial<State> *material = Shader::createMaterial();
+ material->setFlag(QSGMaterial::Blending);
+ setMaterial(material);
+ setFlag(OwnsMaterial);
+ }
+
+ QSGGeometry m_geometry;
+};
+//! [7]
+
+void SimpleMaterialItem::setColor(const QColor &color) {
+ if (m_color != color) {
+ m_color = color;
+ emit colorChanged();
+ update();
+ }
+}
+
+//! [9]
+QSGNode *SimpleMaterialItem::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *)
+{
+ ColorNode *n = static_cast<ColorNode *>(node);
+ if (!node)
+ n = new ColorNode();
+
+ QSGGeometry::updateTexturedRectGeometry(n->geometry(), boundingRect(), QRectF(0, 0, 1, 1));
+ static_cast<QSGSimpleMaterial<State>*>(n->material())->state()->color = m_color;
+
+ n->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
+
+ return n;
+}
+//! [9]
diff --git a/examples/quick/scenegraph/simplematerial/simplematerialitem.h b/examples/quick/scenegraph/simplematerial/simplematerialitem.h
new file mode 100644
index 0000000000..5775ad097b
--- /dev/null
+++ b/examples/quick/scenegraph/simplematerial/simplematerialitem.h
@@ -0,0 +1,82 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the demonstration applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef SIMPLEMATERIALITEM_H
+#define SIMPLEMATERIALITEM_H
+
+#include <QtQuick/qquickitem.h>
+
+//! [8]
+class SimpleMaterialItem : public QQuickItem
+{
+ Q_OBJECT
+
+ Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
+ QML_ELEMENT
+
+public:
+
+ SimpleMaterialItem() { setFlag(ItemHasContents, true); }
+
+ void setColor(const QColor &color);
+ QColor color() const { return m_color; }
+
+signals:
+ void colorChanged();
+
+private:
+ QColor m_color;
+
+public:
+ QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override;
+};
+//! [8]
+
+#endif // SIMPLEMATERIALITEM_H