如何用glutKeyboardFunc(keyboard)放一个动作?

问题描述 投票:1回答:1

这些是我的代码。

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'))我想把图片改成 glBindTexture(GL_TEXTURE_2D, loadTexture('persona.jpg')) 键盘操作,按 p 按钮,代码使用的是 def keyboard(key, x, y):你能帮我解决这个问题吗?

python opengl glut pyopengl opengl-compat
1个回答
1
投票

OpenGL是一个状态引擎。一个状态会被保留,直到它再次被改变。

添加变量d tob_batik, tob_personatob_current 在 globale 命名空间中。

tob_batik = None
tob_persona = None
tob_current = None

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

在调用globale命名空间之前加载2个纹理 glutMainLoop().

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

绑定 tob_currentgambarMeja:

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

    glBindTexture(GL_TEXTURE_2D, tob_current)

    # [...]
© www.soinside.com 2019 - 2024. All rights reserved.