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);
64 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
65 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
67 case QSSGRenderNode::LocalState::Pickable:
68 markDirty(DirtyFlag::PickableDirty);
70 case QSSGRenderNode::LocalState::Imported:
71 markDirty(DirtyFlag::ImportDirty);
78QMatrix4x4 QSSGRenderNode::calculateTransformMatrix(QVector3D position, QVector3D scale, QVector3D pivot, QQuaternion rotation)
83 auto offset = (-pivot * scale);
86 transform(0, 0) = scale[0];
87 transform(1, 1) = scale[1];
88 transform(2, 2) = scale[2];
91 transform(0, 3) = offset[0];
92 transform(1, 3) = offset[1];
93 transform(2, 3) = offset[2];
96 transform = QMatrix4x4{rotation.toRotationMatrix()} * transform;
99 transform(0, 3) += position[0];
100 transform(1, 3) += position[1];
101 transform(2, 3) += position[2];
106void QSSGRenderNode::addChild(QSSGRenderNode &inChild)
108 QSSG_ASSERT_X(inChild.parent !=
this,
"Already a child of this node!",
return);
112 if (type != QSSGRenderNode::Type::Layer && type != QSSGRenderNode::Type::ImportScene) {
113 if (inChild.parent && inChild.parent !=
this)
114 inChild.parent->removeChild(inChild);
115 inChild.parent =
this;
118 QSSG_ASSERT_X(type != QSSGRenderNode::Type::Root || inChild.type == QSSGRenderNode::Type::Layer,
119 "Only layers can be added to the root!",
return);
121 if (inChild.type != QSSGRenderNode::Type::Root)
122 inChild.rootNodeRef = rootNodeRef;
124 children.push_back(inChild);
127 if (inChild.type != QSSGRenderNode::Type::Layer)
128 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
129 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
130 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
133void QSSGRenderNode::removeChild(QSSGRenderNode &inChild)
135 if (Q_UNLIKELY(type != QSSGRenderNode::Type::Layer && inChild.parent !=
this)) {
136 Q_ASSERT(inChild.parent ==
this);
140 inChild.parent =
nullptr;
141 if (inChild.type != QSSGRenderNode::Type::Root)
142 inChild.rootNodeRef =
nullptr;
144 children.remove(inChild);
145 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
146 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
147 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
163QSSGBounds3 QSSGRenderNode::getBounds(QSSGBufferManager &inManager,
164 bool inIncludeChildren)
const
167 if (inIncludeChildren)
168 retval = getChildBounds(inManager);
170 if (type == QSSGRenderGraphObject::Type::Model) {
171 auto model =
static_cast<
const QSSGRenderModel *>(
this);
172 retval.include(inManager.getModelBounds(model));
177QSSGBounds3 QSSGRenderNode::getChildBounds(QSSGBufferManager &inManager)
const
180 QSSGBounds3 childBounds;
181 for (
auto &child : children) {
182 childBounds = child.getBounds(inManager);
183 if (!childBounds.isEmpty()) {
185 childBounds.transform(child.localTransform);
186 retval.include(childBounds);
192QVector3D QSSGRenderNode::getDirection(
const QMatrix4x4 &globalTransform)
194 const float *dataPtr(globalTransform.data());
195 QVector3D retval(dataPtr[8], dataPtr[9], dataPtr[10]);
200QVector3D QSSGRenderNode::getScalingCorrectDirection(
const QMatrix4x4 &globalTransform)
202 QMatrix3x3 theDirMatrix = globalTransform.normalMatrix();
203 QVector3D theOriginalDir(0, 0, -1);
204 QVector3D retval = QSSGUtils::mat33::transform(theDirMatrix, theOriginalDir);
210void QSSGRenderNode::calculateMVP(
const QMatrix4x4 &globalTransform,
const QMatrix4x4 &inViewProjection, QMatrix4x4 &outMVP)
212 outMVP = inViewProjection * globalTransform;
220void QSSGRenderNode::calculateMVPAndNormalMatrix(
const QMatrix4x4 &globalTransform,
221 const QMatrix4x4 &inViewProjection,
223 QMatrix3x3 &outNormalMatrix)
225 calculateMVP(globalTransform, inViewProjection, outMVP);
226 calculateNormalMatrix(globalTransform, outNormalMatrix);