-1

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:

expected texture

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?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257

1 Answers1

0

The texture coordinates are wrong. The texture coordinates should be in the range from (0, 0) to (1, 1):

tex_coords = [(-1, 1), (1,1), (0, 1), (1, 0),]

tex_coords = [
    (0, 0), (0, 1), (1, 1), (1, 0),
]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • it's still the same problem but the way the texure warped changed – PackTheCommand Apr 23 '23 at 15:00
  • what I mean is, I already tried nearly all possible combinations of 1's and 0's, and it always results in the texture being unrecognizable, or only one side of the texture (triangle) looking like it should, and the other one being the mirrored version but warped. Could it be that the problem has something to do with using 'pyopengltk' for the frame? – PackTheCommand Apr 23 '23 at 15:12
  • @PackTheCommand No. The problem is on your side. You just need to find the right order. There is a bug with your coordinates. Of course, 1 set of texture coordinates works only if all 6 planes are drawn with the same vertex order. – Rabbid76 Apr 23 '23 at 15:26
  • I now fixed the problem by using 'GL.GL_TRIANGLES' instead of 'GL.GL_QUADS' and now just rendering the two tryingles of the texture independently instead of reandering a Quardrat – PackTheCommand May 11 '23 at 20:30