如何在min3d Android中平移放大缩小功能

时间:2016-08-22 12:13:39

标签: java android opengl-es min3d

我是OpenGl Devloper的新手,我想在OpenGl android中渲染3d模型,所以我选择了min3d框架库。

我想平移放大我模型的缩小功能,比如相机在min3d中放大缩小

喜欢,我有3d模型的女士,我想缩小她的脸,任何没有缩放对象的解决方案?

2 个答案:

答案 0 :(得分:1)

您需要更改相机位置。我建议你定义一些额外的参数centerdistance,因为你想要平移。

当您有缩放手势时,您只需将缩放比例应用于距离distance *= scale(或distance = originalDistance*scale,具体取决于实施方式)。

在平底锅上,您只需将center移动距离center.x += (newXOnScreen-oldXOnScreen)*speedFactorcenter.y += (newYOnScreen-oldYOnScreen)*speedFactor即可。速度因子在这里可以保持不变(与它一起玩一下)但是最好将它乘以缩放比例,这样如果它非常接近,中心将移动得更少。

现在您有这两个参数,您需要将它们应用到相机。假设您在(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}

答案 1 :(得分:0)

以下代码段对我有用。它不是完整的代码,但我刚刚添加了在min3d框架中进行缩放缩放和拖拽3d对象的部分。我通过遵循here的在线教程并根据应用程序

进行修改来整理代码
    //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;
              }
         }

    } 

对于min3D初始化,请按照教程herehere

进行操作