0

我是安卓新手。我在 opengl 上工作了 2 周,现在卡在了某个时间点。我可以通过单指触摸拖动 3d 图像并通过 2 指触摸放大/缩小。但是在一次触摸之后,当第二次开始缩放时,它不会从先前的状态渲染图像,而是首先缩放 3d 对象,然后开始放大/缩小。

这是我的代码。请看一下..

public class AnimateActivity extends RendererActivity 
{
Object3dContainer objModel;




// touch events
    private final int NONE = 0;
    private final int DRAG = 0;
    private final int ZOOM = 0;

    // pinch to zoom
    float oldDist = 100.0f;
    float newDist;

    int mode = 0;

    private float mPreviousX;
    private float mPreviousY;
    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;

    private GLSurfaceView mGLSurfaceView;

    Renderer renderer;
@Override
public void initScene() {

    scene.lights().add(new Light());

    IParser parser = Parser.createParser(Parser.Type.OBJ,
            getResources(),"com.example.magic3d:raw/camaro_obj", true);
    parser.parse();

    //mGLSurfaceView=new GLSurfaceView(this);
    objModel = parser.getParsedObject();
//  objModel.scale().x = objModel.scale().y = objModel.scale().z = .8f;
    objModel.scale().x = objModel.scale().y = objModel.scale().z = .8f;
    scene.addChild(objModel);
    scene.backgroundColor().setAll(Color.WHITE);
    renderer=new Renderer(scene);
}

@Override
public void updateScene() {
    /*objModel.rotation().x++;
    objModel.rotation().z++;*/

}

public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        mGLSurfaceView=new GLSurfaceView(this);
}

@Override public boolean onTouchEvent(MotionEvent e) {

    float x = e.getX();
    float y = e.getY();
    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:           // one touch: drag
        Log.d("ShaderActivity", "mode=DRAG" );
        System.out.println("Action Down");
        mode = DRAG;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:   // two touches: zoom
        Log.d("ShaderActivity", "mode=ZOOM" );
        System.out.println("Action zoom");
        oldDist = spacing(e);
        if (oldDist > 10.0f) {
            mode = ZOOM; // zoom
        }
        break;
    case MotionEvent.ACTION_UP:     // no mode
        mode = NONE;
        System.out.println("Action up");
        Log.d("ShaderActivity", "mode=NONE" );
        oldDist = 100.0f;
        break;
    case MotionEvent.ACTION_POINTER_UP:     // no mode
        mode = NONE;
        Log.d("ShaderActivity", "mode=NONE" );
        oldDist = 100.0f;
        break;
    case MotionEvent.ACTION_MOVE:   
        System.out.println("Action Move");
        // rotation
        if (e.getPointerCount() > 1 && mode == ZOOM) {
            newDist = spacing(e);
            Log.d("SPACING: ", "OldDist: " + oldDist + ", NewDist: " + newDist);
            if (newDist > 15.0f) {
                float scale = newDist/oldDist; // scale
                // scale in the renderer
                renderer.changeScale(scale);
                objModel.scale().x =renderer.scaleX; 
                        objModel.scale().y = renderer.scaleY ;
                                objModel.scale().z =renderer.scaleZ;

                oldDist = newDist;
            }
        }
        else if (mode == DRAG){
     float dx = x - mPreviousX;
     float dy = y - mPreviousY;
     renderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
      renderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
                //mGLSurfaceView.requestRender();
    updateScene();
                objModel.rotation().x=renderer.mAngleY;
                objModel.rotation().y=renderer.mAngleX;
    //            objModel.rotation().z=
           //Toast.makeText(getApplicationContext(), "action move downl", Toast.LENGTH_SHORT).show();
       //   updateScene();
        }
    }
    mPreviousX = x;
    mPreviousY = y;
    return true;
}
    // finds spacing
    private float spacing(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);
    }

}

谢谢....

4

0 回答 0