2012-09-28 33 views
0

我使用默認的Android示例代碼 http://developer.android.com/training/graphics/opengl/touch.html 在此示例中,我們可以通過觸摸事件旋轉三角形。如何在默認的android樣本中移動opengl三角形?

我只是想通過x,y軸來增加移動來進行測試。 三角形行爲不是我所期待的。我做錯了什麼?從教程

代碼與我的新行加亮後:

public void onDrawFrame(GL10 unused) { 

     // Draw background color 
     GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 

     // Set the camera position (View matrix) 
     Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 

     // Calculate the projection and view transformation 
     Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); 


     // Draw square 
     mSquare.draw(mMVPMatrix); 



     **//Translating this matrix 'brakes' triangle 
    ->  Matrix.translateM(mMVPMatrix, 0, 0, pos, -1.0f); 

     //NOTHING happens here: ??? Why? 
    ->  Matrix.translateM(mRotationMatrix, 0, pos, 0, -1.0f);** 

     // Create a rotation for the triangle 
     // long time = SystemClock.uptimeMillis() % 4000L; 
     // float angle = 0.090f * ((int) time); 
     Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f); 

     // Combine the rotation matrix with the projection and camera view 
     Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); 

     // Draw triangle 
     mTriangle.draw(mMVPMatrix); 
    } 

默認行爲:

enter image description here

我的代碼:

enter image description here

+1

您好。你應該可能知道那個教程有些失敗([看到這個問題])(http://stackoverflow.com/questions/11925647/is-googles-android-opengl-tutorial-teaching-incorrect-linear-algebra))。 Google工程師之一表示他們會盡快修復它,但從您的示例看來,它尚未修復。他們的示例代碼有點荒謬。 – Tim

+1

另外我認爲其中的一個答案包含一些固定的代碼,您可能想嘗試一下。 – Tim

+2

您無法在MVP矩陣上進行平移/旋轉/縮放,並獲得您期望的結果。您必須在模型矩陣中轉換/旋轉您的對象(或在視圖矩陣中進行相機反轉/旋轉)。看看這個http://stackoverflow.com/questions/5550620/the-purpose-of-model-view-projection-matrix更好地理解你需要做什麼 – Igor

回答

1

感謝一個icrev評論:

你不能在MVP矩陣上做翻譯/旋轉/縮放,並按照你的預期得到結果。

您必須在模型矩陣中(或在相機反式/旋轉的視圖矩陣中)翻譯/旋轉對象。

看這個The purpose of Model View Projection Matrix更好地瞭解你需要做的

這是什麼意思步驟:

  1. 集合M矩陣單位矩陣。翻譯或旋轉它。注意萬向節鎖定(en.wikipedia.org/wiki/Gimbal_lock)

  2. 集合V矩陣Matrix.setLookAtM(mVMatrix,0,0,0,-3,0F,0F,0F,0F的,1.0F, 0.0F); 3.您已經有投影矩陣(你的情況mProjMatrix)

  3. multippy M * V * p來收到最終MVP矩陣