Question

I have a GLKView (OpenGL ES2.0) between a navigation bar on the top and a tool bar at the bottom of my iOS app window. I have implemented pinch zoom using UIPinchGestureRecognizer but when I zoom out a good extent, my view runs over the top navigation bar. Surprisingly the view does not go over the tool bar at the bottom. Wonder what I'm doing wrong.

Here's the viewport settings I'm using:

 glViewport(0, 0, self.frame.size.width, self.frame.size.height);

and here's the update and the pinch handler:

-(void) update {    
       float aspect = fabsf(self.bounds.size.width / self.bounds.size.height);

    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f),aspect, 0.01f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;

    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
    modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, _rotMatrix);
    self.effect.transform.modelviewMatrix = modelViewMatrix;
}

-(IBAction) handlePinch: (UIPinchGestureRecognizer *)recognizer {
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1.0;
}
Was it helpful?

Solution

First, you don't need to call glViewport when drawing with GLKView to its builtin framebuffer -- it does that for you automatically before calling your drawing method (drawRect: in a GLKView subclass, or glkView:drawInRect: if you're doing your drawing from the view's delegate). That's not your problem, though -- it's just redundant state setting (which Instruments or the Xcode Frame Debugger will probably tell you about when you use them).

If you want to zoom in on the contents of the view rather than resizing the view, you'll need to change how you're drawing those contents. Luckily, you're already set up well for doing that because you're already adjusting the ModelView and Projection matrices in your update method. Those control how vertices are transformed from model to screen space -- and part of that transformation includes a notion of a "camera" you can adjust to affect how near/far the objects in your scene appear.

In 3D rendering (as in real life), there are two ways to "zoom":

  • Move the camera closer to / farther from the point it's looking at. The translation matrix you're using for your modelViewMatrix is what sets the camera distance (it's the z parameter you currently have fixed at -6.0). Keep track of / change a distance in your pinch recognizer handler and use it when creating the modelViewMatrix if you want to zoom this way.

  • Change the camera's field of view angle -- this is what happens when you adjust the zoom lens on a real camera. This is part of the projectionMatrix (the first parameter, currently fixed at 65 degrees). Keep track of / change the field of view angle in your pinch recognizer handler and use it when creating the projectionMatrix if you want to zoom this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top