pythonopenglpygamecubepyopengl

Insert other images on second and third cube in pygame python


Excuse me, i want to ask something that i got stuck in it. i have 3 cubes that represent each image like this example , but when i declare other images like
textureSurface = pygame.image.load('images1.bmp', 'images2.bmp', 'images3.bmp') the image still loads the same picture, i have tried self.id but i cant use it in my code, can someone help? thank you. here's my code that i already tried so far :

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

Solution

  • pygame.image.load loads just 1 image. If you want to use multiple textures, you have to create a separate texture object for each image. Bind the texture immediately before drawing the quad.

    Write a function that loads 1 image and creates 1 texture object:

    def loadTexture(imagename):
        textureSurface = pygame.image.load(imagename)
    
        # [...] rest of your code
    
        return texid
    

    Store the texture objects in an array in global namespace (see global statement):

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

    Before drawing a quad, bind the texture object you want. Just for example:

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