如何将纹理应用于一组顶点?我无法弄清楚我做错了什么。
我找不到关于这个主题的任何教程或任何内容,所以我大部分时间都没有找到我发现的OpenGL文档和随机脚本,所以我的代码可能没有任何意义。我的脚本有点太大了,无法在此发布,大部分内容都不相关。
我为纹理设置了:
def read_texture(filename):
img = Image.open(filename)
img_data = numpy.array(list(img.getdata()), numpy.int8)
textID = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
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)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
return textID
我加载了texture_0 = read_texture('texture.png')
。
我对立方体的设置:
生成立方体的顶点
def gen_rect_prism_vertices(xloc,yloc,zloc,x,y,z):
x *= 0.5
y *= 0.5
z *= 0.5
vertices = (
(xloc+x, yloc-y, zloc-z),
(xloc+x, yloc+y, zloc-z),
(xloc-x, yloc+y, zloc-z),
(xloc-x, yloc-y, zloc-z),
(xloc+x, yloc-y, zloc+z),
(xloc+x, yloc+y, zloc+z),
(xloc-x, yloc-y, zloc+z),
(xloc-x, yloc+y, zloc+z)
)
return vertices
定义表面
rect_prism_surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6)
)
主要功能
def block(xloc,yloc,zloc,x,y,z):
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D,texture_0)
vertices = gen_rect_prism_vertices(xloc,yloc,zloc,x,y,z)
for surface in rect_prism_surfaces:
n = 0
glBegin(GL_QUADS)
for vertex in surface:
if n == 0:
xv = 0.0
yv = 0.0
if n == 1:
xv = 1.0
yv = 0.0
if n == 2:
xv = 1.0
yv = 1.0
if n == 3:
xv = 0.0
yv = 1.0
glTexCoord2f(xv,yv); glVertex3fv(vertices[vertex])
n += 1
glEnd()
答案 0 :(得分:0)
@TEB在评论中回答。纹理需要在生成纹理ID的glBindTexture(GL_TEXTURE_2D,texture_0)
中绑定(使用read_texture()
)。