1

在 iOS 上的 OpenGL ES 1.1 中,我曾经通过使用以下设置视野来实现捏合和缩放:

// Handles Touches Events
- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender 
{
    static float startFOV=0.0;
    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    UIGestureRecognizerState state;

    state=sender.state;

    if(state==UIGestureRecognizerStateBegan)
    {      
        startFOV=[self getFieldOfView];
    }
    else if(state==UIGestureRecognizerStateChanged)
    {
        float minFOV=5.0;
        float maxFOV=12.0;
        float currentFOV;

        currentFOV=startFOV*factor;

        if((currentFOV>=minFOV) && (currentFOV<=maxFOV))
            [self setFieldOfView:currentFOV];
    }     
}

使用捏合手势我会做这样的事情:

// Set the fulstrum and our field of view for the window
-(void)setClipping
{
    // Near and far are the front and back walls
    // FOV is in degrees
    float aspectRatio;
    const float zNear = .1;                 
    const float zFar = 2000;                    
    GLfloat size;
    float scale;

    // Get the main screen and define the aspect ratio
    CGRect frame = [[UIScreen mainScreen] bounds];      
    aspectRatio=(float)frame.size.width/(float)frame.size.height;                   
    scale=[[UIScreen mainScreen]scale];

    // Use the 2D projection matrix to project our 3D into 2D
    glMatrixMode(GL_PROJECTION);                
    glLoadIdentity();
    if (m_FieldOfView > 75.0) {
        m_FieldOfView = 75.0;
    }
    size = zNear * tanf(GLKMathDegreesToRadians (m_FieldOfView) / 2.0); 

    // Define the pyramid of Giza (4 sided pyramid with top lopped off on its side)
    // ... this is how were viewing things
    glFrustumf(-size, size, -size/aspectRatio, size/aspectRatio, zNear, zFar);  
    glViewport(0, 0, frame.size.width*scale, frame.size.height*scale);      

    // To be safe go back to tranformational matrix
    glMatrixMode(GL_MODELVIEW);             
}

我制作了一个简单的 OpenGL ES 2.0 应用程序,我的更新方法看起来(部分)如下所示:

#pragma mark - GLKView and GLKViewController delegate methods

- (void)update
{

    // Set up the frustrum and projection matrix
    float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
    self.effect.transform.projectionMatrix = projectionMatrix;

我一直在网上搜索如何使用 OpenGL ES 2.0 做到这一点......但无济于事。我如何在 2.0 中做到这一点?

4

1 回答 1

3

将 GLKMatrix4MakePerspective的第一项乘以一个因子(属性或类变量)并在手势识别器中更改该因子 - 与您在 1.1 方法中所做的没有太大区别。

这是我从 GestureRecogniser 调用的方法。以 _ 开头的变量是类变量。ZOOMTOUCHSENSITIVITY 是一个预处理器定义。

-(void)Scale:(UITapGestureRecognizer*)sender
{


    CGFloat scale = _lastScale + (1.0 - [(UIPinchGestureRecognizer*)sender scale])*ZOOMTOUCHSENSITIVITY;

    float newScale = MAX(0.1, MIN(scale, 3));
    _projectionMatrix = GLKMatrix4MakePerspective(newScale*GLKMathDegreesToRadians(45.0f), (float)_screenWidth/(float)_screenHeight, 100.0f, 1000.0f);
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        _lastScale = newScale;
        return;
    }
}

请注意我如何保存scale手势停止后的最后一个值,以便缩放不会每次都“重置”。

于 2013-04-20T17:50:56.210 回答