如果我乘以二维矩阵不起作用?

时间:2013-12-07 11:31:25

标签: opengl matrix glsl transform linear-algebra

我已经创建了5个不同的矩阵,在完成之后我将它们相乘但它不起作用...... 我已经尝试过多种方法来解决这个问题,但没有成功,我已经多次检查了我的数学矩阵的东西,我不认为这是问题,问题是当我把它们加在一起时。

    private Matrix4f getTransformMatrix(){
        Matrix3f translationMatrix = new Matrix3f().initTranslationIdentity(translation.getX(), translation.getY());
        Matrix3f scaleMatrix = new Matrix3f().initScaleIdentity(scale.getX(), scale.getY());
        Matrix3f shearXMatrix = new Matrix3f().initShearXIdentity(shear.getX());
        Matrix3f shearYMatrix = new Matrix3f().initShearYIdentity(shear.getY());
        Matrix3f rotationMatrix = new Matrix3f().initRotationIdentity(rotationAngle);
        return new Matrix4f().translate2Dto3D(translationMatrix.mul(scaleMatrix).mul(shearXMatrix).mul(shearYMatrix).mul(rotationMatrix));
}

这是来自matrix3f

的init ....
    public Matrix3f initIdentity(){
    m[0][0] = 1; m[0][1] = 0; m[0][2] = 0;
    m[1][0] = 0; m[1][1] = 1; m[1][2] = 0;
    m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
    return this;
}

public Matrix3f initTranslationIdentity(float x, float y) {
    m[0][0] = 1; m[0][1] = 0; m[0][2] = x;
    m[1][0] = 0; m[1][1] = 1; m[1][2] = y;
    m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
    return this;
}

public Matrix3f initRotationIdentity(double angle) {
    float cos = (float) Math.cos(angle);
    float sin = (float) Math.sin(angle);
    m[0][0] = cos;  m[0][1] = sin; m[0][2] = 0;
    m[1][0] = -sin; m[1][1] = cos; m[1][2] = 0;
    m[2][0] = 0;    m[2][1] = 0;   m[2][2] = 1;
    return this;
}

public Matrix3f initScaleIdentity(float x, float y) {
    m[0][0] = x; m[0][1] = 0; m[0][2] = 0;
    m[1][0] = 0; m[1][1] = y; m[1][2] = 0;
    m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
    return this;
}

public Matrix3f initShearXIdentity(float shearX) {
    m[0][0] = 1; m[0][1] = shearX; m[0][2] = 0;
    m[1][0] = 0; m[1][1] = 1; m[1][2] = 0;
    m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
    return this;
}

public Matrix3f initShearYIdentity(float shearY) {
    m[0][0] = 1;      m[0][1] = 0; m[0][2] = 0;
    m[1][0] = shearY; m[1][1] = 1; m[1][2] = 0;
    m[2][0] = 0;      m[2][1] = 0; m[2][2] = 1;
    return this;
}

这是从2d到3d矩阵的翻译

    public Matrix4f translate2Dto3D(Matrix3f m2){
    m[0][0] = m2.get(0, 0); m[0][1] = m2.get(0, 1); m[0][2] = 0;    m[0][3] = m2.get(0, 2);
    m[1][0] = m2.get(1, 0); m[1][1] = m2.get(1, 1); m[1][2] = 0;    m[1][3] = m2.get(1, 2);
    m[2][0] = 0;            m[2][1] = 0;            m[2][2] = 1;    m[2][3] = 0;
    m[3][0] = 0;            m[3][1] = 0;            m[3][2] = 0;    m[3][3] = 1;
    return this;
}

0 个答案:

没有答案