PDA

View Full Version : orthographic projection matrix in GLTools



shapeare
02-16-2012, 07:36 PM
I am using the library GLTools that comes with the book OpenGL SuperBible 5.

The orthographic projection building function provided by this library is (in column-major order):


void m3dMakeOrthographicMatrix(M3DMatrix44f mProjection, float xMin, float xMax,
float yMin, float yMax, float zMin, float zMax)
{
m3dLoadIdentity44(mProjection);

mProjection[0] = 2.0f / (xMax - xMin);
mProjection[5] = 2.0f / (yMax - yMin);
mProjection[10] = -2.0f / (zMax - zMin);
mProjection[12] = -((xMax + xMin)/(xMax - xMin));
mProjection[13] = -((yMax + yMin)/(yMax - yMin));
mProjection[14] = -((zMax + zMin)/(zMax - zMin));
mProjection[15] = 1.0f;
}

I am a little confused why mProjection[10] starts with a subtraction sign.

While referring to the book Fundamentals of Computer Graphics third edition, in section 7.1.2 The Orthographic Projection Transformation, the corresponding value to mProjection[10] is given as 2/(n-f), and mProjection[14] is given as -((n + f)/(n - f)),
that is something like:


m3dLoadIdentity44(mProjection);

mProjection[0] = 2.0f / (r-l);
mProjection[5] = 2.0f / (t-b);
mProjection[10] = 2.0f / (n-f);
mProjection[12] = -((r+l)/(r-l));
mProjection[13] = -((t+b)/(t-b));
mProjection[14] = -((n+f)/(n-f));
mProjection[15] = 1.0f;

where
l the left plane, r the right plane,
b the bottom plane, t the top plane,
n the near plane, f the far plane.

Personally, either must be wrong. But which one?

Alfonse Reinheart
02-16-2012, 08:43 PM
Personally, either must be wrong. But which one?

Both. They're the same. Well, the first one is.

Algebra tells us:



2/(n-f) = 2/(-(f-n)) = -2/(f-n) = -2/(zMax-zMin)


Since `zMin` is near and `zMax` is far, that's exactly what you should get.

The second one is wrong in the GLTools. But since it's wrong in the translation of the Z, and that's rarely used in orthographic projections, the guy who wrote it probably never used it.

That's why it's a good idea to use a reputable math library, like GLM (http://glm.g-truc.net/) or tvmet (http://tvmet.sourceforge.net/) or something. "GLTools" isn't really intended to be used outside of the Superbible. Not that you can't of course, but it was written for exactly and only that purpose.