我只创建了像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];
}
}
}
答案 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)混合到模型矩阵中,必须先应用投影,然后再应用模型矩阵。