我使用OpenGL绘制了几个2D形状,现在我想添加捏/缩放.我的观点是观点(顶视图).我假设它是3D,z轴= 0.
现在,我应该如何更改glfrustum并在我的活动中添加触摸方法,以便我能够捏/缩放?
gl.glFrustumf(-ratio,ratio,-1,1,25);
我认为应该是这样的
gl.glFrustumf(-ratio,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);
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,-5,2,0);
//gl.glRotatef(1,0);
// gl.glRotatef(10,1);
tri.draw(gl);
}
public void onSurfaceChanged(GL10 gl,int width,int height ) {
// TODO Auto-generated method stub
gl.glViewport(0,width,height);
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio,25);
}
public static void zoom(float d2) {
// TODO Auto-generated method stub
int zoom = (int) (zoom*d2);
gl.glFrustumf(-ratio,25*zoom);
}
}
最佳答案
如果您有一个确定缩放的浮点值,则可以使用:
原文链接:https://www.f2er.com/android/430599.htmlglFrustum(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);
}
来自渲染器:
public final void zoom(float mult){
zoom *= mult;
Matrix.frustumM(
pMatrix,zoom*left,zoom*right,zoom*bottom,zoom*top,far);
}
请注意,此代码是针对gles 2.0而不是1.0编写的,因此不要调用Matrix.frustumM(),而是调用gl.glFrustumf().您可能必须将此操作发布到gl-thread以使其正常工作.