OpenGL ES 2.0 Щепотка и масштабирование

В OpenGL ES 1.1 на iOS я реализовывал щипок и масштабирование, устанавливая поле зрения, используя следующее:

// 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?


person PhilBot    schedule 20.04.2013    source источник


Ответы (1)


Умножьте первый член 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 после остановки жеста, чтобы масштаб не «сбрасывался» каждый раз.

person AlunAlun    schedule 20.04.2013
comment
Привет, AlunAlun, не могли бы вы предложить какой-нибудь пример кода для функциональности панорамирования в OpenGL Es2.0 в Android. Я видел больше ссылок, но я не мог получить четкого представления об этой функциональности панорамирования. - person harikrishnan; 28.06.2013