我有以下功能:4x4矩陣前乘VS後乘
void Matrix::Scale(const float xScale, const float yScale, const float zScale)
{
Matrix scaleMatrix;
scaleMatrix.m_data[M11] = xScale;
scaleMatrix.m_data[M22] = yScale;
scaleMatrix.m_data[M33] = zScale;
*this *= scaleMatrix;
}
void Matrix::Translate(const float xTranslation, const float yTranslation, const float zTranslation)
{
Matrix translationMatrix;
translationMatrix.m_data[M14] = xTranslation;
translationMatrix.m_data[M24] = yTranslation;
translationMatrix.m_data[M34] = zTranslation;
*this *= translationMatrix;
}
而且我不確定這兩種功能中的最後幾行。我應該做前乘法還是後乘法(即我現在在做什麼)。它對使用這個類有什麼影響?我使用OpenGL的類,所以任何相似之處可能會有用。
編輯:
我的shader代碼看起來是這樣的:
void main()
{
gl_Position = vec4(v_xy, 0.0, 1.0) * v_ModelMatrix * v_ViewMatrix * v_ProjectionMatrix;
f_uv = v_uv;
}
我的矩陣乘法功能如下所示:
// Row 1
result[M11] = lhs[M11] * rhs[M11] + lhs[M12] * rhs[M21] + lhs[M13] * rhs[M31] + lhs[M14] * rhs[M41]; // Column 1
result[M12] = lhs[M11] * rhs[M12] + lhs[M12] * rhs[M22] + lhs[M13] * rhs[M32] + lhs[M14] * rhs[M42]; // Column 2
result[M13] = lhs[M11] * rhs[M13] + lhs[M12] * rhs[M23] + lhs[M13] * rhs[M33] + lhs[M14] * rhs[M43]; // Column 3
result[M14] = lhs[M11] * rhs[M14] + lhs[M12] * rhs[M24] + lhs[M13] * rhs[M34] + lhs[M14] * rhs[M44]; // Column 4
// Row 2
result[M21] = lhs[M21] * rhs[M11] + lhs[M22] * rhs[M21] + lhs[M23] * rhs[M31] + lhs[M24] * rhs[M41]; // Column 1
result[M22] = lhs[M21] * rhs[M12] + lhs[M22] * rhs[M22] + lhs[M23] * rhs[M32] + lhs[M24] * rhs[M42]; // Column 2
result[M23] = lhs[M21] * rhs[M13] + lhs[M22] * rhs[M23] + lhs[M23] * rhs[M33] + lhs[M24] * rhs[M43]; // Column 3
result[M24] = lhs[M21] * rhs[M14] + lhs[M22] * rhs[M24] + lhs[M23] * rhs[M34] + lhs[M24] * rhs[M44]; // Column 4
// Row 3
result[M31] = lhs[M31] * rhs[M11] + lhs[M32] * rhs[M21] + lhs[M33] * rhs[M31] + lhs[M34] * rhs[M41]; // Column 1
result[M32] = lhs[M31] * rhs[M12] + lhs[M32] * rhs[M22] + lhs[M33] * rhs[M32] + lhs[M34] * rhs[M42]; // Column 2
result[M33] = lhs[M31] * rhs[M13] + lhs[M32] * rhs[M23] + lhs[M33] * rhs[M33] + lhs[M34] * rhs[M43]; // Column 3
result[M34] = lhs[M31] * rhs[M14] + lhs[M32] * rhs[M24] + lhs[M33] * rhs[M34] + lhs[M34] * rhs[M44]; // Column 4
// Row 4
result[M41] = lhs[M41] * rhs[M11] + lhs[M42] * rhs[M21] + lhs[M43] * rhs[M31] + lhs[M44] * rhs[M41]; // Column 1
result[M42] = lhs[M41] * rhs[M12] + lhs[M42] * rhs[M22] + lhs[M43] * rhs[M32] + lhs[M44] * rhs[M42]; // Column 2
result[M43] = lhs[M41] * rhs[M13] + lhs[M42] * rhs[M23] + lhs[M43] * rhs[M33] + lhs[M44] * rhs[M43]; // Column 3
result[M44] = lhs[M41] * rhs[M14] + lhs[M42] * rhs[M24] + lhs[M43] * rhs[M34] + lhs[M44] * rhs[M44]; // Column 4
我的印象是,如果你發佈 - 乘上你的矩陣(即viewMatrix = transform * viewMatrix;
),那麼你的着色器代碼需要按照我現在所用的相反順序應用MVP?
EDIT2:
上http://scratchapixel.com/lessons/3d-basic-lessons/lesson-4-geometry/conventions-again-row-major-vs-column-major-vector/彙總表是混淆了我,因爲我使用的是用OpenGL後乘(指示列爲主),但我的矩陣,在內存佈局爲行主?
這不是一個C++的問題,而只是一個mathmatrix問題。對我而言,這取決於你的慣例:如果你所有的變換都是右乘法,那麼一個比例/平移也應該是一個正確的乘法。 – Synxis
隨着縮放,它沒有區別,因爲縮放只包含對角矩陣。翻譯雖然不同,但它不同。 – arne
@arne它*確實與縮放有差異。左乘以對角線矩陣縮放行,而右乘法縮放列。 –