iOS:关于GLKMatrix4MakeLookAt结果中的相机信息的问题

时间:2012-01-29 13:05:05

标签: ios view camera transformation glkit

iOS 5文档显示GLKMatrix4MakeLookAt的操作与gluLookAt相同。

此处提供了定义:

static __inline__ GLKMatrix4 GLKMatrix4MakeLookAt(float eyeX, float eyeY, float eyeZ,
                                                  float centerX, float centerY, float centerZ,
                                                  float upX, float upY, float upZ)
{
    GLKVector3 ev = { eyeX, eyeY, eyeZ };
    GLKVector3 cv = { centerX, centerY, centerZ };
    GLKVector3 uv = { upX, upY, upZ };
    GLKVector3 n = GLKVector3Normalize(GLKVector3Add(ev, GLKVector3Negate(cv)));
    GLKVector3 u = GLKVector3Normalize(GLKVector3CrossProduct(uv, n));
    GLKVector3 v = GLKVector3CrossProduct(n, u);

    GLKMatrix4 m = { u.v[0], v.v[0], n.v[0], 0.0f,
                     u.v[1], v.v[1], n.v[1], 0.0f,
                     u.v[2], v.v[2], n.v[2], 0.0f,
                     GLKVector3DotProduct(GLKVector3Negate(u), ev),
                     GLKVector3DotProduct(GLKVector3Negate(v), ev),
                     GLKVector3DotProduct(GLKVector3Negate(n), ev),
                     1.0f };

    return m;
}

我正试图从中提取相机信息:

1. Read the camera position
    GLKVector3 cPos = GLKVector3Make(mx.m30, mx.m31, mx.m32);
2. Read the camera right vector as `u` in the above
    GLKVector3 cRight = GLKVector3Make(mx.m00, mx.m10, mx.m20);
3. Read the camera up vector as `u` in the above
    GLKVector3 cUp = GLKVector3Make(mx.m01, mx.m11, mx.m21);
4. Read the camera look-at vector as `n` in the above
    GLKVector3 cLookAt = GLKVector3Make(mx.m02, mx.m12, mx.m22);

有两个问题:


  1. 因为他们定义了{@ 1}}而不是(eye - center),因此看起来向量似乎被否定了。实际上,当我使用(center - eye)的相机位置和GLKMatrix4MakeLookAt的中心调用(0,0,-10)时,我提取的外观是(0,0,1),即我预期的负面影响。那么我应该否定我提取的内容吗?

  2. 我提取的摄像机位置是视图变换矩阵预乘视图旋转矩阵的结果,因此是它们定义中的点积。我认为这是不正确的 - 任何人都可以建议我应该如何计算这个位置?


  3. 非常感谢你的时间。

1 个答案:

答案 0 :(得分:2)

its documentation,gluLookAt计算中心 - 眼睛,将其用于某些中间步骤,然后将其置于结果矩阵中。因此,如果你想要中心 - 眼睛,那么服用否定就明确是正确的。

你还会注意到返回的结果相当于一个multMatrix,其结果的旋转部分后跟一个glTranslate -eye。由于经典OpenGL矩阵运算经过乘法,这意味着gluLookAt被定义为将旋转乘以平移。所以Apple的实现是正确的,就像第一次移动相机,然后旋转它一样 - 这是正确的。

因此,如果定义R =(定义指令旋转部分的矩阵),T =(平移模拟),则得到R.T.如果你想提取T,你可以用R的倒数预乘,然后将结果拉出最后一列,因为矩阵乘法是关联的。

作为奖励,因为R是正交的,所以逆只是转置。