我使用默認的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);
}
默認行爲:
我的代碼:
您好。你應該可能知道那個教程有些失敗([看到這個問題])(http://stackoverflow.com/questions/11925647/is-googles-android-opengl-tutorial-teaching-incorrect-linear-algebra))。 Google工程師之一表示他們會盡快修復它,但從您的示例看來,它尚未修復。他們的示例代碼有點荒謬。 – Tim
另外我認爲其中的一個答案包含一些固定的代碼,您可能想嘗試一下。 – Tim
您無法在MVP矩陣上進行平移/旋轉/縮放,並獲得您期望的結果。您必須在模型矩陣中轉換/旋轉您的對象(或在視圖矩陣中進行相機反轉/旋轉)。看看這個http://stackoverflow.com/questions/5550620/the-purpose-of-model-view-projection-matrix更好地理解你需要做什麼 – Igor