对象上的 OpenGL 纹理对齐

OpenGL texture alignment on objects

我对对象的纹理对齐有疑问。 This is what the image looks like.

我一直在关注 PyOpengl 上的这个 tutorial

这是我加载纹理的方式:

self.texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, self.texture)
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

    image = pg.image.load(filepath).convert()
    image_width,image_height = image.get_rect().size
    img_data = pg.image.tostring(image,'RGBA')
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,image_width,image_height,0,GL_RGBA,GL_UNSIGNED_BYTE,img_data)

这是我加载 OBJ 文件的方式:

v = []
    vt = []
    vn = []
    
    #final, assembled and packed result
    vertices = []

    #open the obj file and read the data
    with open(filename,'r') as f:
        line = f.readline()
        while line:
            firstSpace = line.find(" ")
            flag = line[0:firstSpace]
            if flag=="v":
                #vertex
                line = line.replace("v ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                v.append(l)
            elif flag=="vt":
                #texture coordinate
                line = line.replace("vt ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                vt.append(l)
            elif flag=="vn":
                #normal
                line = line.replace("vn ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                vn.append(l)
            elif flag=="f":
                #face, three or more vertices in v/vt/vn form
                line = line.replace("f ","")
                line = line.replace("\n","")
                #get the individual vertices for each line
                line = line.split(" ")
                faceVertices = []
                faceTextures = []
                faceNormals = []
                for vertex in line:
                    #break out into [v,vt,vn],
                    #correct for 0 based indexing.
                    l = vertex.split("/")
                    position = int(l[0]) - 1
                    faceVertices.append(v[position])
                    texture = int(l[1]) - 1
                    faceTextures.append(vt[texture])
                    normal = int(l[2]) - 1
                    faceNormals.append(vn[normal])
                # obj file uses triangle fan format for each face individually.
                # unpack each face
                triangles_in_face = len(line) - 2

                vertex_order = []
                """
                    eg. 0,1,2,3 unpacks to vertices: [0,1,2,0,2,3]
                """
                for i in range(triangles_in_face):
                    vertex_order.append(0)
                    vertex_order.append(i+1)
                    vertex_order.append(i+2)
                for i in vertex_order:
                    for x in faceVertices[i]:
                        vertices.append(x)
                    for x in faceTextures[i]:
                        vertices.append(x)
                    for x in faceNormals[i]:
                        vertices.append(x)
            line = f.readline()
    return vertices

我不确定为什么加载不正确。我唯一的想法是在读取 OBJ 文件时加载纹理坐标的方式存在一些问题,但纹理似乎并没有拉伸只是未对齐。

我尝试在搅拌机中打开对象并检查 UV,甚至重新导出模型。我也尝试过旋转图像,认为可能交换了一些轴,但这也没有帮助。

大多数图像格式都认为数据是按光栅顺序从上到下写入的。但是 GL 的纹理函数期望底行首先出现在内存中。看起来您的图像加载例程没有垂直翻转图像,您的 OBJ 导入器也没有沿 y 轴反转纹理坐标。