Cannot translate a transformation matrix (C++ / OpenGL)

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 🙂

New contributor

NicoTämmler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật