| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "qrenderstateset.h" | 
| 41 | #include "qrenderstateset_p.h" | 
| 42 |  | 
| 43 | #include <Qt3DRender/qrenderstate.h> | 
| 44 | #include <Qt3DRender/qframegraphnodecreatedchange.h> | 
| 45 |  | 
| 46 | QT_BEGIN_NAMESPACE | 
| 47 |  | 
| 48 | using namespace Qt3DCore; | 
| 49 |  | 
| 50 | namespace Qt3DRender { | 
| 51 |  | 
| 52 | QRenderStateSetPrivate::QRenderStateSetPrivate() | 
| 53 |     : QFrameGraphNodePrivate() | 
| 54 | { | 
| 55 | } | 
| 56 |  | 
| 57 | /*! | 
| 58 |     \class Qt3DRender::QRenderStateSet | 
| 59 |     \inmodule Qt3DRender | 
| 60 |     \since 5.5 | 
| 61 |     \brief The QRenderStateSet \l {QFrameGraphNode}{FrameGraph} node offers a way of | 
| 62 |     specifying a set of QRenderState objects to be applied during the execution | 
| 63 |     of a framegraph branch. | 
| 64 |  | 
| 65 |     States set on a QRenderStateSet are set globally, contrary to the per-material | 
| 66 |     states that can be set on a QRenderPass. By default, an empty | 
| 67 |     QRenderStateSet will result in all render states being disabled when | 
| 68 |     executed. Adding a QRenderState state explicitly enables that render | 
| 69 |     state at runtime. | 
| 70 |  | 
| 71 |     The RenderStateSet is enabled when added to the active frame graph: | 
| 72 |  | 
| 73 |     \code | 
| 74 |     // using namespace Qt3DRender; | 
| 75 |  | 
| 76 |     Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); | 
| 77 |  | 
| 78 |     QRenderSettings *renderSettings = new QRenderSettings(); | 
| 79 |  | 
| 80 |     QViewport *viewport = new QViewport(); | 
| 81 |     QCameraSelector *cameraSelector = new QCameraSelector(viewport); | 
| 82 |  | 
| 83 |     QClearBuffers *clearBuffers = new QClearBuffers(cameraSelector); | 
| 84 |     clearBuffers->setBuffers(QClearBuffers::ColorDepthBuffer); | 
| 85 |  | 
| 86 |     QRenderStateSet *renderStateSet = new QRenderStateSet(cameraSelector); | 
| 87 |     QCullFace *cullFace = new QCullFace(renderStateSet); | 
| 88 |     cullFace->setMode(QCullFace::Front); | 
| 89 |     renderStateSet->addRenderState(cullFace); | 
| 90 |  | 
| 91 |     renderSettings->setActiveFrameGraph(viewport); | 
| 92 |  | 
| 93 |     rootEntity->addComponent(renderSettings); | 
| 94 |     \endcode | 
| 95 |  | 
| 96 |     \sa QRenderState, QRenderPass | 
| 97 |  */ | 
| 98 |  | 
| 99 | /*! | 
| 100 |     \qmltype RenderStateSet | 
| 101 |     \inqmlmodule Qt3D.Render | 
| 102 |     \since 5.5 | 
| 103 |     \inherits FrameGraphNode | 
| 104 |     \instantiates Qt3DRender::QRenderStateSet | 
| 105 |     \brief The RenderStateSet \l {FrameGraphNode}{FrameGraph} node offers a way of | 
| 106 |     specifying a set of RenderState objects to be applied during the execution | 
| 107 |     of a framegraph branch. | 
| 108 |  | 
| 109 |     States set on a RenderStateSet are set globally, contrary to the per-material | 
| 110 |     states that can be set on a RenderPass. By default, an empty | 
| 111 |     RenderStateSet will result in all render states being disabled when | 
| 112 |     executed. Adding a RenderState state explicitly enables that render | 
| 113 |     state at runtime. | 
| 114 |  | 
| 115 |     The RenderStateSet is enabled when added to the active frame graph: | 
| 116 |  | 
| 117 |     \qml | 
| 118 |     import Qt3D.Core 2.0 | 
| 119 |     import Qt3D.Render 2.0 | 
| 120 |     import Qt3D.Extras 2.0 | 
| 121 |  | 
| 122 |     Entity { | 
| 123 |         id: rootNode | 
| 124 |         components: [ | 
| 125 |             RenderSettings { | 
| 126 |                 activeFrameGraph: RenderSurfaceSelector { | 
| 127 |                     ClearBuffers { | 
| 128 |                         buffers : ClearBuffers.ColorDepthBuffer | 
| 129 |  | 
| 130 |                         CameraSelector { | 
| 131 |                             camera: Camera { | 
| 132 |                                 position: Qt.vector3d(10, 0, 0) | 
| 133 |                                 viewCenter: Qt.vector3d(0, 0, 0) | 
| 134 |                             } | 
| 135 |                             RenderStateSet { | 
| 136 |                                 renderStates: [ | 
| 137 |                                     CullFace { mode: CullFace.Back } | 
| 138 |                                 ] | 
| 139 |                             } | 
| 140 |                         } | 
| 141 |                     } | 
| 142 |                 } | 
| 143 |             } | 
| 144 |         ] | 
| 145 |  | 
| 146 |         Entity { | 
| 147 |             id: sphereEntity | 
| 148 |             components: [ | 
| 149 |                 SphereMesh {}, | 
| 150 |                 PhongMaterial {} | 
| 151 |             ] | 
| 152 |         } | 
| 153 |     } | 
| 154 |     \endqml | 
| 155 |  | 
| 156 |     \sa RenderState, RenderPass | 
| 157 |  */ | 
| 158 |  | 
| 159 | /*! | 
| 160 |     \qmlproperty list<RenderState> RenderStateSet::renderStates | 
| 161 |     Holds the list of RenderState objects used by the RenderStateSet. | 
| 162 | */ | 
| 163 |  | 
| 164 | QRenderStateSet::QRenderStateSet(QNode *parent) | 
| 165 |     : QFrameGraphNode(*new QRenderStateSetPrivate, parent) | 
| 166 | { | 
| 167 | } | 
| 168 |  | 
| 169 | /*! \internal */ | 
| 170 | QRenderStateSet::~QRenderStateSet() | 
| 171 | { | 
| 172 | } | 
| 173 |  | 
| 174 | /*! | 
| 175 |     Adds a new QRenderState \a state to the QRenderStateSet instance. | 
| 176 |  | 
| 177 |     \note Not setting any QRenderState state on a QRenderStateSet instance | 
| 178 |     implies all the render states will be disabled at render time. | 
| 179 |  */ | 
| 180 | void QRenderStateSet::addRenderState(QRenderState *state) | 
| 181 | { | 
| 182 |     Q_ASSERT(state); | 
| 183 |     Q_D(QRenderStateSet); | 
| 184 |  | 
| 185 |     if (!d->m_renderStates.contains(t: state)) { | 
| 186 |         d->m_renderStates.append(t: state); | 
| 187 |  | 
| 188 |         // Ensures proper bookkeeping | 
| 189 |         d->registerDestructionHelper(node: state, func: &QRenderStateSet::removeRenderState, d->m_renderStates); | 
| 190 |  | 
| 191 |         if (!state->parent()) | 
| 192 |             state->setParent(this); | 
| 193 |  | 
| 194 |         d->updateNode(node: state, property: "renderState" , change: Qt3DCore::PropertyValueAdded); | 
| 195 |     } | 
| 196 | } | 
| 197 |  | 
| 198 | /*! | 
| 199 |     Removes the QRenderState \a state from the QRenderStateSet instance. | 
| 200 |  */ | 
| 201 | void QRenderStateSet::removeRenderState(QRenderState *state) | 
| 202 | { | 
| 203 |     Q_ASSERT(state); | 
| 204 |     Q_D(QRenderStateSet); | 
| 205 |  | 
| 206 |     if (!d->m_renderStates.removeOne(t: state)) | 
| 207 |         return; | 
| 208 |     d->updateNode(node: state, property: "renderState" , change: Qt3DCore::PropertyValueRemoved); | 
| 209 |     // Remove bookkeeping connection | 
| 210 |     d->unregisterDestructionHelper(node: state); | 
| 211 | } | 
| 212 |  | 
| 213 | /*! | 
| 214 |     Returns the list of QRenderState objects that compose the QRenderStateSet instance. | 
| 215 |  */ | 
| 216 | QVector<QRenderState *> QRenderStateSet::renderStates() const | 
| 217 | { | 
| 218 |     Q_D(const QRenderStateSet); | 
| 219 |     return d->m_renderStates; | 
| 220 | } | 
| 221 |  | 
| 222 | Qt3DCore::QNodeCreatedChangeBasePtr QRenderStateSet::createNodeCreationChange() const | 
| 223 | { | 
| 224 |     auto creationChange = QFrameGraphNodeCreatedChangePtr<QRenderStateSetData>::create(arguments: this); | 
| 225 |     auto &data = creationChange->data; | 
| 226 |     Q_D(const QRenderStateSet); | 
| 227 |     data.renderStateIds = qIdsForNodes(nodes: d->m_renderStates); | 
| 228 |     return creationChange; | 
| 229 | } | 
| 230 |  | 
| 231 | } // namespace Qt3DRender | 
| 232 |  | 
| 233 | QT_END_NAMESPACE | 
| 234 |  |