Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgrendernode.cpp
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6
7
9
10#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
11
12#include <QtQuick3DUtils/private/qssgutils_p.h>
13
16
17#include <QtQuick3DRuntimeRender/private/qssgrenderbuffermanager_p.h>
18
19#include <QtQuick3DUtils/private/qssgplane_p.h>
20
22
23QSSGRenderNode::QSSGRenderNode()
24 : QSSGRenderNode(Type::Node)
25{
26}
27
28QSSGRenderNode::QSSGRenderNode(Type type, QSSGRenderGraphObject::FlagT flags)
29 : QSSGRenderGraphObject(type, flags)
30{
31 localTransform = calculateTransformMatrix({}, initScale, {}, {});
32}
33
34QSSGRenderNode::~QSSGRenderNode()
35 = default;
36
37void QSSGRenderNode::markDirty(DirtyFlag dirtyFlag)
38{
39 if ((flags & FlagT(dirtyFlag)) == 0) { // If not already marked
40 flags |= FlagT(dirtyFlag);
41 const bool markSubtreeDirty = ((FlagT(dirtyFlag) & FlagT(DirtyFlag::SubtreeUpdateMask)) != 0);
42 if (markSubtreeDirty) {
43 for (auto &child : children)
44 child.markDirty(dirtyFlag);
45 }
46 }
47}
48
49void QSSGRenderNode::clearDirty(DirtyFlag dirtyFlag)
50{
51 flags &= ~FlagT(dirtyFlag);
52}
53
54void QSSGRenderNode::setState(LocalState state, bool on)
55{
56 const bool changed = (getLocalState(state) != on);
57 if (changed) { // Mark state dirty
58 flags = on ? (flags | FlagT(state)) : (flags & ~FlagT(state));
59
60 // Mark state dirty
61 switch (state) {
62 case QSSGRenderNode::LocalState::Active:
63 markDirty(DirtyFlag::ActiveDirty);
64 break;
65 case QSSGRenderNode::LocalState::Pickable:
66 markDirty(DirtyFlag::PickableDirty);
67 break;
68 case QSSGRenderNode::LocalState::Imported:
69 markDirty(DirtyFlag::ImportDirty);
70 break;
71 }
72 }
73
74}
75
76QMatrix4x4 QSSGRenderNode::calculateTransformMatrix(QVector3D position, QVector3D scale, QVector3D pivot, QQuaternion rotation)
77{
78 QMatrix4x4 transform;
79
80 // Offset the origin (this is our pivot point)
81 auto offset = (-pivot * scale);
82
83 // Scale
84 transform(0, 0) = scale[0];
85 transform(1, 1) = scale[1];
86 transform(2, 2) = scale[2];
87
88 // Offset (before rotation)
89 transform(0, 3) = offset[0];
90 transform(1, 3) = offset[1];
91 transform(2, 3) = offset[2];
92
93 // rotate
94 transform = QMatrix4x4{rotation.toRotationMatrix()} * transform;
95
96 // translate
97 transform(0, 3) += position[0];
98 transform(1, 3) += position[1];
99 transform(2, 3) += position[2];
100
101 return transform;
102}
103
104void QSSGRenderNode::addChild(QSSGRenderNode &inChild)
105{
106 QSSG_ASSERT_X(inChild.parent != this, "Already a child of this node!", return);
107
108 // Adding children to a layer does not reset parent
109 // because layers can share children over with other layers
110 if (type != QSSGRenderNode::Type::Layer) {
111 if (inChild.parent && inChild.parent != this)
112 inChild.parent->removeChild(inChild);
113 inChild.parent = this;
114 }
115
116 QSSG_ASSERT_X(type != QSSGRenderNode::Type::Root || inChild.type == QSSGRenderNode::Type::Layer,
117 "Only layers can be added to the root!", return);
118
119 if (inChild.type != QSSGRenderNode::Type::Root)
120 inChild.rootNodeRef = rootNodeRef;
121
122 // NOTE: We push importScene nodes to the front because this way we'll always index
123 // the imported scene first. This keeps their global index low and stable across
124 // views, so a shared imported scene can be placed at the start of a shared data store.
125 if (inChild.type != QSSGRenderNode::Type::ImportScene)
126 children.push_back(inChild);
127 else
128 children.push_front(inChild);
129
130 // Don't mark dirty if this is a layer, because layers won't have any global values
131 // calculated and therefore will not have the dirty flag cleared!
132 if (inChild.type != QSSGRenderNode::Type::Layer)
133 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
134 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
135 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
136}
137
138void QSSGRenderNode::removeChild(QSSGRenderNode &inChild)
139{
140 if (Q_UNLIKELY(type != QSSGRenderNode::Type::Layer && inChild.parent != this)) {
141 Q_ASSERT(inChild.parent == this);
142 return;
143 }
144
145 inChild.parent = nullptr;
146 if (inChild.type != QSSGRenderNode::Type::Root)
147 inChild.rootNodeRef = nullptr;
148 inChild.h = {};
149 children.remove(inChild);
150 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
151 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
152 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
153}
154
155void QSSGRenderNode::removeFromGraph()
156{
157 if (parent)
158 parent->removeChild(*this);
159
160 // Orphan all of my children.
161 for (auto it = children.begin(), end = children.end(); it != end;) {
162 auto &removedChild = *it++;
163 children.remove(removedChild);
164 removedChild.parent = nullptr;
165 removedChild.rootNodeRef = nullptr;
166 }
167}
168
169QSSGBounds3 QSSGRenderNode::getBounds(QSSGBufferManager &inManager,
170 bool inIncludeChildren) const
171{
172 QSSGBounds3 retval;
173 if (inIncludeChildren)
174 retval = getChildBounds(inManager);
175
176 if (type == QSSGRenderGraphObject::Type::Model) {
177 auto model = static_cast<const QSSGRenderModel *>(this);
178 retval.include(inManager.getModelBounds(model));
179 }
180 return retval;
181}
182
183QSSGBounds3 QSSGRenderNode::getChildBounds(QSSGBufferManager &inManager) const
184{
185 QSSGBounds3 retval;
186 QSSGBounds3 childBounds;
187 for (auto &child : children) {
188 childBounds = child.getBounds(inManager);
189 if (!childBounds.isEmpty()) {
190 // Transform the bounds into our local space.
191 childBounds.transform(child.localTransform);
192 retval.include(childBounds);
193 }
194 }
195 return retval;
196}
197
198QVector3D QSSGRenderNode::getDirection(const QMatrix4x4 &globalTransform)
199{
200 const float *dataPtr(globalTransform.data());
201 QVector3D retval(dataPtr[8], dataPtr[9], dataPtr[10]);
202 retval.normalize();
203 return retval;
204}
205
206QVector3D QSSGRenderNode::getScalingCorrectDirection(const QMatrix4x4 &globalTransform)
207{
208 QMatrix3x3 theDirMatrix = globalTransform.normalMatrix();
209 QVector3D theOriginalDir(0, 0, -1);
210 QVector3D retval = QSSGUtils::mat33::transform(theDirMatrix, theOriginalDir);
211 // Should already be normalized, but whatever
212 retval.normalize();
213 return retval;
214}
215
216void QSSGRenderNode::calculateMVP(const QMatrix4x4 &globalTransform, const QMatrix4x4 &inViewProjection, QMatrix4x4 &outMVP)
217{
218 outMVP = inViewProjection * globalTransform;
219}
220
221void QSSGRenderNode::calculateNormalMatrix(const QMatrix4x4 &globalTransform, QMatrix3x3 &outNormalMatrix)
222{
223 outNormalMatrix = globalTransform.normalMatrix();
224}
225
226void QSSGRenderNode::calculateMVPAndNormalMatrix(const QMatrix4x4 &globalTransform,
227 const QMatrix4x4 &inViewProjection,
228 QMatrix4x4 &outMVP,
229 QMatrix3x3 &outNormalMatrix)
230{
231 calculateMVP(globalTransform, inViewProjection, outMVP);
232 calculateNormalMatrix(globalTransform, outNormalMatrix);
233}
234
235QT_END_NAMESPACE
Combined button and popup list for selecting options.