如何正确使用 gluLookAt?
我不想进入复杂的三角学来计算 3D 世界的旋转和类似的东西,所以 gluLookAt 似乎是一个不错的选择。根据文档,我需要做的就是为相机位置放置 3 个坐标,为我应该查看的位置放置 3 个坐标,以及一个“向上”位置。最后一个没有任何意义,直到我认为它必须与屏幕顶部方向的视线成直角。
它根本不是那样的。我有一些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)”基本上将投影矩阵设置为具有 45 度视角和近远坐标为 0.1 和 100。
第一个 gluLookAt 很好地将相机置于正确的位置。
我有一个以 (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()
现在,向上的位置应该始终与它始终处于直角相同。我本以为它会做的是使用向上和向下键在 z 轴上前后移动,并使用向左和向右键在 x 轴上左右移动。实际发生的情况是,当我使用左右键时,立方体将围绕“眼睛”旋转,并通过按键加速。向上键会导致另一个立方体不知从何而来,切过屏幕并击中第一个立方体。向下键可以带回神秘的克隆立方体。这可以与轮换相结合,以产生与文档所述完全不同的结果。
到底出了什么问题?
谢谢。
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.
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:
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.
The first gluLookAt puts the camera in the correct position, nicely.
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:
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.
What on earth is wrong?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(gluLookAt 中“向上”向量背后的直觉很简单:看任何东西。现在将头倾斜 90 度。你所在的位置没有改变,你看的方向也没有改变,但是你的图像中的图像没有改变。视网膜显然有什么区别?那是向上向量。)
但是回答你的问题: gluLookAt 调用不应该被连接。换句话说,如果您不确切知道 gluLookAt 的工作原理,则可以使用 gluLookAt 的唯一模式如下:
从您的代码看来,您正在做这样的事情:
这将生成奇怪的结果,因为 gluLookAt将当前矩阵乘以它计算的观察矩阵。如果你想连接转换,你最好弄清楚如何让 glTranslate、glScale 和 glRotatef 为你工作。更好的是,您应该了解坐标变换的工作原理并坚持使用 glMultMatrix。
(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.)
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:
It seems from your code that you're doing something like this:
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.