ios - OpenGL ES 2.0 Pinch and Zoom -
in opengl es 1.1 on ios used implement pinch , zoom setting field of view using following:
// 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]; } }
using pinch gesture this:
// set fulstrum , our field of view window -(void)setclipping { // near , far front , walls // fov in degrees float aspectratio; const float znear = .1; const float zfar = 2000; glfloat size; float scale; // main screen , define aspect ratio cgrect frame = [[uiscreen mainscreen] bounds]; aspectratio=(float)frame.size.width/(float)frame.size.height; scale=[[uiscreen mainscreen]scale]; // use 2d projection matrix project our 3d 2d glmatrixmode(gl_projection); glloadidentity(); if (m_fieldofview > 75.0) { m_fieldofview = 75.0; } size = znear * tanf(glkmathdegreestoradians (m_fieldofview) / 2.0); // define pyramid of giza (4 sided pyramid top lopped off on side) // ... how viewing things glfrustumf(-size, size, -size/aspectratio, size/aspectratio, znear, zfar); glviewport(0, 0, frame.size.width*scale, frame.size.height*scale); // safe go tranformational matrix glmatrixmode(gl_modelview); }
i made simple opengl es 2.0 application , update method looks ( partially ) this:
#pragma mark - glkview , glkviewcontroller delegate methods - (void)update { // set frustrum , 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;
i've been scouring web how opengl es 2.0... no avail. how do in 2.0?
multiply first term of glkmatrix4makeperspective factor (a property or class variable) , change factor in gesture recogniser - not different in 1.1 method.
here method call gesturerecogniser. variables starting _ class variables. zoomtouchsensitivity preprocessor define.
-(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; } }
note how i'm saving last value of scale
once gesture stops, zoom isn't 'reset' everytime.
Comments
Post a Comment