29 localTransform = calculateTransformMatrix({}, initScale, {}, {});
35void QSSGRenderNode::markDirty(DirtyFlag dirtyFlag)
37 if ((flags & FlagT(dirtyFlag)) == 0) {
38 flags |= FlagT(dirtyFlag);
39 const bool markSubtreeDirty = ((FlagT(dirtyFlag) & FlagT(DirtyFlag::GlobalValuesDirty)) != 0);
40 if (markSubtreeDirty) {
41 for (
auto &child : children)
42 child.markDirty(dirtyFlag);
52void QSSGRenderNode::setState(LocalState state,
bool on)
54 const bool changed = (getLocalState(state) != on);
56 flags = on ? (flags | FlagT(state)) : (flags & ~FlagT(state));
60 case QSSGRenderNode::LocalState::Active:
61 markDirty(DirtyFlag::ActiveDirty);
62 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
63 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
65 case QSSGRenderNode::LocalState::Pickable:
66 markDirty(DirtyFlag::PickableDirty);
73QMatrix4x4 QSSGRenderNode::calculateTransformMatrix(QVector3D position, QVector3D scale, QVector3D pivot, QQuaternion rotation)
78 auto offset = (-pivot * scale);
81 transform(0, 0) = scale[0];
82 transform(1, 1) = scale[1];
83 transform(2, 2) = scale[2];
86 transform(0, 3) = offset[0];
87 transform(1, 3) = offset[1];
88 transform(2, 3) = offset[2];
91 transform = QMatrix4x4{rotation.toRotationMatrix()} * transform;
94 transform(0, 3) += position[0];
95 transform(1, 3) += position[1];
96 transform(2, 3) += position[2];
101void QSSGRenderNode::addChild(QSSGRenderNode &inChild)
103 QSSG_ASSERT_X(inChild.parent !=
this,
"Already a child of this node!",
return);
107 if (type != QSSGRenderNode::Type::Layer && type != QSSGRenderNode::Type::ImportScene) {
108 if (inChild.parent && inChild.parent !=
this)
109 inChild.parent->removeChild(inChild);
110 inChild.parent =
this;
113 QSSG_ASSERT_X(type != QSSGRenderNode::Type::Root || inChild.type == QSSGRenderNode::Type::Layer,
114 "Only layers can be added to the root!",
return);
116 if (inChild.type != QSSGRenderNode::Type::Root)
117 inChild.rootNodeRef = rootNodeRef;
119 children.push_back(inChild);
122 if (inChild.type != QSSGRenderNode::Type::Layer)
123 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
124 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
125 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
128void QSSGRenderNode::removeChild(QSSGRenderNode &inChild)
130 if (Q_UNLIKELY(type != QSSGRenderNode::Type::Layer && inChild.parent !=
this)) {
131 Q_ASSERT(inChild.parent ==
this);
135 inChild.parent =
nullptr;
136 if (inChild.type != QSSGRenderNode::Type::Root)
137 inChild.rootNodeRef =
nullptr;
139 children.remove(inChild);
140 if (QSSGRenderRoot *rootNode = QSSGRenderRoot::get(rootNodeRef))
141 rootNode->markDirty(QSSGRenderRoot::DirtyFlag::TreeDirty);
142 inChild.markDirty(DirtyFlag::GlobalValuesDirty);
158QSSGBounds3 QSSGRenderNode::getBounds(QSSGBufferManager &inManager,
159 bool inIncludeChildren)
const
162 if (inIncludeChildren)
163 retval = getChildBounds(inManager);
165 if (type == QSSGRenderGraphObject::Type::Model) {
166 auto model =
static_cast<
const QSSGRenderModel *>(
this);
167 retval.include(inManager.getModelBounds(model));
172QSSGBounds3 QSSGRenderNode::getChildBounds(QSSGBufferManager &inManager)
const
175 QSSGBounds3 childBounds;
176 for (
auto &child : children) {
177 childBounds = child.getBounds(inManager);
178 if (!childBounds.isEmpty()) {
180 childBounds.transform(child.localTransform);
181 retval.include(childBounds);
187QVector3D QSSGRenderNode::getDirection(
const QMatrix4x4 &globalTransform)
189 const float *dataPtr(globalTransform.data());
190 QVector3D retval(dataPtr[8], dataPtr[9], dataPtr[10]);
195QVector3D QSSGRenderNode::getScalingCorrectDirection(
const QMatrix4x4 &globalTransform)
197 QMatrix3x3 theDirMatrix = globalTransform.normalMatrix();
198 QVector3D theOriginalDir(0, 0, -1);
199 QVector3D retval = QSSGUtils::mat33::transform(theDirMatrix, theOriginalDir);
205void QSSGRenderNode::calculateMVP(
const QMatrix4x4 &globalTransform,
const QMatrix4x4 &inViewProjection, QMatrix4x4 &outMVP)
207 outMVP = inViewProjection * globalTransform;
215void QSSGRenderNode::calculateMVPAndNormalMatrix(
const QMatrix4x4 &globalTransform,
216 const QMatrix4x4 &inViewProjection,
218 QMatrix3x3 &outNormalMatrix)
220 calculateMVP(globalTransform, inViewProjection, outMVP);
221 calculateNormalMatrix(globalTransform, outNormalMatrix);