对不起,我想问一件事,我被卡住了。我有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()
字符集
1条答案
按热度按时间rryofs0p1#
pygame.image.load
只加载1个图像。如果要使用多个纹理,则必须为每个图像创建单独的纹理对象。在绘制四边形之前立即绑定纹理。编写一个函数,加载1张图像并创建1个纹理对象:
字符集
将纹理对象存储在全局命名空间的数组中(参见
global
语句):型
在绘制四边形之前,请绑定所需的纹理对象。举个例子:
型