我想創建在平鋪平面上方移動的攝像機。相機應該只在XY平面內移動,並且一直往下看。對於正交投影,我期望有一個僞2D渲染器。 我的問題是,我不知道如何翻譯相機。經過一番研究,在我看來,OpenGL中沒有像「相機」,我必須翻譯整個世界。改變Matrix.setLookAtM
-功能中的眼睛位置和視圖中心座標只會導致結果失真。 翻譯整個MVP-Matrix也不起作用。如何在GLES2.0中翻譯相機?
我現在想出來的想法;是否必須直接在頂點緩衝區中翻譯每一幀的每個頂點?這對我來說似乎並不合理。
我得出GLSurfaceView
並實現以下功能設置和更新場景:
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width/height;
// Setup the projection Matrix for an orthogonal view
Matrix.orthoM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
public void onDrawFrame(GL10 unused) {
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
//Setup the camera
float[] camPos = { 0.0f, 0.0f, -3.0f }; //no matter what else I put in here the camera seems to point
float[] lookAt = { 0.0f, 0.0f, 0.0f }; // to the coordinate center and distorts the square
// Set the camera position (View matrix)
Matrix.setLookAtM(vMatrix, 0, camPos[0], camPos[1], camPos[2], lookAt[0], lookAt[1], lookAt[2], 0f, 1f, 0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, projMatrix, 0, vMatrix, 0);
//rotate the viewport
Matrix.setRotateM(mRotationMatrix, 0, getRotationAngle(), 0, 0, -1.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);
//I also tried to translate the viewport here
// (and several other places), but I could not find any solution
//draw the plane (actually a simple square right now)
mPlane.draw(mMVPMatrix);
}
是的,它基於您提到的官方示例中的代碼。你的修復就像一個魅力。謝謝! – Andre 2012-08-15 20:55:15