1

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?

  • Do you have an access to lookAt matrix? Usually you use the view matrix to which you apply the lookAt procedure which accepts the eye position, center toward you are looking at and up vector. With a little bit of math you can easily apply zoom, pan and rotation functionality with it. – Matic Oblak Aug 23 '16 at 06:58
  • i want to touch event ,so i use on touch event in this i write this code GLU.gluLookAt(Shared.renderer().gl(), scene.camera().position.x,scene.camera().position.y,scene.camera().position.z, scene.camera().target.x+1,scene.camera().target.y+1,scene.camera().target.z+1, scene.camera().upAxis.x,scene.camera().upAxis.y,scene.camera().upAxis.z); Can you give code..? – mitesh gandhi Aug 23 '16 at 08:58

2 Answers2

1

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}
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • hello, how to get newYonscreen and newXonScreen ?and how to get origanldistance. on touch event – mitesh gandhi Aug 23 '16 at 10:17
  • You usually track the dragging events on which you get the finger position which is (newXOnScreen, newYOnScreen) you preserve these to (oldXOnScreen, oldYOnScreen) so on next event you can subtract the 2 to get the vector for how much the finger was actually moved and apply this to the panning. Original distance is the distance you use before the user starts zooming. It is any value you choose to apply in the beginning so you have the optimal view on the model. – Matic Oblak Aug 23 '16 at 10:21
  • Number3d number3d=new Number3d(event.getX(0),event.getY(0),scene.camera().position.z-0.5f); scene.camera().position = number3d; scene.camera().target = number3d; scene.camera().upAxis.y =1; : i am writing this code and my model not display – mitesh gandhi Aug 23 '16 at 10:29
0

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

Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92