1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QSSGVIEW3D_H
5#define QSSGVIEW3D_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 <QtQuick/QQuickItem>
19#include <QtCore/qurl.h>
20
21#include <QtQuick3D/qtquick3dglobal.h>
22#include <QtQuick3D/private/qquick3dpickresult_p.h>
23#include <QtQuick/private/qquickshadereffectsource_p.h>
24
25#include "qquick3dsceneenvironment_p.h"
26#include "qquick3drenderstats_p.h"
27#include "qquick3dlightmapbaker_p.h"
28
29QT_BEGIN_NAMESPACE
30
31class QSSGView3DPrivate;
32class QQuick3DCamera;
33class QQuick3DSceneEnvironment;
34class QQuick3DNode;
35class QQuick3DSceneRootNode;
36class QQuick3DSceneRenderer;
37class QQuick3DRenderStats;
38class QQuick3DSceneManager;
39
40class SGFramebufferObjectNode;
41class QQuick3DSGRenderNode;
42class QQuick3DSGDirectRenderer;
43
44class Q_QUICK3D_EXPORT QQuick3DViewport : public QQuickItem
45{
46 Q_OBJECT
47 Q_PROPERTY(QQmlListProperty<QObject> data READ data DESIGNABLE false FINAL)
48 Q_PROPERTY(QQuick3DCamera *camera READ camera WRITE setCamera NOTIFY cameraChanged FINAL)
49 Q_PROPERTY(QQuick3DSceneEnvironment *environment READ environment WRITE setEnvironment NOTIFY environmentChanged FINAL)
50 Q_PROPERTY(QQuick3DNode *scene READ scene NOTIFY sceneChanged)
51 Q_PROPERTY(QQuick3DNode *importScene READ importScene WRITE setImportScene NOTIFY importSceneChanged FINAL)
52 Q_PROPERTY(RenderMode renderMode READ renderMode WRITE setRenderMode NOTIFY renderModeChanged FINAL)
53 Q_PROPERTY(QQuickShaderEffectSource::Format renderFormat READ renderFormat WRITE setRenderFormat NOTIFY renderFormatChanged FINAL REVISION(6, 4))
54 Q_PROPERTY(QQuick3DRenderStats *renderStats READ renderStats CONSTANT)
55 Q_PROPERTY(QQmlListProperty<QQuick3DObject> extensions READ extensions FINAL REVISION(6, 6))
56 Q_CLASSINFO("DefaultProperty", "data")
57
58 QML_NAMED_ELEMENT(View3D)
59
60public:
61 enum RenderMode {
62 Offscreen,
63 Underlay,
64 Overlay,
65 Inline
66 };
67 Q_ENUM(RenderMode)
68
69 explicit QQuick3DViewport(QQuickItem *parent = nullptr);
70 ~QQuick3DViewport() override;
71
72 QQmlListProperty<QObject> data();
73
74 QQuick3DCamera *camera() const;
75 QQuick3DSceneEnvironment *environment() const;
76 QQuick3DNode *scene() const;
77 QQuick3DNode *importScene() const;
78 RenderMode renderMode() const;
79 Q_REVISION(6, 4) QQuickShaderEffectSource::Format renderFormat() const;
80 QQuick3DRenderStats *renderStats() const;
81
82 QQuick3DSceneRenderer *createRenderer() const;
83
84 bool isTextureProvider() const override;
85 QSGTextureProvider *textureProvider() const override;
86 void releaseResources() override;
87
88 Q_INVOKABLE QVector3D mapFrom3DScene(const QVector3D &scenePos) const;
89 Q_INVOKABLE QVector3D mapTo3DScene(const QVector3D &viewPos) const;
90
91 Q_INVOKABLE QQuick3DPickResult pick(float x, float y) const;
92 Q_REVISION(6, 2) Q_INVOKABLE QList<QQuick3DPickResult> pickAll(float x, float y) const;
93 Q_REVISION(6, 2) Q_INVOKABLE QQuick3DPickResult rayPick(const QVector3D &origin, const QVector3D &direction) const;
94 Q_REVISION(6, 2) Q_INVOKABLE QList<QQuick3DPickResult> rayPickAll(const QVector3D &origin, const QVector3D &direction) const;
95
96 void processPointerEventFromRay(const QVector3D &origin, const QVector3D &direction, QPointerEvent *event);
97
98 QQuick3DLightmapBaker *maybeLightmapBaker();
99 QQuick3DLightmapBaker *lightmapBaker();
100
101 Q_INVOKABLE void bakeLightmap();
102
103 QQmlListProperty<QQuick3DObject> extensions();
104
105 // Private helpers
106 [[nodiscard]] bool extensionListDirty() const { return m_extensionListDirty; }
107 [[nodiscard]] const QList<QQuick3DObject *> &extensionList() const { return m_extensions; }
108 void clearExtensionListDirty() { m_extensionListDirty = false; }
109
110protected:
111 void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
112 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
113 void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override;
114
115 bool event(QEvent *) override;
116 void componentComplete() override;
117
118public Q_SLOTS:
119 void setCamera(QQuick3DCamera *camera);
120 void setEnvironment(QQuick3DSceneEnvironment * environment);
121 void setImportScene(QQuick3DNode *inScene);
122 void setRenderMode(QQuick3DViewport::RenderMode renderMode);
123 Q_REVISION(6, 4) void setRenderFormat(QQuickShaderEffectSource::Format format);
124 void cleanupDirectRenderer();
125
126 // Setting this true enables picking for all the models, regardless of
127 // the models pickable property.
128 void setGlobalPickingEnabled(bool isEnabled);
129
130private Q_SLOTS:
131 void invalidateSceneGraph();
132 void updateInputProcessing();
133 void onReleaseCachedResources();
134
135Q_SIGNALS:
136 void cameraChanged();
137 void environmentChanged();
138 void sceneChanged();
139 void importSceneChanged();
140 void renderModeChanged();
141 Q_REVISION(6, 4) void renderFormatChanged();
142
143private:
144 friend class QQuick3DExtensionListHelper;
145
146 Q_DISABLE_COPY(QQuick3DViewport)
147 QQuick3DSceneRenderer *getRenderer() const;
148 void updateDynamicTextures();
149 QSGNode *setupOffscreenRenderer(QSGNode *node);
150 QSGNode *setupInlineRenderer(QSGNode *node);
151 void setupDirectRenderer(RenderMode mode);
152 bool checkIsVisible() const;
153 bool internalPick(QPointerEvent *event, const QVector3D &origin = QVector3D(), const QVector3D &direction = QVector3D()) const;
154 QQuick3DPickResult processPickResult(const QSSGRenderPickResult &pickResult) const;
155 QQuick3DSceneManager *findChildSceneManager(QQuick3DObject *inObject, QQuick3DSceneManager *manager = nullptr);
156 QQuick3DCamera *m_camera = nullptr;
157 QQuick3DSceneEnvironment *m_environment = nullptr;
158 QQuick3DSceneRootNode *m_sceneRoot = nullptr;
159 QQuick3DNode *m_importScene = nullptr;
160 mutable SGFramebufferObjectNode *m_node = nullptr;
161 mutable QQuick3DSGRenderNode *m_renderNode = nullptr;
162 mutable QQuick3DSGDirectRenderer *m_directRenderer = nullptr;
163 bool m_renderModeDirty = false;
164 RenderMode m_renderMode = Offscreen;
165 QQuickShaderEffectSource::Format m_renderFormat = QQuickShaderEffectSource::RGBA8;
166 QQuick3DRenderStats *m_renderStats = nullptr;
167 bool m_enableInputProcessing = false;
168 QQuick3DLightmapBaker *m_lightmapBaker = nullptr;
169 QList<QQuick3DObject *> m_extensions;
170 bool m_extensionListDirty = false;
171 Q_QUICK3D_PROFILE_ID
172};
173
174QT_END_NAMESPACE
175
176#endif // QSSGVIEW3D_H
177

source code of qtquick3d/src/quick3d/qquick3dviewport_p.h