ios - OpenGL旋转问题,控制模型根据手势移动
PHPz
PHPz 2017-04-18 09:03:55
0
1
372

我在渲染的模型的时候定义了三个6个变量

    float _rotationX;//旋转变量x
    float _rotationZ;//旋转变量z
    float _rotationY;//旋转变量y
    float _lastRotationX;//最后一个旋转落点得x
    float _lastRotationZ;//最后一个旋转落点得z
    float _lastRotationY;//最后一个旋转落点的y

在update方法内有如下方法

GLKMatrix4 modelViewMatrix             = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, 0.0f);
    modelViewMatrix                        = GLKMatrix4Rotate(modelViewMatrix, _rotation, 0.0f, 0.0f, 1.0f);   //旋转
    modelViewMatrix                        = GLKMatrix4RotateX(modelViewMatrix, GLKMathDegreesToRadians(_rotationX));
    modelViewMatrix                        = GLKMatrix4RotateZ(modelViewMatrix, GLKMathDegreesToRadians(_rotationZ));
    modelViewMatrix                        = GLKMatrix4RotateY(modelViewMatrix, GLKMathDegreesToRadians(_rotationY));

然后我在手势的移动方法内事这样写的

#pragma  mark  实现单指头拖拽旋转
    UIGestureRecognizerState state = [sender state];
    //判断当手指开始移动或者改变的时候
    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        //声明一个点为当前手指落在屏幕上的点
        CGPoint translation = [sender translationInView:sender.view];
            _rotationY  =  translation.x - _lastRotationX;
            _rotationX  =  translation.y - _lastRotationY ;
        
    
    }
    if (state == UIGestureRecognizerStateEnded) {
       
       //_lastRotationX,Y,Z为用户手指离开屏幕所记录的点
        _lastRotationX = -_rotationY;
        _lastRotationZ =  _rotationZ;
        _lastRotationY = -_rotationX;
        
         NSLog(@"_rotationY = %f",_rotationY);
    }

我现在的出现的问题是
第一:当模型被旋转颠倒后,在去旋转X轴方向就倒过来了(就是说一个小猫刚开始旋转左右旋转是正常的左右方向,如果你把她上下颠倒后,在旋转就是右左方向,反方向旋转了)
第二:我没有添加Z的旋转体系,所以模型的侧面怎么都旋转不到(我是不知道怎么添加。。。。)
求解惑

PHPz
PHPz

学习是最好的投资!

reply all(1)
迷茫

I don’t understand it very well, so here are some ideas:
1. The first problem seems to be that the object is rotated based on its own coordinates, so it will be interfered by its own direction. It should be the matrix before the perspective transformation is passed in, that is, the world coordinate system is the standard.
2. The Z axis is the direction facing the screen. Rotation along the Z axis should be a gesture of rotating two fingers on the screen. Use the UIRotationGestureRecognizer gesture

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template