如何转换顶点坐标

时间:2017-05-09 02:08:51

标签: android opengl-es

我有一个顶点数组,用以下xyzw坐标定义一个正方形:

      -0.5,  0.0, 0.0, 0.0,
      -0.5, -1.0, 0.0, 0.0,
       0.5, -1.0, 0.0, 0.0,
       0.5,  0.0, 0.0, 0.0

我想将它向右翻译两个单位,以便生成的数组变为:

      1.5,  0.0, 0.0, 0.0,
      1.5, -1.0, 0.0, 0.0,
      2.5, -1.0, 0.0, 0.0,
      2.5,  0.0, 0.0, 0.0,

我知道我可以通过简单地在x坐标上添加一个来实现这一点,但我试图使用矩阵来做到这一点。我阅读了很多关于这个主题的教程和文章,但我找不到任何确切的例子。我想用OpenGL ES和Java(android)来做这件事。我还希望在将阵列发送到GPU之前应用它。我尝试使用Matrix.translateM,multiplyMM没有成功。

1 个答案:

答案 0 :(得分:1)

在评论之后,我意识到我认为它错了,我需要在每个顶点上应用矩阵,如下所示:

transformationMatrix = doTransformations(4f, 2f, 4f, 4f, -60);
float[] result = new float[4];
for (int j = 0; j < 12; j += 3) {
    float[] data = new float[]{
            vertexData[j + 0],
            vertexData[j + 1],
            vertexData[j + 2],
            1};
    Matrix.multiplyMV(result, 0, transformationMatrix, 0, data, 0);
    vertexData[j + 0] = result[0];
    vertexData[j + 1] = result[1];
    vertexData[j + 2] = result[2];
}

和转换方法:

public static float[] doTransformations(float x, float y, float scaleX, float scaleY, float angle){
    float[] scratch = new float[16];
    float[] transformation = new float[16];
    float[] mRotationMatrix = new float[16];
    Matrix.setIdentityM(transformation,0);

    Matrix.translateM(transformation, 0, x, y ,0);
    Matrix.scaleM(transformation, 0, scaleX, scaleY, 1);
    Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f);

    Matrix.multiplyMM(scratch, 0,transformation,0,mRotationMatrix,0);
    return scratch;
}

这是我在矩阵变换之前尝试转换的图像: image before transformation

之后: image after transformation

相关问题