1

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

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

像,我有女士的 3dmodel,我想缩小她的脸,没有缩放对象的任何解决方案?

4

2 回答 2

1

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

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

在平移上,您只需移动center距离center.x += (newXOnScreen-oldXOnScreen)*speedFactorcenter.y += (newYOnScreen-oldYOnScreen)*speedFactor。速度因子在这里可能是恒定的(稍微玩一下),但最好也将它乘以缩放比例,这样如果它非常接近,中心移动得更少。

现在您有了这 2 个参数,您需要将它们应用到相机。假设您在以下位置有模特职位(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}
于 2016-08-23T09:32:40.453 回答
0

以下代码片段对我有用。这不是完整的代码,但我刚刚添加了在 min3d 框架中对 3d 对象进行缩放和拖动的部分。我按照这里的在线教程整理了代码,并根据应用程序进行了修改

    //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,请按照此处此处的教程进行操作

于 2016-10-15T18:33:16.247 回答