GlmSharp
GlmSharp copied to clipboard
mat4 ortho
I'm getting a different ortho result from MonoGame than GlmSharp, this is the code from MonoGame's Matrix
public static void CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, out Matrix result)
{
result.M11 = (float)(2.0 / ((double)right - (double)left));
result.M12 = 0.0f;
result.M13 = 0.0f;
result.M14 = 0.0f;
result.M21 = 0.0f;
result.M22 = (float)(2.0 / ((double)top - (double)bottom));
result.M23 = 0.0f;
result.M24 = 0.0f;
result.M31 = 0.0f;
result.M32 = 0.0f;
result.M33 = (float)(1.0 / ((double)zNearPlane - (double)zFarPlane));
result.M34 = 0.0f;
result.M41 = (float)(((double)left + (double)right) / ((double)left - (double)right));
result.M42 = (float)(((double)top + (double)bottom) / ((double)bottom - (double)top));
result.M43 = (float)((double)zNearPlane / ((double)zNearPlane - (double)zFarPlane));
result.M44 = 1.0f;
}
As you can see 33 (or 22 in glm) is 1f / (zNearPlane - zFarPlane);
and not 2f / (zNearPlane - zFarPlane)
. Is there any reason to this, since its causing a rendering issue porting from monogame to glm.
glm
public static mat4 Ortho(float left, float right, float bottom, float top, float zNear, float zFar)
{
var m = Identity;
m.m00 = 2/(right - left);
m.m11 = 2/(top - bottom);
m.m22 = - 2/(zFar - zNear);
m.m30 = - (right + left)/(right - left);
m.m31 = - (top + bottom)/(top - bottom);
m.m32 = - (zFar + zNear)/(zFar - zNear);
return m;
}
and for reference looking at glm (cpp)
if GLM_DEPTH_CLIP_SPACE == GLM_DEPTH_ZERO_TO_ONE
Result[2][2] = static_cast<T>(1) / (zFar - zNear);
Result[3][2] = - zNear / (zFar - zNear);
# else
Result[2][2] = static_cast<T>(2) / (zFar - zNear);
Result[3][2] = - (zFar + zNear) / (zFar - zNear);
# endif
EDIT: Realized I copied the wrong monogame code
Since I'm terrible at math and have basically no idea what I'm doing when it comes to matrices this can probably be closed to user error.
- It seems that if I use Ortho without zNear and zFar I get the same results as if I pass in zNear = 0 and zFar = -1 to xna.
- For whatever reason, even though Xna.Matrix has an overload for (row1, row2, row3, row4) it requires me to pass in Column0-4 of glm to convert to the correct Xna Matrix.