我正在使用python3
我刚刚开始学习openGL,我需要一些帮助,用鼠标转动你的视图相机(就像一个FPS游戏)。现在我的程序看起来像是在移动,但我只是移动对象,所以如果你能告诉我如何移动相机(前进,后退等)也会很好。提前致谢
我的代码:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
vertices = (
(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),
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
colors = (
(1,1,1),
(0,0,0),
(0,1,1),
(0,0,0),
(0,1,1),
(1,0,1),
(0,0,0),
(1,1,1),
(0,0,0),
(0,1,1),
)
def Cube():
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x += 1
glColor3fv(colors[x])
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
def main():
pygame.init()
x = 0
y = 0
z = 0
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL|RESIZABLE)
pygame.event.set_grab(True)
pygame.mouse.set_visible( False )
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
glRotatef(0, 0, 0, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.key == pygame.K_a:
x = 0.1
elif event.key == pygame.K_d:
x = -0.1
elif event.key == pygame.K_w:
z = 0.1
elif event.key == pygame.K_s:
z = -0.1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a and x > 0:
x = 0
elif event.key == pygame.K_d and x < 0:
x = 0
if event.key == pygame.K_w and z > 0:
z = 0
if event.key == pygame.K_s and z < 0:
z = 0
glTranslatef(x,y,z)
#glRotatef(1, 2, 3, 4)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
答案 0 :(得分:1)
对于第一个人物移动,必须逐步更改相机矩阵。这意味着您必须计算当前运动和当前旋转矩阵。将移动和旋转应用到相机并使相机保持下一个循环周期。在循环的下一个循环中,您必须使用上一个循环中的受控相机,并且必须应用新的移动和旋转。这会导致相机增量变化,始终基于其当前位置和方向。
在渲染中,场景的每个网格通常由模型矩阵,视图矩阵和投影矩阵变换。投影矩阵描述了从场景的3D点到视口的2D点的映射。视图矩阵描述了查看场景的方向和位置。模型矩阵定义场景中网格的位置,方向和相对大小
(见Transform the modelMatrix)
在OpenGL中,每种矩阵模式都有一个矩阵堆栈(参见glMatrixMode
)。矩阵模式为GL_MODELVIEW
,GL_PROJECTION
和GL_TEXTURE
。
这意味着您在设置投影时应使用GL_PROJECTION
矩阵模式(gluPerspective
):
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
对于第一人称视图矩阵,您需要一个存储它的变量:
import numpy
def IdentityMat44(): return numpy.matrix(numpy.identity(4), copy=False, dtype='float32')
view_mat = IdentityMat44()
view_mat
应该使用初始位置进行初始化,您可以通过glGetFloatv(GL_MODELVIEW_MATRIX, view_mat)
获取当前模型矩阵:
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -5)
glGetFloatv(GL_MODELVIEW_MATRIX, view_mat)
glLoadIdentity()
对于向左和向右旋转,您必须围绕y轴执行glRotatef
的旋转:
glRotatef(ry, 0, 1, 0)
要逐步更改视图矩阵,您必须执行以下操作:
glTranslatef
和glRotatef
glMultMatrixf
glGetFloatv(GL_MODELVIEW_MATRIX, view_mat)
glLoadIdentity()
glTranslatef(tx,ty,tz)
glRotatef(ry, 0, 1, 0)
glMultMatrixf(view_mat)
glGetFloatv(GL_MODELVIEW_MATRIX, view_mat)
在你的循环结束时,你应该按glPushMatrix
and glPopMatrix
推送并弹出矩阵堆栈。
最终的功能看起来应该是这样的:
tx = 0
ty = 0
tz = 0
ry = 0
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
view_mat = IdentityMat44()
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -5)
glGetFloatv(GL_MODELVIEW_MATRIX, view_mat)
glLoadIdentity()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.key == pygame.K_a: tx = 0.1
elif event.key == pygame.K_d: tx = -0.1
elif event.key == pygame.K_w: tz = 0.1
elif event.key == pygame.K_s: tz = -0.1
elif event.key == pygame.K_RIGHT: ry = 1.0
elif event.key == pygame.K_LEFT: ry = -1.0
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a and tx > 0: tx = 0
elif event.key == pygame.K_d and tx < 0: tx = 0
elif event.key == pygame.K_w and tz > 0: tz = 0
elif event.key == pygame.K_s and tz < 0: tz = 0
elif event.key == pygame.K_RIGHT and ry > 0: ry = 0.0
elif event.key == pygame.K_LEFT and ry < 0: ry = 0.0
glPushMatrix()
glLoadIdentity()
glTranslatef(tx,ty,tz)
glRotatef(ry, 0, 1, 0)
glMultMatrixf(view_mat)
glGetFloatv(GL_MODELVIEW_MATRIX, view_mat)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)