GLES20中的缩放对象

时间:2014-01-15 19:52:37

标签: android opengl-es-2.0

我有一个问题。

当我第一次执行此代码时:

...
restoreMatrix(currentScaleMatrix);
Matrix.translateM(currentScaleMatrix, 0, 0f, mGLSceneHeight / 2, 0f);
Matrix.translateM(currentScaleMatrix, 0, mFocalPoint.x, mFocalPoint.y, 0f);
Matrix.scaleM(currentScaleMatrix, 0, mCurrentScaleFactor, mCurrentScaleFactor, 1f);
Matrix.translateM(currentScaleMatrix, 0, -mFocalPoint.x, -mFocalPoint.y, 0f);
Matrix.translateM(currentScaleMatrix, 0, 0f, -mGLSceneHeight / 2, 0f);
//Draw

我的结果非常好。但是第二次之后,mFocalPoint计算错误,但我无法理解为什么。

private void restoreMatrix(float[] previewMVPMatrix) {
    synchronized (pathStack) {
        for (Path path : pathStack) {
            Matrix.translateM(previewMVPMatrix, 0, 0f, mGLSceneHeight / 2, 0f);

            Matrix.translateM(previewMVPMatrix, 0, path.point.x, path.point.y, 0f);
            Matrix.scaleM(previewMVPMatrix, 0, path.scaleFactor, path.scaleFactor, 1f);
            Matrix.translateM(previewMVPMatrix, 0, -path.point.x, -path.point.y, 0f);

            Matrix.translateM(previewMVPMatrix, 0, 0f, -mGLSceneHeight / 2, 0f);
        }
    }
}

public boolean onScaleBegin(ScaleGestureDetector detector) {
    float glX = (detector.getFocusX() * (mScaleFactorX)) - mGLSceneWidth / 2 ;
    float glY = (detector.getFocusY() * (mScaleFactorY)) - mGLSceneHeight / 2;
    mFocalPoint = new PointF((glX/ realCurrentScaleFactor), (-glY / realCurrentScaleFactor));
    return true;
}

public boolean onScale(ScaleGestureDetector detector) {
    mCurrentScaleFactor *= detector.getScaleFactor();
    return true;
}

public void onScaleEnd(ScaleGestureDetector detector) {
   updateMatrix(mFocalPoint, mCurrentScaleFactor);
    mFocalPoint = new PointF(0f, 0f);
    mCurrentScaleFactor = 1f;
}

private float getRealCurrentScaleFactor() {
    float scaleFactor = 1f; // by default
    for (Path path : pathStack) {
        scaleFactor*=path.scaleFactor;
    }
    Log.d("Scaler","[Scale sum] :: "+scaleFactor);
    return scaleFactor;
}

问题是:如何在Zoomed Image中计算FocalPoint

1 个答案:

答案 0 :(得分:0)

其实我发现了什么问题。焦点计算正确。你只需要:

  • actualMatrix保存到oldMatrix(来自第一次缩放的矩阵)
  • 重置actualMatrix
  • 放大新的身份 actualMatrix
  • 乘以oldMatrix * actualMatrix
  

Matrix.setIdentityM(oldMatrix,0); //在构造函数中初始化旧矩阵


  

onScale()

Matrix.translateM(actualMatrix, 0, focusX, focusY, 0);<br>
Matrix.scaleM(actualMatrix, 0, scaleFactor, scaleFactor, 0);<br>
Matrix.translateM(actualMatrix, 0, -focusX, -focusY, 0);<br>

  

onScaleEnd()

Matrix.multiplyMM(oldMatrix, 0, actualMatrix, 0, oldMatrix, 0);
Matrix.setIdentity(actualMatrix,0);