0

I am using rajawali library for loading .obj file.

My .obj rendering perfectly fine but not able to apply pinch and zoom functionality to it.

4

2 回答 2

1

Have solved using ScaleGestureDetector.

Following is the sample code:

public class MainActivity extends RajawaliExampleActivity implements OnTouchListener, OnObjectPick {// ,
                                                                                                    // BackfromTemple
                                                                                                    // {
private RajawaliLoadModelRenderer mRenderer;
private float mPreviousX;
// private float mPreviousY;
private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private ScaleGestureDetector mScaleDetector;
private boolean isRotationStarted;
private boolean firsttime = false;
private Camera cam;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mRenderer = new RajawaliLoadModelRenderer(this);
    mRenderer.setSurfaceView(mSurfaceView);
    super.setRenderer(mRenderer);
    mSurfaceView.setOnTouchListener(this);
    initLoader();
    mScaleDetector = new ScaleGestureDetector(getBaseContext(), mRenderer);

    firsttime = false;
    cam = mRenderer.getCamera();
    mSurfaceView.setPreserveEGLContextOnPause(true);
}


void showProgressDialog(final Context c, final String msg) {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            pd = new ProgressDialog(c);
            pd.setMessage(msg);
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            if (!pd.isShowing()) {
                pd.show();
            }
        }
    });

}

protected void hideProgressDialog() {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (pd != null && pd.isShowing()) {
                pd.dismiss();
            }
        }
    });

}

@Override
protected void onRestart() {
    super.onRestart();

    firsttime = true;
    isRotationStarted = false;

    mRenderer.mPickedObject = null;
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (SuperActivity.pd != null && SuperActivity.pd.isShowing()) {
                SuperActivity.pd.dismiss();
            }
        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent e) {
    mScaleDetector.onTouchEvent(e);
    float x = e.getX();
    float y = e.getY();

    if (e.getAction() == MotionEvent.ACTION_MOVE) {
        float dx = x - mPreviousX;
        // float dy = y - mPreviousY;
        if (Math.abs(dx) > 2.5) {
            isRotationStarted = true;
            mRenderer.mObjectGroup.getRotation().y -= dx * TOUCH_SCALE_FACTOR;
        }
        // else if (Math.abs(dy) > 2.5) {
        // isRotationStarted = true;
        // mRenderer.mObjectGroup.getRotation().x -= dy *
        // TOUCH_SCALE_FACTOR;
        // }
    }

    mPreviousX = x;
    return false;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    mScaleDetector.onTouchEvent(event);
    float x = event.getX();
    // float y = event.getY();

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mRenderer.getObjectAt(event.getX(), event.getY());
        // if (mRenderer.isObjectPicked) {
        // showProgressDialog();
        // }
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        // if (mRenderer.isObjectPicked) {
        float dx = x - mPreviousX;
        // float dy = y - mPreviousY;
        if (Math.abs(dx) > 2.5 && mRenderer.mObjectGroup != null) {
            isRotationStarted = true;
            mRenderer.mObjectGroup.getRotation().y -= dx * TOUCH_SCALE_FACTOR;
        }
        // else if (Math.abs(dy) > 2.5 && mRenderer.mObjectGroup != null) {
        // isRotationStarted = true;
        // mRenderer.mObjectGroup.getRotation().x -= dy *
        // TOUCH_SCALE_FACTOR;
        // }
        // }
    } else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {

        isRotationStarted = false;

        mRenderer.mPickedObject = null;

        // runOnUiThread(new Runnable() {
        // @Override
        // public void run() {
        // if (!mRenderer.isObjectPicked) {
        // hideProgressDialog();
        // isRotationStarted = false;
        // mRenderer.isObjectPicked = false;
        // mRenderer.mPickedObject = null;
        // }
        // }
        // });
    }

    mPreviousX = x;
    // mPreviousY = y;

    return true;
}

@Override
public void objSelected(BaseObject3D object) {

    final Intent i = new Intent(this, newactivity.class);
    String objName = object.getName();


    isRotationStarted = false;

    mRenderer.mPickedObject = null;
    startActivity(i);
}}
于 2013-09-20T12:39:10.200 回答
0

Easy way to achieve it by using ArcballCamera

ArcballCamera arcball = new ArcballCamera(mContext, ((Activity)mContext).findViewById(R.id.linear_layout));
        arcball.setTarget(object3D); //your 3D Object
        arcball.setPosition(0,0,4); //optional
        //arcball.setScreenMappingRatio(-1); // Not yet merged at master branch as of now

getCurrentScene().replaceAndSwitchCamera(getCurrentCamera(), arcball);
于 2017-09-22T14:23:42.267 回答