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

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

问题描述

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

它没有像那样工作。我有一些Python代码。这是当我进入这部分游戏时初始化一些数据和一些模式代码的代码:

  def init(self ):
self.game_over = False
self.z = -30
self.x = 0
def transfer(self):
#让OpenGL使用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)
如果game.key(KEY_UP):
self.z + = 2.0 / game.get_fps()
如果game.key(KEY_DOWN):
self.z - = 2.0 / game.get_fps()
如果game.key(KEY_LEFT):
self.x + = 2.0 / game.get_fps()
如果game.key( KEY_RIGHT):
self.x - = 2.0 / game.get_fps()

上升位置应该始终与直角位置相同。我认为它会做的是向上和向后移动Z轴,使用向上和向下键以及左右键通过X轴左右移动。实际发生的事情是,当我使用左右键时,立方体将围绕由键加速的眼睛旋转。向上键导致另一个无处不在的立方体穿过屏幕并击中第一个立方体。向下键将神秘克隆的魔方带回来。这可以结合轮换给出一个完全不同的结果,因为文件说会出现。



到底是什么错?



谢谢。

解决方案

(gluLookAt中up向量的直觉很简单:现在把你的头部倾斜90度,你的位置没有改变,你看的方向没有改变,但是你的视网膜上的图像清晰地显示出来,有什么区别?这就是向上的向量。)



但要回答你的问题:gluLookAt调用不应该连接在一起。换句话说,如果你不确切地知道它是如何工作的,可以使用gluLookAt的唯一模式如下:

  glMatrixMode(GL_MODELVIEW); 
glLoadIdentity();
gluLookAt(...);
#不再触摸模型视图矩阵!

从你的代码看来,你正在做这样的事情:

  glMatrixMode(GL_MODELVIEW); 
glLoadIdentity();
gluLookAt(...);
#有些东西..
gluLookAt(...);

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


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.

解决方案

(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:

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

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