【发布时间】:2019-04-17 19:30:39
【问题描述】:
我正在使用 python 使用 OpenGL 和 pyGame 制作魔方应用程序。我可以看到立方体,我想按下一个按钮并加载颜色,但没有显示颜色。 (我单独为每个小立方体着色,但我认为这并不重要)。
如果我在绘制立方体顶点的函数中调用为立方体着色的函数(在程序开始时),颜色会完美显示,但如果我通过按键调用颜色函数(使用 pyGame)虽然应用程序已经在运行,但它们不会。
给小立方体着色的函数:
def color_cubie(self):
surfaces = (
(0, 1, 2, 3),
(4, 5, 6, 7),
(0, 3, 7, 4),
(1, 2, 6, 5),
(2, 3, 7, 6),
(0, 1, 5, 4)
)
colors = [
(1, 0, 0), (1, 0.5, 0),
(0, 1, 0), (0, 0, 1),
(1, 1, 1), (1, 1, 0)
]
#faces
glBegin(GL_QUADS)
index=0
for surface in surfaces:
glColor3fv(colors[index])
for vertex in surface:
glVertex3fv(self.verticies[vertex])
index+=1
glEnd()
我调用函数的部分(当我在键盘上按 c 键时 if 语句为真)。 cube 也是一个 3x3x3 的 numpy 数组,里面装满了 cubie 对象。
if move == "c":
for row in self.cube[2]:
for cubie in row:
cubie.color_cubie()
主/渲染函数:
def render():
pygame.init()
display = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
pygame.display.set_caption("Rubiks Cube")
glEnable(GL_DEPTH_TEST)
glClearColor(1, 1, 1, 1)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -7)
glLineWidth(10)
while True:
mouse_pos = pygame.mouse.get_rel()
glRotatef(1, mouse_pos[1], mouse_pos[0], 0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
#create cube object
new_cube = Cube()
new_cube.show_cube()
pygame.display.flip()
pygame.time.wait(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == K_c:
new_cube.rotate_cube("c")
当我阅读时,我必须重新绘制屏幕才能看到彩色立方体。但是 glClear(...) 不这样做吗?
【问题讨论】:
-
“但是如果我在应用程序已经运行时通过按键(使用 pyGame)调用颜色函数,它们不会。” - 这是什么意思“......他们没有。”?我假设
new_cube.show_cube()绘制立方体。那么color_cubie的作用是什么?再画同一个立方体?这个问题几乎不清楚。请阅读How to create a Minimal, Complete, and Verifiable example。 -
它有点帮助。我在调用函数为立方体着色的点下添加了 pygame.display.flip() 和 pygame.time.wait(10),颜色闪烁了一点
-
"但是如果我在应用程序已经运行时按下键(使用 pyGame)调用颜色函数,它们不会。" - 我的意思是,如果我在应用程序已经运行时调用该函数,颜色不会显示。 new_cube.show_cube() 绘制立方体的线条(实际上并没有着色)
标签: python opengl buffer redraw