1 | // Copyright (C) 2023 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "scatterinstancing_p.h" |
5 | |
6 | ScatterInstancing::ScatterInstancing() |
7 | { |
8 | |
9 | } |
10 | |
11 | QByteArray ScatterInstancing::getInstanceBuffer(int *instanceCount) { |
12 | if (m_dirty) { |
13 | m_instanceData.resize(size: 0); |
14 | int instanceNumber = 0; |
15 | |
16 | for (int i = 0; i < m_dataArray.size(); ++i) { |
17 | auto item = m_dataArray.at(i); |
18 | float x = item.position.x(); |
19 | float y = item.position.y(); |
20 | float z = item.position.z(); |
21 | QVector4D customData{}; |
22 | if (m_rangeGradient) |
23 | customData.setX(m_customData.at(i)); |
24 | |
25 | if (item.hide) { |
26 | // Setting the scale to zero breaks instanced picking. |
27 | item.scale = {0.001f, 0.001f, 0.001f}; |
28 | } |
29 | auto entry = calculateTableEntryFromQuaternion(position: {x,y,z}, scale: item.scale, rotation: item.rotation, color: QColor(Qt::white), customData); |
30 | m_instanceData.append(s: reinterpret_cast<char *>(&entry), len: sizeof(entry)); |
31 | instanceNumber++; |
32 | } |
33 | m_instanceCount = instanceNumber; |
34 | m_dirty = false; |
35 | } |
36 | |
37 | if (instanceCount) |
38 | *instanceCount = m_instanceCount; |
39 | |
40 | return m_instanceData; |
41 | } |
42 | |
43 | bool ScatterInstancing::rangeGradient() const |
44 | { |
45 | return m_rangeGradient; |
46 | } |
47 | |
48 | void ScatterInstancing::setRangeGradient(bool newRangeGradient) |
49 | { |
50 | m_rangeGradient = newRangeGradient; |
51 | } |
52 | |
53 | QList<float> &ScatterInstancing::customData() |
54 | { |
55 | return m_customData; |
56 | } |
57 | |
58 | void ScatterInstancing::setCustomData(const QList<float> &newCustomData) |
59 | { |
60 | m_customData = newCustomData; |
61 | markDataDirty(); |
62 | } |
63 | |
64 | void ScatterInstancing::markDataDirty() |
65 | { |
66 | m_dirty = true; |
67 | markDirty(); |
68 | } |
69 | |
70 | const QList<DataItemHolder> &ScatterInstancing::dataArray() const |
71 | { |
72 | return m_dataArray; |
73 | } |
74 | |
75 | void ScatterInstancing::setDataArray(const QList<DataItemHolder> &newDataArray) |
76 | { |
77 | m_dataArray = newDataArray; |
78 | markDataDirty(); |
79 | } |
80 | |
81 | void ScatterInstancing::hideDataItem(int index) |
82 | { |
83 | m_dataArray[index].hide = true; |
84 | } |
85 | |
86 | void ScatterInstancing::resetVisibilty() |
87 | { |
88 | for (auto &dih : m_dataArray) |
89 | dih.hide = false; |
90 | markDataDirty(); |
91 | } |
92 |