在pygame python中的第二个和第三个立方体上插入其他图像

问题描述

对不起,我想问一些我卡在其中的问题。我有 3 个立方体代表每个图像 example ,但是当我声明其他图像时,例如
纹理表面 = pygame.image.load('images1.bmp','images2.bmp','images3.bmp') 图像仍然加载相同的图片,我试过 self.id 但我不能在我的代码中使用它,有人可以帮忙吗?谢谢。 这是我到目前为止已经尝试过的代码

#import
import pygame
import sys
from OpenGL.GL import *
from OpenGL.glu import *

display = (800,600)

def initialitation():
     glClearColor(1.0,1.0,1.0)   #Warna latar belakang putih
     glViewport(0,display[0],display[1])
     gluPerspective(45,display[0]/display[1],0.1,50.0)
     loadTexture()
     
def loadTexture():
    textureSurface = pygame.image.load('image1.bmp','iamges2.bmp','iamges3.bmp')

    textureData = pygame.image.tostring(textureSurface,"RGBA",1)
    width = textureSurface.get_width()
    height = textureSurface.get_height()

    glEnable(GL_TEXTURE_2D)
    texid = glGenTextures(1)

    glBindTexture(GL_TEXTURE_2D,texid)
    glTexImage2D(GL_TEXTURE_2D,GL_RGB,width,height,GL_RGBA,GL_UNSIGNED_BYTE,textureData)

    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)

    return texid


def cube_image(lines=False):
    if lines:
        glBegin(GL_LInes)
        for edge in edges:
            glColor3fv((1,1,1))
            for vertex in edge:
                glVertex3fv(vertices[vertex])
        glEnd()
    else:
        #front
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,1.0); glVertex3f(-0.5,-0.5,0.5);
        glTexCoord2f(1.0,1.0); glVertex3f( 0.5,0.0); glVertex3f( 0.5,0.5,0.5);
        glTexCoord2f(0.0,0.0); glVertex3f(-0.5,0.5);
        glEnd();
        
        #back
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,-0.5);
        glTexCoord2f(1.0,-0.5);
        glTexCoord2f(0.0,-0.5);
        glEnd();
        
        #left
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,-0.5);
        glEnd();
        
        #right
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,0.5);
        glEnd();
        
        #upper
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,-0.5);
        glEnd();
        
        #below
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,0.5);
        glEnd();

def myImage():
    glPushmatrix()
    glTranslated(1,0)
    cube_image(0)
    glPopMatrix()

    glPushmatrix()
    glTranslated(-1,0)
    cube_image(0)
    glPopMatrix()
    
    glPushmatrix()
    glTranslated(0,0)
    cube_image(0)
    glPopMatrix()
            
def main():
    pygame.init()
    pygame.display.set_mode(display,pygame.OPENGL|pygame.DOUBLEBUF)
    pygame.display.set_caption('Cube and Texture')

    initialitation()
    glTranslatef(0.0,0.0,-5)

    while True:
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        #glrotatef(1,1)
        glEnable( GL_TEXTURE_2D )
        glEnable( GL_DEPTH_TEST )
        
        glPushmatrix()
        glrotatef(30,0)
        myImage()
        glPopMatrix()

        pygame.display.flip()
        pygame.time.wait(10)
    
#if python says run,let's run!
if __name__ == '__main__':
    main()

解决方法

pygame.image.load 仅加载 1 张图像。如果要使用多个纹理,则必须为每个图像创建一个单独的纹理对象。在绘制四边形之前立即绑定纹理。

编写一个加载 1 个图像并创建 1 个纹理对象的函数:

def loadTexture(imagename):
    textureSurface = pygame.image.load(imagename)

    # [...] rest of your code

    return texid

将纹理对象存储在全局命名空间中的数组中(参见global statement):

texture_objects = []

def initialitation():
     global texture_objects 

     glClearColor(1.0,1.0,1.0)   #Warna latar belakang putih
     glViewport(0,display[0],display[1])
     gluPerspective(45,display[0]/display[1],0.1,50.0)

     texture_objects.append(loadTexture('image1.bmp'))
     texture_objects.append(loadTexture('image2.bmp'))
     texture_objects.append(loadTexture('image3.bmp'))

在绘制四边形之前,绑定你想要的纹理对象。举个例子:

def cube_image(lines=False):
    if lines:
        glDisable(GL_TEXTURE_2D)

        glBegin(GL_LINES)
        for edge in edges:
            glColor3fv((1,1,1))
            for vertex in edge:
                glVertex3fv(vertices[vertex])
        glEnd()

    else:
        glEnable(GL_TEXTURE_2D)

        #front
        glBindTexture(GL_TEXTURE_2D,texture_objects[0])
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,1.0); glVertex3f(-0.5,-0.5,0.5);
        glTexCoord2f(1.0,1.0); glVertex3f( 0.5,0.0); glVertex3f( 0.5,0.5,0.5);
        glTexCoord2f(0.0,0.0); glVertex3f(-0.5,0.5);
        glEnd();
        
        #back
        glBindTexture(GL_TEXTURE_2D,texture_objects[1])
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,-0.5);
        glTexCoord2f(1.0,-0.5);
        glTexCoord2f(0.0,-0.5);
        glEnd();
        
        #left
        glBindTexture(GL_TEXTURE_2D,texture_objects[2])
        glBegin(GL_QUADS)
        glTexCoord2f(0.0,-0.5);
        glEnd();
        
        #right
        glBindTexture(GL_TEXTURE_2D,0.5);
        glEnd();
        
        #upper
        glBindTexture(GL_TEXTURE_2D,-0.5);
        glEnd();
        
        #below
        glBindTexture(GL_TEXTURE_2D,0.5);
        glEnd();