Ellie
Ellie

Reputation: 45

How to put an action with glutKeyboardFunc(keyboard)?

These are my code :

import sys
    import pygame

    teapotList = None

    def loadTexture(gambar):
        textureSurface = pygame.image.load(gambar)\
        ...

    def gambarMeja():
     glDisable(GL_LIGHTING)
     glEnable(GL_TEXTURE_2D)
     glBindTexture(GL_TEXTURE_2D, loadTexture('Batik.jpg'))
     glPushMatrix()
     glTranslatef(15.0, 5.0, 1.0)
     glRotatef(-15, 0, 1, 0)
     glRotatef(20, 1, 0, 0)
     glBegin(GL_QUADS)
     ..

    def mejaTV():
        gambarMeja()    

    def display():
        ..
        mejaTV()
        gambarLemari()

    def keyboard(key, x, y):
        if key == chr(27):
            sys.exit()

    # Main Loop
    if __name__ == "__main__":
        ..
        glutDisplayFunc(display)
        glutKeyboardFunc(keyboard)
        glutMainLoop()

i want to change my picture with an action for this code : glBindTexture(GL_TEXTURE_2D, loadTexture('Batik.jpg')) i want to change the picture to glBindTexture(GL_TEXTURE_2D, loadTexture('persona.jpg')) with action of keyboard by pressing p button. the code is using def keyboard(key, x, y): can you help me to solve this issue?

Upvotes: 1

Views: 454

Answers (1)

Rabbid76
Rabbid76

Reputation: 210918

OpenGL is a state engine. A state is kept until it is changed again.

Add the variabled tob_batik, tob_persona and tob_current in globale namespace:

tob_batik = None
tob_persona = None
tob_current = None

def loadTexture(gambar):
    textureSurface = pygame.image.load(gambar)
    # [...]

Load the 2 textures before the call of glutMainLoop().

# Main Loop
if __name__ == "__main__":
    # [...]

    glutKeyboardFunc(keyboard)

    tob_batik = loadTexture('Batik.jpg')
    tob_persona = loadTexture('persona.jpg')
    tob_current = tob_batik
    glutMainLoop()

Change tob_current, dependent on the pressed key (b or p)

def keyboard(key, x, y):
    global tob_current

    if key == chr(27):
        sys.exit()

    elif key == b'b':
        tob_current = tob_batik  

    elif key == b'p':
        tob_current = tob_persona

    glutPostRedisplay()

Bind tob_current in gambarMeja:

def gambarMeja():
    glDisable(GL_LIGHTING)
    glEnable(GL_TEXTURE_2D)

    glBindTexture(GL_TEXTURE_2D, tob_current)

    # [...]

Upvotes: 1

Related Questions