QMatrix4x4 Class
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space. More...
Header: | #include <QMatrix4x4> |
qmake: | QT += gui |
Since: | Qt 4.6 |
This class was introduced in Qt 4.6.
Public Functions
QMatrix4x4(const QMatrix &matrix) | |
QMatrix4x4(const QTransform &transform) | |
QMatrix4x4(const float *values) | |
void | copyDataTo(float *values) const |
double | determinant() const |
void | frustum(float left, float right, float bottom, float top, float nearPlane, float farPlane) |
QMatrix4x4 | inverted(bool *invertible = nullptr) const |
void | lookAt(const QVector3D &eye, const QVector3D ¢er, const QVector3D &up) |
QRect | mapRect(const QRect &rect) const |
QRectF | mapRect(const QRectF &rect) const |
QMatrix3x3 | normalMatrix() const |
void | optimize() |
void | ortho(float left, float right, float bottom, float top, float nearPlane, float farPlane) |
void | ortho(const QRect &rect) |
void | ortho(const QRectF &rect) |
void | perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane) |
void | rotate(float angle, const QVector3D &vector) |
void | rotate(float angle, float x, float y, float z = 0.0f) |
void | rotate(const QQuaternion &quaternion) |
void | scale(const QVector3D &vector) |
void | scale(float x, float y) |
void | scale(float x, float y, float z) |
void | scale(float factor) |
QMatrix | toAffine() const |
QTransform | toTransform() const |
QTransform | toTransform(float distanceToPlane) const |
void | translate(const QVector3D &vector) |
void | translate(float x, float y) |
void | translate(float x, float y, float z) |
QMatrix4x4 | transposed() const |
void | viewport(float left, float bottom, float width, float height, float nearPlane = 0.0f, float farPlane = 1.0f) |
QVariant | operator QVariant() const |
QMatrix4x4 & | operator/=(float divisor) |
Related Non-Members
QMatrix4x4 | operator/(const QMatrix4x4 &matrix, float divisor) |
Detailed Description
The QMatrix4x4 class in general is treated as a row-major matrix, in that the constructors and operator() functions take data in row-major format, as is familiar in C-style usage.
Internally the data is stored as column-major format, so as to be optimal for passing to OpenGL functions, which expect column-major data.
When using these functions be aware that they return data in column-major format:
- data()
- constData()
See also QVector3D and QGenericMatrix.
Member Function Documentation
QMatrix4x4::QMatrix4x4(const QMatrix &matrix)
Constructs a 4x4 matrix from a conventional Qt 2D affine transformation matrix.
If matrix has a special type (identity, translate, scale, etc), the programmer should follow this constructor with a call to optimize() if they wish QMatrix4x4 to optimize further calls to translate(), scale(), etc.
See also toAffine() and optimize().
QMatrix4x4::QMatrix4x4(const QTransform &transform)
Constructs a 4x4 matrix from the conventional Qt 2D transformation matrix transform.
If transform has a special type (identity, translate, scale, etc), the programmer should follow this constructor with a call to optimize() if they wish QMatrix4x4 to optimize further calls to translate(), scale(), etc.
See also toTransform() and optimize().
QMatrix4x4::QMatrix4x4(const float *values)
Constructs a matrix from the given 16 floating-point values. The contents of the array values is assumed to be in row-major order.
If the matrix has a special type (identity, translate, scale, etc), the programmer should follow this constructor with a call to optimize() if they wish QMatrix4x4 to optimize further calls to translate(), scale(), etc.
See also copyDataTo() and optimize().
void QMatrix4x4::copyDataTo(float *values) const
Retrieves the 16 items in this matrix and copies them to values in row-major order.
double QMatrix4x4::determinant() const
Returns the determinant of this matrix.
void QMatrix4x4::frustum(float left, float right, float bottom, float top, float nearPlane, float farPlane)
Multiplies this matrix by another that applies a perspective frustum projection for a window with lower-left corner (left, bottom), upper-right corner (right, top), and the specified nearPlane and farPlane clipping planes.
See also ortho() and perspective().
QMatrix4x4 QMatrix4x4::inverted(bool *invertible = nullptr) const
Returns the inverse of this matrix. Returns the identity if this matrix cannot be inverted; i.e. determinant() is zero. If invertible is not null, then true will be written to that location if the matrix can be inverted; false otherwise.
If the matrix is recognized as the identity or an orthonormal matrix, then this function will quickly invert the matrix using optimized routines.
See also determinant() and normalMatrix().
void QMatrix4x4::lookAt(const QVector3D &eye, const QVector3D ¢er, const QVector3D &up)
Multiplies this matrix by a viewing matrix derived from an eye point. The center value indicates the center of the view that the eye is looking at. The up value indicates which direction should be considered up with respect to the eye.
Note: The up vector must not be parallel to the line of sight from eye to center.
QRect QMatrix4x4::mapRect(const QRect &rect) const
Maps rect by multiplying this matrix by the corners of rect and then forming a new rectangle from the results. The returned rectangle will be an ordinary 2D rectangle with sides parallel to the horizontal and vertical axes.
See also map().
QRectF QMatrix4x4::mapRect(const QRectF &rect) const
Maps rect by multiplying this matrix by the corners of rect and then forming a new rectangle from the results. The returned rectangle will be an ordinary 2D rectangle with sides parallel to the horizontal and vertical axes.
See also map().
QMatrix3x3 QMatrix4x4::normalMatrix() const
Returns the normal matrix corresponding to this 4x4 transformation. The normal matrix is the transpose of the inverse of the top-left 3x3 part of this 4x4 matrix. If the 3x3 sub-matrix is not invertible, this function returns the identity.
See also inverted().
void QMatrix4x4::optimize()
Optimize the usage of this matrix from its current elements.
Some operations such as translate(), scale(), and rotate() can be performed more efficiently if the matrix being modified is already known to be the identity, a previous translate(), a previous scale(), etc.
Normally the QMatrix4x4 class keeps track of this special type internally as operations are performed. However, if the matrix is modified directly with {QLoggingCategory::operator()}{operator()()} or data(), then QMatrix4x4 will lose track of the special type and will revert to the safest but least efficient operations thereafter.
By calling optimize() after directly modifying the matrix, the programmer can force QMatrix4x4 to recover the special type if the elements appear to conform to one of the known optimized types.
See also operator()(), data(), and translate().
void QMatrix4x4::ortho(float left, float right, float bottom, float top, float nearPlane, float farPlane)
Multiplies this matrix by another that applies an orthographic projection for a window with lower-left corner (left, bottom), upper-right corner (right, top), and the specified nearPlane and farPlane clipping planes.
See also frustum() and perspective().
void QMatrix4x4::ortho(const QRect &rect)
This is an overloaded function.
Multiplies this matrix by another that applies an orthographic projection for a window with boundaries specified by rect. The near and far clipping planes will be -1 and 1 respectively.
See also frustum() and perspective().
void QMatrix4x4::ortho(const QRectF &rect)
This is an overloaded function.
Multiplies this matrix by another that applies an orthographic projection for a window with boundaries specified by rect. The near and far clipping planes will be -1 and 1 respectively.
See also frustum() and perspective().
void QMatrix4x4::perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane)
Multiplies this matrix by another that applies a perspective projection. The vertical field of view will be verticalAngle degrees within a window with a given aspectRatio that determines the horizontal field of view. The projection will have the specified nearPlane and farPlane clipping planes which are the distances from the viewer to the corresponding planes.
See also ortho() and frustum().
void QMatrix4x4::rotate(float angle, const QVector3D &vector)
Multiples this matrix by another that rotates coordinates through angle degrees about vector.
See also scale() and translate().
void QMatrix4x4::rotate(float angle, float x, float y, float z = 0.0f)
This is an overloaded function.
Multiplies this matrix by another that rotates coordinates through angle degrees about the vector (x, y, z).
See also scale() and translate().
void QMatrix4x4::rotate(const QQuaternion &quaternion)
Multiples this matrix by another that rotates coordinates according to a specified quaternion. The quaternion is assumed to have been normalized.
See also scale(), translate(), and QQuaternion.
void QMatrix4x4::scale(const QVector3D &vector)
Multiplies this matrix by another that scales coordinates by the components of vector.
See also translate() and rotate().
void QMatrix4x4::scale(float x, float y)
This is an overloaded function.
Multiplies this matrix by another that scales coordinates by the components x, and y.
See also translate() and rotate().
void QMatrix4x4::scale(float x, float y, float z)
This is an overloaded function.
Multiplies this matrix by another that scales coordinates by the components x, y, and z.
See also translate() and rotate().
void QMatrix4x4::scale(float factor)
This is an overloaded function.
Multiplies this matrix by another that scales coordinates by the given factor.
See also translate() and rotate().
QMatrix QMatrix4x4::toAffine() const
Returns the conventional Qt 2D affine transformation matrix that corresponds to this matrix. It is assumed that this matrix only contains 2D affine transformation elements.
See also toTransform().
QTransform QMatrix4x4::toTransform() const
Returns the conventional Qt 2D transformation matrix that corresponds to this matrix.
The returned QTransform is formed by simply dropping the third row and third column of the QMatrix4x4. This is suitable for implementing orthographic projections where the z co-ordinate should be dropped rather than projected.
See also toAffine().
QTransform QMatrix4x4::toTransform(float distanceToPlane) const
Returns the conventional Qt 2D transformation matrix that corresponds to this matrix.
If distanceToPlane is non-zero, it indicates a projection factor to use to adjust for the z co-ordinate. The value of 1024 corresponds to the projection factor used by QTransform::rotate() for the x and y axes.
If distanceToPlane is zero, then the returned QTransform is formed by simply dropping the third row and third column of the QMatrix4x4. This is suitable for implementing orthographic projections where the z co-ordinate should be dropped rather than projected.
See also toAffine().
void QMatrix4x4::translate(const QVector3D &vector)
Multiplies this matrix by another that translates coordinates by the components of vector.
See also scale() and rotate().
void QMatrix4x4::translate(float x, float y)
This is an overloaded function.
Multiplies this matrix by another that translates coordinates by the components x, and y.
See also scale() and rotate().
void QMatrix4x4::translate(float x, float y, float z)
This is an overloaded function.
Multiplies this matrix by another that translates coordinates by the components x, y, and z.
See also scale() and rotate().
QMatrix4x4 QMatrix4x4::transposed() const
Returns this matrix, transposed about its diagonal.
void QMatrix4x4::viewport(float left, float bottom, float width, float height, float nearPlane = 0.0f, float farPlane = 1.0f)
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 (left, bottom, nearPlane) and with size (width, height, farPlane - nearPlane).
This matches the transform used by the fixed function OpenGL viewport transform controlled by the functions glViewport() and glDepthRange().
QVariant QMatrix4x4::operator QVariant() const
Returns the matrix as a QVariant.
QMatrix4x4 &QMatrix4x4::operator/=(float divisor)
This is an overloaded function.
Divides all elements of this matrix by divisor.