Commit 91987426 authored by Richard Gustavsen's avatar Richard Gustavsen

QQuick3dViewport::mapFrom3DScene: clearify if position is leftHanded or rightHanded

parent 67b57df3
Pipeline #5121 passed with stage
in 4 minutes and 44 seconds
......@@ -385,21 +385,26 @@ void QQuick3DCamera::setCustomProjection(QMatrix4x4 customProjection)
/*!
* \qmlmethod vector3d Camera::mapFromScene(vector3d scenePos)
*
* Transforms \a scenePos from scene space (3D) into viewport space (2D). The position
* is normalized, with the top-left of the viewport being [0,0] and
* Transforms \a globalScenePos from scene space (3D) into viewport space (2D). The returned
* position is normalized, with the top-left of the viewport being [0,0] and
* the botton-right being [1,1]. The returned z value will contain the distance from the
* back of the frustum (clipNear) to \a scenePos in view coordinates. If \a scenePos
* back of the frustum (clipNear) to \a globalScenePos in view coordinates. If \a globalScenePos
* cannot be mapped to a position in the viewport, a position of [0, 0, 0] is returned.
*
* \note \a globalScenePos should be in the same \l orientation as the camera.
*
* \sa QQuick3DViewport::mapFrom3DScene QQuick3DCamera::mapToScene
*/
QVector3D QQuick3DCamera::mapFromScene(const QVector3D &scenePos) const
QVector3D QQuick3DCamera::mapFromScene(const QVector3D &globalScenePos) const
{
if (!m_cameraNode)
return QVector3D(0, 0, 0);
// Convert from left-handed to right-handed
const QVector4D scenePosRightHand(scenePos.x(), scenePos.y(), -scenePos.z(), 1);
QVector4D scenePosRightHand(globalScenePos, 1);
if (orientation() == LeftHanded) {
// Convert from left-handed to right-handed
scenePosRightHand.setZ(-scenePosRightHand.z());
}
// Transform position
const QMatrix4x4 sceneToCamera = globalTransformRightHanded().inverted();
......@@ -431,13 +436,15 @@ QVector3D QQuick3DCamera::mapFromScene(const QVector3D &scenePos) const
/*!
* \qmlmethod vector3d Camera::mapToScene(vector3d viewportPos)
*
* Transforms \a viewportPos from viewport space (2D) into scene space (3D). \a The x-, and y
* values of \l viewportPos needs to be normalized, with the top-left of the viewport
* being [0,0] and the botton-right being [1,1]. The z value should be the distance
* from the back of the frustum (clipNear) into the scene in scene coordinates.
* Transforms \a viewportPos from viewport space (2D) into global scene space (3D).
* \a The x-, and y values of \a viewportPos needs to be normalized, with the top-left
* of the viewport being [0,0] and the botton-right being [1,1]. The z value should be
* the distance from the back of the frustum (clipNear) into the scene in scene coordinates.
* If \a viewportPos cannot be mapped to a position in the scene, a position of
* [0, 0, 0] is returned.
*
* \note the returned position will be in the same \l orientation as the camera.
*
* \sa QQuick3DViewport::mapTo3DScene QQuick3DCamera::mapFromScene
*/
QVector3D QQuick3DCamera::mapToScene(const QVector3D &viewportPos) const
......@@ -475,8 +482,12 @@ QVector3D QQuick3DCamera::mapToScene(const QVector3D &viewportPos) const
const QVector3D direction = (clipFarPosScene - clipNearPosScene).normalized();
const float distanceFromClipNear = viewportPos.z();
QVector3D scenePos = clipNearPosScene + (direction * distanceFromClipNear);
// Convert right-handed to left-handed
scenePos.setZ(-scenePos.z());
if (orientation() == LeftHanded) {
// Convert from right-handed to left-handed
scenePos.setZ(-scenePos.z());
}
return scenePos;
}
......
......@@ -84,7 +84,7 @@ public:
QSSGCameraProjectionMode projectionMode() const;
bool enableFrustumCulling() const;
Q_INVOKABLE QVector3D mapFromScene(const QVector3D &scenePos) const;
Q_INVOKABLE QVector3D mapFromScene(const QVector3D &globalScenePos) const;
Q_INVOKABLE QVector3D mapToScene(const QVector3D &viewportPos) const;
QSSGRenderCamera *getCameraNode() const;
......
......@@ -532,22 +532,25 @@ QSurfaceFormat QQuick3DViewport::idealSurfaceFormat()
/*!
* \qmlmethod vector3d View3D::mapFrom3DScene(vector3d scenePos)
*
* Transforms \a scenePos from scene space (3D) into view space (2D). The returned x-, and y values
* Transforms \a globalScenePos from scene space (3D) into view space (2D). The returned x-, and y values
* will be be in view coordinates. The returned z value will contain the distance from the
* back of the frustum (clipNear) to \a scenePos in scene coordinates.
* If \a scenePos cannot be mapped to a position in the scene, a position of [0, 0, 0] is
* If \a globalScenePos cannot be mapped to a position in the scene, a position of [0, 0, 0] is
* returned. This function requires that a camera is assigned to the view.
*
* \note \a globalScenePos should be in the same \l orientation as the camera
* assigned to the view.
*
* \sa QQuick3DCamera::mapFrom3DScene QQuick3DViewport::mapTo3DScene
*/
QVector3D QQuick3DViewport::mapFrom3DScene(const QVector3D &scenePos) const
QVector3D QQuick3DViewport::mapFrom3DScene(const QVector3D &globalScenePos) const
{
if (!m_camera) {
qmlWarning(this) << "Cannot resolve view position without a camera assigned!";
return QVector3D(0, 0, 0);
}
const QVector3D normalizedPos = m_camera->mapFromScene(scenePos);
const QVector3D normalizedPos = m_camera->mapFromScene(globalScenePos);
if (normalizedPos.x() < 0)
return normalizedPos;
return normalizedPos * QVector3D(float(width()), float(height()), 1);
......@@ -562,6 +565,9 @@ QVector3D QQuick3DViewport::mapFrom3DScene(const QVector3D &scenePos) const
* coordinates. If \a viewPos cannot be mapped to a position in the scene, a
* position of [0, 0, 0] is returned.
*
* \note \a the returned position will be in the same \l orientation as the camera
* assigned to the view.
*
* \sa QQuick3DCamera::mapTo3DScene QQuick3DViewport::mapFrom3DScene
*/
QVector3D QQuick3DViewport::mapTo3DScene(const QVector3D &viewPos) const
......
......@@ -99,7 +99,7 @@ public:
static QSurfaceFormat idealSurfaceFormat();
Q_INVOKABLE QVector3D mapFrom3DScene(const QVector3D &scenePos) const;
Q_INVOKABLE QVector3D mapFrom3DScene(const QVector3D &globalScenePos) const;
Q_INVOKABLE QVector3D mapTo3DScene(const QVector3D &viewPos) const;
Q_INVOKABLE QQuick3DPickResult pick(float x, float y) const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment