| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef SPHEREGEOMETRY_P_H |
| 5 | #define SPHEREGEOMETRY_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QQuick3DGeometry> |
| 19 | #include <QQmlEngine> |
| 20 | #include <QVector3D> |
| 21 | |
| 22 | #if QT_CONFIG(concurrent) |
| 23 | #include <QFuture> |
| 24 | #include <QFutureWatcher> |
| 25 | #endif |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class SphereGeometry : public QQuick3DGeometry |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | Q_PROPERTY(float radius READ radius WRITE setRadius NOTIFY radiusChanged FINAL) |
| 33 | Q_PROPERTY(int rings READ rings WRITE setRings NOTIFY ringsChanged FINAL) |
| 34 | Q_PROPERTY(int segments READ segments WRITE setSegments NOTIFY segmentsChanged FINAL) |
| 35 | Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) |
| 36 | Q_PROPERTY(Status status READ status NOTIFY statusChanged) |
| 37 | QML_ELEMENT |
| 38 | QML_ADDED_IN_VERSION(6, 9) |
| 39 | public: |
| 40 | enum Status { Null, Ready, Loading, Error }; |
| 41 | Q_ENUM(Status) |
| 42 | |
| 43 | explicit SphereGeometry(QQuick3DObject *parent = nullptr); |
| 44 | ~SphereGeometry() override; |
| 45 | float radius() const; |
| 46 | void setRadius(float newRadius); |
| 47 | int rings() const; |
| 48 | void setRings(int newRings); |
| 49 | |
| 50 | int segments() const; |
| 51 | void setSegments(int newSegments); |
| 52 | |
| 53 | bool asynchronous() const; |
| 54 | void setAsynchronous(bool newAsynchronous); |
| 55 | |
| 56 | Status status() const; |
| 57 | |
| 58 | private Q_SLOTS: |
| 59 | void doUpdateGeometry(); |
| 60 | void requestFinished(); |
| 61 | |
| 62 | Q_SIGNALS: |
| 63 | void radiusChanged(); |
| 64 | void ringsChanged(); |
| 65 | void segmentsChanged(); |
| 66 | void asynchronousChanged(); |
| 67 | void statusChanged(); |
| 68 | |
| 69 | private: |
| 70 | struct GeometryData { |
| 71 | QByteArray vertexData; |
| 72 | QByteArray indexData; |
| 73 | QVector3D boundsMin; |
| 74 | QVector3D boundsMax; |
| 75 | }; |
| 76 | |
| 77 | void scheduleGeometryUpdate(); |
| 78 | void updateGeometry(const GeometryData &geometryData); |
| 79 | |
| 80 | static SphereGeometry::GeometryData generateSphereGeometry(float radius, |
| 81 | int rings, |
| 82 | int segments); |
| 83 | #if QT_CONFIG(concurrent) |
| 84 | static void generateSphereGeometryAsync(QPromise<SphereGeometry::GeometryData> &promise, |
| 85 | float radius, |
| 86 | int rings, |
| 87 | int segments); |
| 88 | #endif |
| 89 | |
| 90 | float m_radius = 100.0f; |
| 91 | int m_rings = 16; |
| 92 | int m_segments = 32; |
| 93 | bool m_asynchronous = true; |
| 94 | Status m_status = Null; |
| 95 | #if QT_CONFIG(concurrent) |
| 96 | QFuture<GeometryData> m_geometryDataFuture; |
| 97 | QFutureWatcher<GeometryData> m_geometryDataWatcher; |
| 98 | #endif |
| 99 | bool m_geometryUpdateRequested = false; |
| 100 | bool m_pendingAsyncUpdate = false; |
| 101 | }; |
| 102 | |
| 103 | QT_END_NAMESPACE |
| 104 | |
| 105 | #endif // SPHEREGEOMETRY_P_H |
| 106 | |