From ca15825ed4a1d4c8642c2292760726ebdcda863b Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Sat, 19 Apr 2014 16:36:35 +0100 Subject: 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 Reviewed-by: Laszlo Agocs --- .../auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tests') 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() { -- cgit v1.2.3