如何正确使用 gluLookAt? [英] How do I use gluLookAt properly?

查看:35
本文介绍了如何正确使用 gluLookAt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想用复杂的三角学来计算我的 3D 世界的旋转和类似的东西,所以 gluLookAt 似乎是一个不错的选择.根据文档,我需要做的就是为相机位置放置 3 个坐标,三个用于我应该查看的位置和一个向上"位置.最后一个没有意义,直到我认为它必须与屏幕顶部方向的视线成直角.

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.

它根本不是那样工作的.我有一些python代码.这是当我进入游戏的这一部分时初始化一些数据和一些模式代码的代码:

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)" 基本上将投影矩阵设置为具有 45 度视角和 0.1 和 100 的近远坐标.

"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.

第一个 gluLookAt 将相机放在正确的位置,很好.

The first gluLookAt puts the camera in the correct position, nicely.

我绘制了一个以 (0,0,0) 为中心的立方体,它在没有 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:

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 轴.实际发生的是,当我使用左右键时,立方体将围绕被按键加速的眼睛"旋转.向上键使另一个立方体不知从哪里切开屏幕并击中第一个立方体.向下键将神秘的克隆立方体带回来.这可以与旋转相结合,给出完全不同的结果,正如文档所说的那样.

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.

到底哪里出了问题?

谢谢.

推荐答案

(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.)

但是要回答您的问题:不应连接 gluLookAt 调用.换句话说,如果您不确切知道它是如何工作的,那么可以使用 gluLookAt 的唯一模式如下:

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:

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(...);

这会产生奇怪的结果,因为 gluLookAt 将当前矩阵乘以它计算的查看矩阵.如果您想连接转换,您最好弄清楚如何使 glTranslate、glScale 和 glRotatef 为您工作.更好的是,您应该了解坐标变换的工作原理并坚持使用 glMultMatrix.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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