Android捏/缩放和glfrustum

时间:2012-05-04 13:24:44

标签: android opengl-es zoom

我使用OpenGL绘制了几个2D形状,现在我想添加捏合/缩放。我的观点是观点(顶视图)。我假设它是3D,z轴= 0。

现在,我应该如何更改glfrustum并在我的活动中添加触摸方法,以便我能够捏/缩放?

gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);

我认为它应该像

gl.glFrustumf(-ratio, ratio, -1, 1, 1*zoom, 25*zoom);

但是如何编写触摸方法来更改此缩放参数以进行两个手指缩放?

问题是rendrer现在,我在最后添加了zoom方法,但是在zoom方法中,它给了我gl.glfrustum的错误,而onsurfacechanged它不会给我同样的错误!我怎么能解决这个问题?

public class GLrenderer implements Renderer {
    public GLqueue tri;
    public GLrenderer() {
        tri = new GLqueue();


    }


    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
        // TODO Auto-generated method stub
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(.0f, .0f, .0f, 0f);
        gl.glClearDepthf(1f);
    }


    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub
        gl.glDisable(GL10.GL_DITHER);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);

        //gl.glRotatef(1, 1, 0, 0);
    //  gl.glRotatef(10, 0, 0, 1);
        tri.draw(gl);

    }

    public void onSurfaceChanged(GL10 gl, int width, int height ) {
        // TODO Auto-generated method stub
        gl.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
    }

    public static void zoom(float d2) {
        // TODO Auto-generated method stub
          int zoom = (int) (zoom*d2);

                gl.glFrustumf(-ratio, ratio, -1, 1, 1*zoom, 25*zoom);

    }

    }

1 个答案:

答案 0 :(得分:4)

如果您有一些确定缩放的浮点值,则可以使用:

glFrustum(left*zoom, right*zoom, bottom*zoom, top*zoom, near, far);

以下是一个例子:

摘自OnTouchListener

public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        pointers = 1;
        return true;
    case MotionEvent.ACTION_POINTER_2_DOWN:
        pointers = 2;
        distance = fingerDist(event);
        return true;
    case MotionEvent.ACTION_MOVE:
        if( pointers == 2 ){
            float newDist = fingerDist(event);
            float d = distance/newDist;
            renderer.zoom(d);
            distance = newDist;
        }
        return true;
    default:
        return false;
    }

    }

protected final float fingerDist(MotionEvent event){
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
}

来自renderer

public final void zoom(float mult){
    zoom *= mult;
    Matrix.frustumM(
            pMatrix, 0, 
            zoom*left, zoom*right, zoom*bottom, zoom*top, 
            near, far);
}

请注意,此代码是针对gles 2.0而不是1.0编写的,因此您可以调用Matrix.frustumM()而不是调用gl.glFrustumf()。您可能必须将此操作发布到gl-thread以使其正常工作。