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

我在渲染的模型的时候定义了三个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

学习是最好的投资!

répondre à tous(1)
迷茫

Je ne le comprends pas très bien, alors voici quelques idées :
1. Le premier problème semble être que l'objet pivote en fonction de ses propres coordonnées, il sera donc perturbé par sa propre direction. Ce devrait être la matrice avant que la transformation de perspective ne soit transmise, c'est-à-dire que le système de coordonnées mondial est la norme.
2. L'axe Z est la direction face à l'écran. La rotation le long de l'axe Z doit être un geste de rotation de deux doigts sur l'écran. Utilisez le geste UIRotationGestureRecognizer

.
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal