mitesh gandhi
mitesh gandhi

Reputation: 31

How to Pan Zoom in Zoom out Functionality in min3d Android

I am new in OpenGl Devloper,i want to render 3d model in OpenGl android so i choose min3d framework library.

I want to Pan Zoom in zoom out functionality for my model ,like camera zoom in zoom out in min3d

like , i have 3dmodel of lady and i want to zoom out her face any solution without scale object?

Upvotes: 1

Views: 750

Answers (2)

Lakshmi Narayanan
Lakshmi Narayanan

Reputation: 5342

The following code snippet worked for me. It is not the complete code, but I have just added the part which does the pinch zoom and drag of the 3d object in min3d framework. I put together the code by following online tutorials from here and modified according to the application

    //import statements
    public class threedviewActivity extends RendererActivity {
         private Object3dContainer Object3D;
         private ScaleGestureDetector mScaleDetector;
         private float mScaleFactor = 1.f,mLastTouchX,mLastTouchY,mPosX,mPosY;
         private int mActivePointerId = INVALID_POINTER_ID,flag;

         @Override
         public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              mScaleDetector = new ScaleGestureDetector(threedviewActivity.this, new ScaleListener());

         }

         @Override
         public boolean onTouchEvent(MotionEvent ev) {
              // Let the ScaleGestureDetector inspect all events.
              mScaleDetector.onTouchEvent(ev);
              final int action = MotionEventCompat.getActionMasked(ev);

              switch (action) {
                   case MotionEvent.ACTION_DOWN: {
                        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
                        final float x = MotionEventCompat.getX(ev, pointerIndex);
                        final float y = MotionEventCompat.getY(ev, pointerIndex);

                        mLastTouchX = x;
                        mLastTouchY = y;

                        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
                        updateScene();
                        break;
                    }

                    case MotionEvent.ACTION_MOVE: {
                         // Find the index of the active pointer and fetch its position
                         final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
                         final float x = MotionEventCompat.getX(ev, pointerIndex);
                         final float y = MotionEventCompat.getY(ev, pointerIndex);

                         final float dx = x - mLastTouchX;
                         final float dy = y - mLastTouchY;

                         mPosX += dx;
                         mPosY += dy;

                         // Remember this touch position for the next move event
                         mLastTouchX = x;
                         mLastTouchY = y;
                         flag = 1;
                         updateScene();
                         break;
                    }

                    case MotionEvent.ACTION_UP: {
                         mActivePointerId = INVALID_POINTER_ID;
                         break;
                    }

                    case MotionEvent.ACTION_CANCEL: {
                         mActivePointerId = INVALID_POINTER_ID;
                         break;
                    }

                    case MotionEvent.ACTION_POINTER_UP: {

                         final int pointerIndex = MotionEventCompat.getActionIndex(ev);
                         final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);

                         if (pointerId == mActivePointerId) {
                              // This was our active pointer going up. Choose a new
                             // active pointer and adjust accordingly.
                             final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                             mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
                             mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
                             mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
                         }
                         break;
                    }
             }
             return true;
         }

         @Override
         public void initScene()
         {
               //this is where you initialize your 3d object. Check below for links for tutorials on that.
         }

         private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
              @Override
              public boolean onScale(ScaleGestureDetector detector) {
                   mScaleFactor *= detector.getScaleFactor();

                   // Don't let the object get too small or too large.
                   mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
                   Object3D.scale().x = Object3D.scale().y = Object3D.scale().z = mScaleFactor*1.5f;
                   flag = 0;
                   updateScene();
                   return true;

              }
         }

         @Override
         public void updateScene()
         {
              if(flag == 1)
              {
                   Object3D.position().x = mPosX/100;
                   Object3D.position().y = -mPosY/100;
              }
         }

    } 

For initalizing min3D, follow the tutorials here and here

Upvotes: 0

Matic Oblak
Matic Oblak

Reputation: 16774

You need to change the camera position. I suggest you define some additional parameters center and distance since you want to pan.

When you have a zoom gesture you simply apply zoom scale to distance distance *= scale (or distance = originalDistance*scale depending on the implementation).

On pan you simply move the center by distances center.x += (newXOnScreen-oldXOnScreen)*speedFactor and center.y += (newYOnScreen-oldYOnScreen)*speedFactor. The speed factor may be constant here (play around a bit with it) but it might be best to multiply it by a zoom scale as well so that if it is very close the center will move less.

Now that you have these 2 parameters you need to apply them to the camera. Assuming you have a model position at (0,0,0):

scene.camera.position = {center.x, center.y, center.z-distance}
scene.camera.target = {center.x, center.y, center.z}
scene.camera.up = {0, 1, 0}

Upvotes: 1

Related Questions