Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dgeometry.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
8#include <QtQuick3DUtils/private/qssgutils_p.h>
9
10/*!
11 \qmltype Geometry
12 \inherits Object3D
13 \inqmlmodule QtQuick3D
14 \nativetype QQuick3DGeometry
15 \brief Base type for custom geometry.
16
17 Custom geometry allows using application-generated vertex and index data,
18 that can possibly change dynamically as well. To use custom geometry, do
19 not assign a \c{.mesh} file as the \l{Model::source}{source} to a Model.
20 Instead, set its \l{Model::geometry}{geometry} property to reference a
21 Geometry object.
22
23 A typical way of implementing custom geometry is by creating a
24 \l QQuick3DGeometry subclass in C++ and registering the new type for use
25 with QML.
26
27 It is also possible to use the built-in custom geometry provider
28 \l GridGeometry in the \c Helpers module. The following is an example of
29 \l GridGeometry. Any application-provided Geometry subclass can be taken into
30 use in the same manner.
31
32 \code
33 import QtQuick3D.Helpers
34
35 Model {
36 geometry: GridGeometry {
37 }
38 materials: [
39 DefaultMaterial {
40 diffuseColor: "white"
41 lighting: DefaultMaterial.NoLighting
42 }
43 ]
44 }
45 \endcode
46
47 \sa {Qt Quick 3D - Custom Geometry Example}, Model, QQuick3DGeometry
48*/
49
50/*!
51 \class QQuick3DGeometry
52 \inmodule QtQuick3D
53 \inherits QQuick3DObject
54 \since 5.15
55 \brief Base class for defining custom geometry.
56
57 The QQuick3DGeometry can be used to specify custom geometry for a Model in
58 the Qt Quick 3D scene.
59
60 While not strictly required, the typical usage is to inherit from this
61 class. The subclass is then exposed to QML by registering it to the type
62 system. The \l{Model::geometry}{geometry} property of a Model can then be
63 set to reference an instance of the registered type.
64
65 The high-level structure of such a class is typically similar to the following:
66
67 \code
68 class CustomGeometry : public QQuick3DGeometry
69 {
70 public:
71 CustomGeometry() { rebuildGeometry(); }
72
73 void setSomething() {
74 // Change relevant internal data.
75 // ...
76
77 // Then rebuild the vertex and index data and pass it to QQuick3DGeometry.
78 rebuildGeometry();
79
80 // Finally, trigger an update. This is relevant in case nothing else
81 // is changing in the scene; this way we make sure a new frame will
82 // be rendered.
83 update();
84 }
85
86 private:
87 void rebuildGeometry()
88 {
89 QByteArray vertices;
90 QByteArray indices;
91 ...
92 setPrimitiveType(Lines);
93 setVertexBuffer(vertices);
94 setIndexBuffer(indices);
95 setStride(3 * sizeof(float)); // e.g. when having 3 components per vertex
96 setBounds(...); // minimum and maximum extents, for picking
97 addAttribute(PositionSemantic, 0, F32Type);
98 ...
99 }
100 };
101 \endcode
102
103 This class can then be registered as a QML type and used with \l {QtQuick3D::Model}{Model}.
104
105 In Qt 5 type registration happened with qmlRegisterType:
106 \code
107 qmlRegisterType<CustomGeometry>("Example", 1, 0, "CustomGeometry");
108 \endcode
109
110 In Qt 6 the default approach is to use automatic registration with the help
111 of the build system. Instead of calling qmlRegisterType, the \c{.pro} file
112 can now contain:
113
114 \code
115 CONFIG += qmltypes
116 QML_IMPORT_NAME = Example
117 QML_IMPORT_MAJOR_VERSION = 1
118 \endcode
119
120 With CMake, automatic registration is the default behavior, so no special
121 settings are needed beyond basic QML module setup:
122 \code
123 qt_add_qml_module(application
124 URI Example
125 VERSION 1.0
126 )
127 \endcode
128
129 The class implementation should add QML_NAMED_ELEMENT:
130
131 \code
132 class CustomGeometry : public QQuick3DGeometry
133 {
134 Q_OBJECT
135 QML_NAMED_ELEMENT(CustomGeometry)
136 ...
137 };
138 \endcode
139
140 The QML code can then use the custom type:
141
142 \code
143 import Example 1.0
144
145 Model {
146 id: customModel
147 geometry: CustomGeometry {
148 }
149 }
150 \endcode
151
152 At minimum, a custom geometry should have the following specified:
153
154 \list
155 \li vertex data,
156 \li vertex stride,
157 \li primitive type,
158 \li an attribute with PositionSemantic.
159 \endlist
160
161 These are sufficient to render the mesh. For indexed drawing, the index
162 buffer data and an attribute with IndexSemantic needs to be specified as
163 well. In order to support picking (input), the class must specify the bounding volume using setBounds().
164 For proper lighting, an attribute with NormalSemantic is needed. When the
165 material uses texturing, at least one set of UV coordinates must be
166 provided and described in an TexCoord0Semantic or TexCoord1Semantic attribute. Some materials may
167 require tangents and binormals as well.
168
169 As a concrete, minimal example, the following class would provide geometry
170 for a single triangle:
171
172 \code
173 class ExampleGeometry : public QQuick3DGeometry
174 {
175 Q_OBJECT
176 QML_NAMED_ELEMENT(ExampleGeometry)
177
178 public:
179 ExampleGeometry();
180
181 private:
182 void updateData();
183 };
184
185 ExampleGeometry::ExampleGeometry()
186 {
187 updateData();
188 }
189
190 void ExampleGeometry::updateData()
191 {
192 QByteArray v;
193 v.resize(3 * 3 * sizeof(float));
194 float *p = reinterpret_cast<float *>(v.data());
195
196 // a triangle, front face = counter-clockwise
197 *p++ = -1.0f; *p++ = -1.0f; *p++ = 0.0f;
198 *p++ = 1.0f; *p++ = -1.0f; *p++ = 0.0f;
199 *p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f;
200
201 setVertexData(v);
202 setStride(3 * sizeof(float));
203
204 setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles);
205
206 addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
207 0,
208 QQuick3DGeometry::Attribute::F32Type);
209 }
210 \endcode
211
212 Depending on the lighting in the scene, the result of referencing this
213 geometry from a Model:
214
215 \image customgeometry.jpg
216 {Black triangle with gray background showing custom geometry}
217
218 \note Vertex data is expected to follow OpenGL conventions. This means the
219 data must be provided with the assumption that the Y axis is pointing up in
220 the normalized device coordinate system, and that front faces have a
221 counter clockwise winding.
222
223 \sa Model, Geometry
224*/
225
226QT_BEGIN_NAMESPACE
227
228QQuick3DGeometryPrivate::QQuick3DGeometryPrivate()
229 : QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::Geometry)
230{
231
232}
233
234QQuick3DGeometryPrivate::QQuick3DGeometryPrivate(Flags flags)
235 : QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::Geometry, flags)
236{
237}
238
239QQuick3DGeometry::QQuick3DGeometry(QQuick3DObject *parent)
240 : QQuick3DObject(*new QQuick3DGeometryPrivate, parent)
241{
242
243}
244
245/*!
246 \internal
247*/
248QQuick3DGeometry::QQuick3DGeometry(QQuick3DGeometryPrivate &dd, QQuick3DObject *parent)
249 : QQuick3DObject(dd, parent)
250{
251}
252
253QQuick3DGeometry::~QQuick3DGeometry()
254{
255
256}
257
258/*!
259 Returns the vertex buffer data set by setVertexData.
260*/
261QByteArray QQuick3DGeometry::vertexData() const
262{
263 const Q_D(QQuick3DGeometry);
264 return d->m_vertexBuffer;
265}
266
267/*!
268 \since 6.6
269
270 Returns the target buffer data set by setTargetData.
271*/
272QByteArray QQuick3DGeometry::targetData() const
273{
274 const Q_D(QQuick3DGeometry);
275 return d->m_targetBuffer;
276}
277
278/*!
279 Returns the index buffer data.
280*/
281QByteArray QQuick3DGeometry::indexData() const
282{
283 const Q_D(QQuick3DGeometry);
284 return d->m_indexBuffer;
285}
286
287/*!
288 Returns the number of attributes defined for this geometry.
289
290 \sa attribute
291*/
292int QQuick3DGeometry::attributeCount() const
293{
294 const Q_D(QQuick3DGeometry);
295 return d->m_attributeCount;
296}
297
298/*!
299 Returns attribute definition number \a index
300
301 The attribute definitions are numbered from 0 to \c {attributeCount() - 1}
302*/
303QQuick3DGeometry::Attribute QQuick3DGeometry::attribute(int index) const
304{
305 const Q_D(QQuick3DGeometry);
306 return d->m_attributes[index];
307}
308
309/*!
310 \since 6.6
311 Returns the number of morph target attributes defined for this geometry.
312
313 \sa targetAttribute
314*/
315int QQuick3DGeometry::targetAttributeCount() const
316{
317 const Q_D(QQuick3DGeometry);
318 return d->m_targetAttributeCount;
319}
320
321/*!
322 \since 6.6
323
324 Returns morph target attribute definition number \a index
325
326 The attribute definitions are numbered from 0 to \c {attributeCount() - 1}
327*/
328QQuick3DGeometry::TargetAttribute QQuick3DGeometry::targetAttribute(int index) const
329{
330 const Q_D(QQuick3DGeometry);
331 return d->m_targetAttributes[index];
332}
333
334/*!
335 Returns the primitive type used when rendering. The default is \c Triangles.
336
337 \sa setPrimitiveType
338*/
339QQuick3DGeometry::PrimitiveType QQuick3DGeometry::primitiveType() const
340{
341 const Q_D(QQuick3DGeometry);
342 return d->m_primitiveType;
343}
344
345/*!
346 Returns the minimum coordinate of the bounding volume.
347
348 \sa setBounds
349*/
350QVector3D QQuick3DGeometry::boundsMin() const
351{
352 const Q_D(QQuick3DGeometry);
353 return d->m_min;
354}
355
356/*!
357 Returns the maximum coordinate of the bounding volume.
358
359 \sa setBounds
360*/
361QVector3D QQuick3DGeometry::boundsMax() const
362{
363 const Q_D(QQuick3DGeometry);
364 return d->m_max;
365}
366
367/*!
368 Returns the byte stride of the vertex buffer.
369
370 \sa setStride
371*/
372int QQuick3DGeometry::stride() const
373{
374 const Q_D(QQuick3DGeometry);
375 return d->m_stride;
376}
377
378void QQuick3DGeometry::markAllDirty()
379{
380 QQuick3DObject::markAllDirty();
381}
382
383/*!
384 Sets the vertex buffer \a data. The buffer should hold all the vertex data
385 packed in the array, as described by the attribute definitions. Note that
386 this does not include attributes with \c IndexSemantic, which belong in the
387 index buffer.
388
389 \sa addAttribute, setStride, setIndexData
390*/
391void QQuick3DGeometry::setVertexData(const QByteArray &data)
392{
393 Q_D(QQuick3DGeometry);
394 d->m_vertexBuffer = data;
395 d->m_geometryChanged = true;
396}
397
398/*!
399 \overload
400 Updates a subset of the vertex buffer. \a offset specifies the offset in
401 bytes, \a data specifies the size and the data.
402
403 This function will not resize the buffer. If \c {offset + data.size()} is
404 greater than the current size of the buffer, the overshooting data will
405 be ignored.
406
407 \note The partial update functions for vertex, index and morph target data
408 do not offer any guarantee on how such changes are implemented internally.
409 depending on the underlying implementation, even partial changes may lead
410 to updating the entire graphics resource.
411*/
412void QQuick3DGeometry::setVertexData(int offset, const QByteArray &data)
413{
414 Q_D(QQuick3DGeometry);
415 if (offset >= d->m_vertexBuffer.size())
416 return;
417
418 const size_t len = qMin(d->m_vertexBuffer.size() - offset, data.size());
419 memcpy(d->m_vertexBuffer.data() + offset, data.data(), len);
420
421 d->m_geometryChanged = true;
422}
423
424/*!
425 \since 6.6
426
427 Sets the morph target buffer \a data. The buffer should hold all the
428 morph target data.
429
430 \sa addTargetAttribute
431*/
432void QQuick3DGeometry::setTargetData(const QByteArray &data)
433{
434 Q_D(QQuick3DGeometry);
435 d->m_targetBuffer = data;
436 d->m_targetChanged = true;
437}
438
439/*!
440 \since 6.6
441 \overload
442
443 Updates a subset of the morph target buffer. \a offset specifies the offset in
444 bytes, \a data specifies the size and the data.
445
446 This function will not resize the buffer. If \c {offset + data.size()} is
447 greater than the current size of the buffer, the overshooting data will
448 be ignored.
449
450 \note The partial update functions for vertex, index and morph target data
451 do not offer any guarantee on how such changes are implemented internally.
452 Depending on the underlying implementation, even partial changes may lead
453 to updating the entire graphics resource.
454*/
455void QQuick3DGeometry::setTargetData(int offset, const QByteArray &data)
456{
457 Q_D(QQuick3DGeometry);
458 if (offset >= d->m_targetBuffer.size())
459 return;
460
461 const size_t len = qMin(d->m_targetBuffer.size() - offset, data.size());
462 memcpy(d->m_targetBuffer.data() + offset, data.data(), len);
463
464 d->m_targetChanged = true;
465}
466
467/*!
468 Sets the index buffer to \a data. To use indexed drawing, add an attribute with \c IndexSemantic
469
470 \sa addAttribute
471*/
472void QQuick3DGeometry::setIndexData(const QByteArray &data)
473{
474 Q_D(QQuick3DGeometry);
475 d->m_indexBuffer = data;
476 d->m_geometryChanged = true;
477}
478
479/*!
480 \overload
481 Updates a subset of the index buffer. \a offset specifies the offset in
482 bytes, \a data specifies the size and the data.
483
484 This function will not resize the buffer. If \c {offset + data.size()} is
485 greater than the current size of the buffer, the overshooting data will
486 be ignored.
487
488 \note The partial update functions for vertex, index and morph target data
489 do not offer any guarantee on how such changes are implemented internally.
490 Depending on the underlying implementation, even partial changes may lead
491 to updating the entire graphics resource.
492*/
493void QQuick3DGeometry::setIndexData(int offset, const QByteArray &data)
494{
495 Q_D(QQuick3DGeometry);
496 if (offset >= d->m_indexBuffer.size())
497 return;
498
499 const size_t len = qMin(d->m_indexBuffer.size() - offset, data.size());
500 memcpy(d->m_indexBuffer.data() + offset, data.data(), len);
501
502 d->m_geometryChanged = true;
503}
504
505/*!
506 Sets the stride of the vertex buffer to \a stride, measured in bytes.
507 This is the distance between two consecutive vertices in the buffer.
508
509 For example, a tightly packed, interleaved vertex buffer for a geometry using
510 \c PositionSemantic, \c IndexSemantic, and \c ColorSemantic will have a stride of
511 \c 28 (Seven floats in total: Three for position, four for color, and none for indexes,
512 which do not go in the vertex buffer.)
513
514 \note QQuick3DGeometry expects, and works only with, vertex data with an
515 interleaved attribute layout.
516
517 \sa addAttribute
518*/
519void QQuick3DGeometry::setStride(int stride)
520{
521 Q_D(QQuick3DGeometry);
522 if (stride != d->m_stride) {
523 d->m_stride = stride;
524 d->m_geometryChanged = true;
525 }
526}
527
528/*!
529 Sets the bounding volume of the geometry to the cube defined by the points \a min and \a max.
530 This is used for \l {View3D::pick}{picking}.
531*/
532void QQuick3DGeometry::setBounds(const QVector3D &min, const QVector3D &max)
533{
534 Q_D(QQuick3DGeometry);
535 d->m_max = max;
536 d->m_min = min;
537 d->m_geometryBoundsChanged = true;
538}
539
540/*!
541 Sets the primitive type used for rendering to \a type.
542
543 \value Points The primitives are points.
544 \value LineStrip The primitives are lines in a strip.
545 \value Lines The primitives are lines in a list.
546 \value TriangleStrip The primitives are triangles in a strip.
547 \value TriangleFan The primitives are triangles in a fan. Be aware that
548 triangle fans may not be supported at run time, depending on the underlying
549 graphics API.
550 \value Triangles The primitives are triangles in a list.
551
552 The initial value is \c Triangles.
553
554 \note Be aware that triangle fans (TriangleFan) may not be supported at run
555 time, depending on the underlying graphics API. For example, with Direct 3D
556 this topology will not be functional at all.
557
558 \note The point size for Points and the line width for Lines and LineStrip
559 are controlled by the \l{PrincipledMaterial::pointSize}{material}. Be aware
560 however that sizes other than 1 may not be supported at run time, depending
561 on the underlying graphics API.
562
563*/
564void QQuick3DGeometry::setPrimitiveType(PrimitiveType type)
565{
566 Q_D(QQuick3DGeometry);
567 if (d->m_primitiveType != type) {
568 d->m_primitiveType = type;
569 d->m_geometryChanged = true;
570 }
571}
572
573/*!
574 Adds vertex attribute description. Each attribute has a \a semantic, which specifies
575 the usage of the attribute and the number of components it has, an \a offset from the
576 beginning to the vertex to the attribute location inside a vertex and a \a componentType
577 specifying the datatype and size of the attribute.
578
579 The semantic can be one of the following:
580
581 \value PositionSemantic The attribute is a position. 3 components: \e x, \e y, and \e z
582 \value NormalSemantic The attribute is a normal vector. 3 components: \e x, \e y, and \e z
583 \value TexCoord0Semantic The attribute is a texture coordinate. 2 components: \e u and \e v
584 \value TexCoord1Semantic The attribute is a texture coordinate. 2 components: \e u and \e v
585 \value TangentSemantic The attribute is a tangent vector. 3 components: \e x, \e y, and \e z
586 \value BinormalSemantic The attribute is a binormal vector. 3 components: \e x, \e y, and \e z
587 \value JointSemantic The attribute is a joint index vector for \l {Vertex Skinning}{skinning}. 4 components: joint index 1-4
588 \value WeightSemantic The attribute is a weight vector for \l {Vertex Skinning}{skinning}. 4 components: joint weight 1-4
589 \value ColorSemantic The attribute is a vertex color vector. 4 components: \e r, \e g, \e b, and \e a
590 \value TargetPositionSemantic The attribute is a position for the first \l {Morphing Animation}{morph target}. 3 components: \e x, \e y, and \e z
591 \value TargetNormalSemantic The attribute is a normal vector for the first \l {Morphing Animation}{morph target}. 3 components: \e x, \e y, and \e z
592 \value TargetTangentSemantic The attribute is a tangent vector for the first \l {Morphing Animation}{morph target}. 3 components: \e x, \e y, and \e z
593 \value TargetBinormalSemantic The attribute is a binormal vector for the first \l {Morphing Animation}{morph target}. 3 components: \e x, \e y, and \e z
594
595 In addition, \a semantic can be \c IndexSemantic. In this case the attribute does not represent an entry in the vertex
596 buffer, but rather describes the index data in the index buffer. Since there is always just one index per vertex, \a offset
597 makes no sense for the index buffer, and should be left at zero.
598
599 The component type can be one of the following:
600
601 \value U16Type The index component type is unsigned 16-bit integer. Only
602 supported for \c IndexSemantic.
603
604 \value U32Type The attribute (or index component) is an unsigned 32-bit
605 integer.
606
607 \value I32Type The attribute is a signed 32-bit integer. Be aware that old
608 OpenGL versions (such as, 2.1 or OpenGL ES 2.0) may not support this data
609 type.
610
611 \value F32Type The attribute is a single-precision float.
612
613 \note The joint index data is typically \c I32Type. \c F32Type is also supported
614 in order to enable functioning with APIs, such as OpenGL ES 2.0, that do not
615 support integer vertex input attributes.
616
617 \note For index data (\c IndexSemantic) only U16Type and U32Type are
618 sensible and supported.
619
620 \note TargetXXXSemantics will be deprecated. \l addTargetAttribute can be used for the morph targets.
621 Now these semantics are just supported for backward compatibility. If they are mixed-used with
622 addTargetAttribute and setTargetData, the result cannot be quaranteed.
623*/
624void QQuick3DGeometry::addAttribute(Attribute::Semantic semantic, int offset,
625 Attribute::ComponentType componentType)
626{
627 Q_D(QQuick3DGeometry);
628 if (semantic != Attribute::TargetPositionSemantic
629 && semantic != Attribute::TargetNormalSemantic
630 && semantic != Attribute::TargetTangentSemantic
631 && semantic != Attribute::TargetBinormalSemantic) {
632 if (d->m_attributeCount >= QQuick3DGeometryPrivate::MAX_ATTRIBUTE_COUNT)
633 return;
634 d->m_attributes[d->m_attributeCount].semantic = semantic;
635 d->m_attributes[d->m_attributeCount].offset = offset;
636 d->m_attributes[d->m_attributeCount].componentType = componentType;
637 d->m_attributeCount++;
638 d->m_geometryChanged = true;
639 } else {
640 if (d->m_targetAttributeCount >= QQuick3DGeometryPrivate::MAX_TARGET_ATTRIBUTE_COUNT)
641 return;
642 d->m_targetAttributes[d->m_targetAttributeCount].targetId = 0;
643 d->m_targetAttributes[d->m_targetAttributeCount].attr.semantic = semantic;
644 d->m_targetAttributes[d->m_targetAttributeCount].attr.offset = offset;
645 // m_stride and m_vertexBuffer will be used for targetBuffer.
646 d->m_targetAttributeCount++;
647 d->m_targetChanged = true;
648 d->m_usesOldTargetSemantics = true;
649 }
650}
651
652/*!
653 \overload
654
655 Adds vertex attribute description. Each attribute has a semantic, which specifies
656 the usage of the attribute and the number of components it has, an offset from the
657 beginning to the vertex to the attribute location inside a vertex and a componentType
658 specifying the datatype and size of the attribute.
659*/
660void QQuick3DGeometry::addAttribute(const Attribute &attribute)
661{
662 Q_D(QQuick3DGeometry);
663 if (d->m_attributeCount >= QQuick3DGeometryPrivate::MAX_ATTRIBUTE_COUNT)
664 return;
665 d->m_attributes[d->m_attributeCount++] = attribute;
666 d->m_geometryChanged = true;
667}
668
669/*!
670 \since 6.6
671
672 Adds morph target attribute description. Each attribute has a \a targetId which the
673 attribute belongs to, a \a semantic, which specifies the usage of the attribute and the
674 number of components it has, an \a offset from the beginning to the vertex to the attribute
675 location inside a vertex, and a \a stride which is a byte size between the elements.
676
677 \note The targetId should be increased from 0 without skipping any number and all the
678 targets should have the same attributes.
679
680 \note The semantic is the same as the vertex attribute but IndexSemantic, JointSementic
681 and WeightSemantic are not allowed for target attributes.
682
683 \note The componentTypes of all the target attributes must be F32Type.
684
685 \note If the stride is not given or less than or equal to zero, the attribute is
686 considered to be tightly packed.
687
688 \sa addAttribute
689*/
690void QQuick3DGeometry::addTargetAttribute(quint32 targetId,
691 Attribute::Semantic semantic, int offset,
692 int stride)
693{
694 Q_D(QQuick3DGeometry);
695 if (d->m_targetAttributeCount >= QQuick3DGeometryPrivate::MAX_TARGET_ATTRIBUTE_COUNT)
696 return;
697 if (semantic == Attribute::IndexSemantic
698 || semantic == Attribute::JointSemantic
699 || semantic == Attribute::WeightSemantic)
700 return;
701 d->m_targetAttributes[d->m_targetAttributeCount].targetId = targetId;
702 d->m_targetAttributes[d->m_targetAttributeCount].attr.semantic = semantic;
703 d->m_targetAttributes[d->m_targetAttributeCount].attr.offset = offset;
704 d->m_targetAttributes[d->m_targetAttributeCount].stride = stride;
705 d->m_targetAttributeCount++;
706 d->m_targetChanged = true;
707}
708
709/*!
710 \since 6.6
711 \overload
712
713 Adds morph target attribute description. Each attribute has a targetId which the
714 attribute belongs to, a semantic, which specifies the usage of the attribute and the
715 number of components it has, an offset from the beginning to the vertex to the attribute
716 location inside a vertex, and a stride which is a byte size between the elements.
717*/
718void QQuick3DGeometry::addTargetAttribute(const TargetAttribute &attribute)
719{
720 Q_D(QQuick3DGeometry);
721 if (d->m_targetAttributeCount >= QQuick3DGeometryPrivate::MAX_TARGET_ATTRIBUTE_COUNT)
722 return;
723 if (attribute.attr.semantic == Attribute::IndexSemantic ||
724 attribute.attr.semantic == Attribute::JointSemantic ||
725 attribute.attr.semantic == Attribute::WeightSemantic)
726 return;
727 d->m_targetAttributes[d->m_targetAttributeCount++] = attribute;
728 d->m_targetChanged = true;
729}
730
731/*!
732 Resets the geometry to its initial state, clearing previously set vertex and index data as well as attributes.
733*/
734void QQuick3DGeometry::clear()
735{
736 Q_D(QQuick3DGeometry);
737 d->m_vertexBuffer.clear();
738 d->m_targetBuffer.clear();
739 d->m_indexBuffer.clear();
740 d->m_attributeCount = 0;
741 d->m_targetAttributeCount = 0;
742 d->m_subsets.clear();
743 d->m_primitiveType = PrimitiveType::Triangles;
744 d->m_geometryChanged = true;
745 d->m_targetChanged = true;
746 d->m_min = {};
747 d->m_max = {};
748}
749
750/*!
751 Returns the number of subsets.
752*/
753int QQuick3DGeometry::subsetCount() const
754{
755 Q_D(const QQuick3DGeometry);
756 return d->m_subsets.size();
757}
758
759/*!
760 Returns the number of minimum bounds of a \a subset.
761
762 \sa subsetBoundsMax
763*/
764QVector3D QQuick3DGeometry::subsetBoundsMin(int subset) const
765{
766 Q_D(const QQuick3DGeometry);
767 if (subset >= 0 && subset < d->m_subsets.size())
768 return d->m_subsets[subset].boundsMin;
769 return {};
770}
771
772/*!
773 Returns the number of maximum bounds of a \a subset.
774
775 \sa subsetBoundsMin
776*/
777QVector3D QQuick3DGeometry::subsetBoundsMax(int subset) const
778{
779 Q_D(const QQuick3DGeometry);
780 if (subset >= 0 && subset < d->m_subsets.size())
781 return d->m_subsets[subset].boundsMax;
782 return {};
783}
784
785/*!
786 Returns the \a subset offset to the vertex or index buffer.
787
788 \sa subsetCount
789*/
790int QQuick3DGeometry::subsetOffset(int subset) const
791{
792 Q_D(const QQuick3DGeometry);
793 if (subset >= 0 && subset < d->m_subsets.size())
794 return d->m_subsets[subset].offset;
795 return 0;
796}
797
798/*!
799 Returns the subset primitive count.
800
801 \sa subsetOffset
802*/
803int QQuick3DGeometry::subsetCount(int subset) const
804{
805 Q_D(const QQuick3DGeometry);
806 if (subset >= 0 && subset < d->m_subsets.size())
807 return d->m_subsets[subset].count;
808 return 0;
809}
810
811/*!
812 Returns the \a subset name.
813*/
814QString QQuick3DGeometry::subsetName(int subset) const
815{
816 Q_D(const QQuick3DGeometry);
817 if (subset >= 0 && subset < d->m_subsets.size())
818 return d->m_subsets[subset].name;
819 return {};
820}
821
822/*!
823 Adds new subset to the geometry. Subsets allow rendering parts of the geometry with different
824 materials. The materials are specified in the \l {Model::materials}{model}.
825
826 If the geometry has index buffer, then the \a offset and \a count are the primitive offset and
827 count of indices in the subset. If the geometry has only vertex buffer,
828 the offset is the vertex offset and count is the number of vertices in the subset.
829
830 The bounds \a boundsMin and \a boundsMax should enclose the subset just like geometry bounds.
831 Also the subset can have a \a name.
832*/
833void QQuick3DGeometry::addSubset(int offset, int count, const QVector3D &boundsMin, const QVector3D &boundsMax, const QString &name)
834{
835 Q_D(QQuick3DGeometry);
836 d->m_subsets.append({name, boundsMin, boundsMax, quint32(offset), quint32(count)});
837 d->m_geometryChanged = true;
838}
839
840static inline QSSGMesh::Mesh::DrawMode mapPrimitiveType(QQuick3DGeometry::PrimitiveType t)
841{
842 switch (t) {
843 case QQuick3DGeometry::PrimitiveType::Points:
844 return QSSGMesh::Mesh::DrawMode::Points;
845 case QQuick3DGeometry::PrimitiveType::LineStrip:
846 return QSSGMesh::Mesh::DrawMode::LineStrip;
847 case QQuick3DGeometry::PrimitiveType::Lines:
848 return QSSGMesh::Mesh::DrawMode::Lines;
849 case QQuick3DGeometry::PrimitiveType::TriangleStrip:
850 return QSSGMesh::Mesh::DrawMode::TriangleStrip;
851 case QQuick3DGeometry::PrimitiveType::TriangleFan:
852 return QSSGMesh::Mesh::DrawMode::TriangleFan;
853 case QQuick3DGeometry::PrimitiveType::Triangles:
854 return QSSGMesh::Mesh::DrawMode::Triangles;
855 }
856
857 Q_UNREACHABLE_RETURN(QSSGMesh::Mesh::DrawMode::Triangles);
858}
859
860static inline QSSGMesh::RuntimeMeshData::Attribute::Semantic mapSemantic(QQuick3DGeometry::Attribute::Semantic s)
861{
862 switch (s) {
863 case QQuick3DGeometry::Attribute::IndexSemantic:
864 return QSSGMesh::RuntimeMeshData::Attribute::IndexSemantic;
865 case QQuick3DGeometry::Attribute::PositionSemantic:
866 return QSSGMesh::RuntimeMeshData::Attribute::PositionSemantic;
867 case QQuick3DGeometry::Attribute::NormalSemantic:
868 return QSSGMesh::RuntimeMeshData::Attribute::NormalSemantic;
869 case QQuick3DGeometry::Attribute::TexCoord0Semantic:
870 return QSSGMesh::RuntimeMeshData::Attribute::TexCoord0Semantic;
871 case QQuick3DGeometry::Attribute::TexCoord1Semantic:
872 return QSSGMesh::RuntimeMeshData::Attribute::TexCoord1Semantic;
873 case QQuick3DGeometry::Attribute::TangentSemantic:
874 return QSSGMesh::RuntimeMeshData::Attribute::TangentSemantic;
875 case QQuick3DGeometry::Attribute::BinormalSemantic:
876 return QSSGMesh::RuntimeMeshData::Attribute::BinormalSemantic;
877 case QQuick3DGeometry::Attribute::JointSemantic:
878 return QSSGMesh::RuntimeMeshData::Attribute::JointSemantic;
879 case QQuick3DGeometry::Attribute::WeightSemantic:
880 return QSSGMesh::RuntimeMeshData::Attribute::WeightSemantic;
881 case QQuick3DGeometry::Attribute::ColorSemantic:
882 return QSSGMesh::RuntimeMeshData::Attribute::ColorSemantic;
883 case QQuick3DGeometry::Attribute::TargetPositionSemantic:
884 return QSSGMesh::RuntimeMeshData::Attribute::PositionSemantic;
885 case QQuick3DGeometry::Attribute::TargetNormalSemantic:
886 return QSSGMesh::RuntimeMeshData::Attribute::NormalSemantic;
887 case QQuick3DGeometry::Attribute::TargetTangentSemantic:
888 return QSSGMesh::RuntimeMeshData::Attribute::TangentSemantic;
889 case QQuick3DGeometry::Attribute::TargetBinormalSemantic:
890 return QSSGMesh::RuntimeMeshData::Attribute::BinormalSemantic;
891 }
892
893 Q_UNREACHABLE_RETURN(QSSGMesh::RuntimeMeshData::Attribute::PositionSemantic);
894}
895
896static inline QSSGMesh::Mesh::ComponentType mapComponentType(QQuick3DGeometry::Attribute::ComponentType t)
897{
898 switch (t) {
899 case QQuick3DGeometry::Attribute::U16Type:
900 return QSSGMesh::Mesh::ComponentType::UnsignedInt16;
901 case QQuick3DGeometry::Attribute::U32Type:
902 return QSSGMesh::Mesh::ComponentType::UnsignedInt32;
903 case QQuick3DGeometry::Attribute::I32Type:
904 return QSSGMesh::Mesh::ComponentType::Int32;
905 case QQuick3DGeometry::Attribute::F32Type:
906 return QSSGMesh::Mesh::ComponentType::Float32;
907 }
908
909 Q_UNREACHABLE_RETURN(QSSGMesh::Mesh::ComponentType::Float32);
910}
911
912/*!
913 \internal
914 */
915QSSGRenderGraphObject *QQuick3DGeometry::updateSpatialNode(QSSGRenderGraphObject *node)
916{
917 Q_D(QQuick3DGeometry);
918 if (!node) {
919 markAllDirty();
920 node = new QSSGRenderGeometry();
921 emit geometryNodeDirty();
922 }
923 QQuick3DObject::updateSpatialNode(node);
924 QSSGRenderGeometry *geometry = static_cast<QSSGRenderGeometry *>(node);
925 if (d->m_geometryChanged) {
926 geometry->clearVertexAndIndex();
927 geometry->setBounds(d->m_min, d->m_max);
928 geometry->setStride(d->m_stride);
929 // If there is vertex data but no stride is set, the user likely forgot to set the stride.
930 if (d->m_stride < 1 && !d->m_vertexBuffer.isEmpty())
931 qWarning("%d is an invalid stride, was QQuick3DGeometry::setStride() called?", d->m_stride);
932 geometry->setIndexData(d->m_indexBuffer);
933 geometry->setVertexData(d->m_vertexBuffer);
934 geometry->setPrimitiveType(mapPrimitiveType(d->m_primitiveType));
935 quint32 indexBufferComponentSize = 0;
936 for (int i = 0; i < d->m_attributeCount; ++i) {
937 const auto componentType = mapComponentType(d->m_attributes[i].componentType);
938 geometry->addAttribute(mapSemantic(d->m_attributes[i].semantic),
939 d->m_attributes[i].offset,
940 componentType);
941 if (d->m_attributes[i].semantic == Attribute::IndexSemantic) {
942 if (componentType != QSSGMesh::Mesh::ComponentType::UnsignedInt16
943 && componentType != QSSGMesh::Mesh::ComponentType::UnsignedInt32)
944 {
945 qWarning("Index data can only be uint16 or uint32");
946 }
947 indexBufferComponentSize = QSSGMesh::MeshInternal::byteSizeForComponentType(componentType);
948 } else if (componentType == QSSGMesh::Mesh::ComponentType::UnsignedInt16) {
949 qWarning("Attributes cannot be uint16, only index data");
950 }
951 }
952 if (!d->m_indexBuffer.isEmpty() && !indexBufferComponentSize) {
953 qWarning("IndexData has been set, but no index attribute found.");
954 geometry->setIndexData({});
955 }
956 // Implicitely add subset if none set for backwards compatibility
957 if (d->m_subsets.isEmpty()) {
958 quint32 offset = 0;
959 quint32 count = 0;
960 if (!d->m_indexBuffer.isEmpty() && indexBufferComponentSize)
961 count = d->m_indexBuffer.size() / indexBufferComponentSize;
962 else if (d->m_stride)
963 count = d->m_vertexBuffer.size() / d->m_stride;
964 geometry->addSubset(offset, count, d->m_min, d->m_max);
965 } else {
966 for (auto &s : d->m_subsets)
967 geometry->addSubset(s.offset, s.count, s.boundsMin, s.boundsMax, s.name);
968 }
969 d->m_geometryChanged = false;
970 emit geometryChanged();
971 }
972 if (d->m_geometryBoundsChanged) {
973 geometry->setBounds(d->m_min, d->m_max);
974 emit geometryNodeDirty();
975 d->m_geometryBoundsChanged = false;
976 }
977 if (d->m_targetChanged) {
978 geometry->clearTarget();
979 geometry->setTargetData(d->m_usesOldTargetSemantics ? d->m_vertexBuffer : d->m_targetBuffer);
980 for (int i = 0; i < d->m_targetAttributeCount; ++i) {
981 geometry->addTargetAttribute(d->m_targetAttributes[i].targetId,
982 mapSemantic(d->m_targetAttributes[i].attr.semantic),
983 d->m_targetAttributes[i].attr.offset,
984 d->m_usesOldTargetSemantics ? d->m_stride : d->m_targetAttributes[i].stride);
985 }
986 d->m_targetChanged = false;
987 }
988
989 DebugViewHelpers::ensureDebugObjectName(geometry, this);
990
991 return node;
992}
993
994QQuick3DGeometry::Attribute::Semantic QQuick3DGeometryPrivate::semanticFromName(const QByteArray &name)
995{
996 static QMap<const QByteArray, QQuick3DGeometry::Attribute::Semantic> semanticMap;
997 if (semanticMap.isEmpty()) {
998 semanticMap[QSSGMesh::MeshInternal::getPositionAttrName()] = QQuick3DGeometry::Attribute::PositionSemantic;
999 semanticMap[QSSGMesh::MeshInternal::getNormalAttrName()] = QQuick3DGeometry::Attribute::NormalSemantic;
1000 semanticMap[QSSGMesh::MeshInternal::getUV0AttrName()] = QQuick3DGeometry::Attribute::TexCoord0Semantic;
1001 semanticMap[QSSGMesh::MeshInternal::getUV1AttrName()] = QQuick3DGeometry::Attribute::TexCoord1Semantic;
1002 semanticMap[QSSGMesh::MeshInternal::getTexTanAttrName()] = QQuick3DGeometry::Attribute::TangentSemantic;
1003 semanticMap[QSSGMesh::MeshInternal::getTexBinormalAttrName()] = QQuick3DGeometry::Attribute::BinormalSemantic;
1004 semanticMap[QSSGMesh::MeshInternal::getColorAttrName()] = QQuick3DGeometry::Attribute::ColorSemantic;
1005 semanticMap[QSSGMesh::MeshInternal::getWeightAttrName()] = QQuick3DGeometry::Attribute::WeightSemantic;
1006 semanticMap[QSSGMesh::MeshInternal::getJointAttrName()] = QQuick3DGeometry::Attribute::JointSemantic;
1007 }
1008 return semanticMap[name];
1009}
1010
1011QQuick3DGeometry::Attribute::ComponentType QQuick3DGeometryPrivate::toComponentType(QSSGMesh::Mesh::ComponentType ctype)
1012{
1013 switch (ctype) {
1014 case QSSGMesh::Mesh::ComponentType::Float32:
1015 return QQuick3DGeometry::Attribute::F32Type;
1016 case QSSGMesh::Mesh::ComponentType::Int32:
1017 return QQuick3DGeometry::Attribute::I32Type;
1018 case QSSGMesh::Mesh::ComponentType::UnsignedInt16:
1019 return QQuick3DGeometry::Attribute::U16Type;
1020 case QSSGMesh::Mesh::ComponentType::UnsignedInt32:
1021 return QQuick3DGeometry::Attribute::U32Type;
1022
1023 case QSSGMesh::Mesh::ComponentType::Float16:
1024 case QSSGMesh::Mesh::ComponentType::Float64:
1025 case QSSGMesh::Mesh::ComponentType::UnsignedInt8:
1026 case QSSGMesh::Mesh::ComponentType::Int8:
1027 case QSSGMesh::Mesh::ComponentType::Int16:
1028 case QSSGMesh::Mesh::ComponentType::UnsignedInt64:
1029 case QSSGMesh::Mesh::ComponentType::Int64:
1030 default:
1031 Q_ASSERT_X(0, "Incorrect datatype", "QQuick3DGeometryPrivate::toComponentType");
1032 break;
1033 }
1034 return QQuick3DGeometry::Attribute::F32Type;
1035}
1036
1037QT_END_NAMESPACE
static QSSGMesh::Mesh::DrawMode mapPrimitiveType(QQuick3DGeometry::PrimitiveType t)
static QSSGMesh::RuntimeMeshData::Attribute::Semantic mapSemantic(QQuick3DGeometry::Attribute::Semantic s)
static QSSGMesh::Mesh::ComponentType mapComponentType(QQuick3DGeometry::Attribute::ComponentType t)