5#include <QtQuick3DRuntimeRender/private/qssgrenderinstancetable_p.h>
6#include <QtQuick3DUtils/private/qssgutils_p.h>
7#include <QXmlStreamReader>
8#include <QtQml/QQmlFile>
149 return d->m_instanceCountOverride;
155 return d->m_hasTransparency;
161 return d->m_depthSortingEnabled;
167 if (
index >=
int(
data.size() /
sizeof(InstanceTableEntry)))
179 const QVector3D col0{row0[0], row1[0], row2[0]};
180 const QVector3D col1{row0[1], row1[1], row2[1]};
181 const QVector3D col2{row0[2], row1[2], row2[2]};
182 const float scaleX = col0.
length();
183 const float scaleY = col1.length();
184 const float scaleZ = col2.length();
185 return QVector3D(scaleX, scaleY, scaleZ);
194 const float data3x3[3*3] {
195 col0[0], col1[0], col2[0],
196 col0[1], col1[1], col2[1],
197 col0[2], col1[2], col2[2],
240 return entry->getScale();
257 return entry->getRotation();
274 return entry->getColor();
291 return entry->instanceData;
301 d->m_instanceCountOverrideChanged =
true;
320 if (
d->m_depthSortingEnabled ==
enabled)
338 d->m_instanceDataChanged =
true;
349 d->m_instanceDataChanged =
true;
352 auto effectiveInstanceCount = [
d]() {
353 if (
d->m_instanceCountOverride >= 0)
354 return qMin(
d->m_instanceCount,
d->m_instanceCountOverride);
355 return d->m_instanceCount;
358 if (
d->m_instanceDataChanged) {
361 d->m_instanceDataChanged =
false;
362 }
else if (
d->m_instanceCountOverrideChanged) {
363 instanceTable->setInstanceCountOverride(effectiveInstanceCount());
365 d->m_instanceCountOverrideChanged =
false;
366 instanceTable->setHasTransparency(
d->m_hasTransparency);
367 instanceTable->setDepthSorting(
d->m_depthSortingEnabled);
388 auto linearColor = color::sRGBToLinear(
color);
454 auto linearColor = color::sRGBToLinear(
color);
514 generateInstanceData();
517 return m_instanceData;
525 qmlAppendInstanceListEntry,
526 qmlInstanceListEntriesCount,
527 qmlInstanceListEntryAt,
528 qmlClearInstanceListEntries);
540 return m_instances.
size();
543void QQuick3DInstanceList::onInstanceDestroyed(
QObject *
object)
546 handleInstanceChange();
551 if (instance ==
nullptr)
556 if (instance->parentItem() ==
nullptr)
560 self->handleInstanceChange();
578 for (
auto *instance :
self->m_instances) {
583 self->handleInstanceChange();
586void QQuick3DInstanceList::handleInstanceChange()
593void QQuick3DInstanceList::generateInstanceData()
599 m_instanceData.
resize(tableSize);
600 auto *
array =
reinterpret_cast<InstanceTableEntry*
>(m_instanceData.
data());
602 const auto *inst = m_instances.
at(
i);
603 if (inst->m_useEulerRotation)
604 array[
i] =
calculateTableEntry(inst->position(), inst->scale(), inst->eulerRotation(), inst->color(), inst->customData());
651 if (m_scale ==
scale)
670 m_useEulerRotation =
true;
682 if (!m_useEulerRotation && m_rotation ==
rotation)
686 m_useEulerRotation =
false;
698 if (m_color ==
color)
801 char magic[4] = {
'Q',
't',
'I',
'R' };
831 auto binaryFile = std::make_unique<QFile>(filename);
836 const quint64 fileSize = binaryFile->size();
838 qWarning() <<
"data file too small";
841 const char *
data =
reinterpret_cast<const char *
>(binaryFile->map(0, fileSize));
857 m_dataFile = binaryFile.release();
872 QXmlStreamReader reader(&
f);
889 while (reader.readNextStartElement()) {
893 while (reader.readNextStartElement()) {
899 bool useQuaternion =
false;
902 for (
auto &attr : reader.attributes()) {
912 quaternion =
QQuaternion(toVector4D(attr.value()));
913 useQuaternion =
true;
915 custom = toVector4D(attr.value());
923 reader.skipCurrentElement();
926 reader.skipCurrentElement();
931 m_instanceCount = instances;
932 m_instanceData = instanceData;
942 return success ? m_instanceCount : -1;
947 return m_instanceCount;
950bool QQuick3DFileInstancing::loadFromFile(
const QUrl &
source)
961 int oldCount = m_instanceCount;
963 if (m_instanceCount != oldCount)
983 if (m_source == newSource)
985 m_source = newSource;
995 if (!loadFromFile(m_source)) {
1005 return m_instanceData;
1010 "QSSGRenderInstanceTableEntry and QQuick3DInstancing::InstanceTableEntry do not match");
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
void resize(qsizetype size)
Sets the size of the byte array to size bytes.
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
The QColor class provides colors based on RGB, HSV or CMYK values.
static QColor fromString(QAnyStringView name) noexcept
static QColor fromRgbF(float r, float g, float b, float a=1.0)
Static convenience function that returns a QColor constructed from the RGB color values,...
\inmodule QtCore \reentrant
qsizetype size() const noexcept
const_reference at(qsizetype i) const noexcept
qsizetype removeAll(const AT &t)
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
The QQmlContext class defines a context within a QML engine.
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to QFile.
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
The QQuaternion class represents a quaternion consisting of a vector and scalar.
static QQuaternion fromEulerAngles(const QVector3D &eulerAngles)
static QQuaternion fromRotationMatrix(const QMatrix3x3 &rot3x3)
QQuaternion normalized() const
Returns the normalized unit form of this quaternion.
QMatrix3x3 toRotationMatrix() const
void setSource(const QUrl &newSource)
void instanceCountChanged()
~QQuick3DFileInstancing() override
int writeToBinaryFile(QIODevice *out)
QByteArray getInstanceBuffer(int *instanceCount) override
Implement this function to return the contents of the instance table.
bool loadFromBinaryFile(const QString &filename)
bool loadFromXmlFile(const QString &filename)
QQuick3DFileInstancing(QQuick3DObject *parent=nullptr)
QQuick3DInstanceListEntry(QQuick3DObject *parent=nullptr)
\qmltype InstanceListEntry \inherits Object3D \inqmlmodule QtQuick3D
void setRotation(QQuaternion rotation)
\qmlproperty quaternion QtQuick3D::InstanceListEntry::rotation
void setPosition(QVector3D position)
\qmlproperty vector3d QtQuick3D::InstanceListEntry::position
void setCustomData(QVector4D customData)
\qmlproperty vector4d QtQuick3D::InstanceListEntry::customData
void eulerRotationChanged()
void setScale(QVector3D scale)
\qmlproperty vector3d QtQuick3D::InstanceListEntry::scale
void setEulerRotation(QVector3D eulerRotation)
\qmlproperty vector3d QtQuick3D::InstanceListEntry::eulerRotation
void setColor(QColor color)
\qmlproperty vector3d QtQuick3D::InstanceListEntry::color
void instanceCountChanged()
QQuick3DInstanceList(QQuick3DObject *parent=nullptr)
\qmltype InstanceList \inherits Instancing \inqmlmodule QtQuick3D
QByteArray getInstanceBuffer(int *instanceCount) override
Implement this function to return the contents of the instance table.
~QQuick3DInstanceList() override
QQmlListProperty< QQuick3DInstanceListEntry > instances
QQuick3DInstancingPrivate()
\inmodule QtQuick3D \inherits QQuick3DObject
void depthSortingEnabledChanged()
void hasTransparencyChanged()
static InstanceTableEntry calculateTableEntryFromQuaternion(const QVector3D &position, const QVector3D &scale, const QQuaternion &rotation, const QColor &color, const QVector4D &customData={})
Converts the position scale rotation color and customData to the instance table format expected by th...
void setHasTransparency(bool hasTransparency)
virtual QByteArray getInstanceBuffer(int *instanceCount)=0
Implement this function to return the contents of the instance table.
QSSGRenderGraphObject * updateSpatialNode(QSSGRenderGraphObject *node) override
void markDirty()
Mark that the instance data has changed and must be uploaded again.
void setDepthSortingEnabled(bool enabled)
QByteArray instanceBuffer(int *instanceCount)
void instanceTableChanged()
void instanceCountOverrideChanged()
~QQuick3DInstancing() override
void setInstanceCountOverride(int instanceCountOverride)
QQuick3DInstancing(QQuick3DObject *parent=nullptr)
bool depthSortingEnabled
\qmlproperty bool Instancing::depthSortingEnabled
bool hasTransparency
\qmlproperty bool Instancing::hasTransparency
int instanceCountOverride
static InstanceTableEntry calculateTableEntry(const QVector3D &position, const QVector3D &scale, const QVector3D &eulerRotation, const QColor &color, const QVector4D &customData={})
Converts the position scale eulerRotation color and customData to the instance table format expected ...
\qmltype Object3D \inqmlmodule QtQuick3D \instantiates QQuick3DObject \inherits QtObject
QQuick3DObject * parent
\qmlproperty Object3D QtQuick3D::Object3D::parent This property holds the parent of the Object3D in a...
void setParentItem(QQuick3DObject *parentItem)
virtual QSSGRenderGraphObject * updateSpatialNode(QSSGRenderGraphObject *node)
virtual void markAllDirty()
\macro QT_RESTRICTED_CAST_FROM_ASCII
void clear()
Clears the contents of the string and makes it null.
qsizetype size() const
Returns the number of characters in this string.
void push_back(QChar c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
QByteArray toLocal8Bit() const &
qsizetype count(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
The QVector3D class represents a vector or vertex in 3D space.
float length() const noexcept
Returns the length of the vector from the origin.
QVector3D normalized() const noexcept
Returns the normalized unit vector form of this vector.
The QVector4D class represents a vector or vertex in 4D space.
Combined button and popup list for selecting options.
static QString header(const QString &name)
static const qint64 headerSize
static QVector3D toVector3D(const QDoubleVector3D &in)
constexpr const T & qMin(const T &a, const T &b)
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLenum GLenum GLsizei count
GLenum GLenum GLsizei const GLuint GLboolean enabled
const void GLsizei GLsizei stride
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLsizei GLsizei GLchar * source
GLenum GLenum GLenum GLenum GLenum scale
QQmlContext * qmlContext(const QObject *obj)
static QQuick3DInstancing::InstanceTableEntry calculate(const QVector3D &position, const QVector3D &scale, const QVector3D &eulerRotation, const QColor &color, const QVector4D &customData)
static constexpr quint16 currentMajorVersion
\qmltype FileInstancing \inherits Instancing \inqmlmodule QtQuick3D
static bool writeInstanceTable(QIODevice *out, const QByteArray &instanceData, int instanceCount)
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
QLatin1StringView QLatin1String
#define QStringLiteral(str)
unsigned long long quint64
QTextStream out(stdout)
[7]
myObject disconnect()
[26]
QVector3D getScale() const
QQuaternion getRotation() const
QVector3D getPosition() const
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent