diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-04 04:12:08 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-04 07:00:23 (GMT) |
commit | 974aec137dfdbb1dd68c41113b22eb25131965b8 (patch) | |
tree | adbf042502fd35d1bce5a2aea811d41633ecd495 /src/gui/math3d/qgenericmatrix.h | |
parent | 4c501d7fce503a610edabfba5d6efc3ef2778bef (diff) | |
download | Qt-974aec137dfdbb1dd68c41113b22eb25131965b8.zip Qt-974aec137dfdbb1dd68c41113b22eb25131965b8.tar.gz Qt-974aec137dfdbb1dd68c41113b22eb25131965b8.tar.bz2 |
Modify QMatrix4x4 and QQuaternion to use qreal internally
Some concerns were expressed about the float precision of QMatrix4x4,
which this change addresses by using qreal instead.
The QVector2D/3D/4D classes still use float internally, so that they
can be used directly in large arrays of vertex values to be uploaded
to an OpenGL server.
QQuaternion is a client-side class, and it should produce rotations
that are consistent with QMatrix4x4. So its precision was changed too.
A consequence of this change is that the following no longer works
in a portable fashion:
QMatrix4x4 mat;
...
glLoadMatrixf(mat.constData());
The caller must now repack the argument to convert from qreal to GLfloat.
Reviewed-by: Michael Goddard
Reviewed-by: Andreas
Diffstat (limited to 'src/gui/math3d/qgenericmatrix.h')
-rw-r--r-- | src/gui/math3d/qgenericmatrix.h | 209 |
1 files changed, 102 insertions, 107 deletions
diff --git a/src/gui/math3d/qgenericmatrix.h b/src/gui/math3d/qgenericmatrix.h index fe7ba5f..39c4ed8 100644 --- a/src/gui/math3d/qgenericmatrix.h +++ b/src/gui/math3d/qgenericmatrix.h @@ -51,103 +51,103 @@ QT_BEGIN_NAMESPACE QT_MODULE(Gui) -template <int N, int M, typename T, typename InnerT = T> +template <int N, int M, typename T> class QGenericMatrix { public: QGenericMatrix(); - QGenericMatrix(const QGenericMatrix<N, M, T, InnerT>& other); + QGenericMatrix(const QGenericMatrix<N, M, T>& other); explicit QGenericMatrix(const T *values); - T operator()(int row, int column) const; - InnerT& operator()(int row, int column); + const T& operator()(int row, int column) const; + T& operator()(int row, int column); bool isIdentity() const; void setIdentity(); void fill(T value); - QGenericMatrix<M, N, T, InnerT> transposed() const; + QGenericMatrix<M, N, T> transposed() const; - QGenericMatrix<N, M, T, InnerT>& operator+=(const QGenericMatrix<N, M, T, InnerT>& other); - QGenericMatrix<N, M, T, InnerT>& operator-=(const QGenericMatrix<N, M, T, InnerT>& other); - QGenericMatrix<N, M, T, InnerT>& operator*=(T factor); - QGenericMatrix<N, M, T, InnerT>& operator/=(T divisor); - bool operator==(const QGenericMatrix<N, M, T, InnerT>& other) const; - bool operator!=(const QGenericMatrix<N, M, T, InnerT>& other) const; + QGenericMatrix<N, M, T>& operator+=(const QGenericMatrix<N, M, T>& other); + QGenericMatrix<N, M, T>& operator-=(const QGenericMatrix<N, M, T>& other); + QGenericMatrix<N, M, T>& operator*=(T factor); + QGenericMatrix<N, M, T>& operator/=(T divisor); + bool operator==(const QGenericMatrix<N, M, T>& other) const; + bool operator!=(const QGenericMatrix<N, M, T>& other) const; void toValueArray(T *values); - InnerT *data() { return m[0]; } - const InnerT *data() const { return m[0]; } - const InnerT *constData() const { return m[0]; } + T *data() { return m[0]; } + const T *data() const { return m[0]; } + const T *constData() const { return m[0]; } #if !defined(Q_NO_TEMPLATE_FRIENDS) - template<int NN, int MM, typename TT, typename ITT> - friend QGenericMatrix<NN, MM, TT, ITT> operator+(const QGenericMatrix<NN, MM, TT, ITT>& m1, const QGenericMatrix<NN, MM, TT, ITT>& m2); - template<int NN, int MM, typename TT, typename ITT> - friend QGenericMatrix<NN, MM, TT, ITT> operator-(const QGenericMatrix<NN, MM, TT, ITT>& m1, const QGenericMatrix<NN, MM, TT, ITT>& m2); - template<int NN, int M1, int M2, typename TT, typename ITT> - friend QGenericMatrix<M1, M2, TT, ITT> operator*(const QGenericMatrix<NN, M2, TT, ITT>& m1, const QGenericMatrix<M1, NN, TT, ITT>& m2); - template<int NN, int MM, typename TT, typename ITT> - friend QGenericMatrix<NN, MM, TT, ITT> operator-(const QGenericMatrix<NN, MM, TT, ITT>& matrix); - template<int NN, int MM, typename TT, typename ITT> - friend QGenericMatrix<NN, MM, TT, ITT> operator*(TT factor, const QGenericMatrix<NN, MM, TT, ITT>& matrix); - template<int NN, int MM, typename TT, typename ITT> - friend QGenericMatrix<NN, MM, TT, ITT> operator*(const QGenericMatrix<NN, MM, TT, ITT>& matrix, TT factor); - template<int NN, int MM, typename TT, typename ITT> - friend QGenericMatrix<NN, MM, TT, ITT> operator/(const QGenericMatrix<NN, MM, TT, ITT>& matrix, TT divisor); + template<int NN, int MM, typename TT> + friend QGenericMatrix<NN, MM, TT> operator+(const QGenericMatrix<NN, MM, TT>& m1, const QGenericMatrix<NN, MM, TT>& m2); + template<int NN, int MM, typename TT> + friend QGenericMatrix<NN, MM, TT> operator-(const QGenericMatrix<NN, MM, TT>& m1, const QGenericMatrix<NN, MM, TT>& m2); + template<int NN, int M1, int M2, typename TT> + friend QGenericMatrix<M1, M2, TT> operator*(const QGenericMatrix<NN, M2, TT>& m1, const QGenericMatrix<M1, NN, TT>& m2); + template<int NN, int MM, typename TT> + friend QGenericMatrix<NN, MM, TT> operator-(const QGenericMatrix<NN, MM, TT>& matrix); + template<int NN, int MM, typename TT> + friend QGenericMatrix<NN, MM, TT> operator*(TT factor, const QGenericMatrix<NN, MM, TT>& matrix); + template<int NN, int MM, typename TT> + friend QGenericMatrix<NN, MM, TT> operator*(const QGenericMatrix<NN, MM, TT>& matrix, TT factor); + template<int NN, int MM, typename TT> + friend QGenericMatrix<NN, MM, TT> operator/(const QGenericMatrix<NN, MM, TT>& matrix, TT divisor); private: #endif - InnerT m[N][M]; // Column-major order to match OpenGL. + T m[N][M]; // Column-major order to match OpenGL. QGenericMatrix(int) {} // Construct without initializing identity matrix. #if !defined(Q_NO_TEMPLATE_FRIENDS) - template <int NN, int MM, typename TT, typename ITT> + template <int NN, int MM, typename TT> friend class QGenericMatrix; #endif }; -template <int N, int M, typename T, typename InnerT> -Q_INLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT>::QGenericMatrix() +template <int N, int M, typename T> +Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix() { setIdentity(); } -template <int N, int M, typename T, typename InnerT> -Q_INLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT>::QGenericMatrix(const QGenericMatrix<N, M, T, InnerT>& other) +template <int N, int M, typename T> +Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const QGenericMatrix<N, M, T>& other) { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) m[col][row] = other.m[col][row]; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT>::QGenericMatrix(const T *values) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const T *values) { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) m[col][row] = values[row * N + col]; } -template <int N, int M, typename T, typename InnerT> -Q_INLINE_TEMPLATE T QGenericMatrix<N, M, T, InnerT>::operator()(int row, int column) const +template <int N, int M, typename T> +Q_INLINE_TEMPLATE const T& QGenericMatrix<N, M, T>::operator()(int row, int column) const { Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N); - return T(m[column][row]); + return m[column][row]; } -template <int N, int M, typename T, typename InnerT> -Q_INLINE_TEMPLATE InnerT& QGenericMatrix<N, M, T, InnerT>::operator()(int row, int column) +template <int N, int M, typename T> +Q_INLINE_TEMPLATE T& QGenericMatrix<N, M, T>::operator()(int row, int column) { Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N); return m[column][row]; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T, InnerT>::isIdentity() const +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::isIdentity() const { for (int col = 0; col < N; ++col) { for (int row = 0; row < M; ++row) { @@ -163,8 +163,8 @@ Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T, InnerT>::isIdentity() const return true; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T, InnerT>::setIdentity() +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setIdentity() { for (int col = 0; col < N; ++col) { for (int row = 0; row < M; ++row) { @@ -176,51 +176,50 @@ Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T, InnerT>::setIdentity() } } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T, InnerT>::fill(T value) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::fill(T value) { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) m[col][row] = value; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<M, N, T, InnerT> QGenericMatrix<N, M, T, InnerT>::transposed() const +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<M, N, T> QGenericMatrix<N, M, T>::transposed() const { - QGenericMatrix<M, N, T, InnerT> result(1); + QGenericMatrix<M, N, T> result(1); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[row][col] = m[col][row]; return result; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT>& QGenericMatrix<N, M, T, InnerT>::operator+=(const QGenericMatrix<N, M, T, InnerT>& other) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator+=(const QGenericMatrix<N, M, T>& other) { for (int index = 0; index < N * M; ++index) m[0][index] += other.m[0][index]; return *this; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT>& QGenericMatrix<N, M, T, InnerT>::operator-=(const QGenericMatrix<N, M, T, InnerT>& other) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator-=(const QGenericMatrix<N, M, T>& other) { for (int index = 0; index < N * M; ++index) m[0][index] -= other.m[0][index]; return *this; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT>& QGenericMatrix<N, M, T, InnerT>::operator*=(T factor) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator*=(T factor) { - InnerT f(factor); for (int index = 0; index < N * M; ++index) - m[0][index] *= f; + m[0][index] *= factor; return *this; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T, InnerT>::operator==(const QGenericMatrix<N, M, T, InnerT>& other) const +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator==(const QGenericMatrix<N, M, T>& other) const { for (int index = 0; index < N * M; ++index) { if (m[0][index] != other.m[0][index]) @@ -229,8 +228,8 @@ Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T, InnerT>::operator==(const QGen return true; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T, InnerT>::operator!=(const QGenericMatrix<N, M, T, InnerT>& other) const +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator!=(const QGenericMatrix<N, M, T>& other) const { for (int index = 0; index < N * M; ++index) { if (m[0][index] != other.m[0][index]) @@ -239,40 +238,39 @@ Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T, InnerT>::operator!=(const QGen return false; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT>& QGenericMatrix<N, M, T, InnerT>::operator/=(T divisor) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator/=(T divisor) { - InnerT d(divisor); for (int index = 0; index < N * M; ++index) - m[0][index] /= d; + m[0][index] /= divisor; return *this; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT> operator+(const QGenericMatrix<N, M, T, InnerT>& m1, const QGenericMatrix<N, M, T, InnerT>& m2) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator+(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2) { - QGenericMatrix<N, M, T, InnerT> result(1); + QGenericMatrix<N, M, T> result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = m1.m[0][index] + m2.m[0][index]; return result; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT> operator-(const QGenericMatrix<N, M, T, InnerT>& m1, const QGenericMatrix<N, M, T, InnerT>& m2) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2) { - QGenericMatrix<N, M, T, InnerT> result(1); + QGenericMatrix<N, M, T> result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = m1.m[0][index] - m2.m[0][index]; return result; } -template <int N, int M1, int M2, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<M1, M2, T, InnerT> operator*(const QGenericMatrix<N, M2, T, InnerT>& m1, const QGenericMatrix<M1, N, T, InnerT>& m2) +template <int N, int M1, int M2, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<M1, M2, T> operator*(const QGenericMatrix<N, M2, T>& m1, const QGenericMatrix<M1, N, T>& m2) { - QGenericMatrix<M1, M2, T, InnerT> result(1); + QGenericMatrix<M1, M2, T> result(1); for (int row = 0; row < M2; ++row) { for (int col = 0; col < M1; ++col) { - InnerT sum(0.0f); + T sum(0.0f); for (int j = 0; j < N; ++j) sum += m1.m[j][row] * m2.m[col][j]; result.m[col][row] = sum; @@ -281,47 +279,44 @@ Q_OUTOFLINE_TEMPLATE QGenericMatrix<M1, M2, T, InnerT> operator*(const QGenericM return result; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT> operator-(const QGenericMatrix<N, M, T, InnerT>& matrix) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& matrix) { - QGenericMatrix<N, M, T, InnerT> result(1); + QGenericMatrix<N, M, T> result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = -matrix.m[0][index]; return result; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT> operator*(T factor, const QGenericMatrix<N, M, T, InnerT>& matrix) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(T factor, const QGenericMatrix<N, M, T>& matrix) { - InnerT f(factor); - QGenericMatrix<N, M, T, InnerT> result(1); + QGenericMatrix<N, M, T> result(1); for (int index = 0; index < N * M; ++index) - result.m[0][index] = matrix.m[0][index] * f; + result.m[0][index] = matrix.m[0][index] * factor; return result; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT> operator*(const QGenericMatrix<N, M, T, InnerT>& matrix, T factor) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(const QGenericMatrix<N, M, T>& matrix, T factor) { - InnerT f(factor); - QGenericMatrix<N, M, T, InnerT> result(1); + QGenericMatrix<N, M, T> result(1); for (int index = 0; index < N * M; ++index) - result.m[0][index] = matrix.m[0][index] * f; + result.m[0][index] = matrix.m[0][index] * factor; return result; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T, InnerT> operator/(const QGenericMatrix<N, M, T, InnerT>& matrix, T divisor) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator/(const QGenericMatrix<N, M, T>& matrix, T divisor) { - InnerT d(divisor); - QGenericMatrix<N, M, T, InnerT> result(1); + QGenericMatrix<N, M, T> result(1); for (int index = 0; index < N * M; ++index) - result.m[0][index] = matrix.m[0][index] / d; + result.m[0][index] = matrix.m[0][index] / divisor; return result; } -template <int N, int M, typename T, typename InnerT> -Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T, InnerT>::toValueArray(T *values) +template <int N, int M, typename T> +Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::toValueArray(T *values) { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) @@ -329,22 +324,22 @@ Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T, InnerT>::toValueArray(T *value } // Define aliases for the useful variants of QGenericMatrix. -typedef QGenericMatrix<2, 2, qreal, float> QMatrix2x2; -typedef QGenericMatrix<2, 3, qreal, float> QMatrix2x3; -typedef QGenericMatrix<2, 4, qreal, float> QMatrix2x4; -typedef QGenericMatrix<3, 2, qreal, float> QMatrix3x2; -typedef QGenericMatrix<3, 3, qreal, float> QMatrix3x3; -typedef QGenericMatrix<3, 4, qreal, float> QMatrix3x4; -typedef QGenericMatrix<4, 2, qreal, float> QMatrix4x2; -typedef QGenericMatrix<4, 3, qreal, float> QMatrix4x3; +typedef QGenericMatrix<2, 2, qreal> QMatrix2x2; +typedef QGenericMatrix<2, 3, qreal> QMatrix2x3; +typedef QGenericMatrix<2, 4, qreal> QMatrix2x4; +typedef QGenericMatrix<3, 2, qreal> QMatrix3x2; +typedef QGenericMatrix<3, 3, qreal> QMatrix3x3; +typedef QGenericMatrix<3, 4, qreal> QMatrix3x4; +typedef QGenericMatrix<4, 2, qreal> QMatrix4x2; +typedef QGenericMatrix<4, 3, qreal> QMatrix4x3; #ifndef QT_NO_DEBUG_STREAM -template <int N, int M, typename T, typename InnerT> -QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T, InnerT> &m) +template <int N, int M, typename T> +QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T> &m) { dbg.nospace() << "QGenericMatrix<" << N << ", " << M - << ", " << QTypeInfo<T>::name() << ", " << QTypeInfo<InnerT>::name() + << ", " << QTypeInfo<T>::name() << ">(" << endl << qSetFieldWidth(10); for (int row = 0; row < M; ++row) { for (int col = 0; col < N; ++col) |