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
qquick3dxrorigin.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 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
7
9
10#include <QtQuick3D/private/qquick3dnode_p_p.h>
11
12#include <QtQuick3DUtils/private/qssgassert_p.h>
13
15
16/*!
17 \qmltype XrOrigin
18 \inherits Node
19 \inqmlmodule QtQuick3D.Xr
20 \brief The origin location for the XrView.
21
22 The origin determines the center of the \l{XrView::referenceSpace}{reference space}.
23 Changing the position of the origin will transport the user to a different position
24 in the scene.
25
26 Most XR platforms allow the user to move physically, changing the viewpoint. This
27 does not change the position of the origin. To track the position of the headset, create
28 an XrCamera and assign it to the \l camera property.
29
30 \warning Ensure you follow best practices when changing the position/rotation of
31 the origin. Not doing so may cause physical discomfort, nausea, or loss of balance.
32 In the worst case, this could lead to injury or even death.
33*/
34
35QQuick3DXrOrigin::QQuick3DXrOrigin(QQuick3DNode *parent)
36 : QQuick3DNode(parent)
37{
38 // These are the "real" cameras that are used for rendering.
39 QQuick3DXrEyeCamera *leftEyeCamera = new QQuick3DXrEyeCamera(this);
40 leftEyeCamera->setParentItem(this);
41
42 QQuick3DXrEyeCamera *rightEyeCamera = new QQuick3DXrEyeCamera(this);
43 rightEyeCamera->setParentItem(this);
44
45 m_eyeCameras = { leftEyeCamera, rightEyeCamera };
46}
47
48QQuick3DXrOrigin::~QQuick3DXrOrigin()
49{
50
51}
52
53/*!
54 \qmlproperty XrCamera QtQuick3D.Xr::XrOrigin::camera
55 \brief Property for adding a tracked camera node.
56
57 \default null
58
59 Holds an XrCamera, which is a tracked spatial node that tracks the position and orientation
60 of the head-mounted display in the XR environment.
61
62 \note This property is optional.
63
64 \sa XrCamera
65*/
66
67QQuick3DXrCamera *QQuick3DXrOrigin::camera() const
68{
69 return m_camera;
70}
71
72void QQuick3DXrOrigin::setCamera(QQuick3DXrCamera *newCamera)
73{
74 if (m_camera == newCamera)
75 return;
76
77 QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DXrOrigin::setCamera, m_camera, newCamera);
78
79 m_camera = newCamera;
80
81 if (m_camera) {
82 // Ensure that the parent item is the XrOrigin
83 QQuick3DObject *camParentItem = m_camera->parentItem();
84 if (camParentItem != this) {
85 m_camera->setParentItem(this);
86 if (camParentItem != nullptr)
87 qWarning() << "XrCamera needs to be a child of XrOrigin. Reparenting...";
88 }
89
90 // If there's a camera, it will call this function to update the camera settings
91 // when its properties change.
92 syncCameraSettings();
93 } else {
94 // Restore default values
95 resetCameraSettings();
96 }
97
98 emit cameraChanged();
99}
100
101QQuick3DXrEyeCamera *QQuick3DXrOrigin::eyeCamera(int index) const
102{
103 return m_eyeCameras[index];
104}
105
106void QQuick3DXrOrigin::syncCameraSettings()
107{
108 QSSG_ASSERT(m_camera != nullptr, return);
109
110 for (auto eyeCamera : m_eyeCameras) {
111 eyeCamera->setClipNear(m_camera->clipNear());
112 eyeCamera->setClipFar(m_camera->clipFar());
113 }
114}
115
116void QQuick3DXrOrigin::resetCameraSettings()
117{
118 Q_ASSERT(!m_camera);
119
120 if (QQuick3DXrView *xrView = qobject_cast<QQuick3DXrView *>(parentItem())) {
121 // Use the default clip distances from the XrManager
122 float nearClip, farClip;
123 xrView->xrManager()->getDefaultClipDistances(nearClip, farClip);
124 for (auto eyeCamera : m_eyeCameras) {
125 eyeCamera->setClipNear(nearClip);
126 eyeCamera->setClipFar(farClip);
127 }
128 }
129}
130
131void QQuick3DXrOrigin::updateTrackedCamera(const QMatrix4x4 &transform)
132{
133 if (m_camera)
134 QQuick3DNodePrivate::get(m_camera)->setLocalTransform(transform);
135}
136
137void QQuick3DXrOrigin::updateTrackedCamera(QVector3D position, QQuaternion rotation)
138{
139 if (m_camera) {
140 m_camera->setPosition(position);
141 m_camera->setRotation(rotation);
142 }
143}
144
145QT_END_NAMESPACE
Combined button and popup list for selecting options.