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++){     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计算顶点时,它们保持不变并且不会改变,计算顶点的函数是:         // Calculate the vertices in world space:         if(LoggerConfig.ON) Log.d("Middle Vertex", vector1r[2] +","         return new float[] { 即使我移动对象并且对象在 OpenGL 中移动,日志中的结果也保持不变,这是我正在做的计算的问题吗?我该如何纠正它? 在您的  由于 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        // 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};
        float[] vector1r = modelMatrix.MultiplyVec(vector1);
        float[] vector2r = modelMatrix.MultiplyVec(vector2);
        float[] vector3r = modelMatrix.MultiplyVec(vector3);
        float[] vector4r = modelMatrix.MultiplyVec(vector4);
                                                 + vector1r[3]);
                vector1r[0], vector1r[1], 
                vector2r[0], vector2r[1], 
                vector3r[0], vector3r[1], 
                vector4r[0], vector4r[1]
        };
    }
 
 
2
3
4
5
6
7
 
    int value = 0;
    for(int j = 0; j < 4; j++){
        value += matrix[i][j] * vec[j]; 
    }
    newV[i] = value;
}
 
 
2
3
 
    float value = 0f;
    …
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/267556.html
