我想创建一个在平铺平面上方移动的相机。相机应该只在 XY 平面上移动,并且一直向下看。使用正交投影,我期望使用伪 2D 渲染器。我的问题是,我不知道如何翻译相机。经过一些研究,在我看来,OpenGL 中没有像“相机”这样的东西,我必须翻译整个世界。在 -function 中更改眼睛位置和视图中心坐标Matrix.setLookAtM
只会导致结果失真。翻译整个 MVP-Matrix 也不起作用。
我现在没有想法了;我是否必须直接在顶点缓冲区中翻译每帧的每个顶点?这对我来说似乎不合理。
我派生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);
}