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 Tracked nodes must be children of the XrOrigin, and the positions the XR runtime
31 reports are in the origin's coordinate system. Use \l{Node::mapPositionToScene}
32 {mapPositionToScene} to convert them to scene coordinates. Do not apply a scale to
33 the XrOrigin: it scales the tracked motion and breaks the 1:1 correspondence with
34 the physical world.
35
36 \warning Ensure you follow best practices when changing the position/rotation of
37 the origin. Not doing so may cause physical discomfort, nausea, or loss of balance.
38 In the worst case, this could lead to injury or even death.
39
40 \sa {Coordinate Systems and Units in \qxr}
41*/
42
43QQuick3DXrOrigin::QQuick3DXrOrigin(QQuick3DNode *parent)
44 : QQuick3DNode(parent)
45{
46 // These are the "real" cameras that are used for rendering.
47 QQuick3DXrEyeCamera *leftEyeCamera = new QQuick3DXrEyeCamera(this);
48 leftEyeCamera->setParentItem(this);
49
50 QQuick3DXrEyeCamera *rightEyeCamera = new QQuick3DXrEyeCamera(this);
51 rightEyeCamera->setParentItem(this);
52
53 m_eyeCameras = { leftEyeCamera, rightEyeCamera };
54}
55
56QQuick3DXrOrigin::~QQuick3DXrOrigin()
57{
58
59}
60
61/*!
62 \qmlproperty XrCamera QtQuick3D.Xr::XrOrigin::camera
63 \brief Property for adding a tracked camera node.
64
65 \default null
66
67 Holds an XrCamera, which is a tracked spatial node that tracks the position and orientation
68 of the head-mounted display in the XR environment.
69
70 \note This property is optional.
71
72 \sa XrCamera
73*/
74
75QQuick3DXrCamera *QQuick3DXrOrigin::camera() const
76{
77 return m_camera;
78}
79
80void QQuick3DXrOrigin::setCamera(QQuick3DXrCamera *newCamera)
81{
82 if (m_camera == newCamera)
83 return;
84
85 QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DXrOrigin::setCamera, m_camera, newCamera);
86
87 m_camera = newCamera;
88
89 if (m_camera) {
90 // Ensure that the parent item is the XrOrigin
91 QQuick3DObject *camParentItem = m_camera->parentItem();
92 if (camParentItem != this) {
93 m_camera->setParentItem(this);
94 if (camParentItem != nullptr)
95 qWarning() << "XrCamera needs to be a child of XrOrigin. Reparenting...";
96 }
97
98 // If there's a camera, it will call this function to update the camera settings
99 // when its properties change.
100 syncCameraSettings();
101 } else {
102 // Restore default values
103 resetCameraSettings();
104 }
105
106 emit cameraChanged();
107}
108
109QQuick3DXrEyeCamera *QQuick3DXrOrigin::eyeCamera(int index) const
110{
111 return m_eyeCameras[index];
112}
113
114void QQuick3DXrOrigin::syncCameraSettings()
115{
116 QSSG_ASSERT(m_camera != nullptr, return);
117
118 for (auto eyeCamera : m_eyeCameras) {
119 eyeCamera->setClipNear(m_camera->clipNear());
120 eyeCamera->setClipFar(m_camera->clipFar());
121 }
122}
123
124void QQuick3DXrOrigin::resetCameraSettings()
125{
126 Q_ASSERT(!m_camera);
127
128 if (QQuick3DXrView *xrView = qobject_cast<QQuick3DXrView *>(parentItem())) {
129 // Use the default clip distances from the XrManager
130 float nearClip, farClip;
131 xrView->xrManager()->getDefaultClipDistances(nearClip, farClip);
132 for (auto eyeCamera : m_eyeCameras) {
133 eyeCamera->setClipNear(nearClip);
134 eyeCamera->setClipFar(farClip);
135 }
136 }
137}
138
139void QQuick3DXrOrigin::updateTrackedCamera(const QMatrix4x4 &transform)
140{
141 if (m_camera)
142 QQuick3DNodePrivate::get(m_camera)->setLocalTransform(transform);
143}
144
145void QQuick3DXrOrigin::updateTrackedCamera(QVector3D position, QQuaternion rotation)
146{
147 if (m_camera) {
148 m_camera->setPosition(position);
149 m_camera->setRotation(rotation);
150 }
151}
152
153QT_END_NAMESPACE
Combined button and popup list for selecting options.