This is my first question in this forum.
Sorry for my bad English, I am not a native speaker 🙂
I started programming with Java long ago and followed the 3D Java Game Dev series on YT from ThinMatrix. Nowadays I program with C++ and want to build my own little 3D Engine based on this former tutorial. I am at the point where I have to load a transformation matrix into the VertexShader in order to rotate, scale and increase the position of my squad. Therefore I programmed my own Mat4f struct. I already can rotate and scale my squad, the translate() Methode doesn’t work! The quad is not moving but gets scaled at the axis.
Here is the implementation for my Mat4f-struct:
Mat4f::Mat4f()
{
setIdentity();
}
Mat4f::Mat4f(const float data[4][4])
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
m_data[i][j] = data[i][j];
}
void Mat4f::setIdentity()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
m_data[i][j] = (i == j) ? 1.0f : 0.0f;
}
void Mat4f::print() const
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
std::cout << m_data[i][j] << " ";
}
std::cout << std::endl;
}
}
void Mat4f::translate(const Vec3f& translation) {
m_data[0][3] = translation.x;
m_data[1][3] = translation.y;
m_data[2][3] = translation.z;
}
void Mat4f::rotate(float rx, float ry, float rz)
{
float cosX = std::cos(rx);
float sinX = std::sin(rx);
float cosY = std::cos(ry);
float sinY = std::sin(ry);
float cosZ = std::cos(rz);
float sinZ = std::sin(rz);
float rot[4][4] = {
{ cosY * cosZ, -cosX * sinZ + sinX * cosY * sinZ, sinX * cosZ + cosX * cosY * sinZ, 0.0f },
{ cosY * sinZ, cosX * cosZ + sinX * cosY * sinZ, cosX * cosZ - sinX * cosY * sinZ, 0.0f },
{ -sinY, sinX * cosY, cosX * cosY, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f }
};
Mat4f tmp = *this * Mat4f(rot);
*this = tmp;
}
void Mat4f::scale(float scale)
{
float scaledMatrixData[4][4] = {
{scale, 0.f, 0.f, 0.f},
{0.f, scale, 0.f, 0.f},
{0.f, 0.f, scale, 0.f},
{0.f, 0.f, 0.f, 1.f}
};
Mat4f scaledMat(scaledMatrixData);
*this *= scaledMat;
}
Mat4f Mat4f::operator*(const Mat4f& other) const
{
Mat4f result;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
float sum = 0.f;
for (int k = 0; k < 4; k++)
sum += m_data[i][k] * other.m_data[k][j];
result.m_data[i][j] = sum;
}
return result;
}
void Mat4f::operator*=(const Mat4f& other)
{
Mat4f result;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
float sum = 0.f;
for (int k = 0; k < 4; k++) {
sum += m_data[i][k] * other.m_data[k][j];
}
result.m_data[i][j] = sum;
}
}
*this = result;
}
This is the function where I want to create my transformation matrix:
Mat4f createTransformationMatrix(const Vec3f& translation, float rx, float ry, float rz, float scale)
{
Mat4f mat;
mat.translate(translation);
mat.rotate(rx, ry, rz);
mat.scale(scale);
return mat;
}
I load the matrix via glUniformMatrix4fv to the vertex Shader.
As mentioned, the rotation and scaling works just fine!
In the shader I simply multiply: gl_Position = transformationMatrix * vec4(position, 1.0);
I have a strong feeling, that something with the translate()-Method isn’t correct. But even after hours try to figure the error out with the help of AI, I cannot come to a conclusion xD
I am desperated…
Thanks for any help or advice 🙂
NicoTämmler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.