如何使用 glutKeyboardFunc(keyboard) 执行操作?

How to put an action with glutKeyboardFunc(keyboard)?

这些是我的代码:

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

我想用一个动作来改变我的照片 对于此代码:glBindTexture(GL_TEXTURE_2D, loadTexture('Batik.jpg')) 我想通过按 p 按钮通过键盘操作将图片更改为 glBindTexture(GL_TEXTURE_2D, loadTexture('persona.jpg'))。代码正在使用 def keyboard(key, x, y): 你能帮我解决这个问题吗?

OpenGL 是一个状态引擎。状态一直保持到再次更改。

在全局命名空间中添加变量 tob_batiktob_personatob_current

tob_batik = None
tob_persona = None
tob_current = None

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

在调用 glutMainLoop() 之前加载 2 个纹理。

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

    glutKeyboardFunc(keyboard)

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

更改 tob_current,取决于按下的键(bp

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

gambarMeja中绑定tob_current:

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

    glBindTexture(GL_TEXTURE_2D, tob_current)

    # [...]