31 localTransform = calculateTransformMatrix({}, initScale, {}, {});
37void QSSGRenderNode::markDirty(DirtyFlag dirtyFlag)
39 if ((flags & FlagT(dirtyFlag)) == 0) {
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);
54void QSSGRenderNode::setState(LocalState state,
bool on)
56 const bool changed = (getLocalState(state) != on);
58 flags = on ? (flags | FlagT(state)) : (flags & ~FlagT(state));
62 case QSSGRenderNode::LocalState::Active:
63 markDirty(DirtyFlag::ActiveDirty);
65 case QSSGRenderNode::LocalState::Pickable:
66 markDirty(DirtyFlag::PickableDirty);
68 case QSSGRenderNode::LocalState::Imported:
69 markDirty(DirtyFlag::ImportDirty);
76QMatrix4x4 QSSGRenderNode::calculateTransformMatrix(QVector3D position, QVector3D scale, QVector3D pivot, QQuaternion rotation)
81 auto offset = (-pivot * scale);
84 transform(0, 0) = scale[0];
85 transform(1, 1) = scale[1];
86 transform(2, 2) = scale[2];
89 transform(0, 3) = offset[0];
90 transform(1, 3) = offset[1];
91 transform(2, 3) = offset[2];
94 transform = QMatrix4x4{rotation.toRotationMatrix()} * transform;
97 transform(0, 3) += position[0];
98 transform(1, 3) += position[1];
99 transform(2, 3) += position[2];
104void QSSGRenderNode::addChild(QSSGRenderNode &inChild)
106 QSSG_ASSERT_X(inChild.parent !=
this,
"Already a child of this node!",
return);
114 if (type != QSSGRenderNode::Type::Layer) {
115 if (inChild.parent && inChild.parent !=
this)
116 inChild.parent->removeChild(inChild);
117 inChild.parent =
this;
120 QSSG_ASSERT_X(type != QSSGRenderNode::Type::Root || inChild.type == QSSGRenderNode::Type::Layer,
121 "Only layers can be added to the root!",
return);
123 if (inChild.type != QSSGRenderNode::Type::Root)
124 inChild.rootNodeRef = rootNodeRef;
129 if (inChild.type != QSSGRenderNode::Type::ImportScene)
130 children.push_back(inChild);
132 children.push_front(inChild);
136 if (inChild.type != QSSGRenderNode::Type::Layer)
137 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
138 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
139 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
156void QSSGRenderNode::removeChild(QSSGRenderNode &inChild)
158 if (type == QSSGRenderNode::Type::Layer) {
159 if (!isChildOfLayer(
static_cast<
const QSSGRenderLayer &>(*
this), inChild))
161 }
else if (Q_UNLIKELY(inChild.parent !=
this)) {
162 Q_ASSERT(inChild.parent ==
this);
166 inChild.parent =
nullptr;
167 if (inChild.type != QSSGRenderNode::Type::Root)
168 inChild.rootNodeRef =
nullptr;
170 children.remove(inChild);
171 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
172 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
173 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
176void QSSGRenderNode::removeFromGraph()
179 parent->removeChild(*
this);
182 for (
auto it = children.begin(), end = children.end(); it != end;) {
183 auto &removedChild = *it++;
184 children.remove(removedChild);
185 removedChild.parent =
nullptr;
186 removedChild.rootNodeRef =
nullptr;
190QSSGBounds3 QSSGRenderNode::getBounds(QSSGBufferManager &inManager,
191 bool inIncludeChildren)
const
194 if (inIncludeChildren)
195 retval = getChildBounds(inManager);
197 if (type == QSSGRenderGraphObject::Type::Model) {
198 auto model =
static_cast<
const QSSGRenderModel *>(
this);
199 retval.include(inManager.getModelBounds(model));
204QSSGBounds3 QSSGRenderNode::getChildBounds(QSSGBufferManager &inManager)
const
207 QSSGBounds3 childBounds;
208 for (
auto &child : children) {
209 childBounds = child.getBounds(inManager);
210 if (!childBounds.isEmpty()) {
212 childBounds.transform(child.localTransform);
213 retval.include(childBounds);
219QVector3D QSSGRenderNode::getDirection(
const QMatrix4x4 &globalTransform)
221 const float *dataPtr(globalTransform.data());
222 QVector3D retval(dataPtr[8], dataPtr[9], dataPtr[10]);
227QVector3D QSSGRenderNode::getScalingCorrectDirection(
const QMatrix4x4 &globalTransform)
229 QMatrix3x3 theDirMatrix = globalTransform.normalMatrix();
230 QVector3D theOriginalDir(0, 0, -1);
231 QVector3D retval = QSSGUtils::mat33::transform(theDirMatrix, theOriginalDir);
237void QSSGRenderNode::calculateMVP(
const QMatrix4x4 &globalTransform,
const QMatrix4x4 &inViewProjection, QMatrix4x4 &outMVP)
239 outMVP = inViewProjection * globalTransform;
247void QSSGRenderNode::calculateMVPAndNormalMatrix(
const QMatrix4x4 &globalTransform,
248 const QMatrix4x4 &inViewProjection,
250 QMatrix3x3 &outNormalMatrix)
252 calculateMVP(globalTransform, inViewProjection, outMVP);
253 calculateNormalMatrix(globalTransform, outNormalMatrix);