Skip to content
Snippets Groups Projects
Commit c7a120aa authored by Richard Gustavsen's avatar Richard Gustavsen
Browse files

QDemonView3D: add worldToView

parent 7cd1debc
No related branches found
No related tags found
No related merge requests found
Pipeline #3593 passed
...@@ -157,6 +157,8 @@ void QDemonCamera::setProjectionMode(QDemonCamera::QDemonCameraProjectionMode pr ...@@ -157,6 +157,8 @@ void QDemonCamera::setProjectionMode(QDemonCamera::QDemonCameraProjectionMode pr
* is normalized between 0 and 1. The top-left of the viewport is (0,0) and * is normalized between 0 and 1. The top-left of the viewport is (0,0) and
* the botton-right is (1,1). If the position is not visible in the viewport, a * the botton-right is (1,1). If the position is not visible in the viewport, a
* position of [-1, -1] is returned. * position of [-1, -1] is returned.
*
* \sa QDemonView3D::worldToView
*/ */
QVector2D QDemonCamera::worldToViewport(const QVector3D &worldPos) const QVector2D QDemonCamera::worldToViewport(const QVector3D &worldPos) const
{ {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "qdemonscenemanager_p.h" #include "qdemonscenemanager_p.h"
#include "qdemonimage.h" #include "qdemonimage.h"
#include "qdemonscenerenderer.h" #include "qdemonscenerenderer.h"
#include "qdemoncamera.h"
#include <QtDemonRuntimeRender/QDemonRenderLayer> #include <QtDemonRuntimeRender/QDemonRenderLayer>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
...@@ -421,6 +422,26 @@ QSurfaceFormat QDemonView3D::idealSurfaceFormat() ...@@ -421,6 +422,26 @@ QSurfaceFormat QDemonView3D::idealSurfaceFormat()
return f; return f;
} }
/*!
* Transforms \a worldPoint from world space into view space. If the position
* is not visible in the viewport, a position of [-1, -1] is returned. This
* function requires that a camera is assigned to the view.
*
* \sa QDemonCamera::worldToViewport
*/
QVector2D QDemonView3D::worldToView(const QVector3D &worldPos) const
{
if (!m_camera) {
qmlWarning(this) << "Cannot resolve position in view without a camera assigned!";
return QVector2D(-1, -1);
}
const QVector2D normalizedPos = m_camera->worldToViewport(worldPos);
if (normalizedPos.x() < 0)
return normalizedPos;
return normalizedPos * QVector2D(float(width()), float(height()));
}
void QDemonView3D::invalidateSceneGraph() void QDemonView3D::invalidateSceneGraph()
{ {
m_node = nullptr; m_node = nullptr;
......
...@@ -57,6 +57,8 @@ public: ...@@ -57,6 +57,8 @@ public:
static QSurfaceFormat idealSurfaceFormat(); static QSurfaceFormat idealSurfaceFormat();
Q_INVOKABLE QVector2D worldToView(const QVector3D &worldPos) const;
protected: protected:
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment