2
\$\begingroup\$

I'm trying to double check my math in my projection matrices. I'm thinking about "unit tests" when I ask this. I'm looking for 4 or 5 unit tests that would be appropriate to give one confidence that the projections are correct.

This is a little more than an obvious question. Let me explain... When I did this with a quaternion, the potential tests were very logical. The perspective transforms by comparison are somewhat non-intuitive. For a quaternion, if I start with the unit Y vector {0.0, 1.0, 0.0} and I rotate it a quarter turn about the x-axis, I know that the answer is the unit vector in the negative z direction. It's a logical answer. Is there something equally logical/obvious for orthographic and perspective projections?

I've gone through many math books and many tutorials over the years. I'd like to once and for all "prove" that my ortho and perspective matrices are always projecting correctly.

If I composed an ortho matrix using left, right, bottom, top, near, far as follows:

              0.0f, 1.0f,
              0.0f, 1.0f,
              0.0f, 1.0f

I might get the following matrix (right-handed / OpenGL style)

  2.000000,   0.000000,   0.000000,  -1.000000
  0.000000,   2.000000,   0.000000,  -1.000000
  0.000000,   0.000000,  -2.000000,  -1.000000
  0.000000,   0.000000,   0.000000,   1.000000

I'd like to know if that matrix is computed correctly.

Then for a test, I'd multiply a vertex such as {1.0f, 1.0f, 0.0f} against the matrix and get back {1.0f, 1.0f, -1.0f}. Is it correct? ...and I'd like to do that on several standard points.

I'm having trouble visualizing "standard" or "logical" tests for this. Something that at a glance, just makes sense. (like the quaternion example above)

\$\endgroup\$
2
\$\begingroup\$

A few ideas:

  • Any point in front of the camera along its central axis should get mapped to (0, 0) after the perspective division.

  • Any point selected on / outside the camera's view frustum/prism should map, after perspective division, to a value of 1 or greater, or -1 or less on at least one of the x, y, z axes.

Beyond that, you could use some linear algebra first principles to form the projection ray from an arbitrary point toward the camera, intersect this line with the image plane, and verify that the coordinates of the intersection point in the plane's local frame match the projected coordinates provided by your matrix.

I'll leave this answer as a community wiki so it's easy to expand on this initial sketch.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.