1// Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qrenderstateset.h"
5#include "qrenderstateset_p.h"
6
7#include <Qt3DRender/qrenderstate.h>
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt3DCore;
12
13namespace Qt3DRender {
14
15QRenderStateSetPrivate::QRenderStateSetPrivate()
16 : QFrameGraphNodePrivate()
17{
18}
19
20/*!
21 \class Qt3DRender::QRenderStateSet
22 \inmodule Qt3DRender
23 \since 5.5
24 \brief The QRenderStateSet \l {QFrameGraphNode}{FrameGraph} node offers a way of
25 specifying a set of QRenderState objects to be applied during the execution
26 of a framegraph branch.
27
28 States set on a QRenderStateSet are set globally, contrary to the per-material
29 states that can be set on a QRenderPass. By default, an empty
30 QRenderStateSet will result in all render states being disabled when
31 executed. Adding a QRenderState state explicitly enables that render
32 state at runtime.
33
34 The RenderStateSet is enabled when added to the active frame graph:
35
36 \code
37 // using namespace Qt3DRender;
38
39 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
40
41 QRenderSettings *renderSettings = new QRenderSettings();
42
43 QViewport *viewport = new QViewport();
44 QCameraSelector *cameraSelector = new QCameraSelector(viewport);
45
46 QClearBuffers *clearBuffers = new QClearBuffers(cameraSelector);
47 clearBuffers->setBuffers(QClearBuffers::ColorDepthBuffer);
48
49 QRenderStateSet *renderStateSet = new QRenderStateSet(cameraSelector);
50 QCullFace *cullFace = new QCullFace(renderStateSet);
51 cullFace->setMode(QCullFace::Front);
52 renderStateSet->addRenderState(cullFace);
53
54 renderSettings->setActiveFrameGraph(viewport);
55
56 rootEntity->addComponent(renderSettings);
57 \endcode
58
59 \sa QRenderState, QRenderPass
60 */
61
62/*!
63 \qmltype RenderStateSet
64 \inqmlmodule Qt3D.Render
65 \since 5.5
66 \inherits FrameGraphNode
67 \instantiates Qt3DRender::QRenderStateSet
68 \brief The RenderStateSet \l {FrameGraphNode}{FrameGraph} node offers a way of
69 specifying a set of RenderState objects to be applied during the execution
70 of a framegraph branch.
71
72 States set on a RenderStateSet are set globally, contrary to the per-material
73 states that can be set on a RenderPass. By default, an empty
74 RenderStateSet will result in all render states being disabled when
75 executed. Adding a RenderState state explicitly enables that render
76 state at runtime.
77
78 The RenderStateSet is enabled when added to the active frame graph:
79
80 \qml
81 import Qt3D.Core 2.0
82 import Qt3D.Render 2.0
83 import Qt3D.Extras 2.0
84
85 Entity {
86 id: rootNode
87 components: [
88 RenderSettings {
89 activeFrameGraph: RenderSurfaceSelector {
90 ClearBuffers {
91 buffers : ClearBuffers.ColorDepthBuffer
92
93 CameraSelector {
94 camera: Camera {
95 position: Qt.vector3d(10, 0, 0)
96 viewCenter: Qt.vector3d(0, 0, 0)
97 }
98 RenderStateSet {
99 renderStates: [
100 CullFace { mode: CullFace.Back }
101 ]
102 }
103 }
104 }
105 }
106 }
107 ]
108
109 Entity {
110 id: sphereEntity
111 components: [
112 GeometryRenderer { view: SphereMesh {} },
113 PhongMaterial {}
114 ]
115 }
116 }
117 \endqml
118
119 \sa RenderState, RenderPass
120 */
121
122/*!
123 \qmlproperty list<RenderState> RenderStateSet::renderStates
124 Holds the list of RenderState objects used by the RenderStateSet.
125*/
126
127QRenderStateSet::QRenderStateSet(QNode *parent)
128 : QFrameGraphNode(*new QRenderStateSetPrivate, parent)
129{
130}
131
132/*! \internal */
133QRenderStateSet::~QRenderStateSet()
134{
135}
136
137/*!
138 Adds a new QRenderState \a state to the QRenderStateSet instance.
139
140 \note Not setting any QRenderState state on a QRenderStateSet instance
141 implies all the render states will be disabled at render time.
142 */
143void QRenderStateSet::addRenderState(QRenderState *state)
144{
145 Q_ASSERT(state);
146 Q_D(QRenderStateSet);
147
148 if (!d->m_renderStates.contains(t: state)) {
149 d->m_renderStates.append(t: state);
150
151 // Ensures proper bookkeeping
152 d->registerDestructionHelper(node: state, func: &QRenderStateSet::removeRenderState, d->m_renderStates);
153
154 if (!state->parent())
155 state->setParent(this);
156
157 d->update();
158 }
159}
160
161/*!
162 Removes the QRenderState \a state from the QRenderStateSet instance.
163 */
164void QRenderStateSet::removeRenderState(QRenderState *state)
165{
166 Q_ASSERT(state);
167 Q_D(QRenderStateSet);
168
169 if (!d->m_renderStates.removeOne(t: state))
170 return;
171 d->update();
172 // Remove bookkeeping connection
173 d->unregisterDestructionHelper(node: state);
174}
175
176/*!
177 Returns the list of QRenderState objects that compose the QRenderStateSet instance.
178 */
179QList<QRenderState *> QRenderStateSet::renderStates() const
180{
181 Q_D(const QRenderStateSet);
182 return d->m_renderStates;
183}
184
185} // namespace Qt3DRender
186
187QT_END_NAMESPACE
188
189#include "moc_qrenderstateset.cpp"
190

source code of qt3d/src/render/framegraph/qrenderstateset.cpp