我有一个在 OpenGL 中创建的多维数据集字段,并且我按预期工作,并且旋转“相机”的一部分正在工作,直到我尝试向上或向下看。
我有一个有效的代码片段:
if pressed[pygame.K_UP] or pressed[pygame.K_DOWN]:
rotx = cos(rot/radian)
rotz = sin(rot/radian)
if pressed[pygame.K_UP]:
glRotatef(speed / 2, -rotx, 0, rotz)
if pressed[pygame.K_DOWN]:
glRotatef(speed / 2, rotx, 0, -rotz)
但它只在 rot 为 0 时才有效。所以当我第一次运行程序时,如果我只是左右移动,我可以上下查看,而不是左右看,或者前后移动。
verticies = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
def Cube(tX, tY, tZ):
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3f(verticies[vertex][0] + tX, verticies[vertex][1] + tY, verticies[vertex][2] + tZ)
glEnd()
def main():
pygame.init()
screenSize = (1500, 800)
pygame.display.set_mode(screenSize, DOUBLEBUF|OPENGL)
gluPerspective(45, (screenSize[0]/screenSize[1]), 0.1, 50.0)
rot = 0
speed = 3
radian = 57.2958
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pressed = pygame.key.get_pressed()
#==# Rotation with arrow keys #==#
if pressed[pygame.K_LEFT]:
glRotatef(speed / 2, 0, -1, 0)
rot += 1
if pressed[pygame.K_RIGHT]:
glRotatef(speed / 2, 0, 1, 0)
rot -= 1
if pressed[pygame.K_UP] or pressed[pygame.K_DOWN]:
rotx = cos(rot/radian)
rotz = sin(rot/radian)
if pressed[pygame.K_UP]:
glRotatef(speed / 2, -rotx, 0, rotz)
if pressed[pygame.K_DOWN]:
glRotatef(speed / 2, rotx, 0, -rotz)
#==# Walking with WASD #==#
if pressed[pygame.K_w]:
glTranslate(sin(rot/radian) / speed, 0, cos(rot/radian) / speed)
if pressed[pygame.K_s]:
glTranslate(-sin(rot/radian) / speed, 0, -cos(rot/radian) / speed)
if pressed[pygame.K_a]:
glTranslate(sin((rot + 90)/radian) / speed, 0, cos((rot + 90)/radian) / speed)
if pressed[pygame.K_d]:
glTranslate(-sin((rot + 90)/radian) / speed, 0, -cos((rot + 90)/radian) / speed)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
for i in range(8):
for j in range(8):
Cube(-i*2.5, -4, -j*2.5)
pygame.display.flip()
pygame.time.wait(10)
main()
我认为这可以作为 FPS 游戏中的运动和相机,但事实并非如此。