gpt4 book ai didi

python - 如何在 PyOpenGL 上创建一个可以对鼠标移动执行 "perspective rotations"的相机?

转载 作者:行者123 更新时间:2023-11-28 16:57:56 29 4
gpt4 key购买 nike

我正在创建一个第一人称视角的角色扮演游戏,我想在移动鼠标时在 PyOpenGL 中旋转相机(就像其他一些游戏,如 Minecraft)。我可以使用什么功能来执行此操作以及如何执行此操作?

我尝试使用 gluLookAt() 但我不明白它是如何工作的,尽管我查阅了不同的来源。我什至不知道它是否有帮助。

import sys,pygame
from OpenGL.GL import *
from OpenGL.GLU import *
cmddown = False
#...
keypress = pygame.key.get_pressed()#Move using WASD
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)
mouse_movement = pygame.mouse.get_rel()#Get mouse event
#This is where the "look around" should be happen
pygame.display.flip()

最佳答案

您可以使用 glRotate绕轴旋转,旋转量由相对鼠标移动 (pygame.mouse.get_rel()) 给出:

mouseMove = pygame.mouse.get_rel()
glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)

但这不会让您满意,因为如果鼠标离开窗口,解决方案将不再有效。
您必须通过 pygame.mouse.set_pos() 将鼠标置于屏幕中央在每一帧。通过pygame.MOUSEMOTION获取鼠标移动事件。例如:

# init mouse movement and center mouse on screen
displayCenter = [scree.get_size()[i] // 2 for i in range(2)]
mouseMove = [0, 0]
pygame.mouse.set_pos(displayCenter)

paused = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
run = False
if event.key == pygame.K_PAUSE or event.key == pygame.K_p:
paused = not paused
pygame.mouse.set_pos(displayCenter)
if event.type == pygame.MOUSEMOTION:
mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
if not paused:
pygame.mouse.set_pos(displayCenter)

请注意,类似 glRotate 的操作和 glTranslate设置一个矩阵并将当前矩阵乘以新矩阵。

currentMatrix = currentMatrix * newMatrix

这对于模型动画和变换来说是完美的,但对于第一人称运动来说这是错误的方式,因为必须改变相机位置和视角。

viewMatrix = viewTransformMatrix * viewMatrix

做那样的操作glGetFloatv(GL_MODELVIEW_MATRIX)glMultMatrixf .
通过 gluLookAt 初始化 View 矩阵并在主循环之前通过 glGetFloatv(GL_MODELVIEW_MATRIX) 将 View 矩阵加载到变量 (viewMatrix)。

在每一帧的主循环中:

  • 加载Identity matrix ( glLoadIdentity )
  • 对 View 进行新的转换(glRotatefglTranslatef)
  • 将当前 View 矩阵乘以viewMatrix (glMultMatrixf)
  • 为下一帧加载新的 View 矩阵到viewMatrix
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()

# [...]

run = True
while run:

# [...]

# init the view matrix
glLoadIdentity()

# apply the movment
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)

# apply the roation
glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)

# multiply the current matrix by the get the new view matrix and store the final vie matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

为了向上和向下看,您必须围绕 x 轴应用最后的旋转。旋转的枢轴取决于视点。必须对角度求和,并且必须在 View 矩阵之后应用旋转,否则移动会根据角度改变(“高度”)级别。

viewMatrix = viewTransformMatrix * viewMatrix
finlalMatrix = lookUpDownMatrix * viewMatrix

为此,您必须结合上下旋转矩阵并将其乘以 viewMatrix

up_down_angle = 0.0
run = True
while run:

# [...]

# init model view matrix
glLoadIdentity()

# apply the look up and down
up_down_angle += mouseMove[1]*0.1
glRotatef(up_down_angle, 1.0, 0.0, 0.0)

# init the view matrix
glPushMatrix()
glLoadIdentity()

# calculate new `viewMatrix`
# [...]

# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)

请参阅下面的示例程序,它演示了该过程。
请注意,该程序将鼠标保持在窗口的中心,因此您不能再“移动”鼠标。因此,可以通过 ESCreturn 停止应用程序。
该应用程序可以通过pausep暂停。当应用程序暂停时,鼠标未居中到窗口。

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

import math

pygame.init()
display = (400, 300)
scree = pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glShadeModel(GL_SMOOTH)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.5, 0.5, 0.5, 1])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1])

sphere = gluNewQuadric()

glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()

# init mouse movement and center mouse on screen
displayCenter = [scree.get_size()[i] // 2 for i in range(2)]
mouseMove = [0, 0]
pygame.mouse.set_pos(displayCenter)

up_down_angle = 0.0
paused = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
run = False
if event.key == pygame.K_PAUSE or event.key == pygame.K_p:
paused = not paused
pygame.mouse.set_pos(displayCenter)
if not paused:
if event.type == pygame.MOUSEMOTION:
mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
pygame.mouse.set_pos(displayCenter)

if not paused:
# get keys
keypress = pygame.key.get_pressed()
#mouseMove = pygame.mouse.get_rel()

# init model view matrix
glLoadIdentity()

# apply the look up and down
up_down_angle += mouseMove[1]*0.1
glRotatef(up_down_angle, 1.0, 0.0, 0.0)

# init the view matrix
glPushMatrix()
glLoadIdentity()

# apply the movment
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)

# apply the left and right rotation
glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)

# multiply the current matrix by the get the new view matrix and store the final vie matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)

glLightfv(GL_LIGHT0, GL_POSITION, [1, -1, 1, 0])

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

glPushMatrix()

glColor4f(0.5, 0.5, 0.5, 1)
glBegin(GL_QUADS)
glVertex3f(-10, -10, -2)
glVertex3f(10, -10, -2)
glVertex3f(10, 10, -2)
glVertex3f(-10, 10, -2)
glEnd()

glTranslatef(-1.5, 0, 0)
glColor4f(0.5, 0.2, 0.2, 1)
gluSphere(sphere, 1.0, 32, 16)

glTranslatef(3, 0, 0)
glColor4f(0.2, 0.2, 0.5, 1)
gluSphere(sphere, 1.0, 32, 16)

glPopMatrix()

pygame.display.flip()
pygame.time.wait(10)

pygame.quit()

关于python - 如何在 PyOpenGL 上创建一个可以对鼠标移动执行 "perspective rotations"的相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56609044/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com