Python 如何正确使用 gluLookAt?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3380100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use gluLookAt properly?
提问by Matthew Mitchell
I don't want to get into complex trigonometry to calculate rotations and things like that for my 3D world so gluLookAt seems like a nice alternative. According to the documentation all I need to do is place 3 coordinates for the cameras position, three for what I should be looking at and an "up" position. The last made no sense until I assumed it had to be at right angles with the line of sight in the direction the top of the screen should be.
我不想进入复杂的三角学来计算旋转和类似我的 3D 世界的东西,所以 gluLookAt 似乎是一个不错的选择。根据文档,我需要做的就是为相机位置放置 3 个坐标,三个用于我应该查看的位置和一个“向上”位置。最后一个没有意义,直到我认为它必须与屏幕顶部方向的视线成直角。
It doesn't work like that at all. I have some python code. This is the code which initialises some data and some mode code for when I enter this part of the game:
它根本不是那样工作的。我有一些python代码。这是当我进入游戏的这一部分时初始化一些数据和一些模式代码的代码:
def init(self):
self.game_over = False
self.z = -30
self.x = 0
def transfer(self):
#Make OpenGL use 3D
game.engage_3d(45,0.1,100)
gluLookAt(0,0,-30,0,0,0,0,1,0)
"game.engage_3d(45,0.1,100)" basically sets up the projection matrix to have a 45 degree angle of view and near and far coordinates of 0.1 and 100.
“game.engage_3d(45,0.1,100)”基本上将投影矩阵设置为具有 45 度视角和 0.1 和 100 的近远坐标。
The first gluLookAt puts the camera in the correct position, nicely.
第一个 gluLookAt 将相机放在正确的位置,很好。
I have a cube drawn with the centre of (0,0,0) and it works fine without gluLookAt. Before I draw it I have this code:
我有一个以 (0,0,0) 为中心绘制的立方体,它在没有 gluLookAt 的情况下工作正常。在我绘制它之前,我有这个代码:
gluLookAt(self.x,0,self.z,0,0,0,0,1,0)
if game.key(KEY_UP):
self.z += 2.0/game.get_fps()
if game.key(KEY_DOWN):
self.z -= 2.0/game.get_fps()
if game.key(KEY_LEFT):
self.x += 2.0/game.get_fps()
if game.key(KEY_RIGHT):
self.x -= 2.0/game.get_fps()
Now from that, the up position should always be the same as it's always at right angles. What I'd have thought it would do is move forward and back the z-axis with the up and down keys and left and right through the x-axis with the left and right keys. What actually happens, is when I use the left and right keys, the cube will rotate around the "eye" being accelerated by the keys. The up key causes another cube from nowhere to slice through the screen and hit the first cube. THe down key brings the mysterious cloned cube back. This can be combined with the rotation to give a completely different outcome as the documentation said would arise.
从那以后,向上的位置应该始终与它始终处于直角相同。我以为它会做的是使用上下键前后移动 z 轴,使用左右键左右移动 x 轴。实际发生的是,当我使用左右键时,立方体将围绕被按键加速的“眼睛”旋转。向上键使另一个立方体不知从哪里切开屏幕并击中第一个立方体。向下键将神秘的克隆立方体带回来。这可以与旋转相结合,给出完全不同的结果,正如文档所说的那样。
What on earth is wrong?
这到底是怎么回事?
Thank you.
谢谢你。
采纳答案by Carlos Scheidegger
(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.)
(gluLookAt 中“向上”向量背后的直觉很简单:看任何东西。现在将你的头倾斜 90 度。你所在的位置没有改变,你看的方向没有改变,但是你的图像视网膜显然有。有什么区别?你的头顶指向哪里。那是向上的向量。)
But to answer your question: gluLookAt calls should not be concatenated. In other words, the only pattern in which it's OK to use gluLookAt if you don't know exactly how it works is the following:
但是要回答您的问题:不应连接 gluLookAt 调用。换句话说,如果您不确切知道它是如何工作的,那么可以使用 gluLookAt 的唯一模式如下:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);
# do not touch the modelview matrix anymore!
It seems from your code that you're doing something like this:
从你的代码看来,你正在做这样的事情:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);
# some stuff..
gluLookAt(...);
This will generate weird results, because gluLookAt multiplies the current matrix by the viewing matrix it computes. If you want to concatenate transformations you're really better off figuring out how to make glTranslate, glScale and glRotatef work for you. Even better, you should learn how the coordinate transformations work and stick to glMultMatrix.
这将产生奇怪的结果,因为 gluLookAt 将当前矩阵乘以它计算的查看矩阵。如果您想连接转换,您最好弄清楚如何使 glTranslate、glScale 和 glRotatef 为您工作。更好的是,您应该了解坐标变换的工作原理并坚持使用 glMultMatrix。

