使用触摸板放大一个点(使用缩放,在opengl或metalkit中使用Objective-C进行平移和旋转)



我只是创建了一个像osx预览应用程序这样的应用程序。我使用 opengl 或 metalkit 来渲染图像。我可以通过某个矩阵使用中心点 (0,0) 进行缩放,但我不能像预览应用程序那样使用触摸板在两个手指之间的点进行缩放,因为我不知道如何创建我的模型矩阵。

我只是在苹果开发论坛上问我的问题,我在github上找到了演示,但没有人可以帮助我。

- (void)magnifyWithEvent:(NSEvent *)event {
NSPoint eventLocation = [event locationInWindow];
NSPoint center = [self.view convertPoint:eventLocation fromView:nil];
NSPoint openglCenter = CGPointMake(center.x / ([[[NSApplication sharedApplication] mainWindow] frame].size.width / 2.0) - 1.0, center.y / ([[[NSApplication sharedApplication] mainWindow] frame].size.height / 2.0) - 1.0);
NSRect frame = [[[NSApplication sharedApplication] mainWindow] frame];
if ([event magnification] > 0)
{
if ([self zoomValue] <= 2.0)
{
[self setZoomValue:[self zoomValue] + [event magnification]];
if(self.zoomValue > 2.0) {
return;
}
self.slider.floatValue = [self zoomValue];
self.panMatrix = GLKMatrix4Translate(GLKMatrix4Identity, self.swipX / (frame.size.width / 2.0), -self.swipY / (frame.size.height / 2.0), 1);
self.scaleMatrix = GLKMatrix4Scale(self.baseScaleMatrix, self.zoomValue, self.zoomValue, 1);
GLKMatrix4 model = GLKMatrix4Multiply(self.panMatrix, self.scaleMatrix);
[self.testView makeChangeWithMat:model];
} else {
[self setZoomValue:2.0];
self.slider.floatValue = [self zoomValue];
self.panMatrix = GLKMatrix4Translate(GLKMatrix4Identity, self.swipX / (frame.size.width / 2.0), -self.swipY / (frame.size.height / 2.0), 1);
self.scaleMatrix = GLKMatrix4Scale(self.baseScaleMatrix, self.zoomValue, self.zoomValue, 1);
GLKMatrix4 model = GLKMatrix4Multiply(self.panMatrix, self.scaleMatrix);
[self.testView makeChangeWithMat:model];
}
}
else if ([event magnification] < 0)
{
if ([self zoomValue] + [event magnification] >= 1.0)
{
[self setZoomValue:[self zoomValue] + [event magnification]];
self.slider.floatValue = [self zoomValue];
self.panMatrix = GLKMatrix4Translate(GLKMatrix4Identity, self.swipX / (frame.size.width / 2.0), -self.swipY / (frame.size.height / 2.0), 1);
self.scaleMatrix = GLKMatrix4Scale(self.baseScaleMatrix, self.zoomValue, self.zoomValue, 1);
GLKMatrix4 model = GLKMatrix4Multiply(self.panMatrix, self.scaleMatrix);
[self.testView makeChangeWithMat:model];
}
else
{
[self setZoomValue:1.0];
if(self.zoomValue < 1.0) {
return;
}
self.slider.floatValue = [self zoomValue];
self.panMatrix = GLKMatrix4Translate(GLKMatrix4Identity, self.swipX / (frame.size.width / 2.0), -self.swipY / (frame.size.height / 2.0), 1);
self.scaleMatrix = GLKMatrix4Scale(self.baseScaleMatrix, self.zoomValue, self.zoomValue, 1);
GLKMatrix4 model = GLKMatrix4Multiply(self.panMatrix, self.scaleMatrix);
[self.testView makeChangeWithMat:model];
}
}
}

要缩放任意点,您需要:

  1. 平移方式(中心 - 点),从而更改步骤 2 的原点
  2. 按必要量缩放
  3. 通过 -(中心 - 点)回平移,从而将原点返回 (0,0)

您完全缺少其中一个翻译。 你需要这样的东西:

NSPoint eventLocation = [event locationInWindow];
NSPoint center = [self.view convertPoint:eventLocation fromView:nil];
double transX = 1.0 - center.x / (frame.size.width / 2.0);
double transY = 1.0 - center.y / (frame.size.height / 2.0);
GLKMatrix4 trans = GLKMatrix4Translate(GLKMatrix4Identity, transX, transY, 0);
GLKMatrix4 transBack = GLKMatrix4Translate(GLKMatrix4Identity, -transX, -transY, 0);
GLKMatrix4 scaleMatrix = GLKMatrix4Scale(GLKMatrix4Identity, self.zoomValue, self.zoomValue, 1);
GLKMatrix4 model = GLKMatrix4Multiply( transBack, GLKMatrix4Multiply( GLKMatrix4Multiply( scaleMatrix, trans ), _baseScaleMatrix ) );

编辑:您也不能将投影矩阵(_baseScaleMatrix)混合到模型矩阵中,必须首先应用投影,然后应用模型矩阵。

最新更新