本文介绍了Android捏/缩放和glfrustum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I draw several 2D shapes using OpenGL and now I want to add the pinch/zoom. My view is perspective (top view). I assumed that it's 3D with z axis = 0.

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

Now, how should I change glfrustum and add on a touch method in my activity so that I could be able to pinch/zoom?

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

我认为应该是

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

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

but how should the touch method be written to change this zoom parameter for doing two finger zoom?

现在的问题是rendrer,我在末尾添加了zoom方法,但是在zoom方法中,gl.glfrustum给了我错误,而onsurfacechanged却没有给我同样的错误!我该如何解决?

the problem is rendrer right now , I add zoom method at the end , but in zoom method it gives me error with gl.glfrustum while on onsurfacechanged it does not give me error with the same thing ! how could I fix that ?

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);

    }

    }

推荐答案

如果您的浮动值决定了缩放比例,则可以使用以下方法:

If you have some float value that determines your zoom, you can use this:

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编写的,因此可以调用gl.glFrustumf()而不是调用Matrix.frustumM().您可能必须将此操作发布到gl-thread才能正常工作.

Note that this code is written for gles 2.0 rather then 1.0, so instead of calling Matrix.frustumM() you would call gl.glFrustumf(). You might possibly have to post this operation to your gl-thread for it to work properly.

这篇关于Android捏/缩放和glfrustum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:11