summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/math3d/qmatrix4x4.cpp46
-rw-r--r--src/gui/math3d/qmatrix4x4.h7
-rw-r--r--tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp35
3 files changed, 88 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
diff --git a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
index 98f1c1c8f9..e492a8f7bf 100644
--- a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
+++ b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
@@ -147,6 +147,7 @@ private slots:
void ortho();
void frustum();
void perspective();
+ void viewport();
void flipCoordinates();
void convertGeneric();
@@ -2794,6 +2795,40 @@ void tst_QMatrixNxN::perspective()
QVERIFY(m5.isIdentity());
}
+// Test viewport transformations
+void tst_QMatrixNxN::viewport()
+{
+ // Uses default depth range of 0->1
+ QMatrix4x4 m1;
+ m1.viewport(0.0f, 0.0f, 1024.0f, 768.0f);
+
+ // Lower left
+ QVector4D p1 = m1 * QVector4D(-1.0f, -1.0f, 0.0f, 1.0f);
+ QVERIFY(qFuzzyIsNull(p1.x()));
+ QVERIFY(qFuzzyIsNull(p1.y()));
+ QVERIFY(qFuzzyCompare(p1.z(), 0.5f));
+
+ // Lower right
+ QVector4D p2 = m1 * QVector4D(1.0f, -1.0f, 0.0f, 1.0f);
+ QVERIFY(qFuzzyCompare(p2.x(), 1024.0f));
+ QVERIFY(qFuzzyIsNull(p2.y()));
+
+ // Upper right
+ QVector4D p3 = m1 * QVector4D(1.0f, 1.0f, 0.0f, 1.0f);
+ QVERIFY(qFuzzyCompare(p3.x(), 1024.0f));
+ QVERIFY(qFuzzyCompare(p3.y(), 768.0f));
+
+ // Upper left
+ QVector4D p4 = m1 * QVector4D(-1.0f, 1.0f, 0.0f, 1.0f);
+ QVERIFY(qFuzzyIsNull(p4.x()));
+ QVERIFY(qFuzzyCompare(p4.y(), 768.0f));
+
+ // Center
+ QVector4D p5 = m1 * QVector4D(0.0f, 0.0f, 0.0f, 1.0f);
+ QVERIFY(qFuzzyCompare(p5.x(), 1024.0f / 2.0f));
+ QVERIFY(qFuzzyCompare(p5.y(), 768.0f / 2.0f));
+}
+
// Test left-handed vs right-handed coordinate flipping.
void tst_QMatrixNxN::flipCoordinates()
{