【问题标题】:Scaling around point (pinch zoom) with Android and OpenGL ES 2.0使用 Android 和 OpenGL ES 2.0 围绕点缩放(捏缩放)
【发布时间】:2014-07-02 12:14:40
【问题描述】:

我正在尝试使用方法围绕该点进行缩放:

  • 翻译到我想要缩放的点(捏缩放)
  • 规模
  • 向后翻译

看起来像:

@Override
public boolean onScale(ScaleGestureDetector detector) {

    float scaleFactor = detector.getScaleFactor();

    float focusX = detector.getFocusX();
    float focusY = detector.getFocusY();

    Matrix.translateM(modelMatrix, 0, -focusX, -focusY, 0);
    Matrix.scaleM(modelMatrix, 0, scaleFactor, scaleFactor, 0);
    Matrix.translateM(modelMatrix, 0, focusX, focusY, 0);

但是当我创建结果 MVP 矩阵时:

Matrix.multiplyMM(MVPmatrix, 0,
    projectionMatrix, 0,
    modelMatrix, 0);

我得到了不正确的缩放比例,所以它可以缩放,但它不能正确地围绕点缩放..

您能否告知可能是什么原因以及如何正确围绕该点进行缩放?

【问题讨论】:

    标签: android opengl-es opengl-es-2.0


    【解决方案1】:

    据我所知,Android Matrix 实用程序类中的方法从右侧乘以新指定的转换。我没有在文档中看到它,但源代码 (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/opengl/Matrix.java) 清楚地表明它是这样工作的。

    这意味着当您组合来自一系列子变换的变换矩阵时,您调用的 last 方法指定应用于您的点的子变换first。换句话说,您指定矩阵的顺序与它们应用于您的点的顺序相反。

    对于围绕您的点(focusX, focusY) 的旋转,您首先要将(-focusX, -focusY, 0.0f) 的平移应用于您的点,然后是旋转,然后是(focusX, focusY, 0.0f) 的平移。由于调用顺序与此相反,所以应该是:

    Matrix.translateM(modelMatrix, 0, focusX, focusY, 0.0f);
    Matrix.scaleM(modelMatrix, 0, scaleFactor, scaleFactor, scaleFactor);
    Matrix.translateM(modelMatrix, 0, -focusX, -focusY, 0.0f);
    

    我还在这里更改了scaleM() 的最后一个参数。由于 z 方向的比例因子为 0,因此您会将整个几何图形展平到 x-y 平面中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多