关于java:矩阵向量乘法 | 珊瑚贝

Matrix-vector Multiplication


例如,我有一个名为 Matrix4x4 的类,该类具有以下功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
public float[] MultiplyVec(float[] vec){
    float[] newV = new float[4];

    for(int i = 0; i < 4; i++){
        int value = 0;
        for(int j = 0; j < 4; j++){
            value += matrix[i][j] * vec[j];
        }
        newV[i] = value;
    }

    return newV;
}

在另一个类中,我有一个对象名称 modelMatrix,它是 Matrix4x4 的一个实例,它以这种方式初始化:

1
public Matrix4x4 modelMatrix = Matrix4x4.IdentityM();

其中 IdentityM() 函数包含:

1
2
3
4
5
6
7
8
public static Matrix4x4 IdentityM(){
        return new Matrix4x4(new float[][]{
                {1f, 0f, 0f, 0f},
                {0f, 1f, 0f, 0f},
                {0f, 0f, 1f, 0f},
                {0f, 0f, 0f, 1f}
        });
}

现在我有一个小问题我想不通。
例如,假设我有一个带有 x、y、宽度和高度的矩形。
现在例如我这样做:

1
modelMatrix.translate(0.002f, 0.002f, 0);

函数 translate 包含的位置:

1
2
3
4
5
public void translate(float x, float y, float z){
        matrix[0][3] += x;
        matrix[1][3] += y;
        matrix[2][3] += z;
}

当我将modelMatrix发送到着色器时使用OpenGL ES,对象正确移动,但是当我用modelMatrix计算顶点时,它们保持不变并且不会改变,计算顶点的函数是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public float[] getVertices(){
        // Vertices in object space:
        float[] vector1 = {x, y, 0f, 1f};
        float[] vector2 = {x + width, y, 0f, 1f};
        float[] vector3 = {x + width, y + height, 0f, 1f};
        float[] vector4 = {x, y, 0f, 1f};

        // Calculate the vertices in world space:
        float[] vector1r = modelMatrix.MultiplyVec(vector1);
        float[] vector2r = modelMatrix.MultiplyVec(vector2);
        float[] vector3r = modelMatrix.MultiplyVec(vector3);
        float[] vector4r = modelMatrix.MultiplyVec(vector4);

        if(LoggerConfig.ON) Log.d(“Middle Vertex”, vector1r[2] +“,”
                                                 + vector1r[3]);

        return new float[] {
                vector1r[0], vector1r[1],
                vector2r[0], vector2r[1],
                vector3r[0], vector3r[1],
                vector4r[0], vector4r[1]
        };
    }

即使我移动对象并且对象在 OpenGL 中移动,日志中的结果也保持不变,这是我正在做的计算的问题吗?我该如何纠正它?

  • 嗨,想知道你能否在获取顶点部分给我启发?我的意思是它背后的推理和数学如何获得它们?首先十分感谢。
  • @Unheilig 好吧,我在对象空间中取了顶点。然后在我将它们移动到空间(称为世界空间)后获取顶点,我需要将模型矩阵与对象的每个顶点相乘,因为我只修改模型矩阵然后得到我需要乘以对象的顶点空间顶点与模型矩阵,只有这样我才能在移动它们后得到顶点。而已 :)
  • 感谢您的解释。


在您的 MultiplyVec() 方法中,您将所有中间值四舍五入为整数:

1
2
3
4
5
6
7
for(int i = 0; i < 4; i++){
    int value = 0;
    for(int j = 0; j < 4; j++){
        value += matrix[i][j] * vec[j];
    }
    newV[i] = value;
}

由于 value 是 int 类型的变量,所有部分和将四舍五入为整数。您需要使用 float 变量作为中间值:

1
2
3
for(int i = 0; i < 4; i++){
    float value = 0f;
    …
  • 嗯,是的,忘记了(那些小错误一直在发生)。但问题是它仍然给我同样的结果……没有帮助..


来源:https://www.codenong.com/28267591/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9413.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?