放大/缩小OpenGL ES

时间:2012-04-04 11:08:55

标签: android opengl-es zooming

我想学习如何进行放大和缩小。这是旋转的2D三角形的简单示例。你能告诉我如何添加放大和缩小这个简单的例子吗?

我应该使用gl.glScalef吗?怎么样 ?

public class GLrenderer implements Renderer {
    public GLqueue tri;
    private float angle = 0;

    public GLrenderer() {
        tri = new GLqueue();
    }

    @Override
    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);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -10);
        gl.glPushMatrix();
        gl.glRotatef(angle, 0, 0, 1);
        tri.draw(gl);
        gl.glPopMatrix();
        angle++;
    }

    @Override
    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);
    }
}

这是我的活动类,并添加了onTouch个事件。我应该添加什么,以便当我触摸屏幕缩放时有效?

GLSurfaceView OurSurface;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    OurSurface = new GLSurfaceView(this);
    OurSurface.setRenderer(new GLrenderer());
    setContentView(OurSurface);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    switch (action) {
        case (MotionEvent.ACTION_DOWN): // Touch screen pressed
            break;
        case (MotionEvent.ACTION_UP): // Touch screen touch ended
            break;
        case (MotionEvent.ACTION_MOVE): // Contact has moved across screen
            break;
        case (MotionEvent.ACTION_CANCEL): // Touch event cancelled
            break;
    }
    return super.onTouchEvent(event);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    OurSurface.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    OurSurface.onResume();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}

3 个答案:

答案 0 :(得分:5)

从技术上讲,实现相机缩放的正确方法是修改透视投影的参数:

float zoom = 1.5;
// ....
gl.glFrustumf(-ratio/zoom, ratio/zoom, -1/zoom, 1/zoom, 1, 25);

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

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

取决于哪种方法更适合您的应用。

如果您希望缩放在应用程序中间更改,那么您还必须移动这些行:

float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, zoom, 25);

进入你的绘图程序,以便在每一帧上调用它们。

答案 1 :(得分:2)

您可以通过glScalef缩放模型或移动相机。由于缩放会影响所有绘制的对象,因此移动相机可能是更好的方法。插入

gl.glTranslatef(0,0, 5.0f * Math.sin(Math.PI * ((angle % 360.0f)/180.0f)));

glLoadIdentity中致电onDrawFrame之后。这个应该 - 没有运行代码 - 导致对象正反向移动。

考虑将angle的值限制为[0.0f;360.0f]

答案 2 :(得分:0)

您可以在Activity类的onTouch()方法中计算缩放值并使用它 onDrawFrame()中的glTranslatef()方法为

gl.glTranslatef(0,0,-zoom);  就在glLoadIdentity()..

之后

它会起作用...... 如果你被困住,请告诉我......