c# - 3D 图形 - 矩阵数学不起作用

标签 c# math matrix 3d

我是 3D 图形的新手,所以我认为我做错了一些根本性的错误。我构建了 4 个矩阵来处理 3D 空间中的对象,一个缩放矩阵、平移矩阵、透视矩阵和旋转矩阵。

旋转和缩放矩阵工作正常,但是我的透视和平移矩阵没有按预期运行,根据我的理解,平移和透视矩阵看起来像:

Translation      Perspective 
[1, 0, 0, x]    [1, 0,  0 , 0]
[0, 1, 0, y]    [0, 1,  0 , 1]
[0, 0, 1, z]    [0, 0,  1 , 0]
[0, 0, 0, 1]    [0, 0, 1/D, 1]

为了理解幕后的一切工作原理,我正在编写自己的数学库 - 为了创建上面定义的矩阵,我有以下方法:

public static ASMATRIX4 CreatePerspectiveMatrix(double focalLength)
{
    // Create matrix as an identity matrix
    var m = new ASMATRIX4();

    // Set the perspective matrix 
    m.Matrix[3].Points[2] = 1 / focalLength;
    m.Matrix[1].Points[3] = 1;

    return m;
}

public static ASMATRIX4 CreateTranslationMatrix(double x, double y, double z)
{
    // reset to identity matrix
    var m = new ASMATRIX4();

    // Set the translation matrix
    m.Matrix[0].Points[3] = x;
    m.Matrix[1].Points[3] = y;
    m.Matrix[2].Points[3] = z;

   return m;
}

没什么太复杂的,但是当我将我的矩阵相乘时,什么也没有发生——我可以绕任何轴旋转,但我不能平移。还要注意上面的 new ASMATRIX4() 将一个新的 4x4 矩阵初始化为单位矩阵。

为了将我的矩阵相乘,我执行以下操作:

m_scaling = ASMATRIX4.CreateScalingMatrix(s, s, s);
m_translation = ASMATRIX4.CreateTranslationMatrix(tX, tY, tZ);
m_perspective = ASMATRIX4.CreatePerspectiveMatrix(f);
m_rotation = ASMATRIX4.RotateByDegrees(rX, rY, rZ);

var transformationMatrix = m_scaling*m_translation*m_perspective*m_rotation;

我还写了一个运算符重载来一起处理乘法矩阵:

public static ASMATRIX4 operator *(ASMATRIX4 mA, ASMATRIX4 mB)
{
    //  Creates a new identity matrix
    var m = new ASMATRIX4();

    // Multiply the two matrixes together and return the output matrix
    for (var i = 0; i < 4; i++)
    {
        for (var j = 0; j < 4; j++)
        {
            m.Matrix[i].Points[j] =
                (mB.Matrix[i].Points[0]*mA.Matrix[0].Points[j]) +
                (mB.Matrix[i].Points[1]*mA.Matrix[1].Points[j]) +
                (mB.Matrix[i].Points[2]*mA.Matrix[2].Points[j]) +
                (mB.Matrix[i].Points[3]*mA.Matrix[3].Points[j])
        }
    }
    return m;
 }

最后,为了实际转换我的网格,我有一个面数组,我将其绘制到 2D 渲染上下文,下面的方法通过上面定义的转换矩阵转换每个面。

private void TransformMesh()
{
    var transformationMatrix = m_scaling*m_translation*m_perspective*m_rotation;
    for (var i = 0; i < m_numFaces; i++)
    {     
        m_meshData[i] = m_meshData[i].TransformFace(transformationMatrix);
    }
}

TransformFace 方法如下所述:

public ASFace TransformFace(ASMATRIX4 matrix)
{
    var transformedFace = new ASFace();

    for (var i = 0; i < 3; i++)
    {
        transformedFace.m_points[i] = m_points[i].TransformVector(matrix);
        transformedFace.m_points[i] = transformedFace.m_points[i].Rescale();
    }

    return transformedFace;
 }

循环中的第一个方法通过输入矩阵变换齐次点,然后调用rescale方法对向量进行归一化。

我对我做错了什么感到困惑?我认为矩阵相乘的运算顺序可能是错误的,但我不太确定,如有任何建议,我们将不胜感激。

最佳答案

我不是这方面的专家,但你是不是漏掉了一行/一列?

-- 已编辑(tnx andand)

public static ASMATRIX4 operator *(ASMATRIX4 mA, ASMATRIX4 mB)
{
    //  Creates a new identity matrix
    var m = new ASMATRIX4();

    // Multiply the two matrixes together and return the output matrix
    for (var i = 0; i < 4; i++)
    {
        for (var j = 0; j < 4; j++)
        {
            m.Matrix[i].Points[j] =
                (mB.Matrix[i].Points[0]*mA.Matrix[0].Points[j]) +
                (mB.Matrix[i].Points[1]*mA.Matrix[1].Points[j]) +
                (mB.Matrix[i].Points[2]*mA.Matrix[2].Points[j]) +
                (mB.Matrix[i].Points[3]*mA.Matrix[3].Points[j]);   //  <----
        }
    }
    return m;
}

关于c# - 3D 图形 - 矩阵数学不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29802537/

相关文章:

c# - 在 WCF 服务中读取 XML 文件并从服务返回 XML

c# - 强制转换 C# 参数?

math - 我应该如何订购这些 "helpful"分数?

wpf - 数学(在 WPF 中): Getting new x, 平移后的 y 坐标

ruby - 在 Processing 中渲染 Polar Zonohedron 时出现问题

c# - Web API 2 中非常基本的不记名 token 身份验证和授权

C# 服务器 - TCP/IP 套接字效率

arrays - R:整数字符串矩阵到整数计数数组

c - 将特定格式的文本文件读入c中的二维数组/矩阵

c - C 中任意数据类型的矩阵运算