我正在尝试使用 pyopengl 编写一个基本的 3d 游戏,但是尝试向立方体添加纹理时我遇到了纹理映射问题。 纹理在表面上奇怪地交换和拉伸
我的代码:
def createtexturedcube(x, y, z, sx, sy, sz, texture_id): # sx = size_x ,sy = size_y ...
fx = x + sx
fy = y + sy
fz = z + sz
vertices = (
(fz, y, x), (fz, fy, x), (z, fy, x), (z, y, x),
(fz, y, fx), (fz, fy, fx), (z, y, fx), (z, fy, fx)
)
tex_coords = [
(-1, 1), (1,1), (0, 1), (1, 0),
]
planes = ( (1, 2, 7, 5),(1, 0, 4, 5), (7, 6, 3, 2), (6, 4, 5, 7),(0, 1, 2, 3),(6, 4, 0, 3))
for i, plane in enumerate(planes):
glBindTexture(GL_TEXTURE_2D, texture_id)
GL.glBegin(GL.GL_QUADS)
for j, vertex in enumerate(plane):
tc=tex_coords[j]
GL.glTexCoord2f(*tc)
ve=vertices[vertex]
GL.glVertex3fv(ve)
GL.glEnd()
def load_texture(filename):
global texturesStorage
img = Image.open(filename).convert("RGB")
img_data = numpy.array(list(img.getdata()), numpy.int8)
texturesStorage+=[img_data]
textID = glGenTextures(1,)
print("id:",textID)
glBindTexture(GL_TEXTURE_2D, textID)
#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
渲染纹理:
我已经尝试过更改 textureCoords ('tex_coords') 但大多数时候拉伸只是被放大了。
图像最终应该是这样的:
我知道纹理坐标不正确,但我找不到解决方法。
有人可以解释为什么纹理渲染不正确吗?
纹理坐标错误。纹理坐标应在 (0, 0) 到 (1, 1) 的范围内:
tex_coords = [(-1, 1), (1,1), (0, 1), (1, 0),]
tex_coords = [
(0, 0), (1,1), (0, 1), (1, 0),
]