2

iOS で OpenGLES 正投影で表示された画像をズームするピンチを実装しようとすると、問題が発生します。ズームがピンチ位置の下の中心に留まるように、投影行列をスケーリングおよび変換しようとしています。

私の update: メソッドの下で、マトリックスを構成します。

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 は 2 本指のパン レコグナイザーに由来します。):

- (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); 
}

その結果、ズーム中に画像がピンチ位置の下で完全に中央にとどまりません。それはちょっと半分うまくいきます...私は近くにいるに違いないと思いますか???

4

1 に答える 1

0

画像が画面にどのように配置されるかはよくわかりませんが、おそらく画面の中央点を基準にして画像を変換する必要があります。したがって、平均的なピンチ ポイントが画面の右上にある場合は、ピンチ ポイントが中央から x 方向と y 方向に離れている分だけ移動する必要があります。この変換は、画像がカメラからどれだけ離れているかに応じて、スケーリングする必要がある場合があります (正射投影であっても)。おそらく、これらの数字をいじって、自分に合ったものを見つける必要があります。

考えられる代替方法は、正投影ビューを使用せずに遠近法を使用することです。その後、ズーム効果の視野を調整し、ズームインする必要があるポイントにカメラを移動するだけです。

于 2012-07-11T15:21:30.107 に答える