本文介绍了OpenGL 的 LookAt 函数中的 UP 向量究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与LookAt 目标位置与 z = 0 或 z = 1000 或 -1000 无关?

我试过了

    gluLookAt(512, 384, 2000,
              512, 384, 0,
              0.0f, 1.0f, 0.0f);

一切正常,现在我将第三行(向上向量),最后一个数字更改为 0.8:

and things work fine, and now I change the 3rd row (the UP vector), last number to 0.8:

    gluLookAt(512, 384, 2000,
              512, 384, 0,
              0.0f, 1.0f, 0.8f);

它完全一样...接下来我尝试修改了第三行,第一个数字为0.8:

and it is exactly the same... next I tried and modified the 3rd line, the first number to 0.8:

    gluLookAt(512, 384, 2000,
              512, 384, 0,
              0.8f, 1.0f, 0.8f);

现在视图就像向左旋转了 45 度.这个 UP 向量是如何工作的?

Now the view is like it rotated 45 degree to the left. How does this UP vector work?

推荐答案

向上向量用于在提供给 gluLookAt 的眼睛和中心向量之间创建叉积.

The up vector is used to create a cross product between the eye and centre vector supplied to gluLookAt.

从 iOS 上的 GLKit 标头,您可以看到实现:

From the GLKit headers on iOS, you can see the implementation as:

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;
}

此问题中已接受的答案如何正确使用 gluLookAt?很好地描述了向上向量实际影响的内容.

The accepted answer in this question How do I use gluLookAt properly? provides a good description of what the up vector actually impacts.

这篇关于OpenGL 的 LookAt 函数中的 UP 向量究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:06