对不起,我想问一些我被困住的事情。我有 3 个立方体来代表每个图像,如下所示
,但是当我声明其他图像时,例如
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()
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();