我想学习如何执行放大和缩小。这是一个旋转的二维三角形的简单示例。你能告诉我如何在这个简单的例子中添加放大和缩小功能吗?
我应该使用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;
}