2

I'm using python to make a Rubik's cube application with OpenGL and pyGame. I can see the cube and I want to press a button and load the colors, but colors are not shown. (I color each little cube alone but I don't think that it matters).

If I call the function that colors the cube in the function which draws the vertexes for the cube (at the start of the program), the colors are perfectly shown, but if i call the color function with the press of a key (using pyGame) while the application is already running they don't.

the function that colors a little cube:

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()

the part that I call the function (the if statement is true when i press c key in keyboard). Also cube is a 3x3x3 numpy array which is filled with cubie objects.

if move == "c":
    for row in self.cube[2]:
        for cubie in row:
            cubie.color_cubie()

the main/ render function:


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")

As I read i must redraw the screen in order to see the colored cube. But glClear(...) doesn't do that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jimmy
  • 33
  • 4
  • Maybe you'll find some help there: [How to rotate slices of a Rubik's Cube in python PyOpenGL?](https://stackoverflow.com/questions/50303616/how-to-rotate-slices-of-a-rubiks-cube-in-python-pyopengl/54953213#54953213) – Rabbid76 Apr 17 '19 at 19:42
  • 1
    *"but if i call the color function with the press of a key (using pyGame) while the application is already running they don't."* - What does it mean *"... they don't."*? I assume that `new_cube.show_cube()` draws the cube. So what is `color_cubie` assumed to do? Draw the same cube again? The question is almost unclear. Please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Rabbid76 Apr 17 '19 at 20:02
  • it helped a little bit. I added pygame.display.flip() and pygame.time.wait(10) under the point that i call the function to color the cube and the colors flash for a little – Jimmy Apr 17 '19 at 20:04
  • "but if i call the color function with the press of a key (using pyGame) while the application is already running they don't." - I meant that if i call the function when the application is already running the colors don't show up. new_cube.show_cube() draws the lines of the cube (it doesn't actually color it) – Jimmy Apr 17 '19 at 20:07

1 Answers1

1

new_cube.rotate_cube("c") is only executed once, when the key is pressed. This causes that the colors only flash for a short moment.

You have to add a state (self.colored) to the class Cube, which indicates if the colored cube or the lines hav to be drawn. When new_cube.color_cubie is called the change th state of the variable:

e.g.

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
         # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • no...it doesn't work. It still flashes the colors. (Maybe i must do something with openGL buffers?) – Jimmy Apr 17 '19 at 20:36