【发布时间】:2012-12-14 01:49:54
【问题描述】:
我正在寻找一个简单的实现,用于在带有四元数的 3D 模型上实现轨迹球旋转,特别是在 iOS 上使用 GLKit。到目前为止,我已经检查了以下来源:
我也一直在尝试理解 here 和 here 的源代码和数学。我可以旋转我的对象,但它总是以特定角度跳跃,所以我担心万向节锁定在起作用。我正在使用手势识别器来控制旋转(平移手势影响滚动和偏航,旋转手势影响俯仰)。我附上了用于四元数处理以及模型视图矩阵转换的代码。
变量:
GLKQuaternion rotationE;
四元数处理:
- (void)rotateWithXY:(float)x and:(float)y
{
const float rate = M_PI/360.0f;
GLKVector3 up = GLKVector3Make(0.0f, 1.0f, 0.0f);
GLKVector3 right = GLKVector3Make(1.0f, 0.0f, 0.0f);
up = GLKQuaternionRotateVector3(GLKQuaternionInvert(self.rotationE), up);
self.rotationE = GLKQuaternionMultiply(self.rotationE, GLKQuaternionMakeWithAngleAndVector3Axis(x*rate, up));
right = GLKQuaternionRotateVector3(GLKQuaternionInvert(self.rotationE), right);
self.rotationE = GLKQuaternionMultiply(self.rotationE, GLKQuaternionMakeWithAngleAndVector3Axis(y*rate, right));
}
- (void)rotateWithZ:(float)z
{
GLKVector3 front = GLKVector3Make(0.0f, 0.0f, -1.0f);
front = GLKQuaternionRotateVector3(GLKQuaternionInvert(self.rotationE), front);
self.rotationE = GLKQuaternionMultiply(self.rotationE, GLKQuaternionMakeWithAngleAndVector3Axis(z, front));
}
模型视图矩阵变换(内部绘制循环):
// Get Quaternion Rotation
GLKVector3 rAxis = GLKQuaternionAxis(self.transformations.rotationE);
float rAngle = GLKQuaternionAngle(self.transformations.rotationE);
// Set Modelview Matrix
GLKMatrix4 modelviewMatrix = GLKMatrix4Identity;
modelviewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -0.55f);
modelviewMatrix = GLKMatrix4Rotate(modelviewMatrix, rAngle, rAxis.x, rAxis.y, rAxis.z);
modelviewMatrix = GLKMatrix4Scale(modelviewMatrix, 0.5f, 0.5f, 0.5f);
glUniformMatrix4fv(self.sunShader.uModelviewMatrix, 1, 0, modelviewMatrix.m);
非常感谢任何帮助,但我确实希望使其尽可能简单并坚持使用 GLKit。
【问题讨论】:
标签: ios opengl-es quaternions glkit arcball