I'm trying to write a basic 3d game with pyopengl but trying to add textures to the cubes i ran into problems with the texture-maping. The texture is weirdly swapped and stretched across the surface
My Code :
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
The rendered texture:
I already tried changing the textureCoords ('tex_coords') but most times the stretching just gets amplified.
The Image should in the end look like this:
I understand that somehow the texture coordinates are incorrect but I could not find out how to fix it.
Could someone explain why the texture is not rendering properly?