我使用以下代码来执行偏航/俯仰旋转:
- (void)rotateCameraUsingYaw:(double)yaw andPitch:(double)pitch
{
GLKVector3 centerToEyeVec = [self centerToEyeVec];
GLKVector3 rightVector = GLKVector3Normalize(GLKVector3Normalize(GLKVector3CrossProduct(centerToEyeVec, self.up)));
GLKMatrix4 yawRotationMatrix = GLKMatrix4MakeRotation(yaw, self.up.x, self.up.y, self.up.z);
GLKMatrix4 pitchRotationMatrix = GLKMatrix4MakeRotation(pitch, rightVector.x, rightVector.y, rightVector.z);
GLKMatrix4 yawPitchRotationMat = GLKMatrix4Multiply(yawRotationMatrix, pitchRotationMatrix);
GLKVector3 newCenterToEyeVec = GLKMatrix4MultiplyVector3(yawPitchRotationMat, centerToEyeVec);
self.eye = GLKVector3Add(self.center, newCenterToEyeVec);
[self recalculateUpVector];
}
当音高值低于PI / 2时,此代码效果很好。当音高达到PI / 2时,模型旋转(向上旋转向量)。我不知道为什么会这样。