Philip-Trettner / GlmSharp

Open-source semi-generated GLM-flavored math library for .NET/C#
MIT License
47 stars 18 forks source link

mat4 ortho #9

Open xposure opened 7 years ago

xposure commented 7 years ago

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

xposure commented 7 years ago

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.