我正在使用python和OpenGL和pyGame制作Rubik的多维数据集应用程序。我可以看到该多维数据集,并且我想按一个按钮并加载颜色,但是没有显示颜色。 (我单独给每个小方块上色,但我认为这并不重要)。
如果我在绘制多维数据集顶点的函数中调用了为多维数据集着色的函数(在程序开始时),则可以完美显示颜色,但是如果我按下一个键调用了color函数(使用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语句为true)。另外,多维数据集是一个3x3x3的numpy数组,里面充满了立方体对象。
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(...)不这样做吗?
答案 0 :(得分:1)
new_cube.rotate_cube("c")
仅在按下该键时执行一次。这导致颜色仅短暂闪烁。
必须将状态(self.colored
添加到类Cube
中,该状态指示是否要绘制彩色立方体或线条。调用new_cube.color_cubie
时,变量的更改状态:
例如
class Cube:
def __init__(self):
self.colored = False
# [...]
def color_cubie(self):
self.colored = True # change state, from now on draw colored
def show_cube(self)
if self.colored:
self.drawColored()
else:
self.drawLines()
def drawLines(slef):
# draw the lines here
# [...]
def drawColored(self):
# draw the colored cube here
# [...]