Pro OpenGL ES for iOS

(singke) #1

CHAPTER 8: Putting It All Together (^251)



  • (IBAction)handlePinchGesture:(UIGestureRecognizer )sender
    {
    static float startFOV=0.0;
    CGFloat factor = [(UIPinchGestureRecognizer
    )sender scale];
    UIGestureRecognizerState state;


state=sender.state;


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


currentFOV=startFOV*factor;


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


handlePanGesture() calculates the difference in the touch location from the previous call
as a finger is dragged across the screen. It feeds those differences to
setHoverPosition(), which will then move your eye point to a new position over the
earth. Add CGPoint m_PointerLocation to the header.


The other handler, handlePinchGesture(), handles pinches. The
UIPinchGestureRecognizer() returns a simple magnification value starting with
1.0 when the gesture begins. As the gesture continues, the state changes to
UIGestureRecognizerStateChanged, and the scale value increases for an expanding
pinch to indicate a zoom-in operation or decreases when the person is zooming out.
Here it is necessary to cache the starting field of view of the display, because each scale
value is cumulative vs. a delta from the previous event. That way, we’re scaling up the
original value each time as opposed to rescaling the field of view, which would be done
if we only had the deltas to play with. Otherwise, our FOV would jump in successively
larger jumps. Also, notice that I did place limits to the range of values of the FOV, from
5° to 75°.


It is now necessary to add to your solar-system handler the instance variable float
m_FieldOfView, along with the accessor methods in Listing 8-4, and initialize it to 30°.


Listing 8-4. Accessors for m_FieldOfView in the Solar-System Controller


-(float)getFieldOfView
{
return m_FieldOfView;
}

Free download pdf