python - OpenGL:相机 theta 和缩放

标签 python math opengl pygame

在研究另一个 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/

相关文章:

python - 如何在Python中将十六进制字符串与空格进行异或

python - 类似于 python 的 sorted(),按键对 pandas 数据框中的值进行排序

c++ - 如何使用 Glut 让相机飞过 OpenGL 中的对象?

opengl - 如何绘制立方体而不是四边形?

c++ - 如何构建 OpenGL + GLEW + GLFW 程序,以便在带有 exe 的文件夹中不需要 glfw3.dll?

python - 无需 try/catch 即可安全访问 Python 中的对象

python - 如何从数据框中获取当前值来构建列表?

math - 按位 XOR(异或)是什么意思?

python - 在 Python 中实现 Miller-Rabin 复合性时遇到问题

algorithm - 统一选择元素分布到箱子中