diff options
author | Mike Krus <mike.krus@kdab.com> | 2017-01-25 08:07:42 +0000 |
---|---|---|
committer | Mike Krus <mike.krus@kdab.com> | 2017-05-23 17:31:00 +0000 |
commit | 939b9b4b7591e8a421cf048a0a84ed3e75d81d21 (patch) | |
tree | 828ee7a6862ec5c0bd24f97cb540625a2c647376 /src/render/frontend/qcameralens.cpp | |
parent | e27ce2bf3b76ab4ed09508f780a8601875ea2fe8 (diff) |
Add support to move the camera so that the entire model is visible
Adds QCamera::viewAll(), QCamera::viewEntity() and QCamera::viewSphere()
to move and rotate the camera so that the view center is the center
of the sphere and the sphere fits inside the viewport
Only works in perspective projection.
Introduces a job to compute the bounding sphere of the scene
excluding the camera.
Change-Id: Id9d67787ea91c354009d5358d5db63a1c9480c70
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/render/frontend/qcameralens.cpp')
-rw-r--r-- | src/render/frontend/qcameralens.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/render/frontend/qcameralens.cpp b/src/render/frontend/qcameralens.cpp index 3c6b8db68..c9be49484 100644 --- a/src/render/frontend/qcameralens.cpp +++ b/src/render/frontend/qcameralens.cpp @@ -224,6 +224,43 @@ QCameraLensPrivate::QCameraLensPrivate() { } +void QCameraLens::viewAll(Qt3DCore::QNodeId cameraId) +{ + Q_D(QCameraLens); + if (d->m_projectionType == PerspectiveProjection) { + QVariant v; + v.setValue(cameraId); + d->m_pendingViewAllCommand = sendCommand(QLatin1Literal("QueryRootBoundingVolume"), v); + } +} + +void QCameraLens::viewEntity(Qt3DCore::QNodeId entityId, Qt3DCore::QNodeId cameraId) +{ + Q_D(QCameraLens); + if (d->m_projectionType == PerspectiveProjection) { + QVector<Qt3DCore::QNodeId> ids = {entityId, cameraId}; + QVariant v; + v.setValue(ids); + d->m_pendingViewAllCommand = sendCommand(QLatin1Literal("QueryEntityBoundingVolume"), v); + } +} + +void QCameraLensPrivate::processViewAllCommand(Qt3DCore::QNodeCommand::CommandId commandId, + const QVariant &data) +{ + Q_Q(QCameraLens); + if (m_pendingViewAllCommand != commandId) + return; + + QVector<float> boundingVolumeData = data.value< QVector<float> >(); + if (boundingVolumeData.size() != 4) + return; + QVector3D center(boundingVolumeData[0], boundingVolumeData[1], boundingVolumeData[2]); + float radius = boundingVolumeData[3]; + Q_EMIT q->viewSphere(center, radius); + m_pendingViewAllCommand = Qt3DCore::QNodeCommand::CommandId(); +} + /*! * Constructs a QCameraLens with given \a parent */ @@ -593,6 +630,22 @@ Qt3DCore::QNodeCreatedChangeBasePtr QCameraLens::createNodeCreationChange() cons return creationChange; } +void QCameraLens::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) +{ + Q_D(QCameraLens); + switch (change->type()) { + case Qt3DCore::CommandRequested: { + Qt3DCore::QNodeCommandPtr command = qSharedPointerCast<Qt3DCore::QNodeCommand>(change); + + if (command->name() == QLatin1Literal("ViewAll")) + d->processViewAllCommand(command->inReplyTo(), command->data()); + } + break; + default: + break; + } +} + } // Qt3DRender QT_END_NAMESPACE |