尝试在我的 python 和 pygame 3D OpenGL 游戏中实现 3D 漫游相机是我一直在尝试做的事情,但我遇到了障碍。每次我尝试使用 gluLookAt 函数时,相机都会吓坏并四处晃动,而 x 和 z 坐标迅速增加并从负向正切换。我正在尝试制作的游戏只是一个可以四处走动的平坦表面。我已经实现了所有用于移动的控件,但没有实现相机。
以下是一些总结代码,供不想阅读我所有代码的人使用:
while True:
matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
camera_x = matrix[3][0]
camera_y = matrix[3][1]
camera_z = matrix[3][2]
# the 3-6 ones are what the camera is looking at
gluLookAt(camera_x, camera_y, camera_z, 0, 0, 0, 0, 1, 0)
print("({}, {}, {})".format(int(-camera_x), int(-camera_y), int(-camera_z)))
如果上面的代码无法解决问题,这是我的完整(ish)代码:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import os
speed = .5
def enviroment():
glBegin(GL_QUADS)
for surface in surfaces:
for vertex in surface:
glColor3fv((1, 0, 0))
glVertex3fv(verticies[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glColor3fv((0, 1, 1))
glVertex3fv(verticies[vertex])
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0] / display[1]), 0.1, 400)
glTranslatef(0.0, -3, -150)
glRotatef(0, 0, 0, 0)
while True:
press = pygame.key.get_pressed()
matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
camera_x = matrix[3][0]
camera_y = matrix[3][1]
camera_z = matrix[3][2]
# the 3-6 ones are what the camera is looking at
gluLookAt(camera_x, camera_y, camera_z, 0, 0, 0, 0, 1, 0)
print("({}, {}, {})".format(int(-camera_x), int(-camera_y), int(-camera_z)))
if press[pygame.K_UP]:
glTranslatef(0, -speed, 0)
elif press[pygame.K_DOWN]:
glTranslatef(0, speed, 0)
elif press[pygame.K_w]:
glTranslatef(0, 0, speed)
elif press[pygame.K_s]:
glTranslatef(0, 0, -speed)
elif press[pygame.K_a]:
glTranslatef(speed, 0, 0)
elif press[pygame.K_d]:
glTranslatef(-speed, 0, 0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == KEYDOWN:
if event.key == K_UP:
glTranslatef(0, -speed, 0)
elif event.key == K_DOWN:
glTranslatef(0, speed, 0)
elif event.key == K_w:
glTranslatef(0, 0, speed)
elif event.key == K_s:
glTranslatef(0, 0, -speed)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
enviroment()
pygame.display.flip()
pygame.time.wait(10)
main()
任何帮助,将不胜感激!