【问题标题】:Rotate camera around scene with scrolling opengl ES使用滚动opengl ES围绕场景旋转相机
【发布时间】:2020-08-14 00:37:41
【问题描述】:

我正在尝试使用触摸(滚动)围绕场景旋转相机:

public class SurfaceView extends GLSurfaceView 
        implements GestureDetector.OnGestureListener {
    SceneRenderer renderer;        

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                      float distanceX, float distanceY) {
        renderer.setMotion(distanceX, distanceY); // pass movement to render
        return true;
    }    
}

相机在渲染中移动如下:

public class SceneRenderer implements GLSurfaceView.Renderer {
    private float[] viewMatrix = new float[16];
    
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, 3f,
        0f, 0f, 0f, 0f, 1.0f, 0.0f);
    }

    ...
    // movement of camera
    public synchronized void setMotion(float xDistance, float yDistance) {
        Matrix.rotateM(viewMatrix, 0, -xDistance * 0.1f, 0, 1, 0);
        Matrix.rotateM(viewMatrix, 0, -yDistance * 0.1f, 1, 0, 0);
    }
}

这在开始时效果很好。但随后相机开始不按预期旋转。如何使用滚动使相机相对于场景(对象)的中心正确移动?也许有人解决了类似的问题?

感谢您的任何回答/评论!

解决办法:

  private float kx = 0f;
  private float ky = 0f;
  private float radius = 3.0f;
  private float x, y, z = 0f;

  ...
  public synchronized void setMotion(float xDistance, float yDistance) {
      kx = kx + xDistance * 0.001f;
      x = (float) (radius * Math.sin(kx));
      z = (float) (radius * Math.cos(kx));

      ky = ky + yDistance * 0.001f;
      y = (float) (radius * Math.sin(ky));

      Matrix.setLookAtM(viewMatrix, 0, x, -y, z, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
  }

但是使用这种方法,相机在沿 Y 轴旋转时会远离场景。

【问题讨论】:

    标签: android matrix opengl-es opengl-es-2.0 touch-event


    【解决方案1】:

    在矩阵代数中,顺序很重要。由于增量旋转交错矩阵和中断顺序,它不会像您期望的那样工作。因此,您需要在每次 setMotion() 调用时根据累积 delta x/y 重建视图矩阵,以便始终以正确的顺序应用旋转,如下所示。

    public class SceneRenderer implements GLSurfaceView.Renderer {
        private float[] viewMatrix = new float[16];
        private float cumulativeX = 0.0f;
        private float cumulativeY = 0.0f;
    
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, 3f,
            0f, 0f, 0f, 0f, 1.0f, 0.0f);
            cumulativeX = 0.0f;
            cumulativeY = 0.0f;
        }
    
        ...
        // movement of camera
        public synchronized void setMotion(float xDistance, float yDistance) {
    
            cumulativeX += xDistance;
            cumulativeY += yDistance;
    
            Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, 3f,
                0f, 0f, 0f, 0f, 1.0f, 0.0f);
    
            //Matrix.rotateM(viewMatrix, 0, -cumulativeX * 0.1f, 0, 1, 0);
            //Matrix.rotateM(viewMatrix, 0, -cumulativeY * 0.1f, 1, 0, 0);
    
            Matrix.rotateM(viewMatrix, 0, -cumulativeY * 0.1f, 1, 0, 0);
            Matrix.rotateM(viewMatrix, 0, -cumulativeX * 0.1f, 0, 1, 0);
        }
    }
    

    附录:仅通过 setLookAtM 组合视图矩阵

    当然只有使用 setLookAtM 才能达到同样的效果。要使视点保持恒定距离,请在spherical coordinates 中移动视点。并向上调整。

    public synchronized void setMotion(float xDistance, float yDistance) {
    
        kx = kx + xDistance * 0.001f;
        ky = ky + yDistance * 0.001f;
        
        x = (float) (radius * Math.cos(ky) * Math.sin(kx));
        z = (float) (radius * Math.cos(ky) * Math.cos(kx));
        y = (float) (radius * Math.sin(ky));
    
    
        float[] up =
        {
            (float) (Math.sin(ky) * Math.sin(kx)),
            (float) (Math.cos(ky)),
            (float) (Math.sin(ky) * Math.cos(kx)),
        };
        Matrix.setLookAtM(viewMatrix, 0,
                x, -y, z,
                0f, 0f, 0f,
                up[0], up[1], up[2]
        );
    
    }
    

    【讨论】:

    • 感谢您的回答!但问题是在初始旋转之后,场景开始沿其他轴旋转。我在帖子中添加了自我解决方案,但它使相机在沿 Y 轴旋转时远离场景。再次感谢您的回答!
    • 根据您的解决方案,旋转顺序应该是 X 轴 > Y 轴。无论如何,我更新了答案。如果您想使用 setLookAtM 解决方案,请参阅附录。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-11-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多