如何使用glutKeyboardFunc(keyboard)进行操作? #pyopengl

问题描述 投票:0回答: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__":
    ..
    glutKeyboardFunc(keyboard)
    glutMainLoop()

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

pyopengl geany
1个回答
0
投票

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):
    if key == chr(27):
        sys.exit()

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

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

tob_current中绑定gambarMeja

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

    glBindTexture(GL_TEXTURE_2D, tob_current)

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