开发者

I need a formula for a right-handed Orthographic and Perspective projection matrices

I'm doing a 3D graphics "untextured cubes on a blue screen" test using right-handed coordinates. However, they come out strangely clipped or distorted (in Orthographic, the left side of the display ends before the window does; in Perspective, it looks like the display stretches from back-left to forward-right).

Not sure what the problem actually is, but the projection matrices seem a good place to start.

The ones I'm using now:

    // Went back to where I found it and found out this is a 
    // left-handed projection. Oops!
    public static Matrix4x4 Orthographic(float width, float height,
        float near, float far)
    {
        float farmnear = far - near;
        return new Matrix4x4(
            2 / width, 0, 0, 0,
            0, 2 / height, 0, 0,
            0, 0, 1 / farmnear, -near / farmnear,
            0, 0, 0, 1
            );
    }


    // Copied from a previous project.
    public static Matrix4x4 PerspectiveFov(float fov, float aspect, 
  开发者_运维问答      float near, float far)
    {
        float yScale = 1.0F / (float)Math.Tan(fov / 2);
        float xScale = yScale / aspect;
        float farmnear = far - near;
        return new Matrix4x4(
            xScale, 0, 0, 0,
            0, yScale, 0, 0,
            0, 0, far / (farmnear), 1,
            0, 0, -near * far / (farmnear), 1
            );
    }

Thanks


Solved - Thanks for all your help. It wasn't the projection matrices; it was a single mis-placed variable in the LookAt/View matrix. Everything displays correctly now that it's fixed.

    public static Matrix4x4 LookAt(Vector3 eye, Vector3 target, Vector3 up)
    {
        Vector3 zAxis = target; zAxis.Subtract(ref eye); zAxis.Normalize();
        Vector3 xAxis = up; xAxis.Cross(zAxis); xAxis.Normalize();
        Vector3 yAxis = zAxis; yAxis.Cross(xAxis);
        return new Matrix4x4(
            xAxis.X, yAxis.X, zAxis.Z, 0, // There.
            xAxis.Y, yAxis.Y, zAxis.Y, 0,
            xAxis.Z, yAxis.Z, zAxis.Z, 0,
            -xAxis.Dot(eye), -yAxis.Dot(eye), -zAxis.Dot(eye), 1
            );
    }

Oops. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜