对象上的OpenGL纹理对齐

oyxsuwqo  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(151)

对象上的纹理对齐有问题。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文件时纹理坐标的加载方式有一些问题,但是纹理似乎没有拉伸,只是没有对齐。
我试着在blender中打开对象,检查UV,甚至重新导出模型。我也试着旋转图像,想也许一些轴被交换了,但这也没有帮助。

5sxhfpxr

5sxhfpxr1#

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

相关问题