zoom out in OpenGL in android

前端 未结 3 1660
执笔经年
执笔经年 2021-02-09 21:47

I m using 3D object and rendering it and showing it by extends GLSurfaceView implementing Renderer, the problem is that how to do zoom out-in with pinch-in and pinch-out.

相关标签:
3条回答
  • 2021-02-09 22:13

    I would recommend looking at GLU.glLookAt() which allows you to set the eye and center point. You can then move your eye forward and back based on your zoom value.

    Alternatively you can use the android.openglMatrix class. And use perspectiveMatrix and lookAtMatrix functions then multiple those together and set them as your projectionMatrix.

    0 讨论(0)
  • 2021-02-09 22:25

    The SDK can help you a bit, using the standard Gesture detector. More details on how to implement this ScaleGestureDetector can be found here.

    The basic step is that you pass the touch events into the ScaleGestureDetector and you will receive a callback on the listener when a scale event has happened, where you change your zoom state.

    0 讨论(0)
  • 2021-02-09 22:35

    You need to implement OnScaleGestureDetector and create ScaleGestureDetector to listen pinch-in and pinch-out events.

    For example I use it as an inner class of GLSurfaceView:

    ...
    
    private float sizeCoef = 1;
    
    ...
    
    private class ScaleDetectorListener implements ScaleGestureDetector.OnScaleGestureListener{
    
        float scaleFocusX = 0;
        float scaleFocusY = 0;
    
        public boolean onScale(ScaleGestureDetector arg0) {
            float scale = arg0.getScaleFactor() * sizeCoef;
    
            sizeCoef = scale;
    
            requestRender();
    
            return true;
        }
    
        public boolean onScaleBegin(ScaleGestureDetector arg0) {
            invalidate();
    
            scaleFocusX = arg0.getFocusX();
            scaleFocusY = arg0.getFocusY();
    
            return true;
        }
    
        public void onScaleEnd(ScaleGestureDetector arg0) {
            scaleFocusX = 0;
            scaleFocusY = 0;
        }
    } 
    

    Also You have to add some code to onDrawFrame() method:

    public void onDrawFrame(GL10 gl) {
        //Clear Screen And Depth Buffer
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
        gl.glLoadIdentity();                    
        gl.glEnable(GL10.GL_LIGHTING);
        gl.glTranslatef(0.0f, -1.2f, -z);   //Move down 1.2 Unit And Into The Screen 6.0
        gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
        gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y
    
        gl.glScalef(sizeCoef, sizeCoef, 0);  // if You have a 3d object put sizeCoef as all parameters
    
        model.draw(gl);                     //Draw the square
        gl.glLoadIdentity();        
        xrot += xspeed;
        yrot += yspeed;
    
    }
    
    0 讨论(0)
提交回复
热议问题