【发布时间】:2022-01-20 16:53:50
【问题描述】:
我找到了一个程序,你可以在 pyopengl 中用鼠标在相机周围移动,通过做一些我没有完全理解的事情
from pygame.locals import *
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def cube():
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)
)
edegs = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
glBegin(GL_LINES)
glColor3fv((1,1,1))
for edeg in edegs:
for vertex in edeg:
glVertex3fv(vertices[vertex])
glEnd()
pygame.init()
screen = (800,600)
pygame.display.set_mode(screen, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (screen[0]/screen[1]), 0.1, 50.0)
glTranslate(0.0,0.0,-5)
glMatrixMode(GL_MODELVIEW)
modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
busy = True
button_down = False
zoom = -0.5
while True:
glPushMatrix()
glLoadIdentity()
for event in pygame.event.get():
if(event.type == pygame.QUIT):
pygame.quit()
busy = False
elif event.type == pygame.MOUSEMOTION:
if(button_down):
glRotatef(event.rel[1], 1, 0, 0)
glRotatef(event.rel[0], 0, 1, 0)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
zoom += 0.2
if event.button == 5:
zoom -= 0.2
if(not busy): break
for event in pygame.mouse.get_pressed():
if pygame.mouse.get_pressed()[0] == 1:
button_down = True
elif pygame.mouse.get_pressed()[0] == 0:
button_down = False
#mouse scroll
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glMultMatrixf(modelMatrix)
modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
glTranslatef(0.0,0.0,zoom)
glMultMatrixf(modelMatrix)
cube()
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
但是当你在它周围拖动对象时,它开始失去从上到下的方向,这与 Blender 和 SketchUp 等程序不同,有没有办法解决这个问题或用其他方法来操纵相机?
【问题讨论】:
标签: python opengl pygame pyopengl opengl-compat