简体   繁体   English

如何在GLES2.0中翻译相机?

[英]How to translate the camera in GLES2.0?

I want to create a camera moving above a tiled plane. 我想创建一个在平铺平面上方移动的相机。 The camera is supposed to move in the XY-plane only and to look straight down all the time. 相机应该只在XY平面上移动,并且一直向下看。 With an orthogonal projection I expect a pseudo-2D renderer. 使用正交投影,我期望伪2D渲染器。 My problem is, that I don't know how to translate the camera. 我的问题是,我不知道如何翻译相机。 After some research it seems to me, that there is nothing like a "camera" in OpenGL and I have to translate the whole world. 经过一些研究,在我看来,在OpenGL中没有什么像“相机”,我必须翻译整个世界。 Changing the eye-position and view center coordinates in the Matrix.setLookAtM -function just leads to distorted results. 更改Matrix.setLookAtM函数中的眼睛位置和视图中心坐标只会导致结果失真。 Translating the whole MVP-Matrix does not work either. 翻译整个MVP-Matrix也不起作用。

I'm running out of ideas now; 我现在已经没想完了; do I have to translate every single vertex every frame directly in the vertex buffer? 我是否必须直接在顶点缓冲区中每帧翻译每个顶点? That does not seem plausible to me. 这对我来说似乎不合理。

I derived GLSurfaceView and implemented the following functions to setup and update the scene: 我派生了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. 更改“LookAt”功能中的眼睛位置和视图中心坐标只会导致失真的结果。

If you got this from the android tutorial, I think they have a bug in their code. 如果你从android教程得到这个,我认为他们的代码中有一个错误。 (made a comment about it here ) (在这里发表评论)

Try the following fixes: 请尝试以下修复:

  1. Use setLookatM to point to where you want the camera to be. 使用setLookatM指向您想要摄像机的位置。
  2. In the shader, change the gl_Position line 在着色器中,更改gl_Position

    from: " gl_Position = vPosition * uMVPMatrix;" 来自: " gl_Position = vPosition * uMVPMatrix;"
    to: " gl_Position = uMVPMatrix * vPosition;" to: " gl_Position = uMVPMatrix * vPosition;"

  3. I'd think the //rotate the viewport section should be removed as well, as this is not rotating the camera properly. 我认为//rotate the viewport部分也应该被移除,因为这不能正确旋转相机。 You can change the camera's orientation in the setlookat function. 您可以在setlookat功能中更改相机的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM