PyOpenGL glLookAt 在第一次调用后表现怪异,参数相同
PyOpenGL glLookAt behaves weirdly after first call, with same parameters
我目前正在使用 PyOpenGL 为学校编写一个小游戏(Python 不是我的选择...)。
我似乎无法理解 "camera" 的功能。我认为对我的案例最直观的方法是 gluLookAt(...)
函数。我想用第三人称看我的角色。由于我的游戏是基于图块的并且在逻辑上是二维的,因此 Y 坐标永远不会改变。以下是我认为重要的代码(基本上是我用 OpenGL 所做的一切)。
这就是调用一次,最初是为了设置 OpenGL:
# set the perspective
fieldOfView = 45 # degrees
aspectRatio = 1800 / 1000 # given as parameters to constructor previously
gluPerspective(fieldOfView, aspectRatio, 0.1, 50.0)
# things in the back are covered by things in front of them
glEnable(GL_DEPTH_TEST)
这是每次更新时调用的内容:
# Clear Buffers
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
# render the objects in the map and ground where needed
self.renderAllObjects() # in here glBegin(..), glEnd(),
# glColor3fv(...) and glVertex3fv(...)
# are the only used OpenGL functions
# eye coords
eX = 19.5
eY = 3
eZ = 1.5
# center/lookat coords
cX = 12.5
cY = 1
cZ = 1.5
gluLookAt(eX, eY, eZ, cX, cY, cZ, 0, 1, 0)
当第一个更新是 运行 时,一切正常,但是一旦我再次调用更新函数,我们就会被放置在其他坐标上,这似乎是相对于前一个坐标的某种方式相机。
网上查到的功能:
glMatrixMode(...)
与 GL_MODELVIEW 和 GL_PERSPECTIVE
glLoadIdentity()
据说 glLoadIdentity()
会在每次 gluLookAt(...)
调用之前被调用。但是一旦我这样做了,我只会得到一个黑屏。
也在玩glMatrixMode(...)
的地方并没有解决任何问题。
我也试过改变加载对象和相机的顺序,但我现在的顺序似乎是正确的。
如有任何帮助,我们将不胜感激。我是一个完全的 OpenGL 新手。遗憾的是,经过数小时的研究,我无法解决这个问题。
在你gluPerspective
之前你应该:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(fieldOfView, aspectRatio, 0.1, 50.0)
这将设置投影矩阵。
然后每次渲染帧时都会设置 model-view 矩阵:
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(eX, eY, eZ, cX, cY, cZ, 0, 1, 0)
投影矩阵和模型视图矩阵是由 GL 存储的独立状态。因此,为了 glLoadInentity
和 gluLookAt
不覆盖投影矩阵,您必须使用 glMatrixMode
来指定您当前正在使用的矩阵。
您还应该在渲染对象 (self.renderAllObjects
) 之前设置模型视图矩阵 (gluLookAt
)。
我目前正在使用 PyOpenGL 为学校编写一个小游戏(Python 不是我的选择...)。
我似乎无法理解 "camera" 的功能。我认为对我的案例最直观的方法是 gluLookAt(...)
函数。我想用第三人称看我的角色。由于我的游戏是基于图块的并且在逻辑上是二维的,因此 Y 坐标永远不会改变。以下是我认为重要的代码(基本上是我用 OpenGL 所做的一切)。
这就是调用一次,最初是为了设置 OpenGL:
# set the perspective
fieldOfView = 45 # degrees
aspectRatio = 1800 / 1000 # given as parameters to constructor previously
gluPerspective(fieldOfView, aspectRatio, 0.1, 50.0)
# things in the back are covered by things in front of them
glEnable(GL_DEPTH_TEST)
这是每次更新时调用的内容:
# Clear Buffers
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
# render the objects in the map and ground where needed
self.renderAllObjects() # in here glBegin(..), glEnd(),
# glColor3fv(...) and glVertex3fv(...)
# are the only used OpenGL functions
# eye coords
eX = 19.5
eY = 3
eZ = 1.5
# center/lookat coords
cX = 12.5
cY = 1
cZ = 1.5
gluLookAt(eX, eY, eZ, cX, cY, cZ, 0, 1, 0)
当第一个更新是 运行 时,一切正常,但是一旦我再次调用更新函数,我们就会被放置在其他坐标上,这似乎是相对于前一个坐标的某种方式相机。
网上查到的功能:
glMatrixMode(...)
与 GL_MODELVIEW 和 GL_PERSPECTIVEglLoadIdentity()
据说 glLoadIdentity()
会在每次 gluLookAt(...)
调用之前被调用。但是一旦我这样做了,我只会得到一个黑屏。
也在玩glMatrixMode(...)
的地方并没有解决任何问题。
我也试过改变加载对象和相机的顺序,但我现在的顺序似乎是正确的。
如有任何帮助,我们将不胜感激。我是一个完全的 OpenGL 新手。遗憾的是,经过数小时的研究,我无法解决这个问题。
在你gluPerspective
之前你应该:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(fieldOfView, aspectRatio, 0.1, 50.0)
这将设置投影矩阵。
然后每次渲染帧时都会设置 model-view 矩阵:
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(eX, eY, eZ, cX, cY, cZ, 0, 1, 0)
投影矩阵和模型视图矩阵是由 GL 存储的独立状态。因此,为了 glLoadInentity
和 gluLookAt
不覆盖投影矩阵,您必须使用 glMatrixMode
来指定您当前正在使用的矩阵。
您还应该在渲染对象 (self.renderAllObjects
) 之前设置模型视图矩阵 (gluLookAt
)。