I'm trying to make my own column-major matrix class, but I cannot figure out why the results of multiplication operation don't match the results when I perform the same multiplication using glm. Could you please help me?
Here is what I am multiplying:
Here are the results:
Here is the source code for my matrix class:
Here is what I am multiplying:
C++:
ZML::Mat4 mat = {
{75, 85, 93, 100},
{57, 12, 83, 15},
{85, 71, 96, 54},
{76, 82, 19, 56}
};
glm::mat4 mat1 = {
{75, 85, 93, 100},
{57, 12, 83, 15},
{85, 71, 96, 54},
{76, 82, 19, 56}
};
ZML::Mat4 rotationX(1.0f);
rotationX[1][1] = cos(45.0f);
rotationX[1][2] = -sin(45.0f);
rotationX[2][1] = sin(45.0f);
rotationX[2][2] = cos(45.0f);
glm::mat4 rotationX1(1.0f);
rotationX1[1][1] = cos(45.0f);
rotationX1[1][2] = -sin(45.0f);
rotationX1[2][1] = sin(45.0f);
rotationX1[2][2] = cos(45.0f);
mat = rotationX * mat;
mat1 = rotationX1 * mat1;
Here are the results:
Code:
mine:
75.000000 -34.481659 121.181740 100.000000
57.000000 -64.321129 53.812565 15.000000
85.000000 -44.388878 110.845062 54.000000
76.000000 26.909233 79.755203 56.000000
glm:
75 123.786 -23.4719 100
57 76.9289 33.3909 15
85 118.985 -9.98324 54
76 59.2436 -59.793 56
Here is the source code for my matrix class:
C++:
namespace ZML
{
template<size_t columnCount, size_t rowCount, typename T>
class Matrix
{
public:
using Scalar = T;
using Column = std::array<Scalar, rowCount>;
Matrix(std::initializer_list<std::initializer_list<T>> init)
{
auto iteratorY = init.begin();
for (int row = 0; row < rowCount; row++, iteratorY++)
{
auto iteratorX = iteratorY->begin();
for (int column = 0; column < columnCount; column++, iteratorX++)
{
m_data[column][row] = *iteratorX;
}
}
}
Matrix(std::array<std::array<T, rowCount>, columnCount> array)
{
std::copy(array.begin(), array.end(), m_data);
}
Matrix()
:m_columnCount(columnCount), m_rowCount(rowCount), m_data()
{
};
Matrix(Scalar value)
:m_columnCount(columnCount), m_rowCount(rowCount), m_data()
{
//identity matrix
if (value == 1 and columnCount == rowCount)
{
for (int i = 0; i < columnCount; i++)
{
m_data = 1;
}
return;
}
for (int column = 0; column < columnCount; column++)
{
for (int row = 0; row < rowCount; row++)
{
m_data[column][row] = value;
}
}
}
Matrix operator*(const Matrix& mat) const
{
Matrix result(0.0f);
for (size_t col = 0; col < columnCount; col++)
{
for (size_t row = 0; row < rowCount; row++)
{
float x = 0;
for (size_t pos = 0; pos < columnCount; pos++)
{
x += m_data[col][pos] * mat[pos][row];
}
result[col][row] = x;
}
}
return result;
}
operator std::string() const
{
std::string result;
for (int y = 0; y < rowCount; y++)
{
for (int x = 0; x < columnCount; x++)
{
result += std::to_string(m_data[x][y]) + " ";
}
result += "\n";
}
return result;
}
Column& operator[](int index)
{
return m_data[index];
}
const Column& operator[](int index) const
{
return m_data[index];
}
private:
std::array<Column, columnCount> m_data;
size_t m_columnCount = columnCount, m_rowCount = rowCount;
};
template<typename Scalar>
using Mat4 = Matrix<4, 4, Scalar>;
using Mat4f = Mat4<float>;
using Mat4d = Mat4<double>;
using Mat4i = Mat4<int>;
}