在研究另一个 3D GL 游戏的代码时,我决定尝试创建一个非常简单的、四处走动的东西。
一切似乎都工作正常,但仅限于一个轴。向前和向后一个方向行走时,一切都很好。不过,如果你转动 90 度,并开始向前/向后行走,则向后将起到向前的作用,向前将起到向后的作用。
这是地面绘图代码:
def drawground(self):
glPushMatrix()
glTranslate(-1, -1, -3)
glRotate(90, True, False, False)
glBegin(GL_QUADS)
glColor(0.6, 0.6, 0.6)
glVertex(0, 0)
glVertex(0, self.winmain.world.xlen)
glVertex(self.winmain.world.ylen, self.winmain.world.xlen)
glVertex(self.winmain.world.ylen, 0)
glEnd()
glPopMatrix()
移动代码:
if pygame.key.get_pressed()[K_w]:
self.campos[0] -= 0.005*math.sin(self.camtheta * 3.14159 / 180)
self.campos[1] -= 0.005*math.cos(self.camtheta * 3.14159 / 180)
if pygame.key.get_pressed()[K_s]:
self.campos[0] += 0.005*math.sin(self.camtheta * 3.14159 / 180)
self.campos[1] += 0.005*math.cos(self.camtheta * 3.14159 / 180)
并且,相机θ旋转:
if event.type == MOUSEMOTION:
self.camtheta -= 2 * event.rel[0] #Subtracting 2 * the relative moved pixels from the last position.
什么可能导致这个问题?
最佳答案
您没有提供太多详细信息,但无论如何,我建议您研究一个名为 gluLookAt(..)
的令人惊叹的 glu API。它需要 9 个参数,其中 3 个为位置,3 个为方向,3 个为方位。
导入 GLU:
from OpenGL.GLU import *
并说:
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)
对于漫游之类的事情,upX、upY、upZ 将为 0,1,0。
此处的文档:
http://pyopengl.sourceforge.net/documentation/manual-3.0/gluLookAt.html
关于python - OpenGL:相机 theta 和缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11463945/