I was adding textures to a cube but kept running into this issue
I don't really know why this is happening and was wondering if anyone else could help.
Here are some of my code snippets:
Function to generate cube vertices.
def cube_vertices(x, y, z, n):
""" Return the vertices of the cube at position x, y, z with size 2*n."""
return [
[x+n,y-n,z-n], [x+n,y+n,z-n], [x-n,y+n,z-n], [x-n,y-n,z-n],
[x+n,y-n,z+n], [x+n,y+n,z+n], [x-n,y-n,z+n], [x-n,y+n,z+n],
]
Function for generating a cube.
def Cube(cubeverts):
glEnable(GL_TEXTURE_2D)
glBegin(GL_QUADS)
for surf in surfs:
for vertex in surf:
glTexCoord2f(0.0,1.0)
glVertex3fv(cubeverts[vertex])
glEnd()
and a function to make the texture:
def get_texture(texturename,width=16,height=16):
img = Image.open(texturename)
img_data = numpy.array(list(img.getdata()), numpy.int8)
textID = glGenTextures(1)
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, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data)
return textID
I was able to fix this by adding
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
to the for vertex in surf loop
Final cube function:
def Cube(cubeverts):
glBegin(GL_QUADS)
for surf in surfs:
n = 0
for vertex in surf:
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(cubeverts[vertex])
n += 1
glEnd()