OpenGL的LookAt函数中的UP向量到底是什么? [英] What exactly is the UP vector in OpenGL's LookAt function?

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

问题描述

这与我尝试过

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

一切正常,现在我将第三行(UP向量)的最后一个数字更改为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.

(gluLookAt中向上"向量背后的直觉很简单:看任何东西.现在将头倾斜90度.您的位置没有改变,您所看的方向没有改变,但是视网膜中的图像显然具有图像.有什么区别?您的头顶指向的位置.这就是向上的向量.)
(The intuition behind the "up" vector in gluLookAt is simple: Look at anything. Now tilt your head 90 degrees. Where you are hasn't changed, the direction you're looking at hasn't changed, but the image in your retina clearly has. What's the difference? Where the top of your head is pointing to. That's the up vector.)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆