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

brqmpdu1  于 2023-08-04  发布在  Python
关注(0)|答案(1)|浏览(121)

对不起,我想问一件事,我被卡住了。我有3个立方体来表示每个图像像这样example,但是当我声明其他图像像
textureSurface = 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, 1.0)   #Warna latar belakang putih
     glViewport(0,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, 0, GL_RGB, width, height, 0, 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_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, 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.5,  0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5,  0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,  0.5,  0.5);
        glEnd();
        
        #back
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f(-0.5,  0.5, -0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, -0.5, -0.5);
        glEnd();
        
        #left
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5,  0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f(-0.5,  0.5,  0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f(-0.5,  0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, -0.5);
        glEnd();
        
        #right
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f( 0.5, -0.5, -0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f( 0.5,  0.5, -0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5,  0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, -0.5,  0.5);
        glEnd();
        
        #upper
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,  0.5,  0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f( 0.5,  0.5,  0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,  0.5, -0.5);
        glEnd();
        
        #below
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5,  0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5, -0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, -0.5,  0.5);
        glEnd();

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

    glPushMatrix()
    glTranslated(-1,0,0)
    cube_image(0)
    glPopMatrix()
    
    glPushMatrix()
    glTranslated(0,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, 0, 0, 1)
        glEnable( GL_TEXTURE_2D )
        glEnable( GL_DEPTH_TEST )
        
        glPushMatrix()
        glRotatef(30, 1, 1, 0)
        myImage()
        glPopMatrix()

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

字符集

rryofs0p

rryofs0p1#

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

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

    # [...] rest of your code

    return texid

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

texture_objects = []

def initialitation():
     global texture_objects 

     glClearColor(1.0, 1.0, 1.0, 1.0)   #Warna latar belakang putih
     glViewport(0,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.5,  0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5,  0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,  0.5,  0.5);
        glEnd();
        
        #back
        glBindTexture(GL_TEXTURE_2D, texture_objects[1])
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f(-0.5,  0.5, -0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, -0.5, -0.5);
        glEnd();
        
        #left
        glBindTexture(GL_TEXTURE_2D, texture_objects[2])
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5,  0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f(-0.5,  0.5,  0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f(-0.5,  0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, -0.5);
        glEnd();
        
        #right
        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.5, -0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5,  0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, -0.5,  0.5);
        glEnd();
        
        #upper
        glBindTexture(GL_TEXTURE_2D, texture_objects[1])
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,  0.5,  0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f( 0.5,  0.5,  0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5,  0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,  0.5, -0.5);
        glEnd();
        
        #below
        glBindTexture(GL_TEXTURE_2D, texture_objects[2])
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5,  0.5);
        glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
        glTexCoord2f(1.0, 0.0); glVertex3f( 0.5, -0.5, -0.5);
        glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, -0.5,  0.5);
        glEnd();

相关问题