沿x轴和y轴旋转



我在我的opengl-es 2.0 3D应用程序中使用GLKit和PowerVR库。3D场景加载几个网格,模拟车库环境。我有一辆车在车库中央。我试图在应用程序中添加触摸处理,用户可以旋转房间(例如,看到汽车周围的所有4面墙)。我还想允许在x轴上旋转,尽管限制在一个小范围内。基本上,他们可以看到从汽车顶部的一小部分到地板以上的水平。

我可以在Y轴或X轴上旋转,但不能两者都旋转。只要我在两个轴上旋转,汽车就会偏离轴。车跟摄像机不齐了。我希望我能解释得更好,但希望你们能理解。

这是我的触摸实现:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];    
    CGPoint lastLoc = [touch previousLocationInView:self.view];
    CGPoint diff = CGPointMake(lastLoc.x - location.x, lastLoc.y - location.y);
    float rotX = -1 * GLKMathDegreesToRadians(diff.x / 4.0);
    float rotY = GLKMathDegreesToRadians(diff.y / 5.0);
    PVRTVec3 xAxis = PVRTVec3(1, 0, 0);
    PVRTVec3 yAxis = PVRTVec3(0,1,0);
    PVRTMat4 yRotMatrix, xRotMatrix;
    // create rotation matrices with angle
    PVRTMatrixRotationXF(yRotMatrix, rotY);
    PVRTMatrixRotationYF(xRotMatrix, -rotX);
    _rotationY = _rotationY * yRotMatrix;
    _rotationX = _rotationX * xRotMatrix;
}

这是我的更新方法:

- (void)update {
    // Use the loaded effect
    m_pEffect->Activate();

    PVRTVec3    vFrom, vTo, vUp;
    VERTTYPE    fFOV;
    vUp.x = 0.0f;
    vUp.y = 1.0f;
    vUp.z = 0.0f;
    // We can get the camera position, target and field of view (fov) with GetCameraPos()
    fFOV = m_Scene.GetCameraPos(vFrom, vTo, 0);
    /*
     We can build the world view matrix from the camera position, target and an up vector.
     For this we use PVRTMat4LookAtRH().
     */
    m_mView = PVRTMat4::LookAtRH(vFrom, vTo, vUp);
    // rotate the camera based on the users swipe in the X direction (THIS WORKS)
    m_mView = m_mView * _rotationX;
    // Calculates the projection matrix
    bool bRotate = false;
    m_mProjection = PVRTMat4::PerspectiveFovRH(fFOV, (float)1024.0/768.0, CAM_NEAR, CAM_FAR, PVRTMat4::OGL, bRotate);
}

我尝试先将新的X旋转矩阵乘以当前场景旋转,然后再乘以新的Y旋转矩阵。我试过反过来,认为乘法的顺序是我的问题。但这无济于事。然后我尝试在乘以当前旋转之前将新的X和Y旋转矩阵添加在一起,但这也不起作用。我觉得我已经很接近了,但是现在我就是没有主意了。

你们能帮忙吗?谢谢。瓦莱丽

Update:为了解决这个问题,我试图简化它一点。我已经更新了上面的代码,删除了Y旋转范围内的任何限制。基本上我是基于用户在屏幕上的滑动来计算X和Y旋转。

如果我理解正确,我想我想旋转视图矩阵(相机/眼睛)与_rotationX的计算。

我认为我需要使用World矩阵(原点0,0,0)来进行_rotationY计算。我将试着获取一些我正在谈论的内容的图像

哇,成功了!我用X旋转矩阵旋转视图矩阵(由LookAt方法创建)。我用Y旋转矩阵旋转模型视图矩阵。

下面是修改后的Update方法:

- (void)update {
    PVRTVec3    vFrom, vTo, vUp;
    VERTTYPE    fFOV;
    // We can get the camera position, target and field of view (fov) with GetCameraPos()
    fFOV = m_Scene.GetCameraPos(vFrom, vTo, 0);
    /*
     We can build the world view matrix from the camera position, target and an up vector.
     For this we use PVRTMat4LookAtRH().
     */
    m_mView = PVRTMat4::LookAtRH(vFrom, vTo, PVRTVec3(0.0f, 1.0f, 0.0f));
    // rotate on the X axis (finger swipe Y direction)
    m_mView = m_mView * _rotationY;
    // Calculates the projection matrix
    m_mProjection = PVRTMat4::PerspectiveFovRH(fFOV, (float)1024.0/768.0, CAM_NEAR, CAM_FAR, PVRTMat4::OGL, false);
}

这是修改后的触摸移动方法:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];    
    CGPoint lastLoc = [touch previousLocationInView:self.view];
    CGPoint diff = CGPointMake(lastLoc.x - location.x, lastLoc.y - location.y);
    float rotX = -1 * GLKMathDegreesToRadians(diff.x / 2.5);
    float rotY = GLKMathDegreesToRadians(diff.y / 2.5);
    PVRTMat4 rotMatrixX, rotMatrixY;
    // create rotation matrices with angle
    PVRTMatrixRotationYF(rotMatrixX, -rotX);
    PVRTMatrixRotationXF(rotMatrixY, rotY);
    _rotationX = _rotationX * rotMatrixX;
    _rotationY = _rotationY * rotMatrixY;
}

相关内容

  • 没有找到相关文章

最新更新