2017-05-09 94 views
-1

我有一個頂點數組定義帶有以下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沒有成功。

+0

頂點數組並不是一個矩陣,而是一個向量列表。你需要對每個矢量單獨做矩陣運算。這頁可能有助於https://en.wikipedia.org/wiki/Translation_(幾何) –

+0

@Craig我知道它不是一個矩陣,但它被視爲一個。只要看看Matrix.multiplyM簽名。它接受兩個浮點數組作爲被視爲矩陣的參數。 https://developer.android.com/reference/android/opengl/Matrix.html#multiplyMM(float [],int,float [],int,float [],int) –

+0

不是。它是放入4x4矩陣中的4個任意向量。如果是三角形呢? 3x4的?怎麼樣一個複雜的角色模型有1000個頂點。你不能用一個矩陣乘法來轉換它們。 –

回答

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