我在尝试实现缩放以缩放在 iOS 下使用 OpenGLES 正交投影显示的图像时遇到了一些麻烦。我正在尝试缩放和平移投影矩阵,以使缩放保持在捏合位置的中心。
根据我的更新:方法我配置矩阵:
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
projectionMatrix = GLKMatrix4MakeOrtho(-0.5f, 0.5f, -0.5f / aspect, 0.5f / aspect, -1.0f, 1.0f);
projectionMatrix = GLKMatrix4Multiply(projectionMatrix, GLKMatrix4MakeScale(newScale, newScale, 1.0f)); // scale
projectionMatrix = GLKMatrix4Multiply(projectionMatrix, GLKMatrix4MakeTranslation((endPan1.x + endPan2.x), 0.0 - (endPan1.y + endPan2.y), 0.0f)); //pan
我使用捏合手势识别器获取 newScale 和 endPan1 的值。(endPan2 来自一个双指平移识别器。):
- (void)pinchDetected:(UIGestureRecognizer *)sender;
{
if (sender.state == UIGestureRecognizerStateBegan) {
lastScale = newScale;
startPan1.x = endPan1.x;
startPan1.y = endPan1.y;
GLKMatrix4 pinchMatrix = projectionMatrix;
CGFloat aspectInv = self.view.bounds.size.height / self.view.bounds.size.width;
// Get the center offset of the projection matrix:
GLKVector2 middleVect = GLKVector2Make(pinchMatrix.m30, pinchMatrix.m31);
CGPoint pointPinch = [sender locationInView:self.view];
GLKVector2 touchVectPinch = GLKVector2Make((2 * pointPinch.x / self.view.bounds.size.width - 1),
(2 * (self.view.bounds.size.height-pointPinch.y) / self.view.bounds.size.height - 1));
touchToMiddle = GLKVector2Make((middleVect.x - touchVectPinch.x) * (1.0 / pinchMatrix.m00), (middleVect.y - touchVectPinch.y) * ((1.0 / pinchMatrix.m00) * aspectInv) );
}
newScale = [(UIPinchGestureRecognizer*)sender scale] - (1.0 - lastScale);
if (newScale < 0.25) {
newScale = 0.25;
}
if (newScale > 4.0) {
newScale = 4.0;
}
// Pan the image to center the scale under the touch point:???
endPan1.x = (((newScale - 1.0) * 0.5) * (touchToMiddle.x )) - startPan1.x ;
endPan1.y = 0.0 - ((((newScale - 1.0) * 0.5) * (touchToMiddle.y )) + startPan1.y);
}
结果是图像在缩放时不会完全保持在捏合位置的中心。它有点工作......这让我觉得我必须接近???