diff options
author | Sean Harmer <sean.harmer@kdab.com> | 2014-04-19 16:36:35 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-05-16 09:53:58 +0200 |
commit | ca15825ed4a1d4c8642c2292760726ebdcda863b (patch) | |
tree | ca28f9e9d3938386379b3c963d475382fca86166 /src/gui | |
parent | 71fb3633e8d909e9a91e1bee6eaf53c146f25998 (diff) |
Add viewport transform to QMatrix4x4
This allows to easily create a matrix that performs the transformation
used by OpenGL fixed function to go from normalized device coordinates
to window coordinates.
This comes in useful if you need to perform the NDC->window coordinate
conversion inside a shader.
Change-Id: I183b3545bfb3eb1e8b13fc3172911b46926fcbb7
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Reviewed-by: Laszlo Agocs <laszlo.agocs@digia.com>
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/math3d/qmatrix4x4.cpp | 46 | ||||
-rw-r--r-- | src/gui/math3d/qmatrix4x4.h | 7 |
2 files changed, 53 insertions, 0 deletions
diff --git a/src/gui/math3d/qmatrix4x4.cpp b/src/gui/math3d/qmatrix4x4.cpp index 0b3c5b4cb9..5d7a176fe5 100644 --- a/src/gui/math3d/qmatrix4x4.cpp +++ b/src/gui/math3d/qmatrix4x4.cpp @@ -1542,6 +1542,52 @@ void QMatrix4x4::lookAt(const QVector3D& eye, const QVector3D& center, const QVe #endif /*! + \fn void QMatrix4x4::viewport(const QRectF &rect) + \overload + + Sets up viewport transform for viewport bounded by \a rect and with near and far set + to 0 and 1 respectively. +*/ + +/*! + Multiplies this matrix by another that performs the scale and bias + transformation used by OpenGL to transform from normalized device + coordinates (NDC) to viewport (window) coordinates. That is it maps + points from the cube ranging over [-1, 1] in each dimension to the + viewport with it's near-lower-left corner at (\a left, \a bottom, \a nearPlane) + and with size (\a width, \a height, \a farPlane - \a nearPlane). + + This matches the transform used by the fixed function OpenGL viewport + transform controlled by the functions glViewport() and glDepthRange(). + */ +void QMatrix4x4::viewport(float left, float bottom, float width, float height, float nearPlane, float farPlane) +{ + const float w2 = width / 2.0f; + const float h2 = height / 2.0f; + + QMatrix4x4 m(1); + m.m[0][0] = w2; + m.m[1][0] = 0.0f; + m.m[2][0] = 0.0f; + m.m[3][0] = left + w2; + m.m[0][1] = 0.0f; + m.m[1][1] = h2; + m.m[2][1] = 0.0f; + m.m[3][1] = bottom + h2; + m.m[0][2] = 0.0f; + m.m[1][2] = 0.0f; + m.m[2][2] = (farPlane - nearPlane) / 2.0f; + m.m[3][2] = (nearPlane + farPlane) / 2.0f; + m.m[0][3] = 0.0f; + m.m[1][3] = 0.0f; + m.m[2][3] = 0.0f; + m.m[3][3] = 1.0f; + m.flagBits = General; + + *this *= m; +} + +/*! \deprecated Flips between right-handed and left-handed coordinate systems diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h index d4ba7d64d8..c42ccc2dc5 100644 --- a/src/gui/math3d/qmatrix4x4.h +++ b/src/gui/math3d/qmatrix4x4.h @@ -148,6 +148,8 @@ public: #ifndef QT_NO_VECTOR3D void lookAt(const QVector3D& eye, const QVector3D& center, const QVector3D& up); #endif + void viewport(const QRectF &rect); + void viewport(float left, float bottom, float width, float height, float nearPlane = 0.0f, float farPlane = 1.0f); void flipCoordinates(); void copyDataTo(float *values) const; @@ -1077,6 +1079,11 @@ inline float *QMatrix4x4::data() return *m; } +inline void QMatrix4x4::viewport(const QRectF &rect) +{ + viewport(rect.x(), rect.y(), rect.width(), rect.height()); +} + #ifndef QT_NO_DEBUG_STREAM Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QMatrix4x4 &m); #endif |