我是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;
}
最后,为了实际转换我的网格,我有一个Faces数组,我绘制到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方法对矢量进行归一化。
我对自己做错了什么感到困惑?我认为乘法运算的顺序可能是错误的,但我不太确定,任何建议都会受到高度赞赏。
答案 0 :(得分:4)
我不是这方面的专家,但你不是在这里错过了一行/列吗?
- EDITED(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;
}
答案 1 :(得分:3)
我发布的代码中有两个问题。首先,您的CreatePerspectiveMatrix方法不会将(1,3)元素设置为1;透视矩阵的方法或初始定义是错误的。 (或两者兼而有之!)其次,在乘法代码中,您似乎只是将前三行和列的子矩阵相乘。由于这些子矩阵最初是单位矩阵,所以没有任何事情发生并不令人惊讶。