gpt4 book ai didi

java - GLES2.0中如何平移摄像头?

转载 作者:行者123 更新时间:2023-11-29 05:56:21 26 4
gpt4 key购买 nike

我想创建一个在平铺平面上方移动的相机。相机应该只在 XY 平面内移动,并且一直向下看。对于正交投影,我希望使用伪 2D 渲染器。我的问题是,我不知道如何翻译相机。经过一些研究,在我看来,OpenGL 中没有什么比“相机”更好的了,我必须翻译整个世界。在 Matrix.setLookAtM 函数中更改眼睛位置和 View 中心坐标只会导致结果失真。翻译整个 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);

}

最佳答案

Changing the eye-position and view center coordinates in the "LookAt"-function just leads to distorted results.

如果您是从 android 教程中获得的,我认为他们的代码中存在错误。 (对此发表评论 here )

尝试以下修复:

  1. 使用 setLookatM 指向您希望相机所在的位置。
  2. 在着色器中,更改 gl_Position

    来自:“gl_Position = vPosition * uMVPMatrix;”
    到:“gl_Position = uMVPMatrix * vPosition;”

  3. 我认为 //rotate the viewport 部分也应该删除,因为这不能正确旋转相机。您可以在 setlookat 函数中更改相机的方向。

关于java - GLES2.0中如何平移摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11972182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com