1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qgeometry.h" |
5 | #include "qgeometryfactory_p.h" |
6 | #include "qgeometry_p.h" |
7 | #include <private/qnode_p.h> |
8 | #include <Qt3DCore/qattribute.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | namespace Qt3DCore { |
13 | |
14 | QGeometryFactory::~QGeometryFactory() |
15 | { |
16 | } |
17 | |
18 | QGeometryPrivate::QGeometryPrivate() |
19 | : QNodePrivate(), |
20 | m_boundingVolumePositionAttribute(nullptr) |
21 | { |
22 | } |
23 | |
24 | QGeometryPrivate::~QGeometryPrivate() |
25 | { |
26 | } |
27 | |
28 | QGeometryPrivate *QGeometryPrivate::get(QGeometry *q) |
29 | { |
30 | return q->d_func(); |
31 | } |
32 | |
33 | void QGeometryPrivate::setScene(QScene *scene) |
34 | { |
35 | QNodePrivate::setScene(scene); |
36 | if (scene) |
37 | scene->markDirty(changes: QScene::GeometryDirty); |
38 | } |
39 | |
40 | void QGeometryPrivate::update() |
41 | { |
42 | if (!m_blockNotifications) { |
43 | m_dirty = true; |
44 | markDirty(changes: QScene::GeometryDirty); |
45 | } |
46 | QNodePrivate::update(); |
47 | } |
48 | |
49 | void QGeometryPrivate::setExtent(const QVector3D &minExtent, const QVector3D &maxExtent) |
50 | { |
51 | Q_Q(QGeometry); |
52 | if (m_minExtent != minExtent) { |
53 | m_minExtent = minExtent; |
54 | const auto wasBlocked = q->blockNotifications(block: true); |
55 | emit q->minExtentChanged(minExtent); |
56 | q->blockNotifications(block: wasBlocked); |
57 | } |
58 | |
59 | if (m_maxExtent != maxExtent) { |
60 | m_maxExtent = maxExtent; |
61 | const auto wasBlocked = q->blockNotifications(block: true); |
62 | emit q->maxExtentChanged(maxExtent); |
63 | q->blockNotifications(block: wasBlocked); |
64 | } |
65 | } |
66 | |
67 | /*! |
68 | \qmltype Geometry |
69 | \instantiates Qt3DCore::QGeometry |
70 | \inqmlmodule Qt3D.Core |
71 | \inherits Node |
72 | \since 5.7 |
73 | \brief Encapsulates geometry. |
74 | |
75 | A Geometry type is used to group a list of Attribute objects together |
76 | to form a geometric shape Qt3D is able to render using GeometryRenderer. |
77 | Special attribute can be set in order to calculate bounding volume of the shape. |
78 | */ |
79 | |
80 | /*! |
81 | \class Qt3DCore::QGeometry |
82 | \inmodule Qt3DCore |
83 | \since 5.7 |
84 | \brief Encapsulates geometry. |
85 | |
86 | A Qt3DCore::QGeometry class is used to group a list of Qt3DCore::QAttribute |
87 | objects together to form a geometric shape Qt3D is able to render using |
88 | Qt3DCore::QGeometryRenderer. Special attribute can be set in order to calculate |
89 | bounding volume of the shape. |
90 | */ |
91 | |
92 | /*! |
93 | \qmlproperty Attribute Geometry::boundingVolumePositionAttribute |
94 | |
95 | Holds the attribute used to compute the bounding volume. The bounding volume is used internally |
96 | for picking and view frustum culling. |
97 | |
98 | If unspecified, the system will look for the attribute using the name returned by |
99 | QAttribute::defaultPositionAttributeName. |
100 | |
101 | \sa Attribute |
102 | */ |
103 | /*! |
104 | \qmlproperty list<Attribute> Geometry::attributes |
105 | |
106 | Holds the list of attributes the geometry comprises of. |
107 | */ |
108 | |
109 | /*! |
110 | \property QGeometry::boundingVolumePositionAttribute |
111 | |
112 | Holds the attribute used to compute the bounding volume. The bounding volume is used internally |
113 | for picking and view frustum culling. |
114 | |
115 | If unspecified, the system will look for the attribute using the name returned by |
116 | QAttribute::defaultPositionAttributeName. |
117 | |
118 | \sa Qt3DCore::QAttribute |
119 | */ |
120 | |
121 | |
122 | /*! |
123 | Constructs a new QGeometry with \a parent. |
124 | */ |
125 | QGeometry::QGeometry(QNode *parent) |
126 | : QGeometry(*new QGeometryPrivate(), parent) {} |
127 | |
128 | /*! |
129 | \internal |
130 | */ |
131 | QGeometry::~QGeometry() |
132 | { |
133 | } |
134 | |
135 | /*! |
136 | \internal |
137 | */ |
138 | QGeometry::QGeometry(QGeometryPrivate &dd, QNode *parent) |
139 | : QNode(dd, parent) |
140 | { |
141 | } |
142 | |
143 | /*! |
144 | \fn void Qt3DCore::QGeometry::addAttribute(Qt3DCore::QAttribute *attribute) |
145 | Adds an \a attribute to this geometry. |
146 | */ |
147 | void QGeometry::addAttribute(QAttribute *attribute) |
148 | { |
149 | Q_ASSERT(attribute); |
150 | Q_D(QGeometry); |
151 | if (!d->m_attributes.contains(t: attribute)) { |
152 | d->m_attributes.append(t: attribute); |
153 | |
154 | // Ensures proper bookkeeping |
155 | d->registerDestructionHelper(node: attribute, func: &QGeometry::removeAttribute, d->m_attributes); |
156 | |
157 | // We need to add it as a child of the current node if it has been declared inline |
158 | // Or not previously added as a child of the current node so that |
159 | // 1) The backend gets notified about it's creation |
160 | // 2) When the current node is destroyed, it gets destroyed as well |
161 | if (!attribute->parent()) |
162 | attribute->setParent(this); |
163 | |
164 | d->update(); |
165 | } |
166 | } |
167 | |
168 | /*! |
169 | \fn void Qt3DCore::QGeometry::removeAttribute(Qt3DCore::QAttribute *attribute) |
170 | Removes the given \a attribute from this geometry. |
171 | */ |
172 | void QGeometry::removeAttribute(QAttribute *attribute) |
173 | { |
174 | Q_ASSERT(attribute); |
175 | Q_D(QGeometry); |
176 | if (!d->m_attributes.removeOne(t: attribute)) |
177 | return; |
178 | // Remove bookkeeping connection |
179 | d->unregisterDestructionHelper(node: attribute); |
180 | d->update(); |
181 | } |
182 | |
183 | void QGeometry::setBoundingVolumePositionAttribute(QAttribute *boundingVolumePositionAttribute) |
184 | { |
185 | Q_D(QGeometry); |
186 | if (d->m_boundingVolumePositionAttribute != boundingVolumePositionAttribute) { |
187 | d->m_boundingVolumePositionAttribute = boundingVolumePositionAttribute; |
188 | emit boundingVolumePositionAttributeChanged(boundingVolumePositionAttribute); |
189 | } |
190 | } |
191 | |
192 | QAttribute *QGeometry::boundingVolumePositionAttribute() const |
193 | { |
194 | Q_D(const QGeometry); |
195 | return d->m_boundingVolumePositionAttribute; |
196 | } |
197 | |
198 | /*! |
199 | \qmlproperty vector3d Geometry::minExtent |
200 | |
201 | Holds the vertex with the lowest x, y, z position values. |
202 | */ |
203 | |
204 | /*! |
205 | \property QGeometry::minExtent |
206 | |
207 | Holds the vertex with the lowest x, y, z position values. |
208 | */ |
209 | QVector3D QGeometry::minExtent() const |
210 | { |
211 | Q_D(const QGeometry); |
212 | return d->m_minExtent; |
213 | } |
214 | |
215 | /*! |
216 | \qmlproperty vector3d Geometry::maxExtent |
217 | |
218 | Holds the vertex with the highest x, y, z position values. |
219 | */ |
220 | |
221 | /*! |
222 | \property QGeometry::maxExtent |
223 | |
224 | Holds the vertex with the highest x, y, z position values. |
225 | */ |
226 | QVector3D QGeometry::maxExtent() const |
227 | { |
228 | Q_D(const QGeometry); |
229 | return d->m_maxExtent; |
230 | } |
231 | |
232 | /*! |
233 | Returns the list of attributes in this geometry. |
234 | */ |
235 | QList<QAttribute *> QGeometry::attributes() const |
236 | { |
237 | Q_D(const QGeometry); |
238 | return d->m_attributes; |
239 | } |
240 | |
241 | } // namespace Qt3DCore |
242 | |
243 | QT_END_NAMESPACE |
244 | |
245 | #include "moc_qgeometry.cpp" |
246 | |