ios - 捏缩放OpenGL正交投影矩阵

标签 ios opengl-es opengl-es-2.0 glkit

我在尝试实现缩放以缩放在 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); 
}

结果是图像在缩放时不会完全保持在捏合位置的中心。它有点工作......这让我觉得我必须接近???

最佳答案

我不太确定您的图像是如何放置在屏幕上的,但您可能应该将您的图像相对于屏幕的中间点进行平移。因此,如果您的平均夹点是屏幕的右上角,那么您应该按照您的夹点在 x 和 y 方向上远离中间的量进行平移。此平移可能需要根据您的图像与相机的距离(即使其正交)进行缩放。您很可能需要使用这些数字来找到适合您的方法。

一种可能的替代方法是不使用正交 View ,使用透视图,然后您可以调整缩放效果的视野,然后将相机移动到应该放大到的点上。

关于ios - 捏缩放OpenGL正交投影矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11418000/

相关文章:

ios 开发 - 如何做 JSON 解析器?

ios - 应用内购买、消耗品

ios - SWRevealViewController 与 TabBarController

ios - 使 CollectionView 和 TableView 高度动态化

ios - 在 iOS 上更改 OpenGL ES 版本

ios - OpenGL ES 2.0 线条看起来比 Core Animation 更锯齿。在 iOS 4 中可以抗锯齿吗?

android - 如何在 GLSL 中写入/防止写入 OpenGL 深度缓冲区

opengl - 片段着色器 GLSL 中未使用的可变变量 - 它们会伤害性能吗

ios - 如何在OpenGL中多次添加光?

android - 将可绘制资源加载到纹理中并渲染它