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
5
7
8#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
9
10#include <QtQuick3DUtils/private/qssgutils_p.h>
11
14
15#include <QtQuick3DRuntimeRender/private/qssgrenderbuffermanager_p.h>
16
17#include <QtQuick3DUtils/private/qssgplane_p.h>
18
20
21QSSGRenderNode::QSSGRenderNode()
22 : QSSGRenderNode(Type::Node)
23{
24}
25
26QSSGRenderNode::QSSGRenderNode(Type type, QSSGRenderGraphObject::FlagT flags)
27 : QSSGRenderGraphObject(type, flags)
28{
29 localTransform = calculateTransformMatrix({}, initScale, {}, {});
30}
31
32QSSGRenderNode::~QSSGRenderNode()
33 = default;
34
35void QSSGRenderNode::markDirty(DirtyFlag dirtyFlag)
36{
37 if ((flags & FlagT(dirtyFlag)) == 0) { // If not already marked
38 flags |= FlagT(dirtyFlag);
39 const bool markSubtreeDirty = ((FlagT(dirtyFlag) & FlagT(DirtyFlag::SubtreeUpdateMask)) != 0);
40 if (markSubtreeDirty) {
41 for (auto &child : children)
42 child.markDirty(dirtyFlag);
43 }
44 }
45}
46
47void QSSGRenderNode::clearDirty(DirtyFlag dirtyFlag)
48{
49 flags &= ~FlagT(dirtyFlag);
50}
51
52void QSSGRenderNode::setState(LocalState state, bool on)
53{
54 const bool changed = (getLocalState(state) != on);
55 if (changed) { // Mark state dirty
56 flags = on ? (flags | FlagT(state)) : (flags & ~FlagT(state));
57
58 // Mark state dirty
59 switch (state) {
60 case QSSGRenderNode::LocalState::Active:
61 markDirty(DirtyFlag::ActiveDirty);
62 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
63 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
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 && type != QSSGRenderNode::Type::ImportScene) {
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 children.push_back(inChild);
123 // Don't mark dirty if this is a layer, because layers won't have any global values
124 // calculated and therefore will not have the dirty flag cleared!
125 if (inChild.type != QSSGRenderNode::Type::Layer)
126 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
127 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
128 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
129}
130
131void QSSGRenderNode::removeChild(QSSGRenderNode &inChild)
132{
133 if (Q_UNLIKELY(type != QSSGRenderNode::Type::Layer && inChild.parent != this)) {
134 Q_ASSERT(inChild.parent == this);
135 return;
136 }
137
138 inChild.parent = nullptr;
139 if (inChild.type != QSSGRenderNode::Type::Root)
140 inChild.rootNodeRef = nullptr;
141 inChild.h = {};
142 children.remove(inChild);
143 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
144 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
145 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
146}
147
148void QSSGRenderNode::removeFromGraph()
149{
150 if (parent)
151 parent->removeChild(*this);
152
153 // Orphan all of my children.
154 for (auto it = children.begin(), end = children.end(); it != end;) {
155 auto &removedChild = *it++;
156 children.remove(removedChild);
157 removedChild.parent = nullptr;
158 }
159}
160
161QSSGBounds3 QSSGRenderNode::getBounds(QSSGBufferManager &inManager,
162 bool inIncludeChildren) const
163{
164 QSSGBounds3 retval;
165 if (inIncludeChildren)
166 retval = getChildBounds(inManager);
167
168 if (type == QSSGRenderGraphObject::Type::Model) {
169 auto model = static_cast<const QSSGRenderModel *>(this);
170 retval.include(inManager.getModelBounds(model));
171 }
172 return retval;
173}
174
175QSSGBounds3 QSSGRenderNode::getChildBounds(QSSGBufferManager &inManager) const
176{
177 QSSGBounds3 retval;
178 QSSGBounds3 childBounds;
179 for (auto &child : children) {
180 childBounds = child.getBounds(inManager);
181 if (!childBounds.isEmpty()) {
182 // Transform the bounds into our local space.
183 childBounds.transform(child.localTransform);
184 retval.include(childBounds);
185 }
186 }
187 return retval;
188}
189
190QVector3D QSSGRenderNode::getDirection(const QMatrix4x4 &globalTransform)
191{
192 const float *dataPtr(globalTransform.data());
193 QVector3D retval(dataPtr[8], dataPtr[9], dataPtr[10]);
194 retval.normalize();
195 return retval;
196}
197
198QVector3D QSSGRenderNode::getScalingCorrectDirection(const QMatrix4x4 &globalTransform)
199{
200 QMatrix3x3 theDirMatrix = globalTransform.normalMatrix();
201 QVector3D theOriginalDir(0, 0, -1);
202 QVector3D retval = QSSGUtils::mat33::transform(theDirMatrix, theOriginalDir);
203 // Should already be normalized, but whatever
204 retval.normalize();
205 return retval;
206}
207
208void QSSGRenderNode::calculateMVP(const QMatrix4x4 &globalTransform, const QMatrix4x4 &inViewProjection, QMatrix4x4 &outMVP)
209{
210 outMVP = inViewProjection * globalTransform;
211}
212
213void QSSGRenderNode::calculateNormalMatrix(const QMatrix4x4 &globalTransform, QMatrix3x3 &outNormalMatrix)
214{
215 outNormalMatrix = globalTransform.normalMatrix();
216}
217
218void QSSGRenderNode::calculateMVPAndNormalMatrix(const QMatrix4x4 &globalTransform,
219 const QMatrix4x4 &inViewProjection,
220 QMatrix4x4 &outMVP,
221 QMatrix3x3 &outNormalMatrix)
222{
223 calculateMVP(globalTransform, inViewProjection, outMVP);
224 calculateNormalMatrix(globalTransform, outNormalMatrix);
225}
226
227QT_END_NAMESPACE
Combined button and popup list for selecting options.