Browse Source

Math: make Matrix4::transformPoint() work with projection matrices.

Dividing by w instead of ignoring it.
pull/166/head
Vladimír Vondruš 10 years ago
parent
commit
41253de1d5
  1. 5
      src/Magnum/Math/Matrix4.h
  2. 10
      src/Magnum/Math/Test/Matrix4Test.cpp

5
src/Magnum/Math/Matrix4.h

@ -447,13 +447,14 @@ template<class T> class Matrix4: public Matrix4x4<T> {
*
* Unlike in @ref transformVector(), translation is also involved in
* the transformation. @f[
* \boldsymbol v' = \boldsymbol M \begin{pmatrix} v_x \\ v_y \\ v_z \\ 1 \end{pmatrix}
* \boldsymbol v' = v''_{xyz} / v''_w ~~~~~~~~~~ \boldsymbol v'' = \begin{pmatrix} v''_x \\ v''_y \\ v''_z \\ v''_w \end{pmatrix} = \boldsymbol M \begin{pmatrix} v_x \\ v_y \\ v_z \\ 1 \end{pmatrix} \\
* @f]
* @see @ref DualQuaternion::transformPoint(),
* @ref Matrix3::transformPoint()
*/
Vector3<T> transformPoint(const Vector3<T>& vector) const {
return ((*this)*Vector4<T>(vector, T(1))).xyz();
const Vector4<T> transformed{(*this)*Vector4<T>(vector, T(1))};
return transformed.xyz()/transformed.w();
}
MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(4, 4, Matrix4<T>)

10
src/Magnum/Math/Test/Matrix4Test.cpp

@ -95,6 +95,7 @@ struct Matrix4Test: Corrade::TestSuite::Tester {
void vectorParts();
void invertedRigid();
void transform();
void transformProjection();
void debug();
void configuration();
@ -143,6 +144,7 @@ Matrix4Test::Matrix4Test() {
&Matrix4Test::vectorParts,
&Matrix4Test::invertedRigid,
&Matrix4Test::transform,
&Matrix4Test::transformProjection,
&Matrix4Test::debug,
&Matrix4Test::configuration});
@ -546,6 +548,14 @@ void Matrix4Test::transform() {
CORRADE_COMPARE(a.transformPoint(v), Vector3(3.0f, -4.0f, 9.0f));
}
void Matrix4Test::transformProjection() {
Matrix4 a = Matrix4::perspectiveProjection({2.0f, 2.0f}, 1.0f, 100.0f);
Vector3 v{0.0f, 0.0f, -100.0f};
CORRADE_COMPARE(a.transformVector(v), Vector3(0.0f, 0.0f, 0.0f));
CORRADE_COMPARE(a.transformPoint(v), Vector3(0.0f, 0.0f, 1.0f));
}
void Matrix4Test::lookAt() {
Matrix4 a = Matrix4::lookAt({0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f},

Loading…
Cancel
Save